-
Notifications
You must be signed in to change notification settings - Fork 342
/
index.ts
206 lines (186 loc) · 5.3 KB
/
index.ts
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
import {SessionData, Store} from "express-session"
const noop = (_err?: unknown, _data?: any) => {}
interface NormalizedRedisClient {
get(key: string): Promise<string | null>
set(key: string, value: string, ttl?: number): Promise<string | null>
expire(key: string, ttl: number): Promise<number | boolean>
scanIterator(match: string, count: number): AsyncIterable<string>
del(key: string[]): Promise<number>
mget(key: string[]): Promise<(string | null)[]>
}
interface Serializer {
parse(s: string): SessionData | Promise<SessionData>
stringify(s: SessionData): string
}
interface RedisStoreOptions {
client: any
prefix?: string
scanCount?: number
serializer?: Serializer
ttl?: number | {(sess: SessionData): number}
disableTTL?: boolean
disableTouch?: boolean
}
export class RedisStore extends Store {
client: NormalizedRedisClient
prefix: string
scanCount: number
serializer: Serializer
ttl: number | {(sess: SessionData): number}
disableTTL: boolean
disableTouch: boolean
constructor(opts: RedisStoreOptions) {
super()
this.prefix = opts.prefix == null ? "sess:" : opts.prefix
this.scanCount = opts.scanCount || 100
this.serializer = opts.serializer || JSON
this.ttl = opts.ttl || 86400 // One day in seconds.
this.disableTTL = opts.disableTTL || false
this.disableTouch = opts.disableTouch || false
this.client = this.normalizeClient(opts.client)
}
// Create a redis and ioredis compatible client
private normalizeClient(client: any): NormalizedRedisClient {
let isRedis = "scanIterator" in client
return {
get: (key) => client.get(key),
set: (key, val, ttl) => {
if (ttl) {
return isRedis
? client.set(key, val, {EX: ttl})
: client.set(key, val, "EX", ttl)
}
return client.set(key, val)
},
del: (key) => client.del(key),
expire: (key, ttl) => client.expire(key, ttl),
mget: (keys) => (isRedis ? client.mGet(keys) : client.mget(keys)),
scanIterator: (match, count) => {
if (isRedis) return client.scanIterator({MATCH: match, COUNT: count})
// ioredis impl.
return (async function* () {
let [c, xs] = await client.scan("0", "MATCH", match, "COUNT", count)
for (let key of xs) yield key
while (c !== "0") {
;[c, xs] = await client.scan(c, "MATCH", match, "COUNT", count)
for (let key of xs) yield key
}
})()
},
}
}
async get(sid: string, cb = noop) {
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))
} catch (err) {
return cb(err)
}
}
async set(sid: string, sess: SessionData, cb = noop) {
let key = this.prefix + sid
let ttl = this._getTTL(sess)
try {
let val = this.serializer.stringify(sess)
if (ttl > 0) {
if (this.disableTTL) await this.client.set(key, val)
else await this.client.set(key, val, ttl)
return cb()
} else {
return this.destroy(sid, cb)
}
} catch (err) {
return cb(err)
}
}
async touch(sid: string, sess: SessionData, cb = noop) {
let key = this.prefix + sid
if (this.disableTouch || this.disableTTL) return cb()
try {
await this.client.expire(key, this._getTTL(sess))
return cb()
} catch (err) {
return cb(err)
}
}
async destroy(sid: string, cb = noop) {
let key = this.prefix + sid
try {
await this.client.del([key])
return cb()
} catch (err) {
return cb(err)
}
}
async clear(cb = noop) {
try {
let keys = await this._getAllKeys()
if (!keys.length) return cb()
await this.client.del(keys)
return cb()
} catch (err) {
return cb(err)
}
}
async length(cb = noop) {
try {
let keys = await this._getAllKeys()
return cb(null, keys.length)
} catch (err) {
return cb(err)
}
}
async ids(cb = noop) {
let len = this.prefix.length
try {
let keys = await this._getAllKeys()
return cb(
null,
keys.map((k) => k.substring(len)),
)
} catch (err) {
return cb(err)
}
}
async all(cb = noop) {
let len = this.prefix.length
try {
let keys = await this._getAllKeys()
if (keys.length === 0) return cb(null, [])
let data = await this.client.mget(keys)
let results = data.reduce((acc, raw, idx) => {
if (!raw) return acc
let sess = this.serializer.parse(raw) as any
sess.id = keys[idx].substring(len)
acc.push(sess)
return acc
}, [] as SessionData[])
return cb(null, results)
} catch (err) {
return cb(err)
}
}
private _getTTL(sess: SessionData) {
if (typeof this.ttl === "function") {
return this.ttl(sess)
}
let ttl
if (sess && sess.cookie && sess.cookie.expires) {
let ms = Number(new Date(sess.cookie.expires)) - Date.now()
ttl = Math.ceil(ms / 1000)
} else {
ttl = this.ttl
}
return ttl
}
private async _getAllKeys() {
let pattern = this.prefix + "*"
let keys = []
for await (let key of this.client.scanIterator(pattern, this.scanCount)) {
keys.push(key)
}
return keys
}
}