Skip to content

Commit 9118b0a

Browse files
committed
feat: use vlq instead of base64 for encoding/decoding
1 parent 434d8ae commit 9118b0a

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

.changeset/famous-words-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'devalue': minor
3+
---
4+
5+
feat: use vlq instead of base64 for encoding/decoding

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
},
3535
"license": "MIT",
3636
"type": "module",
37-
"packageManager": "[email protected]"
37+
"packageManager": "[email protected]",
38+
"dependencies": {
39+
"vlq": "^2.0.4"
40+
}
3841
}

pnpm-lock.yaml

Lines changed: 20 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parse.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { decode64 } from './base64.js';
2+
import * as vlq from 'vlq';
23
import {
34
HOLE,
45
NAN,
@@ -125,9 +126,7 @@ export function unflatten(parsed, revivers) {
125126
}
126127

127128
case 'ArrayBuffer': {
128-
const base64 = value[1];
129-
const arraybuffer = decode64(base64);
130-
hydrated[index] = arraybuffer;
129+
hydrated[index] = new Uint8Array(vlq.decode(value[1])).buffer;
131130
break;
132131
}
133132

src/stringify.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as vlq from 'vlq';
12
import {
23
DevalueError,
34
enumerable_symbols,
@@ -177,11 +178,7 @@ export function stringify(value, reducers) {
177178
}
178179

179180
case 'ArrayBuffer': {
180-
/** @type {ArrayBuffer} */
181-
const arraybuffer = thing;
182-
const base64 = encode64(arraybuffer);
183-
184-
str = `["ArrayBuffer","${base64}"]`;
181+
str = `["ArrayBuffer","${vlq.encode(new Uint8Array(thing))}"]`;
185182
break;
186183
}
187184

test/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ const fixtures = {
172172
name: 'Uint8Array',
173173
value: new Uint8Array([1, 2, 3]),
174174
js: 'new Uint8Array([1,2,3])',
175-
json: '[["Uint8Array",1],["ArrayBuffer","AQID"]]'
175+
json: '[["Uint8Array",1],["ArrayBuffer","CEG"]]'
176176
},
177177
{
178178
name: 'ArrayBuffer',
179179
value: new Uint8Array([1, 2, 3]).buffer,
180180
js: 'new Uint8Array([1,2,3]).buffer',
181-
json: '[["ArrayBuffer","AQID"]]'
181+
json: '[["ArrayBuffer","CEG"]]'
182182
},
183183
{
184184
name: 'URL',
@@ -198,7 +198,7 @@ const fixtures = {
198198
name: 'Sliced typed array',
199199
value: new Uint16Array([10, 20, 30, 40]).subarray(1, 3),
200200
js: 'new Uint16Array([10,20,30,40]).subarray(1,3)',
201-
json: '[["Uint16Array",1,1,3],["ArrayBuffer","CgAUAB4AKAA="]]'
201+
json: '[["Uint16Array",1,1,3],["ArrayBuffer","UAoBA8BAwCA"]]'
202202
},
203203
{
204204
name: 'Temporal.Duration',
@@ -478,7 +478,7 @@ const fixtures = {
478478
return [uint8, uint16];
479479
})(),
480480
js: '(function(a){return [new Uint8Array([a]),new Uint16Array([a])]}(new Uint8Array([0,1,2,3,4,5,6,7,8,9]).buffer))',
481-
json: '[[1,3],["Uint8Array",2],["ArrayBuffer","AAECAwQFBgcICQ=="],["Uint16Array",2]]',
481+
json: '[[1,3],["Uint8Array",2],["ArrayBuffer","ACEGIKMOQS"],["Uint16Array",2]]',
482482
validate: ([uint8, uint16]) => {
483483
return uint8.buffer === uint16.buffer;
484484
}

0 commit comments

Comments
 (0)