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.

84 lines
1.7 KiB

  1. /*!
  2. * Connect - session - Store
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * MIT Licensed
  6. */
  7. /**
  8. * Module dependencies.
  9. */
  10. var EventEmitter = require('events').EventEmitter
  11. , Session = require('./session')
  12. , Cookie = require('./cookie');
  13. /**
  14. * Initialize abstract `Store`.
  15. *
  16. * @api private
  17. */
  18. var Store = module.exports = function Store(options){};
  19. /**
  20. * Inherit from `EventEmitter.prototype`.
  21. */
  22. Store.prototype.__proto__ = EventEmitter.prototype;
  23. /**
  24. * Re-generate the given requests's session.
  25. *
  26. * @param {IncomingRequest} req
  27. * @return {Function} fn
  28. * @api public
  29. */
  30. Store.prototype.regenerate = function(req, fn){
  31. var self = this;
  32. this.destroy(req.sessionID, function(err){
  33. self.generate(req);
  34. fn(err);
  35. });
  36. };
  37. /**
  38. * Load a `Session` instance via the given `sid`
  39. * and invoke the callback `fn(err, sess)`.
  40. *
  41. * @param {String} sid
  42. * @param {Function} fn
  43. * @api public
  44. */
  45. Store.prototype.load = function(sid, fn){
  46. var self = this;
  47. this.get(sid, function(err, sess){
  48. if (err) return fn(err);
  49. if (!sess) return fn();
  50. var req = { sessionID: sid, sessionStore: self };
  51. sess = self.createSession(req, sess);
  52. fn(null, sess);
  53. });
  54. };
  55. /**
  56. * Create session from JSON `sess` data.
  57. *
  58. * @param {IncomingRequest} req
  59. * @param {Object} sess
  60. * @return {Session}
  61. * @api private
  62. */
  63. Store.prototype.createSession = function(req, sess){
  64. var expires = sess.cookie.expires
  65. , orig = sess.cookie.originalMaxAge;
  66. sess.cookie = new Cookie(sess.cookie);
  67. if ('string' == typeof expires) sess.cookie.expires = new Date(expires);
  68. sess.cookie.originalMaxAge = orig;
  69. req.session = new Session(req, sess);
  70. return req.session;
  71. };