Skip to content

Commit b1a2fed

Browse files
committed
Revert "Fix merging bug in 6.1.0 that resulted in sessions being deleted. (#348)"
This reverts commit bfb6b35.
1 parent 1da4026 commit b1a2fed

3 files changed

Lines changed: 30 additions & 86 deletions

File tree

lib/connect-redis.js

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
* MIT Licensed
55
*/
66

7-
const deepmerge = require("deepmerge")
8-
97
module.exports = function (session) {
108
const Store = session.Store
119

1210
// All callbacks should have a noop if none provided for compatibility
1311
// with the most Redis clients.
1412
const noop = () => {}
15-
const TOMBSTONE = "TOMBSTONE"
1613

1714
class RedisStore extends Store {
1815
constructor(options = {}) {
@@ -30,13 +27,12 @@ module.exports = function (session) {
3027
this.disableTouch = options.disableTouch || false
3128
}
3229

33-
get(sid, cb = noop, showTombs = false) {
30+
get(sid, cb = noop) {
3431
let key = this.prefix + sid
3532

3633
this.client.get(key, (err, data) => {
3734
if (err) return cb(err)
3835
if (!data) return cb()
39-
if (data === TOMBSTONE) return cb(null, showTombs ? data : undefined)
4036

4137
let result
4238
try {
@@ -49,40 +45,28 @@ module.exports = function (session) {
4945
}
5046

5147
set(sid, sess, cb = noop) {
52-
this.get(
53-
sid,
54-
(err, oldSess) => {
55-
if (oldSess === TOMBSTONE) {
56-
return cb()
57-
} else if (oldSess && oldSess.lastModified !== sess.lastModified) {
58-
sess = deepmerge(oldSess, sess)
59-
}
60-
let args = [this.prefix + sid]
61-
let value
62-
sess.lastModified = Date.now()
63-
try {
64-
value = this.serializer.stringify(sess)
65-
} catch (er) {
66-
return cb(er)
67-
}
68-
args.push(value)
69-
args.push("EX", this._getTTL(sess))
48+
let args = [this.prefix + sid]
7049

71-
let ttl = 1
72-
if (!this.disableTTL) {
73-
ttl = this._getTTL(sess)
74-
args.push("EX", ttl)
75-
}
50+
let value
51+
try {
52+
value = this.serializer.stringify(sess)
53+
} catch (er) {
54+
return cb(er)
55+
}
56+
args.push(value)
7657

77-
if (ttl > 0) {
78-
this.client.set(args, cb)
79-
} else {
80-
// If the resulting TTL is negative we can delete / destroy the key
81-
this.destroy(sid, cb)
82-
}
83-
},
84-
true
85-
)
58+
let ttl = 1
59+
if (!this.disableTTL) {
60+
ttl = this._getTTL(sess)
61+
args.push("EX", ttl)
62+
}
63+
64+
if (ttl > 0) {
65+
this.client.set(args, cb)
66+
} else {
67+
// If the resulting TTL is negative we can delete / destroy the key
68+
this.destroy(sid, cb)
69+
}
8670
}
8771

8872
touch(sid, sess, cb = noop) {
@@ -97,9 +81,7 @@ module.exports = function (session) {
9781

9882
destroy(sid, cb = noop) {
9983
let key = this.prefix + sid
100-
this.client.set([key, TOMBSTONE, "EX", 300], (err) => {
101-
cb(err, 1)
102-
})
84+
this.client.del(key, cb)
10385
}
10486

10587
clear(cb = noop) {
@@ -110,9 +92,9 @@ module.exports = function (session) {
11092
}
11193

11294
length(cb = noop) {
113-
this.all((err, result) => {
95+
this._getAllKeys((err, keys) => {
11496
if (err) return cb(err)
115-
return cb(null, result.length)
97+
return cb(null, keys.length)
11698
})
11799
}
118100

@@ -139,7 +121,7 @@ module.exports = function (session) {
139121
let result
140122
try {
141123
result = sessions.reduce((accum, data, index) => {
142-
if (!data || data === TOMBSTONE) return accum
124+
if (!data) return accum
143125
data = this.serializer.parse(data)
144126
data.id = keys[index].substr(prefixLen)
145127
accum.push(data)

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"eslint-config-prettier": "^8.3.0",
1919
"express-session": "^1.17.0",
2020
"ioredis": "^4.17.1",
21-
"mockdate": "^3.0.5",
2221
"nyc": "^15.0.1",
2322
"prettier": "^2.0.5",
2423
"redis-mock": "^0.56.3",
@@ -37,9 +36,6 @@
3736
"fmt": "prettier --write .",
3837
"fmt-check": "prettier --check ."
3938
},
40-
"dependencies": {
41-
"deepmerge": "^4.2.2"
42-
},
4339
"keywords": [
4440
"connect",
4541
"redis",

test/connect-redis-test.js

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ const redisV3 = require("redis-v3")
55
const redisV4 = require("redis-v4")
66
const ioRedis = require("ioredis")
77
const redisMock = require("redis-mock")
8-
const MockDate = require("mockdate")
98

109
let RedisStore = require("../")(session)
11-
MockDate.set("2000-11-22")
1210

1311
let p =
1412
(ctx, method) =>
@@ -72,34 +70,11 @@ test("redis-mock client", async (t) => {
7270
test("teardown", redisSrv.disconnect)
7371

7472
async function lifecycleTest(store, t) {
75-
await p(store, "set")("123", { foo: "bar3" })
76-
let res = await p(store, "get")("123")
77-
t.same(res, { foo: "bar3", lastModified: 974851200000 }, "get value 1")
78-
await p(store, "set")("123", {
79-
foo: "bar3",
80-
luke: "skywalker",
81-
obi: "wan",
82-
lastModified: 974851000000,
83-
})
84-
await p(store, "set")("123", {
85-
luke: "skywalker",
86-
lastModified: 974851000000,
87-
})
88-
res = await p(store, "get")("123")
89-
t.same(
90-
res,
91-
{ foo: "bar3", luke: "skywalker", obi: "wan", lastModified: 974851200000 },
92-
"get merged value"
93-
)
94-
95-
res = await p(store, "clear")()
96-
t.ok(res >= 1, "cleared key")
97-
98-
res = await p(store, "set")("123", { foo: "bar" })
73+
let res = await p(store, "set")("123", { foo: "bar" })
9974
t.equal(res, "OK", "set value")
10075

10176
res = await p(store, "get")("123")
102-
t.same(res, { foo: "bar", lastModified: 974851200000 }, "get value")
77+
t.same(res, { foo: "bar" }, "get value")
10378

10479
res = await p(store.client, "ttl")("sess:123")
10580
t.ok(res >= 86399, "check one day ttl")
@@ -133,29 +108,20 @@ async function lifecycleTest(store, t) {
133108
t.same(
134109
res,
135110
[
136-
{ id: "123", foo: "bar", lastModified: 974851200000 },
137-
{ id: "456", cookie: { expires }, lastModified: 974851200000 },
111+
{ id: "123", foo: "bar" },
112+
{ id: "456", cookie: { expires } },
138113
],
139114
"stored two keys data"
140115
)
141116

142117
res = await p(store, "destroy")("456")
143118
t.equal(res, 1, "destroyed one")
144119

145-
res = await p(store, "get")("456")
146-
t.equal(res, undefined, "tombstoned one")
147-
148-
res = await p(store, "set")("456", { a: "new hope" })
149-
t.equal(res, undefined, "tombstoned set")
150-
151-
res = await p(store, "get")("456")
152-
t.equal(res, undefined, "tombstoned two")
153-
154120
res = await p(store, "length")()
155121
t.equal(res, 1, "one key remains")
156122

157123
res = await p(store, "clear")()
158-
t.equal(res, 2, "cleared remaining key")
124+
t.equal(res, 1, "cleared remaining key")
159125

160126
res = await p(store, "length")()
161127
t.equal(res, 0, "no key remains")

0 commit comments

Comments
 (0)