tricache 1.0.2 → 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 +78 -90
- 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-CWI4jkS7.d.cts → cache-service-BV5T8-20.d.cts} +122 -21
- package/dist/cache-service-UZXOQGEY.js +14 -0
- package/dist/{cache-service-B108FzER.d.ts → cache-service-tpyrXvlH.d.ts} +122 -21
- package/dist/{chunk-UBWLPKTI.js → chunk-EJXG7WO6.js} +152 -32
- package/dist/{chunk-H4GFLQ26.js → chunk-KV7KISK2.js} +47 -1
- package/dist/{chunk-WYF7NOAT.js → chunk-MB3DMVWN.js} +2111 -543
- package/dist/{chunk-OYDU3SMI.js → chunk-RBJRTGFK.js} +5 -0
- package/dist/chunk-S5N65I45.js +17 -0
- package/dist/cli.cjs +2454 -572
- package/dist/cli.js +186 -11
- package/dist/dashboard/index.d.cts +2 -2
- package/dist/dashboard/index.d.ts +2 -2
- package/dist/drizzle/index.cjs +2335 -571
- package/dist/drizzle/index.d.cts +2 -2
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +6 -2
- package/dist/edge/index.cjs +145 -43
- package/dist/edge/index.d.cts +11 -4
- package/dist/edge/index.d.ts +11 -4
- package/dist/edge/index.js +118 -43
- package/dist/hono/index.cjs +2322 -569
- package/dist/hono/index.d.cts +2 -2
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/{hono-C886ok3X.d.cts → hono-CMv2VdQ6.d.cts} +18 -5
- package/dist/{hono-3rDMYVtY.d.ts → hono-Dvq04kX9.d.ts} +18 -5
- package/dist/http/index.cjs +2323 -570
- package/dist/http/index.d.cts +3 -3
- package/dist/http/index.d.ts +3 -3
- package/dist/http/index.js +4 -4
- package/dist/index.cjs +2554 -620
- package/dist/index.d.cts +88 -23
- package/dist/index.d.ts +88 -23
- package/dist/index.js +873 -16
- package/dist/{murmur3-CMGw9PbV.d.cts → murmur3-DF8BFRnu.d.cts} +1 -0
- package/dist/{murmur3-CMGw9PbV.d.ts → murmur3-DF8BFRnu.d.ts} +1 -0
- package/dist/nestjs/index.cjs +2292 -574
- package/dist/nestjs/index.d.cts +19 -5
- package/dist/nestjs/index.d.ts +19 -5
- package/dist/nestjs/index.js +25 -19
- package/dist/next/index.cjs +2306 -571
- package/dist/next/index.d.cts +8 -2
- package/dist/next/index.d.ts +8 -2
- package/dist/next/index.js +36 -9
- package/dist/prisma/index.cjs +26 -25
- package/dist/prisma/index.d.cts +2 -2
- package/dist/prisma/index.d.ts +2 -2
- package/dist/prisma/index.js +14 -25
- package/dist/serialize-worker.cjs +162 -39
- package/dist/serialize-worker.js +9 -7
- package/dist/{types-BA4pz1A0.d.cts → types-F8vRWMrE.d.cts} +162 -45
- package/dist/{types-BA4pz1A0.d.ts → types-F8vRWMrE.d.ts} +162 -45
- package/package.json +21 -2
- package/src/adapters/auto-pipeliner.ts +52 -11
- package/src/cache-service.ts +1537 -415
- package/src/cli.ts +2 -9
- package/src/codec.ts +3 -3
- package/src/compression.ts +39 -16
- package/src/cross-region.ts +1 -1
- package/src/disk-tier.ts +283 -43
- 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 +59 -17
- package/src/edge/types.ts +15 -5
- package/src/encryption.ts +50 -18
- package/src/env.d.ts +1 -1
- package/src/errors.ts +12 -0
- package/src/http/utils.ts +6 -0
- package/src/index.ts +6 -0
- package/src/latency-watchdog.ts +5 -1
- package/src/nestjs/decorators.ts +11 -6
- package/src/nestjs/tricache.module.ts +2 -1
- package/src/nestjs/tricache.store.ts +21 -9
- package/src/nestjs/types.ts +1 -1
- package/src/next/cache-handler.ts +24 -4
- package/src/next/types.ts +4 -0
- package/src/origin-capacity.ts +134 -0
- package/src/prisma/index.ts +14 -32
- 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 +62 -22
- package/src/types.ts +92 -35
- package/src/utils/config-comparison.ts +29 -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/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 +16 -1
- package/src/worker-pool.ts +28 -8
- package/src/wtiny-lfu.ts +62 -14
- package/dist/cache-service-GYPZFB7O.js +0 -12
- package/dist/chunk-VNRDL5RA.js +0 -666
package/dist/drizzle/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as CacheService } from '../cache-service-
|
|
2
|
-
import { W as WrapOptions } from '../types-
|
|
1
|
+
import { C as CacheService } from '../cache-service-BV5T8-20.cjs';
|
|
2
|
+
import { W as WrapOptions } from '../types-F8vRWMrE.cjs';
|
|
3
3
|
import 'crypto';
|
|
4
4
|
|
|
5
5
|
/**
|
package/dist/drizzle/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as CacheService } from '../cache-service-
|
|
2
|
-
import { W as WrapOptions } from '../types-
|
|
1
|
+
import { C as CacheService } from '../cache-service-tpyrXvlH.js';
|
|
2
|
+
import { W as WrapOptions } from '../types-F8vRWMrE.js';
|
|
3
3
|
import 'crypto';
|
|
4
4
|
|
|
5
5
|
/**
|
package/dist/drizzle/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deterministicStringify
|
|
3
|
+
} from "../chunk-S5N65I45.js";
|
|
4
|
+
|
|
1
5
|
// src/drizzle/index.ts
|
|
2
6
|
import crypto from "crypto";
|
|
3
7
|
function generateDrizzleCacheKey(sqlInfo) {
|
|
4
|
-
const normalized =
|
|
8
|
+
const normalized = deterministicStringify({ sql: sqlInfo.sql, params: sqlInfo.params });
|
|
5
9
|
const hash = crypto.createHash("sha256").update(normalized, "utf8").digest("hex").slice(0, 32);
|
|
6
10
|
return `drizzle:${hash}`;
|
|
7
11
|
}
|
|
@@ -9,7 +13,7 @@ async function withCache(query, options = {}) {
|
|
|
9
13
|
const { cache, key, ttl = 300, swr, priority, tags, dependsOn, refreshAhead, xfetchBeta, notFoundTtl } = options;
|
|
10
14
|
let activeCache = cache;
|
|
11
15
|
if (!activeCache) {
|
|
12
|
-
const { CacheService } = await import("../cache-service-
|
|
16
|
+
const { CacheService } = await import("../cache-service-UZXOQGEY.js");
|
|
13
17
|
activeCache = CacheService.create();
|
|
14
18
|
}
|
|
15
19
|
let cacheKey = key;
|
package/dist/edge/index.cjs
CHANGED
|
@@ -220,6 +220,7 @@ var WasmBloomFilter = class {
|
|
|
220
220
|
exports;
|
|
221
221
|
mem;
|
|
222
222
|
stagingTarget;
|
|
223
|
+
stagingView;
|
|
223
224
|
encoder = new TextEncoder();
|
|
224
225
|
numBits = 1e5;
|
|
225
226
|
/** Pre-computed — k and numBits are fixed constants; no need to recalculate on every probe. */
|
|
@@ -231,12 +232,24 @@ var WasmBloomFilter = class {
|
|
|
231
232
|
this.exports = instance.exports;
|
|
232
233
|
this.mem = new Uint8Array(this.exports.memory.buffer);
|
|
233
234
|
this.stagingTarget = new Uint8Array(this.exports.memory.buffer, KEY_STAGING_OFFSET, MAX_KEY_BYTES);
|
|
235
|
+
this.stagingView = new DataView(this.exports.memory.buffer, KEY_STAGING_OFFSET, MAX_KEY_BYTES);
|
|
234
236
|
const k = 7, p = 0.01;
|
|
235
237
|
this._maxCapacity = Math.floor(-this.numBits * Math.log(1 - Math.pow(p, 1 / k)) / k);
|
|
236
238
|
}
|
|
237
239
|
writeKey(key) {
|
|
238
240
|
if (key.length === 0) return 0;
|
|
239
|
-
|
|
241
|
+
const encoded = this.encoder.encodeInto(key, this.stagingTarget);
|
|
242
|
+
if (encoded.read === key.length) return encoded.written;
|
|
243
|
+
let first = 2166136261, second = 5381;
|
|
244
|
+
for (let index = 0; index < key.length; index++) {
|
|
245
|
+
const unit = key.charCodeAt(index);
|
|
246
|
+
first = Math.imul(first ^ unit, 16777619) >>> 0;
|
|
247
|
+
second = (Math.imul(second, 33) ^ unit) >>> 0;
|
|
248
|
+
}
|
|
249
|
+
this.stagingTarget[0] = 255;
|
|
250
|
+
this.stagingView.setUint32(1, first, true);
|
|
251
|
+
this.stagingView.setUint32(5, second, true);
|
|
252
|
+
return 9;
|
|
240
253
|
}
|
|
241
254
|
add(key) {
|
|
242
255
|
const len = this.writeKey(key);
|
|
@@ -440,6 +453,22 @@ function isChildTag(parentTag, candidateTag, delimiter = ":") {
|
|
|
440
453
|
return candidateTag.startsWith(prefix);
|
|
441
454
|
}
|
|
442
455
|
|
|
456
|
+
// src/utils/namespace-key.ts
|
|
457
|
+
var NAMESPACE_KEY_VERSION = "tricache:v3:";
|
|
458
|
+
function encodeNamespaceSegment(namespace) {
|
|
459
|
+
let encoded = "";
|
|
460
|
+
for (let index = 0; index < namespace.length; index++) {
|
|
461
|
+
encoded += namespace.charCodeAt(index).toString(16).padStart(4, "0");
|
|
462
|
+
}
|
|
463
|
+
return encoded;
|
|
464
|
+
}
|
|
465
|
+
function namespaceKeyPrefix(namespace) {
|
|
466
|
+
return `${NAMESPACE_KEY_VERSION}${encodeNamespaceSegment(namespace)}:`;
|
|
467
|
+
}
|
|
468
|
+
function namespaceDomainPrefix(namespace, domain) {
|
|
469
|
+
return `${namespaceKeyPrefix(namespace)}${domain}:`;
|
|
470
|
+
}
|
|
471
|
+
|
|
443
472
|
// src/edge/cache.ts
|
|
444
473
|
var NAMESPACE_TAG = "_tricache_namespace_";
|
|
445
474
|
var EdgeCacheService = class _EdgeCacheService {
|
|
@@ -447,6 +476,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
447
476
|
maxBytes;
|
|
448
477
|
defaultTtl;
|
|
449
478
|
namespace;
|
|
479
|
+
namespacePrefix;
|
|
480
|
+
dataPrefix;
|
|
481
|
+
tagVersionPrefix;
|
|
450
482
|
remoteStorage;
|
|
451
483
|
enc;
|
|
452
484
|
tracer;
|
|
@@ -471,6 +503,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
471
503
|
this.maxBytes = options?.maxBytes ?? 32 * 1024 * 1024;
|
|
472
504
|
this.defaultTtl = options?.defaultTtlSeconds ?? 300;
|
|
473
505
|
this.namespace = options?.namespace?.trim() ?? "";
|
|
506
|
+
this.namespacePrefix = namespaceKeyPrefix(this.namespace);
|
|
507
|
+
this.dataPrefix = namespaceDomainPrefix(this.namespace, "data");
|
|
508
|
+
this.tagVersionPrefix = namespaceDomainPrefix(this.namespace, "tag-version");
|
|
474
509
|
this.remoteStorage = options?.remoteStorage ?? null;
|
|
475
510
|
this.tracer = options?.tracer ?? null;
|
|
476
511
|
this.enc = new WebCryptoEncryption(options?.encryption);
|
|
@@ -495,7 +530,10 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
495
530
|
return this.bloomFilter;
|
|
496
531
|
}
|
|
497
532
|
nk(key) {
|
|
498
|
-
return
|
|
533
|
+
return `${this.dataPrefix}${key}`;
|
|
534
|
+
}
|
|
535
|
+
tagVersionKey(tag) {
|
|
536
|
+
return `${this.tagVersionPrefix}${tag}`;
|
|
499
537
|
}
|
|
500
538
|
_estimateBytes(key, serialized) {
|
|
501
539
|
return (key.length + serialized.length) * 2 + 64;
|
|
@@ -547,6 +585,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
547
585
|
};
|
|
548
586
|
this.storeL1(namespaced, entry);
|
|
549
587
|
if (this.remoteStorage) {
|
|
588
|
+
const registry = namespaceDomainPrefix(this.namespace, "tag-registry");
|
|
589
|
+
for (const tag of entry.tags ?? []) await this.remoteStorage.set(`${registry}${tag}`, "1");
|
|
550
590
|
const envelope = JSON.stringify({
|
|
551
591
|
__t_edge: 2,
|
|
552
592
|
__t_val: value,
|
|
@@ -634,9 +674,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
634
674
|
if (this.remoteStorage) {
|
|
635
675
|
try {
|
|
636
676
|
if (typeof this.remoteStorage.getTagVersion === "function") {
|
|
637
|
-
version = await this.remoteStorage.getTagVersion(this.
|
|
677
|
+
version = await this.remoteStorage.getTagVersion(this.tagVersionKey(tag));
|
|
638
678
|
} else {
|
|
639
|
-
const raw = await this.remoteStorage.get(this.
|
|
679
|
+
const raw = await this.remoteStorage.get(this.tagVersionKey(tag));
|
|
640
680
|
version = raw ? parseInt(raw, 10) || 1 : 1;
|
|
641
681
|
}
|
|
642
682
|
} catch {
|
|
@@ -663,11 +703,11 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
663
703
|
if (missingTags.length > 0 && this.remoteStorage) {
|
|
664
704
|
try {
|
|
665
705
|
if (typeof this.remoteStorage.batchGetTagVersions === "function") {
|
|
666
|
-
const nsTags = missingTags.map((t) => this.
|
|
706
|
+
const nsTags = missingTags.map((t) => this.tagVersionKey(t));
|
|
667
707
|
const remoteVersions = await this.remoteStorage.batchGetTagVersions(nsTags);
|
|
668
708
|
for (let i = 0; i < missingTags.length; i++) {
|
|
669
709
|
const t = missingTags[i];
|
|
670
|
-
const v = remoteVersions[this.
|
|
710
|
+
const v = remoteVersions[this.tagVersionKey(t)] ?? 1;
|
|
671
711
|
result[t] = Math.max(this.tagVersionCache.get(t)?.version ?? 0, v);
|
|
672
712
|
this._setTagVersionCache(t, v, now + this.tagVersionTtlMs);
|
|
673
713
|
}
|
|
@@ -916,11 +956,11 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
916
956
|
if (this.remoteStorage) {
|
|
917
957
|
try {
|
|
918
958
|
if (typeof this.remoteStorage.incrementTagVersion === "function") {
|
|
919
|
-
newVer = await this.remoteStorage.incrementTagVersion(this.
|
|
920
|
-
if (newVer <= previous) newVer = await this.remoteStorage.incrementTagVersion(this.
|
|
959
|
+
newVer = await this.remoteStorage.incrementTagVersion(this.tagVersionKey(tag));
|
|
960
|
+
if (newVer <= previous) newVer = await this.remoteStorage.incrementTagVersion(this.tagVersionKey(tag));
|
|
921
961
|
newVer = Math.max(previous + 1, newVer);
|
|
922
962
|
} else {
|
|
923
|
-
const key = this.
|
|
963
|
+
const key = this.tagVersionKey(tag);
|
|
924
964
|
const current = await this._getTagVersion(tag);
|
|
925
965
|
newVer = Math.max(current + 1, Date.now());
|
|
926
966
|
await this.remoteStorage.set(key, String(newVer));
|
|
@@ -954,6 +994,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
954
994
|
const isPattern = isWildcardTag(tag);
|
|
955
995
|
const shouldCascade = Boolean(opts?.cascade);
|
|
956
996
|
if (isPattern || shouldCascade) {
|
|
997
|
+
if (this.remoteStorage && !this.remoteStorage.scanKeys) {
|
|
998
|
+
throw new Error("Remote wildcard/cascade tag invalidation requires an adapter with scanKeys");
|
|
999
|
+
}
|
|
957
1000
|
const matched = /* @__PURE__ */ new Set();
|
|
958
1001
|
const tagMatches = (t) => {
|
|
959
1002
|
if (t === tag) return true;
|
|
@@ -967,6 +1010,14 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
967
1010
|
for (const t of this.tagVersionCache.keys()) {
|
|
968
1011
|
if (tagMatches(t)) matched.add(t);
|
|
969
1012
|
}
|
|
1013
|
+
if (this.remoteStorage?.scanKeys) {
|
|
1014
|
+
const registry = namespaceDomainPrefix(this.namespace, "tag-registry");
|
|
1015
|
+
for await (const key of this.remoteStorage.scanKeys(registry)) {
|
|
1016
|
+
if (!key.startsWith(registry)) continue;
|
|
1017
|
+
const concrete = key.slice(registry.length);
|
|
1018
|
+
if (tagMatches(concrete)) matched.add(concrete);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
970
1021
|
if (shouldCascade && !isPattern) matched.add(tag);
|
|
971
1022
|
for (const t of matched) {
|
|
972
1023
|
await this._invalidateConcreteTag(t);
|
|
@@ -998,7 +1049,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
998
1049
|
this.bloomFilter.reset();
|
|
999
1050
|
}
|
|
1000
1051
|
if (this.remoteStorage) {
|
|
1001
|
-
await this.remoteStorage.clear?.(this.
|
|
1052
|
+
await this.remoteStorage.clear?.(this.dataPrefix);
|
|
1002
1053
|
}
|
|
1003
1054
|
this._setTagVersionCache(NAMESPACE_TAG, previousNamespace, Date.now() + this.tagVersionTtlMs);
|
|
1004
1055
|
await this._invalidateConcreteTag(NAMESPACE_TAG);
|
|
@@ -1062,7 +1113,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
1062
1113
|
const entries = [];
|
|
1063
1114
|
for (const [nsKey, entry] of this.l1.entries()) {
|
|
1064
1115
|
if (entry.expiresAt > now || entry.staleUntil > now) {
|
|
1065
|
-
|
|
1116
|
+
if (!nsKey.startsWith(this.dataPrefix)) continue;
|
|
1117
|
+
const cleanKey = nsKey.slice(this.dataPrefix.length);
|
|
1066
1118
|
entries.push({
|
|
1067
1119
|
key: cleanKey,
|
|
1068
1120
|
value: entry.value,
|
|
@@ -1074,7 +1126,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
1074
1126
|
}
|
|
1075
1127
|
}
|
|
1076
1128
|
const payload = {
|
|
1077
|
-
version:
|
|
1129
|
+
version: 3,
|
|
1130
|
+
namespace: this.namespacePrefix,
|
|
1078
1131
|
writtenAt: now,
|
|
1079
1132
|
entries
|
|
1080
1133
|
};
|
|
@@ -1141,7 +1194,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
1141
1194
|
} catch {
|
|
1142
1195
|
return 0;
|
|
1143
1196
|
}
|
|
1144
|
-
if (!payload || payload.version !==
|
|
1197
|
+
if (!payload || payload.version !== 3 || payload.namespace !== this.namespacePrefix || !Array.isArray(payload.entries) || this.globalEpoch !== epoch) {
|
|
1145
1198
|
return 0;
|
|
1146
1199
|
}
|
|
1147
1200
|
const now = Date.now();
|
|
@@ -1159,10 +1212,21 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
1159
1212
|
let imported = 0;
|
|
1160
1213
|
for (const item of payload.entries) {
|
|
1161
1214
|
if (!item || typeof item.key !== "string") continue;
|
|
1215
|
+
if (!Number.isFinite(item.expiresAt) || !Number.isFinite(item.staleUntil)) continue;
|
|
1162
1216
|
if (item.expiresAt <= now && item.staleUntil <= now) continue;
|
|
1217
|
+
if (item.value === void 0) continue;
|
|
1218
|
+
if (item.tags !== void 0 && (!Array.isArray(item.tags) || item.tags.some((tag) => typeof tag !== "string"))) continue;
|
|
1219
|
+
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;
|
|
1220
|
+
let serialized;
|
|
1221
|
+
try {
|
|
1222
|
+
const encoded = JSON.stringify(item.value);
|
|
1223
|
+
if (typeof encoded !== "string") continue;
|
|
1224
|
+
serialized = encoded;
|
|
1225
|
+
} catch {
|
|
1226
|
+
continue;
|
|
1227
|
+
}
|
|
1163
1228
|
const nsKey = this.nk(item.key);
|
|
1164
|
-
if (this.l1.has(nsKey) || this.keyEpochs.has(nsKey)
|
|
1165
|
-
const serialized = JSON.stringify(item.value);
|
|
1229
|
+
if (this.l1.has(nsKey) || this.keyEpochs.has(nsKey)) continue;
|
|
1166
1230
|
const bytes = this._estimateBytes(nsKey, serialized);
|
|
1167
1231
|
if (bytes > this.maxBytes || this.maxKeys === 0) continue;
|
|
1168
1232
|
this._deleteL1(nsKey);
|
|
@@ -1211,9 +1275,11 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
1211
1275
|
|
|
1212
1276
|
// src/edge/adapters/upstash.ts
|
|
1213
1277
|
var UpstashRedisAdapter = class {
|
|
1278
|
+
tagConsistency = "atomic";
|
|
1214
1279
|
baseUrl;
|
|
1215
1280
|
token;
|
|
1216
1281
|
customFetch;
|
|
1282
|
+
dangerouslyFlushDatabase;
|
|
1217
1283
|
constructor(options) {
|
|
1218
1284
|
if (!options?.url || !options?.token) {
|
|
1219
1285
|
throw new Error("UpstashRedisAdapter: url and token are required.");
|
|
@@ -1221,6 +1287,7 @@ var UpstashRedisAdapter = class {
|
|
|
1221
1287
|
this.baseUrl = options.url.replace(/\/+$/, "");
|
|
1222
1288
|
this.token = options.token;
|
|
1223
1289
|
this.customFetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
1290
|
+
this.dangerouslyFlushDatabase = options.dangerouslyFlushDatabase ?? false;
|
|
1224
1291
|
}
|
|
1225
1292
|
async _executeCommand(command) {
|
|
1226
1293
|
const res = await this.customFetch(this.baseUrl, {
|
|
@@ -1296,29 +1363,43 @@ var UpstashRedisAdapter = class {
|
|
|
1296
1363
|
await this._executeCommand(["MSET", ...args]);
|
|
1297
1364
|
}
|
|
1298
1365
|
}
|
|
1366
|
+
async *scanKeys(prefix) {
|
|
1367
|
+
const pattern = prefix.replaceAll("\\", "\\\\").replaceAll("*", "\\*").replaceAll("?", "\\?").replaceAll("[", "\\[").replaceAll("]", "\\]") + "*";
|
|
1368
|
+
let cursor = "0";
|
|
1369
|
+
do {
|
|
1370
|
+
const result = await this._executeCommand(["SCAN", cursor, "MATCH", pattern, "COUNT", 256]);
|
|
1371
|
+
if (!Array.isArray(result) || !Array.isArray(result[1])) throw new Error("Invalid Upstash SCAN response");
|
|
1372
|
+
cursor = String(result[0]);
|
|
1373
|
+
for (const key of result[1]) if (key.startsWith(prefix)) yield key;
|
|
1374
|
+
} while (cursor !== "0");
|
|
1375
|
+
}
|
|
1299
1376
|
async clear(prefix) {
|
|
1300
|
-
if (prefix) {
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1377
|
+
if (!prefix) {
|
|
1378
|
+
if (!this.dangerouslyFlushDatabase) throw new Error("Unscoped Upstash clear requires dangerouslyFlushDatabase: true");
|
|
1379
|
+
await this._executeCommand(["FLUSHDB"]);
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
let keys = [];
|
|
1383
|
+
for await (const key of this.scanKeys(prefix)) {
|
|
1384
|
+
keys.push(key);
|
|
1385
|
+
if (keys.length === 256) {
|
|
1304
1386
|
await this._executeCommand(["DEL", ...keys]);
|
|
1387
|
+
keys = [];
|
|
1305
1388
|
}
|
|
1306
|
-
} else {
|
|
1307
|
-
await this._executeCommand(["FLUSHDB"]);
|
|
1308
1389
|
}
|
|
1390
|
+
if (keys.length) await this._executeCommand(["DEL", ...keys]);
|
|
1309
1391
|
}
|
|
1310
1392
|
async incrementTagVersion(tag) {
|
|
1311
|
-
const res = await this._executeCommand(["INCR",
|
|
1393
|
+
const res = await this._executeCommand(["INCR", tag]);
|
|
1312
1394
|
return typeof res === "number" ? res : 1;
|
|
1313
1395
|
}
|
|
1314
1396
|
async getTagVersion(tag) {
|
|
1315
|
-
const res = await this._executeCommand(["GET",
|
|
1397
|
+
const res = await this._executeCommand(["GET", tag]);
|
|
1316
1398
|
return res ? parseInt(res, 10) || 1 : 1;
|
|
1317
1399
|
}
|
|
1318
1400
|
async batchGetTagVersions(tags) {
|
|
1319
1401
|
if (tags.length === 0) return {};
|
|
1320
|
-
const
|
|
1321
|
-
const res = await this.mget(keys);
|
|
1402
|
+
const res = await this.mget(tags);
|
|
1322
1403
|
const result = {};
|
|
1323
1404
|
for (let i = 0; i < tags.length; i++) {
|
|
1324
1405
|
const raw = res[i];
|
|
@@ -1331,6 +1412,7 @@ var UpstashRedisAdapter = class {
|
|
|
1331
1412
|
// src/edge/adapters/cloudflare-kv.ts
|
|
1332
1413
|
var MIN_CF_KV_TTL = 60;
|
|
1333
1414
|
var CloudflareKVAdapter = class {
|
|
1415
|
+
tagConsistency = "eventual";
|
|
1334
1416
|
kv;
|
|
1335
1417
|
constructor(kv) {
|
|
1336
1418
|
if (!kv) {
|
|
@@ -1382,19 +1464,21 @@ var CloudflareKVAdapter = class {
|
|
|
1382
1464
|
const keys = Object.keys(entries);
|
|
1383
1465
|
await Promise.all(keys.map((k) => this.set(k, entries[k], ttlSeconds)));
|
|
1384
1466
|
}
|
|
1385
|
-
async
|
|
1386
|
-
if (!this.kv.list)
|
|
1467
|
+
async *scanKeys(prefix) {
|
|
1468
|
+
if (!this.kv.list) throw new Error("Workers KV binding does not support list");
|
|
1387
1469
|
let cursor;
|
|
1388
1470
|
do {
|
|
1389
|
-
const res = await this.kv.list({ prefix, cursor });
|
|
1390
|
-
|
|
1391
|
-
await Promise.all(res.keys.map((item) => this.kv.delete(item.name)));
|
|
1392
|
-
}
|
|
1471
|
+
const res = await this.kv.list({ prefix, cursor, limit: 256 });
|
|
1472
|
+
for (const item of res.keys) if (item.name.startsWith(prefix)) yield item.name;
|
|
1393
1473
|
cursor = res.list_complete ? void 0 : res.cursor;
|
|
1474
|
+
if (!res.list_complete && !cursor) throw new Error("Workers KV list omitted its continuation cursor");
|
|
1394
1475
|
} while (cursor);
|
|
1395
1476
|
}
|
|
1477
|
+
async clear(prefix = "") {
|
|
1478
|
+
for await (const key of this.scanKeys(prefix)) await this.kv.delete(key);
|
|
1479
|
+
}
|
|
1396
1480
|
async incrementTagVersion(tag) {
|
|
1397
|
-
const key =
|
|
1481
|
+
const key = tag;
|
|
1398
1482
|
const raw = await this.kv.get(key);
|
|
1399
1483
|
const prev = raw ? parseInt(raw, 10) || 0 : 0;
|
|
1400
1484
|
const nextVer = Math.max(prev + 1, Date.now());
|
|
@@ -1402,7 +1486,7 @@ var CloudflareKVAdapter = class {
|
|
|
1402
1486
|
return nextVer;
|
|
1403
1487
|
}
|
|
1404
1488
|
async getTagVersion(tag) {
|
|
1405
|
-
const raw = await this.kv.get(
|
|
1489
|
+
const raw = await this.kv.get(tag);
|
|
1406
1490
|
return raw ? parseInt(raw, 10) || 1 : 1;
|
|
1407
1491
|
}
|
|
1408
1492
|
async batchGetTagVersions(tags) {
|
|
@@ -1417,6 +1501,7 @@ var CloudflareKVAdapter = class {
|
|
|
1417
1501
|
|
|
1418
1502
|
// src/edge/adapters/cloudflare-do.ts
|
|
1419
1503
|
var CloudflareDOStorageAdapter = class {
|
|
1504
|
+
tagConsistency = "atomic";
|
|
1420
1505
|
storage;
|
|
1421
1506
|
constructor(storage) {
|
|
1422
1507
|
if (!storage) {
|
|
@@ -1476,16 +1561,33 @@ var CloudflareDOStorageAdapter = class {
|
|
|
1476
1561
|
}
|
|
1477
1562
|
await this.storage.put(record);
|
|
1478
1563
|
}
|
|
1479
|
-
async
|
|
1480
|
-
if (!this.storage.list)
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1564
|
+
async *scanKeys(prefix) {
|
|
1565
|
+
if (!this.storage.list) throw new Error("Durable Object storage does not support list");
|
|
1566
|
+
let startAfter;
|
|
1567
|
+
while (true) {
|
|
1568
|
+
const page = await this.storage.list({ prefix, startAfter, limit: 256 });
|
|
1569
|
+
if (page.size === 0) return;
|
|
1570
|
+
const keys = [...page.keys()];
|
|
1571
|
+
for (const key of keys) if (key.startsWith(prefix)) yield key;
|
|
1572
|
+
const next = keys[keys.length - 1];
|
|
1573
|
+
if (next === startAfter) throw new Error("Durable Object list did not advance");
|
|
1574
|
+
startAfter = next;
|
|
1575
|
+
if (page.size < 256) return;
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
async clear(prefix = "") {
|
|
1579
|
+
let keys = [];
|
|
1580
|
+
for await (const key of this.scanKeys(prefix)) {
|
|
1581
|
+
keys.push(key);
|
|
1582
|
+
if (keys.length === 256) {
|
|
1583
|
+
await this.storage.delete(keys);
|
|
1584
|
+
keys = [];
|
|
1585
|
+
}
|
|
1485
1586
|
}
|
|
1587
|
+
if (keys.length) await this.storage.delete(keys);
|
|
1486
1588
|
}
|
|
1487
1589
|
async incrementTagVersion(tag) {
|
|
1488
|
-
const key =
|
|
1590
|
+
const key = tag;
|
|
1489
1591
|
const raw = await this.storage.get(key);
|
|
1490
1592
|
const prev = raw && typeof raw === "object" && "val" in raw ? parseInt(raw.val, 10) || 0 : typeof raw === "string" ? parseInt(raw, 10) || 0 : 0;
|
|
1491
1593
|
const nextVer = prev + 1;
|
|
@@ -1493,18 +1595,18 @@ var CloudflareDOStorageAdapter = class {
|
|
|
1493
1595
|
return nextVer;
|
|
1494
1596
|
}
|
|
1495
1597
|
async getTagVersion(tag) {
|
|
1496
|
-
const raw = await this.storage.get(
|
|
1598
|
+
const raw = await this.storage.get(tag);
|
|
1497
1599
|
if (raw && typeof raw === "object" && "val" in raw) {
|
|
1498
1600
|
return parseInt(raw.val, 10) || 1;
|
|
1499
1601
|
}
|
|
1500
1602
|
return typeof raw === "string" ? parseInt(raw, 10) || 1 : 1;
|
|
1501
1603
|
}
|
|
1502
1604
|
async batchGetTagVersions(tags) {
|
|
1503
|
-
const keys = tags
|
|
1605
|
+
const keys = tags;
|
|
1504
1606
|
const map = await this.storage.get(keys);
|
|
1505
1607
|
const result = {};
|
|
1506
1608
|
for (const tag of tags) {
|
|
1507
|
-
const entry = map.get(
|
|
1609
|
+
const entry = map.get(tag);
|
|
1508
1610
|
if (entry && typeof entry === "object" && "val" in entry) {
|
|
1509
1611
|
result[tag] = parseInt(entry.val, 10) || 1;
|
|
1510
1612
|
} else {
|
package/dist/edge/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { I as IEdgeRemoteStorage, U as UpstashRedisOptions, a as CloudflareKVNamespace, d as CloudflareDOStorage } from '../hono-
|
|
2
|
-
export { C as CachedEdgeHttpResponse, e as CloudflareR2Bucket, f as CloudflareR2Object, E as EdgeCacheOptions, g as EdgeCacheService, i as EdgeExportSnapshotOptions, j as EdgeGetOptions, k as EdgeHydrateOptions, l as EdgeSnapshotPayload, m as EdgeSnapshotSource, H as HonoEdgeCacheOptions, n as IEdgeBloomFilter, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoEdgeCache } from '../hono-
|
|
3
|
-
export { B as BloomFilterStats, M as Murmur3BloomFilter, W as WasmBloomFilter, m as murmur3_32 } from '../murmur3-
|
|
4
|
-
import '../types-
|
|
1
|
+
import { I as IEdgeRemoteStorage, U as UpstashRedisOptions, a as CloudflareKVNamespace, d as CloudflareDOStorage } from '../hono-CMv2VdQ6.cjs';
|
|
2
|
+
export { C as CachedEdgeHttpResponse, e as CloudflareR2Bucket, f as CloudflareR2Object, E as EdgeCacheOptions, g as EdgeCacheService, i as EdgeExportSnapshotOptions, j as EdgeGetOptions, k as EdgeHydrateOptions, l as EdgeSnapshotPayload, m as EdgeSnapshotSource, H as HonoEdgeCacheOptions, n as IEdgeBloomFilter, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoEdgeCache } from '../hono-CMv2VdQ6.cjs';
|
|
3
|
+
export { B as BloomFilterStats, M as Murmur3BloomFilter, W as WasmBloomFilter, m as murmur3_32 } from '../murmur3-DF8BFRnu.cjs';
|
|
4
|
+
import '../types-F8vRWMrE.cjs';
|
|
5
5
|
import 'crypto';
|
|
6
6
|
|
|
7
7
|
type WebCryptoMode = 'aes-256-gcm' | 'aes-128-gcm';
|
|
@@ -45,9 +45,11 @@ declare class WebCryptoEncryption {
|
|
|
45
45
|
* Communicates over HTTPS REST with zero native socket dependencies.
|
|
46
46
|
*/
|
|
47
47
|
declare class UpstashRedisAdapter implements IEdgeRemoteStorage {
|
|
48
|
+
readonly tagConsistency: "atomic";
|
|
48
49
|
private readonly baseUrl;
|
|
49
50
|
private readonly token;
|
|
50
51
|
private readonly customFetch;
|
|
52
|
+
private readonly dangerouslyFlushDatabase;
|
|
51
53
|
constructor(options: UpstashRedisOptions);
|
|
52
54
|
private _executeCommand;
|
|
53
55
|
private _executePipeline;
|
|
@@ -56,6 +58,7 @@ declare class UpstashRedisAdapter implements IEdgeRemoteStorage {
|
|
|
56
58
|
delete(key: string): Promise<void>;
|
|
57
59
|
mget(keys: string[]): Promise<(string | null)[]>;
|
|
58
60
|
mset(entries: Record<string, string>, ttlSeconds?: number): Promise<void>;
|
|
61
|
+
scanKeys(prefix: string): AsyncIterable<string>;
|
|
59
62
|
clear(prefix?: string): Promise<void>;
|
|
60
63
|
incrementTagVersion(tag: string): Promise<number>;
|
|
61
64
|
getTagVersion(tag: string): Promise<number>;
|
|
@@ -69,6 +72,7 @@ declare class UpstashRedisAdapter implements IEdgeRemoteStorage {
|
|
|
69
72
|
* TTLs in a lightweight timestamp envelope and validating expiry on read.
|
|
70
73
|
*/
|
|
71
74
|
declare class CloudflareKVAdapter implements IEdgeRemoteStorage {
|
|
75
|
+
readonly tagConsistency: "eventual";
|
|
72
76
|
private readonly kv;
|
|
73
77
|
constructor(kv: CloudflareKVNamespace);
|
|
74
78
|
get(key: string): Promise<string | null>;
|
|
@@ -76,6 +80,7 @@ declare class CloudflareKVAdapter implements IEdgeRemoteStorage {
|
|
|
76
80
|
delete(key: string): Promise<void>;
|
|
77
81
|
mget(keys: string[]): Promise<(string | null)[]>;
|
|
78
82
|
mset(entries: Record<string, string>, ttlSeconds?: number): Promise<void>;
|
|
83
|
+
scanKeys(prefix: string): AsyncIterable<string>;
|
|
79
84
|
clear(prefix?: string): Promise<void>;
|
|
80
85
|
incrementTagVersion(tag: string): Promise<number>;
|
|
81
86
|
getTagVersion(tag: string): Promise<number>;
|
|
@@ -87,6 +92,7 @@ declare class CloudflareKVAdapter implements IEdgeRemoteStorage {
|
|
|
87
92
|
* Enables strongly-consistent edge caching and coordination across edge isolates.
|
|
88
93
|
*/
|
|
89
94
|
declare class CloudflareDOStorageAdapter implements IEdgeRemoteStorage {
|
|
95
|
+
readonly tagConsistency: "atomic";
|
|
90
96
|
private readonly storage;
|
|
91
97
|
constructor(storage: CloudflareDOStorage);
|
|
92
98
|
get(key: string): Promise<string | null>;
|
|
@@ -94,6 +100,7 @@ declare class CloudflareDOStorageAdapter implements IEdgeRemoteStorage {
|
|
|
94
100
|
delete(key: string): Promise<void>;
|
|
95
101
|
mget(keys: string[]): Promise<(string | null)[]>;
|
|
96
102
|
mset(entries: Record<string, string>, ttlSeconds?: number): Promise<void>;
|
|
103
|
+
scanKeys(prefix: string): AsyncIterable<string>;
|
|
97
104
|
clear(prefix?: string): Promise<void>;
|
|
98
105
|
incrementTagVersion(tag: string): Promise<number>;
|
|
99
106
|
getTagVersion(tag: string): Promise<number>;
|
package/dist/edge/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { I as IEdgeRemoteStorage, U as UpstashRedisOptions, a as CloudflareKVNamespace, d as CloudflareDOStorage } from '../hono-
|
|
2
|
-
export { C as CachedEdgeHttpResponse, e as CloudflareR2Bucket, f as CloudflareR2Object, E as EdgeCacheOptions, g as EdgeCacheService, i as EdgeExportSnapshotOptions, j as EdgeGetOptions, k as EdgeHydrateOptions, l as EdgeSnapshotPayload, m as EdgeSnapshotSource, H as HonoEdgeCacheOptions, n as IEdgeBloomFilter, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoEdgeCache } from '../hono-
|
|
3
|
-
export { B as BloomFilterStats, M as Murmur3BloomFilter, W as WasmBloomFilter, m as murmur3_32 } from '../murmur3-
|
|
4
|
-
import '../types-
|
|
1
|
+
import { I as IEdgeRemoteStorage, U as UpstashRedisOptions, a as CloudflareKVNamespace, d as CloudflareDOStorage } from '../hono-Dvq04kX9.js';
|
|
2
|
+
export { C as CachedEdgeHttpResponse, e as CloudflareR2Bucket, f as CloudflareR2Object, E as EdgeCacheOptions, g as EdgeCacheService, i as EdgeExportSnapshotOptions, j as EdgeGetOptions, k as EdgeHydrateOptions, l as EdgeSnapshotPayload, m as EdgeSnapshotSource, H as HonoEdgeCacheOptions, n as IEdgeBloomFilter, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoEdgeCache } from '../hono-Dvq04kX9.js';
|
|
3
|
+
export { B as BloomFilterStats, M as Murmur3BloomFilter, W as WasmBloomFilter, m as murmur3_32 } from '../murmur3-DF8BFRnu.js';
|
|
4
|
+
import '../types-F8vRWMrE.js';
|
|
5
5
|
import 'crypto';
|
|
6
6
|
|
|
7
7
|
type WebCryptoMode = 'aes-256-gcm' | 'aes-128-gcm';
|
|
@@ -45,9 +45,11 @@ declare class WebCryptoEncryption {
|
|
|
45
45
|
* Communicates over HTTPS REST with zero native socket dependencies.
|
|
46
46
|
*/
|
|
47
47
|
declare class UpstashRedisAdapter implements IEdgeRemoteStorage {
|
|
48
|
+
readonly tagConsistency: "atomic";
|
|
48
49
|
private readonly baseUrl;
|
|
49
50
|
private readonly token;
|
|
50
51
|
private readonly customFetch;
|
|
52
|
+
private readonly dangerouslyFlushDatabase;
|
|
51
53
|
constructor(options: UpstashRedisOptions);
|
|
52
54
|
private _executeCommand;
|
|
53
55
|
private _executePipeline;
|
|
@@ -56,6 +58,7 @@ declare class UpstashRedisAdapter implements IEdgeRemoteStorage {
|
|
|
56
58
|
delete(key: string): Promise<void>;
|
|
57
59
|
mget(keys: string[]): Promise<(string | null)[]>;
|
|
58
60
|
mset(entries: Record<string, string>, ttlSeconds?: number): Promise<void>;
|
|
61
|
+
scanKeys(prefix: string): AsyncIterable<string>;
|
|
59
62
|
clear(prefix?: string): Promise<void>;
|
|
60
63
|
incrementTagVersion(tag: string): Promise<number>;
|
|
61
64
|
getTagVersion(tag: string): Promise<number>;
|
|
@@ -69,6 +72,7 @@ declare class UpstashRedisAdapter implements IEdgeRemoteStorage {
|
|
|
69
72
|
* TTLs in a lightweight timestamp envelope and validating expiry on read.
|
|
70
73
|
*/
|
|
71
74
|
declare class CloudflareKVAdapter implements IEdgeRemoteStorage {
|
|
75
|
+
readonly tagConsistency: "eventual";
|
|
72
76
|
private readonly kv;
|
|
73
77
|
constructor(kv: CloudflareKVNamespace);
|
|
74
78
|
get(key: string): Promise<string | null>;
|
|
@@ -76,6 +80,7 @@ declare class CloudflareKVAdapter implements IEdgeRemoteStorage {
|
|
|
76
80
|
delete(key: string): Promise<void>;
|
|
77
81
|
mget(keys: string[]): Promise<(string | null)[]>;
|
|
78
82
|
mset(entries: Record<string, string>, ttlSeconds?: number): Promise<void>;
|
|
83
|
+
scanKeys(prefix: string): AsyncIterable<string>;
|
|
79
84
|
clear(prefix?: string): Promise<void>;
|
|
80
85
|
incrementTagVersion(tag: string): Promise<number>;
|
|
81
86
|
getTagVersion(tag: string): Promise<number>;
|
|
@@ -87,6 +92,7 @@ declare class CloudflareKVAdapter implements IEdgeRemoteStorage {
|
|
|
87
92
|
* Enables strongly-consistent edge caching and coordination across edge isolates.
|
|
88
93
|
*/
|
|
89
94
|
declare class CloudflareDOStorageAdapter implements IEdgeRemoteStorage {
|
|
95
|
+
readonly tagConsistency: "atomic";
|
|
90
96
|
private readonly storage;
|
|
91
97
|
constructor(storage: CloudflareDOStorage);
|
|
92
98
|
get(key: string): Promise<string | null>;
|
|
@@ -94,6 +100,7 @@ declare class CloudflareDOStorageAdapter implements IEdgeRemoteStorage {
|
|
|
94
100
|
delete(key: string): Promise<void>;
|
|
95
101
|
mget(keys: string[]): Promise<(string | null)[]>;
|
|
96
102
|
mset(entries: Record<string, string>, ttlSeconds?: number): Promise<void>;
|
|
103
|
+
scanKeys(prefix: string): AsyncIterable<string>;
|
|
97
104
|
clear(prefix?: string): Promise<void>;
|
|
98
105
|
incrementTagVersion(tag: string): Promise<number>;
|
|
99
106
|
getTagVersion(tag: string): Promise<number>;
|