cookie.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 cookie = require('cookie')
  12. var deprecate = require('depd')('express-session')
  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) {
  25. if (typeof options !== 'object') {
  26. throw new TypeError('argument options must be a object')
  27. }
  28. for (var key in options) {
  29. if (key !== 'data') {
  30. this[key] = options[key]
  31. }
  32. }
  33. }
  34. if (this.originalMaxAge === undefined || this.originalMaxAge === null) {
  35. this.originalMaxAge = this.maxAge
  36. }
  37. };
  38. /*!
  39. * Prototype.
  40. */
  41. Cookie.prototype = {
  42. /**
  43. * Set expires `date`.
  44. *
  45. * @param {Date} date
  46. * @api public
  47. */
  48. set expires(date) {
  49. this._expires = date;
  50. this.originalMaxAge = this.maxAge;
  51. },
  52. /**
  53. * Get expires `date`.
  54. *
  55. * @return {Date}
  56. * @api public
  57. */
  58. get expires() {
  59. return this._expires;
  60. },
  61. /**
  62. * Set expires via max-age in `ms`.
  63. *
  64. * @param {Number} ms
  65. * @api public
  66. */
  67. set maxAge(ms) {
  68. if (ms && typeof ms !== 'number' && !(ms instanceof Date)) {
  69. throw new TypeError('maxAge must be a number or Date')
  70. }
  71. if (ms instanceof Date) {
  72. deprecate('maxAge as Date; pass number of milliseconds instead')
  73. }
  74. this.expires = typeof ms === 'number'
  75. ? new Date(Date.now() + ms)
  76. : ms;
  77. },
  78. /**
  79. * Get expires max-age in `ms`.
  80. *
  81. * @return {Number}
  82. * @api public
  83. */
  84. get maxAge() {
  85. return this.expires instanceof Date
  86. ? this.expires.valueOf() - Date.now()
  87. : this.expires;
  88. },
  89. /**
  90. * Return cookie data object.
  91. *
  92. * @return {Object}
  93. * @api private
  94. */
  95. get data() {
  96. return {
  97. originalMaxAge: this.originalMaxAge
  98. , expires: this._expires
  99. , secure: this.secure
  100. , httpOnly: this.httpOnly
  101. , domain: this.domain
  102. , path: this.path
  103. , sameSite: this.sameSite
  104. }
  105. },
  106. /**
  107. * Return a serialized cookie string.
  108. *
  109. * @return {String}
  110. * @api public
  111. */
  112. serialize: function(name, val){
  113. return cookie.serialize(name, val, this.data);
  114. },
  115. /**
  116. * Return JSON representation of this cookie.
  117. *
  118. * @return {Object}
  119. * @api private
  120. */
  121. toJSON: function(){
  122. return this.data;
  123. }
  124. };