js-base64 3.4.4 → 3.5.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
@@ -6,9 +6,9 @@ Yet another [Base64] transcoder.
6
6
 
7
7
  [Base64]: http://en.wikipedia.org/wiki/Base64
8
8
 
9
- ## HEADS UP: switch to TypeScript since version 3.3
9
+ ## HEADS UP
10
10
 
11
- In version 3.0 `js-base64` switch to ES2015 module. That made it easy to switch to TypeScript(just renaming `base64.mjs` to `base64.ts` was almost enough). Now `base64.mjs` is compiled from `base64.ts` then `base64.js` is generated from `base64.mjs`.
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
12
 
13
13
  ## Install
14
14
 
@@ -29,7 +29,7 @@ Locally…
29
29
  … or Directly from CDN. In which case you don't even need to install.
30
30
 
31
31
  ```html
32
- <script src="https://cdn.jsdelivr.net/npm/js-base64@3.4.4/base64.min.js"></script>
32
+ <script src="https://cdn.jsdelivr.net/npm/js-base64@3.5.2/base64.min.js"></script>
33
33
  ```
34
34
 
35
35
  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 +52,14 @@ or even remotely.
52
52
  ```html
53
53
  <script type="module">
54
54
  // note jsdelivr.net does not automatically minify .mjs
55
- import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.4.4/base64.mjs';
55
+ import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.5.2/base64.mjs';
56
56
  </script>
57
57
  ```
58
58
 
59
59
  ```html
60
60
  <script type="module">
61
61
  // or if you prefer no Base64 namespace
62
- import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.4.4/base64.mjs';
62
+ import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.5.2/base64.mjs';
63
63
  </script>
64
64
  ```
65
65
 
@@ -80,7 +80,6 @@ require=require('esm')(module);
80
80
  import {Base64} from 'js-base64';
81
81
  ```
82
82
 
83
-
84
83
  ## SYNOPSIS
85
84
 
86
85
  ```javascript
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.4.4";
12
+ declare const version = "3.5.2";
13
13
  /**
14
14
  * @deprecated use lowercase `version`.
15
15
  */
16
- declare const VERSION = "3.4.4";
16
+ declare const VERSION = "3.5.2";
17
17
  /**
18
18
  * polyfill version of `btoa`
19
19
  */
@@ -35,7 +35,7 @@ declare const fromUint8Array: (u8a: Uint8Array, urlsafe?: boolean) => string;
35
35
  * @param {string} src UTF-8 string
36
36
  * @returns {string} UTF-16 string
37
37
  */
38
- declare const utob: (src: string) => string;
38
+ declare const utob: (u: string) => string;
39
39
  /**
40
40
  * converts a UTF-8-encoded string to a Base64 string.
41
41
  * @param {boolean} [urlsafe] if `true` make the result URL-safe
@@ -52,7 +52,7 @@ declare const encodeURI: (src: string) => string;
52
52
  * @param {string} src UTF-16 string
53
53
  * @returns {string} UTF-8 string
54
54
  */
55
- declare const btou: (src: string) => string;
55
+ declare const btou: (b: string) => string;
56
56
  /**
57
57
  * polyfill version of `atob`
58
58
  */
@@ -63,16 +63,16 @@ declare const atobPolyfill: (asc: string) => string;
63
63
  * @returns {string} binary string
64
64
  */
65
65
  declare const _atob: (asc: string) => string;
66
+ /**
67
+ * converts a Base64 string to a Uint8Array.
68
+ */
69
+ declare const toUint8Array: (a: string) => Uint8Array;
66
70
  /**
67
71
  * converts a Base64 string to a UTF-8 string.
68
72
  * @param {String} src Base64 string. Both normal and URL-safe are supported
69
73
  * @returns {string} UTF-8 string
70
74
  */
71
75
  declare const decode: (src: string) => string;
