@lumjs/encode 2.0.0 → 2.1.0
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/CHANGELOG.md +24 -1
- package/lib/base64.js +61 -7
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [2.1.0]
|
|
10
|
+
### Changed
|
|
11
|
+
- The `base64` library now supports the `Uint8Array.fromBase64` static method,
|
|
12
|
+
and the corresponding `toBase64()` instance method. As those are rather new
|
|
13
|
+
APIs and aren't supported by every JS runtime yet, it will check for them and
|
|
14
|
+
use them if found, and fall back to the prior implementation otherwise.
|
|
15
|
+
- Updated this changelog, as I forgot to after 2.0 was released last year.
|
|
16
|
+
|
|
17
|
+
## [2.0.0] - 2024-08-14
|
|
18
|
+
### Changed
|
|
19
|
+
- A major overhaul, removing the dependency on `crypto-js` library.
|
|
20
|
+
- Rewrote `base64` library to use `atob`, `btoa`, `TextEncoder`, and `TextDecoder`.
|
|
21
|
+
- Rewrote `hash` library using the modern `SubtleCrypto` APIs.
|
|
22
|
+
- Moved `urlize()` and `deurlize()` functions into `base64` library.
|
|
23
|
+
### Removed
|
|
24
|
+
- The `crypto` sub-module which was designed as a wrapper for `crypto-js`.
|
|
25
|
+
- The `safe64` library has been moved into a standalone [safe64-data] package.
|
|
26
|
+
- Dependencies on `php-serialize` and `ubjson` which were only used by `safe64`.
|
|
27
|
+
|
|
9
28
|
## [1.2.0] - 2023-01-06
|
|
10
29
|
### Changed
|
|
11
30
|
- Bumped `@lumjs/core` to `1.8.0`.
|
|
@@ -23,7 +42,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
23
42
|
### Added
|
|
24
43
|
- Initial release.
|
|
25
44
|
|
|
26
|
-
[Unreleased]: https://github.com/supernovus/lum.encode.js/compare/
|
|
45
|
+
[Unreleased]: https://github.com/supernovus/lum.encode.js/compare/v2.1.0...HEAD
|
|
46
|
+
[2.1.0]: https://github.com/supernovus/lum.encode.js/compare/v2.0.0...v2.1.0
|
|
47
|
+
[2.0.0]: https://github.com/supernovus/lum.encode.js/compare/v1.2.0...v2.0.0
|
|
27
48
|
[1.2.0]: https://github.com/supernovus/lum.encode.js/compare/v1.1.0...v1.2.0
|
|
28
49
|
[1.1.0]: https://github.com/supernovus/lum.encode.js/compare/v1.0.0...v1.1.0
|
|
29
50
|
[1.0.0]: https://github.com/supernovus/lum.encode.js/releases/tag/v1.0.0
|
|
51
|
+
[safe64-data]: https://github.com/supernovus/lum.safe64-data.js
|
|
52
|
+
|
package/lib/base64.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
|
|
2
|
-
const {S,B,isObj} = require('@lumjs/core/types');
|
|
2
|
+
const {S,B,F,isObj} = require('@lumjs/core/types');
|
|
3
3
|
|
|
4
4
|
const D_MIME = 'application/octet-stream';
|
|
5
5
|
const D_ENC = 'utf-8';
|
|
6
6
|
const P_DATA = 'data:';
|
|
7
7
|
const P_B64 = ';base64,';
|
|
8
8
|
const R_PRE = /^data\:(.*?);base64,/;
|
|
9
|
+
const B_64U = 'base64url';
|
|
10
|
+
|
|
11
|
+
const UI8_FB64 = (typeof Uint8Array.fromBase64 === F);
|
|
12
|
+
const URL_CHARS = /_-/;
|
|
9
13
|
|
|
10
14
|
/**
|
|
11
15
|
* Base64 functions.
|
|
@@ -63,12 +67,22 @@ function deurlize(string)
|
|
|
63
67
|
*
|
|
64
68
|
* This is a low-level function with no options.
|
|
65
69
|
* See `decodeText()` for a more full-featured function.
|
|
70
|
+
*
|
|
71
|
+
* This now checks to see if `Uint8Array.fromBase64()` exists,
|
|
72
|
+
* and if it does, will use it. Otherwise it uses the original
|
|
73
|
+
* encoding algorithm.
|
|
66
74
|
*
|
|
67
75
|
* @param {string} base64 - Base64 encoded-string
|
|
76
|
+
* @param {object} [options] Passed to fromBase64() if it exists
|
|
68
77
|
* @returns {Uint8Array}
|
|
69
78
|
*/
|
|
70
|
-
function toBytes(base64)
|
|
79
|
+
function toBytes(base64, options={})
|
|
71
80
|
{
|
|
81
|
+
if (UI8_FB64)
|
|
82
|
+
{
|
|
83
|
+
return Uint8Array.fromBase64(base64, options);
|
|
84
|
+
}
|
|
85
|
+
|
|
72
86
|
const binString = atob(base64);
|
|
73
87
|
return Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
74
88
|
}
|
|
@@ -78,12 +92,22 @@ function toBytes(base64)
|
|
|
78
92
|
*
|
|
79
93
|
* This is a low-level function with no options.
|
|
80
94
|
* See `encodeText()` for a more full-featured function.
|
|
95
|
+
*
|
|
96
|
+
* This now checks to see if `bytes.toBase64()` exists,
|
|
97
|
+
* and if it does, will use it. Otherwise it uses the original
|
|
98
|
+
* decoding algorithm.
|
|
81
99
|
*
|
|
82
100
|
* @param {Uint8Array} bytes - Byte array to convert
|
|
101
|
+
* @param {object} [options] Passed to toBase64() if it exists
|
|
83
102
|
* @returns {string}
|
|
84
103
|
*/
|
|
85
|
-
function fromBytes(bytes)
|
|
104
|
+
function fromBytes(bytes, options={})
|
|
86
105
|
{
|
|
106
|
+
if (typeof bytes.toBase64 === F)
|
|
107
|
+
{
|
|
108
|
+
return bytes.toBase64(options);
|
|
109
|
+
}
|
|
110
|
+
|
|
87
111
|
const binString = Array.from(bytes, (byte) =>
|
|
88
112
|
String.fromCodePoint(byte),
|
|
89
113
|
).join("");
|
|
@@ -115,8 +139,23 @@ function encodeText(data, options={})
|
|
|
115
139
|
}
|
|
116
140
|
|
|
117
141
|
const encoder = new TextEncoder();
|
|
118
|
-
|
|
119
|
-
|
|
142
|
+
data = encoder.encode(data);
|
|
143
|
+
const toB64 = (typeof data.toBase64 === F);
|
|
144
|
+
|
|
145
|
+
if (toB64 && options.url)
|
|
146
|
+
{
|
|
147
|
+
if (typeof options.alphabet !== S)
|
|
148
|
+
{
|
|
149
|
+
options.alphabet = B_64U;
|
|
150
|
+
}
|
|
151
|
+
if (!options.useTildes && typeof options.omitPadding !== B)
|
|
152
|
+
{
|
|
153
|
+
options.omitPadding = true;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const base64 = fromBytes(data, options);
|
|
158
|
+
return (options.url && !toB64) ? urlize(base64, options) : base64;
|
|
120
159
|
}
|
|
121
160
|
|
|
122
161
|
/**
|
|
@@ -149,12 +188,25 @@ function decodeText(base64, options={})
|
|
|
149
188
|
|
|
150
189
|
if (options.url !== false)
|
|
151
190
|
{ // Unless explicitly disabled, use deurlize() first.
|
|
152
|
-
|
|
191
|
+
if (UI8_FB64)
|
|
192
|
+
{
|
|
193
|
+
if (typeof options.alphabet !== S)
|
|
194
|
+
{
|
|
195
|
+
if (URL_CHARS.test(base64))
|
|
196
|
+
{
|
|
197
|
+
options.alphabet = B_64U;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else
|
|
202
|
+
{
|
|
203
|
+
base64 = deurlize(base64);
|
|
204
|
+
}
|
|
153
205
|
}
|
|
154
206
|
|
|
155
207
|
const encoding = options.encoding ?? D_ENC;
|
|
156
208
|
const decoder = new TextDecoder(encoding, options);
|
|
157
|
-
return decoder.decode(toBytes(base64), options);
|
|
209
|
+
return decoder.decode(toBytes(base64, options), options);
|
|
158
210
|
}
|
|
159
211
|
|
|
160
212
|
/**
|
|
@@ -373,4 +425,6 @@ module.exports =
|
|
|
373
425
|
toDataUrl, fromDataUrl,
|
|
374
426
|
encodeData, decodeData,
|
|
375
427
|
encode, decode,
|
|
428
|
+
NATIVE_BASE64: UI8_FB64,
|
|
376
429
|
}
|
|
430
|
+
|