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.
Files changed (101) hide show
  1. package/README.md +78 -90
  2. package/dashboards/tricache-alerts.yaml +80 -4
  3. package/dashboards/tricache-grafana.json +54 -1
  4. package/dashboards/tricache-inventory.rules.yml +21 -0
  5. package/dist/{cache-service-CWI4jkS7.d.cts → cache-service-BV5T8-20.d.cts} +122 -21
  6. package/dist/cache-service-UZXOQGEY.js +14 -0
  7. package/dist/{cache-service-B108FzER.d.ts → cache-service-tpyrXvlH.d.ts} +122 -21
  8. package/dist/{chunk-UBWLPKTI.js → chunk-EJXG7WO6.js} +152 -32
  9. package/dist/{chunk-H4GFLQ26.js → chunk-KV7KISK2.js} +47 -1
  10. package/dist/{chunk-WYF7NOAT.js → chunk-MB3DMVWN.js} +2111 -543
  11. package/dist/{chunk-OYDU3SMI.js → chunk-RBJRTGFK.js} +5 -0
  12. package/dist/chunk-S5N65I45.js +17 -0
  13. package/dist/cli.cjs +2454 -572
  14. package/dist/cli.js +186 -11
  15. package/dist/dashboard/index.d.cts +2 -2
  16. package/dist/dashboard/index.d.ts +2 -2
  17. package/dist/drizzle/index.cjs +2335 -571
  18. package/dist/drizzle/index.d.cts +2 -2
  19. package/dist/drizzle/index.d.ts +2 -2
  20. package/dist/drizzle/index.js +6 -2
  21. package/dist/edge/index.cjs +145 -43
  22. package/dist/edge/index.d.cts +11 -4
  23. package/dist/edge/index.d.ts +11 -4
  24. package/dist/edge/index.js +118 -43
  25. package/dist/hono/index.cjs +2322 -569
  26. package/dist/hono/index.d.cts +2 -2
  27. package/dist/hono/index.d.ts +2 -2
  28. package/dist/hono/index.js +2 -2
  29. package/dist/{hono-C886ok3X.d.cts → hono-CMv2VdQ6.d.cts} +18 -5
  30. package/dist/{hono-3rDMYVtY.d.ts → hono-Dvq04kX9.d.ts} +18 -5
  31. package/dist/http/index.cjs +2323 -570
  32. package/dist/http/index.d.cts +3 -3
  33. package/dist/http/index.d.ts +3 -3
  34. package/dist/http/index.js +4 -4
  35. package/dist/index.cjs +2554 -620
  36. package/dist/index.d.cts +88 -23
  37. package/dist/index.d.ts +88 -23
  38. package/dist/index.js +873 -16
  39. package/dist/{murmur3-CMGw9PbV.d.cts → murmur3-DF8BFRnu.d.cts} +1 -0
  40. package/dist/{murmur3-CMGw9PbV.d.ts → murmur3-DF8BFRnu.d.ts} +1 -0
  41. package/dist/nestjs/index.cjs +2292 -574
  42. package/dist/nestjs/index.d.cts +19 -5
  43. package/dist/nestjs/index.d.ts +19 -5
  44. package/dist/nestjs/index.js +25 -19
  45. package/dist/next/index.cjs +2306 -571
  46. package/dist/next/index.d.cts +8 -2
  47. package/dist/next/index.d.ts +8 -2
  48. package/dist/next/index.js +36 -9
  49. package/dist/prisma/index.cjs +26 -25
  50. package/dist/prisma/index.d.cts +2 -2
  51. package/dist/prisma/index.d.ts +2 -2
  52. package/dist/prisma/index.js +14 -25
  53. package/dist/serialize-worker.cjs +162 -39
  54. package/dist/serialize-worker.js +9 -7
  55. package/dist/{types-BA4pz1A0.d.cts → types-F8vRWMrE.d.cts} +162 -45
  56. package/dist/{types-BA4pz1A0.d.ts → types-F8vRWMrE.d.ts} +162 -45
  57. package/package.json +21 -2
  58. package/src/adapters/auto-pipeliner.ts +52 -11
  59. package/src/cache-service.ts +1537 -415
  60. package/src/cli.ts +2 -9
  61. package/src/codec.ts +3 -3
  62. package/src/compression.ts +39 -16
  63. package/src/cross-region.ts +1 -1
  64. package/src/disk-tier.ts +283 -43
  65. package/src/drizzle/index.ts +2 -1
  66. package/src/edge/adapters/cloudflare-do.ts +25 -10
  67. package/src/edge/adapters/cloudflare-kv.ts +12 -9
  68. package/src/edge/adapters/upstash.ts +27 -11
  69. package/src/edge/cache.ts +59 -17
  70. package/src/edge/types.ts +15 -5
  71. package/src/encryption.ts +50 -18
  72. package/src/env.d.ts +1 -1
  73. package/src/errors.ts +12 -0
  74. package/src/http/utils.ts +6 -0
  75. package/src/index.ts +6 -0
  76. package/src/latency-watchdog.ts +5 -1
  77. package/src/nestjs/decorators.ts +11 -6
  78. package/src/nestjs/tricache.module.ts +2 -1
  79. package/src/nestjs/tricache.store.ts +21 -9
  80. package/src/nestjs/types.ts +1 -1
  81. package/src/next/cache-handler.ts +24 -4
  82. package/src/next/types.ts +4 -0
  83. package/src/origin-capacity.ts +134 -0
  84. package/src/prisma/index.ts +14 -32
  85. package/src/remote-snapshot.ts +37 -27
  86. package/src/serialize-worker.ts +8 -5
  87. package/src/sigv4-snapshot-adapter.ts +37 -27
  88. package/src/smart-memory-cache.ts +62 -22
  89. package/src/types.ts +92 -35
  90. package/src/utils/config-comparison.ts +29 -0
  91. package/src/utils/lz4-size.ts +74 -0
  92. package/src/utils/namespace-key.ts +48 -0
  93. package/src/utils/query-key.ts +13 -0
  94. package/src/utils/snapshot-transfer.ts +83 -0
  95. package/src/utils/work-limits.ts +73 -0
  96. package/src/utils/write-revision.ts +4 -0
  97. package/src/wasm/bloom-filter-wasm.ts +16 -1
  98. package/src/worker-pool.ts +28 -8
  99. package/src/wtiny-lfu.ts +62 -14
  100. package/dist/cache-service-GYPZFB7O.js +0 -12
  101. package/dist/chunk-VNRDL5RA.js +0 -666
