+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {SessionData, Store} from "express-session"

const noop = (_err?: unknown, _data?: any) => {}
type Callback = (_err?: unknown, _data?: any) => any

function optionalCb(err: unknown, data: unknown, cb?: Callback) {
if (cb) return cb(err, data)
if (err) throw err
return data
}

interface NormalizedRedisClient {
get(key: string): Promise<string | null>
Expand Down Expand Up @@ -96,93 +102,94 @@ export class RedisStore extends Store {
}
}

async get(sid: string, cb = noop) {
async get(sid: string, cb?: Callback) {
let key = this.prefix + sid
try {
let data = await this.client.get(key)
if (!data) return cb()
return cb(null, await this.serializer.parse(data))
if (!data) return optionalCb(null, null, cb)
return optionalCb(null, await this.serializer.parse(data), cb)
} catch (err) {
return cb(err)
return optionalCb(err, null, cb)
}
}

async set(sid: string, sess: SessionData, cb = noop) {
async set(sid: string, sess: SessionData, cb?: Callback) {
let key = this.prefix + sid
let ttl = this._getTTL(sess)
try {
if (ttl > 0) {
let val = this.serializer.stringify(sess)
if (this.disableTTL) await this.client.set(key, val)
else await this.client.set(key, val, ttl)
return cb()
return optionalCb(null, null, cb)
} else {
return this.destroy(sid, cb)
}
} catch (err) {
return cb(err)
return optionalCb(err, null, cb)
}
}

async touch(sid: string, sess: SessionData, cb = noop) {
async touch(sid: string, sess: SessionData, cb?: Callback) {
let key = this.prefix + sid
if (this.disableTouch || this.disableTTL) return cb()
if (this.disableTouch || this.disableTTL) return optionalCb(null, null, cb)
try {
await this.client.expire(key, this._getTTL(sess))
return cb()
return optionalCb(null, null, cb)
} catch (err) {
return cb(err)
return optionalCb(err, null, cb)
}
}

async destroy(sid: string, cb = noop) {
async destroy(sid: string, cb?: Callback) {
let key = this.prefix + sid
try {
await this.client.del([key])
return cb()
return optionalCb(null, null, cb)
} catch (err) {
return cb(err)
return optionalCb(err, null, cb)
}
}

async clear(cb = noop) {
async clear(cb?: Callback) {
try {
let keys = await this._getAllKeys()
if (!keys.length) return cb()
if (!keys.length) return optionalCb(null, null, cb)
await this.client.del(keys)
return cb()
return optionalCb(null, null, cb)
} catch (err) {
return cb(err)
return optionalCb(err, null, cb)
}
}

async length(cb = noop) {
async length(cb?: Callback) {
try {
let keys = await this._getAllKeys()
return cb(null, keys.length)
return optionalCb(null, keys.length, cb)
} catch (err) {
return cb(err)
return optionalCb(err, null, cb)
}
}

async ids(cb = noop) {
async ids(cb?: Callback) {
let len = this.prefix.length
try {
let keys = await this._getAllKeys()
return cb(
return optionalCb(
null,
keys.map((k) => k.substring(len)),
cb,
)
} catch (err) {
return cb(err)
return optionalCb(err, null, cb)
}
}

async all(cb = noop) {
async all(cb?: Callback) {
let len = this.prefix.length
try {
let keys = await this._getAllKeys()
if (keys.length === 0) return cb(null, [])
if (keys.length === 0) return optionalCb(null, [], cb)

let data = await this.client.mget(keys)
let results = data.reduce((acc, raw, idx) => {
Expand All @@ -192,9 +199,9 @@ export class RedisStore extends Store {
acc.push(sess)
return acc
}, [] as SessionData[])
return cb(null, results)
return optionalCb(null, results, cb)
} catch (err) {
return cb(err)
return optionalCb(err, null, cb)
}
}

Expand Down
54 changes: 26 additions & 28 deletions index_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Cookie} from "express-session"
import {Redis} from "ioredis"
import {promisify} from "node:util"
import {createClient} from "redis"
import {expect, test} from "vitest"
import {RedisStore} from "./"
Expand Down Expand Up @@ -44,73 +43,72 @@ test("ioredis", async () => {
test("teardown", redisSrv.disconnect)

async function lifecycleTest(store: RedisStore, client: any): Promise<void> {
const P = (f: any) => promisify(f).bind(store)
let res = await P(store.clear)()
let res = await store.clear()

let sess = {foo: "bar"}
await P(store.set)("123", sess)
let sess = {foo: "bar", cookie: {originalMaxAge: null}}
await store.set("123", sess)

res = await P(store.get)("123")
res = await store.get("123")
expect(res).toEqual(sess)

let ttl = await client.ttl("sess:123")
expect(ttl).toBeGreaterThanOrEqual(86399)

ttl = 60
let expires = new Date(Date.now() + ttl * 1000).toISOString()
await P(store.set)("456", {cookie: {expires}})
let expires = new Date(Date.now() + ttl * 1000)
await store.set("456", {cookie: {originalMaxAge: null, expires}})
ttl = await client.ttl("sess:456")
expect(ttl).toBeLessThanOrEqual(60)

ttl = 90
let expires2 = new Date(Date.now() + ttl * 1000).toISOString()
await P(store.touch)("456", {cookie: {expires: expires2}})
let expires2 = new Date(Date.now() + ttl * 1000)
await store.touch("456", {cookie: {originalMaxAge: null, expires: expires2}})
ttl = await client.ttl("sess:456")
expect(ttl).toBeGreaterThan(60)

res = await P(store.length)()
res = await store.length()
expect(res).toBe(2) // stored two keys length

res = await P(store.ids)()
res = await store.ids()
res.sort()
expect(res).toEqual(["123", "456"])

res = await P(store.all)()
res = await store.all()
res.sort((a: any, b: any) => (a.id > b.id ? 1 : -1))
expect(res).toEqual([
{id: "123", foo: "bar"},
{id: "456", cookie: {expires}},
{id: "123", foo: "bar", cookie: {originalMaxAge: null}},
{id: "456", cookie: {originalMaxAge: null, expires: expires.toISOString()}},
])

await P(store.destroy)("456")
res = await P(store.length)()
await store.destroy("456")
res = await store.length()
expect(res).toBe(1) // one key remains

res = await P(store.clear)()
res = await store.clear()

res = await P(store.length)()
res = await store.length()
expect(res).toBe(0) // no keys remain

let count = 1000
await load(store, count)

res = await P(store.length)()
res = await store.length()
expect(res).toBe(count)

await P(store.clear)()
res = await P(store.length)()
await store.clear()
res = await store.length()
expect(res).toBe(0)

expires = new Date(Date.now() + ttl * 1000).toISOString() // expires in the future
res = await P(store.set)("789", {cookie: {expires}})
expires = new Date(Date.now() + ttl * 1000) // expires in the future
res = await store.set("789", {cookie: {originalMaxAge: null, expires}})

res = await P(store.length)()
res = await store.length()
expect(res).toBe(1)

expires = new Date(Date.now() - ttl * 1000).toISOString() // expires in the past
await P(store.set)("789", {cookie: {expires}})
expires = new Date(Date.now() - ttl * 1000) // expires in the past
await store.set("789", {cookie: {originalMaxAge: null, expires}})

res = await P(store.length)()
res = await store.length()
expect(res).toBe(0) // no key remains and that includes session 789
}

Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载