@plotdb/express-session 1.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@plotdb/express-session",
3
+ "version": "1.17.3",
4
+ "description": "Simple session middleware for Express ( PlotDB Forked version )",
5
+ "author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
6
+ "contributors": [
7
+ "Douglas Christopher Wilson <doug@somethingdoug.com>",
8
+ "Joe Wagner <njwjs722@gmail.com>"
9
+ ],
10
+ "repository": "plotdb/express-session",
11
+ "license": "MIT",
12
+ "dependencies": {
13
+ "cookie": "0.4.2",
14
+ "cookie-signature": "1.0.6",
15
+ "debug": "2.6.9",
16
+ "depd": "~2.0.0",
17
+ "on-headers": "~1.0.2",
18
+ "parseurl": "~1.3.3",
19
+ "safe-buffer": "5.2.1",
20
+ "uid-safe": "~2.1.5"
21
+ },
22
+ "devDependencies": {
23
+ "after": "0.8.2",
24
+ "cookie-parser": "1.4.6",
25
+ "eslint": "7.32.0",
26
+ "eslint-plugin-markdown": "2.2.1",
27
+ "express": "4.17.3",
28
+ "mocha": "10.0.0",
29
+ "nyc": "15.1.0",
30
+ "supertest": "6.2.3"
31
+ },
32
+ "files": [
33
+ "session/",
34
+ "HISTORY.md",
35
+ "index.js"
36
+ ],
37
+ "engines": {
38
+ "node": ">= 0.8.0"
39
+ },
40
+ "scripts": {
41
+ "lint": "eslint . && node ./scripts/lint-readme.js",
42
+ "test": "mocha --require test/support/env --check-leaks --bail --no-exit --reporter spec test/",
43
+ "test-ci": "nyc --reporter=lcov --reporter=text npm test",
44
+ "test-cov": "nyc npm test",
45
+ "version": "node scripts/version-history.js && git add HISTORY.md"
46
+ }
47
+ }
@@ -0,0 +1,150 @@
1
+ /*!
2
+ * Connect - session - Cookie
3
+ * Copyright(c) 2010 Sencha Inc.
4
+ * Copyright(c) 2011 TJ Holowaychuk
5
+ * MIT Licensed
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ /**
11
+ * Module dependencies.
12
+ */
13
+
14
+ var cookie = require('cookie')
15
+ var deprecate = require('depd')('express-session')
16
+
17
+ /**
18
+ * Initialize a new `Cookie` with the given `options`.
19
+ *
20
+ * @param {IncomingMessage} req
21
+ * @param {Object} options
22
+ * @api private
23
+ */
24
+
25
+ var Cookie = module.exports = function Cookie(options) {
26
+ this.path = '/';
27
+ this.maxAge = null;
28
+ this.httpOnly = true;
29
+
30
+ if (options) {
31
+ if (typeof options !== 'object') {
32
+ throw new TypeError('argument options must be a object')
33
+ }
34
+
35
+ for (var key in options) {
36
+ if (key !== 'data') {
37
+ this[key] = options[key]
38
+ }
39
+ }
40
+ }
41
+
42
+ if (this.originalMaxAge === undefined || this.originalMaxAge === null) {
43
+ this.originalMaxAge = this.maxAge
44
+ }
45
+ };
46
+
47
+ /*!
48
+ * Prototype.
49
+ */
50
+
51
+ Cookie.prototype = {
52
+
53
+ /**
54
+ * Set expires `date`.
55
+ *
56
+ * @param {Date} date
57
+ * @api public
58
+ */
59
+
60
+ set expires(date) {
61
+ this._expires = date;
62
+ this.originalMaxAge = this.maxAge;
63
+ },
64
+
65
+ /**
66
+ * Get expires `date`.
67
+ *
68
+ * @return {Date}
69
+ * @api public
70
+ */
71
+
72
+ get expires() {
73
+ return this._expires;
74
+ },
75
+
76
+ /**
77
+ * Set expires via max-age in `ms`.
78
+ *
79
+ * @param {Number} ms
80
+ * @api public
81
+ */
82
+
83
+ set maxAge(ms) {
84
+ if (ms && typeof ms !== 'number' && !(ms instanceof Date)) {
85
+ throw new TypeError('maxAge must be a number or Date')
86
+ }
87
+
88
+ if (ms instanceof Date) {
89
+ deprecate('maxAge as Date; pass number of milliseconds instead')
90
+ }
91
+
92
+ this.expires = typeof ms === 'number'
93
+ ? new Date(Date.now() + ms)
94
+ : ms;
95
+ },
96
+
97
+ /**
98
+ * Get expires max-age in `ms`.
99
+ *
100
+ * @return {Number}
101
+ * @api public
102
+ */
103
+
104
+ get maxAge() {
105
+ return this.expires instanceof Date
106
+ ? this.expires.valueOf() - Date.now()
107
+ : this.expires;
108
+ },
109
+
110
+ /**
111
+ * Return cookie data object.
112
+ *
113
+ * @return {Object}
114
+ * @api private
115
+ */
116
+
117
+ get data() {
118
+ return {
119
+ originalMaxAge: this.originalMaxAge
120
+ , expires: this._expires
121
+ , secure: this.secure
122
+ , httpOnly: this.httpOnly
123
+ , domain: this.domain
124
+ , path: this.path
125
+ , sameSite: this.sameSite
126
+ }
127
+ },
128
+
129
+ /**
130
+ * Return a serialized cookie string.
131
+ *
132
+ * @return {String}
133
+ * @api public
134
+ */
135
+
136
+ serialize: function(name, val){
137
+ return cookie.serialize(name, val, this.data);
138
+ },
139
+
140
+ /**
141
+ * Return JSON representation of this cookie.
142
+ *
143
+ * @return {Object}
144
+ * @api private
145
+ */
146
+
147
+ toJSON: function(){
148
+ return this.data;
149
+ }
150
+ };
@@ -0,0 +1,187 @@
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
+
9
+ 'use strict';
10
+
11
+ /**
12
+ * Module dependencies.
13
+ * @private
14
+ */
15
+
16
+ var Store = require('./store')
17
+ var util = require('util')
18
+
19
+ /**
20
+ * Shim setImmediate for node.js < 0.10
21
+ * @private
22
+ */
23
+
24
+ /* istanbul ignore next */
25
+ var defer = typeof setImmediate === 'function'
26
+ ? setImmediate
27
+ : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }
28
+
29
+ /**
30
+ * Module exports.
31
+ */
32
+
33
+ module.exports = MemoryStore
34
+
35
+ /**
36
+ * A session store in memory.
37
+ * @public
38
+ */
39
+
40
+ function MemoryStore() {
41
+ Store.call(this)
42
+ this.sessions = Object.create(null)
43
+ }
44
+
45
+ /**
46
+ * Inherit from Store.
47
+ */
48
+
49
+ util.inherits(MemoryStore, Store)
50
+
51
+ /**
52
+ * Get all active sessions.
53
+ *
54
+ * @param {function} callback
55
+ * @public
56
+ */
57
+
58
+ MemoryStore.prototype.all = function all(callback) {
59
+ var sessionIds = Object.keys(this.sessions)
60
+ var sessions = Object.create(null)
61
+
62
+ for (var i = 0; i < sessionIds.length; i++) {
63
+ var sessionId = sessionIds[i]
64
+ var session = getSession.call(this, sessionId)
65
+
66
+ if (session) {
67
+ sessions[sessionId] = session;
68
+ }
69
+ }
70
+
71
+ callback && defer(callback, null, sessions)
72
+ }
73
+
74
+ /**
75
+ * Clear all sessions.
76
+ *
77
+ * @param {function} callback
78
+ * @public
79
+ */
80
+
81
+ MemoryStore.prototype.clear = function clear(callback) {
82
+ this.sessions = Object.create(null)
83
+ callback && defer(callback)
84
+ }
85
+
86
+ /**
87
+ * Destroy the session associated with the given session ID.
88
+ *
89
+ * @param {string} sessionId
90
+ * @public
91
+ */
92
+
93
+ MemoryStore.prototype.destroy = function destroy(sessionId, callback) {
94
+ delete this.sessions[sessionId]
95
+ callback && defer(callback)
96
+ }
97
+
98
+ /**
99
+ * Fetch session by the given session ID.
100
+ *
101
+ * @param {string} sessionId
102
+ * @param {function} callback
103
+ * @public
104
+ */
105
+
106
+ MemoryStore.prototype.get = function get(sessionId, callback) {
107
+ defer(callback, null, getSession.call(this, sessionId))
108
+ }
109
+
110
+ /**
111
+ * Commit the given session associated with the given sessionId to the store.
112
+ *
113
+ * @param {string} sessionId
114
+ * @param {object} session
115
+ * @param {function} callback
116
+ * @public
117
+ */
118
+
119
+ MemoryStore.prototype.set = function set(sessionId, session, callback) {
120
+ this.sessions[sessionId] = JSON.stringify(session)
121
+ callback && defer(callback)
122
+ }
123
+
124
+ /**
125
+ * Get number of active sessions.
126
+ *
127
+ * @param {function} callback
128
+ * @public
129
+ */
130
+
131
+ MemoryStore.prototype.length = function length(callback) {
132
+ this.all(function (err, sessions) {
133
+ if (err) return callback(err)
134
+ callback(null, Object.keys(sessions).length)
135
+ })
136
+ }
137
+
138
+ /**
139
+ * Touch the given session object associated with the given session ID.
140
+ *
141
+ * @param {string} sessionId
142
+ * @param {object} session
143
+ * @param {function} callback
144
+ * @public
145
+ */
146
+
147
+ MemoryStore.prototype.touch = function touch(sessionId, session, callback) {
148
+ var currentSession = getSession.call(this, sessionId)
149
+
150
+ if (currentSession) {
151
+ // update expiration
152
+ currentSession.cookie = session.cookie
153
+ this.sessions[sessionId] = JSON.stringify(currentSession)
154
+ }
155
+
156
+ callback && defer(callback)
157
+ }
158
+
159
+ /**
160
+ * Get session from the store.
161
+ * @private
162
+ */
163
+
164
+ function getSession(sessionId) {
165
+ var sess = this.sessions[sessionId]
166
+
167
+ if (!sess) {
168
+ return
169
+ }
170
+
171
+ // parse
172
+ sess = JSON.parse(sess)
173
+
174
+ if (sess.cookie) {
175
+ var expires = typeof sess.cookie.expires === 'string'
176
+ ? new Date(sess.cookie.expires)
177
+ : sess.cookie.expires
178
+
179
+ // destroy expired session
180
+ if (expires && expires <= Date.now()) {
181
+ delete this.sessions[sessionId]
182
+ return
183
+ }
184
+ }
185
+
186
+ return sess
187
+ }
@@ -0,0 +1,143 @@
1
+ /*!
2
+ * Connect - session - Session
3
+ * Copyright(c) 2010 Sencha Inc.
4
+ * Copyright(c) 2011 TJ Holowaychuk
5
+ * MIT Licensed
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ /**
11
+ * Expose Session.
12
+ */
13
+
14
+ module.exports = Session;
15
+
16
+ /**
17
+ * Create a new `Session` with the given request and `data`.
18
+ *
19
+ * @param {IncomingRequest} req
20
+ * @param {Object} data
21
+ * @api private
22
+ */
23
+
24
+ function Session(req, data) {
25
+ Object.defineProperty(this, 'req', { value: req });
26
+ Object.defineProperty(this, 'id', { value: req.sessionID });
27
+
28
+ if (typeof data === 'object' && data !== null) {
29
+ // merge data into this, ignoring prototype properties
30
+ for (var prop in data) {
31
+ if (!(prop in this)) {
32
+ this[prop] = data[prop]
33
+ }
34
+ }
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Update reset `.cookie.maxAge` to prevent
40
+ * the cookie from expiring when the
41
+ * session is still active.
42
+ *
43
+ * @return {Session} for chaining
44
+ * @api public
45
+ */
46
+
47
+ defineMethod(Session.prototype, 'touch', function touch() {
48
+ return this.resetMaxAge();
49
+ });
50
+
51
+ /**
52
+ * Reset `.maxAge` to `.originalMaxAge`.
53
+ *
54
+ * @return {Session} for chaining
55
+ * @api public
56
+ */
57
+
58
+ defineMethod(Session.prototype, 'resetMaxAge', function resetMaxAge() {
59
+ this.cookie.maxAge = this.cookie.originalMaxAge;
60
+ return this;
61
+ });
62
+
63
+ /**
64
+ * Save the session data with optional callback `fn(err)`.
65
+ *
66
+ * @param {Function} fn
67
+ * @return {Session} for chaining
68
+ * @api public
69
+ */
70
+
71
+ defineMethod(Session.prototype, 'save', function save(fn) {
72
+ this.req.sessionStore.set(this.id, this, fn || function(){});
73
+ return this;
74
+ });
75
+
76
+ /**
77
+ * Re-loads the session data _without_ altering
78
+ * the maxAge properties. Invokes the callback `fn(err)`,
79
+ * after which time if no exception has occurred the
80
+ * `req.session` property will be a new `Session` object,
81
+ * although representing the same session.
82
+ *
83
+ * @param {Function} fn
84
+ * @return {Session} for chaining
85
+ * @api public
86
+ */
87
+
88
+ defineMethod(Session.prototype, 'reload', function reload(fn) {
89
+ var req = this.req
90
+ var store = this.req.sessionStore
91
+
92
+ store.get(this.id, function(err, sess){
93
+ if (err) return fn(err);
94
+ if (!sess) return fn(new Error('failed to load session'));
95
+ store.createSession(req, sess);
96
+ fn();
97
+ });
98
+ return this;
99
+ });
100
+
101
+ /**
102
+ * Destroy `this` session.
103
+ *
104
+ * @param {Function} fn
105
+ * @return {Session} for chaining
106
+ * @api public
107
+ */
108
+
109
+ defineMethod(Session.prototype, 'destroy', function destroy(fn) {
110
+ delete this.req.session;
111
+ this.req.sessionStore.destroy(this.id, fn);
112
+ return this;
113
+ });
114
+
115
+ /**
116
+ * Regenerate this request's session.
117
+ *
118
+ * @param {Function} fn
119
+ * @return {Session} for chaining
120
+ * @api public
121
+ */
122
+
123
+ defineMethod(Session.prototype, 'regenerate', function regenerate(fn) {
124
+ this.req.sessionStore.regenerate(this.req, fn);
125
+ return this;
126
+ });
127
+
128
+ /**
129
+ * Helper function for creating a method on a prototype.
130
+ *
131
+ * @param {Object} obj
132
+ * @param {String} name
133
+ * @param {Function} fn
134
+ * @private
135
+ */
136
+ function defineMethod(obj, name, fn) {
137
+ Object.defineProperty(obj, name, {
138
+ configurable: true,
139
+ enumerable: false,
140
+ value: fn,
141
+ writable: true
142
+ });
143
+ };
@@ -0,0 +1,102 @@
1
+ /*!
2
+ * Connect - session - Store
3
+ * Copyright(c) 2010 Sencha Inc.
4
+ * Copyright(c) 2011 TJ Holowaychuk
5
+ * MIT Licensed
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ /**
11
+ * Module dependencies.
12
+ * @private
13
+ */
14
+
15
+ var Cookie = require('./cookie')
16
+ var EventEmitter = require('events').EventEmitter
17
+ var Session = require('./session')
18
+ var util = require('util')
19
+
20
+ /**
21
+ * Module exports.
22
+ * @public
23
+ */
24
+
25
+ module.exports = Store
26
+
27
+ /**
28
+ * Abstract base class for session stores.
29
+ * @public
30
+ */
31
+
32
+ function Store () {
33
+ EventEmitter.call(this)
34
+ }
35
+
36
+ /**
37
+ * Inherit from EventEmitter.
38
+ */
39
+
40
+ util.inherits(Store, EventEmitter)
41
+
42
+ /**
43
+ * Re-generate the given requests's session.
44
+ *
45
+ * @param {IncomingRequest} req
46
+ * @return {Function} fn
47
+ * @api public
48
+ */
49
+
50
+ Store.prototype.regenerate = function(req, fn){
51
+ var self = this;
52
+ this.destroy(req.sessionID, function(err){
53
+ self.generate(req);
54
+ fn(err);
55
+ });
56
+ };
57
+
58
+ /**
59
+ * Load a `Session` instance via the given `sid`
60
+ * and invoke the callback `fn(err, sess)`.
61
+ *
62
+ * @param {String} sid
63
+ * @param {Function} fn
64
+ * @api public
65
+ */
66
+
67
+ Store.prototype.load = function(sid, fn){
68
+ var self = this;
69
+ this.get(sid, function(err, sess){
70
+ if (err) return fn(err);
71
+ if (!sess) return fn();
72
+ var req = { sessionID: sid, sessionStore: self };
73
+ fn(null, self.createSession(req, sess))
74
+ });
75
+ };
76
+
77
+ /**
78
+ * Create session from JSON `sess` data.
79
+ *
80
+ * @param {IncomingRequest} req
81
+ * @param {Object} sess
82
+ * @return {Session}
83
+ * @api private
84
+ */
85
+
86
+ Store.prototype.createSession = function(req, sess){
87
+ var expires = sess.cookie.expires
88
+ var originalMaxAge = sess.cookie.originalMaxAge
89
+
90
+ sess.cookie = new Cookie(sess.cookie);
91
+
92
+ if (typeof expires === 'string') {
93
+ // convert expires to a Date object
94
+ sess.cookie.expires = new Date(expires)
95
+ }
96
+
97
+ // keep originalMaxAge intact
98
+ sess.cookie.originalMaxAge = originalMaxAge
99
+
100
+ req.session = new Session(req, sess);
101
+ return req.session;
102
+ };