72
- /**
73
- * converts a Base64 string to a Uint8Array.
74
- */
75
- declare const toUint8Array: (a: string) => any;
76
76
  /**
77
77
  * extend String.prototype with relevant methods
78
78
  */
@@ -97,11 +97,11 @@ declare const gBase64: {
97
97
  encode: (src: string, urlsafe?: boolean) => string;
98
98
  encodeURI: (src: string) => string;
99
99
  encodeURL: (src: string) => string;
100
- utob: (src: string) => string;
101
- btou: (src: string) => string;
100
+ utob: (u: string) => string;
101
+ btou: (b: string) => string;
102
102
  decode: (src: string) => string;
103
103
  fromUint8Array: (u8a: Uint8Array, urlsafe?: boolean) => string;
104
- toUint8Array: (a: string) => any;
104
+ toUint8Array: (a: string) => Uint8Array;
105
105
  extendString: () => void;
106
106
  extendUint8Array: () => void;
107
107
  extendBuiltins: () => void;
package/base64.js CHANGED
@@ -40,7 +40,7 @@
40
40
  *
41
41
  * @author Dan Kogai (https://github.com/dankogai)
42
42
  */
43
- const version = '3.4.4';
43
+ const version = '3.5.2';
44
44
  /**
45
45
  * @deprecated use lowercase `version`.
46
46
  */
@@ -48,6 +48,8 @@ const VERSION = version;
48
48
  const _hasatob = typeof atob === 'function';
49
49
  const _hasbtoa = typeof btoa === 'function';
50
50
  const _hasBuffer = typeof Buffer === 'function';
51
+ const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
52
+ const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
51
53
  const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
52
54
  const b64chs = [...b64ch];
53
55
  const b64tab = ((a) => {
@@ -101,7 +103,7 @@ const _fromUint8Array = _hasBuffer
101
103
  for (let i = 0, l = u8a.length; i < l; i += maxargs) {
102
104
  strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
103
105
  }
104
- return btoa(strs.join(''));
106
+ return _btoa(strs.join(''));
105
107
  };
106
108
  /**
107
109
  * converts a Uint8Array to a Base64 string.
@@ -109,16 +111,42 @@ const _fromUint8Array = _hasBuffer
109
111
  * @returns {string} Base64 string
110
112
  */
111
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;
112
138
  /**
113
139
  * @deprecated should have been internal use only.
114
140
  * @param {string} src UTF-8 string
115
141
  * @returns {string} UTF-16 string
116
142
  */
117
- const utob = (src) => unescape(encodeURIComponent(src));
143
+ const utob = (u) => u.replace(re_utob, cb_utob);
118
144
  //
119
145
  const _encode = _hasBuffer
120
146
  ? (s) => Buffer.from(s, 'utf8').toString('base64')
121
- : (s) => _btoa(utob(s));
147
+ : _TE
148
+ ? (s) => _fromUint8Array(_TE.encode(s))
149
+ : (s) => _btoa(utob(s));
122
150
  /**
123
151
  * converts a UTF-8-encoded string to a Base64 string.
124
152
  * @param {boolean} [urlsafe] if `true` make the result URL-safe
@@ -132,12 +160,34 @@ const encode = (src, urlsafe = false) => urlsafe
132
160
  * @returns {string} Base64 string
133
161
  */
134
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
+ };
135
185
  /**
136
186
  * @deprecated should have been internal use only.
137
187
  * @param {string} src UTF-16 string
138
188
  * @returns {string} UTF-8 string
139
189
  */
140
- const btou = (src) => decodeURIComponent(escape(src));
190
+ const btou = (b) => b.replace(re_btou, cb_btou);
141
191
  /**
142
192
  * polyfill version of `atob`
143
193
  */
@@ -167,9 +217,20 @@ const atobPolyfill = (asc) => {
167
217
  const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
168
218
  : _hasBuffer ? (asc) => Buffer.from(asc, 'base64').toString('binary')
169
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
+ //
170
229
  const _decode = _hasBuffer
171
230
  ? (a) => Buffer.from(a, 'base64').toString('utf8')
172
- : (a) => btou(_atob(a));
231
+ : _TD
232
+ ? (a) => _TD.decode(_toUint8Array(a))
233
+ : (a) => btou(_atob(a));
173
234
  const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
174
235
  /**
175
236
  * converts a Base64 string to a UTF-8 string.
@@ -177,12 +238,7 @@ const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/')
177
238
  * @returns {string} UTF-8 string
178
239
  */
179
240
  const decode = (src) => _decode(_unURI(src));
180
- /**
181
- * converts a Base64 string to a Uint8Array.
182
- */
183
- const toUint8Array = _hasBuffer
184
- ? (a) => _U8Afrom(Buffer.from(_unURI(a), 'base64'))
185
- : (a) => _U8Afrom(_atob(_unURI(a)), c => c.charCodeAt(0));
241
+ //
186
242
  const _noEnum = (v) => {
187
243
  return {
188
244
  value: v, enumerable: false, writable: true, configurable: true
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.4.4';
12
+ const version = '3.5.2';
13
13
  /**
14
14
  * @deprecated use lowercase `version`.
15
15
  */
@@ -17,6 +17,8 @@ const VERSION = version;
17
17
  const _hasatob = typeof atob === 'function';
18
18
  const _hasbtoa = typeof btoa === 'function';
19
19
  const _hasBuffer = typeof Buffer === 'function';
20
+ const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
21
+ const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
20
22
  const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
21
23
  const b64chs = [...b64ch];
22
24
  const b64tab = ((a) => {
@@ -70,7 +72,7 @@ const _fromUint8Array = _hasBuffer
70
72
  for (let i = 0, l = u8a.length; i < l; i += maxargs) {
71
73
  strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
72
74
  }
73
- return btoa(strs.join(''));
75
+ return _btoa(strs.join(''));
74
76
  };
75
77
  /**
76
78
  * converts a Uint8Array to a Base64 string.
@@ -78,16 +80,42 @@ const _fromUint8Array = _hasBuffer
78
80
  * @returns {string} Base64 string
79
81
  */
80
82
  const fromUint8Array = (u8a, urlsafe = false) => urlsafe ? _mkUriSafe(_fromUint8Array(u8a)) : _fromUint8Array(u8a);
83
+ // This trick is found broken https://github.com/dankogai/js-base64/issues/130
84
+ // const utob = (src: string) => unescape(encodeURIComponent(src));
85
+ // reverting good old fationed regexp
86
+ const cb_utob = (c) => {
87
+ if (c.length < 2) {
88
+ var cc = c.charCodeAt(0);
89
+ return cc < 0x80 ? c
90
+ : cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
91
+ + _fromCC(0x80 | (cc & 0x3f)))
92
+ : (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
93
+ + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
94
+ + _fromCC(0x80 | (cc & 0x3f)));
95
+ }
96
+ else {
97
+ var cc = 0x10000
98
+ + (c.charCodeAt(0) - 0xD800) * 0x400
99
+ + (c.charCodeAt(1) - 0xDC00);
100
+ return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
101
+ + _fromCC(0x80 | ((cc >>> 12) & 0x3f))
102
+ + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
103
+ + _fromCC(0x80 | (cc & 0x3f)));
104
+ }
105
+ };
106
+ const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
81
107
  /**
82
108
  * @deprecated should have been internal use only.
83
109
  * @param {string} src UTF-8 string
84
110
  * @returns {string} UTF-16 string
85
111
  */
86
- const utob = (src) => unescape(encodeURIComponent(src));
112
+ const utob = (u) => u.replace(re_utob, cb_utob);
87
113
  //
88
114
  const _encode = _hasBuffer
89
115
  ? (s) => Buffer.from(s, 'utf8').toString('base64')
90
- : (s) => _btoa(utob(s));
116
+ : _TE
117
+ ? (s) => _fromUint8Array(_TE.encode(s))
118
+ : (s) => _btoa(utob(s));
91
119
  /**
92
120
  * converts a UTF-8-encoded string to a Base64 string.
93
121
  * @param {boolean} [urlsafe] if `true` make the result URL-safe
@@ -101,12 +129,34 @@ const encode = (src, urlsafe = false) => urlsafe
101
129
  * @returns {string} Base64 string
102
130
  */
103
131
  const encodeURI = (src) => encode(src, true);
132
+ // This trick is found broken https://github.com/dankogai/js-base64/issues/130
133
+ // const btou = (src: string) => decodeURIComponent(escape(src));
134
+ // reverting good old fationed regexp
135
+ const re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
136
+ const cb_btou = (cccc) => {
137
+ switch (cccc.length) {
138
+ case 4:
139
+ var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
140
+ | ((0x3f & cccc.charCodeAt(1)) << 12)
141
+ | ((0x3f & cccc.charCodeAt(2)) << 6)
142
+ | (0x3f & cccc.charCodeAt(3)), offset = cp - 0x10000;
143
+ return (_fromCC((offset >>> 10) + 0xD800)
144
+ + _fromCC((offset & 0x3FF) + 0xDC00));
145
+ case 3:
146
+ return _fromCC(((0x0f & cccc.charCodeAt(0)) << 12)
147
+ | ((0x3f & cccc.charCodeAt(1)) << 6)
148
+ | (0x3f & cccc.charCodeAt(2)));
149
+ default:
150
+ return _fromCC(((0x1f & cccc.charCodeAt(0)) << 6)
151
+ | (0x3f & cccc.charCodeAt(1)));
152
+ }
153
+ };
104
154
  /**
105
155
  * @deprecated should have been internal use only.
106
156
  * @param {string} src UTF-16 string
107
157
  * @returns {string} UTF-8 string
108
158
  */
109
- const btou = (src) => decodeURIComponent(escape(src));
159
+ const btou = (b) => b.replace(re_btou, cb_btou);
110
160
  /**
111
161
  * polyfill version of `atob`
112
162
  */
@@ -136,9 +186,20 @@ const atobPolyfill = (asc) => {
136
186
  const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
137
187
  : _hasBuffer ? (asc) => Buffer.from(asc, 'base64').toString('binary')
138
188
  : atobPolyfill;
189
+ //
190
+ const _toUint8Array = _hasBuffer
191
+ ? (a) => _U8Afrom(Buffer.from(a, 'base64'))
192
+ : (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
193
+ /**
194
+ * converts a Base64 string to a Uint8Array.
195
+ */
196
+ const toUint8Array = (a) => _toUint8Array(_unURI(a));
197
+ //
139
198
  const _decode = _hasBuffer
140
199
  ? (a) => Buffer.from(a, 'base64').toString('utf8')
141
- : (a) => btou(_atob(a));
200
+ : _TD
201
+ ? (a) => _TD.decode(_toUint8Array(a))
202
+ : (a) => btou(_atob(a));
142
203
  const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
143
204
  /**
144
205
  * converts a Base64 string to a UTF-8 string.
@@ -146,12 +207,7 @@ const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/')
146
207
  * @returns {string} UTF-8 string
147
208
  */
148
209
  const decode = (src) => _decode(_unURI(src));
149
- /**
150
- * converts a Base64 string to a Uint8Array.
151
- */
152
- const toUint8Array = _hasBuffer
153
- ? (a) => _U8Afrom(Buffer.from(_unURI(a), 'base64'))
154
- : (a) => _U8Afrom(_atob(_unURI(a)), c => c.charCodeAt(0));
210
+ //
155
211
  const _noEnum = (v) => {
156
212
  return {
157
213
  value: v, enumerable: false, writable: true, configurable: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-base64",
3
- "version": "3.4.4",
3
+ "version": "3.5.2",
4
4
  "description": "Yet another Base64 transcoder in pure-JS",
5
5
  "main": "base64.js",
6
6
  "module": "base64.mjs",