Skip to content

Commit 06529d4

Browse files
committed
tweaks
1 parent b0bf39f commit 06529d4

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/jsonify/parse.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { decodeBase64 } from "@std/encoding/base64";
21
import {
32
HOLE,
43
INFINITY_NEG,
@@ -92,7 +91,9 @@ function unpack(
9291
}
9392
case "Uint8Array":
9493
// TODO(iuioiua): use `Uint8Array.prototype.fromBase64()` once
95-
// available (https://github.com/denoland/deno/issues/25051)
94+
// available in Deno (https://github.com/denoland/deno/issues/25051)
95+
// and sufficiently supported in browsers
96+
// (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64#browser_compatibility)
9697
return hydrated[idx] = decodeBase64(current[1]);
9798
}
9899
} else {
@@ -121,3 +122,12 @@ function unpack(
121122
return actual;
122123
}
123124
}
125+
126+
function decodeBase64(base64: string): Uint8Array {
127+
const binary = atob(base64);
128+
const bytes = new Uint8Array(binary.length);
129+
for (let i = 0; i < binary.length; i++) {
130+
bytes[i] = binary.charCodeAt(i);
131+
}
132+
return bytes;
133+
}

src/jsonify/stringify.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { encodeBase64 } from "@std/encoding/base64";
21
import {
32
HOLE,
43
INFINITY_NEG,
@@ -111,7 +110,9 @@ function serializeInner(
111110
str += `["RegExp",${JSON.stringify(value.source)}, "${value.flags}"]`;
112111
} else if (value instanceof Uint8Array) {
113112
// TODO(iuioiua): use `Uint8Array.prototype.toBase64()` once available
114-
// (https://github.com/denoland/deno/issues/25051)
113+
// in Deno (https://github.com/denoland/deno/issues/25051) and
114+
// sufficiently supported in browsers
115+
// (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64#browser_compatibility)
115116
str += `["Uint8Array","${encodeBase64(value)}"]`;
116117
} else if (value instanceof Set) {
117118
const items = new Array(value.size);
@@ -150,3 +151,8 @@ function serializeInner(
150151
out[idx] = str;
151152
return idx;
152153
}
154+
155+
function encodeBase64(bytes: Uint8Array): string {
156+
const binary = String.fromCharCode(...bytes);
157+
return btoa(binary);
158+
}

0 commit comments

Comments
 (0)