@decocms/blocks 7.86.3 → 7.86.4
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/encoding.test.ts +26 -10
- package/src/sdk/encoding.ts +12 -3
- package/src/sdk/responseCache.ts +2 -6
package/package.json
CHANGED
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);
|