liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var assert = require('assert')
 
// express-session way
var MemoryStore = require('../')({Store: function () {}})
var session = {MemoryStore: MemoryStore}
 
describe('MemoryStore', function (done) {
  afterEach(function () {
    // runs after each test in this block
    this.store.stopInterval()
  })
 
  it('constructor should use default options', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
    assert.ok(store.options, 'should have an option object')
    assert.equal(store.options.max, Infinity, 'max option should be Infinity')
    assert.equal(store.options.checkPeriod, undefined, 'checkPeriod undefined by default')
    assert.ok(store.store, 'should have the LRU cache store')
    assert.equal(store._checkInterval, undefined, 'should not have the pruning loop')
    done()
  })
 
  it('should set options', function (done) {
    this.store = new session.MemoryStore({
      max: 10,
      checkPeriod: 10 * 1000,
      ttl: 36000,
      dispose: null,
      stale: true
    })
    var store = this.store
    assert.equal(store.options.max, 10, 'should set the max option')
    assert.equal(store.options.checkPeriod, 10 * 1000, 'should set checkPeriod')
    assert.equal(store.options.ttl, 36000, 'should set the TTL')
    assert.equal(store.options.dispose, null, 'should set dispose')
    assert.equal(store.options.stale, true, 'should set stale')
    done()
  })
 
  it('should not set the interval to check for expired entries by default', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
    assert.equal(store._checkInterval, undefined, 'should not exists')
    done()
  })
 
  it('should only contain 10 items', function (done) {
    this.store = new session.MemoryStore({max: 10})
    var store = this.store
 
    for (var i = 0; i < 15; i++) {
      store.set(i, {cookie: { expires: new Date((new Date()).valueOf() + 60 * 10 * 1000) }})
    }
 
    store.length(function (err, length) {
      if (err) return done(err)
      assert.equal(length, 10)
      done()
    })
  })
 
  it('should delete the first item', function (done) {
    this.store = new session.MemoryStore({max: 10})
    var store = this.store
 
    for (var i = 0; i < 15; i++) {
      store.set(i, {cookie: { expires: new Date((new Date()).valueOf() + 60 * 10 * 1000) }})
    }
 
    store.destroy(14)
 
    store.length(function (err, length) {
      if (err) return done(err)
      assert.equal(length, 9)
      done()
    })
  })
 
  it('should delete the last item', function (done) {
    this.store = new session.MemoryStore({max: 10})
    var store = this.store
 
    for (var i = 0; i < 10; i++) {
      store.set(i, {cookie: { expires: new Date((new Date()).valueOf() + 60 * 10 * 1000) }})
    }
 
    store.destroy(0)
    store.destroy(1)
 
    store.length(function (err, length) {
      if (err) return done(err)
      assert.equal(length, 8)
    })
 
    for (i = 10; i < 12; i++) {
      store.set(i, {cookie: { expires: new Date((new Date()).valueOf() + 60 * 10 * 1000) }})
    }
 
    store.length(function (err, length) {
      if (err) return done(err)
      assert.equal(length, 10)
      done()
    })
  })
 
  it('should set and get a sample entry', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    store.set('hello', {cookie: {}, sample: true})
    store.get('hello', function (err, val) {
      if (err) return done(err)
      assert.equal(val.sample, true, 'set and got expected value')
      done()
    })
  })
 
  it('should set TTL from cookie.maxAge', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    store.set('hello', {cookie: {maxAge: 400}, sample: true})
    store.get('hello', function (err, val) {
      if (err) return done(err)
      assert.equal(val.sample, true, 'entry should be valid')
    })
    setTimeout(function () {
      store.get('hello', function (err, val) {
        if (err) return done(err)
        assert.equal(val, undefined, 'entry should be expired')
        done()
      })
    }, 500)
  })
 
  it('should not get empty entry', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    store.get('', function (err, val) {
      if (err) return done(err)
      assert.equal(val, undefined)
      done()
    })
  })
 
  it('should not get a deleted entry', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    store.set('foo', {cookie: {}})
    store.get('foo', function (err, val) {
      if (err) return done(err)
      assert.ok(val, 'entry exists')
      store.destroy('foo')
      store.get('foo', function (err, val) {
        if (err) return done(err)
        assert.equal(val, undefined, 'entry actually deleted')
        done()
      })
    })
  })
 
  it('should not get an expired entry', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    store.set('hello', {cookie: {maxAge: 200}, sample: true})
    setTimeout(function () {
      store.get('hello', function (err, val) {
        if (err) return done(err)
        assert.equal(val, undefined, 'entry should be expired')
        done()
      })
    }, 300)
  })
 
  it('should enable automatic prune for expired entries', function (done) {
    this.store = new session.MemoryStore({checkPeriod: 300})
    var store = this.store
 
    store.set('foo', {cookie: {maxAge: 150}})
    store.set('bar', {cookie: {maxAge: 150}})
    store.length(function (err, count) {
      if (err) return done(err)
      assert.equal(count, 2, 'should count 2 entries')
    })
    setTimeout(function () {
      store.length(function (err, count) {
        if (err) return done(err)
        assert.equal(count, 0, 'expired entries should be pruned')
        done()
      })
    }, 500)
  })
 
  it('automatic check for expired entries should be disabled', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    store.set('foo', {cookie: {maxAge: 150}})
    store.set('bar', {cookie: {maxAge: 150}})
    store.length(function (err, count) {
      if (err) return done(err)
      assert.equal(count, 2, 'should count 2 entries')
    })
    setTimeout(function () {
      store.length(function (err, count) {
        if (err) return done(err)
        assert.equal(count, 2, 'expired entries should not be pruned')
        done()
      })
    }, 500)
  })
 
  it('should touch a given entry', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    store.set('hei', {cookie: {maxAge: 50}})
    store.touch('hei', {cookie: {maxAge: 300}})
    setTimeout(function () {
      store.get('hei', function (err, val) {
        if (err) return done(err)
        assert.ok(val, 'entry should be touched')
        done()
      })
    }, 200)
  })
 
  it('should fetch all entries Ids', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    var k = 10
    var i = 0
    for (i = 0; i < k; i++) { store.set('sess' + i, {cookie: {maxAge: 1000}}) }
 
    store.ids(function (err, ids) {
      if (err) return done(err)
      assert.ok(Array.isArray(ids), 'ids should be an Array')
      i = 10
      ids.forEach(function (sid) {
        assert.equal(sid, 'sess' + (--i), 'got expected key')
      })
      done()
    })
  })
 
  it('should fetch all entries values', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    var k = 10
    var i = 0
    for (i = 0; i < k; i++) { store.set('sess-' + i, {cookie: {maxAge: 1000}, i: i}) }
 
    store.all(function (err, all) {
      if (err) return done(err)
      assert.equal(typeof all, 'object', 'all should be an Object')
      Object.keys(all).forEach(function (sid) {
        var v = sid.split('-')[1]
        assert.equal(all[sid].i, v, 'got expected value')
      })
      done()
    })
  })
 
  it('should count all entries in the store', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    var k = 10
    var i = 0
    for (i = 0; i < k; i++) { store.set(i, {cookie: {maxAge: 1000}}) }
 
    store.length(function (err, n) {
      if (err) return done(err)
      assert.equal(n, k, 'Got expected lenght')
      done()
    })
  })
 
  it('should delete all entries from the store', function (done) {
    this.store = new session.MemoryStore()
    var store = this.store
 
    var k = 10
    var i = 0
    for (i = 0; i < k; i++) { store.set(i, {cookie: {maxAge: 1000}}) }
 
    store.length(function (err, n) {
      if (err) return done(err)
      assert.equal(n, k, 'store is not empty')
    })
    store.clear()
    store.length(function (err, n) {
      if (err) return done(err)
      assert.equal(n, 0, 'store should be empty')
      done()
    })
  })
})