diff --git a/.changeset/healthy-ears-compete.md b/.changeset/healthy-ears-compete.md new file mode 100644 index 000000000..f55d8b1e1 --- /dev/null +++ b/.changeset/healthy-ears-compete.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +astro-island now correctly passes Uint8Array/Uint16Array/Uint32Array diff --git a/packages/astro/src/runtime/server/astro-island.ts b/packages/astro/src/runtime/server/astro-island.ts index 894392524..60db5b6b1 100644 --- a/packages/astro/src/runtime/server/astro-island.ts +++ b/packages/astro/src/runtime/server/astro-island.ts @@ -26,6 +26,9 @@ declare const Astro: { 5: (value) => new Set(JSON.parse(value, reviver)), 6: (value) => BigInt(value), 7: (value) => new URL(value), + 8: (value) => new Uint8Array(JSON.parse(value)), + 9: (value) => new Uint16Array(JSON.parse(value)), + 10: (value) => new Uint32Array(JSON.parse(value)), }; const reviver = (propKey: string, raw: string): any => { diff --git a/packages/astro/src/runtime/server/serialize.ts b/packages/astro/src/runtime/server/serialize.ts index 021c050da..140823600 100644 --- a/packages/astro/src/runtime/server/serialize.ts +++ b/packages/astro/src/runtime/server/serialize.ts @@ -11,6 +11,9 @@ const PROP_TYPE = { Set: 5, BigInt: 6, URL: 7, + Uint8Array: 8, + Uint16Array: 9, + Uint32Array: 10, }; function serializeArray( @@ -85,6 +88,15 @@ function convertToSerializedForm( case '[object Array]': { return [PROP_TYPE.JSON, JSON.stringify(serializeArray(value, metadata, parents))]; } + case '[object Uint8Array]': { + return [PROP_TYPE.Uint8Array, JSON.stringify(Array.from(value as Uint8Array))]; + } + case '[object Uint16Array]': { + return [PROP_TYPE.Uint16Array, JSON.stringify(Array.from(value as Uint16Array))]; + } + case '[object Uint32Array]': { + return [PROP_TYPE.Uint32Array, JSON.stringify(Array.from(value as Uint32Array))]; + } default: { if (value !== null && typeof value === 'object') { return [PROP_TYPE.Value, serializeObject(value, metadata, parents)]; diff --git a/packages/astro/test/serialize.test.js b/packages/astro/test/serialize.test.js index 0646edfd7..c3ff0e5bd 100644 --- a/packages/astro/test/serialize.test.js +++ b/packages/astro/test/serialize.test.js @@ -42,6 +42,21 @@ describe('serialize', () => { const output = `{"a":[7,"https://example.com/"]}`; expect(serializeProps(input)).to.equal(output); }); + it('serializes a Uint8Array', () => { + const input = { a: new Uint8Array([1,2,3]) }; + const output = `{"a":[8,"[1,2,3]"]}`; + expect(serializeProps(input)).to.equal(output); + }); + it('serializes a Uint16Array', () => { + const input = { a: new Uint16Array([1,2,3]) }; + const output = `{"a":[9,"[1,2,3]"]}`; + expect(serializeProps(input)).to.equal(output); + }); + it('serializes a Uint32Array', () => { + const input = { a: new Uint32Array([1,2,3]) }; + const output = `{"a":[10,"[1,2,3]"]}`; + expect(serializeProps(input)).to.equal(output); + }); it('cannot serialize a cyclic reference', () => { const a = {}; a.b = a;