@vizamodo/edge-cache-core 0.3.31 → 0.3.32
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/runtime/edge-cache.js +18 -6
- package/package.json +1 -1
|
@@ -1,16 +1,25 @@
|
|
|
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
|
+
if (!res) {
|
|
15
|
+
console.debug("[edge-cache] MISS", { key });
|
|
11
16
|
return null;
|
|
17
|
+
}
|
|
18
|
+
console.debug("[edge-cache] HIT", { key });
|
|
12
19
|
try {
|
|
13
|
-
|
|
20
|
+
const data = await res.clone().text();
|
|
21
|
+
console.debug("[edge-cache] HIT RAW", { key, size: data.length });
|
|
22
|
+
return JSON.parse(data);
|
|
14
23
|
}
|
|
15
24
|
catch {
|
|
16
25
|
// corrupted cache entry → ignore
|
|
@@ -29,7 +38,8 @@ export async function setEdgeCache(key, value, ttlSec) {
|
|
|
29
38
|
if (ttlSec <= 0)
|
|
30
39
|
return;
|
|
31
40
|
const cache = caches.default;
|
|
32
|
-
const req = new Request(CACHE_KEY_PREFIX + key);
|
|
41
|
+
const req = new Request(CACHE_KEY_PREFIX + normalizeKey(key));
|
|
42
|
+
console.debug("[edge-cache] SET", { key, url: req.url, ttlSec });
|
|
33
43
|
const body = JSON.stringify(value);
|
|
34
44
|
const res = new Response(body, {
|
|
35
45
|
headers: {
|
|
@@ -38,9 +48,11 @@ export async function setEdgeCache(key, value, ttlSec) {
|
|
|
38
48
|
},
|
|
39
49
|
});
|
|
40
50
|
try {
|
|
51
|
+
console.debug("[edge-cache] SET BODY SIZE", { key, size: body.length });
|
|
41
52
|
await cache.put(req, res);
|
|
53
|
+
console.debug("[edge-cache] SET OK", { key });
|
|
42
54
|
}
|
|
43
|
-
catch {
|
|
44
|
-
|
|
55
|
+
catch (err) {
|
|
56
|
+
console.error("[edge-cache] SET FAILED", { key, err });
|
|
45
57
|
}
|
|
46
58
|
}
|