Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: tj/connect-redis
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: WasabiApp/connect-redis
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 4 commits
  • 3 files changed
  • 2 contributors

Commits on Nov 29, 2021

  1. Fixes for v4 support

    BarryCarlyon committed Nov 29, 2021
    Copy the full SHA
    342350a View commit details
  2. Strip out my "test" comments

    Update redis to v4 in package.json
    Update readme to include the new connect command
    BarryCarlyon committed Nov 29, 2021
    Copy the full SHA
    2973e64 View commit details
  3. Fix lints and fmt's

    BarryCarlyon committed Nov 29, 2021
    Copy the full SHA
    8601b5f View commit details

Commits on Dec 6, 2021

  1. Merge pull request #1 from BarryCarlyon/supportv4

    Supportv4
    ashoksahoo authored Dec 6, 2021
    Copy the full SHA
    b14aa86 View commit details
Showing with 97 additions and 55 deletions.
  1. +95 −54 lib/connect-redis.js
  2. +1 −1 package.json
  3. +1 −0 readme.md
149 changes: 95 additions & 54 deletions lib/connect-redis.js
Original file line number Diff line number Diff line change
@@ -31,19 +31,30 @@ module.exports = function (session) {
get(sid, cb = noop, showTombs = false) {
let key = this.prefix + sid

this.client.get(key, (err, data) => {
if (err) return cb(err)
if (!data) return cb()
if (data === TOMBSTONE) return cb(null, showTombs ? data : undefined)
this.client
.GET(key)
.then((data) => {
if (!data) {
cb()
return
}
if (data === TOMBSTONE) {
cb(null, showTombs ? data : undefined)
return
}

let result
try {
result = this.serializer.parse(data)
} catch (err) {
return cb(err)
}
return cb(null, result)
})
let result
try {
result = this.serializer.parse(data)
} catch (err) {
cb(err)
return
}
cb(null, result)
})
.catch((err) => {
cb(err)
})
}

set(sid, sess, cb = noop) {
@@ -55,7 +66,7 @@ module.exports = function (session) {
} else if (oldSess && oldSess.lastModified !== sess.lastModified) {
sess = mergeDeep(oldSess, sess)
}
let args = [this.prefix + sid]
let args = ['SET', this.prefix + sid]
let value
sess.lastModified = Date.now()
try {
@@ -73,7 +84,14 @@ module.exports = function (session) {
}

if (ttl > 0) {
this.client.set(args, cb)
this.client
.sendCommand(args)
.then((r) => {
cb(null, r)
})
.catch((err) => {
cb(err)
})
} else {
// If the resulting TTL is negative we can delete / destroy the key
this.destroy(sid, cb)
@@ -86,24 +104,40 @@ module.exports = function (session) {
touch(sid, sess, cb = noop) {
if (this.disableTouch || this.disableTTL) return cb()
let key = this.prefix + sid
this.client.expire(key, this._getTTL(sess), (err, ret) => {
if (err) return cb(err)
if (ret !== 1) return cb(null, 'EXPIRED')
cb(null, 'OK')
})
this.client
.EXPIRE(key, this._getTTL(sess))
.then((ret) => {
if (ret !== 1) return cb(null, 'EXPIRED')
return cb(null, 'OK')
})
.catch((err) => {
return cb(err)
})
}

destroy(sid, cb = noop) {
let key = this.prefix + sid
this.client.set([key, TOMBSTONE, 'EX', 300], (err) => {
cb(err, 1)
})
this.client
.sendCommand(['SET', key, TOMBSTONE, 'EX', '300'])
.then((r) => {
cb(r)
})
.catch((err) => {
cb(err, 1)
})
}

clear(cb = noop) {
this._getAllKeys((err, keys) => {
if (err) return cb(err)
this.client.del(keys, cb)
this.client
.DEL(keys)
.then((r) => {
cb(r)
})
.catch((err) => {
cb(err)
})
})
}

@@ -131,23 +165,26 @@ module.exports = function (session) {
if (err) return cb(err)
if (keys.length === 0) return cb(null, [])

this.client.mget(keys, (err, sessions) => {
if (err) return cb(err)

let result
try {
result = sessions.reduce((accum, data, index) => {
if (!data || data === TOMBSTONE) return accum
data = this.serializer.parse(data)
data.id = keys[index].substr(prefixLen)
accum.push(data)
return accum
}, [])
} catch (e) {
err = e
}
return cb(err, result)
})
this.client
.MGET(keys)
.then((sessions) => {
let result
try {
result = sessions.reduce((accum, data, index) => {
if (!data || data === TOMBSTONE) return accum
data = this.serializer.parse(data)
data.id = keys[index].substr(prefixLen)
accum.push(data)
return accum
}, [])
} catch (e) {
err = e
}
return cb(err, result)
})
.catch((err) => {
return cb(err)
})
})
}

@@ -159,7 +196,8 @@ module.exports = function (session) {
} else {
ttl = this.ttl
}
return ttl
// v4 cast to string
return '' + ttl
}

_getAllKeys(cb = noop) {
@@ -169,21 +207,24 @@ module.exports = function (session) {

_scanKeys(keys = {}, cursor, pattern, count, cb = noop) {
let args = [cursor, 'match', pattern, 'count', count]
this.client.scan(args, (err, data) => {
if (err) return cb(err)

let [nextCursorId, scanKeys] = data
for (let key of scanKeys) {
keys[key] = true
}
this.client
.SCAN(args)
.then((data) => {
let [nextCursorId, scanKeys] = data
for (let key of scanKeys) {
keys[key] = true
}

// This can be a string or a number. We check both.
if (Number(nextCursorId) !== 0) {
return this._scanKeys(keys, nextCursorId, pattern, count, cb)
}
// This can be a string or a number. We check both.
if (Number(nextCursorId) !== 0) {
return this._scanKeys(keys, nextCursorId, pattern, count, cb)
}

cb(null, Object.keys(keys))
})
cb(null, Object.keys(keys))
})
.catch((err) => {
return cb(err)
})
}
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
"mockdate": "^2.0.5",
"nyc": "^15.0.1",
"prettier": "^2.0.5",
"redis": "^3.1.2",
"redis": "^4.0.0",
"redis-mock": "^0.56.3"
},
"engines": {
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ const session = require('express-session')

let RedisStore = require('connect-redis')(session)
let redisClient = redis.createClient()
redisClient.connect()

app.use(
session({