tricache 1.0.1 → 2.0.0
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/README.md +89 -93
- package/dashboards/tricache-alerts.yaml +80 -4
- package/dashboards/tricache-grafana.json +54 -1
- package/dashboards/tricache-inventory.rules.yml +21 -0
- package/dist/cache-service-BV5T8-20.d.cts +1064 -0
- package/dist/cache-service-UZXOQGEY.js +14 -0
- package/dist/{cache-service-CREOp7MM.d.ts → cache-service-tpyrXvlH.d.ts} +157 -24
- package/dist/{chunk-UBWLPKTI.js → chunk-EJXG7WO6.js} +152 -32
- package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
- package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
- package/dist/{chunk-2AJKEQMZ.js → chunk-KV7KISK2.js} +54 -5
- package/dist/{chunk-JYQFQJNJ.js → chunk-MB3DMVWN.js} +3424 -1022
- package/dist/chunk-RBJRTGFK.js +94 -0
- package/dist/chunk-S5N65I45.js +17 -0
- package/dist/cli.cjs +3694 -1070
- package/dist/cli.d.cts +12 -0
- package/dist/cli.js +210 -14
- package/dist/dashboard/index.cjs +111 -29
- package/dist/dashboard/index.d.cts +185 -0
- package/dist/dashboard/index.d.ts +5 -3
- package/dist/dashboard/index.js +1 -1
- package/dist/drizzle/index.cjs +3578 -1068
- package/dist/drizzle/index.d.cts +46 -0
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +6 -2
- package/dist/edge/index.cjs +368 -162
- package/dist/edge/index.d.cts +134 -0
- package/dist/edge/index.d.ts +11 -4
- package/dist/edge/index.js +314 -155
- package/dist/hono/index.cjs +3614 -1087
- package/dist/hono/index.d.cts +93 -0
- package/dist/hono/index.d.ts +7 -5
- package/dist/hono/index.js +16 -9
- package/dist/hono-CMv2VdQ6.d.cts +404 -0
- package/dist/{hono-DSV2ve66.d.ts → hono-Dvq04kX9.d.ts} +33 -7
- package/dist/http/index.cjs +3712 -1124
- package/dist/http/index.d.cts +103 -0
- package/dist/http/index.d.ts +13 -5
- package/dist/http/index.js +92 -43
- package/dist/index.cjs +4067 -1252
- package/dist/index.d.cts +1071 -0
- package/dist/index.d.ts +123 -27
- package/dist/index.js +878 -21
- package/dist/{murmur3-CMGw9PbV.d.ts → murmur3-DF8BFRnu.d.cts} +1 -0
- package/dist/murmur3-DF8BFRnu.d.ts +99 -0
- package/dist/nestjs/index.cjs +3635 -1112
- package/dist/nestjs/index.d.cts +299 -0
- package/dist/nestjs/index.d.ts +120 -33
- package/dist/nestjs/index.js +132 -43
- package/dist/next/index.cjs +3704 -1175
- package/dist/next/index.d.cts +238 -0
- package/dist/next/index.d.ts +39 -11
- package/dist/next/index.js +202 -102
- package/dist/prisma/index.cjs +29 -18
- package/dist/prisma/index.d.cts +62 -0
- package/dist/prisma/index.d.ts +2 -2
- package/dist/prisma/index.js +17 -18
- package/dist/serialize-worker.cjs +162 -39
- package/dist/serialize-worker.d.cts +2 -0
- package/dist/serialize-worker.js +9 -7
- package/dist/{types-Dyip5wAl.d.ts → types-F8vRWMrE.d.cts} +186 -54
- package/dist/types-F8vRWMrE.d.ts +1636 -0
- package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
- package/dist/utils-Bvzn-rKS.d.ts +29 -0
- package/package.json +116 -37
- package/src/adapters/auto-pipeliner.ts +150 -26
- package/src/adapters/node-redis.ts +106 -27
- package/src/cache-service.ts +2215 -793
- package/src/cli.ts +25 -14
- package/src/codec.ts +3 -3
- package/src/compression.ts +39 -16
- package/src/cross-region.ts +22 -4
- package/src/dashboard/handler.ts +26 -6
- package/src/dashboard/html.ts +35 -10
- package/src/dashboard/index.ts +35 -13
- package/src/dashboard/types.ts +3 -1
- package/src/disk-tier.ts +448 -102
- package/src/drizzle/index.ts +2 -1
- package/src/edge/adapters/cloudflare-do.ts +25 -10
- package/src/edge/adapters/cloudflare-kv.ts +12 -9
- package/src/edge/adapters/upstash.ts +27 -11
- package/src/edge/cache.ts +200 -110
- package/src/edge/hono.ts +23 -6
- package/src/edge/types.ts +17 -5
- package/src/encryption.ts +50 -18
- package/src/env.d.ts +1 -1
- package/src/errors.ts +12 -0
- package/src/hono/index.ts +19 -9
- package/src/http/express.ts +62 -17
- package/src/http/fastify.ts +35 -21
- package/src/http/utils.ts +60 -20
- package/src/index.ts +6 -0
- package/src/ipc-telemetry.ts +35 -9
- package/src/latency-watchdog.ts +5 -1
- package/src/nestjs/decorators.ts +11 -6
- package/src/nestjs/index.ts +2 -0
- package/src/nestjs/keyv-adapter.ts +68 -0
- package/src/nestjs/tricache.module.ts +15 -12
- package/src/nestjs/tricache.store.ts +69 -26
- package/src/nestjs/types.ts +30 -16
- package/src/next/cache-handler.ts +151 -53
- package/src/next/index.ts +1 -0
- package/src/next/types.ts +25 -0
- package/src/origin-capacity.ts +134 -0
- package/src/prisma/index.ts +17 -24
- package/src/remote-snapshot.ts +37 -27
- package/src/serialize-worker.ts +8 -5
- package/src/sigv4-snapshot-adapter.ts +37 -27
- package/src/smart-memory-cache.ts +121 -44
- package/src/types.ts +117 -37
- package/src/utils/cgroup.ts +12 -0
- package/src/utils/config-comparison.ts +29 -0
- package/src/utils/deadline.ts +14 -0
- package/src/utils/diagnostics.ts +27 -0
- package/src/utils/lz4-size.ts +74 -0
- package/src/utils/namespace-key.ts +48 -0
- package/src/utils/query-key.ts +13 -0
- package/src/utils/redis-slot.ts +13 -0
- package/src/utils/snapshot-transfer.ts +83 -0
- package/src/utils/work-limits.ts +73 -0
- package/src/utils/write-revision.ts +4 -0
- package/src/wasm/bloom-filter-wasm.ts +25 -5
- package/src/worker-pool.ts +44 -15
- package/src/wtiny-lfu.ts +82 -20
- package/dist/cache-service-MG7PN6MX.js +0 -12
- package/dist/chunk-3I5L7K6I.js +0 -704
- package/dist/chunk-BQRURGGD.js +0 -60
package/dist/edge/index.js
CHANGED
|
@@ -8,15 +8,17 @@ import {
|
|
|
8
8
|
isChildTag,
|
|
9
9
|
isWildcardTag,
|
|
10
10
|
matchesTagPattern,
|
|
11
|
+
namespaceDomainPrefix,
|
|
12
|
+
namespaceKeyPrefix,
|
|
11
13
|
uint8ArrayToBase64,
|
|
12
14
|
uint8ArrayToUtf8,
|
|
13
15
|
utf8ToUint8Array
|
|
14
|
-
} from "../chunk-
|
|
16
|
+
} from "../chunk-KV7KISK2.js";
|
|
15
17
|
import {
|
|
16
18
|
buildEdgeDeterministicKey,
|
|
17
19
|
computeEdgeETag,
|
|
18
20
|
honoEdgeCache
|
|
19
|
-
} from "../chunk-
|
|
21
|
+
} from "../chunk-EUKB7RKP.js";
|
|
20
22
|
|
|
21
23
|
// src/edge/crypto.ts
|
|
22
24
|
var PREFIX_256 = "enc:v1:";
|
|
@@ -152,15 +154,23 @@ var WebCryptoEncryption = class {
|
|
|
152
154
|
};
|
|
153
155
|
|
|
154
156
|
// src/edge/cache.ts
|
|
157
|
+
var NAMESPACE_TAG = "_tricache_namespace_";
|
|
155
158
|
var EdgeCacheService = class _EdgeCacheService {
|
|
156
159
|
maxKeys;
|
|
157
160
|
maxBytes;
|
|
158
161
|
defaultTtl;
|
|
159
162
|
namespace;
|
|
163
|
+
namespacePrefix;
|
|
164
|
+
dataPrefix;
|
|
165
|
+
tagVersionPrefix;
|
|
160
166
|
remoteStorage;
|
|
161
167
|
enc;
|
|
162
168
|
tracer;
|
|
163
169
|
bloomFilter;
|
|
170
|
+
bloomAuthoritative;
|
|
171
|
+
globalEpoch = 0;
|
|
172
|
+
lastGlobalMutationAt = 0;
|
|
173
|
+
keyEpochs = /* @__PURE__ */ new Map();
|
|
164
174
|
// L1 Dual-bounded LRU
|
|
165
175
|
l1 = /* @__PURE__ */ new Map();
|
|
166
176
|
currentBytes = 0;
|
|
@@ -177,9 +187,14 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
177
187
|
this.maxBytes = options?.maxBytes ?? 32 * 1024 * 1024;
|
|
178
188
|
this.defaultTtl = options?.defaultTtlSeconds ?? 300;
|
|
179
189
|
this.namespace = options?.namespace?.trim() ?? "";
|
|
190
|
+
this.namespacePrefix = namespaceKeyPrefix(this.namespace);
|
|
191
|
+
this.dataPrefix = namespaceDomainPrefix(this.namespace, "data");
|
|
192
|
+
this.tagVersionPrefix = namespaceDomainPrefix(this.namespace, "tag-version");
|
|
180
193
|
this.remoteStorage = options?.remoteStorage ?? null;
|
|
181
194
|
this.tracer = options?.tracer ?? null;
|
|
182
195
|
this.enc = new WebCryptoEncryption(options?.encryption);
|
|
196
|
+
this.bloomAuthoritative = options?.bloomFilterAuthoritative ?? false;
|
|
197
|
+
if (!Number.isInteger(this.maxKeys) || this.maxKeys < 0 || !Number.isFinite(this.maxBytes) || this.maxBytes < 0 || !Number.isFinite(this.defaultTtl) || this.defaultTtl < 0) throw new Error("Edge cache limits and TTL must be finite non-negative values");
|
|
183
198
|
if (options?.bloomFilter === true) {
|
|
184
199
|
try {
|
|
185
200
|
this.bloomFilter = new WasmBloomFilter();
|
|
@@ -199,11 +214,76 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
199
214
|
return this.bloomFilter;
|
|
200
215
|
}
|
|
201
216
|
nk(key) {
|
|
202
|
-
return
|
|
217
|
+
return `${this.dataPrefix}${key}`;
|
|
218
|
+
}
|
|
219
|
+
tagVersionKey(tag) {
|
|
220
|
+
return `${this.tagVersionPrefix}${tag}`;
|
|
203
221
|
}
|
|
204
222
|
_estimateBytes(key, serialized) {
|
|
205
223
|
return (key.length + serialized.length) * 2 + 64;
|
|
206
224
|
}
|
|
225
|
+
fence(key) {
|
|
226
|
+
return `${this.globalEpoch}:${this.keyEpochs.get(key) ?? 0}`;
|
|
227
|
+
}
|
|
228
|
+
mutate(key) {
|
|
229
|
+
this.keyEpochs.set(key, (this.keyEpochs.get(key) ?? 0) + 1);
|
|
230
|
+
this.inflight.delete(key);
|
|
231
|
+
if (this.keyEpochs.size > 5e4) {
|
|
232
|
+
this.globalEpoch++;
|
|
233
|
+
this.keyEpochs.clear();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
invalidateWork() {
|
|
237
|
+
this.globalEpoch++;
|
|
238
|
+
this.lastGlobalMutationAt = Date.now();
|
|
239
|
+
this.inflight.clear();
|
|
240
|
+
}
|
|
241
|
+
storeL1(key, entry) {
|
|
242
|
+
this._deleteL1(key);
|
|
243
|
+
if (entry.bytes > this.maxBytes || this.maxKeys === 0 || entry.staleUntil <= Date.now()) return false;
|
|
244
|
+
this._evictIfNeeded(entry.bytes);
|
|
245
|
+
this.l1.set(key, entry);
|
|
246
|
+
this.currentBytes += entry.bytes;
|
|
247
|
+
for (const tag of entry.tags ?? []) {
|
|
248
|
+
const members = this.tagIndex.get(tag) ?? /* @__PURE__ */ new Set();
|
|
249
|
+
members.add(key);
|
|
250
|
+
this.tagIndex.set(tag, members);
|
|
251
|
+
}
|
|
252
|
+
this.bloomFilter?.add(key);
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
async commit(key, value, ttl, options, versions, epoch) {
|
|
256
|
+
const namespaced = this.nk(key);
|
|
257
|
+
if (this.fence(namespaced) !== epoch) return;
|
|
258
|
+
const serialized = JSON.stringify(value);
|
|
259
|
+
if (serialized === void 0) return;
|
|
260
|
+
const now = Date.now();
|
|
261
|
+
const entry = {
|
|
262
|
+
value,
|
|
263
|
+
serialized,
|
|
264
|
+
bytes: this._estimateBytes(namespaced, serialized),
|
|
265
|
+
expiresAt: now + ttl * 1e3,
|
|
266
|
+
staleUntil: now + (ttl + (options?.swr ?? 0)) * 1e3,
|
|
267
|
+
tags: options?.tags ? [...new Set(options.tags)] : void 0,
|
|
268
|
+
tagVersions: versions
|
|
269
|
+
};
|
|
270
|
+
this.storeL1(namespaced, entry);
|
|
271
|
+
if (this.remoteStorage) {
|
|
272
|
+
const registry = namespaceDomainPrefix(this.namespace, "tag-registry");
|
|
273
|
+
for (const tag of entry.tags ?? []) await this.remoteStorage.set(`${registry}${tag}`, "1");
|
|
274
|
+
const envelope = JSON.stringify({
|
|
275
|
+
__t_edge: 2,
|
|
276
|
+
__t_val: value,
|
|
277
|
+
__t_tv: versions,
|
|
278
|
+
__t_exp: entry.expiresAt,
|
|
279
|
+
__t_stale: entry.staleUntil,
|
|
280
|
+
__t_tags: entry.tags
|
|
281
|
+
});
|
|
282
|
+
const payload = this.enc.isEnabled ? await this.enc.encrypt(envelope) : envelope;
|
|
283
|
+
if (this.fence(namespaced) !== epoch) return;
|
|
284
|
+
await this.remoteStorage.set(namespaced, payload, ttl + (options?.swr ?? 0));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
207
287
|
_evictIfNeeded(newBytes) {
|
|
208
288
|
while (this.currentBytes + newBytes > this.maxBytes && this.l1.size > 0) {
|
|
209
289
|
const oldestKey = this.l1.keys().next().value;
|
|
@@ -265,7 +345,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
265
345
|
if (oldestKey !== void 0) this.tagVersionCache.delete(oldestKey);
|
|
266
346
|
}
|
|
267
347
|
}
|
|
268
|
-
this.tagVersionCache.
|
|
348
|
+
const known = this.tagVersionCache.get(tag)?.version ?? 0;
|
|
349
|
+
this.tagVersionCache.set(tag, { version: Math.max(known, version), expiresAt });
|
|
269
350
|
}
|
|
270
351
|
async _getTagVersion(tag) {
|
|
271
352
|
const cached = this.tagVersionCache.get(tag);
|
|
@@ -277,9 +358,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
277
358
|
if (this.remoteStorage) {
|
|
278
359
|
try {
|
|
279
360
|
if (typeof this.remoteStorage.getTagVersion === "function") {
|
|
280
|
-
version = await this.remoteStorage.getTagVersion(this.
|
|
361
|
+
version = await this.remoteStorage.getTagVersion(this.tagVersionKey(tag));
|
|
281
362
|
} else {
|
|
282
|
-
const raw = await this.remoteStorage.get(this.
|
|
363
|
+
const raw = await this.remoteStorage.get(this.tagVersionKey(tag));
|
|
283
364
|
version = raw ? parseInt(raw, 10) || 1 : 1;
|
|
284
365
|
}
|
|
285
366
|
} catch {
|
|
@@ -289,11 +370,11 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
289
370
|
version = cached?.version ?? 1;
|
|
290
371
|
}
|
|
291
372
|
this._setTagVersionCache(tag, version, now + this.tagVersionTtlMs);
|
|
292
|
-
return version;
|
|
373
|
+
return this.tagVersionCache.get(tag)?.version ?? version;
|
|
293
374
|
}
|
|
294
375
|
async _batchGetTagVersions(tags) {
|
|
295
376
|
const now = Date.now();
|
|
296
|
-
const result =
|
|
377
|
+
const result = /* @__PURE__ */ Object.create(null);
|
|
297
378
|
const missingTags = [];
|
|
298
379
|
for (const t of tags) {
|
|
299
380
|
const c = this.tagVersionCache.get(t);
|
|
@@ -306,12 +387,12 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
306
387
|
if (missingTags.length > 0 && this.remoteStorage) {
|
|
307
388
|
try {
|
|
308
389
|
if (typeof this.remoteStorage.batchGetTagVersions === "function") {
|
|
309
|
-
const nsTags = missingTags.map((t) => this.
|
|
390
|
+
const nsTags = missingTags.map((t) => this.tagVersionKey(t));
|
|
310
391
|
const remoteVersions = await this.remoteStorage.batchGetTagVersions(nsTags);
|
|
311
392
|
for (let i = 0; i < missingTags.length; i++) {
|
|
312
393
|
const t = missingTags[i];
|
|
313
|
-
const v = remoteVersions[this.
|
|
314
|
-
result[t] = v;
|
|
394
|
+
const v = remoteVersions[this.tagVersionKey(t)] ?? 1;
|
|
395
|
+
result[t] = Math.max(this.tagVersionCache.get(t)?.version ?? 0, v);
|
|
315
396
|
this._setTagVersionCache(t, v, now + this.tagVersionTtlMs);
|
|
316
397
|
}
|
|
317
398
|
} else {
|
|
@@ -339,7 +420,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
339
420
|
const span = this._startSpan("tricache.get");
|
|
340
421
|
span.setAttribute("cache.key", key);
|
|
341
422
|
const nsKey = this.nk(key);
|
|
342
|
-
|
|
423
|
+
let now = Date.now();
|
|
343
424
|
const resolvedTtl = ttlSeconds ?? this.defaultTtl;
|
|
344
425
|
try {
|
|
345
426
|
const l1Entry = this.l1.get(nsKey);
|
|
@@ -355,6 +436,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
355
436
|
}
|
|
356
437
|
}
|
|
357
438
|
}
|
|
439
|
+
now = Date.now();
|
|
358
440
|
if (isTagStale) {
|
|
359
441
|
this._deleteL1(nsKey);
|
|
360
442
|
} else {
|
|
@@ -366,85 +448,115 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
366
448
|
span.setAttribute("cache.hit_tier", "l1");
|
|
367
449
|
return l1Entry.value;
|
|
368
450
|
}
|
|
369
|
-
if (l1Entry.staleUntil > now && fetchFn) {
|
|
451
|
+
if (l1Entry.staleUntil > now && fetchFn && (options?.swr ?? 0) > 0) {
|
|
370
452
|
span.setAttribute("cache.hit", true);
|
|
371
453
|
span.setAttribute("cache.item.tier", "memory");
|
|
372
454
|
span.setAttribute("cache.hit_tier", "l1");
|
|
373
455
|
span.setAttribute("cache.stale", true);
|
|
374
|
-
const revalPromise = this._revalidate(key, fetchFn, resolvedTtl, options);
|
|
456
|
+
const revalPromise = this._revalidate(key, fetchFn, resolvedTtl, { ...options, tags: options?.tags ?? l1Entry.tags });
|
|
375
457
|
if (options?.ctx?.waitUntil) {
|
|
376
458
|
options.ctx.waitUntil(revalPromise);
|
|
377
459
|
}
|
|
378
460
|
return l1Entry.value;
|
|
379
461
|
}
|
|
380
|
-
this._deleteL1(nsKey);
|
|
462
|
+
if (l1Entry.staleUntil <= now) this._deleteL1(nsKey);
|
|
381
463
|
}
|
|
382
464
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
465
|
+
const existing = this.inflight.get(nsKey);
|
|
466
|
+
if (existing) {
|
|
467
|
+
span.setAttribute("cache.stampede_coalesced", true);
|
|
468
|
+
return await existing;
|
|
469
|
+
}
|
|
470
|
+
const epoch = this.fence(nsKey);
|
|
471
|
+
const execution = Promise.resolve().then(async () => {
|
|
472
|
+
try {
|
|
473
|
+
if (this.remoteStorage) {
|
|
474
|
+
if (this.bloomAuthoritative && this.bloomFilter && !this.bloomFilter.mightContain(nsKey)) {
|
|
475
|
+
span.setAttribute("cache.bloom.filtered", true);
|
|
476
|
+
} else {
|
|
477
|
+
try {
|
|
478
|
+
const rawRemote = await this.remoteStorage.get(nsKey);
|
|
479
|
+
if (rawRemote !== null) {
|
|
480
|
+
const plain = this.enc.isEnabled ? await this.enc.decrypt(rawRemote) : rawRemote;
|
|
481
|
+
const parsedJson = JSON.parse(plain);
|
|
482
|
+
let parsed;
|
|
483
|
+
let activeTagVersions;
|
|
484
|
+
let isStaleRemote = false;
|
|
485
|
+
if (parsedJson && typeof parsedJson === "object" && "__t_val" in parsedJson && "__t_tv" in parsedJson) {
|
|
486
|
+
parsed = parsedJson.__t_val;
|
|
487
|
+
activeTagVersions = parsedJson.__t_tv;
|
|
488
|
+
if (!activeTagVersions || typeof activeTagVersions !== "object" || Array.isArray(activeTagVersions) || Object.values(activeTagVersions).some((version) => !Number.isSafeInteger(version) || version < 0)) throw new Error("Invalid remote tag versions");
|
|
489
|
+
const tagNames = Object.keys(activeTagVersions);
|
|
490
|
+
const currentVers = await this._batchGetTagVersions(tagNames);
|
|
491
|
+
for (const tag of tagNames) {
|
|
492
|
+
if (currentVers[tag] > activeTagVersions[tag]) {
|
|
493
|
+
isStaleRemote = true;
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
} else {
|
|
498
|
+
parsed = parsedJson;
|
|
499
|
+
}
|
|
500
|
+
if (!isStaleRemote) {
|
|
501
|
+
const freshUntil = Number(parsedJson?.__t_exp);
|
|
502
|
+
const staleUntil = Number(parsedJson?.__t_stale);
|
|
503
|
+
const versioned = parsedJson?.__t_edge === 2 && Number.isFinite(freshUntil) && Number.isFinite(staleUntil) && staleUntil >= freshUntil;
|
|
504
|
+
if (versioned && (staleUntil <= Date.now() || freshUntil <= Date.now() && (!(options?.swr ?? 0) || !fetchFn))) {
|
|
505
|
+
isStaleRemote = true;
|
|
506
|
+
} else {
|
|
507
|
+
if (versioned && this.fence(nsKey) === epoch) {
|
|
508
|
+
const serialized = JSON.stringify(parsed);
|
|
509
|
+
const tags2 = Array.isArray(parsedJson.__t_tags) ? parsedJson.__t_tags : Object.keys(activeTagVersions ?? {}).filter((tag) => tag !== NAMESPACE_TAG);
|
|
510
|
+
this.storeL1(nsKey, {
|
|
511
|
+
value: parsed,
|
|
512
|
+
serialized,
|
|
513
|
+
bytes: this._estimateBytes(nsKey, serialized),
|
|
514
|
+
expiresAt: freshUntil,
|
|
515
|
+
staleUntil,
|
|
516
|
+
tags: tags2,
|
|
517
|
+
tagVersions: activeTagVersions
|
|
518
|
+
});
|
|
519
|
+
if (freshUntil <= Date.now() && fetchFn) {
|
|
520
|
+
const refresh = execution.then(() => this._revalidate(key, fetchFn, resolvedTtl, { ...options, tags: tags2 }));
|
|
521
|
+
options?.ctx?.waitUntil(refresh);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
span.setAttribute("cache.hit", true);
|
|
525
|
+
span.setAttribute("cache.item.tier", "remote");
|
|
526
|
+
span.setAttribute("cache.hit_tier", "l2");
|
|
527
|
+
return parsed;
|
|
528
|
+
}
|
|
404
529
|
}
|
|
405
530
|
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
if (!isStaleRemote) {
|
|
410
|
-
await this.set(key, parsed, resolvedTtl, options);
|
|
411
|
-
span.setAttribute("cache.hit", true);
|
|
412
|
-
span.setAttribute("cache.item.tier", "remote");
|
|
413
|
-
span.setAttribute("cache.hit_tier", "l2");
|
|
414
|
-
return parsed;
|
|
531
|
+
} catch (remoteErr) {
|
|
532
|
+
span.recordException?.(remoteErr);
|
|
415
533
|
}
|
|
416
534
|
}
|
|
417
|
-
} catch (remoteErr) {
|
|
418
|
-
span.recordException?.(remoteErr);
|
|
419
535
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
span.setAttribute("cache.hit_tier", "miss");
|
|
425
|
-
return null;
|
|
426
|
-
}
|
|
427
|
-
let inflightPromise = this.inflight.get(nsKey);
|
|
428
|
-
let coalesced = false;
|
|
429
|
-
if (inflightPromise) {
|
|
430
|
-
coalesced = true;
|
|
431
|
-
} else {
|
|
432
|
-
inflightPromise = (async () => {
|
|
433
|
-
try {
|
|
434
|
-
const fresh = await fetchFn();
|
|
435
|
-
await this.set(key, fresh, resolvedTtl, options);
|
|
436
|
-
return fresh;
|
|
437
|
-
} finally {
|
|
438
|
-
this.inflight.delete(nsKey);
|
|
536
|
+
if (!fetchFn) {
|
|
537
|
+
span.setAttribute("cache.hit", false);
|
|
538
|
+
span.setAttribute("cache.hit_tier", "miss");
|
|
539
|
+
return null;
|
|
439
540
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
541
|
+
const tags = [.../* @__PURE__ */ new Set([NAMESPACE_TAG, ...options?.tags ?? []])];
|
|
542
|
+
const versions = await this._batchGetTagVersions(tags);
|
|
543
|
+
const fresh = await fetchFn();
|
|
544
|
+
const current = await this._batchGetTagVersions(tags);
|
|
545
|
+
if (this.fence(nsKey) === epoch && tags.every((tag) => current[tag] === versions[tag])) {
|
|
546
|
+
try {
|
|
547
|
+
await this.commit(key, fresh, resolvedTtl, options, versions, epoch);
|
|
548
|
+
} catch (error) {
|
|
549
|
+
span.recordException?.(error);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
span.setAttribute("cache.hit", false).setAttribute("cache.hit_tier", "miss");
|
|
553
|
+
return fresh;
|
|
554
|
+
} finally {
|
|
555
|
+
if (this.inflight.get(nsKey) === execution) this.inflight.delete(nsKey);
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
this.inflight.set(nsKey, execution);
|
|
559
|
+
return await execution;
|
|
448
560
|
} catch (err) {
|
|
449
561
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
450
562
|
span.recordException?.(err);
|
|
@@ -456,15 +568,19 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
456
568
|
async _revalidate(key, fetchFn, ttl, options) {
|
|
457
569
|
const nsKey = this.nk(key);
|
|
458
570
|
if (this.inflight.has(nsKey)) return;
|
|
459
|
-
const
|
|
571
|
+
const epoch = this.fence(nsKey);
|
|
572
|
+
const task = Promise.resolve().then(async () => {
|
|
460
573
|
try {
|
|
574
|
+
const tags = [.../* @__PURE__ */ new Set([NAMESPACE_TAG, ...options?.tags ?? []])];
|
|
575
|
+
const versions = await this._batchGetTagVersions(tags);
|
|
461
576
|
const fresh = await fetchFn();
|
|
462
|
-
await this.
|
|
577
|
+
const current = await this._batchGetTagVersions(tags);
|
|
578
|
+
if (this.fence(nsKey) === epoch && tags.every((tag) => current[tag] === versions[tag])) await this.commit(key, fresh, ttl, options, versions, epoch);
|
|
463
579
|
} catch {
|
|
464
580
|
} finally {
|
|
465
|
-
this.inflight.delete(nsKey);
|
|
581
|
+
if (this.inflight.get(nsKey) === task) this.inflight.delete(nsKey);
|
|
466
582
|
}
|
|
467
|
-
})
|
|
583
|
+
});
|
|
468
584
|
this.inflight.set(nsKey, task);
|
|
469
585
|
await task;
|
|
470
586
|
}
|
|
@@ -475,48 +591,15 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
475
591
|
const span = this._startSpan("tricache.set");
|
|
476
592
|
span.setAttribute("cache.key", key);
|
|
477
593
|
const nsKey = this.nk(key);
|
|
594
|
+
this.mutate(nsKey);
|
|
595
|
+
const epoch = this.fence(nsKey);
|
|
478
596
|
const resolvedTtl = ttlSeconds ?? this.defaultTtl;
|
|
479
|
-
const now = Date.now();
|
|
480
597
|
const optObj = Array.isArray(options) ? { tags: options } : options;
|
|
481
598
|
const swrSeconds = optObj?.swr ?? 0;
|
|
482
599
|
try {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
}
|
|
487
|
-
const serialized = JSON.stringify(value);
|
|
488
|
-
const bytes = this._estimateBytes(nsKey, serialized);
|
|
489
|
-
this._deleteL1(nsKey);
|
|
490
|
-
this._evictIfNeeded(bytes);
|
|
491
|
-
const entry = {
|
|
492
|
-
value,
|
|
493
|
-
serialized,
|
|
494
|
-
bytes,
|
|
495
|
-
expiresAt: now + resolvedTtl * 1e3,
|
|
496
|
-
staleUntil: now + (resolvedTtl + swrSeconds) * 1e3,
|
|
497
|
-
tags: optObj?.tags,
|
|
498
|
-
tagVersions: activeTagVersions
|
|
499
|
-
};
|
|
500
|
-
this.l1.set(nsKey, entry);
|
|
501
|
-
this.currentBytes += bytes;
|
|
502
|
-
if (optObj?.tags) {
|
|
503
|
-
for (const tag of optObj.tags) {
|
|
504
|
-
let s = this.tagIndex.get(tag);
|
|
505
|
-
if (!s) {
|
|
506
|
-
s = /* @__PURE__ */ new Set();
|
|
507
|
-
this.tagIndex.set(tag, s);
|
|
508
|
-
}
|
|
509
|
-
s.add(nsKey);
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
if (this.remoteStorage) {
|
|
513
|
-
const toStoreRaw = activeTagVersions ? JSON.stringify({ __t_val: value, __t_tv: activeTagVersions }) : serialized;
|
|
514
|
-
const payloadToStore = this.enc.isEnabled ? await this.enc.encrypt(toStoreRaw) : toStoreRaw;
|
|
515
|
-
await this.remoteStorage.set(nsKey, payloadToStore, resolvedTtl + swrSeconds);
|
|
516
|
-
}
|
|
517
|
-
if (this.bloomFilter) {
|
|
518
|
-
this.bloomFilter.add(nsKey);
|
|
519
|
-
}
|
|
600
|
+
if (!Number.isFinite(resolvedTtl) || resolvedTtl < 0 || !Number.isFinite(swrSeconds) || swrSeconds < 0) throw new Error("Edge TTL and SWR must be finite non-negative values");
|
|
601
|
+
const versions = await this._batchGetTagVersions([.../* @__PURE__ */ new Set([NAMESPACE_TAG, ...optObj?.tags ?? []])]);
|
|
602
|
+
await this.commit(key, value, resolvedTtl, optObj, versions, epoch);
|
|
520
603
|
} catch (err) {
|
|
521
604
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
522
605
|
span.recordException?.(err);
|
|
@@ -533,6 +616,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
533
616
|
span.setAttribute("cache.key", key);
|
|
534
617
|
const nsKey = this.nk(key);
|
|
535
618
|
try {
|
|
619
|
+
this.mutate(nsKey);
|
|
536
620
|
this._deleteL1(nsKey);
|
|
537
621
|
if (this.remoteStorage) {
|
|
538
622
|
await this.remoteStorage.delete(nsKey);
|
|
@@ -550,13 +634,17 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
550
634
|
* re-entering wildcard/cascade resolution.
|
|
551
635
|
*/
|
|
552
636
|
async _invalidateConcreteTag(tag) {
|
|
637
|
+
this.invalidateWork();
|
|
638
|
+
const previous = this.tagVersionCache.get(tag)?.version ?? 1;
|
|
553
639
|
let newVer = 1;
|
|
554
640
|
if (this.remoteStorage) {
|
|
555
641
|
try {
|
|
556
642
|
if (typeof this.remoteStorage.incrementTagVersion === "function") {
|
|
557
|
-
newVer = await this.remoteStorage.incrementTagVersion(this.
|
|
643
|
+
newVer = await this.remoteStorage.incrementTagVersion(this.tagVersionKey(tag));
|
|
644
|
+
if (newVer <= previous) newVer = await this.remoteStorage.incrementTagVersion(this.tagVersionKey(tag));
|
|
645
|
+
newVer = Math.max(previous + 1, newVer);
|
|
558
646
|
} else {
|
|
559
|
-
const key = this.
|
|
647
|
+
const key = this.tagVersionKey(tag);
|
|
560
648
|
const current = await this._getTagVersion(tag);
|
|
561
649
|
newVer = Math.max(current + 1, Date.now());
|
|
562
650
|
await this.remoteStorage.set(key, String(newVer));
|
|
@@ -569,7 +657,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
569
657
|
const current = this.tagVersionCache.get(tag)?.version ?? 1;
|
|
570
658
|
newVer = current + 1;
|
|
571
659
|
}
|
|
572
|
-
this._setTagVersionCache(tag, newVer, Date.now() +
|
|
660
|
+
this._setTagVersionCache(tag, newVer, Date.now() + this.tagVersionTtlMs);
|
|
573
661
|
const keys = this.tagIndex.get(tag);
|
|
574
662
|
if (keys) {
|
|
575
663
|
const keysArr = Array.from(keys);
|
|
@@ -590,6 +678,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
590
678
|
const isPattern = isWildcardTag(tag);
|
|
591
679
|
const shouldCascade = Boolean(opts?.cascade);
|
|
592
680
|
if (isPattern || shouldCascade) {
|
|
681
|
+
if (this.remoteStorage && !this.remoteStorage.scanKeys) {
|
|
682
|
+
throw new Error("Remote wildcard/cascade tag invalidation requires an adapter with scanKeys");
|
|
683
|
+
}
|
|
593
684
|
const matched = /* @__PURE__ */ new Set();
|
|
594
685
|
const tagMatches = (t) => {
|
|
595
686
|
if (t === tag) return true;
|
|
@@ -603,6 +694,14 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
603
694
|
for (const t of this.tagVersionCache.keys()) {
|
|
604
695
|
if (tagMatches(t)) matched.add(t);
|
|
605
696
|
}
|
|
697
|
+
if (this.remoteStorage?.scanKeys) {
|
|
698
|
+
const registry = namespaceDomainPrefix(this.namespace, "tag-registry");
|
|
699
|
+
for await (const key of this.remoteStorage.scanKeys(registry)) {
|
|
700
|
+
if (!key.startsWith(registry)) continue;
|
|
701
|
+
const concrete = key.slice(registry.length);
|
|
702
|
+
if (tagMatches(concrete)) matched.add(concrete);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
606
705
|
if (shouldCascade && !isPattern) matched.add(tag);
|
|
607
706
|
for (const t of matched) {
|
|
608
707
|
await this._invalidateConcreteTag(t);
|
|
@@ -624,6 +723,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
624
723
|
async clear() {
|
|
625
724
|
const span = this._startSpan("tricache.clear");
|
|
626
725
|
try {
|
|
726
|
+
const previousNamespace = await this._getTagVersion(NAMESPACE_TAG);
|
|
727
|
+
this.invalidateWork();
|
|
627
728
|
this.l1.clear();
|
|
628
729
|
this.currentBytes = 0;
|
|
629
730
|
this.tagIndex.clear();
|
|
@@ -632,8 +733,10 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
632
733
|
this.bloomFilter.reset();
|
|
633
734
|
}
|
|
634
735
|
if (this.remoteStorage) {
|
|
635
|
-
await this.remoteStorage.clear?.(this.
|
|
736
|
+
await this.remoteStorage.clear?.(this.dataPrefix);
|
|
636
737
|
}
|
|
738
|
+
this._setTagVersionCache(NAMESPACE_TAG, previousNamespace, Date.now() + this.tagVersionTtlMs);
|
|
739
|
+
await this._invalidateConcreteTag(NAMESPACE_TAG);
|
|
637
740
|
} catch (err) {
|
|
638
741
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
639
742
|
span.recordException?.(err);
|
|
@@ -647,6 +750,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
647
750
|
*/
|
|
648
751
|
async mget(keys, fetchFn, ttlSeconds) {
|
|
649
752
|
const span = this._startSpan("tricache.mget");
|
|
753
|
+
const epochs = new Map(keys.map((key) => [key, this.fence(this.nk(key))]));
|
|
650
754
|
span.setAttribute("cache.batch.size", keys.length);
|
|
651
755
|
try {
|
|
652
756
|
const results = Array.from({ length: keys.length }, () => null);
|
|
@@ -668,10 +772,10 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
668
772
|
const isMap = fetchedMap instanceof Map;
|
|
669
773
|
for (let j = 0; j < missKeys.length; j++) {
|
|
670
774
|
const k = missKeys[j];
|
|
671
|
-
const fetchedVal = isMap ? fetchedMap.get(k) : fetchedMap[k];
|
|
775
|
+
const fetchedVal = isMap ? fetchedMap.get(k) : Object.hasOwn(fetchedMap, k) ? fetchedMap[k] : void 0;
|
|
672
776
|
if (fetchedVal !== void 0) {
|
|
673
777
|
results[missIndices[j]] = fetchedVal;
|
|
674
|
-
await this.set(k, fetchedVal, ttlSeconds);
|
|
778
|
+
if (this.fence(this.nk(k)) === epochs.get(k)) await this.set(k, fetchedVal, ttlSeconds);
|
|
675
779
|
}
|
|
676
780
|
}
|
|
677
781
|
}
|
|
@@ -693,7 +797,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
693
797
|
const entries = [];
|
|
694
798
|
for (const [nsKey, entry] of this.l1.entries()) {
|
|
695
799
|
if (entry.expiresAt > now || entry.staleUntil > now) {
|
|
696
|
-
|
|
800
|
+
if (!nsKey.startsWith(this.dataPrefix)) continue;
|
|
801
|
+
const cleanKey = nsKey.slice(this.dataPrefix.length);
|
|
697
802
|
entries.push({
|
|
698
803
|
key: cleanKey,
|
|
699
804
|
value: entry.value,
|
|
@@ -705,7 +810,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
705
810
|
}
|
|
706
811
|
}
|
|
707
812
|
const payload = {
|
|
708
|
-
version:
|
|
813
|
+
version: 3,
|
|
814
|
+
namespace: this.namespacePrefix,
|
|
709
815
|
writtenAt: now,
|
|
710
816
|
entries
|
|
711
817
|
};
|
|
@@ -733,6 +839,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
733
839
|
* }
|
|
734
840
|
*/
|
|
735
841
|
async hydrate(source, options) {
|
|
842
|
+
const epoch = this.globalEpoch;
|
|
736
843
|
let rawStr = null;
|
|
737
844
|
if (source && typeof source === "object") {
|
|
738
845
|
if ("get" in source && typeof source.get === "function") {
|
|
@@ -771,13 +878,14 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
771
878
|
} catch {
|
|
772
879
|
return 0;
|
|
773
880
|
}
|
|
774
|
-
if (!payload || !Array.isArray(payload.entries)) {
|
|
881
|
+
if (!payload || payload.version !== 3 || payload.namespace !== this.namespacePrefix || !Array.isArray(payload.entries) || this.globalEpoch !== epoch) {
|
|
775
882
|
return 0;
|
|
776
883
|
}
|
|
777
884
|
const now = Date.now();
|
|
778
885
|
const maxAgeMs = options?.maxAgeMs ?? 72e5;
|
|
779
886
|
const tolerance = options?.clockSkewToleranceMs ?? 250;
|
|
780
887
|
const writtenAt = payload.writtenAt ?? 0;
|
|
888
|
+
if (!Number.isFinite(writtenAt) || writtenAt < this.lastGlobalMutationAt) return 0;
|
|
781
889
|
if (writtenAt - now > tolerance) {
|
|
782
890
|
return 0;
|
|
783
891
|
}
|
|
@@ -788,10 +896,23 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
788
896
|
let imported = 0;
|
|
789
897
|
for (const item of payload.entries) {
|
|
790
898
|
if (!item || typeof item.key !== "string") continue;
|
|
899
|
+
if (!Number.isFinite(item.expiresAt) || !Number.isFinite(item.staleUntil)) continue;
|
|
791
900
|
if (item.expiresAt <= now && item.staleUntil <= now) continue;
|
|
901
|
+
if (item.value === void 0) continue;
|
|
902
|
+
if (item.tags !== void 0 && (!Array.isArray(item.tags) || item.tags.some((tag) => typeof tag !== "string"))) continue;
|
|
903
|
+
if (item.tagVersions !== void 0 && (typeof item.tagVersions !== "object" || item.tagVersions === null || Array.isArray(item.tagVersions) || Object.values(item.tagVersions).some((version) => !Number.isFinite(version)))) continue;
|
|
904
|
+
let serialized;
|
|
905
|
+
try {
|
|
906
|
+
const encoded = JSON.stringify(item.value);
|
|
907
|
+
if (typeof encoded !== "string") continue;
|
|
908
|
+
serialized = encoded;
|
|
909
|
+
} catch {
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
792
912
|
const nsKey = this.nk(item.key);
|
|
793
|
-
|
|
913
|
+
if (this.l1.has(nsKey) || this.keyEpochs.has(nsKey)) continue;
|
|
794
914
|
const bytes = this._estimateBytes(nsKey, serialized);
|
|
915
|
+
if (bytes > this.maxBytes || this.maxKeys === 0) continue;
|
|
795
916
|
this._deleteL1(nsKey);
|
|
796
917
|
this._evictIfNeeded(bytes);
|
|
797
918
|
const entry = {
|
|
@@ -816,7 +937,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
816
937
|
}
|
|
817
938
|
}
|
|
818
939
|
if (this.bloomFilter) {
|
|
819
|
-
this.bloomFilter.add(
|
|
940
|
+
this.bloomFilter.add(nsKey);
|
|
820
941
|
}
|
|
821
942
|
imported++;
|
|
822
943
|
}
|
|
@@ -838,9 +959,11 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
838
959
|
|
|
839
960
|
// src/edge/adapters/upstash.ts
|
|
840
961
|
var UpstashRedisAdapter = class {
|
|
962
|
+
tagConsistency = "atomic";
|
|
841
963
|
baseUrl;
|
|
842
964
|
token;
|
|
843
965
|
customFetch;
|
|
966
|
+
dangerouslyFlushDatabase;
|
|
844
967
|
constructor(options) {
|
|
845
968
|
if (!options?.url || !options?.token) {
|
|
846
969
|
throw new Error("UpstashRedisAdapter: url and token are required.");
|
|
@@ -848,6 +971,7 @@ var UpstashRedisAdapter = class {
|
|
|
848
971
|
this.baseUrl = options.url.replace(/\/+$/, "");
|
|
849
972
|
this.token = options.token;
|
|
850
973
|
this.customFetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
974
|
+
this.dangerouslyFlushDatabase = options.dangerouslyFlushDatabase ?? false;
|
|
851
975
|
}
|
|
852
976
|
async _executeCommand(command) {
|
|
853
977
|
const res = await this.customFetch(this.baseUrl, {
|
|
@@ -923,29 +1047,43 @@ var UpstashRedisAdapter = class {
|
|
|
923
1047
|
await this._executeCommand(["MSET", ...args]);
|
|
924
1048
|
}
|
|
925
1049
|
}
|
|
1050
|
+
async *scanKeys(prefix) {
|
|
1051
|
+
const pattern = prefix.replaceAll("\\", "\\\\").replaceAll("*", "\\*").replaceAll("?", "\\?").replaceAll("[", "\\[").replaceAll("]", "\\]") + "*";
|
|
1052
|
+
let cursor = "0";
|
|
1053
|
+
do {
|
|
1054
|
+
const result = await this._executeCommand(["SCAN", cursor, "MATCH", pattern, "COUNT", 256]);
|
|
1055
|
+
if (!Array.isArray(result) || !Array.isArray(result[1])) throw new Error("Invalid Upstash SCAN response");
|
|
1056
|
+
cursor = String(result[0]);
|
|
1057
|
+
for (const key of result[1]) if (key.startsWith(prefix)) yield key;
|
|
1058
|
+
} while (cursor !== "0");
|
|
1059
|
+
}
|
|
926
1060
|
async clear(prefix) {
|
|
927
|
-
if (prefix) {
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
1061
|
+
if (!prefix) {
|
|
1062
|
+
if (!this.dangerouslyFlushDatabase) throw new Error("Unscoped Upstash clear requires dangerouslyFlushDatabase: true");
|
|
1063
|
+
await this._executeCommand(["FLUSHDB"]);
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
let keys = [];
|
|
1067
|
+
for await (const key of this.scanKeys(prefix)) {
|
|
1068
|
+
keys.push(key);
|
|
1069
|
+
if (keys.length === 256) {
|
|
931
1070
|
await this._executeCommand(["DEL", ...keys]);
|
|
1071
|
+
keys = [];
|
|
932
1072
|
}
|
|
933
|
-
} else {
|
|
934
|
-
await this._executeCommand(["FLUSHDB"]);
|
|
935
1073
|
}
|
|
1074
|
+
if (keys.length) await this._executeCommand(["DEL", ...keys]);
|
|
936
1075
|
}
|
|
937
1076
|
async incrementTagVersion(tag) {
|
|
938
|
-
const res = await this._executeCommand(["INCR",
|
|
1077
|
+
const res = await this._executeCommand(["INCR", tag]);
|
|
939
1078
|
return typeof res === "number" ? res : 1;
|
|
940
1079
|
}
|
|
941
1080
|
async getTagVersion(tag) {
|
|
942
|
-
const res = await this._executeCommand(["GET",
|
|
1081
|
+
const res = await this._executeCommand(["GET", tag]);
|
|
943
1082
|
return res ? parseInt(res, 10) || 1 : 1;
|
|
944
1083
|
}
|
|
945
1084
|
async batchGetTagVersions(tags) {
|
|
946
1085
|
if (tags.length === 0) return {};
|
|
947
|
-
const
|
|
948
|
-
const res = await this.mget(keys);
|
|
1086
|
+
const res = await this.mget(tags);
|
|
949
1087
|
const result = {};
|
|
950
1088
|
for (let i = 0; i < tags.length; i++) {
|
|
951
1089
|
const raw = res[i];
|
|
@@ -958,6 +1096,7 @@ var UpstashRedisAdapter = class {
|
|
|
958
1096
|
// src/edge/adapters/cloudflare-kv.ts
|
|
959
1097
|
var MIN_CF_KV_TTL = 60;
|
|
960
1098
|
var CloudflareKVAdapter = class {
|
|
1099
|
+
tagConsistency = "eventual";
|
|
961
1100
|
kv;
|
|
962
1101
|
constructor(kv) {
|
|
963
1102
|
if (!kv) {
|
|
@@ -1009,19 +1148,21 @@ var CloudflareKVAdapter = class {
|
|
|
1009
1148
|
const keys = Object.keys(entries);
|
|
1010
1149
|
await Promise.all(keys.map((k) => this.set(k, entries[k], ttlSeconds)));
|
|
1011
1150
|
}
|
|
1012
|
-
async
|
|
1013
|
-
if (!this.kv.list)
|
|
1151
|
+
async *scanKeys(prefix) {
|
|
1152
|
+
if (!this.kv.list) throw new Error("Workers KV binding does not support list");
|
|
1014
1153
|
let cursor;
|
|
1015
1154
|
do {
|
|
1016
|
-
const res = await this.kv.list({ prefix, cursor });
|
|
1017
|
-
|
|
1018
|
-
await Promise.all(res.keys.map((item) => this.kv.delete(item.name)));
|
|
1019
|
-
}
|
|
1155
|
+
const res = await this.kv.list({ prefix, cursor, limit: 256 });
|
|
1156
|
+
for (const item of res.keys) if (item.name.startsWith(prefix)) yield item.name;
|
|
1020
1157
|
cursor = res.list_complete ? void 0 : res.cursor;
|
|
1158
|
+
if (!res.list_complete && !cursor) throw new Error("Workers KV list omitted its continuation cursor");
|
|
1021
1159
|
} while (cursor);
|
|
1022
1160
|
}
|
|
1161
|
+
async clear(prefix = "") {
|
|
1162
|
+
for await (const key of this.scanKeys(prefix)) await this.kv.delete(key);
|
|
1163
|
+
}
|
|
1023
1164
|
async incrementTagVersion(tag) {
|
|
1024
|
-
const key =
|
|
1165
|
+
const key = tag;
|
|
1025
1166
|
const raw = await this.kv.get(key);
|
|
1026
1167
|
const prev = raw ? parseInt(raw, 10) || 0 : 0;
|
|
1027
1168
|
const nextVer = Math.max(prev + 1, Date.now());
|
|
@@ -1029,7 +1170,7 @@ var CloudflareKVAdapter = class {
|
|
|
1029
1170
|
return nextVer;
|
|
1030
1171
|
}
|
|
1031
1172
|
async getTagVersion(tag) {
|
|
1032
|
-
const raw = await this.kv.get(
|
|
1173
|
+
const raw = await this.kv.get(tag);
|
|
1033
1174
|
return raw ? parseInt(raw, 10) || 1 : 1;
|
|
1034
1175
|
}
|
|
1035
1176
|
async batchGetTagVersions(tags) {
|
|
@@ -1044,6 +1185,7 @@ var CloudflareKVAdapter = class {
|
|
|
1044
1185
|
|
|
1045
1186
|
// src/edge/adapters/cloudflare-do.ts
|
|
1046
1187
|
var CloudflareDOStorageAdapter = class {
|
|
1188
|
+
tagConsistency = "atomic";
|
|
1047
1189
|
storage;
|
|
1048
1190
|
constructor(storage) {
|
|
1049
1191
|
if (!storage) {
|
|
@@ -1103,16 +1245,33 @@ var CloudflareDOStorageAdapter = class {
|
|
|
1103
1245
|
}
|
|
1104
1246
|
await this.storage.put(record);
|
|
1105
1247
|
}
|
|
1106
|
-
async
|
|
1107
|
-
if (!this.storage.list)
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1248
|
+
async *scanKeys(prefix) {
|
|
1249
|
+
if (!this.storage.list) throw new Error("Durable Object storage does not support list");
|
|
1250
|
+
let startAfter;
|
|
1251
|
+
while (true) {
|
|
1252
|
+
const page = await this.storage.list({ prefix, startAfter, limit: 256 });
|
|
1253
|
+
if (page.size === 0) return;
|
|
1254
|
+
const keys = [...page.keys()];
|
|
1255
|
+
for (const key of keys) if (key.startsWith(prefix)) yield key;
|
|
1256
|
+
const next = keys[keys.length - 1];
|
|
1257
|
+
if (next === startAfter) throw new Error("Durable Object list did not advance");
|
|
1258
|
+
startAfter = next;
|
|
1259
|
+
if (page.size < 256) return;
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
async clear(prefix = "") {
|
|
1263
|
+
let keys = [];
|
|
1264
|
+
for await (const key of this.scanKeys(prefix)) {
|
|
1265
|
+
keys.push(key);
|
|
1266
|
+
if (keys.length === 256) {
|
|
1267
|
+
await this.storage.delete(keys);
|
|
1268
|
+
keys = [];
|
|
1269
|
+
}
|
|
1112
1270
|
}
|
|
1271
|
+
if (keys.length) await this.storage.delete(keys);
|
|
1113
1272
|
}
|
|
1114
1273
|
async incrementTagVersion(tag) {
|
|
1115
|
-
const key =
|
|
1274
|
+
const key = tag;
|
|
1116
1275
|
const raw = await this.storage.get(key);
|
|
1117
1276
|
const prev = raw && typeof raw === "object" && "val" in raw ? parseInt(raw.val, 10) || 0 : typeof raw === "string" ? parseInt(raw, 10) || 0 : 0;
|
|
1118
1277
|
const nextVer = prev + 1;
|
|
@@ -1120,18 +1279,18 @@ var CloudflareDOStorageAdapter = class {
|
|
|
1120
1279
|
return nextVer;
|
|
1121
1280
|
}
|
|
1122
1281
|
async getTagVersion(tag) {
|
|
1123
|
-
const raw = await this.storage.get(
|
|
1282
|
+
const raw = await this.storage.get(tag);
|
|
1124
1283
|
if (raw && typeof raw === "object" && "val" in raw) {
|
|
1125
1284
|
return parseInt(raw.val, 10) || 1;
|
|
1126
1285
|
}
|
|
1127
1286
|
return typeof raw === "string" ? parseInt(raw, 10) || 1 : 1;
|
|
1128
1287
|
}
|
|
1129
1288
|
async batchGetTagVersions(tags) {
|
|
1130
|
-
const keys = tags
|
|
1289
|
+
const keys = tags;
|
|
1131
1290
|
const map = await this.storage.get(keys);
|
|
1132
1291
|
const result = {};
|
|
1133
1292
|
for (const tag of tags) {
|
|
1134
|
-
const entry = map.get(
|
|
1293
|
+
const entry = map.get(tag);
|
|
1135
1294
|
if (entry && typeof entry === "object" && "val" in entry) {
|
|
1136
1295
|
result[tag] = parseInt(entry.val, 10) || 1;
|
|
1137
1296
|
} else {
|