@metamask-previews/utils 11.12.1-preview-9962b7e → 11.12.1-preview-c44fd50
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 +1 -5
- package/dist/assert.cjs +122 -0
- package/dist/assert.cjs.map +1 -0
- package/dist/{assert.d.ts → assert.d.cts} +2 -2
- package/dist/assert.d.cts.map +1 -0
- package/dist/assert.d.mts +61 -0
- package/dist/assert.d.mts.map +1 -0
- package/dist/{assert.js → assert.mjs} +3 -3
- package/dist/assert.mjs.map +1 -0
- package/dist/base64.cjs +34 -0
- package/dist/base64.cjs.map +1 -0
- package/dist/{base64.d.ts → base64.d.cts} +2 -2
- package/dist/base64.d.cts.map +1 -0
- package/dist/base64.d.mts +25 -0
- package/dist/base64.d.mts.map +1 -0
- package/dist/{base64.js → base64.mjs} +3 -3
- package/dist/base64.mjs.map +1 -0
- package/dist/bytes.cjs +427 -0
- package/dist/bytes.cjs.map +1 -0
- package/dist/{bytes.d.ts → bytes.d.cts} +2 -2
- package/dist/bytes.d.cts.map +1 -0
- package/dist/bytes.d.mts +198 -0
- package/dist/bytes.d.mts.map +1 -0
- package/dist/{bytes.js → bytes.mjs} +4 -4
- package/dist/bytes.mjs.map +1 -0
- package/dist/caip-types.cjs +390 -0
- package/dist/caip-types.cjs.map +1 -0
- package/dist/{caip-types.d.ts → caip-types.d.cts} +2 -2
- package/dist/caip-types.d.cts.map +1 -0
- package/dist/caip-types.d.mts +294 -0
- package/dist/caip-types.d.mts.map +1 -0
- package/dist/{caip-types.js → caip-types.mjs} +2 -2
- package/dist/caip-types.mjs.map +1 -0
- package/dist/checksum.cjs +7 -0
- package/dist/checksum.cjs.map +1 -0
- package/dist/{checksum.d.ts → checksum.d.cts} +1 -1
- package/dist/checksum.d.cts.map +1 -0
- package/dist/checksum.d.mts +2 -0
- package/dist/checksum.d.mts.map +1 -0
- package/dist/checksum.mjs +4 -0
- package/dist/checksum.mjs.map +1 -0
- package/dist/coercers.cjs +166 -0
- package/dist/coercers.cjs.map +1 -0
- package/dist/{coercers.d.ts → coercers.d.cts} +4 -4
- package/dist/coercers.d.cts.map +1 -0
- package/dist/coercers.d.mts +97 -0
- package/dist/coercers.d.mts.map +1 -0
- package/dist/{coercers.js → coercers.mjs} +5 -5
- package/dist/coercers.mjs.map +1 -0
- package/dist/collections.cjs +109 -0
- package/dist/collections.cjs.map +1 -0
- package/dist/{collections.d.ts → collections.d.cts} +9 -9
- package/dist/collections.d.cts.map +1 -0
- package/dist/collections.d.mts +39 -0
- package/dist/collections.d.mts.map +1 -0
- package/dist/{collections.js → collections.mjs} +1 -1
- package/dist/collections.mjs.map +1 -0
- package/dist/encryption-types.cjs +3 -0
- package/dist/encryption-types.cjs.map +1 -0
- package/dist/{encryption-types.d.ts → encryption-types.d.cts} +1 -1
- package/dist/encryption-types.d.cts.map +1 -0
- package/dist/encryption-types.d.mts +7 -0
- package/dist/encryption-types.d.mts.map +1 -0
- package/dist/encryption-types.mjs +2 -0
- package/dist/encryption-types.mjs.map +1 -0
- package/dist/errors.cjs +129 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/{errors.d.ts → errors.d.cts} +1 -1
- package/dist/errors.d.cts.map +1 -0
- package/dist/errors.d.mts +68 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/{errors.js → errors.mjs} +3 -4
- package/dist/errors.mjs.map +1 -0
- package/dist/fs.cjs +248 -0
- package/dist/fs.cjs.map +1 -0
- package/dist/{fs.d.ts → fs.d.cts} +2 -2
- package/dist/fs.d.cts.map +1 -0
- package/dist/fs.d.mts +133 -0
- package/dist/fs.d.mts.map +1 -0
- package/dist/{fs.js → fs.mjs} +6 -6
- package/dist/{fs.js.map → fs.mjs.map} +1 -1
- package/dist/hashing.cjs +60 -0
- package/dist/hashing.cjs.map +1 -0
- package/dist/{hashing.d.ts → hashing.d.cts} +1 -1
- package/dist/hashing.d.cts.map +1 -0
- package/dist/hashing.d.mts +28 -0
- package/dist/hashing.d.mts.map +1 -0
- package/dist/{hashing.js → hashing.mjs} +3 -8
- package/dist/hashing.mjs.map +1 -0
- package/dist/hex.cjs +188 -0
- package/dist/hex.cjs.map +1 -0
- package/dist/{hex.d.ts → hex.d.cts} +6 -5
- package/dist/hex.d.cts.map +1 -0
- package/dist/hex.d.mts +118 -0
- package/dist/hex.d.mts.map +1 -0
- package/dist/{hex.js → hex.mjs} +6 -5
- package/dist/hex.mjs.map +1 -0
- package/dist/index.cjs +58 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +26 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +26 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +21 -0
- package/dist/index.mjs.map +1 -0
- package/dist/json.cjs +425 -0
- package/dist/json.cjs.map +1 -0
- package/dist/{json.d.ts → json.d.cts} +25 -25
- package/dist/json.d.cts.map +1 -0
- package/dist/json.d.mts +398 -0
- package/dist/json.d.mts.map +1 -0
- package/dist/{json.js → json.mjs} +4 -4
- package/dist/json.mjs.map +1 -0
- package/dist/keyring.cjs +3 -0
- package/dist/keyring.cjs.map +1 -0
- package/dist/{keyring.d.ts → keyring.d.cts} +5 -5
- package/dist/keyring.d.cts.map +1 -0
- package/dist/keyring.d.mts +243 -0
- package/dist/keyring.d.mts.map +1 -0
- package/dist/keyring.mjs +2 -0
- package/dist/keyring.mjs.map +1 -0
- package/dist/logging.cjs +43 -0
- package/dist/logging.cjs.map +1 -0
- package/dist/{logging.d.ts → logging.d.cts} +2 -2
- package/dist/logging.d.cts.map +1 -0
- package/dist/logging.d.mts +30 -0
- package/dist/logging.d.mts.map +1 -0
- package/dist/{logging.js → logging.mjs} +9 -2
- package/dist/logging.mjs.map +1 -0
- package/dist/misc.cjs +154 -0
- package/dist/misc.cjs.map +1 -0
- package/dist/{misc.d.ts → misc.d.cts} +1 -1
- package/dist/misc.d.cts.map +1 -0
- package/dist/misc.d.mts +127 -0
- package/dist/misc.d.mts.map +1 -0
- package/dist/{misc.js → misc.mjs} +1 -1
- package/dist/misc.mjs.map +1 -0
- package/dist/mnemonic.cjs +53 -0
- package/dist/mnemonic.cjs.map +1 -0
- package/dist/{mnemonic.d.ts → mnemonic.d.cts} +1 -1
- package/dist/mnemonic.d.cts.map +1 -0
- package/dist/mnemonic.d.mts +14 -0
- package/dist/mnemonic.d.mts.map +1 -0
- package/dist/{mnemonic.js → mnemonic.mjs} +2 -2
- package/dist/mnemonic.mjs.map +1 -0
- package/dist/node.cjs +19 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.cts +3 -0
- package/dist/node.d.cts.map +1 -0
- package/dist/node.d.mts +3 -0
- package/dist/node.d.mts.map +1 -0
- package/dist/node.mjs +3 -0
- package/dist/node.mjs.map +1 -0
- package/dist/number.cjs +102 -0
- package/dist/number.cjs.map +1 -0
- package/dist/{number.d.ts → number.d.cts} +2 -2
- package/dist/number.d.cts.map +1 -0
- package/dist/number.d.mts +74 -0
- package/dist/number.d.mts.map +1 -0
- package/dist/{number.js → number.mjs} +3 -3
- package/dist/number.mjs.map +1 -0
- package/dist/opaque.cjs +3 -0
- package/dist/opaque.cjs.map +1 -0
- package/dist/{opaque.d.ts → opaque.d.cts} +1 -1
- package/dist/opaque.d.cts.map +1 -0
- package/dist/opaque.d.mts +6 -0
- package/dist/opaque.d.mts.map +1 -0
- package/dist/opaque.mjs +2 -0
- package/dist/opaque.mjs.map +1 -0
- package/dist/promise.cjs +44 -0
- package/dist/promise.cjs.map +1 -0
- package/dist/{promise.d.ts → promise.d.cts} +1 -1
- package/dist/promise.d.cts.map +1 -0
- package/dist/promise.d.mts +45 -0
- package/dist/promise.d.mts.map +1 -0
- package/dist/{promise.js → promise.mjs} +1 -1
- package/dist/promise.mjs.map +1 -0
- package/dist/superstruct.cjs +28 -0
- package/dist/superstruct.cjs.map +1 -0
- package/dist/{superstruct.d.ts → superstruct.d.cts} +2 -2
- package/dist/superstruct.d.cts.map +1 -0
- package/dist/superstruct.d.mts +20 -0
- package/dist/superstruct.d.mts.map +1 -0
- package/dist/{superstruct.js → superstruct.mjs} +2 -2
- package/dist/superstruct.mjs.map +1 -0
- package/dist/time.cjs +67 -0
- package/dist/time.cjs.map +1 -0
- package/dist/{time.d.ts → time.d.cts} +1 -1
- package/dist/time.d.cts.map +1 -0
- package/dist/time.d.mts +49 -0
- package/dist/time.d.mts.map +1 -0
- package/dist/{time.js → time.mjs} +1 -1
- package/dist/time.mjs.map +1 -0
- package/dist/transaction-types.cjs +3 -0
- package/dist/transaction-types.cjs.map +1 -0
- package/dist/{transaction-types.d.ts → transaction-types.d.cts} +3 -3
- package/dist/transaction-types.d.cts.map +1 -0
- package/dist/transaction-types.d.mts +117 -0
- package/dist/transaction-types.d.mts.map +1 -0
- package/dist/transaction-types.mjs +2 -0
- package/dist/transaction-types.mjs.map +1 -0
- package/dist/unitsConversion.cjs +217 -0
- package/dist/unitsConversion.cjs.map +1 -0
- package/dist/{unitsConversion.d.ts → unitsConversion.d.cts} +28 -28
- package/dist/unitsConversion.d.cts.map +1 -0
- package/dist/unitsConversion.d.mts +80 -0
- package/dist/unitsConversion.d.mts.map +1 -0
- package/dist/{unitsConversion.js → unitsConversion.mjs} +1 -1
- package/dist/unitsConversion.mjs.map +1 -0
- package/dist/versions.cjs +95 -0
- package/dist/versions.cjs.map +1 -0
- package/dist/{versions.d.ts → versions.d.cts} +3 -3
- package/dist/versions.d.cts.map +1 -0
- package/dist/versions.d.mts +101 -0
- package/dist/versions.d.mts.map +1 -0
- package/dist/{versions.js → versions.mjs} +4 -4
- package/dist/versions.mjs.map +1 -0
- package/package.json +25 -15
- package/dist/assert.d.ts.map +0 -1
- package/dist/assert.js.map +0 -1
- package/dist/base64.d.ts.map +0 -1
- package/dist/base64.js.map +0 -1
- package/dist/bytes.d.ts.map +0 -1
- package/dist/bytes.js.map +0 -1
- package/dist/caip-types.d.ts.map +0 -1
- package/dist/caip-types.js.map +0 -1
- package/dist/checksum.d.ts.map +0 -1
- package/dist/checksum.js +0 -4
- package/dist/checksum.js.map +0 -1
- package/dist/coercers.d.ts.map +0 -1
- package/dist/coercers.js.map +0 -1
- package/dist/collections.d.ts.map +0 -1
- package/dist/collections.js.map +0 -1
- package/dist/encryption-types.d.ts.map +0 -1
- package/dist/encryption-types.js +0 -2
- package/dist/encryption-types.js.map +0 -1
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js.map +0 -1
- package/dist/fs.d.ts.map +0 -1
- package/dist/hashing.d.ts.map +0 -1
- package/dist/hashing.js.map +0 -1
- package/dist/hex.d.ts.map +0 -1
- package/dist/hex.js.map +0 -1
- package/dist/index.d.ts +0 -26
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -21
- package/dist/index.js.map +0 -1
- package/dist/json.d.ts.map +0 -1
- package/dist/json.js.map +0 -1
- package/dist/keyring.d.ts.map +0 -1
- package/dist/keyring.js +0 -2
- package/dist/keyring.js.map +0 -1
- package/dist/logging.d.ts.map +0 -1
- package/dist/logging.js.map +0 -1
- package/dist/misc.d.ts.map +0 -1
- package/dist/misc.js.map +0 -1
- package/dist/mnemonic.d.ts.map +0 -1
- package/dist/mnemonic.js.map +0 -1
- package/dist/node.d.ts +0 -3
- package/dist/node.d.ts.map +0 -1
- package/dist/node.js +0 -3
- package/dist/node.js.map +0 -1
- package/dist/number.d.ts.map +0 -1
- package/dist/number.js.map +0 -1
- package/dist/opaque.d.ts.map +0 -1
- package/dist/opaque.js +0 -2
- package/dist/opaque.js.map +0 -1
- package/dist/promise.d.ts.map +0 -1
- package/dist/promise.js.map +0 -1
- package/dist/superstruct.d.ts.map +0 -1
- package/dist/superstruct.js.map +0 -1
- package/dist/time.d.ts.map +0 -1
- package/dist/time.js.map +0 -1
- package/dist/transaction-types.d.ts.map +0 -1
- package/dist/transaction-types.js +0 -2
- package/dist/transaction-types.js.map +0 -1
- package/dist/unitsConversion.d.ts.map +0 -1
- package/dist/unitsConversion.js.map +0 -1
- package/dist/versions.d.ts.map +0 -1
- package/dist/versions.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,12 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
-
- **BREAKING:** Drop support for Node 18 and 20 ([#
|
|
12
|
+
- **BREAKING:** Drop support for Node 18 and 20 ([#328](https://github.com/MetaMask/utils/pull/328))
|
|
13
13
|
- The supported range is now `^22.14.0 || ^24`, matching core.
|
|
14
|
-
- **BREAKING:** The package is now ESM only ([#323](https://github.com/MetaMask/utils/pull/323))
|
|
15
|
-
- The CommonJS build is gone. `require('@metamask/utils')` now fails with `ERR_REQUIRE_ESM`; use `import` instead.
|
|
16
|
-
- `main` and `module` are removed. Both `.` and `./node` resolve through `exports` to a single `./dist/*.js` with `./dist/*.d.ts` types.
|
|
17
|
-
- Consumers already using `import` are unaffected.
|
|
18
14
|
- Bump `@ethereumjs/tx` from `^4.2.0` to `^5.4.0` ([#321](https://github.com/MetaMask/utils/pull/321))
|
|
19
15
|
- The deprecated `Keyring.signTransaction` now returns `LegacyTxData` rather than `TxData`. These describe the same shape: `@ethereumjs/tx@5` repurposed the name `TxData` for a map keyed by transaction type and renamed the old meaning to `LegacyTxData`. Implementations do not need changing.
|
|
20
16
|
- Bump `@metamask/scure-bip39` from `^2.0.3` to `^2.1.1` ([#311](https://github.com/MetaMask/utils/pull/311))
|
package/dist/assert.cjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertExhaustive = exports.assertStruct = exports.assert = exports.AssertionError = void 0;
|
|
4
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
5
|
+
const errors_1 = require("./errors.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Check if a value is a constructor, i.e., a function that can be called with
|
|
8
|
+
* the `new` keyword.
|
|
9
|
+
*
|
|
10
|
+
* @param fn - The value to check.
|
|
11
|
+
* @returns `true` if the value is a constructor, or `false` otherwise.
|
|
12
|
+
*/
|
|
13
|
+
function isConstructable(fn) {
|
|
14
|
+
/* istanbul ignore next */
|
|
15
|
+
return Boolean(typeof fn?.prototype?.constructor?.name === 'string');
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Attempts to obtain the message from a possible error object. If it is
|
|
19
|
+
* possible to do so, any trailing period will be removed from the message;
|
|
20
|
+
* otherwise an empty string is returned.
|
|
21
|
+
*
|
|
22
|
+
* @param error - The error object to get the message from.
|
|
23
|
+
* @returns The message without any trailing period if `error` is an object
|
|
24
|
+
* with a `message` property; the string version of `error` without any trailing
|
|
25
|
+
* period if it is not `undefined` or `null`; otherwise an empty string.
|
|
26
|
+
*/
|
|
27
|
+
function getErrorMessageWithoutTrailingPeriod(error) {
|
|
28
|
+
// We'll add our own period.
|
|
29
|
+
return (0, errors_1.getErrorMessage)(error).replace(/\.$/u, '');
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Initialise an {@link AssertionErrorConstructor} error.
|
|
33
|
+
*
|
|
34
|
+
* @param ErrorWrapper - The error class to use.
|
|
35
|
+
* @param message - The error message.
|
|
36
|
+
* @returns The error object.
|
|
37
|
+
*/
|
|
38
|
+
function getError(ErrorWrapper, message) {
|
|
39
|
+
if (isConstructable(ErrorWrapper)) {
|
|
40
|
+
return new ErrorWrapper({
|
|
41
|
+
message,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return ErrorWrapper({
|
|
45
|
+
message,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The default error class that is thrown if an assertion fails.
|
|
50
|
+
*/
|
|
51
|
+
class AssertionError extends Error {
|
|
52
|
+
constructor(options) {
|
|
53
|
+
super(options.message);
|
|
54
|
+
this.code = 'ERR_ASSERTION';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.AssertionError = AssertionError;
|
|
58
|
+
/**
|
|
59
|
+
* Same as Node.js assert.
|
|
60
|
+
* If the value is falsy, throws an error, does nothing otherwise.
|
|
61
|
+
*
|
|
62
|
+
* @throws {@link AssertionError} If value is falsy.
|
|
63
|
+
* @param value - The test that should be truthy to pass.
|
|
64
|
+
* @param message - Message to be passed to {@link AssertionError} or an
|
|
65
|
+
* {@link Error} instance to throw.
|
|
66
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
67
|
+
* Defaults to {@link AssertionError}. If a custom error class is provided for
|
|
68
|
+
* the `message` argument, this argument is ignored.
|
|
69
|
+
*/
|
|
70
|
+
function assert(value, message = 'Assertion failed.', ErrorWrapper = AssertionError) {
|
|
71
|
+
if (!value) {
|
|
72
|
+
if (message instanceof Error) {
|
|
73
|
+
throw message;
|
|
74
|
+
}
|
|
75
|
+
throw getError(ErrorWrapper, message);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.assert = assert;
|
|
79
|
+
/**
|
|
80
|
+
* Assert a value against a Superstruct struct.
|
|
81
|
+
*
|
|
82
|
+
* @param value - The value to validate.
|
|
83
|
+
* @param struct - The struct to validate against.
|
|
84
|
+
* @param errorPrefix - A prefix to add to the error message. Defaults to
|
|
85
|
+
* "Assertion failed".
|
|
86
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
87
|
+
* Defaults to {@link AssertionError}.
|
|
88
|
+
* @throws If the value is not valid.
|
|
89
|
+
*/
|
|
90
|
+
function assertStruct(value, struct, errorPrefix = 'Assertion failed', ErrorWrapper = AssertionError) {
|
|
91
|
+
try {
|
|
92
|
+
(0, superstruct_1.assert)(value, struct);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw getError(ErrorWrapper, `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.assertStruct = assertStruct;
|
|
99
|
+
/**
|
|
100
|
+
* Use in the default case of a switch that you want to be fully exhaustive.
|
|
101
|
+
* Using this function forces the compiler to enforce exhaustivity during
|
|
102
|
+
* compile-time.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```
|
|
106
|
+
* const number = 1;
|
|
107
|
+
* switch (number) {
|
|
108
|
+
* case 0:
|
|
109
|
+
* ...
|
|
110
|
+
* case 1:
|
|
111
|
+
* ...
|
|
112
|
+
* default:
|
|
113
|
+
* assertExhaustive(snapPrefix);
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
* @param _object - The object on which the switch is being operated.
|
|
117
|
+
*/
|
|
118
|
+
function assertExhaustive(_object) {
|
|
119
|
+
throw new Error('Invalid branch reached. Should be detected during compilation.');
|
|
120
|
+
}
|
|
121
|
+
exports.assertExhaustive = assertExhaustive;
|
|
122
|
+
//# sourceMappingURL=assert.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.cjs","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":";;;AACA,uDAAoE;AAEpE,yCAA2C;AAM3C;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,EAA6B;IAE7B,0BAA0B;IAC1B,OAAO,OAAO,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,oCAAoC,CAAC,KAAc;IAC1D,4BAA4B;IAC5B,OAAO,IAAA,wBAAe,EAAC,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AAEH,SAAS,QAAQ,CAAC,YAAuC,EAAE,OAAe;IACxE,IAAI,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAClC,OAAO,IAAI,YAAY,CAAC;YACtB,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IACD,OAAO,YAAY,CAAC;QAClB,OAAO;KACR,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAa,cAAe,SAAQ,KAAK;IAGvC,YAAY,OAA4B;QACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAHhB,SAAI,GAAG,eAAe,CAAC;IAIhC,CAAC;CACF;AAND,wCAMC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,MAAM,CACpB,KAAU,EACV,UAA0B,mBAAmB,EAE7C,eAA0C,cAAc;IAExD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,IAAI,OAAO,YAAY,KAAK,EAAE,CAAC;YAC7B,MAAM,OAAO,CAAC;QAChB,CAAC;QAED,MAAM,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;AACH,CAAC;AAbD,wBAaC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,YAAY,CAC1B,KAAc,EACd,MAA4B,EAC5B,WAAW,GAAG,kBAAkB,EAEhC,eAA0C,cAAc;IAExD,IAAI,CAAC;QACH,IAAA,oBAAiB,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,QAAQ,CACZ,YAAY,EACZ,GAAG,WAAW,KAAK,oCAAoC,CAAC,KAAK,CAAC,GAAG,CAClE,CAAC;IACJ,CAAC;AACH,CAAC;AAfD,oCAeC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,gBAAgB,CAAC,OAAc;IAC7C,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;AACJ,CAAC;AAJD,4CAIC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { assert as assertSuperstruct } from '@metamask/superstruct';\n\nimport { getErrorMessage } from './errors';\n\nexport type AssertionErrorConstructor =\n | (new (args: { message: string }) => Error)\n | ((args: { message: string }) => Error);\n\n/**\n * Check if a value is a constructor, i.e., a function that can be called with\n * the `new` keyword.\n *\n * @param fn - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nfunction isConstructable(\n fn: AssertionErrorConstructor,\n): fn is new (args: { message: string }) => Error {\n /* istanbul ignore next */\n return Boolean(typeof fn?.prototype?.constructor?.name === 'string');\n}\n\n/**\n * Attempts to obtain the message from a possible error object. If it is\n * possible to do so, any trailing period will be removed from the message;\n * otherwise an empty string is returned.\n *\n * @param error - The error object to get the message from.\n * @returns The message without any trailing period if `error` is an object\n * with a `message` property; the string version of `error` without any trailing\n * period if it is not `undefined` or `null`; otherwise an empty string.\n */\nfunction getErrorMessageWithoutTrailingPeriod(error: unknown): string {\n // We'll add our own period.\n return getErrorMessage(error).replace(/\\.$/u, '');\n}\n\n/**\n * Initialise an {@link AssertionErrorConstructor} error.\n *\n * @param ErrorWrapper - The error class to use.\n * @param message - The error message.\n * @returns The error object.\n */\n\nfunction getError(ErrorWrapper: AssertionErrorConstructor, message: string) {\n if (isConstructable(ErrorWrapper)) {\n return new ErrorWrapper({\n message,\n });\n }\n return ErrorWrapper({\n message,\n });\n}\n\n/**\n * The default error class that is thrown if an assertion fails.\n */\nexport class AssertionError extends Error {\n readonly code = 'ERR_ASSERTION';\n\n constructor(options: { message: string }) {\n super(options.message);\n }\n}\n\n/**\n * Same as Node.js assert.\n * If the value is falsy, throws an error, does nothing otherwise.\n *\n * @throws {@link AssertionError} If value is falsy.\n * @param value - The test that should be truthy to pass.\n * @param message - Message to be passed to {@link AssertionError} or an\n * {@link Error} instance to throw.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}. If a custom error class is provided for\n * the `message` argument, this argument is ignored.\n */\nexport function assert(\n value: any,\n message: string | Error = 'Assertion failed.',\n\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value {\n if (!value) {\n if (message instanceof Error) {\n throw message;\n }\n\n throw getError(ErrorWrapper, message);\n }\n}\n\n/**\n * Assert a value against a Superstruct struct.\n *\n * @param value - The value to validate.\n * @param struct - The struct to validate against.\n * @param errorPrefix - A prefix to add to the error message. Defaults to\n * \"Assertion failed\".\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the value is not valid.\n */\nexport function assertStruct<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n errorPrefix = 'Assertion failed',\n\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value is Type {\n try {\n assertSuperstruct(value, struct);\n } catch (error) {\n throw getError(\n ErrorWrapper,\n `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`,\n );\n }\n}\n\n/**\n * Use in the default case of a switch that you want to be fully exhaustive.\n * Using this function forces the compiler to enforce exhaustivity during\n * compile-time.\n *\n * @example\n * ```\n * const number = 1;\n * switch (number) {\n * case 0:\n * ...\n * case 1:\n * ...\n * default:\n * assertExhaustive(snapPrefix);\n * }\n * ```\n * @param _object - The object on which the switch is being operated.\n */\nexport function assertExhaustive(_object: never): never {\n throw new Error(\n 'Invalid branch reached. Should be detected during compilation.',\n );\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Struct } from
|
|
1
|
+
import type { Struct } from "@metamask/superstruct";
|
|
2
2
|
export type AssertionErrorConstructor = (new (args: {
|
|
3
3
|
message: string;
|
|
4
4
|
}) => Error) | ((args: {
|
|
@@ -58,4 +58,4 @@ export declare function assertStruct<Type, Schema>(value: unknown, struct: Struc
|
|
|
58
58
|
* @param _object - The object on which the switch is being operated.
|
|
59
59
|
*/
|
|
60
60
|
export declare function assertExhaustive(_object: never): never;
|
|
61
|
-
//# sourceMappingURL=assert.d.
|
|
61
|
+
//# sourceMappingURL=assert.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.cts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAKpD,MAAM,MAAM,yBAAyB,GACjC,CAAC,KAAK,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,GAC1C,CAAC,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,CAAC;AAkD3C;;GAEG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC,QAAQ,CAAC,IAAI,mBAAmB;gBAEpB,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;CAGzC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,GAAG,EACV,OAAO,GAAE,MAAM,GAAG,KAA2B,EAE7C,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,CAQf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EACvC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,EAC5B,WAAW,SAAqB,EAEhC,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,IAAI,IAAI,CASvB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,KAAK,GAAG,KAAK,CAItD"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Struct } from "@metamask/superstruct";
|
|
2
|
+
export type AssertionErrorConstructor = (new (args: {
|
|
3
|
+
message: string;
|
|
4
|
+
}) => Error) | ((args: {
|
|
5
|
+
message: string;
|
|
6
|
+
}) => Error);
|
|
7
|
+
/**
|
|
8
|
+
* The default error class that is thrown if an assertion fails.
|
|
9
|
+
*/
|
|
10
|
+
export declare class AssertionError extends Error {
|
|
11
|
+
readonly code = "ERR_ASSERTION";
|
|
12
|
+
constructor(options: {
|
|
13
|
+
message: string;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Same as Node.js assert.
|
|
18
|
+
* If the value is falsy, throws an error, does nothing otherwise.
|
|
19
|
+
*
|
|
20
|
+
* @throws {@link AssertionError} If value is falsy.
|
|
21
|
+
* @param value - The test that should be truthy to pass.
|
|
22
|
+
* @param message - Message to be passed to {@link AssertionError} or an
|
|
23
|
+
* {@link Error} instance to throw.
|
|
24
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
25
|
+
* Defaults to {@link AssertionError}. If a custom error class is provided for
|
|
26
|
+
* the `message` argument, this argument is ignored.
|
|
27
|
+
*/
|
|
28
|
+
export declare function assert(value: any, message?: string | Error, ErrorWrapper?: AssertionErrorConstructor): asserts value;
|
|
29
|
+
/**
|
|
30
|
+
* Assert a value against a Superstruct struct.
|
|
31
|
+
*
|
|
32
|
+
* @param value - The value to validate.
|
|
33
|
+
* @param struct - The struct to validate against.
|
|
34
|
+
* @param errorPrefix - A prefix to add to the error message. Defaults to
|
|
35
|
+
* "Assertion failed".
|
|
36
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
37
|
+
* Defaults to {@link AssertionError}.
|
|
38
|
+
* @throws If the value is not valid.
|
|
39
|
+
*/
|
|
40
|
+
export declare function assertStruct<Type, Schema>(value: unknown, struct: Struct<Type, Schema>, errorPrefix?: string, ErrorWrapper?: AssertionErrorConstructor): asserts value is Type;
|
|
41
|
+
/**
|
|
42
|
+
* Use in the default case of a switch that you want to be fully exhaustive.
|
|
43
|
+
* Using this function forces the compiler to enforce exhaustivity during
|
|
44
|
+
* compile-time.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```
|
|
48
|
+
* const number = 1;
|
|
49
|
+
* switch (number) {
|
|
50
|
+
* case 0:
|
|
51
|
+
* ...
|
|
52
|
+
* case 1:
|
|
53
|
+
* ...
|
|
54
|
+
* default:
|
|
55
|
+
* assertExhaustive(snapPrefix);
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
* @param _object - The object on which the switch is being operated.
|
|
59
|
+
*/
|
|
60
|
+
export declare function assertExhaustive(_object: never): never;
|
|
61
|
+
//# sourceMappingURL=assert.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.mts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAKpD,MAAM,MAAM,yBAAyB,GACjC,CAAC,KAAK,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,GAC1C,CAAC,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,CAAC;AAkD3C;;GAEG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC,QAAQ,CAAC,IAAI,mBAAmB;gBAEpB,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;CAGzC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,GAAG,EACV,OAAO,GAAE,MAAM,GAAG,KAA2B,EAE7C,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,CAQf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EACvC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,EAC5B,WAAW,SAAqB,EAEhC,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,IAAI,IAAI,CASvB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,KAAK,GAAG,KAAK,CAItD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { assert as assertSuperstruct } from
|
|
2
|
-
import { getErrorMessage } from
|
|
1
|
+
import { assert as assertSuperstruct } from "@metamask/superstruct";
|
|
2
|
+
import { getErrorMessage } from "./errors.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Check if a value is a constructor, i.e., a function that can be called with
|
|
5
5
|
* the `new` keyword.
|
|
@@ -112,4 +112,4 @@ export function assertStruct(value, struct, errorPrefix = 'Assertion failed', Er
|
|
|
112
112
|
export function assertExhaustive(_object) {
|
|
113
113
|
throw new Error('Invalid branch reached. Should be detected during compilation.');
|
|
114
114
|
}
|
|
115
|
-
//# sourceMappingURL=assert.
|
|
115
|
+
//# sourceMappingURL=assert.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.mjs","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,IAAI,iBAAiB,EAAE,8BAA8B;AAEpE,OAAO,EAAE,eAAe,EAAE,qBAAiB;AAM3C;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,EAA6B;IAE7B,0BAA0B;IAC1B,OAAO,OAAO,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,oCAAoC,CAAC,KAAc;IAC1D,4BAA4B;IAC5B,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AAEH,SAAS,QAAQ,CAAC,YAAuC,EAAE,OAAe;IACxE,IAAI,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAClC,OAAO,IAAI,YAAY,CAAC;YACtB,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IACD,OAAO,YAAY,CAAC;QAClB,OAAO;KACR,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,KAAK;IAGvC,YAAY,OAA4B;QACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAHhB,SAAI,GAAG,eAAe,CAAC;IAIhC,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,MAAM,CACpB,KAAU,EACV,UAA0B,mBAAmB,EAE7C,eAA0C,cAAc;IAExD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,IAAI,OAAO,YAAY,KAAK,EAAE,CAAC;YAC7B,MAAM,OAAO,CAAC;QAChB,CAAC;QAED,MAAM,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAc,EACd,MAA4B,EAC5B,WAAW,GAAG,kBAAkB,EAEhC,eAA0C,cAAc;IAExD,IAAI,CAAC;QACH,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,QAAQ,CACZ,YAAY,EACZ,GAAG,WAAW,KAAK,oCAAoC,CAAC,KAAK,CAAC,GAAG,CAClE,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAc;IAC7C,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;AACJ,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { assert as assertSuperstruct } from '@metamask/superstruct';\n\nimport { getErrorMessage } from './errors';\n\nexport type AssertionErrorConstructor =\n | (new (args: { message: string }) => Error)\n | ((args: { message: string }) => Error);\n\n/**\n * Check if a value is a constructor, i.e., a function that can be called with\n * the `new` keyword.\n *\n * @param fn - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nfunction isConstructable(\n fn: AssertionErrorConstructor,\n): fn is new (args: { message: string }) => Error {\n /* istanbul ignore next */\n return Boolean(typeof fn?.prototype?.constructor?.name === 'string');\n}\n\n/**\n * Attempts to obtain the message from a possible error object. If it is\n * possible to do so, any trailing period will be removed from the message;\n * otherwise an empty string is returned.\n *\n * @param error - The error object to get the message from.\n * @returns The message without any trailing period if `error` is an object\n * with a `message` property; the string version of `error` without any trailing\n * period if it is not `undefined` or `null`; otherwise an empty string.\n */\nfunction getErrorMessageWithoutTrailingPeriod(error: unknown): string {\n // We'll add our own period.\n return getErrorMessage(error).replace(/\\.$/u, '');\n}\n\n/**\n * Initialise an {@link AssertionErrorConstructor} error.\n *\n * @param ErrorWrapper - The error class to use.\n * @param message - The error message.\n * @returns The error object.\n */\n\nfunction getError(ErrorWrapper: AssertionErrorConstructor, message: string) {\n if (isConstructable(ErrorWrapper)) {\n return new ErrorWrapper({\n message,\n });\n }\n return ErrorWrapper({\n message,\n });\n}\n\n/**\n * The default error class that is thrown if an assertion fails.\n */\nexport class AssertionError extends Error {\n readonly code = 'ERR_ASSERTION';\n\n constructor(options: { message: string }) {\n super(options.message);\n }\n}\n\n/**\n * Same as Node.js assert.\n * If the value is falsy, throws an error, does nothing otherwise.\n *\n * @throws {@link AssertionError} If value is falsy.\n * @param value - The test that should be truthy to pass.\n * @param message - Message to be passed to {@link AssertionError} or an\n * {@link Error} instance to throw.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}. If a custom error class is provided for\n * the `message` argument, this argument is ignored.\n */\nexport function assert(\n value: any,\n message: string | Error = 'Assertion failed.',\n\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value {\n if (!value) {\n if (message instanceof Error) {\n throw message;\n }\n\n throw getError(ErrorWrapper, message);\n }\n}\n\n/**\n * Assert a value against a Superstruct struct.\n *\n * @param value - The value to validate.\n * @param struct - The struct to validate against.\n * @param errorPrefix - A prefix to add to the error message. Defaults to\n * \"Assertion failed\".\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the value is not valid.\n */\nexport function assertStruct<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n errorPrefix = 'Assertion failed',\n\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value is Type {\n try {\n assertSuperstruct(value, struct);\n } catch (error) {\n throw getError(\n ErrorWrapper,\n `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`,\n );\n }\n}\n\n/**\n * Use in the default case of a switch that you want to be fully exhaustive.\n * Using this function forces the compiler to enforce exhaustivity during\n * compile-time.\n *\n * @example\n * ```\n * const number = 1;\n * switch (number) {\n * case 0:\n * ...\n * case 1:\n * ...\n * default:\n * assertExhaustive(snapPrefix);\n * }\n * ```\n * @param _object - The object on which the switch is being operated.\n */\nexport function assertExhaustive(_object: never): never {\n throw new Error(\n 'Invalid branch reached. Should be detected during compilation.',\n );\n}\n"]}
|
package/dist/base64.cjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.base64 = void 0;
|
|
4
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
5
|
+
const assert_1 = require("./assert.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Ensure that a provided string-based struct is valid base64.
|
|
8
|
+
*
|
|
9
|
+
* @param struct - The string based struct.
|
|
10
|
+
* @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
|
|
11
|
+
* @returns A superstruct validating base64.
|
|
12
|
+
*/
|
|
13
|
+
const base64 = (struct, options = {}) => {
|
|
14
|
+
const paddingRequired = options.paddingRequired ?? false;
|
|
15
|
+
const characterSet = options.characterSet ?? 'base64';
|
|
16
|
+
let letters;
|
|
17
|
+
if (characterSet === 'base64') {
|
|
18
|
+
letters = String.raw `[A-Za-z0-9+\/]`;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
(0, assert_1.assert)(characterSet === 'base64url');
|
|
22
|
+
letters = String.raw `[-_A-Za-z0-9]`;
|
|
23
|
+
}
|
|
24
|
+
let re;
|
|
25
|
+
if (paddingRequired) {
|
|
26
|
+
re = new RegExp(`^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`, 'u');
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
re = new RegExp(`^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`, 'u');
|
|
30
|
+
}
|
|
31
|
+
return (0, superstruct_1.pattern)(struct, re);
|
|
32
|
+
};
|
|
33
|
+
exports.base64 = base64;
|
|
34
|
+
//# sourceMappingURL=base64.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.cjs","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":";;;AACA,uDAAgD;AAEhD,yCAAkC;AAmBlC;;;;;;GAMG;AACI,MAAM,MAAM,GAAG,CACpB,MAA4B,EAC5B,UAAyB,EAAE,EAC3B,EAAE;IACF,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,KAAK,CAAC;IACzD,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,QAAQ,CAAC;IAEtD,IAAI,OAAe,CAAC;IACpB,IAAI,YAAY,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,gBAAgB,CAAC;IACvC,CAAC;SAAM,CAAC;QACN,IAAA,eAAM,EAAC,YAAY,KAAK,WAAW,CAAC,CAAC;QACrC,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,eAAe,CAAC;IACtC,CAAC;IAED,IAAI,EAAU,CAAC;IACf,IAAI,eAAe,EAAE,CAAC;QACpB,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,QAAQ,OAAO,UAAU,EACzD,GAAG,CACJ,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,SAAS,OAAO,QAAQ,OAAO,UAAU,EACzE,GAAG,CACJ,CAAC;IACJ,CAAC;IAED,OAAO,IAAA,qBAAO,EAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAC7B,CAAC,CAAC;AA7BW,QAAA,MAAM,UA6BjB","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { pattern } from '@metamask/superstruct';\n\nimport { assert } from './assert';\n\nexport type Base64Options = {\n /**\n * Is the `=` padding at the end required or not.\n *\n * @default false\n */\n // Padding is optional in RFC 4648, that's why the default value is false\n paddingRequired?: boolean;\n /**\n * Which character set should be used.\n * The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.\n *\n * @default 'base64'\n */\n characterSet?: 'base64' | 'base64url';\n};\n\n/**\n * Ensure that a provided string-based struct is valid base64.\n *\n * @param struct - The string based struct.\n * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.\n * @returns A superstruct validating base64.\n */\nexport const base64 = <Type extends string, Schema>(\n struct: Struct<Type, Schema>,\n options: Base64Options = {},\n) => {\n const paddingRequired = options.paddingRequired ?? false;\n const characterSet = options.characterSet ?? 'base64';\n\n let letters: string;\n if (characterSet === 'base64') {\n letters = String.raw`[A-Za-z0-9+\\/]`;\n } else {\n assert(characterSet === 'base64url');\n letters = String.raw`[-_A-Za-z0-9]`;\n }\n\n let re: RegExp;\n if (paddingRequired) {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n } else {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n }\n\n return pattern(struct, re);\n};\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Struct } from
|
|
1
|
+
import type { Struct } from "@metamask/superstruct";
|
|
2
2
|
export type Base64Options = {
|
|
3
3
|
/**
|
|
4
4
|
* Is the `=` padding at the end required or not.
|
|
@@ -22,4 +22,4 @@ export type Base64Options = {
|
|
|
22
22
|
* @returns A superstruct validating base64.
|
|
23
23
|
*/
|
|
24
24
|
export declare const base64: <Type extends string, Schema>(struct: Struct<Type, Schema>, options?: Base64Options) => Struct<Type, Schema>;
|
|
25
|
-
//# sourceMappingURL=base64.d.
|
|
25
|
+
//# sourceMappingURL=base64.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.d.cts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAKpD,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;OAIG;IAEH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAC;CACvC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,MAAM,wEAER,aAAa,yBA2BvB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Struct } from "@metamask/superstruct";
|
|
2
|
+
export type Base64Options = {
|
|
3
|
+
/**
|
|
4
|
+
* Is the `=` padding at the end required or not.
|
|
5
|
+
*
|
|
6
|
+
* @default false
|
|
7
|
+
*/
|
|
8
|
+
paddingRequired?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Which character set should be used.
|
|
11
|
+
* The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.
|
|
12
|
+
*
|
|
13
|
+
* @default 'base64'
|
|
14
|
+
*/
|
|
15
|
+
characterSet?: 'base64' | 'base64url';
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Ensure that a provided string-based struct is valid base64.
|
|
19
|
+
*
|
|
20
|
+
* @param struct - The string based struct.
|
|
21
|
+
* @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
|
|
22
|
+
* @returns A superstruct validating base64.
|
|
23
|
+
*/
|
|
24
|
+
export declare const base64: <Type extends string, Schema>(struct: Struct<Type, Schema>, options?: Base64Options) => Struct<Type, Schema>;
|
|
25
|
+
//# sourceMappingURL=base64.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.d.mts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAKpD,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;OAIG;IAEH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAC;CACvC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,MAAM,wEAER,aAAa,yBA2BvB,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { pattern } from
|
|
2
|
-
import { assert } from
|
|
1
|
+
import { pattern } from "@metamask/superstruct";
|
|
2
|
+
import { assert } from "./assert.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Ensure that a provided string-based struct is valid base64.
|
|
5
5
|
*
|
|
@@ -27,4 +27,4 @@ export const base64 = (struct, options = {}) => {
|
|
|
27
27
|
}
|
|
28
28
|
return pattern(struct, re);
|
|
29
29
|
};
|
|
30
|
-
//# sourceMappingURL=base64.
|
|
30
|
+
//# sourceMappingURL=base64.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.mjs","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,8BAA8B;AAEhD,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAmBlC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,MAA4B,EAC5B,UAAyB,EAAE,EAC3B,EAAE;IACF,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,KAAK,CAAC;IACzD,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,QAAQ,CAAC;IAEtD,IAAI,OAAe,CAAC;IACpB,IAAI,YAAY,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,gBAAgB,CAAC;IACvC,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,YAAY,KAAK,WAAW,CAAC,CAAC;QACrC,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,eAAe,CAAC;IACtC,CAAC;IAED,IAAI,EAAU,CAAC;IACf,IAAI,eAAe,EAAE,CAAC;QACpB,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,QAAQ,OAAO,UAAU,EACzD,GAAG,CACJ,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,SAAS,OAAO,QAAQ,OAAO,UAAU,EACzE,GAAG,CACJ,CAAC;IACJ,CAAC;IAED,OAAO,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAC7B,CAAC,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { pattern } from '@metamask/superstruct';\n\nimport { assert } from './assert';\n\nexport type Base64Options = {\n /**\n * Is the `=` padding at the end required or not.\n *\n * @default false\n */\n // Padding is optional in RFC 4648, that's why the default value is false\n paddingRequired?: boolean;\n /**\n * Which character set should be used.\n * The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.\n *\n * @default 'base64'\n */\n characterSet?: 'base64' | 'base64url';\n};\n\n/**\n * Ensure that a provided string-based struct is valid base64.\n *\n * @param struct - The string based struct.\n * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.\n * @returns A superstruct validating base64.\n */\nexport const base64 = <Type extends string, Schema>(\n struct: Struct<Type, Schema>,\n options: Base64Options = {},\n) => {\n const paddingRequired = options.paddingRequired ?? false;\n const characterSet = options.characterSet ?? 'base64';\n\n let letters: string;\n if (characterSet === 'base64') {\n letters = String.raw`[A-Za-z0-9+\\/]`;\n } else {\n assert(characterSet === 'base64url');\n letters = String.raw`[-_A-Za-z0-9]`;\n }\n\n let re: RegExp;\n if (paddingRequired) {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n } else {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n }\n\n return pattern(struct, re);\n};\n"]}
|