Skip to content

Commit

Permalink
Fixed unrecognized ArrayBuffer + DataView
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Jan 23, 2025
1 parent 1a255c5 commit e3aeb51
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 1,720 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
An env agnostic serializer and deserializer with recursion ability and types beyond *JSON* from the *HTML* standard itself.

* [Supported Types](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types)
* *not supported yet*: Blob, File, FileList, ImageBitmap, ImageData, and ArrayBuffer, but typed arrays are supported without major issues, but u/int8, u/int16, and u/int32 are the only safely suppored (right now).
* *not supported yet*: Blob, File, FileList, ImageBitmap, ImageData or others non *JS* types but typed arrays are supported without major issues, but u/int8, u/int16, and u/int32 are the only safely suppored (right now).
* *not possible to implement*: the `{transfer: []}` option can be passed but it's completely ignored.
* [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)
* [Serializer](https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal)
Expand Down
6 changes: 6 additions & 0 deletions cjs/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const deserializer = ($, _) => {
return as(BigInt(value), index);
case 'BigInt':
return as(Object(BigInt(value)), index);
case 'ArrayBuffer':
return as(new Uint8Array(value).buffer, value);
case 'DataView': {
const { buffer } = new Uint8Array(value);
return as(new DataView(buffer), value);
}
}
return as(new env[type](value), index);
};
Expand Down
16 changes: 13 additions & 3 deletions cjs/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const typeOf = value => {
return [MAP, EMPTY];
case 'Set':
return [SET, EMPTY];
case 'DataView':
return [ARRAY, asString];
}

if (asString.includes('Array'))
Expand Down Expand Up @@ -76,9 +78,17 @@ const serializer = (strict, json, $, _) => {
return as([TYPE, entry], value);
}
case ARRAY: {
if (type)
return as([type, [...value]], value);

if (type) {
let spread = value;
if (type === 'DataView') {
spread = new Uint8Array(value.buffer);
}
else if (type === 'ArrayBuffer') {
spread = new Uint8Array(value);
}
return as([type, [...spread]], value);
}

const arr = [];
const index = as([TYPE, arr], value);
for (const entry of value)
Expand Down
6 changes: 6 additions & 0 deletions esm/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const deserializer = ($, _) => {
return as(BigInt(value), index);
case 'BigInt':
return as(Object(BigInt(value)), index);
case 'ArrayBuffer':
return as(new Uint8Array(value).buffer, value);
case 'DataView': {
const { buffer } = new Uint8Array(value);
return as(new DataView(buffer), value);
}
}
return as(new env[type](value), index);
};
Expand Down
16 changes: 13 additions & 3 deletions esm/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const typeOf = value => {
return [MAP, EMPTY];
case 'Set':
return [SET, EMPTY];
case 'DataView':
return [ARRAY, asString];
}

if (asString.includes('Array'))
Expand Down Expand Up @@ -78,9 +80,17 @@ const serializer = (strict, json, $, _) => {
return as([TYPE, entry], value);
}
case ARRAY: {
if (type)
return as([type, [...value]], value);

if (type) {
let spread = value;
if (type === 'DataView') {
spread = new Uint8Array(value.buffer);
}
else if (type === 'ArrayBuffer') {
spread = new Uint8Array(value);
}
return as([type, [...spread]], value);
}

const arr = [];
const index = as([TYPE, arr], value);
for (const entry of value)
Expand Down
Loading

0 comments on commit e3aeb51

Please sign in to comment.