session.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. /**
  3. * Session model.
  4. */
  5. const inspect = Symbol.for('nodejs.util.inspect.custom');
  6. class Session {
  7. /**
  8. * Session constructor
  9. * @param {Context} ctx
  10. * @param {Object} obj
  11. * @api private
  12. */
  13. constructor(sessionContext, obj, externalKey) {
  14. this._sessCtx = sessionContext;
  15. this._ctx = sessionContext.ctx;
  16. this._externalKey = externalKey;
  17. if (!obj) {
  18. this.isNew = true;
  19. } else {
  20. for (const k in obj) {
  21. // restore maxAge from store
  22. if (k === '_maxAge') this._ctx.sessionOptions.maxAge = obj._maxAge;
  23. else if (k === '_session') this._ctx.sessionOptions.maxAge = 'session';
  24. else this[k] = obj[k];
  25. }
  26. }
  27. }
  28. /**
  29. * JSON representation of the session.
  30. *
  31. * @return {Object}
  32. * @api public
  33. */
  34. toJSON() {
  35. const obj = {};
  36. Object.keys(this).forEach(key => {
  37. if (key === 'isNew') return;
  38. if (key[0] === '_') return;
  39. obj[key] = this[key];
  40. });
  41. return obj;
  42. }
  43. /**
  44. *
  45. * alias to `toJSON`
  46. * @api public
  47. */
  48. [inspect]() {
  49. return this.toJSON();
  50. }
  51. /**
  52. * Return how many values there are in the session object.
  53. * Used to see if it's "populated".
  54. *
  55. * @return {Number}
  56. * @api public
  57. */
  58. get length() {
  59. return Object.keys(this.toJSON()).length;
  60. }
  61. /**
  62. * populated flag, which is just a boolean alias of .length.
  63. *
  64. * @return {Boolean}
  65. * @api public
  66. */
  67. get populated() {
  68. return !!this.length;
  69. }
  70. /**
  71. * get session maxAge
  72. *
  73. * @return {Number}
  74. * @api public
  75. */
  76. get maxAge() {
  77. return this._ctx.sessionOptions.maxAge;
  78. }
  79. /**
  80. * set session maxAge
  81. *
  82. * @param {Number}
  83. * @api public
  84. */
  85. set maxAge(val) {
  86. this._ctx.sessionOptions.maxAge = val;
  87. // maxAge changed, must save to cookie and store
  88. this._requireSave = true;
  89. }
  90. /**
  91. * get session external key
  92. * only exist if opts.store present
  93. */
  94. get externalKey() {
  95. return this._externalKey;
  96. }
  97. /**
  98. * save this session no matter whether it is populated
  99. *
  100. * @api public
  101. */
  102. save() {
  103. this._requireSave = true;
  104. }
  105. /**
  106. * commit this session's headers if autoCommit is set to false
  107. *
  108. * @api public
  109. */
  110. async manuallyCommit() {
  111. await this._sessCtx.commit();
  112. }
  113. }
  114. module.exports = Session;