|
| 1 | +import {SessionData, Store} from "express-session" |
| 2 | + |
| 3 | +const noop = (_err?: unknown, _data?: any) => {} |
| 4 | + |
| 5 | +interface NormalizedRedisClient { |
| 6 | + get(key: string): Promise<string | null> |
| 7 | + set(key: string, value: string, ttl?: number): Promise<string | null> |
| 8 | + expire(key: string, ttl: number): Promise<number | boolean> |
| 9 | + scanIterator(match: string, count: number): AsyncIterable<string> |
| 10 | + del(key: string[]): Promise<number> |
| 11 | + mget(key: string[]): Promise<(string | null)[]> |
| 12 | +} |
| 13 | + |
| 14 | +interface Serializer { |
| 15 | + parse(s: string): SessionData |
| 16 | + stringify(s: SessionData): string |
| 17 | +} |
| 18 | + |
| 19 | +interface RedisStoreOptions { |
| 20 | + client: any |
| 21 | + prefix?: string |
| 22 | + scanCount?: number |
| 23 | + serializer?: Serializer |
| 24 | + ttl?: number |
| 25 | + disableTTL?: boolean |
| 26 | + disableTouch?: boolean |
| 27 | +} |
| 28 | + |
| 29 | +class RedisStore extends Store { |
| 30 | + client: NormalizedRedisClient |
| 31 | + prefix: string |
| 32 | + scanCount: number |
| 33 | + serializer: Serializer |
| 34 | + ttl: number |
| 35 | + disableTTL: boolean |
| 36 | + disableTouch: boolean |
| 37 | + |
| 38 | + constructor(opts: RedisStoreOptions) { |
| 39 | + super() |
| 40 | + this.prefix = opts.prefix == null ? "sess:" : opts.prefix |
| 41 | + this.scanCount = opts.scanCount || 100 |
| 42 | + this.serializer = opts.serializer || JSON |
| 43 | + this.ttl = opts.ttl || 86400 // One day in seconds. |
| 44 | + this.disableTTL = opts.disableTTL || false |
| 45 | + this.disableTouch = opts.disableTouch || false |
| 46 | + this.client = this.normalizeClient(opts.client) |
| 47 | + } |
| 48 | + |
| 49 | + // Create a redis and ioredis compatible client |
| 50 | + private normalizeClient(client: any): NormalizedRedisClient { |
| 51 | + let isRedis = "scanIterator" in client |
| 52 | + return { |
| 53 | + get: (key) => client.get(key), |
| 54 | + set: (key, val, ttl) => { |
| 55 | + if (ttl) { |
| 56 | + return isRedis |
| 57 | + ? client.set(key, val, {EX: ttl}) |
| 58 | + : client.set(key, val, "EX", ttl) |
| 59 | + } |
| 60 | + return client.set(key, val) |
| 61 | + }, |
| 62 | + del: (key) => client.del(key), |
| 63 | + expire: (key, ttl) => client.expire(key, ttl), |
| 64 | + mget: (keys) => (isRedis ? client.mGet(keys) : client.mget(keys)), |
| 65 | + scanIterator: (match, count) => { |
| 66 | + if (isRedis) return client.scanIterator({MATCH: match, COUNT: count}) |
| 67 | + |
| 68 | + // ioredis impl. |
| 69 | + return (async function* () { |
| 70 | + let [c, xs] = await client.scan("0", "MATCH", match, "COUNT", count) |
| 71 | + for (let key of xs) yield key |
| 72 | + while (c !== "0") { |
| 73 | + ;[c, xs] = await client.scan(c, "MATCH", match, "COUNT", count) |
| 74 | + for (let key of xs) yield key |
| 75 | + } |
| 76 | + })() |
| 77 | + }, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async get(sid: string, cb = noop) { |
| 82 | + let key = this.prefix + sid |
| 83 | + try { |
| 84 | + let data = await this.client.get(key) |
| 85 | + if (!data) return cb() |
| 86 | + return cb(null, this.serializer.parse(data)) |
| 87 | + } catch (err) { |
| 88 | + return cb(err) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + async set(sid: string, sess: SessionData, cb = noop) { |
| 93 | + let key = this.prefix + sid |
| 94 | + let ttl = this._getTTL(sess) |
| 95 | + try { |
| 96 | + let val = this.serializer.stringify(sess) |
| 97 | + if (ttl > 0) { |
| 98 | + if (this.disableTTL) await this.client.set(key, val) |
| 99 | + else await this.client.set(key, val, ttl) |
| 100 | + return cb() |
| 101 | + } else { |
| 102 | + return this.destroy(sid, cb) |
| 103 | + } |
| 104 | + } catch (err) { |
| 105 | + return cb(err) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + async touch(sid: string, sess: SessionData, cb = noop) { |
| 110 | + let key = this.prefix + sid |
| 111 | + if (this.disableTouch || this.disableTTL) return cb() |
| 112 | + try { |
| 113 | + await this.client.expire(key, this._getTTL(sess)) |
| 114 | + return cb() |
| 115 | + } catch (err) { |
| 116 | + return cb(err) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + async destroy(sid: string, cb = noop) { |
| 121 | + let key = this.prefix + sid |
| 122 | + try { |
| 123 | + await this.client.del([key]) |
| 124 | + return cb() |
| 125 | + } catch (err) { |
| 126 | + return cb(err) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + async clear(cb = noop) { |
| 131 | + try { |
| 132 | + let keys = await this._getAllKeys() |
| 133 | + if (!keys.length) return cb() |
| 134 | + await this.client.del(keys) |
| 135 | + return cb() |
| 136 | + } catch (err) { |
| 137 | + return cb(err) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + async length(cb = noop) { |
| 142 | + try { |
| 143 | + let keys = await this._getAllKeys() |
| 144 | + return cb(null, keys.length) |
| 145 | + } catch (err) { |
| 146 | + return cb(err) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + async ids(cb = noop) { |
| 151 | + let len = this.prefix.length |
| 152 | + try { |
| 153 | + let keys = await this._getAllKeys() |
| 154 | + return cb( |
| 155 | + null, |
| 156 | + keys.map((k) => k.substring(len)) |
| 157 | + ) |
| 158 | + } catch (err) { |
| 159 | + return cb(err) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + async all(cb = noop) { |
| 164 | + let len = this.prefix.length |
| 165 | + try { |
| 166 | + let keys = await this._getAllKeys() |
| 167 | + if (keys.length === 0) return cb(null, []) |
| 168 | + |
| 169 | + let data = await this.client.mget(keys) |
| 170 | + let results = data.reduce((acc, raw, idx) => { |
| 171 | + if (!raw) return acc |
| 172 | + let sess = this.serializer.parse(raw) as any |
| 173 | + sess.id = keys[idx].substring(len) |
| 174 | + acc.push(sess) |
| 175 | + return acc |
| 176 | + }, [] as SessionData[]) |
| 177 | + return cb(null, results) |
| 178 | + } catch (err) { |
| 179 | + return cb(err) |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private _getTTL(sess: SessionData) { |
| 184 | + let ttl |
| 185 | + if (sess && sess.cookie && sess.cookie.expires) { |
| 186 | + let ms = Number(new Date(sess.cookie.expires)) - Date.now() |
| 187 | + ttl = Math.ceil(ms / 1000) |
| 188 | + } else { |
| 189 | + ttl = this.ttl |
| 190 | + } |
| 191 | + return ttl |
| 192 | + } |
| 193 | + |
| 194 | + private async _getAllKeys() { |
| 195 | + let pattern = this.prefix + "*" |
| 196 | + let keys = [] |
| 197 | + for await (let key of this.client.scanIterator(pattern, this.scanCount)) { |
| 198 | + keys.push(key) |
| 199 | + } |
| 200 | + return keys |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +export default RedisStore |
0 commit comments