Skip to content

Commit 9a7b350

Browse files
authored
Move atob (ascii-to-base64) polyfill into its own file. NFC (#19606)
Also, include include it when targeting older versions of node. Browsers have always supported this API.
1 parent d6284bb commit 9a7b350

File tree

6 files changed

+62
-43
lines changed

6 files changed

+62
-43
lines changed

src/base64Utils.js

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,25 @@
1-
// Copied from https://github.com/strophe/strophejs/blob/e06d027/src/polyfills.js#L149
2-
3-
// This code was written by Tyler Akins and has been placed in the
4-
// public domain. It would be nice if you left this header intact.
5-
// Base64 code from Tyler Akins -- http://rumkin.com
6-
71
/**
8-
* Decodes a base64 string.
9-
* @param {string} input The string to decode.
2+
* @license
3+
* Copyright 2017 The Emscripten Authors
4+
* SPDX-License-Identifier: MIT
105
*/
11-
var decodeBase64 = typeof atob == 'function' ? atob : function (input) {
12-
var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
13-
14-
var output = '';
15-
var chr1, chr2, chr3;
16-
var enc1, enc2, enc3, enc4;
17-
var i = 0;
18-
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
19-
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
20-
do {
21-
enc1 = keyStr.indexOf(input.charAt(i++));
22-
enc2 = keyStr.indexOf(input.charAt(i++));
23-
enc3 = keyStr.indexOf(input.charAt(i++));
24-
enc4 = keyStr.indexOf(input.charAt(i++));
25-
26-
chr1 = (enc1 << 2) | (enc2 >> 4);
27-
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
28-
chr3 = ((enc3 & 3) << 6) | enc4;
296

30-
output = output + String.fromCharCode(chr1);
31-
32-
if (enc3 !== 64) {
33-
output = output + String.fromCharCode(chr2);
34-
}
35-
if (enc4 !== 64) {
36-
output = output + String.fromCharCode(chr3);
37-
}
38-
} while (i < input.length);
39-
return output;
40-
};
7+
#if POLYFILL && ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000
8+
#include "polyfill/atob.js"
9+
#endif
4110

4211
// Converts a string of base64 into a byte array.
4312
// Throws error on invalid input.
4413
function intArrayFromBase64(s) {
4514
#if ENVIRONMENT_MAY_BE_NODE
46-
if (typeof ENVIRONMENT_IS_NODE == 'boolean' && ENVIRONMENT_IS_NODE) {
15+
if (typeof ENVIRONMENT_IS_NODE != 'undefined' && ENVIRONMENT_IS_NODE) {
4716
var buf = Buffer.from(s, 'base64');
4817
return new Uint8Array(buf['buffer'], buf['byteOffset'], buf['byteLength']);
4918
}
5019
#endif
5120

5221
try {
53-
var decoded = decodeBase64(s);
22+
var decoded = atob(s);
5423
var bytes = new Uint8Array(decoded.length);
5524
for (var i = 0 ; i < decoded.length ; ++i) {
5625
bytes[i] = decoded.charCodeAt(i);

src/polyfill/atob.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copied from https://github.com/strophe/strophejs/blob/e06d027/src/polyfills.js#L149
2+
3+
// This code was written by Tyler Akins and has been placed in the
4+
// public domain. It would be nice if you left this header intact.
5+
// Base64 code from Tyler Akins -- http://rumkin.com
6+
7+
#if !POLYFILL
8+
#error "this file should never be included unless POLYFILL is set"
9+
#endif
10+
11+
#if !ENVIRONMENT_MAY_BE_NODE
12+
#error "this polyfill should only be included when targetting node"
13+
#endif
14+
15+
if (typeof ENVIRONMENT_IS_NODE != 'undefined' && ENVIRONMENT_IS_NODE && !global.atob) {
16+
/**
17+
* Decodes a base64 string.
18+
* @param {string} input The string to decode.
19+
*/
20+
global.atob = function(input) {
21+
var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
22+
23+
var output = '';
24+
var chr1, chr2, chr3;
25+
var enc1, enc2, enc3, enc4;
26+
var i = 0;
27+
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
28+
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
29+
do {
30+
enc1 = keyStr.indexOf(input.charAt(i++));
31+
enc2 = keyStr.indexOf(input.charAt(i++));
32+
enc3 = keyStr.indexOf(input.charAt(i++));
33+
enc4 = keyStr.indexOf(input.charAt(i++));
34+
35+
chr1 = (enc1 << 2) | (enc2 >> 4);
36+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
37+
chr3 = ((enc3 & 3) << 6) | enc4;
38+
39+
output = output + String.fromCharCode(chr1);
40+
41+
if (enc3 !== 64) {
42+
output = output + String.fromCharCode(chr2);
43+
}
44+
if (enc4 !== 64) {
45+
output = output + String.fromCharCode(chr3);
46+
}
47+
} while (i < input.length);
48+
return output;
49+
};
50+
}

src/polyfill/bigint64array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#if !POLYFILL
2-
assert(false, "this file should never be included unless POLYFILL is set");
2+
#error "this file should never be included unless POLYFILL is set"
33
#endif
44

55
if (typeof globalThis.BigInt64Array === "undefined") {

src/polyfill/objassign.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/js/es6/util/assign.js
33

44
#if !POLYFILL
5-
assert(false, "this file should never be included unless POLYFILL is set");
5+
#error "this file should never be included unless POLYFILL is set"
66
#endif
77

88
if (typeof Object.assign == 'undefined') {

src/polyfill/promise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//==============================================================================
2525

2626
#if !POLYFILL
27-
assert(false, "this file should never be included unless POLYFILL is set");
27+
#error "this file should never be included unless POLYFILL is set"
2828
#endif
2929

3030
/** @suppress{duplicate} This is already defined in from Closure's built-in

tools/file_packager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ def generate_js(data_target, data_files, metadata):
682682
data = base64_encode(utils.read_binary(file_.srcpath))
683683
code += " var fileData%d = '%s';\n" % (counter, data)
684684
# canOwn this data in the filesystem (i.e. there is no need to create a copy in the FS layer).
685-
code += (" Module['FS_createDataFile']('%s', '%s', decodeBase64(fileData%d), true, true, true);\n"
685+
code += (" Module['FS_createDataFile']('%s', '%s', atob(fileData%d), true, true, true);\n"
686686
% (dirname, basename, counter))
687687
elif file_.mode == 'preload':
688688
# Preload

0 commit comments

Comments
 (0)