Supports Uint8Array/Uint16Array/Uint32Array for serialize props (#4669)

* 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>
This commit is contained in:
aggre 2022-09-22 01:19:30 +09:00 committed by GitHub
parent f366967cbc
commit a961aa3c2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
astro-island now correctly passes Uint8Array/Uint16Array/Uint32Array

View file

@ -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 => {

View file

@ -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)];

View file

@ -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;