@lumjs/encode 2.3.1 → 2.3.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/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.3.2] - 2026-01-14
10
+ ### Fixed
11
+ - More things I missed, sigh.
12
+
9
13
  ## [2.3.1] - 2026-01-14
10
14
  ### Fixed
11
15
  - Another broken reference in the HOTP library from its prototype stage.
@@ -73,7 +77,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
73
77
  ### Added
74
78
  - Initial release.
75
79
 
76
- [Unreleased]: https://github.com/supernovus/lum.encode.js/compare/v2.3.1...HEAD
80
+ [Unreleased]: https://github.com/supernovus/lum.encode.js/compare/v2.3.2...HEAD
81
+ [2.3.2]: https://github.com/supernovus/lum.encode.js/compare/v2.3.1...v2.3.2
77
82
  [2.3.1]: https://github.com/supernovus/lum.encode.js/compare/v2.3.0...v2.3.1
78
83
  [2.3.0]: https://github.com/supernovus/lum.encode.js/compare/v2.2.2...v2.3.0
79
84
  [2.2.2]: https://github.com/supernovus/lum.encode.js/compare/v2.2.0...v2.2.2
package/lib/base64.js CHANGED
@@ -65,7 +65,7 @@ function deurlize(string)
65
65
  /**
66
66
  * Convert a Base64-encoded string into a Uint8Array.
67
67
  *
68
- * This is a low-level function with no options.
68
+ * This is a low-level function with minimal options.
69
69
  * See `decodeText()` for a more full-featured function.
70
70
  *
71
71
  * This now checks to see if `Uint8Array.fromBase64()` exists,
@@ -117,10 +117,16 @@ function fromBytes(bytes, options={})
117
117
  /**
118
118
  * Encode a string to Base64.
119
119
  *
120
- * Uses the `TextEncoder` API and `fromBytes()`.
121
- * May optionally pass the output through `urlize()`.
120
+ * This adds a bunch of extra features on top of `fromBytes()`,
121
+ * including optionally passing the output through `urlize()`.
122
+ *
123
+ * @param {(string|Uint8Array)} data - Data to be encoded.
124
+ *
125
+ * If this is a String we'll use a `TextEncoder` instance to
126
+ * convert it into a Uint8Array before passing it to fromBytes().
127
+ *
128
+ * If this is a Uint8Array, we can skip the TextEncoder part.
122
129
  *
123
- * @param {string} data - Any valid (Unicode) string
124
130
  * @param {(object|boolean)} [options] Options
125
131
  *
126
132
  * - If `boolean`, used as `options.url`
@@ -138,8 +144,11 @@ function encodeText(data, options={})
138
144
  options = {url: options};
139
145
  }
140
146
 
141
- const encoder = new TextEncoder();
142
- data = encoder.encode(data);
147
+ if (!(data instanceof Uint8Array))
148
+ {
149
+ const encoder = new TextEncoder();
150
+ data = encoder.encode(data);
151
+ }
143
152
  const toB64 = (typeof data.toBase64 === F);
144
153
 
145
154
  if (toB64 && options.url)
@@ -171,13 +180,15 @@ function encodeText(data, options={})
171
180
  * - Passed to `new TextDecoder()`
172
181
  * - Passed to `decoder.decode()`
173
182
  *
174
- * @param {boolean} [options.url=true] Deurlize the output?
183
+ * @param {boolean} [options.string=true] Decode as a string?
184
+ * @param {boolean} [options.url=true] Deurlize the base64 string?
175
185
  *
176
186
  * Unless this is explicitly set as `false`, the `base64`
177
187
  * string will be passed to `deurlize()` before being
178
188
  * processed further.
179
189
  *
180
- * @returns {string} A Unicode string
190
+ * @returns {(string|Uint8Array)}
191
+ * Returned type depends on `opts.string`.
181
192
  */
182
193
  function decodeText(base64, options={})
183
194
  {
@@ -186,7 +197,7 @@ function decodeText(base64, options={})
186
197
  options = {url: options};
187
198
  }
188
199
 
189
- if (options.url !== false)
200
+ if (options.url ?? true)
190
201
  { // Unless explicitly disabled, use deurlize() first.
191
202
  if (UI8_FB64)
192
203
  {
@@ -204,9 +215,13 @@ function decodeText(base64, options={})
204
215
  }
205
216
  }
206
217
 
207
- const encoding = options.encoding ?? D_ENC;
208
- const decoder = new TextDecoder(encoding, options);
209
- return decoder.decode(toBytes(base64, options), options);
218
+ const bytes = toBytes(base64, options);
219
+ if (options.string ?? true) {
220
+ const encoding = options.encoding ?? D_ENC;
221
+ const decoder = new TextDecoder(encoding, options);
222
+ return decoder.decode(bytes, options);
223
+ }
224
+ return bytes;
210
225
  }
211
226
 
212
227
  /**
package/lib/hash.js CHANGED
@@ -3,6 +3,7 @@
3
3
  const {S,F,isObj,isNil} = require('@lumjs/core/types');
4
4
 
5
5
  const util = require('./util');
6
+ const base32 = require('./base32');
6
7
  const base64 = require('./base64');
7
8
  const base91 = require('./base91');
8
9
 
@@ -156,6 +157,7 @@ module.exports = class
156
157
 
157
158
  this.defaults =
158
159
  {
160
+ base32: options.base32 ?? {},
159
161
  base64: options.base64 ?? {},
160
162
  base91: options.base91 ?? {},
161
163
  }
@@ -289,6 +291,22 @@ module.exports = class
289
291
  return (hashArr.map((b)=>b.toString(16).padStart(2, "0")).join(""));
290
292
  }
291
293
 
294
+ /**
295
+ * Get hash as a Base32-encoded string.
296
+ *
297
+ * @param {(string|object|null)} [input]
298
+ * See [hash()]{@link module:@lumjs/encode/hash#hash}
299
+ *
300
+ * @param {object} [options] Options to pass to base32.encode().
301
+ *
302
+ * @returns {string}
303
+ */
304
+ async base32(input, opts=this.defaults.base32)
305
+ {
306
+ const hash = await this.hash(input);
307
+ return base32.encode(new Uint8Array(hash), opts);
308
+ }
309
+
292
310
  /**
293
311
  * Get hash as a Base64-encoded string.
294
312
  *
@@ -311,12 +329,14 @@ module.exports = class
311
329
  /**
312
330
  * Get hash as a Base91-encoded string.
313
331
  *
314
- * @param {(string|WordArray)} [input]
332
+ * @param {(string|object|null)} [input]
315
333
  * See [hash()]{@link module:@lumjs/encode/hash#hash}
316
334
  * @param {object} [opts] Options for how to encode the hash.
317
335
  *
318
336
  * @param {(boolean|object)} [opts.nba=false] Use `numByteArray()` ?
319
337
  *
338
+ * This options is deprecated and will be removed in v3.0.
339
+ *
320
340
  * If this is `true` the *hash string* will be passed to `numByteArray()`
321
341
  * with the *default options* and the output from that will be passed to
322
342
  * `base91.encode()`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumjs/encode",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "main": "lib/index.js",
5
5
  "exports":
6
6
  {