-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
base64Utils.js
36 lines (31 loc) · 1.02 KB
/
base64Utils.js
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
/**
* @license
* Copyright 2017 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
#if POLYFILL && (ENVIRONMENT_MAY_BE_SHELL || (ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000))
#include "polyfill/atob.js"
#endif
// Converts a string of base64 into a byte array (Uint8Array).
function intArrayFromBase64(s) {
#if ENVIRONMENT_MAY_BE_NODE
if (typeof ENVIRONMENT_IS_NODE != 'undefined' && ENVIRONMENT_IS_NODE) {
var buf = Buffer.from(s, 'base64');
return new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
}
#endif
var decoded = atob(s);
var bytes = new Uint8Array(decoded.length);
for (var i = 0 ; i < decoded.length ; ++i) {
bytes[i] = decoded.charCodeAt(i);
}
return bytes;
}
// If filename is a base64 data URI, parses and returns data (Buffer on node,
// Uint8Array otherwise). If filename is not a base64 data URI, returns undefined.
function tryParseAsDataURI(filename) {
if (!isDataURI(filename)) {
return;
}
return intArrayFromBase64(filename.slice(dataURIPrefix.length));
}