-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.js
More file actions
49 lines (43 loc) · 1.04 KB
/
base64.js
File metadata and controls
49 lines (43 loc) · 1.04 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
47
48
49
import { Base64 } from 'js-base64';
let moduleAvailable = true;
try {
if (Base64 === undefined) {
moduleAvailable = false;
}
} catch (e) {
moduleAvailable = false;
}
function _btoa(d) {
if (window?.btoa) {
return window.btoa(d);
} else if (moduleAvailable) {
return Base64.btoa(d);
} else {
throw new Error("No Base64 API detected or usable");
}
}
function _atob(d) {
if (window?.atob) {
return window.atob(d);
} else if (moduleAvailable) {
return Base64.atob(d);
} else {
throw new Error("No Base64 API detected or usable");
}
}
export function uencode(u) {
if (moduleAvailable) {
return Base64.fromUint8Array(u);
} else {
return _btoa(String.fromCharCode(...u));
}
}
export function udecode(b, t = Uint8Array) {
if (moduleAvailable && t === Uint8Array) {
return Base64.toUint8Array(b);
} else {
return t.from(_atob(b), c => c.charCodeAt(0));
}
}
export const encode = _btoa;
export const decode = _atob;