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
110 changes: 0 additions & 110 deletions src/base64.js

This file was deleted.

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

case 'ArrayBuffer': {
const base64 = value[1];
const arraybuffer = decode64(base64);
const arraybuffer = decode85(value[1]);
hydrated[index] = arraybuffer;
break;
}
Expand Down
8 changes: 2 additions & 6 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
POSITIVE_INFINITY,
UNDEFINED
} from './constants.js';
import { encode64 } from './base64.js';
import { encode85 } from './z85.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","${encode85(thing)}"]`;
break;
}

Expand Down
62 changes: 62 additions & 0 deletions src/z85.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const ENCODE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";
const DECODE = [-1, 68, -1, 84, 83, 82, 72, -1, 75, 76, 70, 65, -1, 63, 62, 69, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 64, -1, 73, 66, 74, 71, 81, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, -1, 78, 67, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 79, -1, 80, -1, -1];
const POW_85 = [1, 85, 7225, 614125, 52200625];
const POW_256 = [1, 256, 65536, 16777216];

/**
* Encodes binary data into a Z85 string.
* @param {ArrayBuffer} data - The binary data to encode
* @returns {string} The Z85 encoded string
*/
export function encode85(data) {
const u8a = new Uint8Array(data);
const length = u8a.length;
const padding = (4 - (length % 4)) % 4;

let result = '', value = 0;
for (let i = 0; i < length + padding; ++i) {
const isPadding = i >= length;
value = value * 256 + (isPadding ? 0 : u8a[i]);
if ((i + 1) % 4 !== 0) continue;

if (!isPadding) {
result += ENCODE[Math.floor(value / POW_85[4]) % 85];
result += ENCODE[Math.floor(value / POW_85[3]) % 85];
result += ENCODE[Math.floor(value / POW_85[2]) % 85];
result += ENCODE[Math.floor(value / POW_85[1]) % 85];
result += ENCODE[value % 85];
} else {
for (let j = 5; j > padding; --j) {
result += ENCODE[Math.floor(value / POW_85[j - 1]) % 85];
}
}
value = 0;
}

return result;
};

/**
* Decodes a Z85 string into binary data.
* @param {string} string - The Z85 encoded string
* @returns {ArrayBuffer} The decoded binary data
*/
export function decode85(string) {
const remainder = string.length % 5;
const padding = 5 - (remainder === 0 ? 5 : remainder);
string = string.padEnd(string.length + padding, ENCODE[ENCODE.length - 1]);
const length = string.length;

let buffer = new Uint8Array((length * 4 / 5) - padding);
let value = 0, char = 0, byte = 0;
for (let i = 0; i < length; ++i) {
value = value * 85 + DECODE[string.charCodeAt(char++) - 32];
if (char % 5 !== 0) continue;

for (let j = 3; j >= 0; --j)
buffer[byte++] = Math.floor(value / POW_256[j]) % 256;
value = 0;
}

return buffer.buffer;
}
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","0rJu"]]'
},
{
name: 'ArrayBuffer',
value: new Uint8Array([1, 2, 3]).buffer,
js: 'new Uint8Array([1,2,3]).buffer',
json: '[["ArrayBuffer","AQID"]]'
json: '[["ArrayBuffer","0rJu"]]'
},
{
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","3ig+u9SNO*"]]'
},
{
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","009c61o!#m2NH"],["Uint16Array",2]]',
validate: ([uint8, uint16]) => {
return uint8.buffer === uint16.buffer;
}
Expand Down