Skip to content
Open
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/famous-words-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'devalue': minor
---

feat: use vlq instead of base64 for encoding/decoding
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
},
"license": "MIT",
"type": "module",
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"dependencies": {
"vlq": "^2.0.4"
}
}
33 changes: 20 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 0 additions & 110 deletions src/base64.js

This file was deleted.

6 changes: 2 additions & 4 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decode64 } from './base64.js';
import * as vlq from 'vlq';
import {
HOLE,
NAN,
Expand Down Expand Up @@ -125,9 +125,7 @@ export function unflatten(parsed, revivers) {
}

case 'ArrayBuffer': {
const base64 = value[1];
const arraybuffer = decode64(base64);
hydrated[index] = arraybuffer;
hydrated[index] = new Uint8Array(vlq.decode(value[1])).buffer;
break;
}

Expand Down
8 changes: 2 additions & 6 deletions src/stringify.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as vlq from 'vlq';
import {
DevalueError,
enumerable_symbols,
Expand All @@ -15,7 +16,6 @@ import {
POSITIVE_INFINITY,
UNDEFINED
} from './constants.js';
import { encode64 } from './base64.js';

/**
* Turn a value into a JSON string that can be parsed with `devalue.parse`
Expand Down Expand Up @@ -177,11 +177,7 @@ export function stringify(value, reducers) {
}

case 'ArrayBuffer': {
/** @type {ArrayBuffer} */
const arraybuffer = thing;
const base64 = encode64(arraybuffer);

str = `["ArrayBuffer","${base64}"]`;
str = `["ArrayBuffer","${vlq.encode(new Uint8Array(thing))}"]`;
break;
}

Expand Down
8 changes: 4 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ const fixtures = {
name: 'Uint8Array',
value: new Uint8Array([1, 2, 3]),
js: 'new Uint8Array([1,2,3])',
json: '[["Uint8Array",1],["ArrayBuffer","AQID"]]'
json: '[["Uint8Array",1],["ArrayBuffer","CEG"]]'
},
{
name: 'ArrayBuffer',
value: new Uint8Array([1, 2, 3]).buffer,
js: 'new Uint8Array([1,2,3]).buffer',
json: '[["ArrayBuffer","AQID"]]'
json: '[["ArrayBuffer","CEG"]]'
},
{
name: 'URL',
Expand All @@ -198,7 +198,7 @@ const fixtures = {
name: 'Sliced typed array',
value: new Uint16Array([10, 20, 30, 40]).subarray(1, 3),
js: 'new Uint16Array([10,20,30,40]).subarray(1,3)',
json: '[["Uint16Array",1,1,3],["ArrayBuffer","CgAUAB4AKAA="]]'
json: '[["Uint16Array",1,1,3],["ArrayBuffer","UAoBA8BAwCA"]]'
},
{
name: 'Temporal.Duration',
Expand Down Expand Up @@ -478,7 +478,7 @@ const fixtures = {
return [uint8, uint16];
})(),
js: '(function(a){return [new Uint8Array([a]),new Uint16Array([a])]}(new Uint8Array([0,1,2,3,4,5,6,7,8,9]).buffer))',
json: '[[1,3],["Uint8Array",2],["ArrayBuffer","AAECAwQFBgcICQ=="],["Uint16Array",2]]',
json: '[[1,3],["Uint8Array",2],["ArrayBuffer","ACEGIKMOQS"],["Uint16Array",2]]',
validate: ([uint8, uint16]) => {
return uint8.buffer === uint16.buffer;
}
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"noImplicitThis": true,
"noEmitOnError": true,
"lib": ["es6", "es2020", "dom"],
"target": "es2020"
"target": "es2020",
"module": "nodenext",
"moduleResolution": "nodenext"
},
"module": "ES6",
"include": ["index.js", "src/*.js"],
Expand Down