layercache 2.1.0 → 3.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/dist/edge.cjs CHANGED
@@ -515,6 +515,9 @@ var TagIndex = class {
515
515
  }
516
516
  insertKnownKey(key) {
517
517
  const isNew = !this.knownKeys.has(key);
518
+ if (!isNew) {
519
+ this.knownKeys.delete(key);
520
+ }
518
521
  this.knownKeys.set(key, Date.now());
519
522
  if (!isNew) {
520
523
  return;
@@ -613,13 +616,13 @@ var TagIndex = class {
613
616
  if (this.maxKnownKeys === void 0 || this.knownKeys.size <= this.maxKnownKeys) {
614
617
  return;
615
618
  }
616
- const sorted = [...this.knownKeys.entries()].sort((a, b) => a[1] - b[1]);
617
619
  const toRemove = Math.ceil(this.maxKnownKeys * 0.1);
618
- for (let i = 0; i < toRemove && i < sorted.length; i += 1) {
619
- const entry = sorted[i];
620
- if (entry) {
621
- this.removeKey(entry[0]);
620
+ for (let i = 0; i < toRemove; i += 1) {
621
+ const oldestKey = this.knownKeys.keys().next().value;
622
+ if (oldestKey === void 0) {
623
+ break;
622
624
  }
625
+ this.removeKnownKey(oldestKey);
623
626
  }
624
627
  }
625
628
  removeKey(key) {
@@ -670,6 +673,41 @@ var TagIndex = class {
670
673
  }
671
674
  };
672
675
 
676
+ // src/integrations/httpCacheKeys.ts
677
+ var SENSITIVE_QUERY_PARAMETERS = /* @__PURE__ */ new Set([
678
+ "access_token",
679
+ "api_key",
680
+ "apikey",
681
+ "auth",
682
+ "authorization",
683
+ "code",
684
+ "credentials",
685
+ "id_token",
686
+ "jwt",
687
+ "password",
688
+ "private_key",
689
+ "refresh_token",
690
+ "secret",
691
+ "session",
692
+ "sessionid",
693
+ "session_id",
694
+ "token"
695
+ ]);
696
+ function normalizeHttpCacheUrl(url) {
697
+ try {
698
+ const parsed = new URL(url, "http://localhost");
699
+ for (const name of [...parsed.searchParams.keys()]) {
700
+ if (SENSITIVE_QUERY_PARAMETERS.has(name.toLowerCase())) {
701
+ parsed.searchParams.delete(name);
702
+ }
703
+ }
704
+ parsed.searchParams.sort();
705
+ return parsed.pathname + parsed.search;
706
+ } catch {
707
+ return url;
708
+ }
709
+ }
710
+
673
711
  // src/integrations/hono.ts
674
712
  function createHonoCacheMiddleware(cache, options = {}) {
675
713
  const allowedMethods = new Set((options.methods ?? ["GET"]).map((method) => method.toUpperCase()));
@@ -684,35 +722,39 @@ function createHonoCacheMiddleware(cache, options = {}) {
684
722
  return;
685
723
  }
686
724
  const rawPath = context.req.path ?? context.req.url ?? "/";
687
- const key = options.keyResolver ? options.keyResolver(context.req) : `${method}:${normalizeUrl(rawPath)}`;
725
+ const key = options.keyResolver ? options.keyResolver(context.req) : `${method}:${normalizeHttpCacheUrl(rawPath)}`;
688
726
  const cached = await cache.get(key, void 0, options);
689
727
  if (cached !== null) {
690
728
  context.header?.("x-cache", "HIT");
691
729
  context.header?.("content-type", "application/json; charset=utf-8");
692
730
  return context.json(cached);
693
731
  }
732
+ let currentStatus;
733
+ const originalStatus = context.status?.bind(context);
734
+ if (originalStatus) {
735
+ context.status = (status) => {
736
+ currentStatus = status;
737
+ return originalStatus(status);
738
+ };
739
+ }
694
740
  const originalJson = context.json.bind(context);
695
741
  context.json = (body, status) => {
696
742
  context.header?.("x-cache", "MISS");
697
- cache.set(key, body, options).catch((err) => {
698
- cache.emit("error", {
699
- operation: "set",
700
- error: err instanceof Error ? err.message : String(err)
743
+ if (isSuccessfulStatus(status ?? currentStatus)) {
744
+ cache.set(key, body, options).catch((err) => {
745
+ cache.emit("error", {
746
+ operation: "set",
747
+ error: err instanceof Error ? err.message : String(err)
748
+ });
701
749
  });
702
- });
750
+ }
703
751
  return originalJson(body, status);
704
752
  };
705
753
  await next();
706
754
  };
707
755
  }
708
- function normalizeUrl(url) {
709
- try {
710
- const parsed = new URL(url, "http://localhost");
711
- parsed.searchParams.sort();
712
- return parsed.pathname + parsed.search;
713
- } catch {
714
- return url;
715
- }
756
+ function isSuccessfulStatus(statusCode) {
757
+ return statusCode === void 0 || statusCode >= 200 && statusCode < 300;
716
758
  }
717
759
  // Annotate the CommonJS export names for ESM import in node:
718
760
  0 && (module.exports = {
package/dist/edge.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BCU8D-Yd.cjs';
1
+ export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BDyuPmIq.cjs';
2
2
  import 'node:events';
package/dist/edge.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BCU8D-Yd.js';
1
+ export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BDyuPmIq.js';
2
2
  import 'node:events';
package/dist/edge.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  MemoryLayer,
3
3
  TagIndex,
4
4
  createHonoCacheMiddleware
5
- } from "./chunk-IVX6ABFX.js";
5
+ } from "./chunk-5CIBABDH.js";
6
6
  import {
7
7
  PatternMatcher
8
8
  } from "./chunk-KJDFYE5T.js";