-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathindex.ts
More file actions
197 lines (176 loc) · 5.03 KB
/
index.ts
File metadata and controls
197 lines (176 loc) · 5.03 KB
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
import {type SessionData, Store} from "express-session"
import type {RedisClientType, RedisClusterType} from "redis"
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 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: RedisClientType | RedisClusterType
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 = opts.client
}
async get(sid: string, cb?: Callback) {
let key = this.prefix + sid
try {
let data = await this.client.get(key)
if (!data) return optionalCb(null, null, cb)
return optionalCb(null, await this.serializer.parse(data), cb)
} catch (err) {
return optionalCb(err, null, cb)
}
}
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, {
expiration: {type: "EX", value: ttl},
})
return optionalCb(null, null, cb)
}
return this.destroy(sid, cb)
} catch (err) {
return optionalCb(err, null, cb)
}
}
async touch(sid: string, sess: SessionData, cb?: Callback) {
let key = this.prefix + sid
if (this.disableTouch || this.disableTTL) return optionalCb(null, null, cb)
try {
await this.client.expire(key, this.getTTL(sess))
return optionalCb(null, null, cb)
} catch (err) {
return optionalCb(err, null, cb)
}
}
async destroy(sid: string, cb?: Callback) {
let key = this.prefix + sid
try {
await this.client.del([key])
return optionalCb(null, null, cb)
} catch (err) {
return optionalCb(err, null, cb)
}
}
async clear(cb?: Callback) {
try {
let keys = await this.getAllKeys()
if (!keys.length) return optionalCb(null, null, cb)
await this.client.del(keys)
return optionalCb(null, null, cb)
} catch (err) {
return optionalCb(err, null, cb)
}
}
async length(cb?: Callback) {
try {
let keys = await this.getAllKeys()
return optionalCb(null, keys.length, cb)
} catch (err) {
return optionalCb(err, null, cb)
}
}
async ids(cb?: Callback) {
let len = this.prefix.length
try {
let keys = await this.getAllKeys()
return optionalCb(
null,
keys.map((k) => k.substring(len)),
cb,
)
} catch (err) {
return optionalCb(err, null, cb)
}
}
async all(cb?: Callback) {
let len = this.prefix.length
try {
let keys = await this.getAllKeys()
if (keys.length === 0) return optionalCb(null, [], cb)
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 optionalCb(null, results, cb)
} catch (err) {
return optionalCb(err, null, cb)
}
}
private getTTL(sess: SessionData) {
if (typeof this.ttl === "function") {
return this.ttl(sess)
}
let ttl
if (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 set = new Set<string>()
for await (let keys of this.scanIterator(pattern, this.scanCount)) {
for (let key of keys) {
set.add(key)
}
}
return set.size > 0 ? Array.from(set) : []
}
private scanIterator(match: string, count: number) {
let client = this.client
if (!("masters" in client)) {
return client.scanIterator({MATCH: match, COUNT: count})
}
return (async function* () {
for (let master of client.masters) {
let c = await client.nodeClient(master)
for await (let keys of c.scanIterator({
COUNT: count,
MATCH: match,
})) {
yield keys
}
}
})()
}
}