Skip to content

Commit b3a1e96

Browse files
chore(perf): faster base64 decoding
1 parent e3c6fb8 commit b3a1e96

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/internal/utils/base64.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ export const toBase64 = (data: string | Uint8Array | null | undefined): string =
2222

2323
export const fromBase64 = (str: string): Uint8Array => {
2424
if (typeof (globalThis as any).Buffer !== 'undefined') {
25-
return new Uint8Array((globalThis as any).Buffer.from(str, 'base64'));
25+
const buf = (globalThis as any).Buffer.from(str, 'base64');
26+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
2627
}
2728

2829
if (typeof atob !== 'undefined') {
29-
return new Uint8Array(
30-
atob(str)
31-
.split('')
32-
.map((c) => c.charCodeAt(0)),
33-
);
30+
const bstr = atob(str);
31+
const buf = new Uint8Array(bstr.length);
32+
for (let i = 0; i < bstr.length; i++) {
33+
buf[i] = bstr.charCodeAt(i);
34+
}
35+
return buf;
3436
}
3537

3638
throw new GitpodError('Cannot decode base64 string; Expected `Buffer` or `atob` to be defined');

0 commit comments

Comments
 (0)