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/edge/index.js
CHANGED
|
@@ -8,10 +8,12 @@ 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,
|
|
@@ -158,6 +160,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
158
160
|
maxBytes;
|
|
159
161
|
defaultTtl;
|
|
160
162
|
namespace;
|
|
163
|
+
namespacePrefix;
|
|
164
|
+
dataPrefix;
|
|
165
|
+
tagVersionPrefix;
|
|
161
166
|
remoteStorage;
|
|
162
167
|
enc;
|
|
163
168
|
tracer;
|
|
@@ -182,6 +187,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
182
187
|
this.maxBytes = options?.maxBytes ?? 32 * 1024 * 1024;
|
|
183
188
|
this.defaultTtl = options?.defaultTtlSeconds ?? 300;
|
|
184
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");
|
|
185
193
|
this.remoteStorage = options?.remoteStorage ?? null;
|
|
186
194
|
this.tracer = options?.tracer ?? null;
|
|
187
195
|
this.enc = new WebCryptoEncryption(options?.encryption);
|
|
@@ -206,7 +214,10 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
206
214
|
return this.bloomFilter;
|
|
207
215
|
}
|
|
208
216
|
nk(key) {
|
|
209
|
-
return
|
|
217
|
+
return `${this.dataPrefix}${key}`;
|
|
218
|
+
}
|
|
219
|
+
tagVersionKey(tag) {
|
|
220
|
+
return `${this.tagVersionPrefix}${tag}`;
|
|
210
221
|
}
|
|
211
222
|
_estimateBytes(key, serialized) {
|
|
212
223
|
return (key.length + serialized.length) * 2 + 64;
|
|
@@ -258,6 +269,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
258
269
|
};
|
|
259
270
|
this.storeL1(namespaced, entry);
|
|
260
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");
|
|
261
274
|
const envelope = JSON.stringify({
|
|
262
275
|
__t_edge: 2,
|
|
263
276
|
__t_val: value,
|
|
@@ -345,9 +358,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
345
358
|
if (this.remoteStorage) {
|
|
346
359
|
try {
|
|
347
360
|
if (typeof this.remoteStorage.getTagVersion === "function") {
|
|
348
|
-
version = await this.remoteStorage.getTagVersion(this.
|
|
361
|
+
version = await this.remoteStorage.getTagVersion(this.tagVersionKey(tag));
|
|
349
362
|
} else {
|
|
350
|
-
const raw = await this.remoteStorage.get(this.
|
|
363
|
+
const raw = await this.remoteStorage.get(this.tagVersionKey(tag));
|
|
351
364
|
version = raw ? parseInt(raw, 10) || 1 : 1;
|
|
352
365
|
}
|
|
353
366
|
} catch {
|
|
@@ -374,11 +387,11 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
374
387
|
if (missingTags.length > 0 && this.remoteStorage) {
|
|
375
388
|
try {
|
|
376
389
|
if (typeof this.remoteStorage.batchGetTagVersions === "function") {
|
|
377
|
-
const nsTags = missingTags.map((t) => this.
|
|
390
|
+
const nsTags = missingTags.map((t) => this.tagVersionKey(t));
|
|
378
391
|
const remoteVersions = await this.remoteStorage.batchGetTagVersions(nsTags);
|
|
379
392
|
for (let i = 0; i < missingTags.length; i++) {
|
|
380
393
|
const t = missingTags[i];
|
|
381
|
-
const v = remoteVersions[this.
|
|
394
|
+
const v = remoteVersions[this.tagVersionKey(t)] ?? 1;
|
|
382
395
|
result[t] = Math.max(this.tagVersionCache.get(t)?.version ?? 0, v);
|
|
383
396
|
this._setTagVersionCache(t, v, now + this.tagVersionTtlMs);
|
|
384
397
|
}
|
|
@@ -627,11 +640,11 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
627
640
|
if (this.remoteStorage) {
|
|
628
641
|
try {
|
|
629
642
|
if (typeof this.remoteStorage.incrementTagVersion === "function") {
|
|
630
|
-
newVer = await this.remoteStorage.incrementTagVersion(this.
|
|
631
|
-
if (newVer <= previous) 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));
|
|
632
645
|
newVer = Math.max(previous + 1, newVer);
|
|
633
646
|
} else {
|
|
634
|
-
const key = this.
|
|
647
|
+
const key = this.tagVersionKey(tag);
|
|
635
648
|
const current = await this._getTagVersion(tag);
|
|
636
649
|
newVer = Math.max(current + 1, Date.now());
|
|
637
650
|
await this.remoteStorage.set(key, String(newVer));
|
|
@@ -665,6 +678,9 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
665
678
|
const isPattern = isWildcardTag(tag);
|
|
666
679
|
const shouldCascade = Boolean(opts?.cascade);
|
|
667
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
|
+
}
|
|
668
684
|
const matched = /* @__PURE__ */ new Set();
|
|
669
685
|
const tagMatches = (t) => {
|
|
670
686
|
if (t === tag) return true;
|
|
@@ -678,6 +694,14 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
678
694
|
for (const t of this.tagVersionCache.keys()) {
|
|
679
695
|
if (tagMatches(t)) matched.add(t);
|
|
680
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
|
+
}
|
|
681
705
|
if (shouldCascade && !isPattern) matched.add(tag);
|
|
682
706
|
for (const t of matched) {
|
|
683
707
|
await this._invalidateConcreteTag(t);
|
|
@@ -709,7 +733,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
709
733
|
this.bloomFilter.reset();
|
|
710
734
|
}
|
|
711
735
|
if (this.remoteStorage) {
|
|
712
|
-
await this.remoteStorage.clear?.(this.
|
|
736
|
+
await this.remoteStorage.clear?.(this.dataPrefix);
|
|
713
737
|
}
|
|
714
738
|
this._setTagVersionCache(NAMESPACE_TAG, previousNamespace, Date.now() + this.tagVersionTtlMs);
|
|
715
739
|
await this._invalidateConcreteTag(NAMESPACE_TAG);
|
|
@@ -773,7 +797,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
773
797
|
const entries = [];
|
|
774
798
|
for (const [nsKey, entry] of this.l1.entries()) {
|
|
775
799
|
if (entry.expiresAt > now || entry.staleUntil > now) {
|
|
776
|
-
|
|
800
|
+
if (!nsKey.startsWith(this.dataPrefix)) continue;
|
|
801
|
+
const cleanKey = nsKey.slice(this.dataPrefix.length);
|
|
777
802
|
entries.push({
|
|
778
803
|
key: cleanKey,
|
|
779
804
|
value: entry.value,
|
|
@@ -785,7 +810,8 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
785
810
|
}
|
|
786
811
|
}
|
|
787
812
|
const payload = {
|
|
788
|
-
version:
|
|
813
|
+
version: 3,
|
|
814
|
+
namespace: this.namespacePrefix,
|
|
789
815
|
writtenAt: now,
|
|
790
816
|
entries
|
|
791
817
|
};
|
|
@@ -852,7 +878,7 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
852
878
|
} catch {
|
|
853
879
|
return 0;
|
|
854
880
|
}
|
|
855
|
-
if (!payload || payload.version !==
|
|
881
|
+
if (!payload || payload.version !== 3 || payload.namespace !== this.namespacePrefix || !Array.isArray(payload.entries) || this.globalEpoch !== epoch) {
|
|
856
882
|
return 0;
|
|
857
883
|
}
|
|
858
884
|
const now = Date.now();
|
|
@@ -870,10 +896,21 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
870
896
|
let imported = 0;
|
|
871
897
|
for (const item of payload.entries) {
|
|
872
898
|
if (!item || typeof item.key !== "string") continue;
|
|
899
|
+
if (!Number.isFinite(item.expiresAt) || !Number.isFinite(item.staleUntil)) continue;
|
|
873
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
|
+
}
|
|
874
912
|
const nsKey = this.nk(item.key);
|
|
875
|
-
if (this.l1.has(nsKey) || this.keyEpochs.has(nsKey)
|
|
876
|
-
const serialized = JSON.stringify(item.value);
|
|
913
|
+
if (this.l1.has(nsKey) || this.keyEpochs.has(nsKey)) continue;
|
|
877
914
|
const bytes = this._estimateBytes(nsKey, serialized);
|
|
878
915
|
if (bytes > this.maxBytes || this.maxKeys === 0) continue;
|
|
879
916
|
this._deleteL1(nsKey);
|
|
@@ -922,9 +959,11 @@ var EdgeCacheService = class _EdgeCacheService {
|
|
|
922
959
|
|
|
923
960
|
// src/edge/adapters/upstash.ts
|
|
924
961
|
var UpstashRedisAdapter = class {
|
|
962
|
+
tagConsistency = "atomic";
|
|
925
963
|
baseUrl;
|
|
926
964
|
token;
|
|
927
965
|
customFetch;
|
|
966
|
+
dangerouslyFlushDatabase;
|
|
928
967
|
constructor(options) {
|
|
929
968
|
if (!options?.url || !options?.token) {
|
|
930
969
|
throw new Error("UpstashRedisAdapter: url and token are required.");
|
|
@@ -932,6 +971,7 @@ var UpstashRedisAdapter = class {
|
|
|
932
971
|
this.baseUrl = options.url.replace(/\/+$/, "");
|
|
933
972
|
this.token = options.token;
|
|
934
973
|
this.customFetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
974
|
+
this.dangerouslyFlushDatabase = options.dangerouslyFlushDatabase ?? false;
|
|
935
975
|
}
|
|
936
976
|
async _executeCommand(command) {
|
|
937
977
|
const res = await this.customFetch(this.baseUrl, {
|
|
@@ -1007,29 +1047,43 @@ var UpstashRedisAdapter = class {
|
|
|
1007
1047
|
await this._executeCommand(["MSET", ...args]);
|
|
1008
1048
|
}
|
|
1009
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
|
+
}
|
|
1010
1060
|
async clear(prefix) {
|
|
1011
|
-
if (prefix) {
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
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) {
|
|
1015
1070
|
await this._executeCommand(["DEL", ...keys]);
|
|
1071
|
+
keys = [];
|
|
1016
1072
|
}
|
|
1017
|
-
} else {
|
|
1018
|
-
await this._executeCommand(["FLUSHDB"]);
|
|
1019
1073
|
}
|
|
1074
|
+
if (keys.length) await this._executeCommand(["DEL", ...keys]);
|
|
1020
1075
|
}
|
|
1021
1076
|
async incrementTagVersion(tag) {
|
|
1022
|
-
const res = await this._executeCommand(["INCR",
|
|
1077
|
+
const res = await this._executeCommand(["INCR", tag]);
|
|
1023
1078
|
return typeof res === "number" ? res : 1;
|
|
1024
1079
|
}
|
|
1025
1080
|
async getTagVersion(tag) {
|
|
1026
|
-
const res = await this._executeCommand(["GET",
|
|
1081
|
+
const res = await this._executeCommand(["GET", tag]);
|
|
1027
1082
|
return res ? parseInt(res, 10) || 1 : 1;
|
|
1028
1083
|
}
|
|
1029
1084
|
async batchGetTagVersions(tags) {
|
|
1030
1085
|
if (tags.length === 0) return {};
|
|
1031
|
-
const
|
|
1032
|
-
const res = await this.mget(keys);
|
|
1086
|
+
const res = await this.mget(tags);
|
|
1033
1087
|
const result = {};
|
|
1034
1088
|
for (let i = 0; i < tags.length; i++) {
|
|
1035
1089
|
const raw = res[i];
|
|
@@ -1042,6 +1096,7 @@ var UpstashRedisAdapter = class {
|
|
|
1042
1096
|
// src/edge/adapters/cloudflare-kv.ts
|
|
1043
1097
|
var MIN_CF_KV_TTL = 60;
|
|
1044
1098
|
var CloudflareKVAdapter = class {
|
|
1099
|
+
tagConsistency = "eventual";
|
|
1045
1100
|
kv;
|
|
1046
1101
|
constructor(kv) {
|
|
1047
1102
|
if (!kv) {
|
|
@@ -1093,19 +1148,21 @@ var CloudflareKVAdapter = class {
|
|
|
1093
1148
|
const keys = Object.keys(entries);
|
|
1094
1149
|
await Promise.all(keys.map((k) => this.set(k, entries[k], ttlSeconds)));
|
|
1095
1150
|
}
|
|
1096
|
-
async
|
|
1097
|
-
if (!this.kv.list)
|
|
1151
|
+
async *scanKeys(prefix) {
|
|
1152
|
+
if (!this.kv.list) throw new Error("Workers KV binding does not support list");
|
|
1098
1153
|
let cursor;
|
|
1099
1154
|
do {
|
|
1100
|
-
const res = await this.kv.list({ prefix, cursor });
|
|
1101
|
-
|
|
1102
|
-
await Promise.all(res.keys.map((item) => this.kv.delete(item.name)));
|
|
1103
|
-
}
|
|
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;
|
|
1104
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");
|
|
1105
1159
|
} while (cursor);
|
|
1106
1160
|
}
|
|
1161
|
+
async clear(prefix = "") {
|
|
1162
|
+
for await (const key of this.scanKeys(prefix)) await this.kv.delete(key);
|
|
1163
|
+
}
|
|
1107
1164
|
async incrementTagVersion(tag) {
|
|
1108
|
-
const key =
|
|
1165
|
+
const key = tag;
|
|
1109
1166
|
const raw = await this.kv.get(key);
|
|
1110
1167
|
const prev = raw ? parseInt(raw, 10) || 0 : 0;
|
|
1111
1168
|
const nextVer = Math.max(prev + 1, Date.now());
|
|
@@ -1113,7 +1170,7 @@ var CloudflareKVAdapter = class {
|
|
|
1113
1170
|
return nextVer;
|
|
1114
1171
|
}
|
|
1115
1172
|
async getTagVersion(tag) {
|
|
1116
|
-
const raw = await this.kv.get(
|
|
1173
|
+
const raw = await this.kv.get(tag);
|
|
1117
1174
|
return raw ? parseInt(raw, 10) || 1 : 1;
|
|
1118
1175
|
}
|
|
1119
1176
|
async batchGetTagVersions(tags) {
|
|
@@ -1128,6 +1185,7 @@ var CloudflareKVAdapter = class {
|
|
|
1128
1185
|
|
|
1129
1186
|
// src/edge/adapters/cloudflare-do.ts
|
|
1130
1187
|
var CloudflareDOStorageAdapter = class {
|
|
1188
|
+
tagConsistency = "atomic";
|
|
1131
1189
|
storage;
|
|
1132
1190
|
constructor(storage) {
|
|
1133
1191
|
if (!storage) {
|
|
@@ -1187,16 +1245,33 @@ var CloudflareDOStorageAdapter = class {
|
|
|
1187
1245
|
}
|
|
1188
1246
|
await this.storage.put(record);
|
|
1189
1247
|
}
|
|
1190
|
-
async
|
|
1191
|
-
if (!this.storage.list)
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
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
|
+
}
|
|
1196
1270
|
}
|
|
1271
|
+
if (keys.length) await this.storage.delete(keys);
|
|
1197
1272
|
}
|
|
1198
1273
|
async incrementTagVersion(tag) {
|
|
1199
|
-
const key =
|
|
1274
|
+
const key = tag;
|
|
1200
1275
|
const raw = await this.storage.get(key);
|
|
1201
1276
|
const prev = raw && typeof raw === "object" && "val" in raw ? parseInt(raw.val, 10) || 0 : typeof raw === "string" ? parseInt(raw, 10) || 0 : 0;
|
|
1202
1277
|
const nextVer = prev + 1;
|
|
@@ -1204,18 +1279,18 @@ var CloudflareDOStorageAdapter = class {
|
|
|
1204
1279
|
return nextVer;
|
|
1205
1280
|
}
|
|
1206
1281
|
async getTagVersion(tag) {
|
|
1207
|
-
const raw = await this.storage.get(
|
|
1282
|
+
const raw = await this.storage.get(tag);
|
|
1208
1283
|
if (raw && typeof raw === "object" && "val" in raw) {
|
|
1209
1284
|
return parseInt(raw.val, 10) || 1;
|
|
1210
1285
|
}
|
|
1211
1286
|
return typeof raw === "string" ? parseInt(raw, 10) || 1 : 1;
|
|
1212
1287
|
}
|
|
1213
1288
|
async batchGetTagVersions(tags) {
|
|
1214
|
-
const keys = tags
|
|
1289
|
+
const keys = tags;
|
|
1215
1290
|
const map = await this.storage.get(keys);
|
|
1216
1291
|
const result = {};
|
|
1217
1292
|
for (const tag of tags) {
|
|
1218
|
-
const entry = map.get(
|
|
1293
|
+
const entry = map.get(tag);
|
|
1219
1294
|
if (entry && typeof entry === "object" && "val" in entry) {
|
|
1220
1295
|
result[tag] = parseInt(entry.val, 10) || 1;
|
|
1221
1296
|
} else {
|