@@ -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-H4GFLQ26.js";
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 this.namespace ? `${this.namespace}:${key}` : key;
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.nk(tag));
361
+ version = await this.remoteStorage.getTagVersion(this.tagVersionKey(tag));
349
362
  } else {
350
- const raw = await this.remoteStorage.get(this.nk(`tag_ver:${tag}`));
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.nk(t));
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.nk(t)] ?? remoteVersions[t] ?? 1;
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.nk(tag));
631
- if (newVer <= previous) newVer = await this.remoteStorage.incrementTagVersion(this.nk(tag));
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.nk(`tag_ver:${tag}`);
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.namespace ? this.namespace + ":" : void 0);
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
- const cleanKey = this.namespace && nsKey.startsWith(this.namespace + ":") ? nsKey.slice(this.namespace.length + 1) : nsKey;
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: 1,
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 !== 1 || !Array.isArray(payload.entries) || this.globalEpoch !== epoch) {
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) || !Number.isFinite(item.expiresAt) || !Number.isFinite(item.staleUntil)) continue;
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
- const pattern = prefix.endsWith("*") ? prefix : `${prefix}*`;
1013
- const keys = await this._executeCommand(["KEYS", pattern]);
1014
- if (Array.isArray(keys) && keys.length > 0) {
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", `tag_ver:${tag}`]);
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", `tag_ver:${tag}`]);
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 keys = tags.map((t) => `tag_ver:${t}`);
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 clear(prefix) {
1097
- if (!this.kv.list) return;
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
- if (res.keys.length > 0) {
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 = `tag_ver:${tag}`;
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(`tag_ver:${tag}`);
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 clear(prefix) {
1191
- if (!this.storage.list) return;
1192
- const map = await this.storage.list({ prefix });
1193
- const keysToDelete = Array.from(map.keys());
1194
- if (keysToDelete.length > 0) {
1195
- await this.storage.delete(keysToDelete);
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 = `tag_ver:${tag}`;
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(`tag_ver:${tag}`);
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.map((t) => `tag_ver:${t}`);
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(`tag_ver:${tag}`);
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 {