Skip to content

Commit e3aeb51

Browse files
committed
Fixed unrecognized ArrayBuffer + DataView
1 parent 1a255c5 commit e3aeb51

File tree

9 files changed

+364
-1720
lines changed

9 files changed

+364
-1720
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
An env agnostic serializer and deserializer with recursion ability and types beyond *JSON* from the *HTML* standard itself.
66

77
* [Supported Types](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types)
8-
* *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).
8+
* *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).
99
* *not possible to implement*: the `{transfer: []}` option can be passed but it's completely ignored.
1010
* [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)
1111
* [Serializer](https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal)

cjs/deserialize.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ const deserializer = ($, _) => {
5858
return as(BigInt(value), index);
5959
case 'BigInt':
6060
return as(Object(BigInt(value)), index);
61+
case 'ArrayBuffer':
62+
return as(new Uint8Array(value).buffer, value);
63+
case 'DataView': {
64+
const { buffer } = new Uint8Array(value);
65+
return as(new DataView(buffer), value);
66+
}
6167
}
6268
return as(new env[type](value), index);
6369
};

cjs/serialize.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const typeOf = value => {
2727
return [MAP, EMPTY];
2828
case 'Set':
2929
return [SET, EMPTY];
30+
case 'DataView':
31+
return [ARRAY, asString];
3032
}
3133

3234
if (asString.includes('Array'))
@@ -76,9 +78,17 @@ const serializer = (strict, json, $, _) => {
7678
return as([TYPE, entry], value);
7779
}
7880
case ARRAY: {
79-
if (type)
80-
return as([type, [...value]], value);
81-
81+
if (type) {
82+
let spread = value;
83+
if (type === 'DataView') {
84+
spread = new Uint8Array(value.buffer);
85+
}
86+
else if (type === 'ArrayBuffer') {
87+
spread = new Uint8Array(value);
88+
}
89+
return as([type, [...spread]], value);
90+
}
91+
8292
const arr = [];
8393
const index = as([TYPE, arr], value);
8494
for (const entry of value)

esm/deserialize.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ const deserializer = ($, _) => {
6060
return as(BigInt(value), index);
6161
case 'BigInt':
6262
return as(Object(BigInt(value)), index);
63+
case 'ArrayBuffer':
64+
return as(new Uint8Array(value).buffer, value);
65+
case 'DataView': {
66+
const { buffer } = new Uint8Array(value);
67+
return as(new DataView(buffer), value);
68+
}
6369
}
6470
return as(new env[type](value), index);
6571
};

esm/serialize.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const typeOf = value => {
2929
return [MAP, EMPTY];
3030
case 'Set':
3131
return [SET, EMPTY];
32+
case 'DataView':
33+
return [ARRAY, asString];
3234
}
3335

3436
if (asString.includes('Array'))
@@ -78,9 +80,17 @@ const serializer = (strict, json, $, _) => {
7880
return as([TYPE, entry], value);
7981
}
8082
case ARRAY: {
81-
if (type)
82-
return as([type, [...value]], value);
83-
83+
if (type) {
84+
let spread = value;
85+
if (type === 'DataView') {
86+
spread = new Uint8Array(value.buffer);
87+
}
88+
else if (type === 'ArrayBuffer') {
89+
spread = new Uint8Array(value);
90+
}
91+
return as([type, [...spread]], value);
92+
}
93+
8494
const arr = [];
8595
const index = as([TYPE, arr], value);
8696
for (const entry of value)

0 commit comments

Comments
 (0)