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.

store.js 1.7KB

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