Skip to content

Commit 800a42b

Browse files
committed
refactor(core): cleanup jsonify code
1 parent cea7a2a commit 800a42b

7 files changed

Lines changed: 52 additions & 159 deletions

File tree

src/jsonify/__snapshots__/round_trip_test.ts.snap

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/jsonify/parse.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { decodeBase64 } from "@std/encoding/base64";
12
import {
23
HOLE,
34
INFINITY_NEG,
@@ -90,7 +91,7 @@ function unpack(
9091
return hydrated[idx] = set;
9192
}
9293
case "Uint8Array":
93-
return hydrated[idx] = b64decode(current[1]);
94+
return hydrated[idx] = decodeBase64(current[1]);
9495
}
9596
} else {
9697
const actual = new Array(current.length);
@@ -118,13 +119,3 @@ function unpack(
118119
return actual;
119120
}
120121
}
121-
122-
function b64decode(b64: string): Uint8Array {
123-
const binString = atob(b64);
124-
const size = binString.length;
125-
const bytes = new Uint8Array(size);
126-
for (let i = 0; i < size; i++) {
127-
bytes[i] = binString.charCodeAt(i);
128-
}
129-
return bytes;
130-
}
Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ Deno.test("custom parse - Signals", () => {
3232
expect(res.peek()).toEqual(2);
3333
});
3434

