@prosopo/database 3.15.19 → 3.15.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +20 -17
- package/.turbo/turbo-build$colon$tsc.log +16 -16
- package/.turbo/turbo-build.log +21 -18
- package/CHANGELOG.md +17 -0
- package/dist/_virtual/_rolldown/runtime.js +3 -0
- package/dist/base/index.js +1 -4
- package/dist/base/mongo.js +133 -137
- package/dist/base/mongoMemory.js +21 -21
- package/dist/cjs/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/cjs/base/index.cjs +4 -6
- package/dist/cjs/base/mongo.cjs +140 -143
- package/dist/cjs/base/mongoMemory.cjs +22 -22
- package/dist/cjs/databases/captcha.cjs +213 -256
- package/dist/cjs/databases/centralDbStreamer.cjs +148 -170
- package/dist/cjs/databases/client.cjs +46 -62
- package/dist/cjs/databases/index.cjs +19 -19
- package/dist/cjs/databases/provider.cjs +1827 -2251
- package/dist/cjs/index.cjs +25 -23
- package/dist/cjs/mongooseOptions.cjs +59 -58
- package/dist/cjs/redisCache.cjs +432 -474
- package/dist/databases/captcha.js +210 -253
- package/dist/databases/centralDbStreamer.js +147 -169
- package/dist/databases/client.js +44 -60
- package/dist/databases/index.js +10 -14
- package/dist/databases/provider.d.ts +4 -4
- package/dist/databases/provider.d.ts.map +1 -1
- package/dist/databases/provider.js +1823 -2247
- package/dist/databases/provider.js.map +1 -1
- package/dist/index.js +9 -18
- package/dist/mongooseOptions.js +59 -59
- package/dist/redisCache.js +432 -474
- package/package.json +14 -14
package/dist/cjs/redisCache.cjs
CHANGED
|
@@ -1,476 +1,434 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
//#region src/redisCache.ts
|
|
2
|
+
var BIGINT_TAG = "__bigint__:";
|
|
3
|
+
/** JSON replacer that tags BigInt values so they can be restored by bigIntReviver. */
|
|
4
|
+
var bigIntReplacer = (_key, value) => "bigint" === typeof value ? `${BIGINT_TAG}${value.toString()}` : value;
|
|
5
|
+
/** JSON reviver that restores tagged BigInt values produced by bigIntReplacer. */
|
|
6
|
+
var bigIntReviver = (_key, value) => "string" === typeof value && value.startsWith(BIGINT_TAG) ? BigInt(value.slice(11)) : value;
|
|
7
|
+
var SESSION_KEY_PATTERNS = [
|
|
8
|
+
"cache:session:*",
|
|
9
|
+
"writeq:session:*",
|
|
10
|
+
"writeq:session:pending"
|
|
10
11
|
];
|
|
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
|
-
timeoutMs
|
|
434
|
-
)
|
|
435
|
-
)
|
|
436
|
-
]);
|
|
437
|
-
} catch (error) {
|
|
438
|
-
this.logger.warn(() => ({
|
|
439
|
-
msg: "Skipped Redis session cleanup — Redis not reachable",
|
|
440
|
-
err: error
|
|
441
|
-
}));
|
|
442
|
-
return;
|
|
443
|
-
}
|
|
444
|
-
try {
|
|
445
|
-
let totalDeleted = 0;
|
|
446
|
-
for (const pattern of SESSION_KEY_PATTERNS) {
|
|
447
|
-
for await (const key of client.scanIterator({
|
|
448
|
-
MATCH: pattern,
|
|
449
|
-
COUNT: 500
|
|
450
|
-
})) {
|
|
451
|
-
const keys = Array.isArray(key) ? key : [key];
|
|
452
|
-
if (keys.length > 0) {
|
|
453
|
-
await client.del(keys);
|
|
454
|
-
totalDeleted += keys.length;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
this.logger.info(() => ({
|
|
459
|
-
msg: "Cleared Redis session records at startup",
|
|
460
|
-
data: { totalDeleted }
|
|
461
|
-
}));
|
|
462
|
-
} catch (error) {
|
|
463
|
-
this.logger.warn(() => ({
|
|
464
|
-
msg: "Failed to clear Redis session records at startup",
|
|
465
|
-
err: error
|
|
466
|
-
}));
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* Check if the underlying Redis connection is ready.
|
|
471
|
-
*/
|
|
472
|
-
isReady() {
|
|
473
|
-
return this.connection.isReady();
|
|
474
|
-
}
|
|
475
|
-
}
|
|
12
|
+
/**
|
|
13
|
+
* Redis-backed write queue and read cache for reducing MongoDB load.
|
|
14
|
+
*
|
|
15
|
+
* Provides:
|
|
16
|
+
* - Session record caching (reduces MongoDB reads)
|
|
17
|
+
* - Session-by-hash caching for frictionless deduplication
|
|
18
|
+
* - Session write queuing with periodic flush to MongoDB
|
|
19
|
+
*
|
|
20
|
+
* Uses an existing RedisConnection from @prosopo/redis-client rather than
|
|
21
|
+
* creating its own connection.
|
|
22
|
+
*/
|
|
23
|
+
var RedisWriteQueue = class {
|
|
24
|
+
constructor(connection, logger) {
|
|
25
|
+
this.flushTimer = null;
|
|
26
|
+
this.flushCallback = null;
|
|
27
|
+
this.connection = connection;
|
|
28
|
+
this.logger = logger;
|
|
29
|
+
}
|
|
30
|
+
async getClient() {
|
|
31
|
+
if (!this.connection.isReady()) return null;
|
|
32
|
+
try {
|
|
33
|
+
return await this.connection.getClient();
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Cache a session record in Redis for fast lookups, reducing MongoDB reads.
|
|
40
|
+
*/
|
|
41
|
+
async cacheSession(sessionId, sessionData, ttlSeconds = 86400) {
|
|
42
|
+
const client = await this.getClient();
|
|
43
|
+
if (!client) return false;
|
|
44
|
+
try {
|
|
45
|
+
const key = `cache:session:${sessionId}`;
|
|
46
|
+
await client.set(key, JSON.stringify(sessionData, bigIntReplacer), { EX: ttlSeconds });
|
|
47
|
+
return true;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
this.logger.warn(() => ({
|
|
50
|
+
msg: "Failed to cache session in Redis",
|
|
51
|
+
err: error,
|
|
52
|
+
sessionId
|
|
53
|
+
}));
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Retrieve a cached session record from Redis.
|
|
59
|
+
*/
|
|
60
|
+
async getCachedSession(sessionId) {
|
|
61
|
+
const client = await this.getClient();
|
|
62
|
+
if (!client) return null;
|
|
63
|
+
try {
|
|
64
|
+
const key = `cache:session:${sessionId}`;
|
|
65
|
+
const data = await client.get(key);
|
|
66
|
+
if (!data) return null;
|
|
67
|
+
return JSON.parse(data, bigIntReviver);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
this.logger.warn(() => ({
|
|
70
|
+
msg: "Failed to get cached session from Redis",
|
|
71
|
+
err: error,
|
|
72
|
+
sessionId
|
|
73
|
+
}));
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Patch the cached session in place — read the existing entry, shallow-
|
|
79
|
+
* merge the provided updates, and write it back. No-op on cache miss so
|
|
80
|
+
* the next read repopulates from Mongo. Mirrors the field-level Mongo
|
|
81
|
+
* patch done by `db.updateSessionRecord`; callers invoke both to keep
|
|
82
|
+
* Redis consistent with the DB after every in-request write.
|
|
83
|
+
*/
|
|
84
|
+
async patchCachedSession(sessionId, updates, ttlSeconds = 86400) {
|
|
85
|
+
const existing = await this.getCachedSession(sessionId);
|
|
86
|
+
if (!existing) return false;
|
|
87
|
+
return this.cacheSession(sessionId, {
|
|
88
|
+
...existing,
|
|
89
|
+
...updates,
|
|
90
|
+
lastUpdatedTimestamp: /* @__PURE__ */ new Date()
|
|
91
|
+
}, ttlSeconds);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* First-hop-wins SIMD attach on the cache — mirrors the atomic Mongo
|
|
95
|
+
* `$ifNull` pipeline update in `db.recordSessionSimdReadingsIfAbsent`.
|
|
96
|
+
* Reads the cache; if it doesn't already carry `simdReadings`, merges
|
|
97
|
+
* the new readings + stage. No-op on cache miss or when readings are
|
|
98
|
+
* already present.
|
|
99
|
+
*/
|
|
100
|
+
async patchCachedSimdReadingsIfAbsent(sessionId, readings, stage, ttlSeconds = 86400) {
|
|
101
|
+
const existing = await this.getCachedSession(sessionId);
|
|
102
|
+
if (!existing) return false;
|
|
103
|
+
if (existing.simdReadings) return false;
|
|
104
|
+
return this.cacheSession(sessionId, {
|
|
105
|
+
...existing,
|
|
106
|
+
simdReadings: readings,
|
|
107
|
+
simdReadingsStage: stage,
|
|
108
|
+
lastUpdatedTimestamp: /* @__PURE__ */ new Date()
|
|
109
|
+
}, ttlSeconds);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Invalidate a cached session when it's updated.
|
|
113
|
+
*/
|
|
114
|
+
async invalidateCachedSession(sessionId) {
|
|
115
|
+
const client = await this.getClient();
|
|
116
|
+
if (!client) return;
|
|
117
|
+
try {
|
|
118
|
+
await client.del(`cache:session:${sessionId}`);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
this.logger.warn(() => ({
|
|
121
|
+
msg: "Failed to invalidate cached session in Redis",
|
|
122
|
+
err: error,
|
|
123
|
+
sessionId
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Invalidate the hash → sessionId mapping used by /frictionless for
|
|
129
|
+
* dedup. Must be called alongside `invalidateCachedSession` when a
|
|
130
|
+
* session is consumed, otherwise the hash mapping keeps resolving to
|
|
131
|
+
* a dead sessionId for the remainder of its 1-hour TTL.
|
|
132
|
+
*/
|
|
133
|
+
async invalidateCachedSessionByHash(userSitekeyIpHash) {
|
|
134
|
+
const client = await this.getClient();
|
|
135
|
+
if (!client) return;
|
|
136
|
+
try {
|
|
137
|
+
await client.del(`cache:session:hash:${userSitekeyIpHash}`);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
this.logger.warn(() => ({
|
|
140
|
+
msg: "Failed to invalidate cached session hash in Redis",
|
|
141
|
+
err: error,
|
|
142
|
+
userSitekeyIpHash
|
|
143
|
+
}));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Cache a session lookup by userSitekeyIpHash for frictionless deduplication.
|
|
148
|
+
* Extends the effective TTL of the cached session, reducing duplicate
|
|
149
|
+
* session creation under concurrent requests for the same user+site+IP.
|
|
150
|
+
*/
|
|
151
|
+
async cacheSessionByHash(userSitekeyIpHash, sessionId, ttlSeconds = 3600) {
|
|
152
|
+
const client = await this.getClient();
|
|
153
|
+
if (!client) return false;
|
|
154
|
+
try {
|
|
155
|
+
const key = `cache:session:hash:${userSitekeyIpHash}`;
|
|
156
|
+
await client.set(key, sessionId, { EX: ttlSeconds });
|
|
157
|
+
return true;
|
|
158
|
+
} catch (error) {
|
|
159
|
+
this.logger.warn(() => ({
|
|
160
|
+
msg: "Failed to cache session hash in Redis",
|
|
161
|
+
err: error,
|
|
162
|
+
userSitekeyIpHash
|
|
163
|
+
}));
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Get cached sessionId by userSitekeyIpHash for frictionless deduplication.
|
|
169
|
+
*/
|
|
170
|
+
async getCachedSessionByHash(userSitekeyIpHash) {
|
|
171
|
+
const client = await this.getClient();
|
|
172
|
+
if (!client) return null;
|
|
173
|
+
try {
|
|
174
|
+
const key = `cache:session:hash:${userSitekeyIpHash}`;
|
|
175
|
+
return await client.get(key);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
this.logger.warn(() => ({
|
|
178
|
+
msg: "Failed to get cached session hash from Redis",
|
|
179
|
+
err: error,
|
|
180
|
+
userSitekeyIpHash
|
|
181
|
+
}));
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Cache an origin → escalation sessionId mapping for the post-PoW
|
|
187
|
+
* escalation flow.
|
|
188
|
+
*
|
|
189
|
+
* Background: when the routing machine returns image/puzzle from the
|
|
190
|
+
* postPow phase, `buildEscalation` (`api/captcha/submitPoWCaptchaSolution.ts`)
|
|
191
|
+
* mints a new session with a fresh `sessionId` and returns it on the
|
|
192
|
+
* `escalation` field. A widget that handles the escalation cleanly
|
|
193
|
+
* mounts the next captcha with the new sessionId. But there are
|
|
194
|
+
* several real-world paths where the widget keeps using the original
|
|
195
|
+
* sessionId for the follow-up `/captcha/{type}` request — bundled SDK
|
|
196
|
+
* versions that predate the escalation handler, dapps that hand-roll
|
|
197
|
+
* the wrapper, network-glitch retries, or browser-tab races. The
|
|
198
|
+
* original session has been consumed and soft-deleted by the
|
|
199
|
+
* preceding `/captcha/pow`, so the follow-up gets
|
|
200
|
+
* `CAPTCHA.NO_SESSION_FOUND` and the user sees an inline error → FAQ.
|
|
201
|
+
*
|
|
202
|
+
* This mapping lets `isValidRequest` resolve the original sessionId
|
|
203
|
+
* forward to the live escalation session and proceed normally.
|
|
204
|
+
*
|
|
205
|
+
* TTL is short on purpose: escalation handoffs complete in seconds
|
|
206
|
+
* (the user solves the follow-up immediately) and a stale mapping
|
|
207
|
+
* pointing at a long-gone session would just produce a different
|
|
208
|
+
* shape of NO_SESSION_FOUND.
|
|
209
|
+
*/
|
|
210
|
+
async cacheSessionEscalation(originSessionId, escalationSessionId, ttlSeconds = 600) {
|
|
211
|
+
const client = await this.getClient();
|
|
212
|
+
if (!client) return false;
|
|
213
|
+
try {
|
|
214
|
+
const key = `cache:session:escalation:${originSessionId}`;
|
|
215
|
+
await client.set(key, escalationSessionId, { EX: ttlSeconds });
|
|
216
|
+
return true;
|
|
217
|
+
} catch (error) {
|
|
218
|
+
this.logger.warn(() => ({
|
|
219
|
+
msg: "Failed to cache session escalation mapping in Redis",
|
|
220
|
+
err: error,
|
|
221
|
+
originSessionId,
|
|
222
|
+
escalationSessionId
|
|
223
|
+
}));
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Resolve an original sessionId to its escalation sessionId, if one was
|
|
229
|
+
* recorded. Returns null when there is no mapping (either no escalation
|
|
230
|
+
* happened, or the TTL has elapsed).
|
|
231
|
+
*/
|
|
232
|
+
async getCachedSessionEscalation(originSessionId) {
|
|
233
|
+
const client = await this.getClient();
|
|
234
|
+
if (!client) return null;
|
|
235
|
+
try {
|
|
236
|
+
const key = `cache:session:escalation:${originSessionId}`;
|
|
237
|
+
return await client.get(key);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
this.logger.warn(() => ({
|
|
240
|
+
msg: "Failed to get cached session escalation mapping from Redis",
|
|
241
|
+
err: error,
|
|
242
|
+
originSessionId
|
|
243
|
+
}));
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Invalidate an escalation mapping. Called when the escalation session
|
|
249
|
+
* itself is consumed so subsequent retries on the origin sessionId fall
|
|
250
|
+
* through to the standard NO_SESSION_FOUND path rather than chasing a
|
|
251
|
+
* second-hand stale pointer.
|
|
252
|
+
*/
|
|
253
|
+
async invalidateCachedSessionEscalation(originSessionId) {
|
|
254
|
+
const client = await this.getClient();
|
|
255
|
+
if (!client) return;
|
|
256
|
+
try {
|
|
257
|
+
const key = `cache:session:escalation:${originSessionId}`;
|
|
258
|
+
await client.del(key);
|
|
259
|
+
} catch (error) {
|
|
260
|
+
this.logger.warn(() => ({
|
|
261
|
+
msg: "Failed to invalidate cached session escalation mapping",
|
|
262
|
+
err: error,
|
|
263
|
+
originSessionId
|
|
264
|
+
}));
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Queue a session record for batched insertion into MongoDB.
|
|
269
|
+
* The session data is also cached in Redis for immediate reads.
|
|
270
|
+
*/
|
|
271
|
+
async queueSessionRecord(sessionId, record, ttlSeconds = 86400) {
|
|
272
|
+
const client = await this.getClient();
|
|
273
|
+
if (!client) return false;
|
|
274
|
+
try {
|
|
275
|
+
const key = `writeq:session:${sessionId}`;
|
|
276
|
+
const serialized = JSON.stringify(record, bigIntReplacer);
|
|
277
|
+
await client.set(key, serialized, { EX: ttlSeconds });
|
|
278
|
+
await client.sAdd("writeq:session:pending", sessionId);
|
|
279
|
+
await client.set(`cache:session:${sessionId}`, serialized, { EX: ttlSeconds });
|
|
280
|
+
this.triggerEarlyFlushIfNeeded();
|
|
281
|
+
return true;
|
|
282
|
+
} catch (error) {
|
|
283
|
+
this.logger.warn(() => ({
|
|
284
|
+
msg: "Failed to queue session record in Redis",
|
|
285
|
+
err: error,
|
|
286
|
+
sessionId
|
|
287
|
+
}));
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Get all pending session records ready for batch flush.
|
|
293
|
+
*/
|
|
294
|
+
async drainSessionRecords(limit = 500) {
|
|
295
|
+
const client = await this.getClient();
|
|
296
|
+
if (!client) return [];
|
|
297
|
+
try {
|
|
298
|
+
const batch = (await client.sMembers("writeq:session:pending")).slice(0, limit);
|
|
299
|
+
const results = [];
|
|
300
|
+
for (const sessionId of batch) {
|
|
301
|
+
const key = `writeq:session:${sessionId}`;
|
|
302
|
+
const data = await client.get(key);
|
|
303
|
+
if (data) results.push({
|
|
304
|
+
sessionId,
|
|
305
|
+
record: JSON.parse(data, bigIntReviver)
|
|
306
|
+
});
|
|
307
|
+
await client.sRem("writeq:session:pending", sessionId);
|
|
308
|
+
await client.del(key);
|
|
309
|
+
}
|
|
310
|
+
return results;
|
|
311
|
+
} catch (error) {
|
|
312
|
+
this.logger.warn(() => ({
|
|
313
|
+
msg: "Failed to drain session records from Redis",
|
|
314
|
+
err: error
|
|
315
|
+
}));
|
|
316
|
+
return [];
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Start periodic background flush of queued records.
|
|
321
|
+
* The callback receives this queue instance and should drain + bulk-write records.
|
|
322
|
+
*
|
|
323
|
+
* When the queue depth exceeds `earlyFlushThreshold`, an early flush is
|
|
324
|
+
* triggered on the next `queueSessionRecord` call without waiting for
|
|
325
|
+
* the regular interval.
|
|
326
|
+
*/
|
|
327
|
+
startPeriodicFlush(callback, intervalMs = 1e4, earlyFlushThreshold = 50) {
|
|
328
|
+
this.stopPeriodicFlush();
|
|
329
|
+
this.flushCallback = callback;
|
|
330
|
+
this.earlyFlushThreshold = earlyFlushThreshold;
|
|
331
|
+
this.flushTimer = setInterval(() => {
|
|
332
|
+
this.flushCallback?.(this).catch((error) => {
|
|
333
|
+
this.logger.error(() => ({
|
|
334
|
+
msg: "Periodic flush failed",
|
|
335
|
+
err: error
|
|
336
|
+
}));
|
|
337
|
+
});
|
|
338
|
+
}, intervalMs);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Trigger an early flush if the pending queue exceeds the threshold.
|
|
342
|
+
* Called automatically after queueing a record. This avoids large
|
|
343
|
+
* batch build-ups between regular interval flushes.
|
|
344
|
+
*/
|
|
345
|
+
triggerEarlyFlushIfNeeded() {
|
|
346
|
+
if (!this.flushCallback || !this.earlyFlushThreshold) return;
|
|
347
|
+
this.getPendingCount().then((count) => {
|
|
348
|
+
if (count >= (this.earlyFlushThreshold ?? 50)) return this.flushCallback?.(this);
|
|
349
|
+
}).catch((error) => {
|
|
350
|
+
this.logger.error(() => ({
|
|
351
|
+
msg: "Early flush failed",
|
|
352
|
+
err: error
|
|
353
|
+
}));
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
/** Get the number of pending session records awaiting flush. */
|
|
357
|
+
async getPendingCount() {
|
|
358
|
+
const client = await this.getClient();
|
|
359
|
+
if (!client) return 0;
|
|
360
|
+
try {
|
|
361
|
+
return await client.sCard("writeq:session:pending");
|
|
362
|
+
} catch {
|
|
363
|
+
return 0;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Stop the periodic flush timer.
|
|
368
|
+
*/
|
|
369
|
+
stopPeriodicFlush() {
|
|
370
|
+
if (this.flushTimer) {
|
|
371
|
+
clearInterval(this.flushTimer);
|
|
372
|
+
this.flushTimer = null;
|
|
373
|
+
}
|
|
374
|
+
this.flushCallback = null;
|
|
375
|
+
this.earlyFlushThreshold = void 0;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Drop every session-related key from Redis. Intended for provider
|
|
379
|
+
* startup so a fresh process can never inherit stale read-cache,
|
|
380
|
+
* dedup-by-hash, or pending-write-queue entries written by an
|
|
381
|
+
* earlier (possibly crashed) run.
|
|
382
|
+
*
|
|
383
|
+
* Uses SCAN — KEYS would block the Redis server on large keyspaces.
|
|
384
|
+
*
|
|
385
|
+
* Bypasses the `isReady()` fast-path that other methods use because
|
|
386
|
+
* this runs during startup, before the Redis client's async
|
|
387
|
+
* connect() handshake has had time to flip the flag. We await the
|
|
388
|
+
* connection promise directly with a bounded timeout so we don't
|
|
389
|
+
* hang the boot sequence when Redis is unreachable.
|
|
390
|
+
*/
|
|
391
|
+
async clearAllSessionRecords(timeoutMs = 5e3) {
|
|
392
|
+
this.logger.info(() => ({ msg: "Clearing Redis session records at startup" }));
|
|
393
|
+
let client;
|
|
394
|
+
try {
|
|
395
|
+
client = await Promise.race([this.connection.getClient(), new Promise((_, reject) => setTimeout(() => reject(/* @__PURE__ */ new Error("Redis connection timeout")), timeoutMs))]);
|
|
396
|
+
} catch (error) {
|
|
397
|
+
this.logger.warn(() => ({
|
|
398
|
+
msg: "Skipped Redis session cleanup — Redis not reachable",
|
|
399
|
+
err: error
|
|
400
|
+
}));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
try {
|
|
404
|
+
let totalDeleted = 0;
|
|
405
|
+
for (const pattern of SESSION_KEY_PATTERNS) for await (const key of client.scanIterator({
|
|
406
|
+
MATCH: pattern,
|
|
407
|
+
COUNT: 500
|
|
408
|
+
})) {
|
|
409
|
+
const keys = Array.isArray(key) ? key : [key];
|
|
410
|
+
if (keys.length > 0) {
|
|
411
|
+
await client.del(keys);
|
|
412
|
+
totalDeleted += keys.length;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
this.logger.info(() => ({
|
|
416
|
+
msg: "Cleared Redis session records at startup",
|
|
417
|
+
data: { totalDeleted }
|
|
418
|
+
}));
|
|
419
|
+
} catch (error) {
|
|
420
|
+
this.logger.warn(() => ({
|
|
421
|
+
msg: "Failed to clear Redis session records at startup",
|
|
422
|
+
err: error
|
|
423
|
+
}));
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Check if the underlying Redis connection is ready.
|
|
428
|
+
*/
|
|
429
|
+
isReady() {
|
|
430
|
+
return this.connection.isReady();
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
//#endregion
|
|
476
434
|
exports.RedisWriteQueue = RedisWriteQueue;
|