-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
index.js
443 lines (380 loc) · 13.7 KB
/
index.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// @ts-check
/// <reference types="node" />
'use strict';
const DEFAULT_PRUNE_INTERVAL_IN_SECONDS = 60 * 15;
const ONE_DAY = 86400;
/** @typedef {*} ExpressSession */
/** @typedef {*} ExpressSessionStore */
/**
* Inspired by util.callbackify()
*
* Never throws, even if callback is left out, as that's how it was
*
* @template T
* @param {Promise<T>} value
* @param {((err: Error|null, result: T) => void)|undefined} cb
* @returns {void}
*/
const callbackifyPromiseResolution = (value, cb) => {
if (!cb) {
// eslint-disable-next-line promise/prefer-await-to-then
value.catch(() => {});
} else {
// eslint-disable-next-line promise/catch-or-return, promise/prefer-await-to-then
value.then(
// eslint-disable-next-line unicorn/no-null
(ret) => process.nextTick(cb, null, ret),
(err) => process.nextTick(cb, err || new Error('Promise was rejected with falsy value'))
);
}
};
/** @returns {number} */
const currentTimestamp = () => Math.ceil(Date.now() / 1000);
/**
* @see https://www.postgresql.org/docs/9.5/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
* @param {string} value
* @returns {string}
*/
const escapePgIdentifier = (value) => value.replaceAll('"', '""');
/** @typedef {(err: Error|null) => void} SimpleErrorCallback */
/** @typedef {{ cookie: { maxAge?: number, expire?: number, [property: string]: any }, [property: string]: any }} SessionObject */
/** @typedef {(delay: number) => number} PGStorePruneDelayRandomizer */
/** @typedef {Object<string, any>} PGStoreQueryResult */
/** @typedef {(err: Error|null, firstRow?: PGStoreQueryResult) => void} PGStoreQueryCallback */
/**
* @typedef PGStoreOptions
* @property {string} [schemaName]
* @property {string} [tableName]
* @property {boolean} [createTableIfMissing]
* @property {number} [ttl]
* @property {boolean} [disableTouch]
* @property {typeof console.error} [errorLog]
* @property {import('pg').Pool} [pool]
* @property {*} [pgPromise]
* @property {string} [conString]
* @property {*} [conObject]
* @property {false|number} [pruneSessionInterval]
* @property {false|PGStorePruneDelayRandomizer} [pruneSessionRandomizedInterval]
*/
/**
* @param {ExpressSession} session
* @returns {ExpressSessionStore}
*/
module.exports = function connectPgSimple (session) {
/** @type {ExpressSessionStore} */
const Store = session.Store ||
// @ts-ignore
session.session.Store;
class PGStore extends Store {
/** @type {boolean} */
#createTableIfMissing;
/** @type {boolean} */
#disableTouch;
/** @type {typeof console.error} */
#errorLog;
/** @type {boolean} */
#ownsPg;
/** @type {*} */
#pgPromise;
/** @type {import('pg').Pool|undefined} */
#pool;
/** @type {false|number} */
#pruneSessionInterval;
/** @type {PGStorePruneDelayRandomizer|undefined} */
#pruneSessionRandomizedInterval;
/** @type {string|undefined} */
#schemaName;
/** @type {Promise<void>|undefined} */
#tableCreationPromise;
/** @type {string} */
#tableName;
/** @param {PGStoreOptions} options */
constructor (options = {}) {
super(options);
this.#schemaName = options.schemaName ? escapePgIdentifier(options.schemaName) : undefined;
this.#tableName = options.tableName ? escapePgIdentifier(options.tableName) : 'session';
if (!this.#schemaName && this.#tableName.includes('"."')) {
// eslint-disable-next-line no-console
console.warn('DEPRECATION WARNING: Schema should be provided through its dedicated "schemaName" option rather than through "tableName"');
this.#tableName = this.#tableName.replace(/^([^"]+)""\.""([^"]+)$/, '$1"."$2');
}
this.#createTableIfMissing = !!options.createTableIfMissing;
this.#tableCreationPromise = undefined;
this.ttl = options.ttl; // TODO: Make this private as well, some bug in at least TS 4.6.4 stops that
this.#disableTouch = !!options.disableTouch;
// eslint-disable-next-line no-console
this.#errorLog = options.errorLog || console.error.bind(console);
if (options.pool !== undefined) {
this.#pool = options.pool;
this.#ownsPg = false;
} else if (options.pgPromise !== undefined) {
if (typeof options.pgPromise.any !== 'function') {
throw new TypeError('`pgPromise` config must point to an existing and configured instance of pg-promise pointing at your database');
}
this.#pgPromise = options.pgPromise;
this.#ownsPg = false;
} else {
// eslint-disable-next-line n/no-process-env
const conString = options.conString || process.env['DATABASE_URL'];
let conObject = options.conObject;
if (!conObject) {
conObject = {};
if (conString) {
conObject.connectionString = conString;
}
}
this.#pool = new (require('pg')).Pool(conObject);
this.#pool.on('error', err => {
this.#errorLog('PG Pool error:', err);
});
this.#ownsPg = true;
}
if (options.pruneSessionInterval === false) {
this.#pruneSessionInterval = false;
} else {
this.#pruneSessionInterval = (options.pruneSessionInterval || DEFAULT_PRUNE_INTERVAL_IN_SECONDS) * 1000;
if (options.pruneSessionRandomizedInterval !== false) {
this.#pruneSessionRandomizedInterval = (
options.pruneSessionRandomizedInterval ||
// Results in at least 50% of the specified interval and at most 150%. Makes it so that multiple instances doesn't all prune at the same time.
(delay => Math.ceil(delay / 2 + delay * Math.random()))
);
}
}
}
/**
* Ensures the session store table exists, creating it if its missing
*
* @access private
* @returns {Promise<void>}
*/
async _rawEnsureSessionStoreTable () {
const quotedTable = this.quotedTable();
const res = await this._asyncQuery('SELECT to_regclass($1::text)', [quotedTable], true);
if (res && res['to_regclass'] === null) {
const pathModule = require('node:path');
const fs = require('node:fs').promises;
const tableDefString = await fs.readFile(pathModule.resolve(__dirname, './table.sql'), 'utf8');
const tableDefModified = tableDefString.replaceAll('"session"', quotedTable);
await this._asyncQuery(tableDefModified, [], true);
}
}
/**
* Ensures the session store table exists, creating it if its missing
*
* @access private
* @param {boolean|undefined} noTableCreation
* @returns {Promise<void>}
*/
async _ensureSessionStoreTable (noTableCreation) {
if (noTableCreation || this.#createTableIfMissing === false) return;
if (!this.#tableCreationPromise) {
this.#tableCreationPromise = this._rawEnsureSessionStoreTable();
}
return this.#tableCreationPromise;
}
/**
* Closes the session store
*
* Currently only stops the automatic pruning, if any, from continuing
*
* @access public
* @returns {Promise<void>}
*/
async close () {
this.closed = true;
this.#clearPruneTimer();
if (this.#ownsPg && this.#pool) {
await this.#pool.end();
}
}
#initPruneTimer () {
if (this.#pruneSessionInterval && !this.closed && !this.pruneTimer) {
const delay = this.#pruneSessionRandomizedInterval
? this.#pruneSessionRandomizedInterval(this.#pruneSessionInterval)
: this.#pruneSessionInterval;
this.pruneTimer = setTimeout(
() => { this.pruneSessions(); },
delay
);
this.pruneTimer.unref();
}
}
#clearPruneTimer () {
if (this.pruneTimer) {
clearTimeout(this.pruneTimer);
this.pruneTimer = undefined;
}
}
/**
* Does garbage collection for expired session in the database
*
* @param {SimpleErrorCallback} [fn] - standard Node.js callback called on completion
* @returns {void}
* @access public
*/
pruneSessions (fn) {
this.query('DELETE FROM ' + this.quotedTable() + ' WHERE expire < to_timestamp($1)', [currentTimestamp()], err => {
if (fn && typeof fn === 'function') {
return fn(err);
}
if (err) {
this.#errorLog('Failed to prune sessions:', err);
}
this.#clearPruneTimer();
this.#initPruneTimer();
});
}
/**
* Get the quoted table.
*
* @returns {string} the quoted schema + table for use in queries
* @access private
*/
quotedTable () {
let result = '"' + this.#tableName + '"';
if (this.#schemaName) {
result = '"' + this.#schemaName + '".' + result;
}
return result;
}
/**
* Figure out when a session should expire
*
* @param {SessionObject} sess – the session object to store
* @returns {number} the unix timestamp, in seconds
* @access private
*/
#getExpireTime (sess) {
let expire;
if (sess && sess.cookie && sess.cookie['expires']) {
const expireDate = new Date(sess.cookie['expires']);
expire = Math.ceil(expireDate.valueOf() / 1000);
} else {
const ttl = this.ttl || ONE_DAY;
expire = Math.ceil(Date.now() / 1000 + ttl);
}
return expire;
}
/**
* Query the database.
*
* @param {string} query - the database query to perform
* @param {any[]} [params] - the parameters of the query
* @param {boolean} [noTableCreation]
* @returns {Promise<PGStoreQueryResult|undefined>}
* @access private
*/
async _asyncQuery (query, params, noTableCreation) {
await this._ensureSessionStoreTable(noTableCreation);
if (this.#pgPromise) {
const res = await this.#pgPromise.any(query, params);
return res && res[0] ? res[0] : undefined;
} else {
if (!this.#pool) throw new Error('Pool missing for some reason');
const res = await this.#pool.query(query, params);
return res && res.rows && res.rows[0] ? res.rows[0] : undefined;
}
}
/**
* Query the database.
*
* @param {string} query - the database query to perform
* @param {any[]|PGStoreQueryCallback} [params] - the parameters of the query or the callback function
* @param {PGStoreQueryCallback} [fn] - standard Node.js callback returning the resulting rows
* @param {boolean} [noTableCreation]
* @returns {void}
* @access private
*/
query (query, params, fn, noTableCreation) {
/** @type {any[]} */
let resolvedParams;
if (typeof params === 'function') {
if (fn) throw new Error('Two callback functions set at once');
fn = params;
resolvedParams = [];
} else {
resolvedParams = params || [];
}
const result = this._asyncQuery(query, resolvedParams, noTableCreation);
callbackifyPromiseResolution(result, fn);
}
/**
* Attempt to fetch session by the given `sid`.
*
* @param {string} sid – the session id
* @param {(err: Error|null, firstRow?: PGStoreQueryResult) => void} fn – a standard Node.js callback returning the parsed session object
* @access public
*/
get (sid, fn) {
this.#initPruneTimer();
this.query('SELECT sess FROM ' + this.quotedTable() + ' WHERE sid = $1 AND expire >= to_timestamp($2)', [sid, currentTimestamp()], (err, data) => {
if (err) { return fn(err); }
// eslint-disable-next-line unicorn/no-null
if (!data) { return fn(null); }
try {
// eslint-disable-next-line unicorn/no-null
return fn(null, (typeof data['sess'] === 'string') ? JSON.parse(data['sess']) : data['sess']);
} catch {
return this.destroy(sid, fn);
}
});
}
/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {string} sid – the session id
* @param {SessionObject} sess – the session object to store
* @param {SimpleErrorCallback} fn – a standard Node.js callback returning the parsed session object
* @access public
*/
set (sid, sess, fn) {
this.#initPruneTimer();
const expireTime = this.#getExpireTime(sess);
const query = 'INSERT INTO ' + this.quotedTable() + ' (sess, expire, sid) SELECT $1, to_timestamp($2), $3 ON CONFLICT (sid) DO UPDATE SET sess=$1, expire=to_timestamp($2) RETURNING sid';
this.query(
query,
[sess, expireTime, sid],
err => { fn && fn(err); }
);
}
/**
* Destroy the session associated with the given `sid`.
*
* @param {string} sid – the session id
* @param {SimpleErrorCallback} fn – a standard Node.js callback returning the parsed session object
* @access public
*/
destroy (sid, fn) {
this.#initPruneTimer();
this.query(
'DELETE FROM ' + this.quotedTable() + ' WHERE sid = $1',
[sid],
err => { fn && fn(err); }
);
}
/**
* Touch the given session object associated with the given session ID.
*
* @param {string} sid – the session id
* @param {SessionObject} sess – the session object to store
* @param {SimpleErrorCallback} fn – a standard Node.js callback returning the parsed session object
* @access public
*/
touch (sid, sess, fn) {
this.#initPruneTimer();
if (this.#disableTouch) {
// eslint-disable-next-line unicorn/no-null
fn && fn(null);
return;
}
const expireTime = this.#getExpireTime(sess);
this.query(
'UPDATE ' + this.quotedTable() + ' SET expire = to_timestamp($1) WHERE sid = $2 RETURNING sid',
[expireTime, sid],
err => { fn && fn(err); }
);
}
}
return PGStore;
};