-
Notifications
You must be signed in to change notification settings - Fork 645
Expand file tree
/
Copy pathbase64.ts
More file actions
46 lines (43 loc) · 1.35 KB
/
base64.ts
File metadata and controls
46 lines (43 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
42
43
44
45
46
import { bytesToBase64 } from '@metamask/utils';
import { getBytes } from './bytes';
import type { VirtualFile } from './virtual-file';
/**
* Provides fast, asynchronous base64 encoding.
*
* @param input - The input value, assumed to be coercible to bytes.
* @returns A base64 string.
*/
export async function encodeBase64(input: Uint8Array | VirtualFile | string) {
const bytes = getBytes(input);
// In the browser, FileReader is much faster than bytesToBase64.
if ('FileReader' in globalThis && navigator?.product !== 'ReactNative') {
return await new Promise((resolve, reject) => {
const reader = Object.assign(new FileReader(), {
onload: () =>
resolve(
(reader.result as string).replace(
'data:application/octet-stream;base64,',
'',
),
),
onerror: () => reject(reader.error),
});
reader.readAsDataURL(
new File([bytes], '', { type: 'application/octet-stream' }),
);
});
}
return bytesToBase64(bytes);
}
/**
* Provides fast, asynchronous base64 decoding.
*
* @param base64 - A base64 string.
* @returns A Uint8Array of bytes.
*/
export async function decodeBase64(base64: string) {
const response = await fetch(
`data:application/octet-stream;base64,${base64}`,
);
return new Uint8Array(await response.arrayBuffer());
}