35-
Deno.test("custom stringify - Signals", () => {
36-
const s = signal(2);
37-
expect(stringify(s, {
38-
Signal: (s2: unknown) => {
39-
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
40-
},
41-
})).toEqual(
42-
'[["Signal",1],2]',
43-
);
44-
});
45-
4635
Deno.test("custom parse - Signals with null value", () => {
4736
const res = parse<Signal>('[["Signal",-2]]', {
4837
Signal: (value) => signal(value),
@@ -51,43 +40,10 @@ Deno.test("custom parse - Signals with null value", () => {
5140
expect(res.peek()).toEqual(null);
5241
});
5342

54-
Deno.test("custom stringify - Signals with null value", () => {
55-
const s = signal(null);
56-
expect(stringify(s, {
57-
Signal: (s2: unknown) => {
58-
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
59-
},
60-
})).toEqual(
61-
'[["Signal",-2]]',
62-
);
63-
});
64-
6543
Deno.test("custom parse - Signals with undefined value", () => {
6644
const res = parse<Signal>('[["Signal",-1]]', {
6745
Signal: (value) => signal(value),
6846
});
6947
expect(res).toBeInstanceOf(Signal);
7048
expect(res.peek()).toEqual(undefined);
7149
});
72-
73-
Deno.test("custom stringify - Signals with undefined value", () => {
74-
const s = signal(undefined);
75-
expect(stringify(s, {
76-
Signal: (s2: unknown) => {
77-
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
78-
},
79-
})).toEqual(
80-
'[["Signal",-1]]',
81-
);
82-
});
83-
84-
Deno.test("custom stringify - referenced Signals", () => {
85-
const s = signal(2);
86-
expect(stringify([s, s], {
87-
Signal: (s2: unknown) => {
88-
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
89-
},
90-
})).toEqual(
91-
'[[1,1],["Signal",2],2]',
92-
);
93-
});

src/jsonify/round_trip_test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { expect } from "@std/expect";
22
import { parse } from "./parse.ts";
3-
import { assertSnapshot } from "@std/testing/snapshot";
4-
import { inspect } from "node:util";
53
import { stringify } from "./stringify.ts";
64

75
const inner = { foo: 123 };
@@ -42,9 +40,8 @@ const TESTS = [
4240
];
4341

4442
for (const value of TESTS) {
45-
Deno.test(`round trip - ${inspect(value)}`, async (t) => {
43+
Deno.test(`round trip - ${Deno.inspect(value)}`, () => {
4644
const str = stringify(value);
47-
await assertSnapshot(t, str);
4845
const parsed = parse(str);
4946
expect(parsed).toEqual(value);
5047
});

src/jsonify/stringify.ts

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { encodeBase64 } from "@std/encoding/base64";
12
import {
3+
HOLE,
24
INFINITY_NEG,
35
INFINITY_POS,
46
NAN,
57
NULL,
68
UNDEFINED,
79
ZERO_NEG,
810
} from "./constants.ts";
9-
import { HOLE } from "./constants.ts";
1011

1112
export type Stringifiers = Record<
1213
string,
@@ -109,7 +110,7 @@ function serializeInner(
109110
} else if (value instanceof RegExp) {
110111
str += `["RegExp",${JSON.stringify(value.source)}, "${value.flags}"]`;
111112
} else if (value instanceof Uint8Array) {
112-
str += `["Uint8Array","${b64encode(value.buffer)}"]`;
113+
str += `["Uint8Array","${encodeBase64(value)}"]`;
113114
} else if (value instanceof Set) {
114115
const items = new Array(value.size);
115116
let i = 0;
@@ -147,43 +148,3 @@ function serializeInner(
147148
out[idx] = str;
148149
return idx;
149150
}
150-
151-
// deno-fmt-ignore
152-
const base64abc = [
153-
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
154-
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d",
155-
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
156-
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
157-
"8", "9", "+", "/",
158-
];
159-
160-
/**
161-
* CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
162-
* Encodes a given Uint8Array, ArrayBuffer or string into RFC4648 base64 representation
163-
*/
164-
export function b64encode(buffer: ArrayBufferLike): string {
165-
const uint8 = new Uint8Array(buffer);
166-
let result = "",
167-
i;
168-
const l = uint8.length;
169-
for (i = 2; i < l; i += 3) {
170-
result += base64abc[uint8[i - 2] >> 2];
171-
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
172-
result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)];
173-
result += base64abc[uint8[i] & 0x3f];
174-
}
175-
if (i === l + 1) {
176-
// 1 octet yet to write
177-
result += base64abc[uint8[i - 2] >> 2];
178-
result += base64abc[(uint8[i - 2] & 0x03) << 4];
179-
result += "==";
180-
}
181-
if (i === l) {
182-
// 2 octets yet to write
183-
result += base64abc[uint8[i - 2] >> 2];
184-
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
185-
result += base64abc[(uint8[i - 1] & 0x0f) << 2];
186-
result += "=";
187-
}
188-
return result;
189-
}

src/jsonify/stringify_test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from "@std/expect";
22
import { stringify } from "./stringify.ts";
3+
import { Signal, signal } from "@preact/signals";
34

45
Deno.test("stringify - object prototype", () => {
56
const obj = { __proto__: 123, foo: 1 };
@@ -12,3 +13,47 @@ Deno.test("stringify - throw serializing functions", () => {
1213
const fn = () => {};
1314
expect(() => stringify(fn)).toThrow();
1415
});
16+
17+
Deno.test("custom stringify - Signals", () => {
18+
const s = signal(2);
19+
expect(stringify(s, {
20+
Signal: (s2: unknown) => {
21+
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
22+
},
23+
})).toEqual(
24+
'[["Signal",1],2]',
25+
);
26+
});
27+
28+
Deno.test("custom stringify - Signals with null value", () => {
29+
const s = signal(null);
30+
expect(stringify(s, {
31+
Signal: (s2: unknown) => {
32+
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
33+
},
34+
})).toEqual(
35+
'[["Signal",-2]]',
36+
);
37+
});
38+
39+
Deno.test("custom stringify - Signals with undefined value", () => {
40+
const s = signal(undefined);
41+
expect(stringify(s, {
42+
Signal: (s2: unknown) => {
43+
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
44+
},
45+
})).toEqual(
46+
'[["Signal",-1]]',
47+
);
48+
});
49+
50+
Deno.test("custom stringify - referenced Signals", () => {
51+
const s = signal(2);
52+
expect(stringify([s, s], {
53+
Signal: (s2: unknown) => {
54+
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
55+
},
56+
})).toEqual(
57+
'[[1,1],["Signal",2],2]',
58+
);
59+
});

src/runtime/server/preact_hooks.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,7 @@ function FreshRuntimeScript() {
474474
.join(",") +
475475
"}";
476476

477-
const serializedProps = JSON.stringify(
478-
stringify(islandProps, stringifiers),
479-
);
477+
const serializedProps = stringify(islandProps, stringifiers);
480478

481479
const runtimeUrl = `${basePath}/_fresh/js/${BUILD_ID}/fresh-runtime.js`;
482480
const scriptContent =

0 commit comments

Comments
 (0)