@vizamodo/edge-cache-core 0.3.31 → 0.3.33

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.
@@ -21,27 +21,18 @@ export async function getCachedOrFetch(key, fetcher, options) {
21
21
  // Use DedupePromiseCache as L1. On forceRefresh we bypass by deleting first.
22
22
  if (forceRefresh)
23
23
  l1.delete(key);
24
- if (forceRefresh) {
25
- console.debug("[cache] forceRefresh → bypass L1");
26
- }
27
24
  return l1.getOrCreate(key, async () => {
28
- console.debug("[cache] L1 MISS → entering factory", { key });
29
25
  // ── L2: edge cache (skip on forceRefresh) ─────────────────────────────
30
26
  if (!forceRefresh) {
31
- console.debug("[cache] checking L2 (edge cache)", { key });
32
27
  const edgeValue = await getEdgeCache(key).catch(() => null);
33
28
  if (edgeValue !== null && edgeValue !== undefined) {
34
- console.debug("[cache] L2 HIT", { key });
35
29
  // Populate L1 for next request; edge TTL is not observable,
36
30
  // so use caller-supplied ttlSec as an upper bound (may slightly over-cache).
37
31
  return { value: edgeValue, ttlMs: ttlSec * 1000 };
38
32
  }
39
33
  }
40
- console.debug("[cache] L2 MISS → calling fetcher", { key });
41
34
  // ── L3: fetcher ───────────────────────────────────────────────────────
42
- console.debug("[cache] L3 FETCH start", { key });
43
35
  const raw = await fetcher();
44
- console.debug("[cache] L3 FETCH done", { key });
45
36
  let unwrapped;
46
37
  let edgeTtlSec = ttlSec;
47
38
  if (isWrapped(raw)) {
@@ -63,12 +54,10 @@ export async function getCachedOrFetch(key, fetcher, options) {
63
54
  }
64
55
  // Write to edge cache (fire-and-forget — failure must not affect return value).
65
56
  if (edgeTtlSec > 0) {
66
- console.debug("[cache] writing to L2", { key, ttl: edgeTtlSec });
67
57
  setEdgeCache(key, unwrapped, edgeTtlSec).catch(() => { });
68
58
  }
69
59
  // ttlMs for L1 = same window as edge TTL so both layers expire together.
70
60
  const l1TtlMs = edgeTtlSec * 1000;
71
- console.debug("[cache] writing to L1", { key, ttlMs: l1TtlMs });
72
61
  return { value: unwrapped, ttlMs: l1TtlMs };
73
62
  });
74
63
  }
@@ -1,24 +1,59 @@
1
1
  const CACHE_KEY_PREFIX = "https://edge-cache.internal/";
2
+ function normalizeKey(key) {
3
+ return btoa(unescape(encodeURIComponent(key)));
4
+ }
2
5
  /**
3
6
  * Generic edge cache GET helper
4
7
  */
5
8
  export async function getEdgeCache(key) {
6
9
  try {
7
10
  const cache = caches.default;
8
- const req = new Request(CACHE_KEY_PREFIX + key);
11
+ const req = new Request(CACHE_KEY_PREFIX + normalizeKey(key));
12
+ console.debug("[edge-cache] GET", { key, url: req.url });
9
13
  const res = await cache.match(req);
10
- if (!res)
14
+ console.debug("[edge-cache] MATCH RESULT", {
15
+ key,
16
+ hasResponse: !!res,
17
+ status: res?.status,
18
+ headers: res ? Object.fromEntries(res.headers.entries()) : null,
19
+ });
20
+ if (!res) {
21
+ console.debug("[edge-cache] MISS", { key });
11
22
  return null;
23
+ }
24
+ console.debug("[edge-cache] HIT", {
25
+ key,
26
+ status: res.status,
27
+ headers: Object.fromEntries(res.headers.entries()),
28
+ });
12
29
  try {
13
- return (await res.json());
30
+ const data = await res.clone().text();
31
+ console.debug("[edge-cache] HIT RAW", {
32
+ key,
33
+ size: data.length,
34
+ preview: data.slice(0, 200), // avoid huge logs
35
+ });
36
+ let parsed;
37
+ try {
38
+ parsed = JSON.parse(data);
39
+ }
40
+ catch (err) {
41
+ console.error("[edge-cache] JSON PARSE FAILED", { key, err, dataPreview: data.slice(0, 200) });
42
+ return null;
43
+ }
44
+ console.debug("[edge-cache] PARSED OK", {
45
+ key,
46
+ type: typeof parsed,
47
+ });
48
+ return parsed;
14
49
  }
15
50
  catch {
16
51
  // corrupted cache entry → ignore
17
52
  return null;
18
53
  }
19
54
  }
20
- catch {
21
- // cache API failure should not break runtime
55
+ catch (err) {
56
+ console.error("[edge-cache] GET FAILED", { key, err });
22
57
  return null;
23
58
  }
24
59
  }
@@ -29,7 +64,8 @@ export async function setEdgeCache(key, value, ttlSec) {
29
64
  if (ttlSec <= 0)
30
65
  return;
31
66
  const cache = caches.default;
32
- const req = new Request(CACHE_KEY_PREFIX + key);
67
+ const req = new Request(CACHE_KEY_PREFIX + normalizeKey(key));
68
+ console.debug("[edge-cache] SET", { key, url: req.url, ttlSec });
33
69
  const body = JSON.stringify(value);
34
70
  const res = new Response(body, {
35
71
  headers: {
@@ -38,9 +74,11 @@ export async function setEdgeCache(key, value, ttlSec) {
38
74
  },
39
75
  });
40
76
  try {
77
+ console.debug("[edge-cache] SET BODY SIZE", { key, size: body.length });
41
78
  await cache.put(req, res);
79
+ console.debug("[edge-cache] SET OK", { key });
42
80
  }
43
- catch {
44
- // cache write failure is non-fatal
81
+ catch (err) {
82
+ console.error("[edge-cache] SET FAILED", { key, err });
45
83
  }
46
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizamodo/edge-cache-core",
3
- "version": "0.3.31",
3
+ "version": "0.3.33",
4
4
  "description": "Edge cache primitives for Cloudflare Workers (L1 memory + L2 edge cache)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",