@prosopo/database 3.5.6 → 3.13.7
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 +17 -13
- package/.turbo/turbo-build$colon$tsc.log +47 -0
- package/.turbo/turbo-build.log +22 -14
- package/CHANGELOG.md +690 -0
- package/dist/base/index.d.ts +3 -0
- package/dist/base/index.d.ts.map +1 -0
- package/dist/base/index.js.map +1 -0
- package/dist/base/mongo.d.ts +18 -0
- package/dist/base/mongo.d.ts.map +1 -0
- package/dist/base/mongo.js +5 -2
- package/dist/base/mongo.js.map +1 -0
- package/dist/base/mongoMemory.d.ts +10 -0
- package/dist/base/mongoMemory.d.ts.map +1 -0
- package/dist/base/mongoMemory.js.map +1 -0
- package/dist/cjs/base/mongo.cjs +6 -3
- package/dist/cjs/databases/captcha.cjs +2 -1
- package/dist/cjs/databases/centralDbStreamer.cjs +136 -0
- package/dist/cjs/databases/index.cjs +2 -0
- package/dist/cjs/databases/provider.cjs +691 -165
- package/dist/cjs/index.cjs +4 -0
- package/dist/cjs/redisCache.cjs +388 -0
- package/dist/databases/captcha.d.ts +25 -0
- package/dist/databases/captcha.d.ts.map +1 -0
- package/dist/databases/captcha.js +2 -1
- package/dist/databases/captcha.js.map +1 -0
- package/dist/databases/centralDbStreamer.d.ts +19 -0
- package/dist/databases/centralDbStreamer.d.ts.map +1 -0
- package/dist/databases/centralDbStreamer.js +136 -0
- package/dist/databases/centralDbStreamer.js.map +1 -0
- package/dist/databases/client.d.ts +12 -0
- package/dist/databases/client.d.ts.map +1 -0
- package/dist/databases/client.js.map +1 -0
- package/dist/databases/index.d.ts +17 -0
- package/dist/databases/index.d.ts.map +1 -0
- package/dist/databases/index.js +2 -0
- package/dist/databases/index.js.map +1 -0
- package/dist/databases/provider.d.ts +143 -0
- package/dist/databases/provider.d.ts.map +1 -0
- package/dist/databases/provider.js +692 -166
- package/dist/databases/provider.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -0
- package/dist/redisCache.d.ts +31 -0
- package/dist/redisCache.d.ts.map +1 -0
- package/dist/redisCache.js +388 -0
- package/dist/redisCache.js.map +1 -0
- package/dist/tests/integration/ipInfoPersistence.integration.test.d.ts +2 -0
- package/dist/tests/integration/ipInfoPersistence.integration.test.d.ts.map +1 -0
- package/dist/tests/integration/ipInfoPersistence.integration.test.js +243 -0
- package/dist/tests/integration/ipInfoPersistence.integration.test.js.map +1 -0
- package/dist/tests/unit/captchaLabel.unit.test.d.ts +2 -0
- package/dist/tests/unit/captchaLabel.unit.test.d.ts.map +1 -0
- package/dist/tests/unit/captchaLabel.unit.test.js +41 -0
- package/dist/tests/unit/captchaLabel.unit.test.js.map +1 -0
- package/dist/tests/unit/databases/centralDbStreamer.unit.test.d.ts +2 -0
- package/dist/tests/unit/databases/centralDbStreamer.unit.test.d.ts.map +1 -0
- package/dist/tests/unit/databases/centralDbStreamer.unit.test.js +221 -0
- package/dist/tests/unit/databases/centralDbStreamer.unit.test.js.map +1 -0
- package/package.json +14 -10
- package/vite.cjs.config.ts +1 -1
- package/vite.esm.config.ts +1 -1
- package/vite.test.config.ts +18 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,15 +3,19 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const makeDir = require("make-dir");
|
|
4
4
|
require("./base/index.cjs");
|
|
5
5
|
const index = require("./databases/index.cjs");
|
|
6
|
+
const redisCache = require("./redisCache.cjs");
|
|
6
7
|
const mongo = require("./base/mongo.cjs");
|
|
7
8
|
const mongoMemory = require("./base/mongoMemory.cjs");
|
|
8
9
|
const provider = require("./databases/provider.cjs");
|
|
9
10
|
const captcha = require("./databases/captcha.cjs");
|
|
11
|
+
const centralDbStreamer = require("./databases/centralDbStreamer.cjs");
|
|
10
12
|
const client = require("./databases/client.cjs");
|
|
11
13
|
console.debug(makeDir);
|
|
12
14
|
exports.Databases = index.Databases;
|
|
15
|
+
exports.RedisWriteQueue = redisCache.RedisWriteQueue;
|
|
13
16
|
exports.MongoDatabase = mongo.MongoDatabase;
|
|
14
17
|
exports.MongoMemoryDatabase = mongoMemory.MongoMemoryDatabase;
|
|
15
18
|
exports.ProviderDatabase = provider.ProviderDatabase;
|
|
16
19
|
exports.CaptchaDatabase = captcha.CaptchaDatabase;
|
|
20
|
+
exports.CentralDbStreamer = centralDbStreamer.CentralDbStreamer;
|
|
17
21
|
exports.ClientDatabase = client.ClientDatabase;
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const BIGINT_TAG = "__bigint__:";
|
|
4
|
+
const bigIntReplacer = (_key, value) => "bigint" === typeof value ? `${BIGINT_TAG}${value.toString()}` : value;
|
|
5
|
+
const bigIntReviver = (_key, value) => "string" === typeof value && value.startsWith(BIGINT_TAG) ? BigInt(value.slice(BIGINT_TAG.length)) : value;
|
|
6
|
+
const SESSION_KEY_PATTERNS = [
|
|
7
|
+
"cache:session:*",
|
|
8
|
+
"writeq:session:*",
|
|
9
|
+
"writeq:session:pending"
|
|
10
|
+
];
|
|
11
|
+
class RedisWriteQueue {
|
|
12
|
+
constructor(connection, logger) {
|
|
13
|
+
this.flushTimer = null;
|
|
14
|
+
this.flushCallback = null;
|
|
15
|
+
this.connection = connection;
|
|
16
|
+
this.logger = logger;
|
|
17
|
+
}
|
|
18
|
+
async getClient() {
|
|
19
|
+
if (!this.connection.isReady()) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
return await this.connection.getClient();
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// ── Session read cache ──────────────────────────────────────────────
|
|
29
|
+
/**
|
|
30
|
+
* Cache a session record in Redis for fast lookups, reducing MongoDB reads.
|
|
31
|
+
*/
|
|
32
|
+
async cacheSession(sessionId, sessionData, ttlSeconds = 86400) {
|
|
33
|
+
const client = await this.getClient();
|
|
34
|
+
if (!client) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const key = `cache:session:${sessionId}`;
|
|
39
|
+
await client.set(key, JSON.stringify(sessionData, bigIntReplacer), {
|
|
40
|
+
EX: ttlSeconds
|
|
41
|
+
});
|
|
42
|
+
return true;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
this.logger.warn(() => ({
|
|
45
|
+
msg: "Failed to cache session in Redis",
|
|
46
|
+
err: error,
|
|
47
|
+
sessionId
|
|
48
|
+
}));
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Retrieve a cached session record from Redis.
|
|
54
|
+
*/
|
|
55
|
+
async getCachedSession(sessionId) {
|
|
56
|
+
const client = await this.getClient();
|
|
57
|
+
if (!client) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const key = `cache:session:${sessionId}`;
|
|
62
|
+
const data = await client.get(key);
|
|
63
|
+
if (!data) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
return JSON.parse(data, bigIntReviver);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
this.logger.warn(() => ({
|
|
69
|
+
msg: "Failed to get cached session from Redis",
|
|
70
|
+
err: error,
|
|
71
|
+
sessionId
|
|
72
|
+
}));
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Patch the cached session in place — read the existing entry, shallow-
|
|
78
|
+
* merge the provided updates, and write it back. No-op on cache miss so
|
|
79
|
+
* the next read repopulates from Mongo. Mirrors the field-level Mongo
|
|
80
|
+
* patch done by `db.updateSessionRecord`; callers invoke both to keep
|
|
81
|
+
* Redis consistent with the DB after every in-request write.
|
|
82
|
+
*/
|
|
83
|
+
async patchCachedSession(sessionId, updates, ttlSeconds = 86400) {
|
|
84
|
+
const existing = await this.getCachedSession(sessionId);
|
|
85
|
+
if (!existing) return false;
|
|
86
|
+
return this.cacheSession(
|
|
87
|
+
sessionId,
|
|
88
|
+
{ ...existing, ...updates, lastUpdatedTimestamp: /* @__PURE__ */ new Date() },
|
|
89
|
+
ttlSeconds
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* First-hop-wins SIMD attach on the cache — mirrors the atomic Mongo
|
|
94
|
+
* `$ifNull` pipeline update in `db.recordSessionSimdReadingsIfAbsent`.
|
|
95
|
+
* Reads the cache; if it doesn't already carry `simdReadings`, merges
|
|
96
|
+
* the new readings + stage. No-op on cache miss or when readings are
|
|
97
|
+
* already present.
|
|
98
|
+
*/
|
|
99
|
+
async patchCachedSimdReadingsIfAbsent(sessionId, readings, stage, ttlSeconds = 86400) {
|
|
100
|
+
const existing = await this.getCachedSession(sessionId);
|
|
101
|
+
if (!existing) return false;
|
|
102
|
+
if (existing.simdReadings) return false;
|
|
103
|
+
return this.cacheSession(
|
|
104
|
+
sessionId,
|
|
105
|
+
{
|
|
106
|
+
...existing,
|
|
107
|
+
simdReadings: readings,
|
|
108
|
+
simdReadingsStage: stage,
|
|
109
|
+
lastUpdatedTimestamp: /* @__PURE__ */ new Date()
|
|
110
|
+
},
|
|
111
|
+
ttlSeconds
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Invalidate a cached session when it's updated.
|
|
116
|
+
*/
|
|
117
|
+
async invalidateCachedSession(sessionId) {
|
|
118
|
+
const client = await this.getClient();
|
|
119
|
+
if (!client) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
await client.del(`cache:session:${sessionId}`);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
this.logger.warn(() => ({
|
|
126
|
+
msg: "Failed to invalidate cached session in Redis",
|
|
127
|
+
err: error,
|
|
128
|
+
sessionId
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Invalidate the hash → sessionId mapping used by /frictionless for
|
|
134
|
+
* dedup. Must be called alongside `invalidateCachedSession` when a
|
|
135
|
+
* session is consumed, otherwise the hash mapping keeps resolving to
|
|
136
|
+
* a dead sessionId for the remainder of its 1-hour TTL.
|
|
137
|
+
*/
|
|
138
|
+
async invalidateCachedSessionByHash(userSitekeyIpHash) {
|
|
139
|
+
const client = await this.getClient();
|
|
140
|
+
if (!client) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
await client.del(`cache:session:hash:${userSitekeyIpHash}`);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
this.logger.warn(() => ({
|
|
147
|
+
msg: "Failed to invalidate cached session hash in Redis",
|
|
148
|
+
err: error,
|
|
149
|
+
userSitekeyIpHash
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// ── Frictionless session deduplication cache ────────────────────────
|
|
154
|
+
/**
|
|
155
|
+
* Cache a session lookup by userSitekeyIpHash for frictionless deduplication.
|
|
156
|
+
* Extends the effective TTL of the cached session, reducing duplicate
|
|
157
|
+
* session creation under concurrent requests for the same user+site+IP.
|
|
158
|
+
*/
|
|
159
|
+
async cacheSessionByHash(userSitekeyIpHash, sessionId, ttlSeconds = 3600) {
|
|
160
|
+
const client = await this.getClient();
|
|
161
|
+
if (!client) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const key = `cache:session:hash:${userSitekeyIpHash}`;
|
|
166
|
+
await client.set(key, sessionId, { EX: ttlSeconds });
|
|
167
|
+
return true;
|
|
168
|
+
} catch (error) {
|
|
169
|
+
this.logger.warn(() => ({
|
|
170
|
+
msg: "Failed to cache session hash in Redis",
|
|
171
|
+
err: error,
|
|
172
|
+
userSitekeyIpHash
|
|
173
|
+
}));
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get cached sessionId by userSitekeyIpHash for frictionless deduplication.
|
|
179
|
+
*/
|
|
180
|
+
async getCachedSessionByHash(userSitekeyIpHash) {
|
|
181
|
+
const client = await this.getClient();
|
|
182
|
+
if (!client) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const key = `cache:session:hash:${userSitekeyIpHash}`;
|
|
187
|
+
return await client.get(key);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
this.logger.warn(() => ({
|
|
190
|
+
msg: "Failed to get cached session hash from Redis",
|
|
191
|
+
err: error,
|
|
192
|
+
userSitekeyIpHash
|
|
193
|
+
}));
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// ── Session write queue ─────────────────────────────────────────────
|
|
198
|
+
/**
|
|
199
|
+
* Queue a session record for batched insertion into MongoDB.
|
|
200
|
+
* The session data is also cached in Redis for immediate reads.
|
|
201
|
+
*/
|
|
202
|
+
async queueSessionRecord(sessionId, record, ttlSeconds = 86400) {
|
|
203
|
+
const client = await this.getClient();
|
|
204
|
+
if (!client) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
const key = `writeq:session:${sessionId}`;
|
|
209
|
+
const serialized = JSON.stringify(record, bigIntReplacer);
|
|
210
|
+
await client.set(key, serialized, { EX: ttlSeconds });
|
|
211
|
+
await client.sAdd("writeq:session:pending", sessionId);
|
|
212
|
+
await client.set(`cache:session:${sessionId}`, serialized, {
|
|
213
|
+
EX: ttlSeconds
|
|
214
|
+
});
|
|
215
|
+
this.triggerEarlyFlushIfNeeded();
|
|
216
|
+
return true;
|
|
217
|
+
} catch (error) {
|
|
218
|
+
this.logger.warn(() => ({
|
|
219
|
+
msg: "Failed to queue session record in Redis",
|
|
220
|
+
err: error,
|
|
221
|
+
sessionId
|
|
222
|
+
}));
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Get all pending session records ready for batch flush.
|
|
228
|
+
*/
|
|
229
|
+
async drainSessionRecords(limit = 500) {
|
|
230
|
+
const client = await this.getClient();
|
|
231
|
+
if (!client) {
|
|
232
|
+
return [];
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
const sessionIds = await client.sMembers("writeq:session:pending");
|
|
236
|
+
const batch = sessionIds.slice(0, limit);
|
|
237
|
+
const results = [];
|
|
238
|
+
for (const sessionId of batch) {
|
|
239
|
+
const key = `writeq:session:${sessionId}`;
|
|
240
|
+
const data = await client.get(key);
|
|
241
|
+
if (data) {
|
|
242
|
+
results.push({
|
|
243
|
+
sessionId,
|
|
244
|
+
record: JSON.parse(data, bigIntReviver)
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
await client.sRem("writeq:session:pending", sessionId);
|
|
248
|
+
await client.del(key);
|
|
249
|
+
}
|
|
250
|
+
return results;
|
|
251
|
+
} catch (error) {
|
|
252
|
+
this.logger.warn(() => ({
|
|
253
|
+
msg: "Failed to drain session records from Redis",
|
|
254
|
+
err: error
|
|
255
|
+
}));
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
// ── Periodic flush ──────────────────────────────────────────────────
|
|
260
|
+
/**
|
|
261
|
+
* Start periodic background flush of queued records.
|
|
262
|
+
* The callback receives this queue instance and should drain + bulk-write records.
|
|
263
|
+
*
|
|
264
|
+
* When the queue depth exceeds `earlyFlushThreshold`, an early flush is
|
|
265
|
+
* triggered on the next `queueSessionRecord` call without waiting for
|
|
266
|
+
* the regular interval.
|
|
267
|
+
*/
|
|
268
|
+
startPeriodicFlush(callback, intervalMs = 1e4, earlyFlushThreshold = 50) {
|
|
269
|
+
this.stopPeriodicFlush();
|
|
270
|
+
this.flushCallback = callback;
|
|
271
|
+
this.earlyFlushThreshold = earlyFlushThreshold;
|
|
272
|
+
this.flushTimer = setInterval(() => {
|
|
273
|
+
this.flushCallback?.(this).catch((error) => {
|
|
274
|
+
this.logger.error(() => ({
|
|
275
|
+
msg: "Periodic flush failed",
|
|
276
|
+
err: error
|
|
277
|
+
}));
|
|
278
|
+
});
|
|
279
|
+
}, intervalMs);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Trigger an early flush if the pending queue exceeds the threshold.
|
|
283
|
+
* Called automatically after queueing a record. This avoids large
|
|
284
|
+
* batch build-ups between regular interval flushes.
|
|
285
|
+
*/
|
|
286
|
+
triggerEarlyFlushIfNeeded() {
|
|
287
|
+
if (!this.flushCallback || !this.earlyFlushThreshold) return;
|
|
288
|
+
this.getPendingCount().then((count) => {
|
|
289
|
+
if (count >= (this.earlyFlushThreshold ?? 50)) {
|
|
290
|
+
return this.flushCallback?.(this);
|
|
291
|
+
}
|
|
292
|
+
}).catch((error) => {
|
|
293
|
+
this.logger.error(() => ({
|
|
294
|
+
msg: "Early flush failed",
|
|
295
|
+
err: error
|
|
296
|
+
}));
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
/** Get the number of pending session records awaiting flush. */
|
|
300
|
+
async getPendingCount() {
|
|
301
|
+
const client = await this.getClient();
|
|
302
|
+
if (!client) return 0;
|
|
303
|
+
try {
|
|
304
|
+
return await client.sCard("writeq:session:pending");
|
|
305
|
+
} catch {
|
|
306
|
+
return 0;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Stop the periodic flush timer.
|
|
311
|
+
*/
|
|
312
|
+
stopPeriodicFlush() {
|
|
313
|
+
if (this.flushTimer) {
|
|
314
|
+
clearInterval(this.flushTimer);
|
|
315
|
+
this.flushTimer = null;
|
|
316
|
+
}
|
|
317
|
+
this.flushCallback = null;
|
|
318
|
+
this.earlyFlushThreshold = void 0;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Drop every session-related key from Redis. Intended for provider
|
|
322
|
+
* startup so a fresh process can never inherit stale read-cache,
|
|
323
|
+
* dedup-by-hash, or pending-write-queue entries written by an
|
|
324
|
+
* earlier (possibly crashed) run.
|
|
325
|
+
*
|
|
326
|
+
* Uses SCAN — KEYS would block the Redis server on large keyspaces.
|
|
327
|
+
*
|
|
328
|
+
* Bypasses the `isReady()` fast-path that other methods use because
|
|
329
|
+
* this runs during startup, before the Redis client's async
|
|
330
|
+
* connect() handshake has had time to flip the flag. We await the
|
|
331
|
+
* connection promise directly with a bounded timeout so we don't
|
|
332
|
+
* hang the boot sequence when Redis is unreachable.
|
|
333
|
+
*/
|
|
334
|
+
async clearAllSessionRecords(timeoutMs = 5e3) {
|
|
335
|
+
this.logger.info(() => ({
|
|
336
|
+
msg: "Clearing Redis session records at startup"
|
|
337
|
+
}));
|
|
338
|
+
let client;
|
|
339
|
+
try {
|
|
340
|
+
client = await Promise.race([
|
|
341
|
+
this.connection.getClient(),
|
|
342
|
+
new Promise(
|
|
343
|
+
(_, reject) => setTimeout(
|
|
344
|
+
() => reject(new Error("Redis connection timeout")),
|
|
345
|
+
timeoutMs
|
|
346
|
+
)
|
|
347
|
+
)
|
|
348
|
+
]);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
this.logger.warn(() => ({
|
|
351
|
+
msg: "Skipped Redis session cleanup — Redis not reachable",
|
|
352
|
+
err: error
|
|
353
|
+
}));
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
let totalDeleted = 0;
|
|
358
|
+
for (const pattern of SESSION_KEY_PATTERNS) {
|
|
359
|
+
for await (const key of client.scanIterator({
|
|
360
|
+
MATCH: pattern,
|
|
361
|
+
COUNT: 500
|
|
362
|
+
})) {
|
|
363
|
+
const keys = Array.isArray(key) ? key : [key];
|
|
364
|
+
if (keys.length > 0) {
|
|
365
|
+
await client.del(keys);
|
|
366
|
+
totalDeleted += keys.length;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
this.logger.info(() => ({
|
|
371
|
+
msg: "Cleared Redis session records at startup",
|
|
372
|
+
data: { totalDeleted }
|
|
373
|
+
}));
|
|
374
|
+
} catch (error) {
|
|
375
|
+
this.logger.warn(() => ({
|
|
376
|
+
msg: "Failed to clear Redis session records at startup",
|
|
377
|
+
err: error
|
|
378
|
+
}));
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Check if the underlying Redis connection is ready.
|
|
383
|
+
*/
|
|
384
|
+
isReady() {
|
|
385
|
+
return this.connection.isReady();
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
exports.RedisWriteQueue = RedisWriteQueue;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Logger } from "@prosopo/logger";
|
|
2
|
+
import { type CaptchaProperties, type ICaptchaDatabase, type PoWCaptchaRecord, type StoredSession, type Tables, type UserCommitmentRecord } from "@prosopo/types-database";
|
|
3
|
+
import type { RootFilterQuery } from "mongoose";
|
|
4
|
+
import { MongoDatabase } from "../base/index.js";
|
|
5
|
+
declare enum TableNames {
|
|
6
|
+
frictionlessToken = "frictionlessToken",
|
|
7
|
+
session = "session",
|
|
8
|
+
commitment = "commitment",
|
|
9
|
+
powcaptcha = "powcaptcha"
|
|
10
|
+
}
|
|
11
|
+
export declare class CaptchaDatabase extends MongoDatabase implements ICaptchaDatabase {
|
|
12
|
+
tables: Tables<TableNames>;
|
|
13
|
+
private indexesEnsured;
|
|
14
|
+
constructor(url: string, dbname?: string, authSource?: string, logger?: Logger);
|
|
15
|
+
connect(): Promise<void>;
|
|
16
|
+
getTables(): Tables<TableNames>;
|
|
17
|
+
ensureIndexes(): Promise<void>;
|
|
18
|
+
saveCaptchas(sessionEvents: StoredSession[], imageCaptchaEvents: UserCommitmentRecord[], powCaptchaEvents: PoWCaptchaRecord[]): Promise<void>;
|
|
19
|
+
getCaptchas(filter?: RootFilterQuery<CaptchaProperties>, limit?: number): Promise<{
|
|
20
|
+
userCommitmentRecords: UserCommitmentRecord[];
|
|
21
|
+
powCaptchaRecords: PoWCaptchaRecord[];
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=captcha.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"captcha.d.ts","sourceRoot":"","sources":["../../src/databases/captcha.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,KAAK,MAAM,EAAa,MAAM,iBAAiB,CAAC;AACzD,OAAO,EACN,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EAErB,KAAK,aAAa,EAGlB,KAAK,MAAM,EACX,KAAK,oBAAoB,EACzB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAIjD,aAAK,UAAU;IACd,iBAAiB,sBAAsB;IACvC,OAAO,YAAY;IACnB,UAAU,eAAe;IACzB,UAAU,eAAe;CACzB;AAoBD,qBAAa,eAAgB,SAAQ,aAAc,YAAW,gBAAgB;IAC7E,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;IAC3B,OAAO,CAAC,cAAc,CAAS;gBAG9B,GAAG,EAAE,MAAM,EACX,MAAM,CAAC,EAAE,MAAM,EACf,UAAU,CAAC,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,MAAM;IAMD,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAUvC,SAAS,IAAI,MAAM,CAAC,UAAU,CAAC;IAUzB,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAmC9B,YAAY,CACjB,aAAa,EAAE,aAAa,EAAE,EAC9B,kBAAkB,EAAE,oBAAoB,EAAE,EAC1C,gBAAgB,EAAE,gBAAgB,EAAE;IA2E/B,WAAW,CAChB,MAAM,GAAE,eAAe,CAAC,iBAAiB,CAAM,EAC/C,KAAK,SAAM,GACT,OAAO,CAAC;QACV,qBAAqB,EAAE,oBAAoB,EAAE,CAAC;QAC9C,iBAAiB,EAAE,gBAAgB,EAAE,CAAC;KACtC,CAAC;CAgCF"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProsopoDBError } from "@prosopo/common";
|
|
2
|
+
import { getLogger } from "@prosopo/logger";
|
|
2
3
|
import { StoredSessionRecordSchema, StoredPoWCaptchaRecordSchema, StoredUserCommitmentRecordSchema } from "@prosopo/types-database";
|
|
3
4
|
import "../base/index.js";
|
|
4
5
|
import { MongoDatabase } from "../base/mongo.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../src/databases/captcha.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAe,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAIN,4BAA4B,EAE5B,yBAAyB,EACzB,gCAAgC,GAGhC,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAElD,IAAK,UAKJ;AALD,WAAK,UAAU;IACd,qDAAuC,CAAA;IACvC,iCAAmB,CAAA;IACnB,uCAAyB,CAAA;IACzB,uCAAyB,CAAA;AAC1B,CAAC,EALI,UAAU,KAAV,UAAU,QAKd;AAED,MAAM,cAAc,GAAG;IACtB;QACC,cAAc,EAAE,UAAU,CAAC,OAAO;QAClC,SAAS,EAAE,SAAS;QACpB,MAAM,EAAE,yBAAyB;KACjC;IACD;QACC,cAAc,EAAE,UAAU,CAAC,UAAU;QACrC,SAAS,EAAE,YAAY;QACvB,MAAM,EAAE,4BAA4B;KACpC;IACD;QACC,cAAc,EAAE,UAAU,CAAC,UAAU;QACrC,SAAS,EAAE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BAAsB,EAAE;gBAChD,OAAO,EAAE;oBACR,KAAK;oBACL,MAAM;oBACN,KAAK;oBACL,cAAc,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI;iBACrC;gBACD,MAAM,EAAE,IAAI,CAAC,MAAM;aACnB,CAAC,CAAC;QACJ,CAAC;gBAAS,CAAC;YACV,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACpB,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Logger } from "@prosopo/logger";
|
|
2
|
+
import type { PoWCaptchaRecord, StoredSession, UserCommitmentRecord } from "@prosopo/types-database";
|
|
3
|
+
export type MarkStoredCallback = (streamedTimestamp: Date) => Promise<void>;
|
|
4
|
+
export declare class CentralDbStreamer {
|
|
5
|
+
private db;
|
|
6
|
+
private logger;
|
|
7
|
+
private connectPromise;
|
|
8
|
+
private lastFailureTime;
|
|
9
|
+
private static readonly RECONNECT_COOLDOWN_MS;
|
|
10
|
+
constructor(mongoCaptchaUri: string, logger?: Logger);
|
|
11
|
+
private ensureConnected;
|
|
12
|
+
private getRecordTimestamp;
|
|
13
|
+
streamPowRecord(record: PoWCaptchaRecord, markStored?: MarkStoredCallback): void;
|
|
14
|
+
streamPowUpdate(getFullRecord: () => Promise<PoWCaptchaRecord | null>, markStored?: MarkStoredCallback): void;
|
|
15
|
+
streamImageRecord(record: UserCommitmentRecord, markStored?: MarkStoredCallback): void;
|
|
16
|
+
streamImageUpdate(getFullRecord: () => Promise<UserCommitmentRecord | null>, markStored?: MarkStoredCallback): void;
|
|
17
|
+
streamSessionRecord(record: StoredSession, markStored?: MarkStoredCallback): void;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=centralDbStreamer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"centralDbStreamer.d.ts","sourceRoot":"","sources":["../../src/databases/centralDbStreamer.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,KAAK,MAAM,EAAa,MAAM,iBAAiB,CAAC;AACzD,OAAO,KAAK,EACX,gBAAgB,EAChB,aAAa,EACb,oBAAoB,EACpB,MAAM,yBAAyB,CAAC;AAUjC,MAAM,MAAM,kBAAkB,GAAG,CAAC,iBAAiB,EAAE,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAc5E,qBAAa,iBAAiB;IAC7B,OAAO,CAAC,EAAE,CAAkB;IAC5B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,cAAc,CAA4B;IAClD,OAAO,CAAC,eAAe,CAAK;IAC5B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,qBAAqB,CAAS;gBAE1C,eAAe,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;IAUpD,OAAO,CAAC,eAAe;IA8BvB,OAAO,CAAC,kBAAkB;IAiB1B,eAAe,CACd,MAAM,EAAE,gBAAgB,EACxB,UAAU,CAAC,EAAE,kBAAkB,GAC7B,IAAI;IAuBP,eAAe,CACd,aAAa,EAAE,MAAM,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,EACrD,UAAU,CAAC,EAAE,kBAAkB,GAC7B,IAAI;IAmBP,iBAAiB,CAChB,MAAM,EAAE,oBAAoB,EAC5B,UAAU,CAAC,EAAE,kBAAkB,GAC7B,IAAI;IAuBP,iBAAiB,CAChB,aAAa,EAAE,MAAM,OAAO,CAAC,oBAAoB,GAAG,IAAI,CAAC,EACzD,UAAU,CAAC,EAAE,kBAAkB,GAC7B,IAAI;IAmBP,mBAAmB,CAClB,MAAM,EAAE,aAAa,EACrB,UAAU,CAAC,EAAE,kBAAkB,GAC7B,IAAI;CAmBP"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { getLogger } from "@prosopo/logger";
|
|
2
|
+
import { CaptchaDatabase } from "./captcha.js";
|
|
3
|
+
class CentralDbStreamer {
|
|
4
|
+
constructor(mongoCaptchaUri, logger) {
|
|
5
|
+
this.lastFailureTime = 0;
|
|
6
|
+
this.logger = logger || getLogger("info", "CentralDbStreamer");
|
|
7
|
+
this.db = new CaptchaDatabase(
|
|
8
|
+
mongoCaptchaUri,
|
|
9
|
+
void 0,
|
|
10
|
+
void 0,
|
|
11
|
+
this.logger
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
static {
|
|
15
|
+
this.RECONNECT_COOLDOWN_MS = 5e3;
|
|
16
|
+
}
|
|
17
|
+
ensureConnected() {
|
|
18
|
+
if (this.connectPromise && !this.db.connected) {
|
|
19
|
+
this.connectPromise = void 0;
|
|
20
|
+
}
|
|
21
|
+
if (!this.connectPromise) {
|
|
22
|
+
const elapsed = Date.now() - this.lastFailureTime;
|
|
23
|
+
if (elapsed < CentralDbStreamer.RECONNECT_COOLDOWN_MS) {
|
|
24
|
+
return Promise.reject(
|
|
25
|
+
new Error("CentralDbStreamer reconnect cooldown")
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
this.connectPromise = this.db.connect().catch((err) => {
|
|
29
|
+
this.logger.error(() => ({
|
|
30
|
+
err,
|
|
31
|
+
msg: "CentralDbStreamer failed to connect"
|
|
32
|
+
}));
|
|
33
|
+
this.lastFailureTime = Date.now();
|
|
34
|
+
this.connectPromise = void 0;
|
|
35
|
+
throw err;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return this.connectPromise;
|
|
39
|
+
}
|
|
40
|
+
getRecordTimestamp(record) {
|
|
41
|
+
return record.lastUpdatedTimestamp ?? record.createdAt ?? record.requestedAtTimestamp ?? /* @__PURE__ */ new Date();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Stream a PoW captcha record (create or update) to the central DB.
|
|
45
|
+
* Fire-and-forget: errors are logged, never thrown.
|
|
46
|
+
*/
|
|
47
|
+
streamPowRecord(record, markStored) {
|
|
48
|
+
const timestamp = this.getRecordTimestamp(record);
|
|
49
|
+
this.ensureConnected().then(() => {
|
|
50
|
+
const { _id, ...safeDoc } = record;
|
|
51
|
+
return this.db.tables.powcaptcha.updateOne(
|
|
52
|
+
{ challenge: safeDoc.challenge },
|
|
53
|
+
{ $set: safeDoc },
|
|
54
|
+
{ upsert: true }
|
|
55
|
+
);
|
|
56
|
+
}).then(() => markStored?.(timestamp)).catch((err) => {
|
|
57
|
+
this.logger.error(() => ({
|
|
58
|
+
err,
|
|
59
|
+
msg: "Failed to stream PoW record to central DB"
|
|
60
|
+
}));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Stream a partial PoW update by fetching the full record first, then upserting.
|
|
65
|
+
*/
|
|
66
|
+
streamPowUpdate(getFullRecord, markStored) {
|
|
67
|
+
getFullRecord().then((record) => {
|
|
68
|
+
if (record) {
|
|
69
|
+
this.streamPowRecord(record, markStored);
|
|
70
|
+
}
|
|
71
|
+
}).catch((err) => {
|
|
72
|
+
this.logger.error(() => ({
|
|
73
|
+
err,
|
|
74
|
+
msg: "Failed to fetch PoW record for central DB streaming"
|
|
75
|
+
}));
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Stream an image captcha commitment record to the central DB.
|
|
80
|
+
* Fire-and-forget: errors are logged, never thrown.
|
|
81
|
+
*/
|
|
82
|
+
streamImageRecord(record, markStored) {
|
|
83
|
+
const timestamp = this.getRecordTimestamp(record);
|
|
84
|
+
this.ensureConnected().then(() => {
|
|
85
|
+
const { _id, ...safeDoc } = record;
|
|
86
|
+
return this.db.tables.commitment.updateOne(
|
|
87
|
+
{ id: safeDoc.id },
|
|
88
|
+
{ $set: safeDoc },
|
|
89
|
+
{ upsert: true }
|
|
90
|
+
);
|
|
91
|
+
}).then(() => markStored?.(timestamp)).catch((err) => {
|
|
92
|
+
this.logger.error(() => ({
|
|
93
|
+
err,
|
|
94
|
+
msg: "Failed to stream image record to central DB"
|
|
95
|
+
}));
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Stream an image captcha update by fetching the full record first.
|
|
100
|
+
*/
|
|
101
|
+
streamImageUpdate(getFullRecord, markStored) {
|
|
102
|
+
getFullRecord().then((record) => {
|
|
103
|
+
if (record) {
|
|
104
|
+
this.streamImageRecord(record, markStored);
|
|
105
|
+
}
|
|
106
|
+
}).catch((err) => {
|
|
107
|
+
this.logger.error(() => ({
|
|
108
|
+
err,
|
|
109
|
+
msg: "Failed to fetch image record for central DB streaming"
|
|
110
|
+
}));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Stream a session record to the central DB.
|
|
115
|
+
* Fire-and-forget: errors are logged, never thrown.
|
|
116
|
+
*/
|
|
117
|
+
streamSessionRecord(record, markStored) {
|
|
118
|
+
const timestamp = this.getRecordTimestamp(record);
|
|
119
|
+
this.ensureConnected().then(() => {
|
|
120
|
+
const { _id, ...safeDoc } = record;
|
|
121
|
+
return this.db.tables.session.updateOne(
|
|
122
|
+
{ sessionId: safeDoc.sessionId },
|
|
123
|
+
{ $set: safeDoc },
|
|
124
|
+
{ upsert: true }
|
|
125
|
+
);
|
|
126
|
+
}).then(() => markStored?.(timestamp)).catch((err) => {
|
|
127
|
+
this.logger.error(() => ({
|
|
128
|
+
err,
|
|
129
|
+
msg: "Failed to stream session record to central DB"
|
|
130
|
+
}));
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
export {
|
|
135
|
+
CentralDbStreamer
|
|
136
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"centralDbStreamer.js","sourceRoot":"","sources":["../../src/databases/centralDbStreamer.ts"],"names":[],"mappings":"AAcA,OAAO,EAAe,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAMzD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAuB/C,MAAM,OAAO,iBAAiB;IAO7B,YAAY,eAAuB,EAAE,MAAe;QAH5C,oBAAe,GAAG,CAAC,CAAC;QAI3B,IAAI,CAAC,MAAM,GAAG,MAAM,IAA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;YACV,MAAM,EAAE,GAAG,EAAE,GAAG,OAAO,EAAE,GAAG,MAAM,CAAC;YACnC,OAAO,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CACtC,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,EAChC,EAAE,IAAI,EAAE,OAAO,EAAE,EACjB,EAAE,MAAM,EAAE,IAAI,EAAE,CAChB,CAAC;QACH,CAAC,CAAC;aACD,IAAI,CAAC,GAAG,EAAE,CAAC,UAAU,EAAE,CAAC,SAAS,CAAC,CAAC;aACnC,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE;YACvB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC;gBACxB,GAAG;gBACH,GAAG,EAAE,+CAA+C;aACpD,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;;AAhLuB,uCAAqB,GAAG,KAAK,AAAR,CAAS"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Logger } from "@prosopo/logger";
|
|
2
|
+
import type { Timestamp } from "@prosopo/types";
|
|
3
|
+
import { type ClientRecord, type IClientDatabase, TableNames, type Tables } from "@prosopo/types-database";
|
|
4
|
+
import { MongoDatabase } from "../base/index.js";
|
|
5
|
+
export declare class ClientDatabase extends MongoDatabase implements IClientDatabase {
|
|
6
|
+
tables: Tables<TableNames>;
|
|
7
|
+
constructor(url: string, dbname?: string, authSource?: string, logger?: Logger);
|
|
8
|
+
connect(): Promise<void>;
|
|
9
|
+
getTables(): Tables<TableNames>;
|
|
10
|
+
getUpdatedClients(updatedAtTimestamp: Timestamp): Promise<ClientRecord[]>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/databases/client.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,MAAM,EAAE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,CAAC,KAAK,CAAC,OAAO;YAC7B,QAAQ,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ;YAC/B,IAAI,EAAE,MAAM,CAAC,IAAI;SACjB,CAAiB,CACnB,CACD,CAAC;QAEH,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACnB,OAAO,gBAAgB,CAAC;IACzB,CAAC;CACD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MongoDatabase } from "../base/mongo.js";
|
|
2
|
+
import { MongoMemoryDatabase } from "../base/mongoMemory.js";
|
|
3
|
+
import { CaptchaDatabase } from "./captcha.js";
|
|
4
|
+
import { ClientDatabase } from "./client.js";
|
|
5
|
+
import { ProviderDatabase } from "./provider.js";
|
|
6
|
+
export * from "./captcha.js";
|
|
7
|
+
export * from "./centralDbStreamer.js";
|
|
8
|
+
export * from "./client.js";
|
|
9
|
+
export { ProviderDatabase } from "./provider.js";
|
|
10
|
+
export declare const Databases: {
|
|
11
|
+
mongo: typeof MongoDatabase;
|
|
12
|
+
provider: typeof ProviderDatabase;
|
|
13
|
+
client: typeof ClientDatabase;
|
|
14
|
+
captcha: typeof CaptchaDatabase;
|
|
15
|
+
mongoMemory: typeof MongoMemoryDatabase;
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/databases/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,cAAc,cAAc,CAAC;AAC7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,eAAO,MAAM,SAAS;;;;;;CAMrB,CAAC"}
|