feat: support passing Uint8Array as island props (#1225)

This commit adds support for passing Uint8Array's to islands in
component props.
This commit is contained in:
Luca Casonato
2023-05-31 08:34:57 +00:00
committed by GitHub
parent e3f9a0df08
commit 38e67b0a97
5 changed files with 72 additions and 0 deletions
+1
View File
@@ -39,6 +39,7 @@ Fresh can serialize the following types of values:
`null`, and `bigint`s are not supported) `null`, and `bigint`s are not supported)
- Plain objects with string keys and serializable values - Plain objects with string keys and serializable values
- Arrays containing serializable values - Arrays containing serializable values
- Uint8Array
- Preact Signals (if the inner value is serializable) - Preact Signals (if the inner value is serializable)
Circular references are supported. If an object or signal is referenced multiple Circular references are supported. If an object or signal is referenced multiple
+13
View File
@@ -8,6 +8,16 @@ interface Signal<T> {
value: T; 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( export function deserialize(
str: string, str: string,
signal?: <T>(a: T) => Signal<T>, signal?: <T>(a: T) => Signal<T>,
@@ -19,6 +29,9 @@ export function deserialize(
if (v[KEY] === "s") { if (v[KEY] === "s") {
return signal!(v.v); return signal!(v.v);
} }
if (v[KEY] === "u8a") {
return b64decode(v.d);
}
if (v[KEY] === "l") { if (v[KEY] === "l") {
const val = v.v; const val = v.v;
val[KEY] = v.k; val[KEY] = v.k;
@@ -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 - 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 - signals 1`] = `'{"v":{"a":1,"b":{"_f":"s","v":2}}}'`;
snapshot[`serializer - magic key 1`] = `'{"v":{"_f":"l","k":"f","v":{"a":1}}}'`; snapshot[`serializer - magic key 1`] = `'{"v":{"_f":"l","k":"f","v":{"a":1}}}'`;
+46
View File
@@ -8,6 +8,7 @@
* - `string` * - `string`
* - `array` * - `array`
* - `object` (no prototypes) * - `object` (no prototypes)
* - `Uint8Array`
* - `Signal` from `@preact/signals` * - `Signal` from `@preact/signals`
* *
* Circular references are supported and objects with the same reference are * 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() }; const res = { [KEY]: "s", v: value.peek() };
parentStack.push(res); parentStack.push(res);
return 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) { } else if (typeof value === "object" && value && KEY in value) {
requiresDeserializer = true; requiresDeserializer = true;
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
@@ -136,3 +142,43 @@ export function serialize(data: unknown): SerializeResult {
const serialized = JSON.stringify(toSerialize, replacer); const serialized = JSON.stringify(toSerialize, replacer);
return { serialized, requiresDeserializer, hasSignals }; 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;
}
+10
View File
@@ -22,6 +22,16 @@ Deno.test("serializer - primitives & plain objects", async (t) => {
assertEquals(deserialized, data); 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) => { Deno.test("serializer - signals", async (t) => {
const data = { const data = {
a: 1, a: 1,