@vizamodo/edge-cache-core 0.3.34 → 0.3.36

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.
@@ -52,9 +52,14 @@ export async function getCachedOrFetch(key, fetcher, options) {
52
52
  else {
53
53
  unwrapped = raw;
54
54
  }
55
- // Write to edge cache (fire-and-forget failure must not affect return value).
55
+ // Write to edge cache (must await to ensure commit before worker exits).
56
56
  if (edgeTtlSec > 0) {
57
- setEdgeCache(key, unwrapped, edgeTtlSec).catch(() => { });
57
+ try {
58
+ await setEdgeCache(key, unwrapped, edgeTtlSec);
59
+ }
60
+ catch {
61
+ // best-effort: do not break response on cache failure
62
+ }
58
63
  }
59
64
  // ttlMs for L1 = same window as edge TTL so both layers expire together.
60
65
  const l1TtlMs = edgeTtlSec * 1000;
@@ -36,6 +36,12 @@ export async function getEdgeCache(key) {
36
36
  let parsed;
37
37
  try {
38
38
  parsed = JSON.parse(data);
39
+ // Normalize back: unwrap primitive values
40
+ if (parsed &&
41
+ typeof parsed === "object" &&
42
+ parsed.__primitive === true) {
43
+ parsed = parsed.value;
44
+ }
39
45
  }
40
46
  catch (err) {
41
47
  console.error("[edge-cache] JSON PARSE FAILED", { key, err, dataPreview: data.slice(0, 200) });
@@ -44,6 +50,9 @@ export async function getEdgeCache(key) {
44
50
  console.debug("[edge-cache] PARSED OK", {
45
51
  key,
46
52
  type: typeof parsed,
53
+ isPrimitiveWrapped: typeof parsed !== "object"
54
+ ? true
55
+ : parsed?.__primitive === true,
47
56
  });
48
57
  return parsed;
49
58
  }
@@ -66,7 +75,11 @@ export async function setEdgeCache(key, value, ttlSec) {
66
75
  const cache = caches.default;
67
76
  const req = new Request(CACHE_KEY_PREFIX + normalizeKey(key));
68
77
  console.debug("[edge-cache] SET", { key, url: req.url, ttlSec });
69
- const body = JSON.stringify(value);
78
+ // Normalize value: always store as object to ensure stable JSON shape
79
+ const normalizedValue = value !== null && typeof value === "object"
80
+ ? value
81
+ : { __primitive: true, value };
82
+ const body = JSON.stringify(normalizedValue);
70
83
  const res = new Response(body, {
71
84
  headers: {
72
85
  "Content-Type": "application/json",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizamodo/edge-cache-core",
3
- "version": "0.3.34",
3
+ "version": "0.3.36",
4
4
  "description": "Edge cache primitives for Cloudflare Workers (L1 memory + L2 edge cache)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",