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