@metamask/snaps-utils 9.3.0 → 10.0.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 +30 -1
- package/dist/account.cjs +53 -0
- package/dist/account.cjs.map +1 -1
- package/dist/account.d.cts +26 -1
- package/dist/account.d.cts.map +1 -1
- package/dist/account.d.mts +26 -1
- package/dist/account.d.mts.map +1 -1
- package/dist/account.mjs +49 -1
- package/dist/account.mjs.map +1 -1
- package/dist/cronjob.cjs +16 -15
- package/dist/cronjob.cjs.map +1 -1
- package/dist/cronjob.d.cts +27 -36
- package/dist/cronjob.d.cts.map +1 -1
- package/dist/cronjob.d.mts +27 -36
- package/dist/cronjob.d.mts.map +1 -1
- package/dist/cronjob.mjs +16 -14
- package/dist/cronjob.mjs.map +1 -1
- package/dist/errors.cjs +22 -24
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +22 -24
- package/dist/errors.mjs.map +1 -1
- package/dist/eval-worker.cjs +38 -11
- package/dist/eval-worker.cjs.map +1 -1
- package/dist/eval-worker.d.cts.map +1 -1
- package/dist/eval-worker.d.mts.map +1 -1
- package/dist/eval-worker.mjs +38 -11
- package/dist/eval-worker.mjs.map +1 -1
- package/dist/eval.cjs +7 -0
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.d.cts +7 -0
- package/dist/eval.d.cts.map +1 -1
- package/dist/eval.d.mts +7 -0
- package/dist/eval.d.mts.map +1 -1
- package/dist/eval.mjs +7 -0
- package/dist/eval.mjs.map +1 -1
- package/dist/fs.cjs +51 -6
- package/dist/fs.cjs.map +1 -1
- package/dist/fs.d.cts +16 -4
- package/dist/fs.d.cts.map +1 -1
- package/dist/fs.d.mts +16 -4
- package/dist/fs.d.mts.map +1 -1
- package/dist/fs.mjs +49 -5
- package/dist/fs.mjs.map +1 -1
- package/dist/handlers/home-page.d.cts +3 -3
- package/dist/handlers/home-page.d.mts +3 -3
- package/dist/handlers/settings-page.d.cts +1 -1
- package/dist/handlers/settings-page.d.mts +1 -1
- package/dist/handlers/signature.d.cts +1 -1
- package/dist/handlers/signature.d.mts +1 -1
- package/dist/handlers/transaction.d.cts +3 -3
- package/dist/handlers/transaction.d.mts +3 -3
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/manifest/manifest.cjs +36 -4
- package/dist/manifest/manifest.cjs.map +1 -1
- package/dist/manifest/manifest.d.cts +40 -7
- package/dist/manifest/manifest.d.cts.map +1 -1
- package/dist/manifest/manifest.d.mts +40 -7
- package/dist/manifest/manifest.d.mts.map +1 -1
- package/dist/manifest/manifest.mjs +13 -4
- package/dist/manifest/manifest.mjs.map +1 -1
- package/dist/manifest/validator-types.cjs.map +1 -1
- package/dist/manifest/validator-types.d.cts +18 -1
- package/dist/manifest/validator-types.d.cts.map +1 -1
- package/dist/manifest/validator-types.d.mts +18 -1
- package/dist/manifest/validator-types.d.mts.map +1 -1
- package/dist/manifest/validator-types.mjs.map +1 -1
- package/dist/manifest/validator.cjs +21 -21
- package/dist/manifest/validator.cjs.map +1 -1
- package/dist/manifest/validator.d.cts +4 -2
- package/dist/manifest/validator.d.cts.map +1 -1
- package/dist/manifest/validator.d.mts +4 -2
- package/dist/manifest/validator.d.mts.map +1 -1
- package/dist/manifest/validator.mjs +21 -21
- package/dist/manifest/validator.mjs.map +1 -1
- package/dist/manifest/validators/index.cjs +2 -0
- package/dist/manifest/validators/index.cjs.map +1 -1
- package/dist/manifest/validators/index.d.cts +2 -0
- package/dist/manifest/validators/index.d.cts.map +1 -1
- package/dist/manifest/validators/index.d.mts +2 -0
- package/dist/manifest/validators/index.d.mts.map +1 -1
- package/dist/manifest/validators/index.mjs +2 -0
- package/dist/manifest/validators/index.mjs.map +1 -1
- package/dist/manifest/validators/production-platform-version.cjs +47 -0
- package/dist/manifest/validators/production-platform-version.cjs.map +1 -0
- package/dist/manifest/validators/production-platform-version.d.cts +7 -0
- package/dist/manifest/validators/production-platform-version.d.cts.map +1 -0
- package/dist/manifest/validators/production-platform-version.d.mts +7 -0
- package/dist/manifest/validators/production-platform-version.d.mts.map +1 -0
- package/dist/manifest/validators/production-platform-version.mjs +44 -0
- package/dist/manifest/validators/production-platform-version.mjs.map +1 -0
- package/dist/manifest/validators/unused-exports.cjs +53 -0
- package/dist/manifest/validators/unused-exports.cjs.map +1 -0
- package/dist/manifest/validators/unused-exports.d.cts +7 -0
- package/dist/manifest/validators/unused-exports.d.cts.map +1 -0
- package/dist/manifest/validators/unused-exports.d.mts +7 -0
- package/dist/manifest/validators/unused-exports.d.mts.map +1 -0
- package/dist/manifest/validators/unused-exports.mjs +50 -0
- package/dist/manifest/validators/unused-exports.mjs.map +1 -0
- package/dist/time.cjs +15 -1
- package/dist/time.cjs.map +1 -1
- package/dist/time.d.cts +7 -0
- package/dist/time.d.cts.map +1 -1
- package/dist/time.d.mts +7 -0
- package/dist/time.d.mts.map +1 -1
- package/dist/time.mjs +13 -0
- package/dist/time.mjs.map +1 -1
- package/dist/virtual-file/VirtualFile.cjs +4 -0
- package/dist/virtual-file/VirtualFile.cjs.map +1 -1
- package/dist/virtual-file/VirtualFile.mjs +4 -0
- package/dist/virtual-file/VirtualFile.mjs.map +1 -1
- package/package.json +8 -7
package/dist/cronjob.mjs
CHANGED
|
@@ -1,36 +1,38 @@
|
|
|
1
|
+
import { selectiveUnion } from "@metamask/snaps-sdk";
|
|
1
2
|
import { array, create, object, optional, refine, string } from "@metamask/superstruct";
|
|
2
|
-
import { JsonRpcIdStruct, JsonRpcParamsStruct, JsonRpcVersionStruct } from "@metamask/utils";
|
|
3
|
+
import { hasProperty, isObject, JsonRpcIdStruct, JsonRpcParamsStruct, JsonRpcVersionStruct } from "@metamask/utils";
|
|
3
4
|
import $cronparser from "cron-parser";
|
|
4
5
|
const { parseExpression } = $cronparser;
|
|
6
|
+
import { ISO8601DurationStruct } from "./time.mjs";
|
|
5
7
|
export const CronjobRpcRequestStruct = object({
|
|
6
8
|
jsonrpc: optional(JsonRpcVersionStruct),
|
|
7
9
|
id: optional(JsonRpcIdStruct),
|
|
8
10
|
method: string(),
|
|
9
11
|
params: optional(JsonRpcParamsStruct),
|
|
10
12
|
});
|
|
11
|
-
export const CronExpressionStruct = refine(string(), '
|
|
13
|
+
export const CronExpressionStruct = refine(string(), 'cronjob expression', (value) => {
|
|
12
14
|
try {
|
|
13
15
|
parseExpression(value);
|
|
14
16
|
return true;
|
|
15
17
|
}
|
|
16
18
|
catch {
|
|
17
|
-
return
|
|
19
|
+
return `Expected a cronjob expression, but received: "${value}"`;
|
|
18
20
|
}
|
|
19
21
|
});
|
|
20
|
-
|
|
21
|
-
* Parses a cron expression.
|
|
22
|
-
*
|
|
23
|
-
* @param expression - Expression to parse.
|
|
24
|
-
* @returns A CronExpression class instance.
|
|
25
|
-
*/
|
|
26
|
-
export function parseCronExpression(expression) {
|
|
27
|
-
const ensureStringExpression = create(expression, CronExpressionStruct);
|
|
28
|
-
return parseExpression(ensureStringExpression);
|
|
29
|
-
}
|
|
30
|
-
export const CronjobSpecificationStruct = object({
|
|
22
|
+
const CronjobExpressionSpecificationStruct = object({
|
|
31
23
|
expression: CronExpressionStruct,
|
|
32
24
|
request: CronjobRpcRequestStruct,
|
|
33
25
|
});
|
|
26
|
+
const CronjobDurationSpecificationStruct = object({
|
|
27
|
+
duration: ISO8601DurationStruct,
|
|
28
|
+
request: CronjobRpcRequestStruct,
|
|
29
|
+
});
|
|
30
|
+
export const CronjobSpecificationStruct = selectiveUnion((value) => {
|
|
31
|
+
if (isObject(value) && hasProperty(value, 'duration')) {
|
|
32
|
+
return CronjobDurationSpecificationStruct;
|
|
33
|
+
}
|
|
34
|
+
return CronjobExpressionSpecificationStruct;
|
|
35
|
+
});
|
|
34
36
|
/**
|
|
35
37
|
* Check if the given value is a {@link CronjobSpecification} object.
|
|
36
38
|
*
|
package/dist/cronjob.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cronjob.mjs","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cronjob.mjs","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,4BAA4B;AAErD,OAAO,EACL,KAAK,EACL,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACP,8BAA8B;AAC/B,OAAO,EACL,WAAW,EACX,QAAQ,EACR,eAAe,EACf,mBAAmB,EACnB,oBAAoB,EACrB,wBAAwB;;;AAGzB,OAAO,EAAE,qBAAqB,EAAE,mBAAe;AAE/C,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC;IAC5C,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC;IACvC,EAAE,EAAE,QAAQ,CAAC,eAAe,CAAC;IAC7B,MAAM,EAAE,MAAM,EAAE;IAChB,MAAM,EAAE,QAAQ,CAAC,mBAAmB,CAAC;CACtC,CAAC,CAAC;AAIH,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CACxC,MAAM,EAAE,EACR,oBAAoB,EACpB,CAAC,KAAK,EAAE,EAAE;IACR,IAAI,CAAC;QACH,eAAe,CAAC,KAAK,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,iDAAiD,KAAK,GAAG,CAAC;IACnE,CAAC;AACH,CAAC,CACF,CAAC;AAIF,MAAM,oCAAoC,GAAG,MAAM,CAAC;IAClD,UAAU,EAAE,oBAAoB;IAChC,OAAO,EAAE,uBAAuB;CACjC,CAAC,CAAC;AAEH,MAAM,kCAAkC,GAAG,MAAM,CAAC;IAChD,QAAQ,EAAE,qBAAqB;IAC/B,OAAO,EAAE,uBAAuB;CACjC,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,0BAA0B,GAAG,cAAc,CAAC,CAAC,KAAK,EAAE,EAAE;IACjE,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;QACtD,OAAO,kCAAkC,CAAC;IAC5C,CAAC;IAED,OAAO,oCAAoC,CAAC;AAC9C,CAAC,CAAC,CAAC;AAIH;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAc;IACnD,IAAI,CAAC;QACH,MAAM,CAAC,KAAK,EAAE,0BAA0B,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,+BAA+B,GAAG,KAAK,CAClD,0BAA0B,CAC3B,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CAAC,KAAc;IACxD,IAAI,CAAC;QACH,MAAM,CAAC,KAAK,EAAE,+BAA+B,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC","sourcesContent":["import { selectiveUnion } from '@metamask/snaps-sdk';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n array,\n create,\n object,\n optional,\n refine,\n string,\n} from '@metamask/superstruct';\nimport {\n hasProperty,\n isObject,\n JsonRpcIdStruct,\n JsonRpcParamsStruct,\n JsonRpcVersionStruct,\n} from '@metamask/utils';\nimport { parseExpression } from 'cron-parser';\n\nimport { ISO8601DurationStruct } from './time';\n\nexport const CronjobRpcRequestStruct = object({\n jsonrpc: optional(JsonRpcVersionStruct),\n id: optional(JsonRpcIdStruct),\n method: string(),\n params: optional(JsonRpcParamsStruct),\n});\n\nexport type CronjobRpcRequest = Infer<typeof CronjobRpcRequestStruct>;\n\nexport const CronExpressionStruct = refine(\n string(),\n 'cronjob expression',\n (value) => {\n try {\n parseExpression(value);\n return true;\n } catch {\n return `Expected a cronjob expression, but received: \"${value}\"`;\n }\n },\n);\n\nexport type CronExpression = Infer<typeof CronExpressionStruct>;\n\nconst CronjobExpressionSpecificationStruct = object({\n expression: CronExpressionStruct,\n request: CronjobRpcRequestStruct,\n});\n\nconst CronjobDurationSpecificationStruct = object({\n duration: ISO8601DurationStruct,\n request: CronjobRpcRequestStruct,\n});\n\nexport const CronjobSpecificationStruct = selectiveUnion((value) => {\n if (isObject(value) && hasProperty(value, 'duration')) {\n return CronjobDurationSpecificationStruct;\n }\n\n return CronjobExpressionSpecificationStruct;\n});\n\nexport type CronjobSpecification = Infer<typeof CronjobSpecificationStruct>;\n\n/**\n * Check if the given value is a {@link CronjobSpecification} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link CronjobSpecification} object.\n */\nexport function isCronjobSpecification(value: unknown): boolean {\n try {\n create(value, CronjobSpecificationStruct);\n return true;\n } catch {\n return false;\n }\n}\n\nexport const CronjobSpecificationArrayStruct = array(\n CronjobSpecificationStruct,\n);\n\n/**\n * Check if the given value is an array of {@link CronjobSpecification} objects.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid array of {@link CronjobSpecification} objects.\n */\nexport function isCronjobSpecificationArray(value: unknown): boolean {\n try {\n create(value, CronjobSpecificationArrayStruct);\n return true;\n } catch {\n return false;\n }\n}\n"]}
|
package/dist/errors.cjs
CHANGED
|
@@ -1,16 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
-
};
|
|
8
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var _WrappedSnapError_error, _WrappedSnapError_message, _WrappedSnapError_stack;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
3
|
exports.unwrapError = exports.isWrappedSnapError = exports.isSerializedSnapError = exports.isSnapError = exports.WrappedSnapError = exports.SNAP_ERROR_WRAPPER_MESSAGE = exports.SNAP_ERROR_WRAPPER_CODE = void 0;
|
|
16
4
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
@@ -19,6 +7,9 @@ const utils_1 = require("@metamask/utils");
|
|
|
19
7
|
exports.SNAP_ERROR_WRAPPER_CODE = -31001;
|
|
20
8
|
exports.SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
|
|
21
9
|
class WrappedSnapError extends Error {
|
|
10
|
+
#error;
|
|
11
|
+
#message;
|
|
12
|
+
#stack;
|
|
22
13
|
/**
|
|
23
14
|
* Create a new `WrappedSnapError`.
|
|
24
15
|
*
|
|
@@ -27,12 +18,9 @@ class WrappedSnapError extends Error {
|
|
|
27
18
|
constructor(error) {
|
|
28
19
|
const message = (0, snaps_sdk_1.getErrorMessage)(error);
|
|
29
20
|
super(message);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
__classPrivateFieldSet(this, _WrappedSnapError_error, error, "f");
|
|
34
|
-
__classPrivateFieldSet(this, _WrappedSnapError_message, message, "f");
|
|
35
|
-
__classPrivateFieldSet(this, _WrappedSnapError_stack, (0, snaps_sdk_1.getErrorStack)(error), "f");
|
|
21
|
+
this.#error = error;
|
|
22
|
+
this.#message = message;
|
|
23
|
+
this.#stack = (0, snaps_sdk_1.getErrorStack)(error);
|
|
36
24
|
}
|
|
37
25
|
/**
|
|
38
26
|
* The error name.
|
|
@@ -48,7 +36,7 @@ class WrappedSnapError extends Error {
|
|
|
48
36
|
* @returns The error message.
|
|
49
37
|
*/
|
|
50
38
|
get message() {
|
|
51
|
-
return
|
|
39
|
+
return this.#message;
|
|
52
40
|
}
|
|
53
41
|
/**
|
|
54
42
|
* The error stack.
|
|
@@ -56,7 +44,7 @@ class WrappedSnapError extends Error {
|
|
|
56
44
|
* @returns The error stack.
|
|
57
45
|
*/
|
|
58
46
|
get stack() {
|
|
59
|
-
return
|
|
47
|
+
return this.#stack;
|
|
60
48
|
}
|
|
61
49
|
/**
|
|
62
50
|
* Convert the error to a JSON object.
|
|
@@ -64,9 +52,9 @@ class WrappedSnapError extends Error {
|
|
|
64
52
|
* @returns The JSON object.
|
|
65
53
|
*/
|
|
66
54
|
toJSON() {
|
|
67
|
-
const cause = isSnapError(
|
|
68
|
-
?
|
|
69
|
-
: (0, rpc_errors_1.serializeCause)(
|
|
55
|
+
const cause = isSnapError(this.#error)
|
|
56
|
+
? this.#error.serialize()
|
|
57
|
+
: (0, rpc_errors_1.serializeCause)(this.#error);
|
|
70
58
|
return {
|
|
71
59
|
code: exports.SNAP_ERROR_WRAPPER_CODE,
|
|
72
60
|
message: exports.SNAP_ERROR_WRAPPER_MESSAGE,
|
|
@@ -86,7 +74,6 @@ class WrappedSnapError extends Error {
|
|
|
86
74
|
}
|
|
87
75
|
}
|
|
88
76
|
exports.WrappedSnapError = WrappedSnapError;
|
|
89
|
-
_WrappedSnapError_error = new WeakMap(), _WrappedSnapError_message = new WeakMap(), _WrappedSnapError_stack = new WeakMap();
|
|
90
77
|
/**
|
|
91
78
|
* Check if an object is a `SnapError`.
|
|
92
79
|
*
|
|
@@ -161,6 +148,17 @@ function unwrapError(error) {
|
|
|
161
148
|
const { code, message, stack, data } = error.data.cause.data.cause;
|
|
162
149
|
return [getJsonRpcError(code, message, stack, data), true];
|
|
163
150
|
}
|
|
151
|
+
// If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.
|
|
152
|
+
if ((0, utils_1.isObject)(error.data.cause.data) &&
|
|
153
|
+
(0, utils_1.isObject)(error.data.cause.data.cause) &&
|
|
154
|
+
error.data.cause.message === error.data.cause.data.cause.message) {
|
|
155
|
+
const nestedCause = error.data.cause.data.cause;
|
|
156
|
+
const { code, message } = error.data.cause;
|
|
157
|
+
return [
|
|
158
|
+
getJsonRpcError(code, message, (0, snaps_sdk_1.getErrorStack)(nestedCause)),
|
|
159
|
+
false,
|
|
160
|
+
];
|
|
161
|
+
}
|
|
164
162
|
// Otherwise, we use the original JSON-RPC error.
|
|
165
163
|
const { code, message, stack, data } = error.data.cause;
|
|
166
164
|
return [getJsonRpcError(code, message, stack, data), false];
|
package/dist/errors.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qDAI8B;AAG9B,mDAK6B;AAE7B,2CAA2D;AAE9C,QAAA,uBAAuB,GAAG,CAAC,KAAK,CAAC;AACjC,QAAA,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAa,gBAAiB,SAAQ,KAAK;IAOzC;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAbR,0CAAgB;QAEhB,4CAAiB;QAEjB,0CAAgB;QAWvB,uBAAA,IAAI,2BAAU,KAAK,MAAA,CAAC;QACpB,uBAAA,IAAI,6BAAY,OAAO,MAAA,CAAC;QACxB,u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AAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;QAC9D,CAAC;QAED,sEAAsE;QACtE,WAAW;QACX,OAAO;YACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EACjC,IAAA,yBAAa,EAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAChC;YACD,KAAK;SACN,CAAC;IACJ,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IACrD,IAAI,IAAA,sBAAc,EAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;QAC7C,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,EACtB,IAAA,yBAAa,EAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC;AAnDD,kCAmDC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error);\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,qDAI8B;AAG9B,mDAK6B;AAE7B,2CAA2D;AAE9C,QAAA,uBAAuB,GAAG,CAAC,KAAK,CAAC;AACjC,QAAA,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAa,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAU;IAEhB,QAAQ,CAAS;IAEjB,MAAM,CAAU;IAEzB;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,IAAA,yBAAa,EAAC,KAAK,CAAC,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,kBAAkB,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,MAAM,CAA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AE,IAAA,yBAAa,EAAC,WAAW,CAAC,CAAC;oBAC1D,KAAK;iBACN,CAAC;YACJ,CAAC;YAED,iDAAiD;YACjD,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;YACxD,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;QAC9D,CAAC;QAED,sEAAsE;QACtE,WAAW;QACX,OAAO;YACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EACjC,IAAA,yBAAa,EAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAChC;YACD,KAAK;SACN,CAAC;IACJ,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IACrD,IAAI,IAAA,sBAAc,EAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;QAC7C,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,EACtB,IAAA,yBAAa,EAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC;AAjED,kCAiEC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error);\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.\n if (\n isObject(error.data.cause.data) &&\n isObject(error.data.cause.data.cause) &&\n error.data.cause.message === error.data.cause.data.cause.message\n ) {\n const nestedCause = error.data.cause.data.cause;\n const { code, message } = error.data.cause;\n return [\n getJsonRpcError(code, message, getErrorStack(nestedCause)),\n false,\n ];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"]}
|
package/dist/errors.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.cts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,IAAI,QAAQ,EAEzB,6BAA6B;AAC9B,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B;AAClE,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,4BAA4B;AAO1E,OAAO,KAAK,EAAE,IAAI,EAAE,YAAY,EAAE,wBAAwB;AAG1D,eAAO,MAAM,uBAAuB,SAAS,CAAC;AAC9C,eAAO,MAAM,0BAA0B,uBAAuB,CAAC;AAE/D,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAA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AAuB,SAAS,CAAC;AAC9C,eAAO,MAAM,0BAA0B,uBAAuB,CAAC;AAE/D,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,OAAO,uBAAuB,CAAC;IACrC,OAAO,EAAE,OAAO,0BAA0B,CAAC;IAC3C,IAAI,EAAE;QACJ,KAAK,EAAE,IAAI,CAAC;KACb,CAAC;CACH,CAAC;AAEF,qBAAa,gBAAiB,SAAQ,KAAK;;IAOzC;;;;OAIG;gBACS,KAAK,EAAE,OAAO;IAS1B;;;;OAIG;IACH,IAAI,IAAI,WAEP;IAED;;;;OAIG;IACH,IAAI,OAAO,WAEV;IAED;;;;OAIG;IACH,IAAI,KAAK,uBAER;IAED;;;;OAIG;IACH,MAAM,IAAI,0BAA0B;IAcpC;;;;;OAKG;IACH,SAAS;CAGV;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAW9D;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,YAAY,GAClB,KAAK,IAAI,mBAAmB,CAE9B;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,0BAA0B,CAMrC;AAuBD;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,OAAO,GACb,CAAC,KAAK,EAAE,QAAQ,CAAC,qBAAqB,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,CA+D9D"}
|
package/dist/errors.mjs
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _WrappedSnapError_error, _WrappedSnapError_message, _WrappedSnapError_stack;
|
|
13
1
|
import { errorCodes, JsonRpcError as RpcError, serializeCause } from "@metamask/rpc-errors";
|
|
14
2
|
import { getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE } from "@metamask/snaps-sdk";
|
|
15
3
|
import { isObject, isJsonRpcError } from "@metamask/utils";
|
|
16
4
|
export const SNAP_ERROR_WRAPPER_CODE = -31001;
|
|
17
5
|
export const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
|
|
18
6
|
export class WrappedSnapError extends Error {
|
|
7
|
+
#error;
|
|
8
|
+
#message;
|
|
9
|
+
#stack;
|
|
19
10
|
/**
|
|
20
11
|
* Create a new `WrappedSnapError`.
|
|
21
12
|
*
|
|
@@ -24,12 +15,9 @@ export class WrappedSnapError extends Error {
|
|
|
24
15
|
constructor(error) {
|
|
25
16
|
const message = getErrorMessage(error);
|
|
26
17
|
super(message);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
__classPrivateFieldSet(this, _WrappedSnapError_error, error, "f");
|
|
31
|
-
__classPrivateFieldSet(this, _WrappedSnapError_message, message, "f");
|
|
32
|
-
__classPrivateFieldSet(this, _WrappedSnapError_stack, getErrorStack(error), "f");
|
|
18
|
+
this.#error = error;
|
|
19
|
+
this.#message = message;
|
|
20
|
+
this.#stack = getErrorStack(error);
|
|
33
21
|
}
|
|
34
22
|
/**
|
|
35
23
|
* The error name.
|
|
@@ -45,7 +33,7 @@ export class WrappedSnapError extends Error {
|
|
|
45
33
|
* @returns The error message.
|
|
46
34
|
*/
|
|
47
35
|
get message() {
|
|
48
|
-
return
|
|
36
|
+
return this.#message;
|
|
49
37
|
}
|
|
50
38
|
/**
|
|
51
39
|
* The error stack.
|
|
@@ -53,7 +41,7 @@ export class WrappedSnapError extends Error {
|
|
|
53
41
|
* @returns The error stack.
|
|
54
42
|
*/
|
|
55
43
|
get stack() {
|
|
56
|
-
return
|
|
44
|
+
return this.#stack;
|
|
57
45
|
}
|
|
58
46
|
/**
|
|
59
47
|
* Convert the error to a JSON object.
|
|
@@ -61,9 +49,9 @@ export class WrappedSnapError extends Error {
|
|
|
61
49
|
* @returns The JSON object.
|
|
62
50
|
*/
|
|
63
51
|
toJSON() {
|
|
64
|
-
const cause = isSnapError(
|
|
65
|
-
?
|
|
66
|
-
: serializeCause(
|
|
52
|
+
const cause = isSnapError(this.#error)
|
|
53
|
+
? this.#error.serialize()
|
|
54
|
+
: serializeCause(this.#error);
|
|
67
55
|
return {
|
|
68
56
|
code: SNAP_ERROR_WRAPPER_CODE,
|
|
69
57
|
message: SNAP_ERROR_WRAPPER_MESSAGE,
|
|
@@ -82,7 +70,6 @@ export class WrappedSnapError extends Error {
|
|
|
82
70
|
return this.toJSON();
|
|
83
71
|
}
|
|
84
72
|
}
|
|
85
|
-
_WrappedSnapError_error = new WeakMap(), _WrappedSnapError_message = new WeakMap(), _WrappedSnapError_stack = new WeakMap();
|
|
86
73
|
/**
|
|
87
74
|
* Check if an object is a `SnapError`.
|
|
88
75
|
*
|
|
@@ -154,6 +141,17 @@ export function unwrapError(error) {
|
|
|
154
141
|
const { code, message, stack, data } = error.data.cause.data.cause;
|
|
155
142
|
return [getJsonRpcError(code, message, stack, data), true];
|
|
156
143
|
}
|
|
144
|
+
// If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.
|
|
145
|
+
if (isObject(error.data.cause.data) &&
|
|
146
|
+
isObject(error.data.cause.data.cause) &&
|
|
147
|
+
error.data.cause.message === error.data.cause.data.cause.message) {
|
|
148
|
+
const nestedCause = error.data.cause.data.cause;
|
|
149
|
+
const { code, message } = error.data.cause;
|
|
150
|
+
return [
|
|
151
|
+
getJsonRpcError(code, message, getErrorStack(nestedCause)),
|
|
152
|
+
false,
|
|
153
|
+
];
|
|
154
|
+
}
|
|
157
155
|
// Otherwise, we use the original JSON-RPC error.
|
|
158
156
|
const { code, message, stack, data } = error.data.cause;
|
|
159
157
|
return [getJsonRpcError(code, message, stack, data), false];
|
package/dist/errors.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,UAAU,EACV,YAAY,IAAI,QAAQ,EACxB,cAAc,EACf,6BAA6B;AAG9B,OAAO,EACL,eAAe,EACf,aAAa,EACb,eAAe,EACf,kBAAkB,EACnB,4BAA4B;AAE7B,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,wBAAwB;AAE3D,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,KAAK,CAAC;AAC9C,MAAM,CAAC,MAAM,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAOzC;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAbR,0CAAgB;QAEhB,4CAAiB;QAEjB,0CAAgB;QAWvB,uBAAA,IAAI,2BAAU,KAAK,MAAA,CAAC;QACpB,uBAAA,IAAI,6BAAY,OAAO,MAAA,CAAC;QACxB,uBAAA,IAAI,2BAAU,aAAa,CAAC,KAAK,CAAC,MAAA,CAAC;IACrC,CA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ACb,UAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,eAAe,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EACjC,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAChC;YACD,KAAK;SACN,CAAC;IACJ,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IACrD,IAAI,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;QAC7C,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,UAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,eAAe,CAAC,KAAK,CAAC,EACtB,aAAa,CAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error);\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,YAAY,IAAI,QAAQ,EACxB,cAAc,EACf,6BAA6B;AAG9B,OAAO,EACL,eAAe,EACf,aAAa,EACb,eAAe,EACf,kBAAkB,EACnB,4BAA4B;AAE7B,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,wBAAwB;AAE3D,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,KAAK,CAAC;AAC9C,MAAM,CAAC,MAAM,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAU;IAEhB,QAAQ,CAAS;IAEjB,MAAM,CAAU;IAEzB;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,kBAAkB,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC;YACpC,CAAC,CAAC,IAAI,CAAC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O,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;YACxD,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;QAC9D,CAAC;QAED,sEAAsE;QACtE,WAAW;QACX,OAAO;YACL,eAAe,CACb,UAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,eAAe,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EACjC,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAChC;YACD,KAAK;SACN,CAAC;IACJ,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IACrD,IAAI,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;QAC7C,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,UAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,eAAe,CAAC,KAAK,CAAC,EACtB,aAAa,CAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error);\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.\n if (\n isObject(error.data.cause.data) &&\n isObject(error.data.cause.data.cause) &&\n error.data.cause.message === error.data.cause.data.cause.message\n ) {\n const nestedCause = error.data.cause.data.cause;\n const { code, message } = error.data.cause;\n return [\n getJsonRpcError(code, message, getErrorStack(nestedCause)),\n false,\n ];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"]}
|
package/dist/eval-worker.cjs
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
4
|
-
// @ts-ignore - TypeScript complains about this being ESM in a CJS file, but
|
|
5
|
-
// `ses/lockdown` has a CommonJS entry point.
|
|
6
3
|
// eslint-disable-next-line import-x/no-unassigned-import
|
|
7
4
|
require("ses/lockdown");
|
|
5
|
+
const utils_1 = require("@metamask/utils");
|
|
8
6
|
const fs_1 = require("fs");
|
|
9
7
|
const handlers_1 = require("./handlers/index.cjs");
|
|
10
8
|
const mock_1 = require("./mock.cjs");
|
|
@@ -25,17 +23,46 @@ const compartment = new Compartment({
|
|
|
25
23
|
module: snapModule,
|
|
26
24
|
exports: snapModule.exports,
|
|
27
25
|
});
|
|
28
|
-
//
|
|
26
|
+
// Add self referential properties for compatibility with 3rd party libraries.
|
|
27
|
+
// This mirrors the implementation in the Snaps execution environment.
|
|
29
28
|
compartment.globalThis.self = compartment.globalThis;
|
|
30
29
|
compartment.globalThis.global = compartment.globalThis;
|
|
31
30
|
compartment.globalThis.window = compartment.globalThis;
|
|
32
31
|
compartment.evaluate((0, fs_1.readFileSync)(snapFilePath, 'utf8'));
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Check the exports of the Snap module to ensure they are valid, and exit the
|
|
34
|
+
* worker process.
|
|
35
|
+
*
|
|
36
|
+
* @param exports - The exports of the Snap module.
|
|
37
|
+
*/
|
|
38
|
+
function checkExports(exports) {
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
40
|
+
(0, utils_1.assert)(process.send, 'This script must be run as a child process.');
|
|
41
|
+
process.send({
|
|
42
|
+
type: 'snap-exports',
|
|
43
|
+
data: {
|
|
44
|
+
exports: Object.keys(exports),
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
const invalidExports = Object.keys(snapModule.exports).filter((snapExport) => !handlers_1.SNAP_EXPORT_NAMES.includes(snapExport));
|
|
48
|
+
if (invalidExports.length > 0) {
|
|
49
|
+
// eslint-disable-next-line no-console
|
|
50
|
+
console.warn(`Invalid snap exports detected:\n${invalidExports.join('\n')}`);
|
|
51
|
+
}
|
|
52
|
+
// To ensure the worker exits we explicitly call exit here. If we didn't, the
|
|
53
|
+
// worker would wait for timers set during `Compartment` eval.
|
|
54
|
+
process.exit(0);
|
|
55
|
+
}
|
|
56
|
+
if (snapModule.exports instanceof Promise) {
|
|
57
|
+
// The Snap may use async logic (e.g., when loading WASM), so we need to
|
|
58
|
+
// handle that case.
|
|
59
|
+
snapModule.exports.then(checkExports).catch((error) => {
|
|
60
|
+
// eslint-disable-next-line no-console
|
|
61
|
+
console.error('Error loading Snap module:', error);
|
|
62
|
+
process.exit(1);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
checkExports(snapModule.exports);
|
|
37
67
|
}
|
|
38
|
-
// To ensure the worker exits we explicitly call exit here
|
|
39
|
-
// If we didn't the eval would wait for timers set during Compartment eval
|
|
40
|
-
process.exit(0);
|
|
41
68
|
//# sourceMappingURL=eval-worker.cjs.map
|
package/dist/eval-worker.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eval-worker.cjs","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":";;AAAA,
|
|
1
|
+
{"version":3,"file":"eval-worker.cjs","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":";;AAAA,yDAAyD;AACzD,wBAAsB;AAEtB,2CAAyC;AACzC,2BAAkC;AAGlC,mDAA+C;AAC/C,qCAAgD;AAEhD,QAAQ,CAAC;IACP,WAAW,EAAE,QAAQ;IACrB,cAAc,EAAE,SAAS;IACzB,cAAc,EAAE,QAAQ;IACxB,YAAY,EAAE,QAAQ;IAEtB,2EAA2E;IAC3E,yEAAyE;IACzE,kCAAkC;IAClC,YAAY,EAAE,QAAQ;CACvB,CAAC,CAAC;AAEH,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAErC,MAAM,UAAU,GAAsB,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AAEtD,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC;IAClC,GAAG,IAAA,6BAAsB,GAAE;IAC3B,MAAM,EAAE,UAAU;IAClB,OAAO,EAAE,UAAU,CAAC,OAAO;CAC5B,CAAC,CAAC;AAEH,8EAA8E;AAC9E,sEAAsE;AACtE,WAAW,CAAC,UAAU,CAAC,IAAI,GAAG,WAAW,CAAC,UAAU,CAAC;AACrD,WAAW,CAAC,UAAU,CAAC,MAAM,GAAG,WAAW,CAAC,UAAU,CAAC;AACvD,WAAW,CAAC,UAAU,CAAC,MAAM,GAAG,WAAW,CAAC,UAAU,CAAC;AAEvD,WAAW,CAAC,QAAQ,CAAC,IAAA,iBAAY,EAAC,YAAY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assert } from '@metamask/utils';\nimport { readFileSync } from 'fs';\n\nimport type { HandlerType } from './handlers';\nimport { SNAP_EXPORT_NAMES } from './handlers';\nimport { generateMockEndowments } from './mock';\n\nlockdown({\n errorTaming: 'unsafe',\n stackFiltering: 'verbose',\n overrideTaming: 'severe',\n localeTaming: 'unsafe',\n\n // We disable domain taming, because it does not work in certain cases when\n // running tests. This is unlikely to be a problem in production, because\n // Node.js domains are deprecated.\n domainTaming: 'unsafe',\n});\n\nconst snapFilePath = process.argv[2];\n\nconst snapModule: { exports?: any } = { exports: {} };\n\nconst compartment = new Compartment({\n ...generateMockEndowments(),\n module: snapModule,\n exports: snapModule.exports,\n});\n\n// Add self referential properties for compatibility with 3rd party libraries.\n// This mirrors the implementation in the Snaps execution environment.\ncompartment.globalThis.self = compartment.globalThis;\ncompartment.globalThis.global = compartment.globalThis;\ncompartment.globalThis.window = compartment.globalThis;\n\ncompartment.evaluate(readFileSync(snapFilePath, 'utf8'));\n\n/**\n * Check the exports of the Snap module to ensure they are valid, and exit the\n * worker process.\n *\n * @param exports - The exports of the Snap module.\n */\nfunction checkExports(exports: any) {\n // eslint-disable-next-line @typescript-eslint/unbound-method\n assert(process.send, 'This script must be run as a child process.');\n\n process.send({\n type: 'snap-exports',\n data: {\n exports: Object.keys(exports),\n },\n });\n\n const invalidExports = Object.keys(snapModule.exports).filter(\n (snapExport) => !SNAP_EXPORT_NAMES.includes(snapExport as HandlerType),\n );\n\n if (invalidExports.length > 0) {\n // eslint-disable-next-line no-console\n console.warn(\n `Invalid snap exports detected:\\n${invalidExports.join('\\n')}`,\n );\n }\n\n // To ensure the worker exits we explicitly call exit here. If we didn't, the\n // worker would wait for timers set during `Compartment` eval.\n process.exit(0);\n}\n\nif (snapModule.exports instanceof Promise) {\n // The Snap may use async logic (e.g., when loading WASM), so we need to\n // handle that case.\n snapModule.exports.then(checkExports).catch((error: Error) => {\n // eslint-disable-next-line no-console\n console.error('Error loading Snap module:', error);\n process.exit(1);\n });\n} else {\n checkExports(snapModule.exports);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eval-worker.d.cts","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"eval-worker.d.cts","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":"AACA,sBAAsB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eval-worker.d.mts","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"eval-worker.d.mts","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":"AACA,sBAAsB"}
|
package/dist/eval-worker.mjs
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
-
// @ts-ignore - TypeScript complains about this being ESM in a CJS file, but
|
|
3
|
-
// `ses/lockdown` has a CommonJS entry point.
|
|
4
1
|
// eslint-disable-next-line import-x/no-unassigned-import
|
|
5
2
|
import "ses/lockdown";
|
|
3
|
+
import { assert } from "@metamask/utils";
|
|
6
4
|
import { readFileSync } from "fs";
|
|
7
5
|
import { SNAP_EXPORT_NAMES } from "./handlers/index.mjs";
|
|
8
6
|
import { generateMockEndowments } from "./mock.mjs";
|
|
@@ -23,17 +21,46 @@ const compartment = new Compartment({
|
|
|
23
21
|
module: snapModule,
|
|
24
22
|
exports: snapModule.exports,
|
|
25
23
|
});
|
|
26
|
-
//
|
|
24
|
+
// Add self referential properties for compatibility with 3rd party libraries.
|
|
25
|
+
// This mirrors the implementation in the Snaps execution environment.
|
|
27
26
|
compartment.globalThis.self = compartment.globalThis;
|
|
28
27
|
compartment.globalThis.global = compartment.globalThis;
|
|
29
28
|
compartment.globalThis.window = compartment.globalThis;
|
|
30
29
|
compartment.evaluate(readFileSync(snapFilePath, 'utf8'));
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Check the exports of the Snap module to ensure they are valid, and exit the
|
|
32
|
+
* worker process.
|
|
33
|
+
*
|
|
34
|
+
* @param exports - The exports of the Snap module.
|
|
35
|
+
*/
|
|
36
|
+
function checkExports(exports) {
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
38
|
+
assert(process.send, 'This script must be run as a child process.');
|
|
39
|
+
process.send({
|
|
40
|
+
type: 'snap-exports',
|
|
41
|
+
data: {
|
|
42
|
+
exports: Object.keys(exports),
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const invalidExports = Object.keys(snapModule.exports).filter((snapExport) => !SNAP_EXPORT_NAMES.includes(snapExport));
|
|
46
|
+
if (invalidExports.length > 0) {
|
|
47
|
+
// eslint-disable-next-line no-console
|
|
48
|
+
console.warn(`Invalid snap exports detected:\n${invalidExports.join('\n')}`);
|
|
49
|
+
}
|
|
50
|
+
// To ensure the worker exits we explicitly call exit here. If we didn't, the
|
|
51
|
+
// worker would wait for timers set during `Compartment` eval.
|
|
52
|
+
process.exit(0);
|
|
53
|
+
}
|
|
54
|
+
if (snapModule.exports instanceof Promise) {
|
|
55
|
+
// The Snap may use async logic (e.g., when loading WASM), so we need to
|
|
56
|
+
// handle that case.
|
|
57
|
+
snapModule.exports.then(checkExports).catch((error) => {
|
|
58
|
+
// eslint-disable-next-line no-console
|
|
59
|
+
console.error('Error loading Snap module:', error);
|
|
60
|
+
process.exit(1);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
checkExports(snapModule.exports);
|
|
35
65
|
}
|
|
36
|
-
// To ensure the worker exits we explicitly call exit here
|
|
37
|
-
// If we didn't the eval would wait for timers set during Compartment eval
|
|
38
|
-
process.exit(0);
|
|
39
66
|
//# sourceMappingURL=eval-worker.mjs.map
|
package/dist/eval-worker.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eval-worker.mjs","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"eval-worker.mjs","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,sBAAsB;AAEtB,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,YAAY,EAAE,WAAW;AAGlC,OAAO,EAAE,iBAAiB,EAAE,6BAAmB;AAC/C,OAAO,EAAE,sBAAsB,EAAE,mBAAe;AAEhD,QAAQ,CAAC;IACP,WAAW,EAAE,QAAQ;IACrB,cAAc,EAAE,SAAS;IACzB,cAAc,EAAE,QAAQ;IACxB,YAAY,EAAE,QAAQ;IAEtB,2EAA2E;IAC3E,yEAAyE;IACzE,kCAAkC;IAClC,YAAY,EAAE,QAAQ;CACvB,CAAC,CAAC;AAEH,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAErC,MAAM,UAAU,GAAsB,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AAEtD,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC;IAClC,GAAG,sBAAsB,EAAE;IAC3B,MAAM,EAAE,UAAU;IAClB,OAAO,EAAE,UAAU,CAAC,OAAO;CAC5B,CAAC,CAAC;AAEH,8EAA8E;AAC9E,sEAAsE;AACtE,WAAW,CAAC,UAAU,CAAC,IAAI,GAAG,WAAW,CAAC,UAAU,CAAC;AACrD,WAAW,CAAC,UAAU,CAAC,MAAM,GAAG,WAAW,CAAC,UAAU,CAAC;AACvD,WAAW,CAAC,UAAU,CAAC,MAAM,GAAG,WAAW,CAAC,UAAU,CAAC;AAEvD,WAAW,CAAC,QAAQ,CAAC,YAAY,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC;AAEzD;;;;;GAKG;AACH,SAAS,YAAY,CAAC,OAAY;IAChC,6DAA6D;IAC7D,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,6CAA6C,CAAC,CAAC;IAEpE,OAAO,CAAC,IAAI,CAAC;QACX,IAAI,EAAE,cAAc;QACpB,IAAI,EAAE;YACJ,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;SAC9B;KACF,CAAC,CAAC;IAEH,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,CAC3D,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,iBAAiB,CAAC,QAAQ,CAAC,UAAyB,CAAC,CACvE,CAAC;IAEF,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC9B,sCAAsC;QACtC,OAAO,CAAC,IAAI,CACV,mCAAmC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAC/D,CAAC;IACJ,CAAC;IAED,6EAA6E;IAC7E,8DAA8D;IAC9D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC;AAED,IAAI,UAAU,CAAC,OAAO,YAAY,OAAO,EAAE,CAAC;IAC1C,wEAAwE;IACxE,oBAAoB;IACpB,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,KAAK,CAAC,CAAC,KAAY,EAAE,EAAE;QAC3D,sCAAsC;QACtC,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,KAAK,CAAC,CAAC;QACnD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC;KAAM,CAAC;IACN,YAAY,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC","sourcesContent":["// eslint-disable-next-line import-x/no-unassigned-import\nimport 'ses/lockdown';\n\nimport { assert } from '@metamask/utils';\nimport { readFileSync } from 'fs';\n\nimport type { HandlerType } from './handlers';\nimport { SNAP_EXPORT_NAMES } from './handlers';\nimport { generateMockEndowments } from './mock';\n\nlockdown({\n errorTaming: 'unsafe',\n stackFiltering: 'verbose',\n overrideTaming: 'severe',\n localeTaming: 'unsafe',\n\n // We disable domain taming, because it does not work in certain cases when\n // running tests. This is unlikely to be a problem in production, because\n // Node.js domains are deprecated.\n domainTaming: 'unsafe',\n});\n\nconst snapFilePath = process.argv[2];\n\nconst snapModule: { exports?: any } = { exports: {} };\n\nconst compartment = new Compartment({\n ...generateMockEndowments(),\n module: snapModule,\n exports: snapModule.exports,\n});\n\n// Add self referential properties for compatibility with 3rd party libraries.\n// This mirrors the implementation in the Snaps execution environment.\ncompartment.globalThis.self = compartment.globalThis;\ncompartment.globalThis.global = compartment.globalThis;\ncompartment.globalThis.window = compartment.globalThis;\n\ncompartment.evaluate(readFileSync(snapFilePath, 'utf8'));\n\n/**\n * Check the exports of the Snap module to ensure they are valid, and exit the\n * worker process.\n *\n * @param exports - The exports of the Snap module.\n */\nfunction checkExports(exports: any) {\n // eslint-disable-next-line @typescript-eslint/unbound-method\n assert(process.send, 'This script must be run as a child process.');\n\n process.send({\n type: 'snap-exports',\n data: {\n exports: Object.keys(exports),\n },\n });\n\n const invalidExports = Object.keys(snapModule.exports).filter(\n (snapExport) => !SNAP_EXPORT_NAMES.includes(snapExport as HandlerType),\n );\n\n if (invalidExports.length > 0) {\n // eslint-disable-next-line no-console\n console.warn(\n `Invalid snap exports detected:\\n${invalidExports.join('\\n')}`,\n );\n }\n\n // To ensure the worker exits we explicitly call exit here. If we didn't, the\n // worker would wait for timers set during `Compartment` eval.\n process.exit(0);\n}\n\nif (snapModule.exports instanceof Promise) {\n // The Snap may use async logic (e.g., when loading WASM), so we need to\n // handle that case.\n snapModule.exports.then(checkExports).catch((error: Error) => {\n // eslint-disable-next-line no-console\n console.error('Error loading Snap module:', error);\n process.exit(1);\n });\n} else {\n checkExports(snapModule.exports);\n}\n"]}
|
package/dist/eval.cjs
CHANGED
|
@@ -6,6 +6,7 @@ const child_process_1 = require("child_process");
|
|
|
6
6
|
const path_1 = require("path");
|
|
7
7
|
const fs_1 = require("./fs.cjs");
|
|
8
8
|
class SnapEvalError extends Error {
|
|
9
|
+
output;
|
|
9
10
|
constructor(message, output) {
|
|
10
11
|
super(message);
|
|
11
12
|
this.name = 'SnapEvalError';
|
|
@@ -30,8 +31,13 @@ async function evalBundle(bundlePath) {
|
|
|
30
31
|
});
|
|
31
32
|
let stdout = '';
|
|
32
33
|
let stderr = '';
|
|
34
|
+
let exports = [];
|
|
33
35
|
(0, utils_1.assert)(worker.stdout, '`stdout` should be defined.');
|
|
34
36
|
(0, utils_1.assert)(worker.stderr, '`stderr` should be defined.');
|
|
37
|
+
worker.on('message', (message) => {
|
|
38
|
+
(0, utils_1.assert)(message.type === 'snap-exports', `Received unexpected message with type "${message.type}".`);
|
|
39
|
+
exports = message.data.exports;
|
|
40
|
+
});
|
|
35
41
|
worker.stdout.on('data', (data) => {
|
|
36
42
|
stdout += data.toString();
|
|
37
43
|
});
|
|
@@ -42,6 +48,7 @@ async function evalBundle(bundlePath) {
|
|
|
42
48
|
const output = {
|
|
43
49
|
stdout,
|
|
44
50
|
stderr,
|
|
51
|
+
exports,
|
|
45
52
|
};
|
|
46
53
|
if (exitCode === 0) {
|
|
47
54
|
return resolve(output);
|