@depup/memorystore 1.6.7-depup.0
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/.travis.yml +10 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/changes.json +14 -0
- package/index.d.ts +81 -0
- package/index.js +1 -0
- package/lib/memorystore.js +295 -0
- package/package.json +66 -0
- package/test/store.js +304 -0
package/.travis.yml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Rocco Musolino
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @depup/memorystore
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [memorystore](https://www.npmjs.com/package/memorystore)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/memorystore
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [memorystore](https://www.npmjs.com/package/memorystore) @ 1.6.7 |
|
|
17
|
+
| Processed | 2026-03-19 |
|
|
18
|
+
| Smoke test | passed |
|
|
19
|
+
| Deps updated | 2 |
|
|
20
|
+
|
|
21
|
+
## Dependency Changes
|
|
22
|
+
|
|
23
|
+
| Dependency | From | To |
|
|
24
|
+
|------------|------|-----|
|
|
25
|
+
| debug | ^4.3.0 | ^4.4.3 |
|
|
26
|
+
| lru-cache | ^4.0.3 | ^11.2.7 |
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/memorystore
|
|
31
|
+
|
|
32
|
+
License inherited from the original package.
|
package/changes.json
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import expressSession, {
|
|
2
|
+
MemoryStore as ExpressMemoryStore,
|
|
3
|
+
} from "express-session";
|
|
4
|
+
|
|
5
|
+
interface MemoryStoreOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Define how long MemoryStore will check for expired.
|
|
8
|
+
* The period is in ms. The automatic check is disabled by default!
|
|
9
|
+
* Not setting this is kind of silly, since that's the whole purpose of
|
|
10
|
+
* this lib.
|
|
11
|
+
*/
|
|
12
|
+
checkPeriod?: number;
|
|
13
|
+
/**
|
|
14
|
+
* The maximum size of the cache, checked by applying the length
|
|
15
|
+
* function to all values in the cache. It defaults to `Infinity`.
|
|
16
|
+
*/
|
|
17
|
+
max?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Session TTL (expiration) in milliseconds.
|
|
20
|
+
* Defaults to `session.maxAge` (if set), or one day.
|
|
21
|
+
*/
|
|
22
|
+
ttl?: number | ((options: any, sess: any, sessionId: any) => number);
|
|
23
|
+
/**
|
|
24
|
+
* Function that is called on sessions when they are dropped from the
|
|
25
|
+
* cache. This can be handy if you want to close file descriptors or do
|
|
26
|
+
* other cleanup tasks when sessions are no longer accessible. It's
|
|
27
|
+
* called before actually removing the item from the internal cache, so
|
|
28
|
+
* if you want to immediately put it back in, you'll have to do that in
|
|
29
|
+
* a `nextTick` or `setTimeout` callback or it won't do anything.
|
|
30
|
+
*/
|
|
31
|
+
dispose?: (key: any, value: any) => void;
|
|
32
|
+
/**
|
|
33
|
+
* By default, if you set a `maxAge`, it'll only actually pull stale
|
|
34
|
+
* items out of the cache when you `get(key)`. (That is, it's not
|
|
35
|
+
* pre-emptively doing a setTimeout or anything.) If you set
|
|
36
|
+
* `stale:true`, it'll return the stale value before deleting it. If
|
|
37
|
+
* you don't set this, then it'll return undefined when you try to get
|
|
38
|
+
* a stale entry, as if it had already been deleted.
|
|
39
|
+
*/
|
|
40
|
+
stale?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* By default, if you set a `dispose()` method, then it'll be called
|
|
43
|
+
* whenever a `set()` operation overwrites an existing key. If you set
|
|
44
|
+
* this option, `dispose()` will only be called when a key falls out of
|
|
45
|
+
* the cache, not when it is overwritten.
|
|
46
|
+
*/
|
|
47
|
+
noDisposeOnSet?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* An object compatible with Javascript's JSON to override the
|
|
50
|
+
* serializer used.
|
|
51
|
+
*/
|
|
52
|
+
serializer?: {
|
|
53
|
+
stringify: (arg: object) => string;
|
|
54
|
+
parse: (str: string) => object;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
declare class MemoryStore extends ExpressMemoryStore {
|
|
59
|
+
constructor(options: MemoryStoreOptions);
|
|
60
|
+
/** method to start the automatic check for expired. */
|
|
61
|
+
startInterval(): void;
|
|
62
|
+
/** method to clear the automatic check for expired. */
|
|
63
|
+
stopInterval(): void;
|
|
64
|
+
/** use to manually remove only the expired entries from the store. */
|
|
65
|
+
prune(): void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Sample usage:
|
|
70
|
+
* ```
|
|
71
|
+
* import session from 'express-session';
|
|
72
|
+
* import createMemoryStore from 'memorystore';
|
|
73
|
+
* const MemoryStore = createMemoryStore(session);
|
|
74
|
+
* ...
|
|
75
|
+
* app.use(session({ store: new MemoryStore({ ...options }) }));
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
declare function createMemoryStore(
|
|
79
|
+
session: typeof expressSession
|
|
80
|
+
): typeof MemoryStore;
|
|
81
|
+
export = createMemoryStore;
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/memorystore.js')
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* memorystore
|
|
3
|
+
* Copyright(c) 2020 Rocco Musolino <@roccomuso>
|
|
4
|
+
* MIT Licensed
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
var debug = require('debug')('memorystore')
|
|
8
|
+
var LRU = require('lru-cache')
|
|
9
|
+
var util = require('util')
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* One day in milliseconds.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
var oneDay = 86400000
|
|
16
|
+
|
|
17
|
+
function getTTL (options, sess, sid) {
|
|
18
|
+
if (typeof options.ttl === 'number') return options.ttl
|
|
19
|
+
if (typeof options.ttl === 'function') return options.ttl(options, sess, sid)
|
|
20
|
+
if (options.ttl) throw new TypeError('`options.ttl` must be a number or function.')
|
|
21
|
+
|
|
22
|
+
var maxAge = (sess && sess.cookie) ? sess.cookie.maxAge : null
|
|
23
|
+
return (typeof maxAge === 'number'
|
|
24
|
+
? Math.floor(maxAge)
|
|
25
|
+
: oneDay)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function prune (store) {
|
|
29
|
+
debug('Pruning expired entries')
|
|
30
|
+
store.forEach(function (value, key) {
|
|
31
|
+
store.get(key)
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
var defer = typeof setImmediate === 'function'
|
|
36
|
+
? setImmediate
|
|
37
|
+
: function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) }
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Return the `MemoryStore` extending `express`'s session Store.
|
|
41
|
+
*
|
|
42
|
+
* @param {object} express session
|
|
43
|
+
* @return {Function}
|
|
44
|
+
* @api public
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
module.exports = function (session) {
|
|
48
|
+
/**
|
|
49
|
+
* Express's session Store.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
var Store = session.Store
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Initialize MemoryStore with the given `options`.
|
|
56
|
+
*
|
|
57
|
+
* @param {Object} options
|
|
58
|
+
* @api public
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
function MemoryStore (options) {
|
|
62
|
+
if (!(this instanceof MemoryStore)) {
|
|
63
|
+
throw new TypeError('Cannot call MemoryStore constructor as a function')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
options = options || {}
|
|
67
|
+
Store.call(this, options)
|
|
68
|
+
|
|
69
|
+
this.options = {}
|
|
70
|
+
this.options.checkPeriod = options.checkPeriod
|
|
71
|
+
this.options.max = options.max || Infinity
|
|
72
|
+
this.options.ttl = options.ttl
|
|
73
|
+
this.options.dispose = options.dispose
|
|
74
|
+
this.options.stale = options.stale
|
|
75
|
+
this.options.noDisposeOnSet = options.noDisposeOnSet
|
|
76
|
+
|
|
77
|
+
this.serializer = options.serializer || JSON
|
|
78
|
+
this.store = LRU(this.options)
|
|
79
|
+
debug('Init MemoryStore')
|
|
80
|
+
|
|
81
|
+
this.startInterval()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Inherit from `Store`.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
util.inherits(MemoryStore, Store)
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Attempt to fetch session by the given `sid`.
|
|
92
|
+
*
|
|
93
|
+
* @param {String} sid
|
|
94
|
+
* @param {Function} fn
|
|
95
|
+
* @api public
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
MemoryStore.prototype.get = function (sid, fn) {
|
|
99
|
+
var store = this.store
|
|
100
|
+
|
|
101
|
+
debug('GET "%s"', sid)
|
|
102
|
+
|
|
103
|
+
var data = store.get(sid)
|
|
104
|
+
if (!data) return fn()
|
|
105
|
+
|
|
106
|
+
debug('GOT %s', data)
|
|
107
|
+
var err = null
|
|
108
|
+
var result
|
|
109
|
+
try {
|
|
110
|
+
result = this.serializer.parse(data)
|
|
111
|
+
} catch (er) {
|
|
112
|
+
err = er
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fn && defer(fn, err, result)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Commit the given `sess` object associated with the given `sid`.
|
|
120
|
+
*
|
|
121
|
+
* @param {String} sid
|
|
122
|
+
* @param {Session} sess
|
|
123
|
+
* @param {Function} fn
|
|
124
|
+
* @api public
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
MemoryStore.prototype.set = function (sid, sess, fn) {
|
|
128
|
+
var store = this.store
|
|
129
|
+
|
|
130
|
+
var ttl = getTTL(this.options, sess, sid)
|
|
131
|
+
try {
|
|
132
|
+
var jsess = this.serializer.stringify(sess)
|
|
133
|
+
} catch (err) {
|
|
134
|
+
fn && defer(fn, err)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
store.set(sid, jsess, ttl)
|
|
138
|
+
debug('SET "%s" %s ttl:%s', sid, jsess, ttl)
|
|
139
|
+
fn && defer(fn, null)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Destroy the session associated with the given `sid`.
|
|
144
|
+
*
|
|
145
|
+
* @param {String} sid
|
|
146
|
+
* @api public
|
|
147
|
+
*/
|
|
148
|
+
|
|
149
|
+
MemoryStore.prototype.destroy = function (sid, fn) {
|
|
150
|
+
var store = this.store
|
|
151
|
+
|
|
152
|
+
if (Array.isArray(sid)) {
|
|
153
|
+
sid.forEach(function (s) {
|
|
154
|
+
debug('DEL "%s"', s)
|
|
155
|
+
store.del(s)
|
|
156
|
+
})
|
|
157
|
+
} else {
|
|
158
|
+
debug('DEL "%s"', sid)
|
|
159
|
+
store.del(sid)
|
|
160
|
+
}
|
|
161
|
+
fn && defer(fn, null)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Refresh the time-to-live for the session with the given `sid`.
|
|
166
|
+
*
|
|
167
|
+
* @param {String} sid
|
|
168
|
+
* @param {Session} sess
|
|
169
|
+
* @param {Function} fn
|
|
170
|
+
* @api public
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
MemoryStore.prototype.touch = function (sid, sess, fn) {
|
|
174
|
+
var store = this.store
|
|
175
|
+
|
|
176
|
+
var ttl = getTTL(this.options, sess, sid)
|
|
177
|
+
|
|
178
|
+
debug('EXPIRE "%s" ttl:%s', sid, ttl)
|
|
179
|
+
var err = null
|
|
180
|
+
if (store.get(sid) !== undefined) {
|
|
181
|
+
try {
|
|
182
|
+
var s = this.serializer.parse(store.get(sid))
|
|
183
|
+
s.cookie = sess.cookie
|
|
184
|
+
store.set(sid, this.serializer.stringify(s), ttl)
|
|
185
|
+
} catch (e) {
|
|
186
|
+
err = e
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
fn && defer(fn, err)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Fetch all sessions' ids
|
|
194
|
+
*
|
|
195
|
+
* @param {Function} fn
|
|
196
|
+
* @api public
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
MemoryStore.prototype.ids = function (fn) {
|
|
200
|
+
var store = this.store
|
|
201
|
+
|
|
202
|
+
var Ids = store.keys()
|
|
203
|
+
debug('Getting IDs: %s', Ids)
|
|
204
|
+
fn && defer(fn, null, Ids)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Fetch all sessions
|
|
209
|
+
*
|
|
210
|
+
* @param {Function} fn
|
|
211
|
+
* @api public
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
MemoryStore.prototype.all = function (fn) {
|
|
215
|
+
var store = this.store
|
|
216
|
+
var self = this
|
|
217
|
+
|
|
218
|
+
debug('Fetching all sessions')
|
|
219
|
+
var err = null
|
|
220
|
+
var result = {}
|
|
221
|
+
try {
|
|
222
|
+
store.forEach(function (val, key) {
|
|
223
|
+
result[key] = self.serializer.parse(val)
|
|
224
|
+
})
|
|
225
|
+
} catch (e) {
|
|
226
|
+
err = e
|
|
227
|
+
}
|
|
228
|
+
fn && defer(fn, err, result)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Delete all sessions from the store
|
|
233
|
+
*
|
|
234
|
+
* @param {Function} fn
|
|
235
|
+
* @api public
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
MemoryStore.prototype.clear = function (fn) {
|
|
239
|
+
var store = this.store
|
|
240
|
+
debug('delete all sessions from the store')
|
|
241
|
+
store.reset()
|
|
242
|
+
fn && defer(fn, null)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Get the count of all sessions in the store
|
|
247
|
+
*
|
|
248
|
+
* @param {Function} fn
|
|
249
|
+
* @api public
|
|
250
|
+
*/
|
|
251
|
+
|
|
252
|
+
MemoryStore.prototype.length = function (fn) {
|
|
253
|
+
var store = this.store
|
|
254
|
+
debug('getting length', store.itemCount)
|
|
255
|
+
fn && defer(fn, null, store.itemCount)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Start the check interval
|
|
260
|
+
* @api public
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
MemoryStore.prototype.startInterval = function () {
|
|
264
|
+
var self = this
|
|
265
|
+
var ms = this.options.checkPeriod
|
|
266
|
+
if (ms && typeof ms === 'number') {
|
|
267
|
+
clearInterval(this._checkInterval)
|
|
268
|
+
debug('starting periodic check for expired sessions')
|
|
269
|
+
this._checkInterval = setInterval(function () {
|
|
270
|
+
prune(self.store) // iterates over the entire cache proactively pruning old entries
|
|
271
|
+
}, Math.floor(ms)).unref()
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Stop the check interval
|
|
277
|
+
* @api public
|
|
278
|
+
*/
|
|
279
|
+
|
|
280
|
+
MemoryStore.prototype.stopInterval = function () {
|
|
281
|
+
debug('stopping periodic check for expired sessions')
|
|
282
|
+
clearInterval(this._checkInterval)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Remove only expired entries from the store
|
|
287
|
+
* @api public
|
|
288
|
+
*/
|
|
289
|
+
|
|
290
|
+
MemoryStore.prototype.prune = function () {
|
|
291
|
+
prune(this.store)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return MemoryStore
|
|
295
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@depup/memorystore",
|
|
3
|
+
"version": "1.6.7-depup.0",
|
|
4
|
+
"description": "express-session full featured MemoryStore layer without leaks! (with updated dependencies)",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "mocha --check-leaks --bail --no-exit test/"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/roccomuso/memorystore.git"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=0.10"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"memorystore",
|
|
19
|
+
"depup",
|
|
20
|
+
"updated-dependencies",
|
|
21
|
+
"security",
|
|
22
|
+
"latest",
|
|
23
|
+
"patched",
|
|
24
|
+
"express-session",
|
|
25
|
+
"session",
|
|
26
|
+
"memory",
|
|
27
|
+
"store",
|
|
28
|
+
"noleak",
|
|
29
|
+
"ram"
|
|
30
|
+
],
|
|
31
|
+
"author": "Rocco Musolino (roccomuso)",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/roccomuso/memorystore/issues"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/roccomuso/memorystore#readme",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"debug": "^4.4.3",
|
|
39
|
+
"lru-cache": "^11.2.7"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"mocha": "9.2.0"
|
|
43
|
+
},
|
|
44
|
+
"standard": {
|
|
45
|
+
"env": [
|
|
46
|
+
"mocha"
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
"depup": {
|
|
50
|
+
"changes": {
|
|
51
|
+
"debug": {
|
|
52
|
+
"from": "^4.3.0",
|
|
53
|
+
"to": "^4.4.3"
|
|
54
|
+
},
|
|
55
|
+
"lru-cache": {
|
|
56
|
+
"from": "^4.0.3",
|
|
57
|
+
"to": "^11.2.7"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"depsUpdated": 2,
|
|
61
|
+
"originalPackage": "memorystore",
|
|
62
|
+
"originalVersion": "1.6.7",
|
|
63
|
+
"processedAt": "2026-03-19T03:03:03.740Z",
|
|
64
|
+
"smokeTest": "passed"
|
|
65
|
+
}
|
|
66
|
+
}
|
package/test/store.js
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
var assert = require('assert')
|
|
2
|
+
|
|
3
|
+
// express-session way
|
|
4
|
+
var MemoryStore = require('../')({Store: function () {}})
|
|
5
|
+
var session = {MemoryStore: MemoryStore}
|
|
6
|
+
|
|
7
|
+
describe('MemoryStore', function (done) {
|
|
8
|
+
afterEach(function () {
|
|
9
|
+
// runs after each test in this block
|
|
10
|
+
this.store.stopInterval()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('constructor should use default options', function (done) {
|
|
14
|
+
this.store = new session.MemoryStore()
|
|
15
|
+
var store = this.store
|
|
16
|
+
assert.ok(store.options, 'should have an option object')
|
|
17
|
+
assert.equal(store.options.max, Infinity, 'max option should be Infinity')
|
|
18
|
+
assert.equal(store.options.checkPeriod, undefined, 'checkPeriod undefined by default')
|
|
19
|
+
assert.ok(store.store, 'should have the LRU cache store')
|
|
20
|
+
assert.equal(store._checkInterval, undefined, 'should not have the pruning loop')
|
|
21
|
+
done()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should set options', function (done) {
|
|
25
|
+
this.store = new session.MemoryStore({
|
|
26
|
+
max: 10,
|
|
27
|
+
checkPeriod: 10 * 1000,
|
|
28
|
+
ttl: 36000,
|
|
29
|
+
dispose: null,
|
|
30
|
+
stale: true
|
|
31
|
+
})
|
|
32
|
+
var store = this.store
|
|
33
|
+
assert.equal(store.options.max, 10, 'should set the max option')
|
|
34
|
+
assert.equal(store.options.checkPeriod, 10 * 1000, 'should set checkPeriod')
|
|
35
|
+
assert.equal(store.options.ttl, 36000, 'should set the TTL')
|
|
36
|
+
assert.equal(store.options.dispose, null, 'should set dispose')
|
|
37
|
+
assert.equal(store.options.stale, true, 'should set stale')
|
|
38
|
+
done()
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should not set the interval to check for expired entries by default', function (done) {
|
|
42
|
+
this.store = new session.MemoryStore()
|
|
43
|
+
var store = this.store
|
|
44
|
+
assert.equal(store._checkInterval, undefined, 'should not exists')
|
|
45
|
+
done()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should only contain 10 items', function (done) {
|
|
49
|
+
this.store = new session.MemoryStore({max: 10})
|
|
50
|
+
var store = this.store
|
|
51
|
+
|
|
52
|
+
for (var i = 0; i < 15; i++) {
|
|
53
|
+
store.set(i, {cookie: { expires: new Date((new Date()).valueOf() + 60 * 10 * 1000) }})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
store.length(function (err, length) {
|
|
57
|
+
if (err) return done(err)
|
|
58
|
+
assert.equal(length, 10)
|
|
59
|
+
done()
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('should delete the first item', function (done) {
|
|
64
|
+
this.store = new session.MemoryStore({max: 10})
|
|
65
|
+
var store = this.store
|
|
66
|
+
|
|
67
|
+
for (var i = 0; i < 15; i++) {
|
|
68
|
+
store.set(i, {cookie: { expires: new Date((new Date()).valueOf() + 60 * 10 * 1000) }})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
store.destroy(14)
|
|
72
|
+
|
|
73
|
+
store.length(function (err, length) {
|
|
74
|
+
if (err) return done(err)
|
|
75
|
+
assert.equal(length, 9)
|
|
76
|
+
done()
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('should delete the last item', function (done) {
|
|
81
|
+
this.store = new session.MemoryStore({max: 10})
|
|
82
|
+
var store = this.store
|
|
83
|
+
|
|
84
|
+
for (var i = 0; i < 10; i++) {
|
|
85
|
+
store.set(i, {cookie: { expires: new Date((new Date()).valueOf() + 60 * 10 * 1000) }})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
store.destroy(0)
|
|
89
|
+
store.destroy(1)
|
|
90
|
+
|
|
91
|
+
store.length(function (err, length) {
|
|
92
|
+
if (err) return done(err)
|
|
93
|
+
assert.equal(length, 8)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
for (i = 10; i < 12; i++) {
|
|
97
|
+
store.set(i, {cookie: { expires: new Date((new Date()).valueOf() + 60 * 10 * 1000) }})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
store.length(function (err, length) {
|
|
101
|
+
if (err) return done(err)
|
|
102
|
+
assert.equal(length, 10)
|
|
103
|
+
done()
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('should set and get a sample entry', function (done) {
|
|
108
|
+
this.store = new session.MemoryStore()
|
|
109
|
+
var store = this.store
|
|
110
|
+
|
|
111
|
+
store.set('hello', {cookie: {}, sample: true})
|
|
112
|
+
store.get('hello', function (err, val) {
|
|
113
|
+
if (err) return done(err)
|
|
114
|
+
assert.equal(val.sample, true, 'set and got expected value')
|
|
115
|
+
done()
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('should set TTL from cookie.maxAge', function (done) {
|
|
120
|
+
this.store = new session.MemoryStore()
|
|
121
|
+
var store = this.store
|
|
122
|
+
|
|
123
|
+
store.set('hello', {cookie: {maxAge: 400}, sample: true})
|
|
124
|
+
store.get('hello', function (err, val) {
|
|
125
|
+
if (err) return done(err)
|
|
126
|
+
assert.equal(val.sample, true, 'entry should be valid')
|
|
127
|
+
})
|
|
128
|
+
setTimeout(function () {
|
|
129
|
+
store.get('hello', function (err, val) {
|
|
130
|
+
if (err) return done(err)
|
|
131
|
+
assert.equal(val, undefined, 'entry should be expired')
|
|
132
|
+
done()
|
|
133
|
+
})
|
|
134
|
+
}, 500)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('should not get empty entry', function (done) {
|
|
138
|
+
this.store = new session.MemoryStore()
|
|
139
|
+
var store = this.store
|
|
140
|
+
|
|
141
|
+
store.get('', function (err, val) {
|
|
142
|
+
if (err) return done(err)
|
|
143
|
+
assert.equal(val, undefined)
|
|
144
|
+
done()
|
|
145
|
+
})
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('should not get a deleted entry', function (done) {
|
|
149
|
+
this.store = new session.MemoryStore()
|
|
150
|
+
var store = this.store
|
|
151
|
+
|
|
152
|
+
store.set('foo', {cookie: {}})
|
|
153
|
+
store.get('foo', function (err, val) {
|
|
154
|
+
if (err) return done(err)
|
|
155
|
+
assert.ok(val, 'entry exists')
|
|
156
|
+
store.destroy('foo')
|
|
157
|
+
store.get('foo', function (err, val) {
|
|
158
|
+
if (err) return done(err)
|
|
159
|
+
assert.equal(val, undefined, 'entry actually deleted')
|
|
160
|
+
done()
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('should not get an expired entry', function (done) {
|
|
166
|
+
this.store = new session.MemoryStore()
|
|
167
|
+
var store = this.store
|
|
168
|
+
|
|
169
|
+
store.set('hello', {cookie: {maxAge: 200}, sample: true})
|
|
170
|
+
setTimeout(function () {
|
|
171
|
+
store.get('hello', function (err, val) {
|
|
172
|
+
if (err) return done(err)
|
|
173
|
+
assert.equal(val, undefined, 'entry should be expired')
|
|
174
|
+
done()
|
|
175
|
+
})
|
|
176
|
+
}, 300)
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('should enable automatic prune for expired entries', function (done) {
|
|
180
|
+
this.store = new session.MemoryStore({checkPeriod: 300})
|
|
181
|
+
var store = this.store
|
|
182
|
+
|
|
183
|
+
store.set('foo', {cookie: {maxAge: 150}})
|
|
184
|
+
store.set('bar', {cookie: {maxAge: 150}})
|
|
185
|
+
store.length(function (err, count) {
|
|
186
|
+
if (err) return done(err)
|
|
187
|
+
assert.equal(count, 2, 'should count 2 entries')
|
|
188
|
+
})
|
|
189
|
+
setTimeout(function () {
|
|
190
|
+
store.length(function (err, count) {
|
|
191
|
+
if (err) return done(err)
|
|
192
|
+
assert.equal(count, 0, 'expired entries should be pruned')
|
|
193
|
+
done()
|
|
194
|
+
})
|
|
195
|
+
}, 500)
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('automatic check for expired entries should be disabled', function (done) {
|
|
199
|
+
this.store = new session.MemoryStore()
|
|
200
|
+
var store = this.store
|
|
201
|
+
|
|
202
|
+
store.set('foo', {cookie: {maxAge: 150}})
|
|
203
|
+
store.set('bar', {cookie: {maxAge: 150}})
|
|
204
|
+
store.length(function (err, count) {
|
|
205
|
+
if (err) return done(err)
|
|
206
|
+
assert.equal(count, 2, 'should count 2 entries')
|
|
207
|
+
})
|
|
208
|
+
setTimeout(function () {
|
|
209
|
+
store.length(function (err, count) {
|
|
210
|
+
if (err) return done(err)
|
|
211
|
+
assert.equal(count, 2, 'expired entries should not be pruned')
|
|
212
|
+
done()
|
|
213
|
+
})
|
|
214
|
+
}, 500)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('should touch a given entry', function (done) {
|
|
218
|
+
this.store = new session.MemoryStore()
|
|
219
|
+
var store = this.store
|
|
220
|
+
|
|
221
|
+
store.set('hei', {cookie: {maxAge: 50}})
|
|
222
|
+
store.touch('hei', {cookie: {maxAge: 300}})
|
|
223
|
+
setTimeout(function () {
|
|
224
|
+
store.get('hei', function (err, val) {
|
|
225
|
+
if (err) return done(err)
|
|
226
|
+
assert.ok(val, 'entry should be touched')
|
|
227
|
+
done()
|
|
228
|
+
})
|
|
229
|
+
}, 200)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it('should fetch all entries Ids', function (done) {
|
|
233
|
+
this.store = new session.MemoryStore()
|
|
234
|
+
var store = this.store
|
|
235
|
+
|
|
236
|
+
var k = 10
|
|
237
|
+
var i = 0
|
|
238
|
+
for (i = 0; i < k; i++) { store.set('sess' + i, {cookie: {maxAge: 1000}}) }
|
|
239
|
+
|
|
240
|
+
store.ids(function (err, ids) {
|
|
241
|
+
if (err) return done(err)
|
|
242
|
+
assert.ok(Array.isArray(ids), 'ids should be an Array')
|
|
243
|
+
i = 10
|
|
244
|
+
ids.forEach(function (sid) {
|
|
245
|
+
assert.equal(sid, 'sess' + (--i), 'got expected key')
|
|
246
|
+
})
|
|
247
|
+
done()
|
|
248
|
+
})
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('should fetch all entries values', function (done) {
|
|
252
|
+
this.store = new session.MemoryStore()
|
|
253
|
+
var store = this.store
|
|
254
|
+
|
|
255
|
+
var k = 10
|
|
256
|
+
var i = 0
|
|
257
|
+
for (i = 0; i < k; i++) { store.set('sess-' + i, {cookie: {maxAge: 1000}, i: i}) }
|
|
258
|
+
|
|
259
|
+
store.all(function (err, all) {
|
|
260
|
+
if (err) return done(err)
|
|
261
|
+
assert.equal(typeof all, 'object', 'all should be an Object')
|
|
262
|
+
Object.keys(all).forEach(function (sid) {
|
|
263
|
+
var v = sid.split('-')[1]
|
|
264
|
+
assert.equal(all[sid].i, v, 'got expected value')
|
|
265
|
+
})
|
|
266
|
+
done()
|
|
267
|
+
})
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
it('should count all entries in the store', function (done) {
|
|
271
|
+
this.store = new session.MemoryStore()
|
|
272
|
+
var store = this.store
|
|
273
|
+
|
|
274
|
+
var k = 10
|
|
275
|
+
var i = 0
|
|
276
|
+
for (i = 0; i < k; i++) { store.set(i, {cookie: {maxAge: 1000}}) }
|
|
277
|
+
|
|
278
|
+
store.length(function (err, n) {
|
|
279
|
+
if (err) return done(err)
|
|
280
|
+
assert.equal(n, k, 'Got expected lenght')
|
|
281
|
+
done()
|
|
282
|
+
})
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
it('should delete all entries from the store', function (done) {
|
|
286
|
+
this.store = new session.MemoryStore()
|
|
287
|
+
var store = this.store
|
|
288
|
+
|
|
289
|
+
var k = 10
|
|
290
|
+
var i = 0
|
|
291
|
+
for (i = 0; i < k; i++) { store.set(i, {cookie: {maxAge: 1000}}) }
|
|
292
|
+
|
|
293
|
+
store.length(function (err, n) {
|
|
294
|
+
if (err) return done(err)
|
|
295
|
+
assert.equal(n, k, 'store is not empty')
|
|
296
|
+
})
|
|
297
|
+
store.clear()
|
|
298
|
+
store.length(function (err, n) {
|
|
299
|
+
if (err) return done(err)
|
|
300
|
+
assert.equal(n, 0, 'store should be empty')
|
|
301
|
+
done()
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
})
|