@lumjs/encode 2.4.1 → 2.6.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 +16 -1
- package/jsdoc.js +6 -0
- package/lib/base64.js +30 -17
- package/lib/hash.js +1 -1
- package/lib/hotp.js +3 -0
- package/lib/index.js +16 -0
- package/lib/polyfill.js +135 -0
- package/lib/sets/_inc.js +18 -0
- package/lib/sets/all.js +14 -0
- package/lib/sets/base.js +20 -0
- package/lib/sets/digest.js +16 -0
- package/lib/sets/otp.js +18 -0
- package/lib/sets/sign.js +20 -0
- package/lib/totp.js +3 -0
- package/lib/util.js +103 -84
- package/lum.build.js +24 -0
- package/package.json +73 -24
- package/tsconfig.json +12 -0
- package/types/auto/base32.d.ts +72 -0
- package/types/auto/base32.d.ts.map +1 -0
- package/types/auto/base64.d.ts +258 -0
- package/types/auto/base64.d.ts.map +1 -0
- package/types/auto/base91.d.ts +3 -0
- package/types/auto/base91.d.ts.map +1 -0
- package/types/auto/hash.d.ts +216 -0
- package/types/auto/hash.d.ts.map +1 -0
- package/types/auto/hmac.d.ts +35 -0
- package/types/auto/hmac.d.ts.map +1 -0
- package/types/auto/hotp.d.ts +42 -0
- package/types/auto/hotp.d.ts.map +1 -0
- package/types/auto/index.d.ts +2 -0
- package/types/auto/index.d.ts.map +1 -0
- package/types/auto/pem.d.ts +62 -0
- package/types/auto/pem.d.ts.map +1 -0
- package/types/auto/polyfill.d.ts +72 -0
- package/types/auto/polyfill.d.ts.map +1 -0
- package/types/auto/signature.d.ts +25 -0
- package/types/auto/signature.d.ts.map +1 -0
- package/types/auto/totp.d.ts +22 -0
- package/types/auto/totp.d.ts.map +1 -0
- package/types/auto/util.d.ts +26 -0
- package/types/auto/util.d.ts.map +1 -0
- package/types/sets/_inc.d.ts +6 -0
- package/types/sets/all.d.ts +2 -0
- package/types/sets/base.d.ts +6 -0
- package/types/sets/digest.d.ts +4 -0
- package/types/sets/otp.d.ts +5 -0
- package/types/sets/sign.d.ts +6 -0
- package/jsdoc.json +0 -33
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [2.6.0] - 2026-02-24
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## [2.5.0] - 2026-02-20
|
|
13
|
+
### Added
|
|
14
|
+
- A bunch of extra exports that are _module-sets_.
|
|
15
|
+
- `util.bytesToHex()` to be the inverse of `hexToBytes()`.
|
|
16
|
+
- A new `polyfill` sub-module that is not part of the default exports.
|
|
17
|
+
Currently just has polyfills for a few newer Uint8Array methods.
|
|
18
|
+
### Changed
|
|
19
|
+
- Added options to `util.hexToBytes()` for returning a Uint8Array.
|
|
20
|
+
- Added options to `base64.fromBytes()` and `base64.toBytes()`.
|
|
21
|
+
|
|
9
22
|
## [2.4.1] - 2026-02-09
|
|
10
23
|
### Changed
|
|
11
24
|
- Refactored hashifier a bit to make resetting progressive digests simpler.
|
|
@@ -96,7 +109,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
96
109
|
### Added
|
|
97
110
|
- Initial release.
|
|
98
111
|
|
|
99
|
-
[Unreleased]: https://github.com/supernovus/lum.encode.js/compare/v2.
|
|
112
|
+
[Unreleased]: https://github.com/supernovus/lum.encode.js/compare/v2.6.0...HEAD
|
|
113
|
+
[2.6.0]: https://github.com/supernovus/lum.encode.js/compare/v2.5.0...v2.6.0
|
|
114
|
+
[2.5.0]: https://github.com/supernovus/lum.encode.js/compare/v2.4.1...v2.5.0
|
|
100
115
|
[2.4.1]: https://github.com/supernovus/lum.encode.js/compare/v2.4.0...v2.4.1
|
|
101
116
|
[2.4.0]: https://github.com/supernovus/lum.encode.js/compare/v2.3.2...v2.4.0
|
|
102
117
|
[2.3.2]: https://github.com/supernovus/lum.encode.js/compare/v2.3.1...v2.3.2
|
package/jsdoc.js
ADDED
package/lib/base64.js
CHANGED
|
@@ -67,18 +67,24 @@ function deurlize(string)
|
|
|
67
67
|
*
|
|
68
68
|
* This is a low-level function with minimal options.
|
|
69
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.
|
|
74
70
|
*
|
|
75
|
-
* @param {string} base64 - Base64 encoded-string
|
|
76
|
-
* @param {object} [options]
|
|
71
|
+
* @param {string} base64 - Base64 encoded-string.
|
|
72
|
+
* @param {object} [options] Options.
|
|
73
|
+
* @param {boolean} [options.native=true] Use fromBase64() if it exists?
|
|
74
|
+
*
|
|
75
|
+
* When enabled, this checks to see if `Uint8Array.fromBase64()` exists,
|
|
76
|
+
* and if it does, will use it, passing the options to it.
|
|
77
|
+
*
|
|
78
|
+
* If this is disabled, the original toBytes algorithm will be used.
|
|
79
|
+
*
|
|
80
|
+
* This option is enabled by default, the only reason I could think of to
|
|
81
|
+
* disable it is if you are using this function as a polyfill.
|
|
82
|
+
*
|
|
77
83
|
* @returns {Uint8Array}
|
|
78
84
|
*/
|
|
79
85
|
function toBytes(base64, options={})
|
|
80
86
|
{
|
|
81
|
-
if (UI8_FB64)
|
|
87
|
+
if (UI8_FB64 && (options.native ?? true))
|
|
82
88
|
{
|
|
83
89
|
return Uint8Array.fromBase64(base64, options);
|
|
84
90
|
}
|
|
@@ -92,18 +98,24 @@ function toBytes(base64, options={})
|
|
|
92
98
|
*
|
|
93
99
|
* This is a low-level function with no options.
|
|
94
100
|
* 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.
|
|
99
101
|
*
|
|
100
|
-
* @param {Uint8Array} bytes - Byte array to convert
|
|
101
|
-
* @param {object} [options]
|
|
102
|
+
* @param {Uint8Array} bytes - Byte array to convert.
|
|
103
|
+
* @param {object} [options] Options.
|
|
104
|
+
* @param {boolean} [options.native=true] Use toBase64() if it exists?
|
|
105
|
+
*
|
|
106
|
+
* When enabled, this checks to see if `bytes.toBase64()` exists,
|
|
107
|
+
* and if it does, will use it, passing the options to it.
|
|
108
|
+
*
|
|
109
|
+
* If this is disabled, the original fromBytes algorithm will be used.
|
|
110
|
+
*
|
|
111
|
+
* This option is enabled by default, the only reason I could think of to
|
|
112
|
+
* disable it is if you are using this function as a polyfill.
|
|
113
|
+
*
|
|
102
114
|
* @returns {string}
|
|
103
115
|
*/
|
|
104
116
|
function fromBytes(bytes, options={})
|
|
105
117
|
{
|
|
106
|
-
if (typeof bytes.toBase64 === 'function')
|
|
118
|
+
if ((options.native ?? true) && typeof bytes.toBase64 === 'function')
|
|
107
119
|
{
|
|
108
120
|
return bytes.toBase64(options);
|
|
109
121
|
}
|
|
@@ -127,10 +139,11 @@ function fromBytes(bytes, options={})
|
|
|
127
139
|
*
|
|
128
140
|
* If this is a Uint8Array, we can skip the TextEncoder part.
|
|
129
141
|
*
|
|
130
|
-
* @param {(object|boolean)} [options] Options
|
|
142
|
+
* @param {(object|boolean)} [options] Options.
|
|
131
143
|
*
|
|
132
|
-
* - If
|
|
133
|
-
* - Passed to
|
|
144
|
+
* - If boolean, used as `options.url`.
|
|
145
|
+
* - Passed to urlize() if `options.url` is true.
|
|
146
|
+
* - Always passed to fromBytes().
|
|
134
147
|
*
|
|
135
148
|
* @param {boolean} [options.url=false] Urlize the output?
|
|
136
149
|
* If true, converts `+`, `/`, and `=` to URL-friendly alternatives.
|
package/lib/hash.js
CHANGED
package/lib/hotp.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A full set of encoding utilities.
|
|
3
|
+
*
|
|
4
|
+
* This module includes alias properties for all sub-modules except `polyfill`.
|
|
5
|
+
*
|
|
6
|
+
* This currently uses lazy-loading for everything other than `util`,
|
|
7
|
+
* but in version 3.0 I will be rewriting this package into ESM format,
|
|
8
|
+
* and the default module will become an alias for the `all` sub-module.
|
|
9
|
+
*
|
|
10
|
+
* I'd suggest using one of the named sub-modules or module-sets if you
|
|
11
|
+
* want to load only specific components.
|
|
12
|
+
*
|
|
3
13
|
* @module @lumjs/encode
|
|
4
14
|
*/
|
|
5
15
|
|
|
@@ -7,6 +17,12 @@ const {df,lazy} = require('@lumjs/core');
|
|
|
7
17
|
const E = {enumerable: true};
|
|
8
18
|
const util = require('./util');
|
|
9
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @alias module:@lumjs/encode.util
|
|
22
|
+
* @see {@link module:@lumjs/encode/util}
|
|
23
|
+
*/
|
|
24
|
+
df(exports, 'Util', {value: util}, E);
|
|
25
|
+
|
|
10
26
|
/**
|
|
11
27
|
* @alias module:@lumjs/encode.util
|
|
12
28
|
* @see {@link module:@lumjs/encode/util}
|
package/lib/polyfill.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Some simple polyfills.
|
|
3
|
+
* @module @lumjs/encode/polyfill
|
|
4
|
+
*/
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
const cp = Object.assign;
|
|
8
|
+
const { isComplex, isObj } = require('@lumjs/core/types');
|
|
9
|
+
const { bytesToHex, hexToBytes } = require('./util.js');
|
|
10
|
+
const { fromBytes: bytesToB64, toBytes: b64ToBytes } = require('./base64.js');
|
|
11
|
+
|
|
12
|
+
const NON = { native: false };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Polyfills for Uint8Array.
|
|
16
|
+
*
|
|
17
|
+
* This is an array of two ruleset objects:
|
|
18
|
+
*
|
|
19
|
+
* - Uint8Array
|
|
20
|
+
* - fromBase64()
|
|
21
|
+
* - fromHex()
|
|
22
|
+
* - Uint8Array.prototype
|
|
23
|
+
* - toBase64()
|
|
24
|
+
* - toHex()
|
|
25
|
+
*
|
|
26
|
+
* @alias module:@lumjs/encode/polyfill.UI8
|
|
27
|
+
* @type {PolyfillRuleset[]}
|
|
28
|
+
*/
|
|
29
|
+
const UI8 = [
|
|
30
|
+
{
|
|
31
|
+
into: Uint8Array,
|
|
32
|
+
fill: {
|
|
33
|
+
fromBase64(string, options) {
|
|
34
|
+
return b64ToBytes(string, cp({}, options, NON));
|
|
35
|
+
},
|
|
36
|
+
fromHex(string) {
|
|
37
|
+
return hexToBytes(string, true, false);
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
into: Uint8Array.prototype,
|
|
43
|
+
fill: {
|
|
44
|
+
toBase64(options) {
|
|
45
|
+
return bytesToB64(this, cp({}, options, NON))
|
|
46
|
+
},
|
|
47
|
+
toHex() {
|
|
48
|
+
return bytesToHex(this, false);
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
const isPolyfill = v => (isObj(v)
|
|
55
|
+
&& isComplex(v.into)
|
|
56
|
+
&& isObj(v.fill)
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Process one or more polyfill definitions.
|
|
61
|
+
* @param {(PolyfillRuleset|PolyfillRuleset[])} rules - Rulesets to apply.
|
|
62
|
+
*
|
|
63
|
+
* You can pass a single Ruleset object, or an array of associated Rulesets.
|
|
64
|
+
*
|
|
65
|
+
* @returns {Map}
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
*
|
|
69
|
+
* For these examples I'll be applying the `UI8` polyfills.
|
|
70
|
+
*
|
|
71
|
+
* **Usage if using *ESModules***:
|
|
72
|
+
*
|
|
73
|
+
* ```js
|
|
74
|
+
* import { polyfill, UI8 } from '@lumjs/encode/polyfill';
|
|
75
|
+
* polyfill(UI8);
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* **OR if using *CommonJS***:
|
|
79
|
+
*
|
|
80
|
+
* ```js
|
|
81
|
+
* const { polyfill, UI8 } = require('@lumjs/encode/polyfill');
|
|
82
|
+
* polyfill(UI8);
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @alias module:@lumjs/encode/polyfill.polyfill
|
|
86
|
+
*/
|
|
87
|
+
function polyfill(rules) {
|
|
88
|
+
let filled = new Map();
|
|
89
|
+
|
|
90
|
+
if (!Array.isArray(rules)) {
|
|
91
|
+
rules = [rules];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (let rule of rules) {
|
|
95
|
+
|
|
96
|
+
if (Array.isArray(rule)) {
|
|
97
|
+
polyfill(...rule);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
else if (!isPolyfill(rule)) {
|
|
101
|
+
console.error('invalid polyfill definition', rule);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let target = rule.into;
|
|
106
|
+
for (let meth in rule.fill) {
|
|
107
|
+
if (target[meth] === undefined) {
|
|
108
|
+
target[meth] = rule.fill[meth];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return filled;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
module.exports = {NON, UI8, isPolyfill, polyfill}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Polyfill Ruleset
|
|
120
|
+
* @typedef {object} PolyfillRuleset
|
|
121
|
+
*
|
|
122
|
+
* @prop {(object|function)} into - The target to polyfill.
|
|
123
|
+
*
|
|
124
|
+
* Class constructors are functions, class prototypes are objects.
|
|
125
|
+
* Each Ruleset may only have one target, so if you need to polyfill
|
|
126
|
+
* both static and instance methods you'll need to use an array with
|
|
127
|
+
* a Ruleset for the constructor, and a Ruleset for the prototype.
|
|
128
|
+
*
|
|
129
|
+
* @prop {object} fill - An object containing the polyfill methods.
|
|
130
|
+
*
|
|
131
|
+
* Any method in this object that doesn't exist in the `for` target,
|
|
132
|
+
* will be added to it. As these are expected to be methods respecting
|
|
133
|
+
* the `this` context variable they should NOT be arrow (`=>`) closures.
|
|
134
|
+
*
|
|
135
|
+
*/
|
package/lib/sets/_inc.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make lowercase aliases for every key in an object.
|
|
3
|
+
* @param {object} obj - Target object.
|
|
4
|
+
* @returns {object} `obj`
|
|
5
|
+
*/
|
|
6
|
+
function makeSet(obj) {
|
|
7
|
+
for (let key in obj) {
|
|
8
|
+
let lc = key.toLowerCase();
|
|
9
|
+
if (key !== lc) {
|
|
10
|
+
obj[lc] = obj[key];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return obj;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
makeSet,
|
|
18
|
+
}
|
package/lib/sets/all.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { makeSet } = require('./_inc.js');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A module-set that includes all sub-modules except `polyfill`.
|
|
5
|
+
*
|
|
6
|
+
* More specifically this composes together the `digest` and `otp`
|
|
7
|
+
* module-sets (which in turn compose `base` and `sign` respectively).
|
|
8
|
+
*
|
|
9
|
+
* @alias module:@lumjs/encode/all
|
|
10
|
+
*/
|
|
11
|
+
module.exports = makeSet({
|
|
12
|
+
...(require('./digest')),
|
|
13
|
+
...(require('./otp')),
|
|
14
|
+
});
|
package/lib/sets/base.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const { makeSet } = require('./_inc.js');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A module-set for working with encodings.
|
|
5
|
+
*
|
|
6
|
+
* Includes:
|
|
7
|
+
* - Base32
|
|
8
|
+
* - Base64
|
|
9
|
+
* - Base91
|
|
10
|
+
* - Util
|
|
11
|
+
* As well as lowercase aliases for all of those.
|
|
12
|
+
*
|
|
13
|
+
* @module @lumjs/encode/base
|
|
14
|
+
*/
|
|
15
|
+
module.exports = makeSet({
|
|
16
|
+
Base32: require('../base32'),
|
|
17
|
+
Base64: require('../base64'),
|
|
18
|
+
Base91: require('../base91'),
|
|
19
|
+
Util: require('../util'),
|
|
20
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const { makeSet } = require('./_inc.js');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A module-set for working with digests (aka data hashes).
|
|
5
|
+
*
|
|
6
|
+
* Includes:
|
|
7
|
+
* - Hash
|
|
8
|
+
* As well as lowercase aliases for all of those,
|
|
9
|
+
* and everything from the `base` set.
|
|
10
|
+
*
|
|
11
|
+
* @module @lumjs/encode/digest
|
|
12
|
+
*/
|
|
13
|
+
module.exports = makeSet({
|
|
14
|
+
...(require('./base.js')),
|
|
15
|
+
Hash: require('../hash.js'),
|
|
16
|
+
});
|
package/lib/sets/otp.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const { makeSet } = require('./_inc.js');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A module-set for working with One-Time-Passwords (OTP).
|
|
5
|
+
*
|
|
6
|
+
* Includes:
|
|
7
|
+
* - HOTP
|
|
8
|
+
* - TOTP
|
|
9
|
+
* As well as lowercase aliases for all of those,
|
|
10
|
+
* and everything from the `sign` set.
|
|
11
|
+
*
|
|
12
|
+
* @module @lumjs/encode/otp
|
|
13
|
+
*/
|
|
14
|
+
module.exports = makeSet({
|
|
15
|
+
...(require('./sign')),
|
|
16
|
+
HOTP: require('../hotp'),
|
|
17
|
+
TOTP: require('../totp'),
|
|
18
|
+
});
|
package/lib/sets/sign.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const { makeSet } = require('./_inc.js');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A module-set for working with digital signatures.
|
|
5
|
+
*
|
|
6
|
+
* Includes:
|
|
7
|
+
* - HMAC
|
|
8
|
+
* - PEM
|
|
9
|
+
* - Signature
|
|
10
|
+
* - Util
|
|
11
|
+
* As well as lowercase aliases for all of those.
|
|
12
|
+
*
|
|
13
|
+
* @module @lumjs/encode/sign
|
|
14
|
+
*/
|
|
15
|
+
module.exports = makeSet({
|
|
16
|
+
HMAC: require('../hmac.js'),
|
|
17
|
+
PEM: require('../pem.js'),
|
|
18
|
+
Signature: require('../signature.js'),
|
|
19
|
+
Util: require('../util.js'),
|
|
20
|
+
});
|