Compare commits

...
Sign in to create a new pull request.

13 commits

Author SHA1 Message Date
Nate Moore
46b68943ed feat: serialize UintArrays directly 2022-09-09 14:54:43 -05:00
aggre
e850437dbf Merge branch 'main' of github.com:withastro/astro into main 2022-09-09 13:26:51 +09:00
aggre
e307288461 add type assertion for psychological safety 2022-09-09 13:04:13 +09:00
aggre
1e3f9b7896 fix the broken test cases 2022-09-09 12:56:22 +09:00
aggre
28998cc796 write test cases for serializing Uint8Array/Uint16Array/Uint32Array 2022-09-09 12:51:22 +09:00
aggre
de0002136c using Array type 2022-09-09 12:49:57 +09:00
aggre
b297723598 Merge branch 'main' of github.com:withastro/astro into main 2022-09-09 10:48:05 +09:00
aggre
b71eef574b pass metadata through 2022-09-09 10:46:21 +09:00
Matthew Phillips
ee300ec1b7
Merge branch 'main' into main 2022-09-08 12:33:59 -04:00
aggre
1a2f12ae07 apply format 2022-09-08 11:25:48 +09:00
aggre
cfa5823f74 run changeset 2022-09-08 11:19:38 +09:00
aggre
1a009138e4 update astro-island to supports the added types 2022-09-08 11:14:59 +09:00
aggre
fe53d0c470 supports Uint8Array/Uint16Array/Uint32Array 2022-09-08 10:45:18 +09:00
5 changed files with 36 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
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;

View file

@ -40,6 +40,7 @@ export function mkdirp(dir: string) {
}
function isEmpty(dirPath: string) {
console.log(dirPath);
return !fs.existsSync(dirPath) || fs.readdirSync(dirPath).length === 0;
}