@metamask/snaps-utils 9.4.0 → 10.1.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 +28 -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.cjs +1 -0
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.mjs +1 -0
- package/dist/eval.mjs.map +1 -1
- package/dist/handlers/exports.cjs +14 -0
- package/dist/handlers/exports.cjs.map +1 -1
- package/dist/handlers/exports.d.cts +11 -1
- package/dist/handlers/exports.d.cts.map +1 -1
- package/dist/handlers/exports.d.mts +11 -1
- package/dist/handlers/exports.d.mts.map +1 -1
- package/dist/handlers/exports.mjs +14 -0
- package/dist/handlers/exports.mjs.map +1 -1
- package/dist/handlers/types.cjs +2 -0
- package/dist/handlers/types.cjs.map +1 -1
- package/dist/handlers/types.d.cts +3 -1
- package/dist/handlers/types.d.cts.map +1 -1
- package/dist/handlers/types.d.mts +3 -1
- package/dist/handlers/types.d.mts.map +1 -1
- package/dist/handlers/types.mjs +2 -0
- package/dist/handlers/types.mjs.map +1 -1
- package/dist/localization.d.cts +1 -1
- package/dist/localization.d.mts +1 -1
- package/dist/manifest/validation.d.cts +3 -3
- package/dist/manifest/validation.d.cts.map +1 -1
- package/dist/manifest/validation.d.mts +3 -3
- package/dist/manifest/validation.d.mts.map +1 -1
- package/dist/manifest/validator.cjs +8 -21
- package/dist/manifest/validator.cjs.map +1 -1
- package/dist/manifest/validator.mjs +8 -21
- package/dist/manifest/validator.mjs.map +1 -1
- package/dist/manifest/validators/unused-exports.cjs +12 -2
- package/dist/manifest/validators/unused-exports.cjs.map +1 -1
- package/dist/manifest/validators/unused-exports.d.cts.map +1 -1
- package/dist/manifest/validators/unused-exports.d.mts.map +1 -1
- package/dist/manifest/validators/unused-exports.mjs +12 -2
- package/dist/manifest/validators/unused-exports.mjs.map +1 -1
- package/dist/time.cjs.map +1 -1
- package/dist/time.d.cts.map +1 -1
- package/dist/time.d.mts.map +1 -1
- package/dist/time.mjs.map +1 -1
- package/dist/types.cjs +7 -3
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +2 -2
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +2 -2
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +8 -4
- package/dist/types.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 +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [10.1.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add support for the `onWebSocketEvent` handler ([#3450](https://github.com/MetaMask/snaps/pull/3450), [#3459](https://github.com/MetaMask/snaps/pull/3459))
|
|
15
|
+
- Add support for the `onStart` handler ([#3455](https://github.com/MetaMask/snaps/pull/3455))
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- Fix unused permission detection for endowments with multiple handlers ([#3452](https://github.com/MetaMask/snaps/pull/3452))
|
|
20
|
+
|
|
21
|
+
## [10.0.0]
|
|
22
|
+
|
|
23
|
+
### Added
|
|
24
|
+
|
|
25
|
+
- Support scheduling cronjobs with an ISO 8601 duration ([#3421](https://github.com/MetaMask/snaps/pull/3421))
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- **BREAKING:** Drop support for Node.js 18 and 21 ([#3447](https://github.com/MetaMask/snaps/pull/3447))
|
|
30
|
+
|
|
31
|
+
### Fixed
|
|
32
|
+
|
|
33
|
+
- Unwrap double-wrapped JSON-RPC errors ([#3432](https://github.com/MetaMask/snaps/pull/3432))
|
|
34
|
+
|
|
10
35
|
## [9.4.0]
|
|
11
36
|
|
|
12
37
|
### Added
|
|
@@ -623,7 +648,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
623
648
|
- The version of the package no longer needs to match the version of all other
|
|
624
649
|
MetaMask Snaps packages.
|
|
625
650
|
|
|
626
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@
|
|
651
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@10.1.0...HEAD
|
|
652
|
+
[10.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@10.0.0...@metamask/snaps-utils@10.1.0
|
|
653
|
+
[10.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@9.4.0...@metamask/snaps-utils@10.0.0
|
|
627
654
|
[9.4.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@9.3.0...@metamask/snaps-utils@9.4.0
|
|
628
655
|
[9.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@9.2.2...@metamask/snaps-utils@9.3.0
|
|
629
656
|
[9.2.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@9.2.1...@metamask/snaps-utils@9.2.2
|
package/dist/cronjob.cjs
CHANGED
|
@@ -1,39 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isCronjobSpecificationArray = exports.CronjobSpecificationArrayStruct = exports.isCronjobSpecification = exports.CronjobSpecificationStruct = exports.
|
|
3
|
+
exports.isCronjobSpecificationArray = exports.CronjobSpecificationArrayStruct = exports.isCronjobSpecification = exports.CronjobSpecificationStruct = exports.CronExpressionStruct = exports.CronjobRpcRequestStruct = void 0;
|
|
4
|
+
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
4
5
|
const superstruct_1 = require("@metamask/superstruct");
|
|
5
6
|
const utils_1 = require("@metamask/utils");
|
|
6
7
|
const cron_parser_1 = require("cron-parser");
|
|
8
|
+
const time_1 = require("./time.cjs");
|
|
7
9
|
exports.CronjobRpcRequestStruct = (0, superstruct_1.object)({
|
|
8
10
|
jsonrpc: (0, superstruct_1.optional)(utils_1.JsonRpcVersionStruct),
|
|
9
11
|
id: (0, superstruct_1.optional)(utils_1.JsonRpcIdStruct),
|
|
10
12
|
method: (0, superstruct_1.string)(),
|
|
11
13
|
params: (0, superstruct_1.optional)(utils_1.JsonRpcParamsStruct),
|
|
12
14
|
});
|
|
13
|
-
exports.CronExpressionStruct = (0, superstruct_1.refine)((0, superstruct_1.string)(), '
|
|
15
|
+
exports.CronExpressionStruct = (0, superstruct_1.refine)((0, superstruct_1.string)(), 'cronjob expression', (value) => {
|
|
14
16
|
try {
|
|
15
17
|
(0, cron_parser_1.parseExpression)(value);
|
|
16
18
|
return true;
|
|
17
19
|
}
|
|
18
20
|
catch {
|
|
19
|
-
return
|
|
21
|
+
return `Expected a cronjob expression, but received: "${value}"`;
|
|
20
22
|
}
|
|
21
23
|
});
|
|
22
|
-
|
|
23
|
-
* Parses a cron expression.
|
|
24
|
-
*
|
|
25
|
-
* @param expression - Expression to parse.
|
|
26
|
-
* @returns A CronExpression class instance.
|
|
27
|
-
*/
|
|
28
|
-
function parseCronExpression(expression) {
|
|
29
|
-
const ensureStringExpression = (0, superstruct_1.create)(expression, exports.CronExpressionStruct);
|
|
30
|
-
return (0, cron_parser_1.parseExpression)(ensureStringExpression);
|
|
31
|
-
}
|
|
32
|
-
exports.parseCronExpression = parseCronExpression;
|
|
33
|
-
exports.CronjobSpecificationStruct = (0, superstruct_1.object)({
|
|
24
|
+
const CronjobExpressionSpecificationStruct = (0, superstruct_1.object)({
|
|
34
25
|
expression: exports.CronExpressionStruct,
|
|
35
26
|
request: exports.CronjobRpcRequestStruct,
|
|
36
27
|
});
|
|
28
|
+
const CronjobDurationSpecificationStruct = (0, superstruct_1.object)({
|
|
29
|
+
duration: time_1.ISO8601DurationStruct,
|
|
30
|
+
request: exports.CronjobRpcRequestStruct,
|
|
31
|
+
});
|
|
32
|
+
exports.CronjobSpecificationStruct = (0, snaps_sdk_1.selectiveUnion)((value) => {
|
|
33
|
+
if ((0, utils_1.isObject)(value) && (0, utils_1.hasProperty)(value, 'duration')) {
|
|
34
|
+
return CronjobDurationSpecificationStruct;
|
|
35
|
+
}
|
|
36
|
+
return CronjobExpressionSpecificationStruct;
|
|
37
|
+
});
|
|
37
38
|
/**
|
|
38
39
|
* Check if the given value is a {@link CronjobSpecification} object.
|
|
39
40
|
*
|
package/dist/cronjob.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cronjob.cjs","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"cronjob.cjs","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":";;;AAAA,mDAAqD;AAErD,uDAO+B;AAC/B,2CAMyB;AACzB,6CAA8C;AAE9C,qCAA+C;AAElC,QAAA,uBAAuB,GAAG,IAAA,oBAAM,EAAC;IAC5C,OAAO,EAAE,IAAA,sBAAQ,EAAC,4BAAoB,CAAC;IACvC,EAAE,EAAE,IAAA,sBAAQ,EAAC,uBAAe,CAAC;IAC7B,MAAM,EAAE,IAAA,oBAAM,GAAE;IAChB,MAAM,EAAE,IAAA,sBAAQ,EAAC,2BAAmB,CAAC;CACtC,CAAC,CAAC;AAIU,Q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CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAPD,kEAOC","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/cronjob.d.cts
CHANGED
|
@@ -3,45 +3,33 @@ export declare const CronjobRpcRequestStruct: import("@metamask/superstruct").St
|
|
|
3
3
|
method: string;
|
|
4
4
|
jsonrpc?: "2.0" | undefined;
|
|
5
5
|
id?: string | number | null | undefined;
|
|
6
|
-
params?: Record<string, import("@metamask/
|
|
6
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
7
7
|
}, {
|
|
8
8
|
jsonrpc: import("@metamask/superstruct").Struct<"2.0" | undefined, "2.0">;
|
|
9
9
|
id: import("@metamask/superstruct").Struct<string | number | null | undefined, null>;
|
|
10
10
|
method: import("@metamask/superstruct").Struct<string, null>;
|
|
11
|
-
params: import("@metamask/superstruct").Struct<Record<string, import("@metamask/
|
|
11
|
+
params: import("@metamask/superstruct").Struct<Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined, null>;
|
|
12
12
|
}>;
|
|
13
13
|
export type CronjobRpcRequest = Infer<typeof CronjobRpcRequestStruct>;
|
|
14
14
|
export declare const CronExpressionStruct: import("@metamask/superstruct").Struct<string, null>;
|
|
15
15
|
export type CronExpression = Infer<typeof CronExpressionStruct>;
|
|
16
|
-
/**
|
|
17
|
-
* Parses a cron expression.
|
|
18
|
-
*
|
|
19
|
-
* @param expression - Expression to parse.
|
|
20
|
-
* @returns A CronExpression class instance.
|
|
21
|
-
*/
|
|
22
|
-
export declare function parseCronExpression(expression: string | object): import("cron-parser").CronExpression<false>;
|
|
23
16
|
export declare const CronjobSpecificationStruct: import("@metamask/superstruct").Struct<{
|
|
24
17
|
expression: string;
|
|
25
18
|
request: {
|
|
26
19
|
method: string;
|
|
27
20
|
jsonrpc?: "2.0" | undefined;
|
|
28
21
|
id?: string | number | null | undefined;
|
|
29
|
-
params?: Record<string, import("@metamask/
|
|
22
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
30
23
|
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
request: import("@metamask/superstruct").Struct<{
|
|
24
|
+
} | {
|
|
25
|
+
request: {
|
|
34
26
|
method: string;
|
|
35
27
|
jsonrpc?: "2.0" | undefined;
|
|
36
28
|
id?: string | number | null | undefined;
|
|
37
|
-
params?: Record<string, import("@metamask/
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
method: import("@metamask/superstruct").Struct<string, null>;
|
|
42
|
-
params: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined, null>;
|
|
43
|
-
}>;
|
|
44
|
-
}>;
|
|
29
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
30
|
+
};
|
|
31
|
+
duration: string;
|
|
32
|
+
}, null>;
|
|
45
33
|
export type CronjobSpecification = Infer<typeof CronjobSpecificationStruct>;
|
|
46
34
|
/**
|
|
47
35
|
* Check if the given value is a {@link CronjobSpecification} object.
|
|
@@ -50,36 +38,39 @@ export type CronjobSpecification = Infer<typeof CronjobSpecificationStruct>;
|
|
|
50
38
|
* @returns Whether the value is a valid {@link CronjobSpecification} object.
|
|
51
39
|
*/
|
|
52
40
|
export declare function isCronjobSpecification(value: unknown): boolean;
|
|
53
|
-
export declare const CronjobSpecificationArrayStruct: import("@metamask/superstruct").Struct<{
|
|
41
|
+
export declare const CronjobSpecificationArrayStruct: import("@metamask/superstruct").Struct<({
|
|
54
42
|
expression: string;
|
|
55
43
|
request: {
|
|
56
44
|
method: string;
|
|
57
45
|
jsonrpc?: "2.0" | undefined;
|
|
58
46
|
id?: string | number | null | undefined;
|
|
59
|
-
params?: Record<string, import("@metamask/
|
|
47
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
60
48
|
};
|
|
61
|
-
}
|
|
49
|
+
} | {
|
|
50
|
+
request: {
|
|
51
|
+
method: string;
|
|
52
|
+
jsonrpc?: "2.0" | undefined;
|
|
53
|
+
id?: string | number | null | undefined;
|
|
54
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
55
|
+
};
|
|
56
|
+
duration: string;
|
|
57
|
+
})[], import("@metamask/superstruct").Struct<{
|
|
62
58
|
expression: string;
|
|
63
59
|
request: {
|
|
64
60
|
method: string;
|
|
65
61
|
jsonrpc?: "2.0" | undefined;
|
|
66
62
|
id?: string | number | null | undefined;
|
|
67
|
-
params?: Record<string, import("@metamask/
|
|
63
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
68
64
|
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
request: import("@metamask/superstruct").Struct<{
|
|
65
|
+
} | {
|
|
66
|
+
request: {
|
|
72
67
|
method: string;
|
|
73
68
|
jsonrpc?: "2.0" | undefined;
|
|
74
69
|
id?: string | number | null | undefined;
|
|
75
|
-
params?: Record<string, import("@metamask/
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
method: import("@metamask/superstruct").Struct<string, null>;
|
|
80
|
-
params: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined, null>;
|
|
81
|
-
}>;
|
|
82
|
-
}>>;
|
|
70
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
71
|
+
};
|
|
72
|
+
duration: string;
|
|
73
|
+
}, null>>;
|
|
83
74
|
/**
|
|
84
75
|
* Check if the given value is an array of {@link CronjobSpecification} objects.
|
|
85
76
|
*
|
package/dist/cronjob.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cronjob.d.cts","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cronjob.d.cts","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAoBnD,eAAO,MAAM,uBAAuB;;;;;;;;;;EAKlC,CAAC;AAEH,MAAM,MAAM,iBAAiB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAEtE,eAAO,MAAM,oBAAoB,sDAWhC,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAYhE,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;;;;QAMrC,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,KAAK,CAAC,OAAO,0BAA0B,CAAC,CAAC;AAE5E;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAO9D;AAED,eAAO,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAE3C,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAOnE"}
|
package/dist/cronjob.d.mts
CHANGED
|
@@ -3,45 +3,33 @@ export declare const CronjobRpcRequestStruct: import("@metamask/superstruct").St
|
|
|
3
3
|
method: string;
|
|
4
4
|
jsonrpc?: "2.0" | undefined;
|
|
5
5
|
id?: string | number | null | undefined;
|
|
6
|
-
params?: Record<string, import("@metamask/
|
|
6
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
7
7
|
}, {
|
|
8
8
|
jsonrpc: import("@metamask/superstruct").Struct<"2.0" | undefined, "2.0">;
|
|
9
9
|
id: import("@metamask/superstruct").Struct<string | number | null | undefined, null>;
|
|
10
10
|
method: import("@metamask/superstruct").Struct<string, null>;
|
|
11
|
-
params: import("@metamask/superstruct").Struct<Record<string, import("@metamask/
|
|
11
|
+
params: import("@metamask/superstruct").Struct<Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined, null>;
|
|
12
12
|
}>;
|
|
13
13
|
export type CronjobRpcRequest = Infer<typeof CronjobRpcRequestStruct>;
|
|
14
14
|
export declare const CronExpressionStruct: import("@metamask/superstruct").Struct<string, null>;
|
|
15
15
|
export type CronExpression = Infer<typeof CronExpressionStruct>;
|
|
16
|
-
/**
|
|
17
|
-
* Parses a cron expression.
|
|
18
|
-
*
|
|
19
|
-
* @param expression - Expression to parse.
|
|
20
|
-
* @returns A CronExpression class instance.
|
|
21
|
-
*/
|
|
22
|
-
export declare function parseCronExpression(expression: string | object): import("cron-parser").CronExpression<false>;
|
|
23
16
|
export declare const CronjobSpecificationStruct: import("@metamask/superstruct").Struct<{
|
|
24
17
|
expression: string;
|
|
25
18
|
request: {
|
|
26
19
|
method: string;
|
|
27
20
|
jsonrpc?: "2.0" | undefined;
|
|
28
21
|
id?: string | number | null | undefined;
|
|
29
|
-
params?: Record<string, import("@metamask/
|
|
22
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
30
23
|
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
request: import("@metamask/superstruct").Struct<{
|
|
24
|
+
} | {
|
|
25
|
+
request: {
|
|
34
26
|
method: string;
|
|
35
27
|
jsonrpc?: "2.0" | undefined;
|
|
36
28
|
id?: string | number | null | undefined;
|
|
37
|
-
params?: Record<string, import("@metamask/
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
method: import("@metamask/superstruct").Struct<string, null>;
|
|
42
|
-
params: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined, null>;
|
|
43
|
-
}>;
|
|
44
|
-
}>;
|
|
29
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
30
|
+
};
|
|
31
|
+
duration: string;
|
|
32
|
+
}, null>;
|
|
45
33
|
export type CronjobSpecification = Infer<typeof CronjobSpecificationStruct>;
|
|
46
34
|
/**
|
|
47
35
|
* Check if the given value is a {@link CronjobSpecification} object.
|
|
@@ -50,36 +38,39 @@ export type CronjobSpecification = Infer<typeof CronjobSpecificationStruct>;
|
|
|
50
38
|
* @returns Whether the value is a valid {@link CronjobSpecification} object.
|
|
51
39
|
*/
|
|
52
40
|
export declare function isCronjobSpecification(value: unknown): boolean;
|
|
53
|
-
export declare const CronjobSpecificationArrayStruct: import("@metamask/superstruct").Struct<{
|
|
41
|
+
export declare const CronjobSpecificationArrayStruct: import("@metamask/superstruct").Struct<({
|
|
54
42
|
expression: string;
|
|
55
43
|
request: {
|
|
56
44
|
method: string;
|
|
57
45
|
jsonrpc?: "2.0" | undefined;
|
|
58
46
|
id?: string | number | null | undefined;
|
|
59
|
-
params?: Record<string, import("@metamask/
|
|
47
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
60
48
|
};
|
|
61
|
-
}
|
|
49
|
+
} | {
|
|
50
|
+
request: {
|
|
51
|
+
method: string;
|
|
52
|
+
jsonrpc?: "2.0" | undefined;
|
|
53
|
+
id?: string | number | null | undefined;
|
|
54
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
55
|
+
};
|
|
56
|
+
duration: string;
|
|
57
|
+
})[], import("@metamask/superstruct").Struct<{
|
|
62
58
|
expression: string;
|
|
63
59
|
request: {
|
|
64
60
|
method: string;
|
|
65
61
|
jsonrpc?: "2.0" | undefined;
|
|
66
62
|
id?: string | number | null | undefined;
|
|
67
|
-
params?: Record<string, import("@metamask/
|
|
63
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
68
64
|
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
request: import("@metamask/superstruct").Struct<{
|
|
65
|
+
} | {
|
|
66
|
+
request: {
|
|
72
67
|
method: string;
|
|
73
68
|
jsonrpc?: "2.0" | undefined;
|
|
74
69
|
id?: string | number | null | undefined;
|
|
75
|
-
params?: Record<string, import("@metamask/
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
method: import("@metamask/superstruct").Struct<string, null>;
|
|
80
|
-
params: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined, null>;
|
|
81
|
-
}>;
|
|
82
|
-
}>>;
|
|
70
|
+
params?: Record<string, import("@metamask/snaps-sdk").Json> | import("@metamask/snaps-sdk").Json[] | undefined;
|
|
71
|
+
};
|
|
72
|
+
duration: string;
|
|
73
|
+
}, null>>;
|
|
83
74
|
/**
|
|
84
75
|
* Check if the given value is an array of {@link CronjobSpecification} objects.
|
|
85
76
|
*
|
package/dist/cronjob.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cronjob.d.mts","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cronjob.d.mts","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAoBnD,eAAO,MAAM,uBAAuB;;;;;;;;;;EAKlC,CAAC;AAEH,MAAM,MAAM,iBAAiB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAEtE,eAAO,MAAM,oBAAoB,sDAWhC,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAYhE,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;;;;QAMrC,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,KAAK,CAAC,OAAO,0BAA0B,CAAC,CAAC;AAE5E;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAO9D;AAED,eAAO,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAE3C,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAOnE"}
|
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,uBA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AClD,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,uBAAA,IAAI,2BAAU,IAAA,yBAAa,EAAC,KAAK,CAAC,MAAA,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,kBAAkB,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,iCAAS,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACP,OAAO,uBAAA,IAAI,+BAAO,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,MAAM,KAAK,GAAG,WAAW,CAAC,uBAAA,IAAI,+BAAO,CAAC;YACpC,CAAC,CAAC,uBAAA,IAAI,+BAAO,CAAC,SAAS,EAAE;YACzB,CAAC,CAAC,IAAA,2BAAc,EAAC,uBAAA,IAAI,+BAAO,CAAC,CAAC;QAEhC,OAAO;YACL,IAAI,EAAE,+BAAuB;YAC7B,OAAO,EAAE,kCAA0B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,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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DAAqD;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,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,CA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