Skip to content
Closed
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
55 changes: 0 additions & 55 deletions src/jsonify/__snapshots__/round_trip_test.ts.snap

This file was deleted.

17 changes: 10 additions & 7 deletions src/jsonify/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ function unpack(
return hydrated[idx] = set;
}
case "Uint8Array":
return hydrated[idx] = b64decode(current[1]);
// TODO(iuioiua): use `Uint8Array.prototype.fromBase64()` once
// available in Deno (https://github.com/denoland/deno/issues/25051)
// and sufficiently supported in browsers
// (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64#browser_compatibility)
return hydrated[idx] = decodeBase64(current[1]);
}
} else {
const actual = new Array(current.length);
Expand Down Expand Up @@ -119,12 +123,11 @@ function unpack(
}
}

function b64decode(b64: string): Uint8Array {
const binString = atob(b64);
const size = binString.length;
const bytes = new Uint8Array(size);
for (let i = 0; i < size; i++) {
bytes[i] = binString.charCodeAt(i);
function decodeBase64(base64: string): Uint8Array {
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}
44 changes: 0 additions & 44 deletions src/jsonify/custom_test.ts → src/jsonify/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ Deno.test("custom parse - Signals", () => {
expect(res.peek()).toEqual(2);
});

Deno.test("custom stringify - Signals", () => {
const s = signal(2);
expect(stringify(s, {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[["Signal",1],2]',
);
});

Deno.test("custom parse - Signals with null value", () => {
const res = parse<Signal>('[["Signal",-2]]', {
Signal: (value) => signal(value),
Expand All @@ -51,43 +40,10 @@ Deno.test("custom parse - Signals with null value", () => {
expect(res.peek()).toEqual(null);
});

Deno.test("custom stringify - Signals with null value", () => {
const s = signal(null);
expect(stringify(s, {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[["Signal",-2]]',
);
});

Deno.test("custom parse - Signals with undefined value", () => {
const res = parse<Signal>('[["Signal",-1]]', {
Signal: (value) => signal(value),
});
expect(res).toBeInstanceOf(Signal);
expect(res.peek()).toEqual(undefined);
});

Deno.test("custom stringify - Signals with undefined value", () => {
const s = signal(undefined);
expect(stringify(s, {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[["Signal",-1]]',
);
});

Deno.test("custom stringify - referenced Signals", () => {
const s = signal(2);
expect(stringify([s, s], {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[[1,1],["Signal",2],2]',
);
});
5 changes: 1 addition & 4 deletions src/jsonify/round_trip_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { expect } from "@std/expect";
import { parse } from "./parse.ts";
import { assertSnapshot } from "@std/testing/snapshot";
import { inspect } from "node:util";
import { stringify } from "./stringify.ts";

const inner = { foo: 123 };
Expand Down Expand Up @@ -42,9 +40,8 @@ const TESTS = [
];

for (const value of TESTS) {
Deno.test(`round trip - ${inspect(value)}`, async (t) => {
Deno.test(`round trip - ${Deno.inspect(value)}`, () => {
const str = stringify(value);
await assertSnapshot(t, str);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure why snapshot assertions were used here originally.

const parsed = parse(str);
expect(parsed).toEqual(value);
});
Expand Down
49 changes: 9 additions & 40 deletions src/jsonify/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
HOLE,
INFINITY_NEG,
INFINITY_POS,
NAN,
NULL,
UNDEFINED,
ZERO_NEG,
} from "./constants.ts";
import { HOLE } from "./constants.ts";

export type Stringifiers = Record<
string,
Expand Down Expand Up @@ -109,7 +109,11 @@ function serializeInner(
} else if (value instanceof RegExp) {
str += `["RegExp",${JSON.stringify(value.source)}, "${value.flags}"]`;
} else if (value instanceof Uint8Array) {
str += `["Uint8Array","${b64encode(value.buffer)}"]`;
// TODO(iuioiua): use `Uint8Array.prototype.toBase64()` once available
// in Deno (https://github.com/denoland/deno/issues/25051) and
// sufficiently supported in browsers
// (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64#browser_compatibility)
str += `["Uint8Array","${encodeBase64(value)}"]`;
} else if (value instanceof Set) {
const items = new Array(value.size);
let i = 0;
Expand Down Expand Up @@ -148,42 +152,7 @@ function serializeInner(
return idx;
}

// deno-fmt-ignore
const base64abc = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d",
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "+", "/",
];

/**
* CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
* Encodes a given Uint8Array, ArrayBuffer or string into RFC4648 base64 representation
*/
export function b64encode(buffer: ArrayBufferLike): string {
const uint8 = new Uint8Array(buffer);
let result = "",
i;
const l = uint8.length;
for (i = 2; i < l; i += 3) {
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)];
result += base64abc[uint8[i] & 0x3f];
}
if (i === l + 1) {
// 1 octet yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[(uint8[i - 2] & 0x03) << 4];
result += "==";
}
if (i === l) {
// 2 octets yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[(uint8[i - 1] & 0x0f) << 2];
result += "=";
}
return result;
function encodeBase64(bytes: Uint8Array): string {
const binary = String.fromCharCode(...bytes);
return btoa(binary);
}
45 changes: 45 additions & 0 deletions src/jsonify/stringify_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from "@std/expect";
import { stringify } from "./stringify.ts";
import { Signal, signal } from "@preact/signals";

Deno.test("stringify - object prototype", () => {
const obj = { __proto__: 123, foo: 1 };
Expand All @@ -12,3 +13,47 @@ Deno.test("stringify - throw serializing functions", () => {
const fn = () => {};
expect(() => stringify(fn)).toThrow();
});

Deno.test("custom stringify - Signals", () => {
const s = signal(2);
expect(stringify(s, {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[["Signal",1],2]',
);
});

Deno.test("custom stringify - Signals with null value", () => {
const s = signal(null);
expect(stringify(s, {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[["Signal",-2]]',
);
});

Deno.test("custom stringify - Signals with undefined value", () => {
const s = signal(undefined);
expect(stringify(s, {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[["Signal",-1]]',
);
});

Deno.test("custom stringify - referenced Signals", () => {
const s = signal(2);
expect(stringify([s, s], {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[[1,1],["Signal",2],2]',
);
});
Loading