@metamask/utils 8.0.0 → 8.2.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 +25 -1
- package/dist/assert.js +16 -0
- package/dist/assert.js.map +1 -0
- package/dist/assert.mjs +16 -0
- package/dist/assert.mjs.map +1 -0
- package/dist/base64.js +11 -0
- package/dist/base64.js.map +1 -0
- package/dist/base64.mjs +11 -0
- package/dist/base64.mjs.map +1 -0
- package/dist/bytes.js +43 -0
- package/dist/bytes.js.map +1 -0
- package/dist/bytes.mjs +43 -0
- package/dist/bytes.mjs.map +1 -0
- package/dist/caip-types.js +40 -0
- package/dist/caip-types.js.map +1 -0
- package/dist/caip-types.mjs +40 -0
- package/dist/caip-types.mjs.map +1 -0
- package/dist/checksum.js +12 -0
- package/dist/checksum.js.map +1 -0
- package/dist/checksum.mjs +12 -0
- package/dist/checksum.mjs.map +1 -0
- package/dist/chunk-2CY4F77D.mjs +95 -0
- package/dist/chunk-2CY4F77D.mjs.map +1 -0
- package/dist/chunk-2LBGT4GH.js +15 -0
- package/dist/chunk-2LBGT4GH.js.map +1 -0
- package/dist/chunk-2TBCL6EF.js +1 -0
- package/dist/chunk-2TBCL6EF.js.map +1 -0
- package/dist/chunk-3W5G4CYI.js +25 -0
- package/dist/chunk-3W5G4CYI.js.map +1 -0
- package/dist/chunk-4JNG5UTO.js +44 -0
- package/dist/chunk-4JNG5UTO.js.map +1 -0
- package/dist/chunk-4RMX5YWE.js +34 -0
- package/dist/chunk-4RMX5YWE.js.map +1 -0
- package/dist/chunk-5WQRFJVQ.js +257 -0
- package/dist/chunk-5WQRFJVQ.js.map +1 -0
- package/dist/chunk-65KBH4FZ.mjs +257 -0
- package/dist/chunk-65KBH4FZ.mjs.map +1 -0
- package/dist/chunk-7S5AWFX7.mjs +61 -0
- package/dist/chunk-7S5AWFX7.mjs.map +1 -0
- package/dist/chunk-AY6FDCBT.mjs +1 -0
- package/dist/chunk-AY6FDCBT.mjs.map +1 -0
- package/dist/chunk-BKR45GQY.mjs +273 -0
- package/dist/chunk-BKR45GQY.mjs.map +1 -0
- package/dist/chunk-CSDMTDND.mjs +59 -0
- package/dist/chunk-CSDMTDND.mjs.map +1 -0
- package/dist/chunk-DMPG3H3G.js +61 -0
- package/dist/chunk-DMPG3H3G.js.map +1 -0
- package/dist/chunk-DYMCEQIV.mjs +1 -0
- package/dist/chunk-DYMCEQIV.mjs.map +1 -0
- package/dist/chunk-EQMZL4XU.js +1 -0
- package/dist/chunk-EQMZL4XU.js.map +1 -0
- package/dist/chunk-G66TREC6.mjs +69 -0
- package/dist/chunk-G66TREC6.mjs.map +1 -0
- package/dist/chunk-H4YFDLB7.mjs +70 -0
- package/dist/chunk-H4YFDLB7.mjs.map +1 -0
- package/dist/chunk-HLDWS2JR.mjs +35 -0
- package/dist/chunk-HLDWS2JR.mjs.map +1 -0
- package/dist/chunk-I575FZFH.mjs +1 -0
- package/dist/chunk-I575FZFH.mjs.map +1 -0
- package/dist/chunk-JCYFTGED.js +95 -0
- package/dist/chunk-JCYFTGED.js.map +1 -0
- package/dist/chunk-JPAL7Q5S.mjs +90 -0
- package/dist/chunk-JPAL7Q5S.mjs.map +1 -0
- package/dist/chunk-LC2CRSWD.js +1 -0
- package/dist/chunk-LC2CRSWD.js.map +1 -0
- package/dist/chunk-N53OILN6.mjs +16 -0
- package/dist/chunk-N53OILN6.mjs.map +1 -0
- package/dist/chunk-QVEKZRZ2.js +70 -0
- package/dist/chunk-QVEKZRZ2.js.map +1 -0
- package/dist/chunk-RIRDOQPX.mjs +15 -0
- package/dist/chunk-RIRDOQPX.mjs.map +1 -0
- package/dist/chunk-RKRGAFXY.js +1 -0
- package/dist/chunk-RKRGAFXY.js.map +1 -0
- package/dist/chunk-RRYOWRCV.mjs +1 -0
- package/dist/chunk-RRYOWRCV.mjs.map +1 -0
- package/dist/chunk-TGOMTREC.mjs +75 -0
- package/dist/chunk-TGOMTREC.mjs.map +1 -0
- package/dist/chunk-THNLGEXV.mjs +34 -0
- package/dist/chunk-THNLGEXV.mjs.map +1 -0
- package/dist/chunk-U7ZUGCE7.js +75 -0
- package/dist/chunk-U7ZUGCE7.js.map +1 -0
- package/dist/chunk-UOTVU7OQ.js +1 -0
- package/dist/chunk-UOTVU7OQ.js.map +1 -0
- package/dist/chunk-VBYRLAP6.js +59 -0
- package/dist/chunk-VBYRLAP6.js.map +1 -0
- package/dist/chunk-VL5DYXC5.mjs +122 -0
- package/dist/chunk-VL5DYXC5.mjs.map +1 -0
- package/dist/chunk-VSEW3V2L.mjs +44 -0
- package/dist/chunk-VSEW3V2L.mjs.map +1 -0
- package/dist/chunk-WA4DHIND.mjs +1 -0
- package/dist/chunk-WA4DHIND.mjs.map +1 -0
- package/dist/chunk-WQDTPIQK.js +122 -0
- package/dist/chunk-WQDTPIQK.js.map +1 -0
- package/dist/chunk-WRBN6DWM.js +35 -0
- package/dist/chunk-WRBN6DWM.js.map +1 -0
- package/dist/chunk-X66SUIEF.mjs +25 -0
- package/dist/chunk-X66SUIEF.mjs.map +1 -0
- package/dist/chunk-XYYGZF5Y.js +16 -0
- package/dist/chunk-XYYGZF5Y.js.map +1 -0
- package/dist/chunk-YQJBIHC7.js +273 -0
- package/dist/chunk-YQJBIHC7.js.map +1 -0
- package/dist/chunk-Z2RGWDD7.js +90 -0
- package/dist/chunk-Z2RGWDD7.js.map +1 -0
- package/dist/chunk-ZRNIY7OW.js +69 -0
- package/dist/chunk-ZRNIY7OW.js.map +1 -0
- package/dist/coercers.js +18 -0
- package/dist/coercers.js.map +1 -0
- package/dist/coercers.mjs +18 -0
- package/dist/coercers.mjs.map +1 -0
- package/dist/collections.js +10 -0
- package/dist/collections.js.map +1 -0
- package/dist/collections.mjs +10 -0
- package/dist/collections.mjs.map +1 -0
- package/dist/encryption-types.js +2 -0
- package/dist/encryption-types.js.map +1 -0
- package/dist/encryption-types.mjs +2 -0
- package/dist/encryption-types.mjs.map +1 -0
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -0
- package/dist/errors.mjs +17 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/fs.js +26 -0
- package/dist/fs.js.map +1 -0
- package/dist/fs.mjs +26 -0
- package/dist/fs.mjs.map +1 -0
- package/dist/hex.js +35 -0
- package/dist/hex.js.map +1 -0
- package/dist/hex.mjs +35 -0
- package/dist/hex.mjs.map +1 -0
- package/dist/index.js +289 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +289 -0
- package/dist/index.mjs.map +1 -0
- package/dist/json.js +75 -0
- package/dist/json.js.map +1 -0
- package/dist/json.mjs +75 -0
- package/dist/json.mjs.map +1 -0
- package/dist/keyring.js +2 -0
- package/dist/keyring.js.map +1 -0
- package/dist/keyring.mjs +2 -0
- package/dist/keyring.mjs.map +1 -0
- package/dist/logging.js +10 -0
- package/dist/logging.js.map +1 -0
- package/dist/logging.mjs +10 -0
- package/dist/logging.mjs.map +1 -0
- package/dist/misc.js +28 -0
- package/dist/misc.js.map +1 -0
- package/dist/misc.mjs +28 -0
- package/dist/misc.mjs.map +1 -0
- package/dist/node.js +309 -0
- package/dist/node.js.map +1 -0
- package/dist/node.mjs +309 -0
- package/dist/node.mjs.map +1 -0
- package/dist/number.js +18 -0
- package/dist/number.js.map +1 -0
- package/dist/number.mjs +18 -0
- package/dist/number.mjs.map +1 -0
- package/dist/opaque.js +2 -0
- package/dist/opaque.js.map +1 -0
- package/dist/opaque.mjs +2 -0
- package/dist/opaque.mjs.map +1 -0
- package/dist/time.js +12 -0
- package/dist/time.js.map +1 -0
- package/dist/time.mjs +12 -0
- package/dist/time.mjs.map +1 -0
- package/dist/transaction-types.js +2 -0
- package/dist/transaction-types.js.map +1 -0
- package/dist/transaction-types.mjs +2 -0
- package/dist/transaction-types.mjs.map +1 -0
- package/dist/types/assert.d.ts.map +1 -1
- package/dist/types/bytes.d.ts +14 -0
- package/dist/types/bytes.d.ts.map +1 -1
- package/dist/types/errors.d.ts +58 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/fs.d.ts +133 -0
- package/dist/types/fs.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/json.d.ts +83 -47
- package/dist/types/json.d.ts.map +1 -1
- package/dist/types/node.d.ts +3 -0
- package/dist/types/node.d.ts.map +1 -0
- package/dist/versions.js +27 -0
- package/dist/versions.js.map +1 -0
- package/dist/versions.mjs +27 -0
- package/dist/versions.mjs.map +1 -0
- package/package.json +21 -20
- package/dist/cjs/assert.js +0 -116
- package/dist/cjs/assert.js.map +0 -1
- package/dist/cjs/base64.js +0 -32
- package/dist/cjs/base64.js.map +0 -1
- package/dist/cjs/bytes.js +0 -255
- package/dist/cjs/bytes.js.map +0 -1
- package/dist/cjs/caip-types.js +0 -115
- package/dist/cjs/caip-types.js.map +0 -1
- package/dist/cjs/checksum.js +0 -17
- package/dist/cjs/checksum.js.map +0 -1
- package/dist/cjs/coercers.js +0 -96
- package/dist/cjs/coercers.js.map +0 -1
- package/dist/cjs/collections.js +0 -157
- package/dist/cjs/collections.js.map +0 -1
- package/dist/cjs/encryption-types.js +0 -6
- package/dist/cjs/encryption-types.js.map +0 -1
- package/dist/cjs/hex.js +0 -107
- package/dist/cjs/hex.js.map +0 -1
- package/dist/cjs/index.js +0 -37
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/json.js +0 -263
- package/dist/cjs/json.js.map +0 -1
- package/dist/cjs/keyring.js +0 -6
- package/dist/cjs/keyring.js.map +0 -1
- package/dist/cjs/logging.js +0 -33
- package/dist/cjs/logging.js.map +0 -1
- package/dist/cjs/misc.js +0 -111
- package/dist/cjs/misc.js.map +0 -1
- package/dist/cjs/number.js +0 -52
- package/dist/cjs/number.js.map +0 -1
- package/dist/cjs/opaque.js +0 -8
- package/dist/cjs/opaque.js.map +0 -1
- package/dist/cjs/time.js +0 -63
- package/dist/cjs/time.js.map +0 -1
- package/dist/cjs/transaction-types.js +0 -6
- package/dist/cjs/transaction-types.js.map +0 -1
- package/dist/cjs/versions.js +0 -79
- package/dist/cjs/versions.js.map +0 -1
- package/dist/esm/assert.js +0 -133
- package/dist/esm/assert.js.map +0 -1
- package/dist/esm/base64.js +0 -28
- package/dist/esm/base64.js.map +0 -1
- package/dist/esm/bytes.js +0 -334
- package/dist/esm/bytes.js.map +0 -1
- package/dist/esm/caip-types.js +0 -99
- package/dist/esm/caip-types.js.map +0 -1
- package/dist/esm/checksum.js +0 -7
- package/dist/esm/checksum.js.map +0 -1
- package/dist/esm/coercers.js +0 -153
- package/dist/esm/coercers.js.map +0 -1
- package/dist/esm/collections.js +0 -140
- package/dist/esm/collections.js.map +0 -1
- package/dist/esm/encryption-types.js +0 -3
- package/dist/esm/encryption-types.js.map +0 -1
- package/dist/esm/hex.js +0 -108
- package/dist/esm/hex.js.map +0 -1
- package/dist/esm/index.js +0 -20
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/json.js +0 -301
- package/dist/esm/json.js.map +0 -1
- package/dist/esm/keyring.js +0 -3
- package/dist/esm/keyring.js.map +0 -1
- package/dist/esm/logging.js +0 -34
- package/dist/esm/logging.js.map +0 -1
- package/dist/esm/misc.js +0 -129
- package/dist/esm/misc.js.map +0 -1
- package/dist/esm/number.js +0 -92
- package/dist/esm/number.js.map +0 -1
- package/dist/esm/opaque.js +0 -5
- package/dist/esm/opaque.js.map +0 -1
- package/dist/esm/package.json +0 -1
- package/dist/esm/time.js +0 -53
- package/dist/esm/time.js.map +0 -1
- package/dist/esm/transaction-types.js +0 -3
- package/dist/esm/transaction-types.js.map +0 -1
- package/dist/esm/versions.js +0 -78
- package/dist/esm/versions.js.map +0 -1
- package/dist/types/caip-types.test-d.d.ts +0 -2
- package/dist/types/caip-types.test-d.d.ts.map +0 -1
- package/dist/types/hex.test-d.d.ts +0 -2
- package/dist/types/hex.test-d.d.ts.map +0 -1
- package/dist/types/json.test-d.d.ts +0 -2
- package/dist/types/json.test-d.d.ts.map +0 -1
- package/dist/types/misc.test-d.d.ts +0 -2
- package/dist/types/misc.test-d.d.ts.map +0 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isNullOrUndefined,
|
|
3
|
+
isObject
|
|
4
|
+
} from "./chunk-H4YFDLB7.mjs";
|
|
5
|
+
|
|
6
|
+
// src/errors.ts
|
|
7
|
+
import { ErrorWithCause } from "pony-cause";
|
|
8
|
+
function isError(error) {
|
|
9
|
+
return error instanceof Error || isObject(error) && error.constructor.name === "Error";
|
|
10
|
+
}
|
|
11
|
+
function isErrorWithCode(error) {
|
|
12
|
+
return typeof error === "object" && error !== null && "code" in error;
|
|
13
|
+
}
|
|
14
|
+
function isErrorWithMessage(error) {
|
|
15
|
+
return typeof error === "object" && error !== null && "message" in error;
|
|
16
|
+
}
|
|
17
|
+
function isErrorWithStack(error) {
|
|
18
|
+
return typeof error === "object" && error !== null && "stack" in error;
|
|
19
|
+
}
|
|
20
|
+
function getErrorMessage(error) {
|
|
21
|
+
if (isErrorWithMessage(error) && typeof error.message === "string") {
|
|
22
|
+
return error.message;
|
|
23
|
+
}
|
|
24
|
+
if (isNullOrUndefined(error)) {
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
return String(error);
|
|
28
|
+
}
|
|
29
|
+
function wrapError(originalError, message) {
|
|
30
|
+
if (isError(originalError)) {
|
|
31
|
+
const error = Error.length === 2 ? (
|
|
32
|
+
// This branch is getting tested by using the Node version that
|
|
33
|
+
// supports `cause` on the Error constructor.
|
|
34
|
+
// istanbul ignore next
|
|
35
|
+
// Also, for some reason `tsserver` is not complaining that the
|
|
36
|
+
// Error constructor doesn't support a second argument in the editor,
|
|
37
|
+
// but `tsc` does. I'm not sure why, but we disable this in the
|
|
38
|
+
// meantime.
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
new Error(message, { cause: originalError })
|
|
42
|
+
) : new ErrorWithCause(message, { cause: originalError });
|
|
43
|
+
if (isErrorWithCode(originalError)) {
|
|
44
|
+
error.code = originalError.code;
|
|
45
|
+
}
|
|
46
|
+
return error;
|
|
47
|
+
}
|
|
48
|
+
if (message.length > 0) {
|
|
49
|
+
return new Error(`${String(originalError)}: ${message}`);
|
|
50
|
+
}
|
|
51
|
+
return new Error(String(originalError));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export {
|
|
55
|
+
isErrorWithCode,
|
|
56
|
+
isErrorWithMessage,
|
|
57
|
+
isErrorWithStack,
|
|
58
|
+
getErrorMessage,
|
|
59
|
+
wrapError
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=chunk-7S5AWFX7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts"],"sourcesContent":["import { ErrorWithCause } from 'pony-cause';\n\nimport { isNullOrUndefined, isObject } from './misc';\n\n/**\n * Type guard for determining whether the given value is an instance of Error.\n * For errors generated via `fs.promises`, `error instanceof Error` won't work,\n * so we have to come up with another way of testing.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nfunction isError(error: unknown): error is Error {\n return (\n error instanceof Error ||\n (isObject(error) && error.constructor.name === 'Error')\n );\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `code` property such as the type of error that Node throws for filesystem\n * operations, etc.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithCode(error: unknown): error is { code: string } {\n return typeof error === 'object' && error !== null && 'code' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `message` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithMessage(\n error: unknown,\n): error is { message: string } {\n return typeof error === 'object' && error !== null && 'message' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `stack` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithStack(error: unknown): error is { stack: string } {\n return typeof error === 'object' && error !== null && 'stack' in error;\n}\n\n/**\n * Attempts to obtain the message from a possible error object, defaulting to an\n * empty string if it is impossible to do so.\n *\n * @param error - The possible error to get the message from.\n * @returns The message if `error` is an object with a `message` property;\n * the string version of `error` if it is not `undefined` or `null`; otherwise\n * an empty string.\n */\nexport function getErrorMessage(error: unknown): string {\n if (isErrorWithMessage(error) && typeof error.message === 'string') {\n return error.message;\n }\n\n if (isNullOrUndefined(error)) {\n return '';\n }\n\n return String(error);\n}\n\n/**\n * Builds a new error object, linking it to the original error via the `cause`\n * property if it is an Error.\n *\n * This function is useful to reframe error messages in general, but is\n * _critical_ when interacting with any of Node's filesystem functions as\n * provided via `fs.promises`, because these do not produce stack traces in the\n * case of an I/O error (see <https://github.com/nodejs/node/issues/30944>).\n *\n * @param originalError - The error to be wrapped (something throwable).\n * @param message - The desired message of the new error.\n * @returns A new error object.\n */\nexport function wrapError<Throwable>(\n originalError: Throwable,\n message: string,\n): Error & { code?: string } {\n if (isError(originalError)) {\n const error: Error & { code?: string } =\n Error.length === 2\n ? // This branch is getting tested by using the Node version that\n // supports `cause` on the Error constructor.\n // istanbul ignore next\n // Also, for some reason `tsserver` is not complaining that the\n // Error constructor doesn't support a second argument in the editor,\n // but `tsc` does. I'm not sure why, but we disable this in the\n // meantime.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n new Error(message, { cause: originalError })\n : new ErrorWithCause(message, { cause: originalError });\n\n if (isErrorWithCode(originalError)) {\n error.code = originalError.code;\n }\n\n return error;\n }\n\n if (message.length > 0) {\n return new Error(`${String(originalError)}: ${message}`);\n }\n\n return new Error(String(originalError));\n}\n"],"mappings":";;;;;;AAAA,SAAS,sBAAsB;AAY/B,SAAS,QAAQ,OAAgC;AAC/C,SACE,iBAAiB,SAChB,SAAS,KAAK,KAAK,MAAM,YAAY,SAAS;AAEnD;AAUO,SAAS,gBAAgB,OAA2C;AACzE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,UAAU;AAClE;AASO,SAAS,mBACd,OAC8B;AAC9B,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,aAAa;AACrE;AASO,SAAS,iBAAiB,OAA4C;AAC3E,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,WAAW;AACnE;AAWO,SAAS,gBAAgB,OAAwB;AACtD,MAAI,mBAAmB,KAAK,KAAK,OAAO,MAAM,YAAY,UAAU;AAClE,WAAO,MAAM;AAAA,EACf;AAEA,MAAI,kBAAkB,KAAK,GAAG;AAC5B,WAAO;AAAA,EACT;AAEA,SAAO,OAAO,KAAK;AACrB;AAeO,SAAS,UACd,eACA,SAC2B;AAC3B,MAAI,QAAQ,aAAa,GAAG;AAC1B,UAAM,QACJ,MAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAUb,IAAI,MAAM,SAAS,EAAE,OAAO,cAAc,CAAC;AAAA,QAC3C,IAAI,eAAe,SAAS,EAAE,OAAO,cAAc,CAAC;AAE1D,QAAI,gBAAgB,aAAa,GAAG;AAClC,YAAM,OAAO,cAAc;AAAA,IAC7B;AAEA,WAAO;AAAA,EACT;AAEA,MAAI,QAAQ,SAAS,GAAG;AACtB,WAAO,IAAI,MAAM,GAAG,OAAO,aAAa,CAAC,KAAK,OAAO,EAAE;AAAA,EACzD;AAEA,SAAO,IAAI,MAAM,OAAO,aAAa,CAAC;AACxC;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-AY6FDCBT.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assert
|
|
3
|
+
} from "./chunk-CSDMTDND.mjs";
|
|
4
|
+
|
|
5
|
+
// src/hex.ts
|
|
6
|
+
import { keccak_256 as keccak256 } from "@noble/hashes/sha3";
|
|
7
|
+
import { is, pattern, string } from "superstruct";
|
|
8
|
+
|
|
9
|
+
// src/bytes.ts
|
|
10
|
+
import { base64 } from "@scure/base";
|
|
11
|
+
var HEX_MINIMUM_NUMBER_CHARACTER = 48;
|
|
12
|
+
var HEX_MAXIMUM_NUMBER_CHARACTER = 58;
|
|
13
|
+
var HEX_CHARACTER_OFFSET = 87;
|
|
14
|
+
function getPrecomputedHexValuesBuilder() {
|
|
15
|
+
const lookupTable = [];
|
|
16
|
+
return () => {
|
|
17
|
+
if (lookupTable.length === 0) {
|
|
18
|
+
for (let i = 0; i < 256; i++) {
|
|
19
|
+
lookupTable.push(i.toString(16).padStart(2, "0"));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return lookupTable;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
var getPrecomputedHexValues = getPrecomputedHexValuesBuilder();
|
|
26
|
+
function isBytes(value) {
|
|
27
|
+
return value instanceof Uint8Array;
|
|
28
|
+
}
|
|
29
|
+
function assertIsBytes(value) {
|
|
30
|
+
assert(isBytes(value), "Value must be a Uint8Array.");
|
|
31
|
+
}
|
|
32
|
+
function bytesToHex(bytes) {
|
|
33
|
+
assertIsBytes(bytes);
|
|
34
|
+
if (bytes.length === 0) {
|
|
35
|
+
return "0x";
|
|
36
|
+
}
|
|
37
|
+
const lookupTable = getPrecomputedHexValues();
|
|
38
|
+
const hexadecimal = new Array(bytes.length);
|
|
39
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
40
|
+
hexadecimal[i] = lookupTable[bytes[i]];
|
|
41
|
+
}
|
|
42
|
+
return add0x(hexadecimal.join(""));
|
|
43
|
+
}
|
|
44
|
+
function bytesToBigInt(bytes) {
|
|
45
|
+
assertIsBytes(bytes);
|
|
46
|
+
const hexadecimal = bytesToHex(bytes);
|
|
47
|
+
return BigInt(hexadecimal);
|
|
48
|
+
}
|
|
49
|
+
function bytesToSignedBigInt(bytes) {
|
|
50
|
+
assertIsBytes(bytes);
|
|
51
|
+
let value = BigInt(0);
|
|
52
|
+
for (const byte of bytes) {
|
|
53
|
+
value = (value << BigInt(8)) + BigInt(byte);
|
|
54
|
+
}
|
|
55
|
+
return BigInt.asIntN(bytes.length * 8, value);
|
|
56
|
+
}
|
|
57
|
+
function bytesToNumber(bytes) {
|
|
58
|
+
assertIsBytes(bytes);
|
|
59
|
+
const bigint = bytesToBigInt(bytes);
|
|
60
|
+
assert(
|
|
61
|
+
bigint <= BigInt(Number.MAX_SAFE_INTEGER),
|
|
62
|
+
"Number is not a safe integer. Use `bytesToBigInt` instead."
|
|
63
|
+
);
|
|
64
|
+
return Number(bigint);
|
|
65
|
+
}
|
|
66
|
+
function bytesToString(bytes) {
|
|
67
|
+
assertIsBytes(bytes);
|
|
68
|
+
return new TextDecoder().decode(bytes);
|
|
69
|
+
}
|
|
70
|
+
function bytesToBase64(bytes) {
|
|
71
|
+
assertIsBytes(bytes);
|
|
72
|
+
return base64.encode(bytes);
|
|
73
|
+
}
|
|
74
|
+
function hexToBytes(value) {
|
|
75
|
+
if (value?.toLowerCase?.() === "0x") {
|
|
76
|
+
return new Uint8Array();
|
|
77
|
+
}
|
|
78
|
+
assertIsHexString(value);
|
|
79
|
+
const strippedValue = remove0x(value).toLowerCase();
|
|
80
|
+
const normalizedValue = strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;
|
|
81
|
+
const bytes = new Uint8Array(normalizedValue.length / 2);
|
|
82
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
83
|
+
const c1 = normalizedValue.charCodeAt(i * 2);
|
|
84
|
+
const c2 = normalizedValue.charCodeAt(i * 2 + 1);
|
|
85
|
+
const n1 = c1 - (c1 < HEX_MAXIMUM_NUMBER_CHARACTER ? HEX_MINIMUM_NUMBER_CHARACTER : HEX_CHARACTER_OFFSET);
|
|
86
|
+
const n2 = c2 - (c2 < HEX_MAXIMUM_NUMBER_CHARACTER ? HEX_MINIMUM_NUMBER_CHARACTER : HEX_CHARACTER_OFFSET);
|
|
87
|
+
bytes[i] = n1 * 16 + n2;
|
|
88
|
+
}
|
|
89
|
+
return bytes;
|
|
90
|
+
}
|
|
91
|
+
function bigIntToBytes(value) {
|
|
92
|
+
assert(typeof value === "bigint", "Value must be a bigint.");
|
|
93
|
+
assert(value >= BigInt(0), "Value must be a non-negative bigint.");
|
|
94
|
+
const hexadecimal = value.toString(16);
|
|
95
|
+
return hexToBytes(hexadecimal);
|
|
96
|
+
}
|
|
97
|
+
function bigIntFits(value, bytes) {
|
|
98
|
+
assert(bytes > 0);
|
|
99
|
+
const mask = value >> BigInt(31);
|
|
100
|
+
return !((~value & mask) + (value & ~mask) >> BigInt(bytes * 8 + ~0));
|
|
101
|
+
}
|
|
102
|
+
function signedBigIntToBytes(value, byteLength) {
|
|
103
|
+
assert(typeof value === "bigint", "Value must be a bigint.");
|
|
104
|
+
assert(typeof byteLength === "number", "Byte length must be a number.");
|
|
105
|
+
assert(byteLength > 0, "Byte length must be greater than 0.");
|
|
106
|
+
assert(
|
|
107
|
+
bigIntFits(value, byteLength),
|
|
108
|
+
"Byte length is too small to represent the given value."
|
|
109
|
+
);
|
|
110
|
+
let numberValue = value;
|
|
111
|
+
const bytes = new Uint8Array(byteLength);
|
|
112
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
113
|
+
bytes[i] = Number(BigInt.asUintN(8, numberValue));
|
|
114
|
+
numberValue >>= BigInt(8);
|
|
115
|
+
}
|
|
116
|
+
return bytes.reverse();
|
|
117
|
+
}
|
|
118
|
+
function numberToBytes(value) {
|
|
119
|
+
assert(typeof value === "number", "Value must be a number.");
|
|
120
|
+
assert(value >= 0, "Value must be a non-negative number.");
|
|
121
|
+
assert(
|
|
122
|
+
Number.isSafeInteger(value),
|
|
123
|
+
"Value is not a safe integer. Use `bigIntToBytes` instead."
|
|
124
|
+
);
|
|
125
|
+
const hexadecimal = value.toString(16);
|
|
126
|
+
return hexToBytes(hexadecimal);
|
|
127
|
+
}
|
|
128
|
+
function stringToBytes(value) {
|
|
129
|
+
assert(typeof value === "string", "Value must be a string.");
|
|
130
|
+
return new TextEncoder().encode(value);
|
|
131
|
+
}
|
|
132
|
+
function base64ToBytes(value) {
|
|
133
|
+
assert(typeof value === "string", "Value must be a string.");
|
|
134
|
+
return base64.decode(value);
|
|
135
|
+
}
|
|
136
|
+
function valueToBytes(value) {
|
|
137
|
+
if (typeof value === "bigint") {
|
|
138
|
+
return bigIntToBytes(value);
|
|
139
|
+
}
|
|
140
|
+
if (typeof value === "number") {
|
|
141
|
+
return numberToBytes(value);
|
|
142
|
+
}
|
|
143
|
+
if (typeof value === "string") {
|
|
144
|
+
if (value.startsWith("0x")) {
|
|
145
|
+
return hexToBytes(value);
|
|
146
|
+
}
|
|
147
|
+
return stringToBytes(value);
|
|
148
|
+
}
|
|
149
|
+
if (isBytes(value)) {
|
|
150
|
+
return value;
|
|
151
|
+
}
|
|
152
|
+
throw new TypeError(`Unsupported value type: "${typeof value}".`);
|
|
153
|
+
}
|
|
154
|
+
function concatBytes(values) {
|
|
155
|
+
const normalizedValues = new Array(values.length);
|
|
156
|
+
let byteLength = 0;
|
|
157
|
+
for (let i = 0; i < values.length; i++) {
|
|
158
|
+
const value = valueToBytes(values[i]);
|
|
159
|
+
normalizedValues[i] = value;
|
|
160
|
+
byteLength += value.length;
|
|
161
|
+
}
|
|
162
|
+
const bytes = new Uint8Array(byteLength);
|
|
163
|
+
for (let i = 0, offset = 0; i < normalizedValues.length; i++) {
|
|
164
|
+
bytes.set(normalizedValues[i], offset);
|
|
165
|
+
offset += normalizedValues[i].length;
|
|
166
|
+
}
|
|
167
|
+
return bytes;
|
|
168
|
+
}
|
|
169
|
+
function createDataView(bytes) {
|
|
170
|
+
if (typeof Buffer !== "undefined" && bytes instanceof Buffer) {
|
|
171
|
+
const buffer = bytes.buffer.slice(
|
|
172
|
+
bytes.byteOffset,
|
|
173
|
+
bytes.byteOffset + bytes.byteLength
|
|
174
|
+
);
|
|
175
|
+
return new DataView(buffer);
|
|
176
|
+
}
|
|
177
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/hex.ts
|
|
181
|
+
var HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);
|
|
182
|
+
var StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu);
|
|
183
|
+
var HexAddressStruct = pattern(
|
|
184
|
+
string(),
|
|
185
|
+
/^0x[0-9a-f]{40}$/u
|
|
186
|
+
);
|
|
187
|
+
var HexChecksumAddressStruct = pattern(
|
|
188
|
+
string(),
|
|
189
|
+
/^0x[0-9a-fA-F]{40}$/u
|
|
190
|
+
);
|
|
191
|
+
function isHexString(value) {
|
|
192
|
+
return is(value, HexStruct);
|
|
193
|
+
}
|
|
194
|
+
function isStrictHexString(value) {
|
|
195
|
+
return is(value, StrictHexStruct);
|
|
196
|
+
}
|
|
197
|
+
function assertIsHexString(value) {
|
|
198
|
+
assert(isHexString(value), "Value must be a hexadecimal string.");
|
|
199
|
+
}
|
|
200
|
+
function assertIsStrictHexString(value) {
|
|
201
|
+
assert(
|
|
202
|
+
isStrictHexString(value),
|
|
203
|
+
'Value must be a hexadecimal string, starting with "0x".'
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
function isValidHexAddress(possibleAddress) {
|
|
207
|
+
return is(possibleAddress, HexAddressStruct) || isValidChecksumAddress(possibleAddress);
|
|
208
|
+
}
|
|
209
|
+
function getChecksumAddress(address) {
|
|
210
|
+
assert(is(address, HexChecksumAddressStruct), "Invalid hex address.");
|
|
211
|
+
const unPrefixed = remove0x(address.toLowerCase());
|
|
212
|
+
const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));
|
|
213
|
+
return `0x${unPrefixed.split("").map((character, nibbleIndex) => {
|
|
214
|
+
const hashCharacter = unPrefixedHash[nibbleIndex];
|
|
215
|
+
assert(is(hashCharacter, string()), "Hash shorter than address.");
|
|
216
|
+
return parseInt(hashCharacter, 16) > 7 ? character.toUpperCase() : character;
|
|
217
|
+
}).join("")}`;
|
|
218
|
+
}
|
|
219
|
+
function isValidChecksumAddress(possibleChecksum) {
|
|
220
|
+
if (!is(possibleChecksum, HexChecksumAddressStruct)) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
return getChecksumAddress(possibleChecksum) === possibleChecksum;
|
|
224
|
+
}
|
|
225
|
+
function add0x(hexadecimal) {
|
|
226
|
+
if (hexadecimal.startsWith("0x")) {
|
|
227
|
+
return hexadecimal;
|
|
228
|
+
}
|
|
229
|
+
if (hexadecimal.startsWith("0X")) {
|
|
230
|
+
return `0x${hexadecimal.substring(2)}`;
|
|
231
|
+
}
|
|
232
|
+
return `0x${hexadecimal}`;
|
|
233
|
+
}
|
|
234
|
+
function remove0x(hexadecimal) {
|
|
235
|
+
if (hexadecimal.startsWith("0x") || hexadecimal.startsWith("0X")) {
|
|
236
|
+
return hexadecimal.substring(2);
|
|
237
|
+
}
|
|
238
|
+
return hexadecimal;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export {
|
|
242
|
+
HexStruct,
|
|
243
|
+
StrictHexStruct,
|
|
244
|
+
HexAddressStruct,
|
|
245
|
+
HexChecksumAddressStruct,
|
|
246
|
+
isHexString,
|
|
247
|
+
isStrictHexString,
|
|
248
|
+
assertIsHexString,
|
|
249
|
+
assertIsStrictHexString,
|
|
250
|
+
isValidHexAddress,
|
|
251
|
+
getChecksumAddress,
|
|
252
|
+
isValidChecksumAddress,
|
|
253
|
+
add0x,
|
|
254
|
+
remove0x,
|
|
255
|
+
isBytes,
|
|
256
|
+
assertIsBytes,
|
|
257
|
+
bytesToHex,
|
|
258
|
+
bytesToBigInt,
|
|
259
|
+
bytesToSignedBigInt,
|
|
260
|
+
bytesToNumber,
|
|
261
|
+
bytesToString,
|
|
262
|
+
bytesToBase64,
|
|
263
|
+
hexToBytes,
|
|
264
|
+
bigIntToBytes,
|
|
265
|
+
signedBigIntToBytes,
|
|
266
|
+
numberToBytes,
|
|
267
|
+
stringToBytes,
|
|
268
|
+
base64ToBytes,
|
|
269
|
+
valueToBytes,
|
|
270
|
+
concatBytes,
|
|
271
|
+
createDataView
|
|
272
|
+
};
|
|
273
|
+
//# sourceMappingURL=chunk-BKR45GQY.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hex.ts","../src/bytes.ts"],"sourcesContent":["import { keccak_256 as keccak256 } from '@noble/hashes/sha3';\nimport type { Struct } from 'superstruct';\nimport { is, pattern, string } from 'superstruct';\n\nimport { assert } from './assert';\nimport { bytesToHex } from './bytes';\n\nexport type Hex = `0x${string}`;\n\nexport const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);\nexport const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct<\n Hex,\n null\n>;\nexport const HexAddressStruct = pattern(\n string(),\n /^0x[0-9a-f]{40}$/u,\n) as Struct<Hex, null>;\nexport const HexChecksumAddressStruct = pattern(\n string(),\n /^0x[0-9a-fA-F]{40}$/u,\n) as Struct<Hex, null>;\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return is(value, HexStruct);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return is(value, StrictHexStruct);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport function isValidHexAddress(possibleAddress: Hex) {\n return (\n is(possibleAddress, HexAddressStruct) ||\n isValidChecksumAddress(possibleAddress)\n );\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n *\n * @param address - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport function getChecksumAddress(address: Hex) {\n assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');\n const unPrefixed = remove0x(address.toLowerCase());\n const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));\n return `0x${unPrefixed\n .split('')\n .map((character, nibbleIndex) => {\n const hashCharacter = unPrefixedHash[nibbleIndex];\n assert(is(hashCharacter, string()), 'Hash shorter than address.');\n return parseInt(hashCharacter, 16) > 7\n ? character.toUpperCase()\n : character;\n })\n .join('')}`;\n}\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport function isValidChecksumAddress(possibleChecksum: Hex) {\n if (!is(possibleChecksum, HexChecksumAddressStruct)) {\n return false;\n }\n\n return getChecksumAddress(possibleChecksum) === possibleChecksum;\n}\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hexadecimal: string): Hex {\n if (hexadecimal.startsWith('0x')) {\n return hexadecimal as Hex;\n }\n\n if (hexadecimal.startsWith('0X')) {\n return `0x${hexadecimal.substring(2)}`;\n }\n\n return `0x${hexadecimal}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hexadecimal: string): string {\n if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {\n return hexadecimal.substring(2);\n }\n\n return hexadecimal;\n}\n","import { base64 } from '@scure/base';\n\nimport { assert } from './assert';\nimport type { Hex } from './hex';\nimport { add0x, assertIsHexString, remove0x } from './hex';\n\n// '0'.charCodeAt(0) === 48\nconst HEX_MINIMUM_NUMBER_CHARACTER = 48;\n\n// '9'.charCodeAt(0) === 57\nconst HEX_MAXIMUM_NUMBER_CHARACTER = 58;\nconst HEX_CHARACTER_OFFSET = 87;\n\nexport type Bytes = bigint | number | string | Uint8Array;\n\n/**\n * Memoized function that returns an array to be used as a lookup table for\n * converting bytes to hexadecimal values.\n *\n * The array is created lazily and then cached for future use. The benefit of\n * this approach is that the performance of converting bytes to hex is much\n * better than if we were to call `toString(16)` on each byte.\n *\n * The downside is that the array is created once and then never garbage\n * collected. This is not a problem in practice because the array is only 256\n * elements long.\n *\n * @returns A function that returns the lookup table.\n */\nfunction getPrecomputedHexValuesBuilder(): () => string[] {\n // To avoid issues with tree shaking, we need to use a function to return the\n // array. This is because the array is only used in the `bytesToHex` function\n // and if we were to use a global variable, the array might be removed by the\n // tree shaker.\n const lookupTable: string[] = [];\n\n return () => {\n if (lookupTable.length === 0) {\n for (let i = 0; i < 256; i++) {\n lookupTable.push(i.toString(16).padStart(2, '0'));\n }\n }\n\n return lookupTable;\n };\n}\n\n/**\n * Function implementation of the {@link getPrecomputedHexValuesBuilder}\n * function.\n */\nconst getPrecomputedHexValues = getPrecomputedHexValuesBuilder();\n\n/**\n * Check if a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @returns Whether the value is a `Uint8Array`.\n */\nexport function isBytes(value: unknown): value is Uint8Array {\n return value instanceof Uint8Array;\n}\n\n/**\n * Assert that a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @throws If the value is not a `Uint8Array`.\n */\nexport function assertIsBytes(value: unknown): asserts value is Uint8Array {\n assert(isBytes(value), 'Value must be a Uint8Array.');\n}\n\n/**\n * Convert a `Uint8Array` to a hexadecimal string.\n *\n * @param bytes - The bytes to convert to a hexadecimal string.\n * @returns The hexadecimal string.\n */\nexport function bytesToHex(bytes: Uint8Array): Hex {\n assertIsBytes(bytes);\n\n if (bytes.length === 0) {\n return '0x';\n }\n\n const lookupTable = getPrecomputedHexValues();\n const hexadecimal = new Array(bytes.length);\n\n for (let i = 0; i < bytes.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n hexadecimal[i] = lookupTable[bytes[i]!];\n }\n\n return add0x(hexadecimal.join(''));\n}\n\n/**\n * Convert a `Uint8Array` to a `bigint`.\n *\n * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.\n * To convert a two's complement encoded `Uint8Array` to a `bigint`, use\n * {@link bytesToSignedBigInt}.\n *\n * @param bytes - The bytes to convert to a `bigint`.\n * @returns The `bigint`.\n */\nexport function bytesToBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n const hexadecimal = bytesToHex(bytes);\n return BigInt(hexadecimal);\n}\n\n/**\n * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are\n * encoded in two's complement.\n *\n * To convert a `Uint8Array` to an unsigned `bigint` instead, use\n * {@link bytesToBigInt}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param bytes - The bytes to convert to a signed `bigint`.\n * @returns The signed `bigint`.\n */\nexport function bytesToSignedBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n let value = BigInt(0);\n for (const byte of bytes) {\n // eslint-disable-next-line no-bitwise\n value = (value << BigInt(8)) + BigInt(byte);\n }\n\n return BigInt.asIntN(bytes.length * 8, value);\n}\n\n/**\n * Convert a `Uint8Array` to a `number`.\n *\n * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.\n *\n * @param bytes - The bytes to convert to a number.\n * @returns The number.\n * @throws If the resulting number is not a safe integer.\n */\nexport function bytesToNumber(bytes: Uint8Array): number {\n assertIsBytes(bytes);\n\n const bigint = bytesToBigInt(bytes);\n\n assert(\n bigint <= BigInt(Number.MAX_SAFE_INTEGER),\n 'Number is not a safe integer. Use `bytesToBigInt` instead.',\n );\n\n return Number(bigint);\n}\n\n/**\n * Convert a UTF-8 encoded `Uint8Array` to a `string`.\n *\n * @param bytes - The bytes to convert to a string.\n * @returns The string.\n */\nexport function bytesToString(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return new TextDecoder().decode(bytes);\n}\n\n/**\n * Convert a `Uint8Array` to a base64 encoded string.\n *\n * @param bytes - The bytes to convert to a base64 encoded string.\n * @returns The base64 encoded string.\n */\nexport function bytesToBase64(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return base64.encode(bytes);\n}\n\n/**\n * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be\n * prefixed with `0x`. It accepts even and odd length strings.\n *\n * If the value is \"0x\", an empty `Uint8Array` is returned.\n *\n * @param value - The hexadecimal string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function hexToBytes(value: string): Uint8Array {\n // \"0x\" is often used as empty byte array.\n if (value?.toLowerCase?.() === '0x') {\n return new Uint8Array();\n }\n\n assertIsHexString(value);\n\n // Remove the `0x` prefix if it exists, and pad the string to have an even\n // number of characters.\n const strippedValue = remove0x(value).toLowerCase();\n const normalizedValue =\n strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;\n const bytes = new Uint8Array(normalizedValue.length / 2);\n\n for (let i = 0; i < bytes.length; i++) {\n // While this is not the prettiest way to convert a hexadecimal string to a\n // `Uint8Array`, it is a lot faster than using `parseInt` to convert each\n // character.\n const c1 = normalizedValue.charCodeAt(i * 2);\n const c2 = normalizedValue.charCodeAt(i * 2 + 1);\n const n1 =\n c1 -\n (c1 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n const n2 =\n c2 -\n (c2 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n\n bytes[i] = n1 * 16 + n2;\n }\n\n return bytes;\n}\n\n/**\n * Convert a `bigint` to a `Uint8Array`.\n *\n * This assumes that the `bigint` is an unsigned integer. To convert a signed\n * `bigint` instead, use {@link signedBigIntToBytes}.\n *\n * @param value - The bigint to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function bigIntToBytes(value: bigint): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= BigInt(0), 'Value must be a non-negative bigint.');\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Check if a `bigint` fits in a certain number of bytes.\n *\n * @param value - The `bigint` to check.\n * @param bytes - The number of bytes.\n * @returns Whether the `bigint` fits in the number of bytes.\n */\nfunction bigIntFits(value: bigint, bytes: number): boolean {\n assert(bytes > 0);\n\n /* eslint-disable no-bitwise */\n const mask = value >> BigInt(31);\n return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));\n /* eslint-enable no-bitwise */\n}\n\n/**\n * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement\n * encoding to represent negative numbers.\n *\n * To convert an unsigned `bigint` to a `Uint8Array` instead, use\n * {@link bigIntToBytes}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param value - The number to convert to bytes.\n * @param byteLength - The length of the resulting `Uint8Array`. If the number\n * is larger than the maximum value that can be represented by the given length,\n * an error is thrown.\n * @returns The bytes as `Uint8Array`.\n */\nexport function signedBigIntToBytes(\n value: bigint,\n byteLength: number,\n): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(typeof byteLength === 'number', 'Byte length must be a number.');\n assert(byteLength > 0, 'Byte length must be greater than 0.');\n assert(\n bigIntFits(value, byteLength),\n 'Byte length is too small to represent the given value.',\n );\n\n // ESLint doesn't like mutating function parameters, so to avoid having to\n // disable the rule, we create a new variable.\n let numberValue = value;\n const bytes = new Uint8Array(byteLength);\n\n for (let i = 0; i < bytes.length; i++) {\n bytes[i] = Number(BigInt.asUintN(8, numberValue));\n // eslint-disable-next-line no-bitwise\n numberValue >>= BigInt(8);\n }\n\n return bytes.reverse();\n}\n\n/**\n * Convert a `number` to a `Uint8Array`.\n *\n * @param value - The number to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n * @throws If the number is not a safe integer.\n */\nexport function numberToBytes(value: number): Uint8Array {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToBytes` instead.',\n );\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Convert a `string` to a UTF-8 encoded `Uint8Array`.\n *\n * @param value - The string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function stringToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return new TextEncoder().encode(value);\n}\n\n/**\n * Convert a base64 encoded string to a `Uint8Array`.\n *\n * @param value - The base64 encoded string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function base64ToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return base64.decode(value);\n}\n\n/**\n * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,\n * a `bigint`, a `number`, or a `string`.\n *\n * This will attempt to guess the type of the value based on its type and\n * contents. For more control over the conversion, use the more specific\n * conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.\n *\n * If the value is a `string`, and it is prefixed with `0x`, it will be\n * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a\n * UTF-8 string. To convert a hexadecimal string to bytes without interpreting\n * it as a UTF-8 string, use {@link hexToBytes} instead.\n *\n * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed\n * `bigint` to bytes, use {@link signedBigIntToBytes} instead.\n *\n * If the value is a `Uint8Array`, it will be returned as-is.\n *\n * @param value - The value to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function valueToBytes(value: Bytes): Uint8Array {\n if (typeof value === 'bigint') {\n return bigIntToBytes(value);\n }\n\n if (typeof value === 'number') {\n return numberToBytes(value);\n }\n\n if (typeof value === 'string') {\n if (value.startsWith('0x')) {\n return hexToBytes(value);\n }\n\n return stringToBytes(value);\n }\n\n if (isBytes(value)) {\n return value;\n }\n\n throw new TypeError(`Unsupported value type: \"${typeof value}\".`);\n}\n\n/**\n * Concatenate multiple byte-like values into a single `Uint8Array`. The values\n * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses\n * {@link valueToBytes} under the hood to convert each value to bytes. Refer to\n * the documentation of that function for more information.\n *\n * @param values - The values to concatenate.\n * @returns The concatenated bytes as `Uint8Array`.\n */\nexport function concatBytes(values: Bytes[]): Uint8Array {\n const normalizedValues = new Array(values.length);\n let byteLength = 0;\n\n for (let i = 0; i < values.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const value = valueToBytes(values[i]!);\n\n normalizedValues[i] = value;\n byteLength += value.length;\n }\n\n const bytes = new Uint8Array(byteLength);\n for (let i = 0, offset = 0; i < normalizedValues.length; i++) {\n // While we could simply spread the values into an array and use\n // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.\n bytes.set(normalizedValues[i], offset);\n offset += normalizedValues[i].length;\n }\n\n return bytes;\n}\n\n/**\n * Create a {@link DataView} from a {@link Uint8Array}. This is a convenience\n * function that avoids having to create a {@link DataView} manually, which\n * requires passing the `byteOffset` and `byteLength` parameters every time.\n *\n * Not passing the `byteOffset` and `byteLength` parameters can result in\n * unexpected behavior when the {@link Uint8Array} is a view of a larger\n * {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.\n *\n * This function also supports Node.js {@link Buffer}s.\n *\n * @example\n * ```typescript\n * const bytes = new Uint8Array([1, 2, 3]);\n *\n * // This is equivalent to:\n * // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n * const dataView = createDataView(bytes);\n * ```\n * @param bytes - The bytes to create the {@link DataView} from.\n * @returns The {@link DataView}.\n */\nexport function createDataView(bytes: Uint8Array): DataView {\n // To maintain compatibility with Node.js, we need to check if the bytes are\n // a Buffer. If so, we need to slice the buffer to get the underlying\n // ArrayBuffer.\n // eslint-disable-next-line no-restricted-globals\n if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {\n const buffer = bytes.buffer.slice(\n bytes.byteOffset,\n bytes.byteOffset + bytes.byteLength,\n );\n\n return new DataView(buffer);\n }\n\n return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n}\n"],"mappings":"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","names":[]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getErrorMessage
|
|
3
|
+
} from "./chunk-7S5AWFX7.mjs";
|
|
4
|
+
|
|
5
|
+
// src/assert.ts
|
|
6
|
+
import { assert as assertSuperstruct } from "superstruct";
|
|
7
|
+
function isConstructable(fn) {
|
|
8
|
+
return Boolean(typeof fn?.prototype?.constructor?.name === "string");
|
|
9
|
+
}
|
|
10
|
+
function getErrorMessageWithoutTrailingPeriod(error) {
|
|
11
|
+
return getErrorMessage(error).replace(/\.$/u, "");
|
|
12
|
+
}
|
|
13
|
+
function getError(ErrorWrapper, message) {
|
|
14
|
+
if (isConstructable(ErrorWrapper)) {
|
|
15
|
+
return new ErrorWrapper({
|
|
16
|
+
message
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return ErrorWrapper({
|
|
20
|
+
message
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
var AssertionError = class extends Error {
|
|
24
|
+
constructor(options) {
|
|
25
|
+
super(options.message);
|
|
26
|
+
this.code = "ERR_ASSERTION";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
function assert(value, message = "Assertion failed.", ErrorWrapper = AssertionError) {
|
|
30
|
+
if (!value) {
|
|
31
|
+
if (message instanceof Error) {
|
|
32
|
+
throw message;
|
|
33
|
+
}
|
|
34
|
+
throw getError(ErrorWrapper, message);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function assertStruct(value, struct, errorPrefix = "Assertion failed", ErrorWrapper = AssertionError) {
|
|
38
|
+
try {
|
|
39
|
+
assertSuperstruct(value, struct);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw getError(
|
|
42
|
+
ErrorWrapper,
|
|
43
|
+
`${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function assertExhaustive(_object) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"Invalid branch reached. Should be detected during compilation."
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export {
|
|
54
|
+
AssertionError,
|
|
55
|
+
assert,
|
|
56
|
+
assertStruct,
|
|
57
|
+
assertExhaustive
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=chunk-CSDMTDND.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/assert.ts"],"sourcesContent":["import type { Struct } from 'superstruct';\nimport { assert as assertSuperstruct } from '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// eslint-disable-next-line @typescript-eslint/naming-convention\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 // eslint-disable-next-line @typescript-eslint/naming-convention\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 // eslint-disable-next-line @typescript-eslint/naming-convention\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"],"mappings":";;;;;AACA,SAAS,UAAU,yBAAyB;AAe5C,SAAS,gBACP,IACgD;AAEhD,SAAO,QAAQ,OAAO,IAAI,WAAW,aAAa,SAAS,QAAQ;AACrE;AAYA,SAAS,qCAAqC,OAAwB;AAEpE,SAAO,gBAAgB,KAAK,EAAE,QAAQ,QAAQ,EAAE;AAClD;AAUA,SAAS,SAAS,cAAyC,SAAiB;AAC1E,MAAI,gBAAgB,YAAY,GAAG;AACjC,WAAO,IAAI,aAAa;AAAA,MACtB;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO,aAAa;AAAA,IAClB;AAAA,EACF,CAAC;AACH;AAKO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAGxC,YAAY,SAA8B;AACxC,UAAM,QAAQ,OAAO;AAHvB,SAAS,OAAO;AAAA,EAIhB;AACF;AAcO,SAAS,OACd,OACA,UAA0B,qBAE1B,eAA0C,gBAC3B;AACf,MAAI,CAAC,OAAO;AACV,QAAI,mBAAmB,OAAO;AAC5B,YAAM;AAAA,IACR;AAEA,UAAM,SAAS,cAAc,OAAO;AAAA,EACtC;AACF;AAaO,SAAS,aACd,OACA,QACA,cAAc,oBAEd,eAA0C,gBACnB;AACvB,MAAI;AACF,sBAAkB,OAAO,MAAM;AAAA,EACjC,SAAS,OAAO;AACd,UAAM;AAAA,MACJ;AAAA,MACA,GAAG,WAAW,KAAK,qCAAqC,KAAK,CAAC;AAAA,IAChE;AAAA,EACF;AACF;AAqBO,SAAS,iBAAiB,SAAuB;AACtD,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkQVEKZRZ2js = require('./chunk-QVEKZRZ2.js');
|
|
5
|
+
|
|
6
|
+
// src/errors.ts
|
|
7
|
+
var _ponycause = require('pony-cause');
|
|
8
|
+
function isError(error) {
|
|
9
|
+
return error instanceof Error || _chunkQVEKZRZ2js.isObject.call(void 0, error) && error.constructor.name === "Error";
|
|
10
|
+
}
|
|
11
|
+
function isErrorWithCode(error) {
|
|
12
|
+
return typeof error === "object" && error !== null && "code" in error;
|
|
13
|
+
}
|
|
14
|
+
function isErrorWithMessage(error) {
|
|
15
|
+
return typeof error === "object" && error !== null && "message" in error;
|
|
16
|
+
}
|
|
17
|
+
function isErrorWithStack(error) {
|
|
18
|
+
return typeof error === "object" && error !== null && "stack" in error;
|
|
19
|
+
}
|
|
20
|
+
function getErrorMessage(error) {
|
|
21
|
+
if (isErrorWithMessage(error) && typeof error.message === "string") {
|
|
22
|
+
return error.message;
|
|
23
|
+
}
|
|
24
|
+
if (_chunkQVEKZRZ2js.isNullOrUndefined.call(void 0, error)) {
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
return String(error);
|
|
28
|
+
}
|
|
29
|
+
function wrapError(originalError, message) {
|
|
30
|
+
if (isError(originalError)) {
|
|
31
|
+
const error = Error.length === 2 ? (
|
|
32
|
+
// This branch is getting tested by using the Node version that
|
|
33
|
+
// supports `cause` on the Error constructor.
|
|
34
|
+
// istanbul ignore next
|
|
35
|
+
// Also, for some reason `tsserver` is not complaining that the
|
|
36
|
+
// Error constructor doesn't support a second argument in the editor,
|
|
37
|
+
// but `tsc` does. I'm not sure why, but we disable this in the
|
|
38
|
+
// meantime.
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
new Error(message, { cause: originalError })
|
|
42
|
+
) : new (0, _ponycause.ErrorWithCause)(message, { cause: originalError });
|
|
43
|
+
if (isErrorWithCode(originalError)) {
|
|
44
|
+
error.code = originalError.code;
|
|
45
|
+
}
|
|
46
|
+
return error;
|
|
47
|
+
}
|
|
48
|
+
if (message.length > 0) {
|
|
49
|
+
return new Error(`${String(originalError)}: ${message}`);
|
|
50
|
+
}
|
|
51
|
+
return new Error(String(originalError));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
exports.isErrorWithCode = isErrorWithCode; exports.isErrorWithMessage = isErrorWithMessage; exports.isErrorWithStack = isErrorWithStack; exports.getErrorMessage = getErrorMessage; exports.wrapError = wrapError;
|
|
61
|
+
//# sourceMappingURL=chunk-DMPG3H3G.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,sBAAsB;AAY/B,SAAS,QAAQ,OAAgC;AAC/C,SACE,iBAAiB,SAChB,SAAS,KAAK,KAAK,MAAM,YAAY,SAAS;AAEnD;AAUO,SAAS,gBAAgB,OAA2C;AACzE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,UAAU;AAClE;AASO,SAAS,mBACd,OAC8B;AAC9B,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,aAAa;AACrE;AASO,SAAS,iBAAiB,OAA4C;AAC3E,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,WAAW;AACnE;AAWO,SAAS,gBAAgB,OAAwB;AACtD,MAAI,mBAAmB,KAAK,KAAK,OAAO,MAAM,YAAY,UAAU;AAClE,WAAO,MAAM;AAAA,EACf;AAEA,MAAI,kBAAkB,KAAK,GAAG;AAC5B,WAAO;AAAA,EACT;AAEA,SAAO,OAAO,KAAK;AACrB;AAeO,SAAS,UACd,eACA,SAC2B;AAC3B,MAAI,QAAQ,aAAa,GAAG;AAC1B,UAAM,QACJ,MAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAUb,IAAI,MAAM,SAAS,EAAE,OAAO,cAAc,CAAC;AAAA,QAC3C,IAAI,eAAe,SAAS,EAAE,OAAO,cAAc,CAAC;AAE1D,QAAI,gBAAgB,aAAa,GAAG;AAClC,YAAM,OAAO,cAAc;AAAA,IAC7B;AAEA,WAAO;AAAA,EACT;AAEA,MAAI,QAAQ,SAAS,GAAG;AACtB,WAAO,IAAI,MAAM,GAAG,OAAO,aAAa,CAAC,KAAK,OAAO,EAAE;AAAA,EACzD;AAEA,SAAO,IAAI,MAAM,OAAO,aAAa,CAAC;AACxC","sourcesContent":["import { ErrorWithCause } from 'pony-cause';\n\nimport { isNullOrUndefined, isObject } from './misc';\n\n/**\n * Type guard for determining whether the given value is an instance of Error.\n * For errors generated via `fs.promises`, `error instanceof Error` won't work,\n * so we have to come up with another way of testing.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nfunction isError(error: unknown): error is Error {\n return (\n error instanceof Error ||\n (isObject(error) && error.constructor.name === 'Error')\n );\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `code` property such as the type of error that Node throws for filesystem\n * operations, etc.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithCode(error: unknown): error is { code: string } {\n return typeof error === 'object' && error !== null && 'code' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `message` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithMessage(\n error: unknown,\n): error is { message: string } {\n return typeof error === 'object' && error !== null && 'message' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `stack` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithStack(error: unknown): error is { stack: string } {\n return typeof error === 'object' && error !== null && 'stack' in error;\n}\n\n/**\n * Attempts to obtain the message from a possible error object, defaulting to an\n * empty string if it is impossible to do so.\n *\n * @param error - The possible error to get the message from.\n * @returns The message if `error` is an object with a `message` property;\n * the string version of `error` if it is not `undefined` or `null`; otherwise\n * an empty string.\n */\nexport function getErrorMessage(error: unknown): string {\n if (isErrorWithMessage(error) && typeof error.message === 'string') {\n return error.message;\n }\n\n if (isNullOrUndefined(error)) {\n return '';\n }\n\n return String(error);\n}\n\n/**\n * Builds a new error object, linking it to the original error via the `cause`\n * property if it is an Error.\n *\n * This function is useful to reframe error messages in general, but is\n * _critical_ when interacting with any of Node's filesystem functions as\n * provided via `fs.promises`, because these do not produce stack traces in the\n * case of an I/O error (see <https://github.com/nodejs/node/issues/30944>).\n *\n * @param originalError - The error to be wrapped (something throwable).\n * @param message - The desired message of the new error.\n * @returns A new error object.\n */\nexport function wrapError<Throwable>(\n originalError: Throwable,\n message: string,\n): Error & { code?: string } {\n if (isError(originalError)) {\n const error: Error & { code?: string } =\n Error.length === 2\n ? // This branch is getting tested by using the Node version that\n // supports `cause` on the Error constructor.\n // istanbul ignore next\n // Also, for some reason `tsserver` is not complaining that the\n // Error constructor doesn't support a second argument in the editor,\n // but `tsc` does. I'm not sure why, but we disable this in the\n // meantime.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n new Error(message, { cause: originalError })\n : new ErrorWithCause(message, { cause: originalError });\n\n if (isErrorWithCode(originalError)) {\n error.code = originalError.code;\n }\n\n return error;\n }\n\n if (message.length > 0) {\n return new Error(`${String(originalError)}: ${message}`);\n }\n\n return new Error(String(originalError));\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-DYMCEQIV.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";//# sourceMappingURL=chunk-EQMZL4XU.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertStruct
|
|
3
|
+
} from "./chunk-CSDMTDND.mjs";
|
|
4
|
+
|
|
5
|
+
// src/versions.ts
|
|
6
|
+
import {
|
|
7
|
+
gt as gtSemver,
|
|
8
|
+
gtr as gtrSemver,
|
|
9
|
+
satisfies as satisfiesSemver,
|
|
10
|
+
valid as validSemVerVersion,
|
|
11
|
+
validRange as validSemVerRange
|
|
12
|
+
} from "semver";
|
|
13
|
+
import { is, refine, string } from "superstruct";
|
|
14
|
+
var VersionStruct = refine(
|
|
15
|
+
string(),
|
|
16
|
+
"Version",
|
|
17
|
+
(value) => {
|
|
18
|
+
if (validSemVerVersion(value) === null) {
|
|
19
|
+
return `Expected SemVer version, got "${value}"`;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
var VersionRangeStruct = refine(
|
|
25
|
+
string(),
|
|
26
|
+
"Version range",
|
|
27
|
+
(value) => {
|
|
28
|
+
if (validSemVerRange(value) === null) {
|
|
29
|
+
return `Expected SemVer range, got "${value}"`;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
function isValidSemVerVersion(version) {
|
|
35
|
+
return is(version, VersionStruct);
|
|
36
|
+
}
|
|
37
|
+
function isValidSemVerRange(versionRange) {
|
|
38
|
+
return is(versionRange, VersionRangeStruct);
|
|
39
|
+
}
|
|
40
|
+
function assertIsSemVerVersion(version) {
|
|
41
|
+
assertStruct(version, VersionStruct);
|
|
42
|
+
}
|
|
43
|
+
function assertIsSemVerRange(range) {
|
|
44
|
+
assertStruct(range, VersionRangeStruct);
|
|
45
|
+
}
|
|
46
|
+
function gtVersion(version1, version2) {
|
|
47
|
+
return gtSemver(version1, version2);
|
|
48
|
+
}
|
|
49
|
+
function gtRange(version, range) {
|
|
50
|
+
return gtrSemver(version, range);
|
|
51
|
+
}
|
|
52
|
+
function satisfiesVersionRange(version, versionRange) {
|
|
53
|
+
return satisfiesSemver(version, versionRange, {
|
|
54
|
+
includePrerelease: true
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export {
|
|
59
|
+
VersionStruct,
|
|
60
|
+
VersionRangeStruct,
|
|
61
|
+
isValidSemVerVersion,
|
|
62
|
+
isValidSemVerRange,
|
|
63
|
+
assertIsSemVerVersion,
|
|
64
|
+
assertIsSemVerRange,
|
|
65
|
+
gtVersion,
|
|
66
|
+
gtRange,
|
|
67
|
+
satisfiesVersionRange
|
|
68
|
+
};
|
|
69
|
+
//# sourceMappingURL=chunk-G66TREC6.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/versions.ts"],"sourcesContent":["import {\n gt as gtSemver,\n gtr as gtrSemver,\n satisfies as satisfiesSemver,\n valid as validSemVerVersion,\n validRange as validSemVerRange,\n} from 'semver';\nimport type { Struct } from 'superstruct';\nimport { is, refine, string } from 'superstruct';\n\nimport { assertStruct } from './assert';\nimport type { Opaque } from './opaque';\n\n/**\n * {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for SemVer ranges.\n *\n * @example Use {@link assertIsSemVerRange} and {@link isValidSemVerRange} to cast to proper type.\n * ```typescript\n * const unsafeRange: string = dataFromUser();\n * assertIsSemVerRange(unsafeRange);\n * unsafeRange\n * // ^? SemVerRange\n * ```\n * @example If you know what you're doing and want to side-step type safety, casting from a string works correctly.\n * ```typescript\n * const unsafeRange: string = dataFromUser();\n * unsafeRange as SemVerRange;\n * // ^? SemVerRange\n * ```\n * @see {@link assertIsSemVerRange}\n * @see {@link isValidSemVerRange}\n */\nexport type SemVerRange = Opaque<string, typeof semVerRange>;\ndeclare const semVerRange: unique symbol;\n\n/**\n * {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for singular SemVer version.\n *\n * @example Use {@link assertIsSemVerVersion} and {@link isValidSemVerVersion} to cast to proper type.\n * ```typescript\n * const unsafeVersion: string = dataFromUser();\n * assertIsSemVerVersion(unsafeRange);\n * unsafeVersion\n * // ^? SemVerVersion\n * ```\n * @example If you know what you're doing and want to side-step type safety, casting from a string works correctly.\n * ```typescript\n * const unsafeVersion: string = dataFromUser();\n * unsafeRange as SemVerVersion;\n * // ^? SemVerVersion\n * ```\n * @see {@link assertIsSemVerVersion}\n * @see {@link isValidSemVerVersion}\n */\nexport type SemVerVersion = Opaque<string, typeof semVerVersion>;\ndeclare const semVerVersion: unique symbol;\n\n/**\n * A struct for validating a version string.\n */\nexport const VersionStruct = refine<SemVerVersion, null>(\n string() as unknown as Struct<SemVerVersion, null>,\n 'Version',\n (value) => {\n if (validSemVerVersion(value) === null) {\n return `Expected SemVer version, got \"${value}\"`;\n }\n return true;\n },\n);\n\nexport const VersionRangeStruct = refine<SemVerRange, null>(\n string() as unknown as Struct<SemVerRange, null>,\n 'Version range',\n (value) => {\n if (validSemVerRange(value) === null) {\n return `Expected SemVer range, got \"${value}\"`;\n }\n return true;\n },\n);\n\n/**\n * Checks whether a SemVer version is valid.\n *\n * @param version - A potential version.\n * @returns `true` if the version is valid, and `false` otherwise.\n */\nexport function isValidSemVerVersion(\n version: unknown,\n): version is SemVerVersion {\n return is(version, VersionStruct);\n}\n\n/**\n * Checks whether a SemVer version range is valid.\n *\n * @param versionRange - A potential version range.\n * @returns `true` if the version range is valid, and `false` otherwise.\n */\nexport function isValidSemVerRange(\n versionRange: unknown,\n): versionRange is SemVerRange {\n return is(versionRange, VersionRangeStruct);\n}\n\n/**\n * Asserts that a value is a valid concrete SemVer version.\n *\n * @param version - A potential SemVer concrete version.\n */\nexport function assertIsSemVerVersion(\n version: unknown,\n): asserts version is SemVerVersion {\n assertStruct(version, VersionStruct);\n}\n\n/**\n * Asserts that a value is a valid SemVer range.\n *\n * @param range - A potential SemVer range.\n */\nexport function assertIsSemVerRange(\n range: unknown,\n): asserts range is SemVerRange {\n assertStruct(range, VersionRangeStruct);\n}\n\n/**\n * Checks whether a SemVer version is greater than another.\n *\n * @param version1 - The left-hand version.\n * @param version2 - The right-hand version.\n * @returns `version1 > version2`.\n */\nexport function gtVersion(\n version1: SemVerVersion,\n version2: SemVerVersion,\n): boolean {\n return gtSemver(version1, version2);\n}\n\n/**\n * Checks whether a SemVer version is greater than all possibilities in a range.\n *\n * @param version - A SemvVer version.\n * @param range - The range to check against.\n * @returns `version > range`.\n */\nexport function gtRange(version: SemVerVersion, range: SemVerRange): boolean {\n return gtrSemver(version, range);\n}\n\n/**\n * Returns whether a SemVer version satisfies a SemVer range.\n *\n * @param version - The SemVer version to check.\n * @param versionRange - The SemVer version range to check against.\n * @returns Whether the version satisfied the version range.\n */\nexport function satisfiesVersionRange(\n version: SemVerVersion,\n versionRange: SemVerRange,\n): boolean {\n return satisfiesSemver(version, versionRange, {\n includePrerelease: true,\n });\n}\n"],"mappings":";;;;;AAAA;AAAA,EACE,MAAM;AAAA,EACN,OAAO;AAAA,EACP,aAAa;AAAA,EACb,SAAS;AAAA,EACT,cAAc;AAAA,OACT;AAEP,SAAS,IAAI,QAAQ,cAAc;AAoD5B,IAAM,gBAAgB;AAAA,EAC3B,OAAO;AAAA,EACP;AAAA,EACA,CAAC,UAAU;AACT,QAAI,mBAAmB,KAAK,MAAM,MAAM;AACtC,aAAO,iCAAiC,KAAK;AAAA,IAC/C;AACA,WAAO;AAAA,EACT;AACF;AAEO,IAAM,qBAAqB;AAAA,EAChC,OAAO;AAAA,EACP;AAAA,EACA,CAAC,UAAU;AACT,QAAI,iBAAiB,KAAK,MAAM,MAAM;AACpC,aAAO,+BAA+B,KAAK;AAAA,IAC7C;AACA,WAAO;AAAA,EACT;AACF;AAQO,SAAS,qBACd,SAC0B;AAC1B,SAAO,GAAG,SAAS,aAAa;AAClC;AAQO,SAAS,mBACd,cAC6B;AAC7B,SAAO,GAAG,cAAc,kBAAkB;AAC5C;AAOO,SAAS,sBACd,SACkC;AAClC,eAAa,SAAS,aAAa;AACrC;AAOO,SAAS,oBACd,OAC8B;AAC9B,eAAa,OAAO,kBAAkB;AACxC;AASO,SAAS,UACd,UACA,UACS;AACT,SAAO,SAAS,UAAU,QAAQ;AACpC;AASO,SAAS,QAAQ,SAAwB,OAA6B;AAC3E,SAAO,UAAU,SAAS,KAAK;AACjC;AASO,SAAS,sBACd,SACA,cACS;AACT,SAAO,gBAAgB,SAAS,cAAc;AAAA,IAC5C,mBAAmB;AAAA,EACrB,CAAC;AACH;","names":[]}
|