js-base64 3.9.4 → 3.9.5
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 +32 -6
- package/base64.js +2 -2
- package/base64.mjs +2 -2
- package/package.json +1 -1
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.
|
|
29
|
+
<script src="https://cdn.jsdelivr.net/npm/js-base64@3.9.5/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.
|
|
52
|
+
import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.9.5/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.
|
|
59
|
+
import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.9.5/base64.mjs';
|
|
60
60
|
</script>
|
|
61
61
|
```
|
|
62
62
|
|
|
@@ -89,7 +89,7 @@ Base64.encodeURI(latin); // ZGFua29nYWk
|
|
|
89
89
|
Base64.btoa(latin); // ZGFua29nYWk=
|
|
90
90
|
Base64.btoa(utf8); // raises exception
|
|
91
91
|
Base64.fromUint8Array(u8s); // ZGFua29nYWk=
|
|
92
|
-
Base64.fromUint8Array(u8s, true); //
|
|
92
|
+
Base64.fromUint8Array(u8s, true); // ZGFua29nYWk which is URI safe
|
|
93
93
|
Base64.encode(utf8); // 5bCP6aO85by+
|
|
94
94
|
Base64.encode(utf8, true) // 5bCP6aO85by-
|
|
95
95
|
Base64.encodeURI(utf8); // 5bCP6aO85by-
|
|
@@ -117,6 +117,32 @@ Base64.isValid('--'); // true: or URL-safe
|
|
|
117
117
|
Base64.isValid('+-'); // false: can't mix both
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
+
### Base64 variants
|
|
121
|
+
|
|
122
|
+
The default encoding uses the standard Base64 alphabet from
|
|
123
|
+
[RFC 4648, section 4](https://www.rfc-editor.org/rfc/rfc4648.html#section-4).
|
|
124
|
+
The URL-safe form in js-base64 uses the base64url alphabet from
|
|
125
|
+
[section 5](https://www.rfc-editor.org/rfc/rfc4648.html#section-5) and omits padding.
|
|
126
|
+
|
|
127
|
+
| Form | Characters for values 62 and 63 | Padding | Encoding methods |
|
|
128
|
+
| --- | --- | --- | --- |
|
|
129
|
+
| Standard Base64 | `+` and `/` | Trailing `=` as needed | `Base64.encode(text)`, `Base64.fromUint8Array(bytes)` |
|
|
130
|
+
| URL-safe Base64 | `-` and `_` | Omitted | `Base64.encodeURI(text)`, `Base64.encode(text, true)`, `Base64.fromUint8Array(bytes, true)` |
|
|
131
|
+
|
|
132
|
+
For example, these bytes show both the alphabet and padding differences:
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
const bytes = new Uint8Array([251, 255]);
|
|
136
|
+
Base64.fromUint8Array(bytes); // '+/8='
|
|
137
|
+
Base64.fromUint8Array(bytes, true); // '-_8'
|
|
138
|
+
Base64.toUint8Array('+/8='); // Uint8Array [251, 255]
|
|
139
|
+
Base64.toUint8Array('-_8'); // Uint8Array [251, 255]
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`Base64.decode()` and `Base64.toUint8Array()` accept both alphabets, with or
|
|
143
|
+
without padding. Use `decode()` for UTF-8 text and `toUint8Array()` for bytes.
|
|
144
|
+
Encoded output has no line wrapping; MIME or PEM line wrapping is not added.
|
|
145
|
+
|
|
120
146
|
### Built-in Extensions
|
|
121
147
|
|
|
122
148
|
By default `Base64` leaves built-in prototypes untouched. But you can extend them as below.
|
|
@@ -133,7 +159,7 @@ Base64.extendString();
|
|
|
133
159
|
'ZGFua29nYWk='.fromBase64(); // dankogai
|
|
134
160
|
'5bCP6aO85by+'.fromBase64(); // 小飼弾
|
|
135
161
|
'5bCP6aO85by-'.fromBase64(); // 小飼弾
|
|
136
|
-
'5bCP6aO85by-'.toUint8Array();//
|
|
162
|
+
'5bCP6aO85by-'.toUint8Array();// UTF-8 bytes of '小飼弾'
|
|
137
163
|
```
|
|
138
164
|
|
|
139
165
|
```javascript
|
|
@@ -159,7 +185,7 @@ var pngBase64 =
|
|
|
159
185
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
|
|
160
186
|
```
|
|
161
187
|
|
|
162
|
-
Which is a Base64-encoded 1x1 transparent PNG, **DO NOT USE** `Base64.decode(pngBase64)`. Use `Base64.atob(pngBase64)` instead. `Base64.decode()` decodes to UTF-8 string while `Base64.atob()` decodes to bytes, which is compatible to browser built-in `atob()
|
|
188
|
+
Which is a Base64-encoded 1x1 transparent PNG, **DO NOT USE** `Base64.decode(pngBase64)`. Use `Base64.atob(pngBase64)` instead. `Base64.decode()` decodes to UTF-8 string while `Base64.atob()` decodes to bytes, which is compatible to browser built-in `atob()`. The same rule applies to the opposite direction.
|
|
163
189
|
|
|
164
190
|
Or even better, `Base64.toUint8Array(pngBase64)`.
|
|
165
191
|
|
package/base64.js
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
*
|
|
24
24
|
* @author Dan Kogai (https://github.com/dankogai)
|
|
25
25
|
*/
|
|
26
|
-
var version = '3.9.
|
|
26
|
+
var version = '3.9.5';
|
|
27
27
|
/**
|
|
28
28
|
* @deprecated use lowercase `version`.
|
|
29
29
|
*/
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
*/
|
|
73
73
|
var _btoa = typeof btoa === 'function' ? function (bin) { return btoa(bin); }
|
|
74
74
|
: btoaPolyfill;
|
|
75
|
-
var _fromUint8Array = typeof Uint8Array.prototype.toBase64 === 'function' ?
|
|
75
|
+
var _fromUint8Array = typeof Uint8Array.prototype.toBase64 === 'function' ? Function.prototype.call.bind(Uint8Array.prototype.toBase64)
|
|
76
76
|
: function (u8a) {
|
|
77
77
|
// cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
|
|
78
78
|
var maxargs = 0x1000;
|
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.
|
|
12
|
+
const version = '3.9.5';
|
|
13
13
|
/**
|
|
14
14
|
* @deprecated use lowercase `version`.
|
|
15
15
|
*/
|
|
@@ -58,7 +58,7 @@ const btoaPolyfill = (bin) => {
|
|
|
58
58
|
*/
|
|
59
59
|
const _btoa = typeof btoa === 'function' ? (bin) => btoa(bin)
|
|
60
60
|
: btoaPolyfill;
|
|
61
|
-
const _fromUint8Array = typeof Uint8Array.prototype.toBase64 === 'function' ? (
|
|
61
|
+
const _fromUint8Array = typeof Uint8Array.prototype.toBase64 === 'function' ? Function.prototype.call.bind(Uint8Array.prototype.toBase64)
|
|
62
62
|
: (u8a) => {
|
|
63
63
|
// cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
|
|
64
64
|
const maxargs = 0x1000;
|