Skip to content

Commit

Permalink
Supports Uint8Array/Uint16Array/Uint32Array for serialize props (#4669)
Browse files Browse the repository at this point in the history
* supports Uint8Array/Uint16Array/Uint32Array

* update astro-island to supports the added types

* run changeset

* apply format

* pass metadata through

* using Array type

* write test cases for serializing Uint8Array/Uint16Array/Uint32Array

* fix the broken test cases

* add type assertion for psychological safety

* this changes is minor change

* feat: serialize UintArrays directly

* Update index.ts

Co-authored-by: Matthew Phillips <matthew@skypack.dev>
Co-authored-by: Nate Moore <nate@astro.build>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
4 people committed Sep 21, 2022
1 parent f366967 commit a961aa3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-ears-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

astro-island now correctly passes Uint8Array/Uint16Array/Uint32Array
3 changes: 3 additions & 0 deletions packages/astro/src/runtime/server/astro-island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/src/runtime/server/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const PROP_TYPE = {
Set: 5,
BigInt: 6,
URL: 7,
Uint8Array: 8,
Uint16Array: 9,
Uint32Array: 10,
};

function serializeArray(
Expand Down Expand Up @@ -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)];
Expand Down
15 changes: 15 additions & 0 deletions packages/astro/test/serialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a961aa3

Please sign in to comment.