Skip to content

Commit 818e94b

Browse files
authored
Revert concurrent sessions (#347)
This is causing data loss issues for users. Will need to revisit later. #346
1 parent 1be0cdf commit 818e94b

File tree

3 files changed

+30
-113
lines changed

3 files changed

+30
-113
lines changed

lib/connect-redis.js

Lines changed: 25 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module.exports = function (session) {
1010
// All callbacks should have a noop if none provided for compatibility
1111
// with the most Redis clients.
1212
const noop = () => {}
13-
const TOMBSTONE = "TOMBSTONE"
1413

1514
class RedisStore extends Store {
1615
constructor(options = {}) {
@@ -28,13 +27,12 @@ module.exports = function (session) {
2827
this.disableTouch = options.disableTouch || false
2928
}
3029

31-
get(sid, cb = noop, showTombs = false) {
30+
get(sid, cb = noop) {
3231
let key = this.prefix + sid
3332

3433
this.client.get(key, (err, data) => {
3534
if (err) return cb(err)
3635
if (!data) return cb()
37-
if (data === TOMBSTONE) return cb(null, showTombs ? data : undefined)
3836

3937
let result
4038
try {
@@ -47,40 +45,28 @@ module.exports = function (session) {
4745
}
4846

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

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

75-
if (ttl > 0) {
76-
this.client.set(args, cb)
77-
} else {
78-
// If the resulting TTL is negative we can delete / destroy the key
79-
this.destroy(sid, cb)
80-
}
81-
},
82-
true
83-
)
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+
}
8470
}
8571

8672
touch(sid, sess, cb = noop) {
@@ -95,9 +81,7 @@ module.exports = function (session) {
9581

9682
destroy(sid, cb = noop) {
9783
let key = this.prefix + sid
98-
this.client.set([key, TOMBSTONE, "EX", 300], (err) => {
99-
cb(err, 1)
100-
})
84+
this.client.del(key, cb)
10185
}
10286

10387
clear(cb = noop) {
@@ -108,9 +92,9 @@ module.exports = function (session) {
10892
}
10993

11094
length(cb = noop) {
111-
this.all((err, result) => {
95+
this._getAllKeys((err, keys) => {
11296
if (err) return cb(err)
113-
return cb(null, result.length)
97+
return cb(null, keys.length)
11498
})
11599
}
116100

@@ -137,7 +121,7 @@ module.exports = function (session) {
137121
let result
138122
try {
139123
result = sessions.reduce((accum, data, index) => {
140-
if (!data || data === TOMBSTONE) return accum
124+
if (!data) return accum
141125
data = this.serializer.parse(data)
142126
data.id = keys[index].substr(prefixLen)
143127
accum.push(data)
@@ -189,35 +173,3 @@ module.exports = function (session) {
189173

190174
return RedisStore
191175
}
192-
193-
/**
194-
* Simple object check.
195-
* @param item
196-
* @returns {boolean}
197-
*/
198-
function isObject(item) {
199-
return item && typeof item === "object" && !Array.isArray(item)
200-
}
201-
202-
/**
203-
* Deep merge two objects.
204-
* @param target
205-
* @param ...sources
206-
*/
207-
function mergeDeep(target, ...sources) {
208-
if (!sources.length) return target
209-
const source = sources.shift()
210-
211-
if (isObject(target) && isObject(source)) {
212-
for (const key in source) {
213-
if (isObject(source[key])) {
214-
if (!target[key]) Object.assign(target, { [key]: {} })
215-
mergeDeep(target[key], source[key])
216-
} else {
217-
Object.assign(target, { [key]: source[key] })
218-
}
219-
}
220-
}
221-
222-
return mergeDeep(target, ...sources)
223-
}

package.json

Lines changed: 0 additions & 1 deletion
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": "^2.0.5",
2221
"nyc": "^15.0.1",
2322
"prettier": "^2.0.5",
2423
"redis-mock": "^0.56.3",

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)