From 38e67b0a97418759785d9ec0b10f1daabf5396f4 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 31 May 2023 10:34:57 +0200 Subject: [PATCH] feat: support passing Uint8Array as island props (#1225) This commit adds support for passing Uint8Array's to islands in component props. --- docs/concepts/islands.md | 1 + src/runtime/deserializer.ts | 13 ++++++ .../__snapshots__/serializer_test.ts.snap | 2 + src/server/serializer.ts | 46 +++++++++++++++++++ src/server/serializer_test.ts | 10 ++++ 5 files changed, 72 insertions(+) diff --git a/docs/concepts/islands.md b/docs/concepts/islands.md index 76a53e05..e196e5fd 100644 --- a/docs/concepts/islands.md +++ b/docs/concepts/islands.md @@ -39,6 +39,7 @@ Fresh can serialize the following types of values: `null`, and `bigint`s are not supported) - Plain objects with string keys and serializable values - Arrays containing serializable values +- Uint8Array - Preact Signals (if the inner value is serializable) Circular references are supported. If an object or signal is referenced multiple diff --git a/src/runtime/deserializer.ts b/src/runtime/deserializer.ts index 54c814c1..75b2d262 100644 --- a/src/runtime/deserializer.ts +++ b/src/runtime/deserializer.ts @@ -8,6 +8,16 @@ interface Signal { value: T; } +function b64decode(b64: string): Uint8Array { + const binString = atob(b64); + const size = binString.length; + const bytes = new Uint8Array(size); + for (let i = 0; i < size; i++) { + bytes[i] = binString.charCodeAt(i); + } + return bytes; +} + export function deserialize( str: string, signal?: (a: T) => Signal, @@ -19,6 +29,9 @@ export function deserialize( if (v[KEY] === "s") { return signal!(v.v); } + if (v[KEY] === "u8a") { + return b64decode(v.d); + } if (v[KEY] === "l") { const val = v.v; val[KEY] = v.k; diff --git a/src/server/__snapshots__/serializer_test.ts.snap b/src/server/__snapshots__/serializer_test.ts.snap index c1d16cc0..fc4e3467 100644 --- a/src/server/__snapshots__/serializer_test.ts.snap +++ b/src/server/__snapshots__/serializer_test.ts.snap @@ -2,6 +2,8 @@ export const snapshot = {}; snapshot[`serializer - primitives & plain objects 1`] = `'{"v":{"a":1,"b":"2","c":true,"d":null,"f":[1,2,3],"g":{"a":1,"b":2,"c":3}}}'`; +snapshot[`serializer - Uint8Array 1`] = `'{"v":{"a":{"_f":"u8a","d":"AQID"}}}'`; + snapshot[`serializer - signals 1`] = `'{"v":{"a":1,"b":{"_f":"s","v":2}}}'`; snapshot[`serializer - magic key 1`] = `'{"v":{"_f":"l","k":"f","v":{"a":1}}}'`; diff --git a/src/server/serializer.ts b/src/server/serializer.ts index e8beefbc..a8efbb44 100644 --- a/src/server/serializer.ts +++ b/src/server/serializer.ts @@ -8,6 +8,7 @@ * - `string` * - `array` * - `object` (no prototypes) + * - `Uint8Array` * - `Signal` from `@preact/signals` * * Circular references are supported and objects with the same reference are @@ -118,6 +119,11 @@ export function serialize(data: unknown): SerializeResult { const res = { [KEY]: "s", v: value.peek() }; parentStack.push(res); return res; + } else if (value instanceof Uint8Array) { + requiresDeserializer = true; + const res = { [KEY]: "u8a", d: b64encode(value) }; + parentStack.push(res); + return res; } else if (typeof value === "object" && value && KEY in value) { requiresDeserializer = true; // deno-lint-ignore no-explicit-any @@ -136,3 +142,43 @@ export function serialize(data: unknown): SerializeResult { const serialized = JSON.stringify(toSerialize, replacer); return { serialized, requiresDeserializer, hasSignals }; } + +// deno-fmt-ignore +const base64abc = [ + "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", + "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", + "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", + "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", + "8", "9", "+", "/", +]; + +/** + * CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727 + * Encodes a given Uint8Array, ArrayBuffer or string into RFC4648 base64 representation + */ +export function b64encode(buffer: ArrayBuffer): string { + const uint8 = new Uint8Array(buffer); + let result = "", + i; + const l = uint8.length; + for (i = 2; i < l; i += 3) { + result += base64abc[uint8[i - 2] >> 2]; + result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)]; + result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)]; + result += base64abc[uint8[i] & 0x3f]; + } + if (i === l + 1) { + // 1 octet yet to write + result += base64abc[uint8[i - 2] >> 2]; + result += base64abc[(uint8[i - 2] & 0x03) << 4]; + result += "=="; + } + if (i === l) { + // 2 octets yet to write + result += base64abc[uint8[i - 2] >> 2]; + result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)]; + result += base64abc[(uint8[i - 1] & 0x0f) << 2]; + result += "="; + } + return result; +} diff --git a/src/server/serializer_test.ts b/src/server/serializer_test.ts index 0b0f8630..9674b225 100644 --- a/src/server/serializer_test.ts +++ b/src/server/serializer_test.ts @@ -22,6 +22,16 @@ Deno.test("serializer - primitives & plain objects", async (t) => { assertEquals(deserialized, data); }); +Deno.test("serializer - Uint8Array", async (t) => { + const data = { a: new Uint8Array([1, 2, 3]) }; + const res = serialize(data); + assert(res.requiresDeserializer); + assert(!res.hasSignals); + await assertSnapshot(t, res.serialized); + const deserialized = deserialize(res.serialized); + assertEquals(deserialized, data); +}); + Deno.test("serializer - signals", async (t) => { const data = { a: 1,