js-base64 3.6.1 → 3.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![build status](https://secure.travis-ci.org/dankogai/js-base64.png)](http://travis-ci.org/dankogai/js-base64)
1
+ [![build status](https://app.travis-ci.com/dankogai/js-base64.svg)](https://app.travis-ci.com/github/dankogai/js-base64)
2
2
 
3
3
  # base64.js
4
4
 
@@ -6,10 +6,6 @@ Yet another [Base64] transcoder.
6
6
 
7
7
  [Base64]: http://en.wikipedia.org/wiki/Base64
8
8
 
9
- ## HEADS UP
10
-
11
- In version 3.0 `js-base64` switch to ES2015 module so it is no longer compatible with legacy browsers like IE (see below). And since version 3.3 it is written in TypeScript. Now `base64.mjs` is compiled from `base64.ts` then `base64.js` is generated from `base64.mjs`.
12
-
13
9
  ## Install
14
10
 
15
11
  ```shell
@@ -29,7 +25,7 @@ Locally…
29
25
  … or Directly from CDN. In which case you don't even need to install.
30
26
 
31
27
  ```html
32
- <script src="https://cdn.jsdelivr.net/npm/js-base64@3.6.1/base64.min.js"></script>
28
+ <script src="https://cdn.jsdelivr.net/npm/js-base64@3.7.2/base64.min.js"></script>
33
29
  ```
34
30
 
35
31
  This good old way loads `Base64` in the global context (`window`). Though `Base64.noConflict()` is made available, you should consider using ES6 Module to avoid tainting `window`.
@@ -52,14 +48,14 @@ or even remotely.
52
48
  ```html
53
49
  <script type="module">
54
50
  // note jsdelivr.net does not automatically minify .mjs
55
- import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.6.1/base64.mjs';
51
+ import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.7.2/base64.mjs';
56
52
  </script>
57
53
  ```
58
54
 
59
55
  ```html
60
56
  <script type="module">
61
57
  // or if you prefer no Base64 namespace
62
- import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.6.1/base64.mjs';
58
+ import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.7.2/base64.mjs';
63
59
  </script>
64
60
  ```
65
61
 
@@ -140,8 +136,8 @@ Base64.extendString();
140
136
  ```
141
137
 
142
138
  ```javascript
143
- // you have to explicitly extend String.prototype
144
- Base64.extendString();
139
+ // you have to explicitly extend Uint8Array.prototype
140
+ Base64.extendUint8Array();
145
141
  // once extended, you can do the following
146
142
  u8s.toBase64(); // 'ZGFua29nYWk='
147
143
  u8s.toBase64URI(); // 'ZGFua29nYWk'
@@ -168,8 +164,14 @@ Or even better, `Base64.toUint8Array(pngBase64)`.
168
164
 
169
165
  ### If you really, really need an ES5 version
170
166
 
171
- You can transpiles to an ES5 that runs on IE11. Do the following in your shell.
167
+ You can transpiles to an ES5 that runs on IEs before 11. Do the following in your shell.
172
168
 
173
169
  ```shell
174
170
  $ make base64.es5.js
175
171
  ```
172
+
173
+ ## Brief History
174
+
175
+ * Since version 3.3 it is written in TypeScript. Now `base64.mjs` is compiled from `base64.ts` then `base64.js` is generated from `base64.mjs`.
176
+ * Since version 3.7 `base64.js` is ES5-compatible again (hence IE11-compabile).
177
+ * Since 3.0 `js-base64` switch to ES2015 module so it is no longer compatible with legacy browsers like IE (see above)
package/base64.d.ts CHANGED
@@ -9,11 +9,11 @@
9
9
  *
10
10
  * @author Dan Kogai (https://github.com/dankogai)
11
11
  */
12
- declare const version = "3.6.1";
12
+ declare const version = "3.7.2";
13
13
  /**
14
14
  * @deprecated use lowercase `version`.
15
15
  */
16
- declare const VERSION = "3.6.1";
16
+ declare const VERSION = "3.7.2";
17
17
  /**
18
18
  * polyfill version of `btoa`
19
19
  */
package/base64.js CHANGED
@@ -1,317 +1,319 @@
1
-
2
-
3
1
  //
4
2
  // THIS FILE IS AUTOMATICALLY GENERATED! DO NOT EDIT BY HAND!
5
3
  //
6
- ;(function(global, factory) {
4
+ ;
5
+ (function (global, factory) {
7
6
  typeof exports === 'object' && typeof module !== 'undefined'
8
7
  ? module.exports = factory()
9
8
  : typeof define === 'function' && define.amd
10
- ? define(factory) :
11
- // cf. https://github.com/dankogai/js-base64/issues/119
12
- (function() {
13
- // existing version for noConflict()
14
- const _Base64 = global.Base64;
15
- const gBase64 = factory();
16
- gBase64.noConflict = () => {
17
- global.Base64 = _Base64;
18
- return gBase64;
19
- };
20
- if (global.Meteor) { // Meteor.js
21
- Base64 = gBase64;
22
- }
23
- global.Base64 = gBase64;
24
- })();
9
+ ? define(factory) :
10
+ // cf. https://github.com/dankogai/js-base64/issues/119
11
+ (function () {
12
+ // existing version for noConflict()
13
+ var _Base64 = global.Base64;
14
+ var gBase64 = factory();
15
+ gBase64.noConflict = function () {
16
+ global.Base64 = _Base64;
17
+ return gBase64;
18
+ };
19
+ if (global.Meteor) { // Meteor.js
20
+ Base64 = gBase64;
21
+ }
22
+ global.Base64 = gBase64;
23
+ })();
25
24
  }((typeof self !== 'undefined' ? self
26
- : typeof window !== 'undefined' ? window
25
+ : typeof window !== 'undefined' ? window
27
26
  : typeof global !== 'undefined' ? global
28
- : this
29
- ), function() {
27
+ : this), function () {
30
28
  'use strict';
31
-
32
- /**
33
- * base64.ts
34
- *
35
- * Licensed under the BSD 3-Clause License.
36
- * http://opensource.org/licenses/BSD-3-Clause
37
- *
38
- * References:
39
- * http://en.wikipedia.org/wiki/Base64
40
- *
41
- * @author Dan Kogai (https://github.com/dankogai)
42
- */
43
- const version = '3.6.1';
44
- /**
45
- * @deprecated use lowercase `version`.
46
- */
47
- const VERSION = version;
48
- const _hasatob = typeof atob === 'function';
49
- const _hasbtoa = typeof btoa === 'function';
50
- const _hasBuffer = typeof Buffer === 'function';
51
- const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
52
- const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
53
- const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
54
- const b64chs = [...b64ch];
55
- const b64tab = ((a) => {
56
- let tab = {};
57
- a.forEach((c, i) => tab[c] = i);
58
- return tab;
59
- })(b64chs);
60
- const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
61
- const _fromCC = String.fromCharCode.bind(String);
62
- const _U8Afrom = typeof Uint8Array.from === 'function'
63
- ? Uint8Array.from.bind(Uint8Array)
64
- : (it, fn = (x) => x) => new Uint8Array(Array.prototype.slice.call(it, 0).map(fn));
65
- const _mkUriSafe = (src) => src
66
- .replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_')
67
- .replace(/=+$/m, '');
68
- const _tidyB64 = (s) => s.replace(/[^A-Za-z0-9\+\/]/g, '');
69
- /**
70
- * polyfill version of `btoa`
71
- */
72
- const btoaPolyfill = (bin) => {
73
- // console.log('polyfilled');
74
- let u32, c0, c1, c2, asc = '';
75
- const pad = bin.length % 3;
76
- for (let i = 0; i < bin.length;) {
77
- if ((c0 = bin.charCodeAt(i++)) > 255 ||
78
- (c1 = bin.charCodeAt(i++)) > 255 ||
79
- (c2 = bin.charCodeAt(i++)) > 255)
80
- throw new TypeError('invalid character found');
81
- u32 = (c0 << 16) | (c1 << 8) | c2;
82
- asc += b64chs[u32 >> 18 & 63]
83
- + b64chs[u32 >> 12 & 63]
84
- + b64chs[u32 >> 6 & 63]
85
- + b64chs[u32 & 63];
86
- }
87
- return pad ? asc.slice(0, pad - 3) + "===".substring(pad) : asc;
88
- };
89
- /**
90
- * does what `window.btoa` of web browsers do.
91
- * @param {String} bin binary string
92
- * @returns {string} Base64-encoded string
93
- */
94
- const _btoa = _hasbtoa ? (bin) => btoa(bin)
95
- : _hasBuffer ? (bin) => Buffer.from(bin, 'binary').toString('base64')
96
- : btoaPolyfill;
97
- const _fromUint8Array = _hasBuffer
98
- ? (u8a) => Buffer.from(u8a).toString('base64')
99
- : (u8a) => {
100
- // cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
101
- const maxargs = 0x1000;
102
- let strs = [];
103
- for (let i = 0, l = u8a.length; i < l; i += maxargs) {
104
- strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
29
+ /**
30
+ * base64.ts
31
+ *
32
+ * Licensed under the BSD 3-Clause License.
33
+ * http://opensource.org/licenses/BSD-3-Clause
34
+ *
35
+ * References:
36
+ * http://en.wikipedia.org/wiki/Base64
37
+ *
38
+ * @author Dan Kogai (https://github.com/dankogai)
39
+ */
40
+ var version = '3.7.2';
41
+ /**
42
+ * @deprecated use lowercase `version`.
43
+ */
44
+ var VERSION = version;
45
+ var _hasatob = typeof atob === 'function';
46
+ var _hasbtoa = typeof btoa === 'function';
47
+ var _hasBuffer = typeof Buffer === 'function';
48
+ var _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
49
+ var _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
50
+ var b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
51
+ var b64chs = Array.prototype.slice.call(b64ch);
52
+ var b64tab = (function (a) {
53
+ var tab = {};
54
+ a.forEach(function (c, i) { return tab[c] = i; });
55
+ return tab;
56
+ })(b64chs);
57
+ var b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
58
+ var _fromCC = String.fromCharCode.bind(String);
59
+ var _U8Afrom = typeof Uint8Array.from === 'function'
60
+ ? Uint8Array.from.bind(Uint8Array)
61
+ : function (it, fn) {
62
+ if (fn === void 0) { fn = function (x) { return x; }; }
63
+ return new Uint8Array(Array.prototype.slice.call(it, 0).map(fn));
64
+ };
65
+ var _mkUriSafe = function (src) { return src
66
+ .replace(/=/g, '').replace(/[+\/]/g, function (m0) { return m0 == '+' ? '-' : '_'; }); };
67
+ var _tidyB64 = function (s) { return s.replace(/[^A-Za-z0-9\+\/]/g, ''); };
68
+ /**
69
+ * polyfill version of `btoa`
70
+ */
71
+ var btoaPolyfill = function (bin) {
72
+ // console.log('polyfilled');
73
+ var u32, c0, c1, c2, asc = '';
74
+ var pad = bin.length % 3;
75
+ for (var i = 0; i < bin.length;) {
76
+ if ((c0 = bin.charCodeAt(i++)) > 255 ||
77
+ (c1 = bin.charCodeAt(i++)) > 255 ||
78
+ (c2 = bin.charCodeAt(i++)) > 255)
79
+ throw new TypeError('invalid character found');
80
+ u32 = (c0 << 16) | (c1 << 8) | c2;
81
+ asc += b64chs[u32 >> 18 & 63]
82
+ + b64chs[u32 >> 12 & 63]
83
+ + b64chs[u32 >> 6 & 63]
84
+ + b64chs[u32 & 63];
105
85
  }
106
- return _btoa(strs.join(''));
86
+ return pad ? asc.slice(0, pad - 3) + "===".substring(pad) : asc;
107
87
  };
108
- /**
109
- * converts a Uint8Array to a Base64 string.
110
- * @param {boolean} [urlsafe] URL-and-filename-safe a la RFC4648 §5
111
- * @returns {string} Base64 string
112
- */
113
- const fromUint8Array = (u8a, urlsafe = false) => urlsafe ? _mkUriSafe(_fromUint8Array(u8a)) : _fromUint8Array(u8a);
114
- // This trick is found broken https://github.com/dankogai/js-base64/issues/130
115
- // const utob = (src: string) => unescape(encodeURIComponent(src));
116
- // reverting good old fationed regexp
117
- const cb_utob = (c) => {
118
- if (c.length < 2) {
119
- var cc = c.charCodeAt(0);
120
- return cc < 0x80 ? c
121
- : cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
122
- + _fromCC(0x80 | (cc & 0x3f)))
123
- : (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
124
- + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
125
- + _fromCC(0x80 | (cc & 0x3f)));
126
- }
127
- else {
128
- var cc = 0x10000
129
- + (c.charCodeAt(0) - 0xD800) * 0x400
130
- + (c.charCodeAt(1) - 0xDC00);
131
- return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
132
- + _fromCC(0x80 | ((cc >>> 12) & 0x3f))
133
- + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
134
- + _fromCC(0x80 | (cc & 0x3f)));
135
- }
136
- };
137
- const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
138
- /**
139
- * @deprecated should have been internal use only.
140
- * @param {string} src UTF-8 string
141
- * @returns {string} UTF-16 string
142
- */
143
- const utob = (u) => u.replace(re_utob, cb_utob);
144
- //
145
- const _encode = _hasBuffer
146
- ? (s) => Buffer.from(s, 'utf8').toString('base64')
147
- : _TE
148
- ? (s) => _fromUint8Array(_TE.encode(s))
149
- : (s) => _btoa(utob(s));
150
- /**
151
- * converts a UTF-8-encoded string to a Base64 string.
152
- * @param {boolean} [urlsafe] if `true` make the result URL-safe
153
- * @returns {string} Base64 string
154
- */
155
- const encode = (src, urlsafe = false) => urlsafe
156
- ? _mkUriSafe(_encode(src))
157
- : _encode(src);
158
- /**
159
- * converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5.
160
- * @returns {string} Base64 string
161
- */
162
- const encodeURI = (src) => encode(src, true);
163
- // This trick is found broken https://github.com/dankogai/js-base64/issues/130
164
- // const btou = (src: string) => decodeURIComponent(escape(src));
165
- // reverting good old fationed regexp
166
- const re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
167
- const cb_btou = (cccc) => {
168
- switch (cccc.length) {
169
- case 4:
170
- var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
171
- | ((0x3f & cccc.charCodeAt(1)) << 12)
172
- | ((0x3f & cccc.charCodeAt(2)) << 6)
173
- | (0x3f & cccc.charCodeAt(3)), offset = cp - 0x10000;
174
- return (_fromCC((offset >>> 10) + 0xD800)
175
- + _fromCC((offset & 0x3FF) + 0xDC00));
176
- case 3:
177
- return _fromCC(((0x0f & cccc.charCodeAt(0)) << 12)
178
- | ((0x3f & cccc.charCodeAt(1)) << 6)
179
- | (0x3f & cccc.charCodeAt(2)));
180
- default:
181
- return _fromCC(((0x1f & cccc.charCodeAt(0)) << 6)
182
- | (0x3f & cccc.charCodeAt(1)));
183
- }
184
- };
185
- /**
186
- * @deprecated should have been internal use only.
187
- * @param {string} src UTF-16 string
188
- * @returns {string} UTF-8 string
189
- */
190
- const btou = (b) => b.replace(re_btou, cb_btou);
191
- /**
192
- * polyfill version of `atob`
193
- */
194
- const atobPolyfill = (asc) => {
195
- // console.log('polyfilled');
196
- asc = asc.replace(/\s+/g, '');
197
- if (!b64re.test(asc))
198
- throw new TypeError('malformed base64.');
199
- asc += '=='.slice(2 - (asc.length & 3));
200
- let u24, bin = '', r1, r2;
201
- for (let i = 0; i < asc.length;) {
202
- u24 = b64tab[asc.charAt(i++)] << 18
203
- | b64tab[asc.charAt(i++)] << 12
204
- | (r1 = b64tab[asc.charAt(i++)]) << 6
205
- | (r2 = b64tab[asc.charAt(i++)]);
206
- bin += r1 === 64 ? _fromCC(u24 >> 16 & 255)
207
- : r2 === 64 ? _fromCC(u24 >> 16 & 255, u24 >> 8 & 255)
208
- : _fromCC(u24 >> 16 & 255, u24 >> 8 & 255, u24 & 255);
209
- }
210
- return bin;
211
- };
212
- /**
213
- * does what `window.atob` of web browsers do.
214
- * @param {String} asc Base64-encoded string
215
- * @returns {string} binary string
216
- */
217
- const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
218
- : _hasBuffer ? (asc) => Buffer.from(asc, 'base64').toString('binary')
219
- : atobPolyfill;
220
- //
221
- const _toUint8Array = _hasBuffer
222
- ? (a) => _U8Afrom(Buffer.from(a, 'base64'))
223
- : (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
224
- /**
225
- * converts a Base64 string to a Uint8Array.
226
- */
227
- const toUint8Array = (a) => _toUint8Array(_unURI(a));
228
- //
229
- const _decode = _hasBuffer
230
- ? (a) => Buffer.from(a, 'base64').toString('utf8')
231
- : _TD
232
- ? (a) => _TD.decode(_toUint8Array(a))
233
- : (a) => btou(_atob(a));
234
- const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
235
- /**
236
- * converts a Base64 string to a UTF-8 string.
237
- * @param {String} src Base64 string. Both normal and URL-safe are supported
238
- * @returns {string} UTF-8 string
239
- */
240
- const decode = (src) => _decode(_unURI(src));
241
- /**
242
- * check if a value is a valid Base64 string
243
- * @param {String} src a value to check
244
- */
245
- const isValid = (src) => {
246
- if (typeof src !== 'string')
247
- return false;
248
- const s = src.replace(/\s+/g, '').replace(/=+$/, '');
249
- return !/[^\s0-9a-zA-Z\+/]/.test(s) || !/[^\s0-9a-zA-Z\-_]/.test(s);
250
- };
251
- //
252
- const _noEnum = (v) => {
253
- return {
254
- value: v, enumerable: false, writable: true, configurable: true
88
+ /**
89
+ * does what `window.btoa` of web browsers do.
90
+ * @param {String} bin binary string
91
+ * @returns {string} Base64-encoded string
92
+ */
93
+ var _btoa = _hasbtoa ? function (bin) { return btoa(bin); }
94
+ : _hasBuffer ? function (bin) { return Buffer.from(bin, 'binary').toString('base64'); }
95
+ : btoaPolyfill;
96
+ var _fromUint8Array = _hasBuffer
97
+ ? function (u8a) { return Buffer.from(u8a).toString('base64'); }
98
+ : function (u8a) {
99
+ // cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
100
+ var maxargs = 0x1000;
101
+ var strs = [];
102
+ for (var i = 0, l = u8a.length; i < l; i += maxargs) {
103
+ strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
104
+ }
105
+ return _btoa(strs.join(''));
106
+ };
107
+ /**
108
+ * converts a Uint8Array to a Base64 string.
109
+ * @param {boolean} [urlsafe] URL-and-filename-safe a la RFC4648 §5
110
+ * @returns {string} Base64 string
111
+ */
112
+ var fromUint8Array = function (u8a, urlsafe) {
113
+ if (urlsafe === void 0) { urlsafe = false; }
114
+ return urlsafe ? _mkUriSafe(_fromUint8Array(u8a)) : _fromUint8Array(u8a);
115
+ };
116
+ // This trick is found broken https://github.com/dankogai/js-base64/issues/130
117
+ // const utob = (src: string) => unescape(encodeURIComponent(src));
118
+ // reverting good old fationed regexp
119
+ var cb_utob = function (c) {
120
+ if (c.length < 2) {
121
+ var cc = c.charCodeAt(0);
122
+ return cc < 0x80 ? c
123
+ : cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
124
+ + _fromCC(0x80 | (cc & 0x3f)))
125
+ : (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
126
+ + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
127
+ + _fromCC(0x80 | (cc & 0x3f)));
128
+ }
129
+ else {
130
+ var cc = 0x10000
131
+ + (c.charCodeAt(0) - 0xD800) * 0x400
132
+ + (c.charCodeAt(1) - 0xDC00);
133
+ return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
134
+ + _fromCC(0x80 | ((cc >>> 12) & 0x3f))
135
+ + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
136
+ + _fromCC(0x80 | (cc & 0x3f)));
137
+ }
138
+ };
139
+ var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
140
+ /**
141
+ * @deprecated should have been internal use only.
142
+ * @param {string} src UTF-8 string
143
+ * @returns {string} UTF-16 string
144
+ */
145
+ var utob = function (u) { return u.replace(re_utob, cb_utob); };
146
+ //
147
+ var _encode = _hasBuffer
148
+ ? function (s) { return Buffer.from(s, 'utf8').toString('base64'); }
149
+ : _TE
150
+ ? function (s) { return _fromUint8Array(_TE.encode(s)); }
151
+ : function (s) { return _btoa(utob(s)); };
152
+ /**
153
+ * converts a UTF-8-encoded string to a Base64 string.
154
+ * @param {boolean} [urlsafe] if `true` make the result URL-safe
155
+ * @returns {string} Base64 string
156
+ */
157
+ var encode = function (src, urlsafe) {
158
+ if (urlsafe === void 0) { urlsafe = false; }
159
+ return urlsafe
160
+ ? _mkUriSafe(_encode(src))
161
+ : _encode(src);
162
+ };
163
+ /**
164
+ * converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5.
165
+ * @returns {string} Base64 string
166
+ */
167
+ var encodeURI = function (src) { return encode(src, true); };
168
+ // This trick is found broken https://github.com/dankogai/js-base64/issues/130
169
+ // const btou = (src: string) => decodeURIComponent(escape(src));
170
+ // reverting good old fationed regexp
171
+ var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
172
+ var cb_btou = function (cccc) {
173
+ switch (cccc.length) {
174
+ case 4:
175
+ var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
176
+ | ((0x3f & cccc.charCodeAt(1)) << 12)
177
+ | ((0x3f & cccc.charCodeAt(2)) << 6)
178
+ | (0x3f & cccc.charCodeAt(3)), offset = cp - 0x10000;
179
+ return (_fromCC((offset >>> 10) + 0xD800)
180
+ + _fromCC((offset & 0x3FF) + 0xDC00));
181
+ case 3:
182
+ return _fromCC(((0x0f & cccc.charCodeAt(0)) << 12)
183
+ | ((0x3f & cccc.charCodeAt(1)) << 6)
184
+ | (0x3f & cccc.charCodeAt(2)));
185
+ default:
186
+ return _fromCC(((0x1f & cccc.charCodeAt(0)) << 6)
187
+ | (0x3f & cccc.charCodeAt(1)));
188
+ }
189
+ };
190
+ /**
191
+ * @deprecated should have been internal use only.
192
+ * @param {string} src UTF-16 string
193
+ * @returns {string} UTF-8 string
194
+ */
195
+ var btou = function (b) { return b.replace(re_btou, cb_btou); };
196
+ /**
197
+ * polyfill version of `atob`
198
+ */
199
+ var atobPolyfill = function (asc) {
200
+ // console.log('polyfilled');
201
+ asc = asc.replace(/\s+/g, '');
202
+ if (!b64re.test(asc))
203
+ throw new TypeError('malformed base64.');
204
+ asc += '=='.slice(2 - (asc.length & 3));
205
+ var u24, bin = '', r1, r2;
206
+ for (var i = 0; i < asc.length;) {
207
+ u24 = b64tab[asc.charAt(i++)] << 18
208
+ | b64tab[asc.charAt(i++)] << 12
209
+ | (r1 = b64tab[asc.charAt(i++)]) << 6
210
+ | (r2 = b64tab[asc.charAt(i++)]);
211
+ bin += r1 === 64 ? _fromCC(u24 >> 16 & 255)
212
+ : r2 === 64 ? _fromCC(u24 >> 16 & 255, u24 >> 8 & 255)
213
+ : _fromCC(u24 >> 16 & 255, u24 >> 8 & 255, u24 & 255);
214
+ }
215
+ return bin;
216
+ };
217
+ /**
218
+ * does what `window.atob` of web browsers do.
219
+ * @param {String} asc Base64-encoded string
220
+ * @returns {string} binary string
221
+ */
222
+ var _atob = _hasatob ? function (asc) { return atob(_tidyB64(asc)); }
223
+ : _hasBuffer ? function (asc) { return Buffer.from(asc, 'base64').toString('binary'); }
224
+ : atobPolyfill;
225
+ //
226
+ var _toUint8Array = _hasBuffer
227
+ ? function (a) { return _U8Afrom(Buffer.from(a, 'base64')); }
228
+ : function (a) { return _U8Afrom(_atob(a), function (c) { return c.charCodeAt(0); }); };
229
+ /**
230
+ * converts a Base64 string to a Uint8Array.
231
+ */
232
+ var toUint8Array = function (a) { return _toUint8Array(_unURI(a)); };
233
+ //
234
+ var _decode = _hasBuffer
235
+ ? function (a) { return Buffer.from(a, 'base64').toString('utf8'); }
236
+ : _TD
237
+ ? function (a) { return _TD.decode(_toUint8Array(a)); }
238
+ : function (a) { return btou(_atob(a)); };
239
+ var _unURI = function (a) { return _tidyB64(a.replace(/[-_]/g, function (m0) { return m0 == '-' ? '+' : '/'; })); };
240
+ /**
241
+ * converts a Base64 string to a UTF-8 string.
242
+ * @param {String} src Base64 string. Both normal and URL-safe are supported
243
+ * @returns {string} UTF-8 string
244
+ */
245
+ var decode = function (src) { return _decode(_unURI(src)); };
246
+ /**
247
+ * check if a value is a valid Base64 string
248
+ * @param {String} src a value to check
249
+ */
250
+ var isValid = function (src) {
251
+ if (typeof src !== 'string')
252
+ return false;
253
+ var s = src.replace(/\s+/g, '').replace(/={0,2}$/, '');
254
+ return !/[^\s0-9a-zA-Z\+/]/.test(s) || !/[^\s0-9a-zA-Z\-_]/.test(s);
255
+ };
256
+ //
257
+ var _noEnum = function (v) {
258
+ return {
259
+ value: v, enumerable: false, writable: true, configurable: true
260
+ };
261
+ };
262
+ /**
263
+ * extend String.prototype with relevant methods
264
+ */
265
+ var extendString = function () {
266
+ var _add = function (name, body) { return Object.defineProperty(String.prototype, name, _noEnum(body)); };
267
+ _add('fromBase64', function () { return decode(this); });
268
+ _add('toBase64', function (urlsafe) { return encode(this, urlsafe); });
269
+ _add('toBase64URI', function () { return encode(this, true); });
270
+ _add('toBase64URL', function () { return encode(this, true); });
271
+ _add('toUint8Array', function () { return toUint8Array(this); });
272
+ };
273
+ /**
274
+ * extend Uint8Array.prototype with relevant methods
275
+ */
276
+ var extendUint8Array = function () {
277
+ var _add = function (name, body) { return Object.defineProperty(Uint8Array.prototype, name, _noEnum(body)); };
278
+ _add('toBase64', function (urlsafe) { return fromUint8Array(this, urlsafe); });
279
+ _add('toBase64URI', function () { return fromUint8Array(this, true); });
280
+ _add('toBase64URL', function () { return fromUint8Array(this, true); });
281
+ };
282
+ /**
283
+ * extend Builtin prototypes with relevant methods
284
+ */
285
+ var extendBuiltins = function () {
286
+ extendString();
287
+ extendUint8Array();
288
+ };
289
+ var gBase64 = {
290
+ version: version,
291
+ VERSION: VERSION,
292
+ atob: _atob,
293
+ atobPolyfill: atobPolyfill,
294
+ btoa: _btoa,
295
+ btoaPolyfill: btoaPolyfill,
296
+ fromBase64: decode,
297
+ toBase64: encode,
298
+ encode: encode,
299
+ encodeURI: encodeURI,
300
+ encodeURL: encodeURI,
301
+ utob: utob,
302
+ btou: btou,
303
+ decode: decode,
304
+ isValid: isValid,
305
+ fromUint8Array: fromUint8Array,
306
+ toUint8Array: toUint8Array,
307
+ extendString: extendString,
308
+ extendUint8Array: extendUint8Array,
309
+ extendBuiltins: extendBuiltins
255
310
  };
256
- };
257
- /**
258
- * extend String.prototype with relevant methods
259
- */
260
- const extendString = function () {
261
- const _add = (name, body) => Object.defineProperty(String.prototype, name, _noEnum(body));
262
- _add('fromBase64', function () { return decode(this); });
263
- _add('toBase64', function (urlsafe) { return encode(this, urlsafe); });
264
- _add('toBase64URI', function () { return encode(this, true); });
265
- _add('toBase64URL', function () { return encode(this, true); });
266
- _add('toUint8Array', function () { return toUint8Array(this); });
267
- };
268
- /**
269
- * extend Uint8Array.prototype with relevant methods
270
- */
271
- const extendUint8Array = function () {
272
- const _add = (name, body) => Object.defineProperty(Uint8Array.prototype, name, _noEnum(body));
273
- _add('toBase64', function (urlsafe) { return fromUint8Array(this, urlsafe); });
274
- _add('toBase64URI', function () { return fromUint8Array(this, true); });
275
- _add('toBase64URL', function () { return fromUint8Array(this, true); });
276
- };
277
- /**
278
- * extend Builtin prototypes with relevant methods
279
- */
280
- const extendBuiltins = () => {
281
- extendString();
282
- extendUint8Array();
283
- };
284
- const gBase64 = {
285
- version: version,
286
- VERSION: VERSION,
287
- atob: _atob,
288
- atobPolyfill: atobPolyfill,
289
- btoa: _btoa,
290
- btoaPolyfill: btoaPolyfill,
291
- fromBase64: decode,
292
- toBase64: encode,
293
- encode: encode,
294
- encodeURI: encodeURI,
295
- encodeURL: encodeURI,
296
- utob: utob,
297
- btou: btou,
298
- decode: decode,
299
- isValid: isValid,
300
- fromUint8Array: fromUint8Array,
301
- toUint8Array: toUint8Array,
302
- extendString: extendString,
303
- extendUint8Array: extendUint8Array,
304
- extendBuiltins: extendBuiltins,
305
- };
306
-
307
311
  //
308
312
  // export Base64 to the namespace
309
313
  //
310
314
  // ES5 is yet to have Object.assign() that may make transpilers unhappy.
311
315
  // gBase64.Base64 = Object.assign({}, gBase64);
312
316
  gBase64.Base64 = {};
313
- Object.keys(gBase64).forEach(k => gBase64.Base64[k] = gBase64[k]);
317
+ Object.keys(gBase64).forEach(function (k) { return gBase64.Base64[k] = gBase64[k]; });
314
318
  return gBase64;
315
319
  }));
316
-
317
-
package/base64.mjs CHANGED
@@ -9,7 +9,7 @@
9
9
  *
10
10
  * @author Dan Kogai (https://github.com/dankogai)
11
11
  */
12
- const version = '3.6.1';
12
+ const version = '3.7.2';
13
13
  /**
14
14
  * @deprecated use lowercase `version`.
15
15
  */
@@ -20,7 +20,7 @@ const _hasBuffer = typeof Buffer === 'function';
20
20
  const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
21
21
  const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
22
22
  const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
23
- const b64chs = [...b64ch];
23
+ const b64chs = Array.prototype.slice.call(b64ch);
24
24
  const b64tab = ((a) => {
25
25
  let tab = {};
26
26
  a.forEach((c, i) => tab[c] = i);
@@ -32,8 +32,7 @@ const _U8Afrom = typeof Uint8Array.from === 'function'
32
32
  ? Uint8Array.from.bind(Uint8Array)
33
33
  : (it, fn = (x) => x) => new Uint8Array(Array.prototype.slice.call(it, 0).map(fn));
34
34
  const _mkUriSafe = (src) => src
35
- .replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_')
36
- .replace(/=+$/m, '');
35
+ .replace(/=/g, '').replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_');
37
36
  const _tidyB64 = (s) => s.replace(/[^A-Za-z0-9\+\/]/g, '');
38
37
  /**
39
38
  * polyfill version of `btoa`
@@ -214,7 +213,7 @@ const decode = (src) => _decode(_unURI(src));
214
213
  const isValid = (src) => {
215
214
  if (typeof src !== 'string')
216
215
  return false;
217
- const s = src.replace(/\s+/g, '').replace(/=+$/, '');
216
+ const s = src.replace(/\s+/g, '').replace(/={0,2}$/, '');
218
217
  return !/[^\s0-9a-zA-Z\+/]/.test(s) || !/[^\s0-9a-zA-Z\-_]/.test(s);
219
218
  };
220
219
  //
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-base64",
3
- "version": "3.6.1",
3
+ "version": "3.7.2",
4
4
  "description": "Yet another Base64 transcoder in pure-JS",
5
5
  "main": "base64.js",
6
6
  "module": "base64.mjs",
@@ -21,10 +21,10 @@
21
21
  "test": "make clean && make test"
22
22
  },
23
23
  "devDependencies": {
24
- "typescript": "^3.9.7",
25
24
  "@types/node": "^14.0.26",
26
25
  "esm": "^3.2.25",
27
- "mocha": "^8.0.0"
26
+ "mocha": "^8.4.0",
27
+ "typescript": "^3.9.7"
28
28
  },
29
29
  "repository": "git+https://github.com/dankogai/js-base64.git",
30
30
  "keywords": [