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.

memory.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*!
  2. * express-session
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * Copyright(c) 2015 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. 'use strict';
  9. /**
  10. * Module dependencies.
  11. * @private
  12. */
  13. var Store = require('./store')
  14. var util = require('util')
  15. /**
  16. * Shim setImmediate for node.js < 0.10
  17. * @private
  18. */
  19. /* istanbul ignore next */
  20. var defer = typeof setImmediate === 'function'
  21. ? setImmediate
  22. : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }
  23. /**
  24. * Module exports.
  25. */
  26. module.exports = MemoryStore
  27. /**
  28. * A session store in memory.
  29. * @public
  30. */
  31. function MemoryStore() {
  32. Store.call(this)
  33. this.sessions = Object.create(null)
  34. }
  35. /**
  36. * Inherit from Store.
  37. */
  38. util.inherits(MemoryStore, Store)
  39. /**
  40. * Get all active sessions.
  41. *
  42. * @param {function} callback
  43. * @public
  44. */
  45. MemoryStore.prototype.all = function all(callback) {
  46. var sessionIds = Object.keys(this.sessions)
  47. var sessions = Object.create(null)
  48. for (var i = 0; i < sessionIds.length; i++) {
  49. var sessionId = sessionIds[i]
  50. var session = getSession.call(this, sessionId)
  51. if (session) {
  52. sessions[sessionId] = session;
  53. }
  54. }
  55. callback && defer(callback, null, sessions)
  56. }
  57. /**
  58. * Clear all sessions.
  59. *
  60. * @param {function} callback
  61. * @public
  62. */
  63. MemoryStore.prototype.clear = function clear(callback) {
  64. this.sessions = Object.create(null)
  65. callback && defer(callback)
  66. }
  67. /**
  68. * Destroy the session associated with the given session ID.
  69. *
  70. * @param {string} sessionId
  71. * @public
  72. */
  73. MemoryStore.prototype.destroy = function destroy(sessionId, callback) {
  74. delete this.sessions[sessionId]
  75. callback && defer(callback)
  76. }
  77. /**
  78. * Fetch session by the given session ID.
  79. *
  80. * @param {string} sessionId
  81. * @param {function} callback
  82. * @public
  83. */
  84. MemoryStore.prototype.get = function get(sessionId, callback) {
  85. defer(callback, null, getSession.call(this, sessionId))
  86. }
  87. /**
  88. * Commit the given session associated with the given sessionId to the store.
  89. *
  90. * @param {string} sessionId
  91. * @param {object} session
  92. * @param {function} callback
  93. * @public
  94. */
  95. MemoryStore.prototype.set = function set(sessionId, session, callback) {
  96. this.sessions[sessionId] = JSON.stringify(session)
  97. callback && defer(callback)
  98. }
  99. /**
  100. * Get number of active sessions.
  101. *
  102. * @param {function} callback
  103. * @public
  104. */
  105. MemoryStore.prototype.length = function length(callback) {
  106. this.all(function (err, sessions) {
  107. if (err) return callback(err)
  108. callback(null, Object.keys(sessions).length)
  109. })
  110. }
  111. /**
  112. * Touch the given session object associated with the given session ID.
  113. *
  114. * @param {string} sessionId
  115. * @param {object} session
  116. * @param {function} callback
  117. * @public
  118. */
  119. MemoryStore.prototype.touch = function touch(sessionId, session, callback) {
  120. var currentSession = getSession.call(this, sessionId)
  121. if (currentSession) {
  122. // update expiration
  123. currentSession.cookie = session.cookie
  124. this.sessions[sessionId] = JSON.stringify(currentSession)
  125. }
  126. callback && defer(callback)
  127. }
  128. /**
  129. * Get session from the store.
  130. * @private
  131. */
  132. function getSession(sessionId) {
  133. var sess = this.sessions[sessionId]
  134. if (!sess) {
  135. return
  136. }
  137. // parse
  138. sess = JSON.parse(sess)
  139. if (sess.cookie) {
  140. var expires = typeof sess.cookie.expires === 'string'
  141. ? new Date(sess.cookie.expires)
  142. : sess.cookie.expires
  143. // destroy expired session
  144. if (expires && expires <= Date.now()) {
  145. delete this.sessions[sessionId]
  146. return
  147. }
  148. }
  149. return sess
  150. }