layercache 1.2.5 → 1.2.6

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/dist/edge.cjs CHANGED
@@ -39,19 +39,47 @@ function isStoredValueEnvelope(value) {
39
39
  if (v.kind !== "value" && v.kind !== "empty") {
40
40
  return false;
41
41
  }
42
- if (v.freshUntil !== null && typeof v.freshUntil !== "number") {
42
+ if (v.freshUntil !== null && (!Number.isFinite(v.freshUntil) || typeof v.freshUntil !== "number")) {
43
43
  return false;
44
44
  }
45
- if (v.staleUntil !== null && typeof v.staleUntil !== "number") {
45
+ if (v.staleUntil !== null && (!Number.isFinite(v.staleUntil) || typeof v.staleUntil !== "number")) {
46
46
  return false;
47
47
  }
48
- if (v.errorUntil !== null && typeof v.errorUntil !== "number") {
48
+ if (v.errorUntil !== null && (!Number.isFinite(v.errorUntil) || typeof v.errorUntil !== "number")) {
49
49
  return false;
50
50
  }
51
51
  const maxTimestamp = Date.now() + 10 * 365 * 24 * 60 * 60 * 1e3;
52
52
  if (typeof v.freshUntil === "number" && v.freshUntil > maxTimestamp) {
53
53
  return false;
54
54
  }
55
+ if (typeof v.staleUntil === "number" && v.staleUntil > maxTimestamp) {
56
+ return false;
57
+ }
58
+ if (typeof v.errorUntil === "number" && v.errorUntil > maxTimestamp) {
59
+ return false;
60
+ }
61
+ if (v.freshUntil === null && (v.staleUntil !== null || v.errorUntil !== null)) {
62
+ return false;
63
+ }
64
+ if (typeof v.freshUntil === "number" && typeof v.staleUntil === "number" && v.staleUntil < v.freshUntil) {
65
+ return false;
66
+ }
67
+ if (typeof v.freshUntil === "number" && typeof v.errorUntil === "number" && v.errorUntil < v.freshUntil) {
68
+ return false;
69
+ }
70
+ const maxTtlSeconds = 10 * 365 * 24 * 60 * 60;
71
+ if (!isValidEnvelopeTtlSeconds(v.freshTtlSeconds, maxTtlSeconds)) {
72
+ return false;
73
+ }
74
+ if (!isValidEnvelopeTtlSeconds(v.staleWhileRevalidateSeconds, maxTtlSeconds)) {
75
+ return false;
76
+ }
77
+ if (!isValidEnvelopeTtlSeconds(v.staleIfErrorSeconds, maxTtlSeconds)) {
78
+ return false;
79
+ }
80
+ if (v.freshTtlSeconds == null && (v.staleWhileRevalidateSeconds != null || v.staleIfErrorSeconds != null)) {
81
+ return false;
82
+ }
55
83
  return true;
56
84
  }
57
85
  function unwrapStoredValue(stored) {
@@ -63,6 +91,12 @@ function unwrapStoredValue(stored) {
63
91
  }
64
92
  return stored.value ?? null;
65
93
  }
94
+ function isValidEnvelopeTtlSeconds(value, maxTtlSeconds) {
95
+ if (value == null) {
96
+ return true;
97
+ }
98
+ return typeof value === "number" && Number.isFinite(value) && value > 0 && value <= maxTtlSeconds;
99
+ }
66
100
 
67
101
  // src/layers/MemoryLayer.ts
68
102
  var MemoryLayer = class {
@@ -180,6 +214,12 @@ var MemoryLayer = class {
180
214
  this.pruneExpired();
181
215
  return [...this.entries.keys()];
182
216
  }
217
+ async forEachKey(visitor) {
218
+ this.pruneExpired();
219
+ for (const key of this.entries.keys()) {
220
+ await visitor(key);
221
+ }
222
+ }
183
223
  exportState() {
184
224
  this.pruneExpired();
185
225
  return [...this.entries.entries()].map(([key, entry]) => ({
@@ -336,6 +376,11 @@ var TagIndex = class {
336
376
  async keysForTag(tag) {
337
377
  return [...this.tagToKeys.get(tag) ?? /* @__PURE__ */ new Set()];
338
378
  }
379
+ async forEachKeyForTag(tag, visitor) {
380
+ for (const key of this.tagToKeys.get(tag) ?? /* @__PURE__ */ new Set()) {
381
+ await visitor(key);
382
+ }
383
+ }
339
384
  async keysForPrefix(prefix) {
340
385
  const node = this.findNode(prefix);
341
386
  if (!node) {
@@ -345,6 +390,13 @@ var TagIndex = class {
345
390
  this.collectFromNode(node, prefix, matches);
346
391
  return matches;
347
392
  }
393
+ async forEachKeyForPrefix(prefix, visitor) {
394
+ const node = this.findNode(prefix);
395
+ if (!node) {
396
+ return;
397
+ }
398
+ await this.visitFromNode(node, prefix, visitor);
399
+ }
348
400
  async tagsForKey(key) {
349
401
  return [...this.keyToTags.get(key) ?? /* @__PURE__ */ new Set()];
350
402
  }
@@ -353,6 +405,12 @@ var TagIndex = class {
353
405
  this.collectPatternMatches(this.root, "", pattern, 0, matches, /* @__PURE__ */ new Set(), 0);
354
406
  return [...matches];
355
407
  }
408
+ async forEachKeyMatchingPattern(pattern, visitor) {
409
+ const matches = await this.matchPattern(pattern);
410
+ for (const key of matches) {
411
+ await visitor(key);
412
+ }
413
+ }
356
414
  async clear() {
357
415
  this.tagToKeys.clear();
358
416
  this.keyToTags.clear();
@@ -402,6 +460,14 @@ var TagIndex = class {
402
460
  this.collectFromNode(child, `${prefix}${character}`, matches);
403
461
  }
404
462
  }
463
+ async visitFromNode(node, prefix, visitor) {
464
+ if (node.terminal) {
465
+ await visitor(prefix);
466
+ }
467
+ for (const [character, child] of node.children) {
468
+ await this.visitFromNode(child, `${prefix}${character}`, visitor);
469
+ }
470
+ }
405
471
  collectPatternMatches(node, prefix, pattern, patternIndex, matches, visited, depth) {
406
472
  if (depth > MAX_PATTERN_RECURSION_DEPTH) {
407
473
  return;
@@ -526,14 +592,17 @@ function createHonoCacheMiddleware(cache, options = {}) {
526
592
  await next();
527
593
  return;
528
594
  }
595
+ if (!options.keyResolver && options.allowPrivateCaching !== true) {
596
+ await next();
597
+ return;
598
+ }
529
599
  const rawPath = context.req.path ?? context.req.url ?? "/";
530
600
  const key = options.keyResolver ? options.keyResolver(context.req) : `${method}:${normalizeUrl(rawPath)}`;
531
601
  const cached = await cache.get(key, void 0, options);
532
602
  if (cached !== null) {
533
603
  context.header?.("x-cache", "HIT");
534
604
  context.header?.("content-type", "application/json; charset=utf-8");
535
- context.json(cached);
536
- return;
605
+ return context.json(cached);
537
606
  }
538
607
  const originalJson = context.json.bind(context);
539
608
  context.json = (body, status) => {
package/dist/edge.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- export { e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, t as CacheMetricsSnapshot, w as CacheRateLimitOptions, B as CacheTtlPolicy, D as CacheTtlPolicyContext, J as CacheWriteOptions, K as EvictionPolicy, M as MemoryLayer, N as MemoryLayerOptions, O as MemoryLayerSnapshotEntry, P as PatternMatcher, T as TagIndex, Q as createHonoCacheMiddleware } from './edge-P07GCO2Y.cjs';
1
+ export { e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, t as CacheMetricsSnapshot, w as CacheRateLimitOptions, B as CacheTtlPolicy, D as CacheTtlPolicyContext, J as CacheWriteOptions, K as EvictionPolicy, M as MemoryLayer, N as MemoryLayerOptions, O as MemoryLayerSnapshotEntry, P as PatternMatcher, T as TagIndex, Q as createHonoCacheMiddleware } from './edge-DLstcDMn.cjs';
2
2
  import 'node:events';
package/dist/edge.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, t as CacheMetricsSnapshot, w as CacheRateLimitOptions, B as CacheTtlPolicy, D as CacheTtlPolicyContext, J as CacheWriteOptions, K as EvictionPolicy, M as MemoryLayer, N as MemoryLayerOptions, O as MemoryLayerSnapshotEntry, P as PatternMatcher, T as TagIndex, Q as createHonoCacheMiddleware } from './edge-P07GCO2Y.js';
1
+ export { e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, t as CacheMetricsSnapshot, w as CacheRateLimitOptions, B as CacheTtlPolicy, D as CacheTtlPolicyContext, J as CacheWriteOptions, K as EvictionPolicy, M as MemoryLayer, N as MemoryLayerOptions, O as MemoryLayerSnapshotEntry, P as PatternMatcher, T as TagIndex, Q as createHonoCacheMiddleware } from './edge-DLstcDMn.js';
2
2
  import 'node:events';
package/dist/edge.js CHANGED
@@ -2,10 +2,10 @@ import {
2
2
  MemoryLayer,
3
3
  TagIndex,
4
4
  createHonoCacheMiddleware
5
- } from "./chunk-JC26W3KK.js";
5
+ } from "./chunk-GJBKCFE6.js";
6
6
  import {
7
7
  PatternMatcher
8
- } from "./chunk-7V7XAB74.js";
8
+ } from "./chunk-4PPBOOXT.js";
9
9
  export {
10
10
  MemoryLayer,
11
11
  PatternMatcher,