Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cookie.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*!
  2. * Connect - session - Cookie
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * MIT Licensed
  6. */
  7. 'use strict';
  8. /**
  9. * Module dependencies.
  10. */
  11. var merge = require('utils-merge')
  12. , cookie = require('cookie');
  13. /**
  14. * Initialize a new `Cookie` with the given `options`.
  15. *
  16. * @param {IncomingMessage} req
  17. * @param {Object} options
  18. * @api private
  19. */
  20. var Cookie = module.exports = function Cookie(options) {
  21. this.path = '/';
  22. this.maxAge = null;
  23. this.httpOnly = true;
  24. if (options) merge(this, options);
  25. this.originalMaxAge = undefined == this.originalMaxAge
  26. ? this.maxAge
  27. : this.originalMaxAge;
  28. };
  29. /*!
  30. * Prototype.
  31. */
  32. Cookie.prototype = {
  33. /**
  34. * Set expires `date`.
  35. *
  36. * @param {Date} date
  37. * @api public
  38. */
  39. set expires(date) {
  40. this._expires = date;
  41. this.originalMaxAge = this.maxAge;
  42. },
  43. /**
  44. * Get expires `date`.
  45. *
  46. * @return {Date}
  47. * @api public
  48. */
  49. get expires() {
  50. return this._expires;
  51. },
  52. /**
  53. * Set expires via max-age in `ms`.
  54. *
  55. * @param {Number} ms
  56. * @api public
  57. */
  58. set maxAge(ms) {
  59. this.expires = 'number' == typeof ms
  60. ? new Date(Date.now() + ms)
  61. : ms;
  62. },
  63. /**
  64. * Get expires max-age in `ms`.
  65. *
  66. * @return {Number}
  67. * @api public
  68. */
  69. get maxAge() {
  70. return this.expires instanceof Date
  71. ? this.expires.valueOf() - Date.now()
  72. : this.expires;
  73. },
  74. /**
  75. * Return cookie data object.
  76. *
  77. * @return {Object}
  78. * @api private
  79. */
  80. get data() {
  81. return {
  82. originalMaxAge: this.originalMaxAge
  83. , expires: this._expires
  84. , secure: this.secure
  85. , httpOnly: this.httpOnly
  86. , domain: this.domain
  87. , path: this.path
  88. , sameSite: this.sameSite
  89. }
  90. },
  91. /**
  92. * Return a serialized cookie string.
  93. *
  94. * @return {String}
  95. * @api public
  96. */
  97. serialize: function(name, val){
  98. return cookie.serialize(name, val, this.data);
  99. },
  100. /**
  101. * Return JSON representation of this cookie.
  102. *
  103. * @return {Object}
  104. * @api private
  105. */
  106. toJSON: function(){
  107. return this.data;
  108. }
  109. };