Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dataview-subview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"devalue": patch
---

fix: serialize DataView subviews with the correct byte offset and length
17 changes: 14 additions & 3 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,33 @@ function run(async, value, reducers) {
case 'Float32Array':
case 'Float64Array':
case 'BigInt64Array':
case 'BigUint64Array':
case 'DataView': {
case 'BigUint64Array': {
/** @type {import("./types.js").TypedArray} */
const typedArray = thing;
str = '["' + type + '",' + flatten(typedArray.buffer);

// handle subarrays
if (typedArray.byteLength !== typedArray.buffer.byteLength) {
// to be used with `new TypedArray(buffer, byteOffset, length)`
str += `,${typedArray.byteOffset},${typedArray.length}`;
}

str += ']';
break;
}

case 'DataView': {
/** @type {DataView} */
const view = thing;
str = '["' + type + '",' + flatten(view.buffer);

if (view.byteLength !== view.buffer.byteLength) {
str += `,${view.byteOffset},${view.byteLength}`;
}

str += ']';
break;
}

case 'ArrayBuffer': {
/** @type {ArrayBuffer} */
const arraybuffer = thing;
Expand Down
2 changes: 1 addition & 1 deletion src/uneval.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export function uneval(value, replacer) {

// handle subviews
if (thing.byteLength !== thing.buffer.byteLength) {
str += `,${thing.startOffset},${thing.byteLength}`;
str += `,${thing.byteOffset},${thing.byteLength}`;
}

return str + ')';
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ const fixtures = {
js: 'new DataView(new Uint8Array([1,2,3]).buffer)',
json: '[["DataView",1],["ArrayBuffer","AQID"]]'
},
{
name: 'DataView subview',
value: new DataView(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).buffer, 2, 4),
js: 'new DataView(new Uint8Array([0,1,2,3,4,5,6,7,8,9]).buffer,2,4)',
json: '[["DataView",1,2,4],["ArrayBuffer","AAECAwQFBgcICQ=="]]'
},
{
name: 'URL',
value: new URL('https://user:password@example.com/<script>/path?foo=bar#hash'),
Expand Down Expand Up @@ -698,6 +704,16 @@ const fixtures = {
})(),
js: '(function(a,b){a=new DataView(b);return [a,a,b]}({},new Uint8Array([0,1,2,3,4,5,6,7,8,9]).buffer))',
json: '[[1,1,2],["DataView",2],["ArrayBuffer","AAECAwQFBgcICQ=="]]'
},
{
name: 'DataView subview (repetition)',
value: (() => {
const uint8 = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
const dv = new DataView(uint8.buffer, 2, 4);
return [dv, dv];
})(),
js: '(function(a){a=new DataView(new Uint8Array([0,1,2,3,4,5,6,7,8,9]).buffer,2,4);return [a,a]}({}))',
json: '[[1,1],["DataView",2,2,4],["ArrayBuffer","AAECAwQFBgcICQ=="]]'
}
],

Expand Down
Loading