@decocms/blocks 7.86.3 → 7.86.5
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/package.json +1 -1
- package/src/sdk/cacheStorage.test.ts +24 -0
- package/src/sdk/cacheStorage.ts +17 -10
- package/src/sdk/encoding.test.ts +26 -10
- package/src/sdk/encoding.ts +12 -3
- package/src/sdk/responseCache.ts +2 -6
package/package.json
CHANGED
|
@@ -169,6 +169,30 @@ it("retains non-JSON values locally without silently corrupting them in KV", asy
|
|
|
169
169
|
});
|
|
170
170
|
});
|
|
171
171
|
|
|
172
|
+
it("serializes a cached value once per write and not at all on a storage hit", async () => {
|
|
173
|
+
const storage = createKVCacheStorage(fakeKV());
|
|
174
|
+
const value = { items: [{ id: 1 }, { id: 2 }] };
|
|
175
|
+
const stringify = vi.spyOn(JSON, "stringify");
|
|
176
|
+
const ofValue = () =>
|
|
177
|
+
stringify.mock.calls.filter(([v]) => v === value || (v as { value?: unknown })?.value === value)
|
|
178
|
+
.length;
|
|
179
|
+
const writer = createCacheStore<typeof value>("once");
|
|
180
|
+
await run(storage, "site", async () => {
|
|
181
|
+
writer.set(writer.key("k"), value, Date.now() + 60_000);
|
|
182
|
+
await flush();
|
|
183
|
+
});
|
|
184
|
+
expect(ofValue()).toBe(1);
|
|
185
|
+
stringify.mockClear();
|
|
186
|
+
const reader = createCacheStore<typeof value>("once");
|
|
187
|
+
const hit = await run(storage, "site", () => reader.read(reader.key("k")));
|
|
188
|
+
expect(hit).toEqual(value);
|
|
189
|
+
// Sizing the memory tier reuses the raw text it just parsed.
|
|
190
|
+
expect(
|
|
191
|
+
stringify.mock.calls.filter(([v]) => (v as { items?: unknown })?.items !== undefined),
|
|
192
|
+
).toHaveLength(0);
|
|
193
|
+
stringify.mockRestore();
|
|
194
|
+
});
|
|
195
|
+
|
|
172
196
|
it("deduplicates a cold loader through slow storage and survives a memory wipe", async () => {
|
|
173
197
|
const storage = createKVCacheStorage(fakeKV());
|
|
174
198
|
const fn = vi.fn(async () => ({ product: 42 }));
|
package/src/sdk/cacheStorage.ts
CHANGED
|
@@ -118,7 +118,10 @@ export function createKVCacheStorage(
|
|
|
118
118
|
if (!Number.isFinite(expiresAt) || expiresAt <= Date.now()) return;
|
|
119
119
|
const raw = JSON.stringify({ value, expiresAt });
|
|
120
120
|
// Leave space below KV's 25 MiB value limit. Oversized cache entries are optional.
|
|
121
|
-
|
|
121
|
+
// UTF-8 takes at most 3 bytes per UTF-16 unit, so only a value that long needs the
|
|
122
|
+
// exact count: encoding every write copied each entry once more just to measure it.
|
|
123
|
+
const limit = 25 * 1024 * 1024;
|
|
124
|
+
if (raw.length * 3 > limit && new TextEncoder().encode(raw).byteLength > limit) return;
|
|
122
125
|
await kv.put(await keyFor(key), raw, {
|
|
123
126
|
expirationTtl: Math.max(60, Math.ceil((expiresAt - Date.now()) / 1000)),
|
|
124
127
|
});
|
|
@@ -175,7 +178,9 @@ function serialize(value: unknown): string | undefined {
|
|
|
175
178
|
throw new Error("Unsupported cached object");
|
|
176
179
|
}
|
|
177
180
|
seen.add(v);
|
|
178
|
-
|
|
181
|
+
// Index and key loops: `Object.values` copied every array and object it walked.
|
|
182
|
+
if (Array.isArray(v)) for (let i = 0; i < v.length; i++) check(v[i]);
|
|
183
|
+
else for (const key in v) check((v as Record<string, unknown>)[key]);
|
|
179
184
|
seen.delete(v);
|
|
180
185
|
}
|
|
181
186
|
check(value);
|
|
@@ -203,13 +208,14 @@ export function createCacheStore<T>(
|
|
|
203
208
|
if (entry) bytes -= entry.bytes;
|
|
204
209
|
local.delete(key);
|
|
205
210
|
}
|
|
206
|
-
|
|
211
|
+
/** `json`: the value's serialization when the caller already has it, so sizing doesn't stringify again. */
|
|
212
|
+
function remember(key: string, value: T, expiresAt: number, json?: string) {
|
|
207
213
|
remove(key);
|
|
208
214
|
let size: number;
|
|
209
215
|
try {
|
|
210
216
|
size = sizeOf
|
|
211
217
|
? sizeOf(value)
|
|
212
|
-
: Math.max(512, (key.length + (JSON.stringify(value)?.length ?? 0)) * 2);
|
|
218
|
+
: Math.max(512, (key.length + ((json ?? JSON.stringify(value))?.length ?? 0)) * 2);
|
|
213
219
|
} catch {
|
|
214
220
|
size = 1024;
|
|
215
221
|
}
|
|
@@ -252,7 +258,7 @@ export function createCacheStore<T>(
|
|
|
252
258
|
if (raw === null) return undefined;
|
|
253
259
|
const entry = JSON.parse(raw) as { value: T; expiresAt: number };
|
|
254
260
|
if (!Number.isFinite(entry.expiresAt) || Date.now() >= entry.expiresAt) return undefined;
|
|
255
|
-
remember(key, entry.value, entry.expiresAt);
|
|
261
|
+
remember(key, entry.value, entry.expiresAt, raw);
|
|
256
262
|
return entry.value;
|
|
257
263
|
} catch {
|
|
258
264
|
return undefined;
|
|
@@ -265,15 +271,16 @@ export function createCacheStore<T>(
|
|
|
265
271
|
expiresAt <= Date.now()
|
|
266
272
|
)
|
|
267
273
|
return;
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
if (!persist || !storage || !Number.isFinite(expiresAt)) return;
|
|
274
|
+
const storage = persist ? getCacheStorageContext()?.storage : null;
|
|
275
|
+
let raw: string | undefined;
|
|
271
276
|
try {
|
|
272
|
-
|
|
273
|
-
if (raw !== undefined) cacheBackground(storage.set(key, raw, expiresAt));
|
|
277
|
+
if (storage) raw = serialize({ value, expiresAt });
|
|
274
278
|
} catch {
|
|
275
279
|
/* Non-serializable results remain in the memory tier. */
|
|
276
280
|
}
|
|
281
|
+
// The memory tier is sized from the same serialization the storage gets.
|
|
282
|
+
remember(key, value, expiresAt, raw);
|
|
283
|
+
if (storage && raw !== undefined) cacheBackground(storage.set(key, raw, expiresAt));
|
|
277
284
|
},
|
|
278
285
|
delete(key: string): void {
|
|
279
286
|
remove(key);
|
package/src/sdk/encoding.test.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
decodeBase64,
|
|
4
|
-
decodeBase64Url,
|
|
5
|
-
encodeBase64,
|
|
6
|
-
encodeBase64Url,
|
|
7
|
-
} from "./encoding";
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { decodeBase64, decodeBase64Url, encodeBase64, encodeBase64Url } from "./encoding";
|
|
8
3
|
|
|
9
4
|
describe("encodeBase64 / decodeBase64", () => {
|
|
10
5
|
it("round-trips ASCII strings", () => {
|
|
@@ -39,8 +34,31 @@ describe("encodeBase64 / decodeBase64", () => {
|
|
|
39
34
|
const bytes = new Uint8Array(0x8000 + 7);
|
|
40
35
|
for (let i = 0; i < bytes.length; i++) bytes[i] = i & 0xff;
|
|
41
36
|
const b64 = encodeBase64(bytes);
|
|
37
|
+
expect(b64).toBe(Buffer.from(bytes).toString("base64"));
|
|
42
38
|
expect(decodeBase64(b64)).toEqual(bytes);
|
|
43
39
|
});
|
|
40
|
+
|
|
41
|
+
it("uses the runtime's native Uint8Array base64 when present (workerd)", () => {
|
|
42
|
+
const proto = Uint8Array.prototype as unknown as { toBase64?: () => string };
|
|
43
|
+
const ctor = Uint8Array as unknown as { fromBase64?: (b64: string) => Uint8Array };
|
|
44
|
+
const had = { to: proto.toBase64, from: ctor.fromBase64 };
|
|
45
|
+
const toBase64 = vi.fn(function (this: Uint8Array) {
|
|
46
|
+
return Buffer.from(this).toString("base64");
|
|
47
|
+
});
|
|
48
|
+
const fromBase64 = vi.fn((b64: string) => new Uint8Array(Buffer.from(b64, "base64")));
|
|
49
|
+
proto.toBase64 = toBase64;
|
|
50
|
+
ctor.fromBase64 = fromBase64;
|
|
51
|
+
try {
|
|
52
|
+
const bytes = new Uint8Array([104, 105, 255, 0]);
|
|
53
|
+
expect(encodeBase64(bytes)).toBe("aGn/AA==");
|
|
54
|
+
expect(decodeBase64("aGn/AA==")).toEqual(bytes);
|
|
55
|
+
expect(toBase64).toHaveBeenCalledOnce();
|
|
56
|
+
expect(fromBase64).toHaveBeenCalledOnce();
|
|
57
|
+
} finally {
|
|
58
|
+
proto.toBase64 = had.to;
|
|
59
|
+
ctor.fromBase64 = had.from;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
44
62
|
});
|
|
45
63
|
|
|
46
64
|
describe("encodeBase64Url / decodeBase64Url", () => {
|
|
@@ -50,9 +68,7 @@ describe("encodeBase64Url / decodeBase64Url", () => {
|
|
|
50
68
|
const standard = encodeBase64(bytes);
|
|
51
69
|
const url = encodeBase64Url(bytes);
|
|
52
70
|
// Every '+' becomes '-', '/' becomes '_', trailing '=' is stripped.
|
|
53
|
-
expect(url).toBe(
|
|
54
|
-
standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""),
|
|
55
|
-
);
|
|
71
|
+
expect(url).toBe(standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""));
|
|
56
72
|
expect(url).not.toContain("+");
|
|
57
73
|
expect(url).not.toContain("/");
|
|
58
74
|
expect(url).not.toContain("=");
|
package/src/sdk/encoding.ts
CHANGED
|
@@ -14,19 +14,28 @@ function toBytes(data: ArrayBuffer | Uint8Array | string): Uint8Array {
|
|
|
14
14
|
return data;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
type NativeBase64 = { toBase64?: () => string };
|
|
18
|
+
type NativeFromBase64 = { fromBase64?: (b64: string) => Uint8Array<ArrayBuffer> };
|
|
19
|
+
|
|
17
20
|
export function encodeBase64(data: ArrayBuffer | Uint8Array | string): string {
|
|
18
21
|
const bytes = toBytes(data);
|
|
19
|
-
//
|
|
20
|
-
|
|
22
|
+
// workerd encodes natively, straight from the bytes, with the same output as btoa().
|
|
23
|
+
const native = (bytes as NativeBase64).toBase64;
|
|
24
|
+
if (native) return native.call(bytes);
|
|
25
|
+
// Chunked so a large input doesn't blow the call stack. `apply`, not a spread: spreading
|
|
26
|
+
// a Uint8Array goes through its iterator, one `{ value, done }` object per byte, ~24 MB
|
|
27
|
+
// of garbage for a 1 MB HTML body.
|
|
21
28
|
let bin = "";
|
|
22
29
|
const CHUNK = 0x8000;
|
|
23
30
|
for (let i = 0; i < bytes.byteLength; i += CHUNK) {
|
|
24
|
-
bin += String.fromCharCode(
|
|
31
|
+
bin += String.fromCharCode.apply(null, bytes.subarray(i, i + CHUNK) as unknown as number[]);
|
|
25
32
|
}
|
|
26
33
|
return btoa(bin);
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
export function decodeBase64(b64: string): Uint8Array<ArrayBuffer> {
|
|
37
|
+
const native = (Uint8Array as unknown as NativeFromBase64).fromBase64;
|
|
38
|
+
if (native) return native(b64);
|
|
30
39
|
const bin = atob(b64);
|
|
31
40
|
const out = new Uint8Array(bin.length);
|
|
32
41
|
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
package/src/sdk/responseCache.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CacheStorage } from "./cacheStorage";
|
|
2
|
-
import { decodeBase64 } from "./encoding";
|
|
2
|
+
import { decodeBase64, encodeBase64 } from "./encoding";
|
|
3
3
|
|
|
4
4
|
interface StoredResponse {
|
|
5
5
|
status: number;
|
|
@@ -76,15 +76,11 @@ export function createResponseCache(storage: CacheStorage, scope: string) {
|
|
|
76
76
|
bytes.set(chunk, offset);
|
|
77
77
|
offset += chunk.byteLength;
|
|
78
78
|
}
|
|
79
|
-
let binary = "";
|
|
80
|
-
for (let i = 0; i < bytes.length; i += 8192) {
|
|
81
|
-
binary += String.fromCharCode(...bytes.subarray(i, i + 8192));
|
|
82
|
-
}
|
|
83
79
|
const value: StoredResponse = {
|
|
84
80
|
status: response.status,
|
|
85
81
|
statusText: response.statusText,
|
|
86
82
|
headers: [...response.headers.entries()],
|
|
87
|
-
body:
|
|
83
|
+
body: encodeBase64(bytes),
|
|
88
84
|
};
|
|
89
85
|
try {
|
|
90
86
|
await storage.set(keyFor(request), JSON.stringify(value), expiresAt);
|