@metamask/snaps-utils 9.4.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 +16 -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/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/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/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,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [10.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Support scheduling cronjobs with an ISO 8601 duration ([#3421](https://github.com/MetaMask/snaps/pull/3421))
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- **BREAKING:** Drop support for Node.js 18 and 21 ([#3447](https://github.com/MetaMask/snaps/pull/3447))
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
|
|
22
|
+
- Unwrap double-wrapped JSON-RPC errors ([#3432](https://github.com/MetaMask/snaps/pull/3432))
|
|
23
|
+
|
|
10
24
|
## [9.4.0]
|
|
11
25
|
|
|
12
26
|
### Added
|
|
@@ -623,7 +637,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
623
637
|
- The version of the package no longer needs to match the version of all other
|
|
624
638
|
MetaMask Snaps packages.
|
|
625
639
|
|
|
626
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@
|
|
640
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@10.0.0...HEAD
|
|
641
|
+
[10.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@9.4.0...@metamask/snaps-utils@10.0.0
|
|
627
642
|
[9.4.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@9.3.0...@metamask/snaps-utils@9.4.0
|
|
628
643
|
[9.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@9.2.2...@metamask/snaps-utils@9.3.0
|
|
629
644
|
[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,QAAA,oBAAoB,GAAG,IAAA,oBAAM,EACxC,IAAA,oBAAM,GAAE,EACR,oBAAoB,EACpB,CAAC,KAAK,EAAE,EAAE;IACR,IAAI,CAAC;QACH,IAAA,6BAAe,EAAC,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,IAAA,oBAAM,EAAC;IAClD,UAAU,EAAE,4BAAoB;IAChC,OAAO,EAAE,+BAAuB;CACjC,CAAC,CAAC;AAEH,MAAM,kCAAkC,GAAG,IAAA,oBAAM,EAAC;IAChD,QAAQ,EAAE,4BAAqB;IAC/B,OAAO,EAAE,+BAAuB;CACjC,CAAC,CAAC;AAEU,QAAA,0BAA0B,GAAG,IAAA,0BAAc,EAAC,CAAC,KAAK,EAAE,EAAE;IACjE,IAAI,IAAA,gBAAQ,EAAC,KAAK,CAAC,IAAI,IAAA,mBAAW,EAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;QACtD,OAAO,kCAAkC,CAAC;IAC5C,CAAC;IAED,OAAO,oCAAoC,CAAC;AAC9C,CAAC,CAAC,CAAC;AAIH;;;;;GAKG;AACH,SAAgB,sBAAsB,CAAC,KAAc;IACnD,IAAI,CAAC;QACH,IAAA,oBAAM,EAAC,KAAK,EAAE,kCAA0B,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAPD,wDAOC;AAEY,QAAA,+BAA+B,GAAG,IAAA,mBAAK,EAClD,kCAA0B,CAC3B,CAAC;AAEF;;;;;GAKG;AACH,SAAgB,2BAA2B,CAAC,KAAc;IACxD,IAAI,CAAC;QACH,IAAA,oBAAM,EAAC,KAAK,EAAE,uCAA+B,CAAC,CAAC;QAC/C,OAAO,IAAI,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,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;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AAO,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":";;;;;;;;;;;;;;;AA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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,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,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
CHANG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;;;;;;;;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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,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;gBACnE,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;YAC7D,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,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,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,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.cjs
CHANGED
package/dist/eval.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eval.cjs","sourceRoot":"","sources":["../src/eval.ts"],"names":[],"mappings":";;;AAAA,2CAAyC;AACzC,iDAAqC;AACrC,+BAA4B;AAE5B,iCAAwC;AA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QACjC,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAY,EAAE,EAAE;YACxC,MAAM,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QAC5B,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAY,EAAE,EAAE;YACxC,MAAM,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QAC5B,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,QAAgB,EAAE,EAAE;YACrC,MAAM,MAAM,GAAG;gBACb,MAAM;gBACN,MAAM;gBACN,OAAO;aACR,CAAC;YAEF,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;gBACnB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;YACzB,CAAC;YAED,OAAO,MAAM,CACX,IAAI,aAAa,CACf,2CAA2C,QAAQ,GAAG,EACtD,MAAM,CACP,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AArDD,gCAqDC","sourcesContent":["import { assert } from '@metamask/utils';\nimport { fork } from 'child_process';\nimport { join } from 'path';\n\nimport { validateFilePath } from './fs';\n\nexport type EvalWorkerMessage = {\n type: 'snap-exports';\n data: {\n exports: string[];\n };\n};\n\nexport type EvalOutput = {\n stdout: string;\n stderr: string;\n exports: string[];\n};\n\nexport class SnapEvalError extends Error {\n readonly output: EvalOutput;\n\n constructor(message: string, output: EvalOutput) {\n super(message);\n\n this.name = 'SnapEvalError';\n this.output = output;\n }\n}\n\n/**\n * Spawn a new process to run the provided bundle in.\n *\n * @param bundlePath - The path to the bundle to run.\n * @returns `null` if the worker ran successfully.\n * @throws If the worker failed to run successfully.\n */\nexport async function evalBundle(bundlePath: string): Promise<EvalOutput> {\n await validateFilePath(bundlePath);\n\n return new Promise((resolve, reject) => {\n const worker = fork(join(__dirname, 'eval-worker.cjs'), [bundlePath], {\n // To avoid printing the output of the worker to the console, we set\n // `stdio` to `pipe` and handle the output ourselves.\n stdio: 'pipe',\n });\n\n let stdout = '';\n let stderr = '';\n let exports: string[] = [];\n\n assert(worker.stdout, '`stdout` should be defined.');\n assert(worker.stderr, '`stderr` should be defined.');\n\n worker.on('message', (message: EvalWorkerMessage) => {\n assert(\n message.type === 'snap-exports',\n `Received unexpected message with type \"${message.type}\".`,\n );\n\n exports = message.data.exports;\n });\n\n worker.stdout.on('data', (data: Buffer) => {\n stdout += data.toString();\n });\n\n worker.stderr.on('data', (data: Buffer) => {\n stderr += data.toString();\n });\n\n worker.on('exit', (exitCode: number) => {\n const output = {\n stdout,\n stderr,\n exports,\n };\n\n if (exitCode === 0) {\n return resolve(output);\n }\n\n return reject(\n new SnapEvalError(\n `Process exited with non-zero exit code: ${exitCode}.`,\n output,\n ),\n );\n });\n });\n}\n"]}
|
package/dist/eval.mjs
CHANGED
package/dist/eval.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eval.mjs","sourceRoot":"","sources":["../src/eval.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,IAAI,EAAE,sBAAsB;AACrC,OAAO,EAAE,IAAI,EAAE,aAAa;AAE5B,OAAO,EAAE,gBAAgB,EAAE,iBAAa;AAexC,MAAM,OAAO,aAAc,SAAQ,KAAK;
|
|
1
|
+
{"version":3,"file":"eval.mjs","sourceRoot":"","sources":["../src/eval.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,IAAI,EAAE,sBAAsB;AACrC,OAAO,EAAE,IAAI,EAAE,aAAa;AAE5B,OAAO,EAAE,gBAAgB,EAAE,iBAAa;AAexC,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC7B,MAAM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YACxC,MAAM,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QAC5B,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,QAAgB,EAAE,EAAE;YACrC,MAAM,MAAM,GAAG;gBACb,MAAM;gBACN,MAAM;gBACN,OAAO;aACR,CAAC;YAEF,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;gBACnB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;YACzB,CAAC;YAED,OAAO,MAAM,CACX,IAAI,aAAa,CACf,2CAA2C,QAAQ,GAAG,EACtD,MAAM,CACP,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { assert } from '@metamask/utils';\nimport { fork } from 'child_process';\nimport { join } from 'path';\n\nimport { validateFilePath } from './fs';\n\nexport type EvalWorkerMessage = {\n type: 'snap-exports';\n data: {\n exports: string[];\n };\n};\n\nexport type EvalOutput = {\n stdout: string;\n stderr: string;\n exports: string[];\n};\n\nexport class SnapEvalError extends Error {\n readonly output: EvalOutput;\n\n constructor(message: string, output: EvalOutput) {\n super(message);\n\n this.name = 'SnapEvalError';\n this.output = output;\n }\n}\n\n/**\n * Spawn a new process to run the provided bundle in.\n *\n * @param bundlePath - The path to the bundle to run.\n * @returns `null` if the worker ran successfully.\n * @throws If the worker failed to run successfully.\n */\nexport async function evalBundle(bundlePath: string): Promise<EvalOutput> {\n await validateFilePath(bundlePath);\n\n return new Promise((resolve, reject) => {\n const worker = fork(join(__dirname, 'eval-worker.cjs'), [bundlePath], {\n // To avoid printing the output of the worker to the console, we set\n // `stdio` to `pipe` and handle the output ourselves.\n stdio: 'pipe',\n });\n\n let stdout = '';\n let stderr = '';\n let exports: string[] = [];\n\n assert(worker.stdout, '`stdout` should be defined.');\n assert(worker.stderr, '`stderr` should be defined.');\n\n worker.on('message', (message: EvalWorkerMessage) => {\n assert(\n message.type === 'snap-exports',\n `Received unexpected message with type \"${message.type}\".`,\n );\n\n exports = message.data.exports;\n });\n\n worker.stdout.on('data', (data: Buffer) => {\n stdout += data.toString();\n });\n\n worker.stderr.on('data', (data: Buffer) => {\n stderr += data.toString();\n });\n\n worker.on('exit', (exitCode: number) => {\n const output = {\n stdout,\n stderr,\n exports,\n };\n\n if (exitCode === 0) {\n return resolve(output);\n }\n\n return reject(\n new SnapEvalError(\n `Process exited with non-zero exit code: ${exitCode}.`,\n output,\n ),\n );\n });\n });\n}\n"]}
|
|
@@ -22,23 +22,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
26
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
27
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
28
|
-
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");
|
|
29
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
30
|
-
};
|
|
31
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
32
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
33
|
-
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");
|
|
34
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
35
|
-
};
|
|
36
|
-
var _Context_options, _Context_nextSeverity;
|
|
37
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
26
|
exports.hasFixes = exports.runValidators = void 0;
|
|
39
27
|
const utils_1 = require("@metamask/utils");
|
|
40
28
|
const defaultValidators = __importStar(require("./validators/index.cjs"));
|
|
41
29
|
class Context {
|
|
30
|
+
reports = [];
|
|
31
|
+
#options = {};
|
|
32
|
+
#nextSeverity = undefined;
|
|
42
33
|
/**
|
|
43
34
|
* Construct a new validator context.
|
|
44
35
|
*
|
|
@@ -46,30 +37,26 @@ class Context {
|
|
|
46
37
|
* @param options.exports - Exports detected by evaluating the bundle.
|
|
47
38
|
*/
|
|
48
39
|
constructor(options) {
|
|
49
|
-
this
|
|
50
|
-
_Context_options.set(this, {});
|
|
51
|
-
_Context_nextSeverity.set(this, undefined);
|
|
52
|
-
__classPrivateFieldSet(this, _Context_options, options, "f");
|
|
40
|
+
this.#options = options;
|
|
53
41
|
}
|
|
54
42
|
report(message, fix) {
|
|
55
|
-
(0, utils_1.assert)(
|
|
43
|
+
(0, utils_1.assert)(this.#nextSeverity !== undefined);
|
|
56
44
|
this.reports.push({
|
|
57
|
-
severity:
|
|
45
|
+
severity: this.#nextSeverity,
|
|
58
46
|
message,
|
|
59
47
|
fix,
|
|
60
48
|
});
|
|
61
49
|
}
|
|
62
50
|
prepareForValidator(settings) {
|
|
63
|
-
|
|
51
|
+
this.#nextSeverity = settings.severity;
|
|
64
52
|
}
|
|
65
53
|
get hasErrors() {
|
|
66
54
|
return this.reports.some((report) => report.severity === 'error');
|
|
67
55
|
}
|
|
68
56
|
get options() {
|
|
69
|
-
return
|
|
57
|
+
return this.#options;
|
|
70
58
|
}
|
|
71
59
|
}
|
|
72
|
-
_Context_options = new WeakMap(), _Context_nextSeverity = new WeakMap();
|
|
73
60
|
/**
|
|
74
61
|
* Verify that snap files are completely valid.
|
|
75
62
|
* First it runs validators on unparsed files to check structure.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.cjs","sourceRoot":"","sources":["../../src/manifest/validator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validator.cjs","sourceRoot":"","sources":["../../src/manifest/validator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA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AC;IAED,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;QACtB,OAAO;YACL,OAAO,EAAE,OAAO,CAAC,OAAO;SACzB,CAAC;IACJ,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,OAAO,CAAC,mBAAmB,CAAC;YAC1B,QAAQ,EAAE,IAAI,CAAC,QAAQ;SACxB,CAAC,CAAC;QACH,MAAM,IAAI,CAAC,aAAa,EAAE,CAAC,KAAkB,EAAE,OAAO,CAAC,CAAC;IAC1D,CAAC;IAED,OAAO;QACL,KAAK,EAAE,KAAkB;QACzB,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC;AACJ,CAAC;AA/BD,sCA+BC;AAED;;;;;GAKG;AACH,SAAgB,QAAQ,CAAC,OAAyB;IAChD,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACtD,CAAC;AAFD,4BAEC","sourcesContent":["import { assert } from '@metamask/utils';\n\nimport type {\n ValidatorContext,\n ValidatorContextOptions,\n ValidatorFix,\n ValidatorMeta,\n ValidatorReport,\n ValidatorSeverity,\n} from './validator-types';\nimport * as defaultValidators from './validators';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\n\nexport type ValidatorResults = {\n files?: SnapFiles;\n reports: ValidatorReport[];\n};\n\nclass Context implements ValidatorContext {\n reports: ValidatorReport[] = [];\n\n readonly #options: ValidatorContextOptions = {};\n\n #nextSeverity?: ValidatorSeverity = undefined;\n\n /**\n * Construct a new validator context.\n *\n * @param options - The options for the validator context.\n * @param options.exports - Exports detected by evaluating the bundle.\n */\n constructor(options: ValidatorContextOptions) {\n this.#options = options;\n }\n\n report(message: string, fix?: ValidatorFix): void {\n assert(this.#nextSeverity !== undefined);\n this.reports.push({\n severity: this.#nextSeverity,\n message,\n fix,\n });\n }\n\n prepareForValidator(settings: { severity: ValidatorSeverity }) {\n this.#nextSeverity = settings.severity;\n }\n\n get hasErrors() {\n return this.reports.some((report) => report.severity === 'error');\n }\n\n get options() {\n return this.#options;\n }\n}\n\n/**\n * Verify that snap files are completely valid.\n * First it runs validators on unparsed files to check structure.\n * Secondly it runs validators on parsed files to check semantics.\n *\n * @param files - All files required to run a snap.\n * @param rules - Validators to run.\n * @param options - Options for the validation.\n * @param options.exports - Exports detected by evaluating the bundle.\n * @returns The validation results.\n */\n// TODO(ritave): snap.manifest.json and package.json should check\n// json parsing as well instead of assuming it's\n// already parsed\nexport async function runValidators(\n files: UnvalidatedSnapFiles,\n rules: ValidatorMeta[] = Object.values(defaultValidators),\n options: ValidatorContextOptions = {},\n): Promise<ValidatorResults> {\n const context = new Context(options);\n\n for (const rule of rules) {\n context.prepareForValidator({\n severity: rule.severity,\n });\n await rule.structureCheck?.(files, context);\n }\n\n if (context.hasErrors) {\n return {\n reports: context.reports,\n };\n }\n\n for (const rule of rules) {\n context.prepareForValidator({\n severity: rule.severity,\n });\n await rule.semanticCheck?.(files as SnapFiles, context);\n }\n\n return {\n files: files as SnapFiles,\n reports: context.reports,\n };\n}\n\n/**\n * Get whether any reports has pending fixes.\n *\n * @param results - Results of the validation run.\n * @returns Whether there are fixes pending.\n */\nexport function hasFixes(results: ValidatorResults): boolean {\n return results.reports.some((report) => report.fix);\n}\n"]}
|
|
@@ -1,18 +1,9 @@
|
|
|
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 _Context_options, _Context_nextSeverity;
|
|
13
1
|
import { assert } from "@metamask/utils";
|
|
14
2
|
import * as defaultValidators from "./validators/index.mjs";
|
|
15
3
|
class Context {
|
|
4
|
+
reports = [];
|
|
5
|
+
#options = {};
|
|
6
|
+
#nextSeverity = undefined;
|
|
16
7
|
/**
|
|
17
8
|
* Construct a new validator context.
|
|
18
9
|
*
|
|
@@ -20,30 +11,26 @@ class Context {
|
|
|
20
11
|
* @param options.exports - Exports detected by evaluating the bundle.
|
|
21
12
|
*/
|
|
22
13
|
constructor(options) {
|
|
23
|
-
this
|
|
24
|
-
_Context_options.set(this, {});
|
|
25
|
-
_Context_nextSeverity.set(this, undefined);
|
|
26
|
-
__classPrivateFieldSet(this, _Context_options, options, "f");
|
|
14
|
+
this.#options = options;
|
|
27
15
|
}
|
|
28
16
|
report(message, fix) {
|
|
29
|
-
assert(
|
|
17
|
+
assert(this.#nextSeverity !== undefined);
|
|
30
18
|
this.reports.push({
|
|
31
|
-
severity:
|
|
19
|
+
severity: this.#nextSeverity,
|
|
32
20
|
message,
|
|
33
21
|
fix,
|
|
34
22
|
});
|
|
35
23
|
}
|
|
36
24
|
prepareForValidator(settings) {
|
|
37
|
-
|
|
25
|
+
this.#nextSeverity = settings.severity;
|
|
38
26
|
}
|
|
39
27
|
get hasErrors() {
|
|
40
28
|
return this.reports.some((report) => report.severity === 'error');
|
|
41
29
|
}
|
|
42
30
|
get options() {
|
|
43
|
-
return
|
|
31
|
+
return this.#options;
|
|
44
32
|
}
|
|
45
33
|
}
|
|
46
|
-
_Context_options = new WeakMap(), _Context_nextSeverity = new WeakMap();
|
|
47
34
|
/**
|
|
48
35
|
* Verify that snap files are completely valid.
|
|
49
36
|
* First it runs validators on unparsed files to check structure.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.mjs","sourceRoot":"","sources":["../../src/manifest/validator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validator.mjs","sourceRoot":"","sources":["../../src/manifest/validator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAUzC,OAAO,KAAK,iBAAiB,+BAAqB;AAQlD,MAAM,OAAO;IACX,OAAO,GAAsB,EAAE,CAAC;IAEvB,QAAQ,GAA4B,EAAE,CAAC;IAEhD,aAAa,GAAuB,SAAS,CAAC;IAE9C;;;;;OAKG;IACH,YAAY,OAAgC;QAC1C,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,MAAM,CAAC,OAAe,EAAE,GAAkB;QACxC,MAAM,CAAC,IAAI,CAAC,aAAa,KAAK,SAAS,CAAC,CAAC;QACzC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;YAChB,QAAQ,EAAE,IAAI,CAAC,aAAa;YAC5B,OAAO;YACP,GAAG;SACJ,CAAC,CAAC;IACL,CAAC;IAED,mBAAmB,CAAC,QAAyC;QAC3D,IAAI,CAAC,aAAa,GAAG,QAAQ,CAAC,QAAQ,CAAC;IACzC,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC;IACpE,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF;AAED;;;;;;;;;;GAUG;AACH,iEAAiE;AACjE,8DAA8D;AAC9D,+BAA+B;AAC/B,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,KAA2B,EAC3B,QAAyB,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC,EACzD,UAAmC,EAAE;IAErC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAErC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,OAAO,CAAC,mBAAmB,CAAC;YAC1B,QAAQ,EAAE,IAAI,CAAC,QAAQ;SACxB,CAAC,CAAC;QACH,MAAM,IAAI,CAAC,cAAc,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;IAED,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;QACtB,OAAO;YACL,OAAO,EAAE,OAAO,CAAC,OAAO;SACzB,CAAC;IACJ,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,OAAO,CAAC,mBAAmB,CAAC;YAC1B,QAAQ,EAAE,IAAI,CAAC,QAAQ;SACxB,CAAC,CAAC;QACH,MAAM,IAAI,CAAC,aAAa,EAAE,CAAC,KAAkB,EAAE,OAAO,CAAC,CAAC;IAC1D,CAAC;IAED,OAAO;QACL,KAAK,EAAE,KAAkB;QACzB,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,OAAyB;IAChD,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACtD,CAAC","sourcesContent":["import { assert } from '@metamask/utils';\n\nimport type {\n ValidatorContext,\n ValidatorContextOptions,\n ValidatorFix,\n ValidatorMeta,\n ValidatorReport,\n ValidatorSeverity,\n} from './validator-types';\nimport * as defaultValidators from './validators';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\n\nexport type ValidatorResults = {\n files?: SnapFiles;\n reports: ValidatorReport[];\n};\n\nclass Context implements ValidatorContext {\n reports: ValidatorReport[] = [];\n\n readonly #options: ValidatorContextOptions = {};\n\n #nextSeverity?: ValidatorSeverity = undefined;\n\n /**\n * Construct a new validator context.\n *\n * @param options - The options for the validator context.\n * @param options.exports - Exports detected by evaluating the bundle.\n */\n constructor(options: ValidatorContextOptions) {\n this.#options = options;\n }\n\n report(message: string, fix?: ValidatorFix): void {\n assert(this.#nextSeverity !== undefined);\n this.reports.push({\n severity: this.#nextSeverity,\n message,\n fix,\n });\n }\n\n prepareForValidator(settings: { severity: ValidatorSeverity }) {\n this.#nextSeverity = settings.severity;\n }\n\n get hasErrors() {\n return this.reports.some((report) => report.severity === 'error');\n }\n\n get options() {\n return this.#options;\n }\n}\n\n/**\n * Verify that snap files are completely valid.\n * First it runs validators on unparsed files to check structure.\n * Secondly it runs validators on parsed files to check semantics.\n *\n * @param files - All files required to run a snap.\n * @param rules - Validators to run.\n * @param options - Options for the validation.\n * @param options.exports - Exports detected by evaluating the bundle.\n * @returns The validation results.\n */\n// TODO(ritave): snap.manifest.json and package.json should check\n// json parsing as well instead of assuming it's\n// already parsed\nexport async function runValidators(\n files: UnvalidatedSnapFiles,\n rules: ValidatorMeta[] = Object.values(defaultValidators),\n options: ValidatorContextOptions = {},\n): Promise<ValidatorResults> {\n const context = new Context(options);\n\n for (const rule of rules) {\n context.prepareForValidator({\n severity: rule.severity,\n });\n await rule.structureCheck?.(files, context);\n }\n\n if (context.hasErrors) {\n return {\n reports: context.reports,\n };\n }\n\n for (const rule of rules) {\n context.prepareForValidator({\n severity: rule.severity,\n });\n await rule.semanticCheck?.(files as SnapFiles, context);\n }\n\n return {\n files: files as SnapFiles,\n reports: context.reports,\n };\n}\n\n/**\n * Get whether any reports has pending fixes.\n *\n * @param results - Results of the validation run.\n * @returns Whether there are fixes pending.\n */\nexport function hasFixes(results: ValidatorResults): boolean {\n return results.reports.some((report) => report.fix);\n}\n"]}
|
package/dist/time.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.cjs","sourceRoot":"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AAC;AACd,CAAC,CAAC,CAAC;AAEH;;;;;GAKG;AACH,SAAgB,uBAAuB,CAAC,KAAa;IACnD,MAAM,IAAI,GAAG,gBAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;IAExD,kDAAkD;IAClD,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC;QAClC,oBAAoB,EAAE,IAAI;KAC3B,CAAW,CAAC;AACf,CAAC;AAPD,0DAOC","sourcesContent":["import { refine, string } from '@metamask/superstruct';\nimport { DateTime, Duration } from 'luxon';\n\n/**\n * Refines a string as an ISO 8601 duration.\n */\nexport const ISO8601DurationStruct = refine(\n string(),\n 'ISO 8601 duration',\n (value) => {\n const parsedDuration = Duration.fromISO(value);\n if (!parsedDuration.isValid) {\n return 'Not a valid ISO 8601 duration';\n }\n\n return true;\n },\n);\n\n/**\n * Regex to match the offset part of an ISO 8601 date.\n */\nconst offsetRegex = /Z|([+-]\\d{2}:?\\d{2})$/u;\n\n/**\n * Refines a string as an ISO 8601 date.\n */\nexport const ISO8601DateStruct = refine(string(), 'ISO 8601 date', (value) => {\n const parsedDate = DateTime.fromISO(value);\n\n if (!parsedDate.isValid) {\n return 'Not a valid ISO 8601 date';\n }\n\n if (!offsetRegex.test(value)) {\n // Luxon doesn't have a reliable way to check if timezone info was not provided\n return 'ISO 8601 date must have timezone information';\n }\n\n return true;\n});\n\n/**\n * Remove millisecond precision from an ISO 8601 string.\n *\n * @param value - A valid ISO 8601 date.\n * @returns A valid ISO 8601 date with millisecond precision removed.\n */\nexport function toCensoredISO8601String(value: string) {\n const date = DateTime.fromISO(value, { setZone: true });\n\n // Make sure any millisecond precision is removed.\n return date.startOf('second').toISO({\n suppressMilliseconds: true,\n }) as string;\n}\n"]}
|
package/dist/time.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.d.cts","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,eAAO,MAAM,qBAAqB,
|
|
1
|
+
{"version":3,"file":"time.d.cts","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,eAAO,MAAM,qBAAqB,sDAWjC,CAAC;AAOF;;GAEG;AACH,eAAO,MAAM,iBAAiB,sDAa5B,CAAC;AAEH;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,UAOpD"}
|
package/dist/time.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.d.mts","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,eAAO,MAAM,qBAAqB,
|
|
1
|
+
{"version":3,"file":"time.d.mts","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,eAAO,MAAM,qBAAqB,sDAWjC,CAAC;AAOF;;GAEG;AACH,eAAO,MAAM,iBAAiB,sDAa5B,CAAC;AAEH;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,UAOpD"}
|
package/dist/time.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.mjs","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B;AACvD,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,cAAc;AAE3C;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CACzC,MAAM,EAAE,EACR,mBAAmB,EACnB,CAAC,KAAK,EAAE,EAAE;IACR,MAAM,cAAc,GAAG,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC/C,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,CAAC;QAC5B,OAAO,+BAA+B,CAAC;IACzC,CAAC;
|
|
1
|
+
{"version":3,"file":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CAAW,CAAC;AACf,CAAC","sourcesContent":["import { refine, string } from '@metamask/superstruct';\nimport { DateTime, Duration } from 'luxon';\n\n/**\n * Refines a string as an ISO 8601 duration.\n */\nexport const ISO8601DurationStruct = refine(\n string(),\n 'ISO 8601 duration',\n (value) => {\n const parsedDuration = Duration.fromISO(value);\n if (!parsedDuration.isValid) {\n return 'Not a valid ISO 8601 duration';\n }\n\n return true;\n },\n);\n\n/**\n * Regex to match the offset part of an ISO 8601 date.\n */\nconst offsetRegex = /Z|([+-]\\d{2}:?\\d{2})$/u;\n\n/**\n * Refines a string as an ISO 8601 date.\n */\nexport const ISO8601DateStruct = refine(string(), 'ISO 8601 date', (value) => {\n const parsedDate = DateTime.fromISO(value);\n\n if (!parsedDate.isValid) {\n return 'Not a valid ISO 8601 date';\n }\n\n if (!offsetRegex.test(value)) {\n // Luxon doesn't have a reliable way to check if timezone info was not provided\n return 'ISO 8601 date must have timezone information';\n }\n\n return true;\n});\n\n/**\n * Remove millisecond precision from an ISO 8601 string.\n *\n * @param value - A valid ISO 8601 date.\n * @returns A valid ISO 8601 date with millisecond precision removed.\n */\nexport function toCensoredISO8601String(value: string) {\n const date = DateTime.fromISO(value, { setZone: true });\n\n // Make sure any millisecond precision is removed.\n return date.startOf('second').toISO({\n suppressMilliseconds: true,\n }) as string;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VirtualFile.cjs","sourceRoot":"","sources":["../../src/virtual-file/VirtualFile.ts"],"names":[],"mappings":";;;AAAA,0HAA0H;AAC1H,iEAAiE;AACjE,2FAA2F;AAC3F,wFAAwF;AACxF,EAAE;AACF,oHAAoH;AACpH,qGAAqG;AACrG,sDAAsD;AACtD,2CAAqD;AACrD,sCAAqC;AAErC,kDAA0C;AA+B1C,MAAa,WAAW;IACtB,YAAY,KAA0B;QACpC,IAAI,OAA4B,CAAC;QACjC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,YAAY,UAAU,EAAE,CAA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AAE,CAAC;YACnC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QAC3B,CAAC;aAAM,CAAC;YACN,mFAAmF;YACnF,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACpC,CAAC;QACD,KAAK,CAAC,MAAM,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACtC,KAAK,CAAC,IAAI,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAlED,kCAkEC","sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert, bytesToHex } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-object-type\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n get size() {\n return typeof this.value === 'string'\n ? this.value.length\n : this.value.byteLength;\n }\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n } else if (this.value instanceof Uint8Array && encoding === 'hex') {\n return bytesToHex(this.value);\n } else if (this.value instanceof Uint8Array && encoding === 'base64') {\n // For large files, this is quite slow, instead use `encodeBase64()`\n // TODO: Use @metamask/utils for this\n return base64.encode(this.value);\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path = this.path;\n return vfile;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VirtualFile.mjs","sourceRoot":"","sources":["../../src/virtual-file/VirtualFile.ts"],"names":[],"mappings":"AAAA,0HAA0H;AAC1H,iEAAiE;AACjE,2FAA2F;AAC3F,wFAAwF;AACxF,EAAE;AACF,oHAAoH;AACpH,qGAAqG;AACrG,sDAAs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AAoE;YACpE,qCAAqC;YACrC,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,QAAQ,CAAC,CAAC;QAC1C,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;IAED,KAAK;QACH,MAAM,KAAK,GAAG,IAAI,WAAW,EAAU,CAAC;QACxC,IAAI,OAAO,IAAI,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YACnC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QAC3B,CAAC;aAAM,CAAC;YACN,mFAAmF;YACnF,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACpC,CAAC;QACD,KAAK,CAAC,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACtC,KAAK,CAAC,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert, bytesToHex } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-object-type\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n get size() {\n return typeof this.value === 'string'\n ? this.value.length\n : this.value.byteLength;\n }\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n } else if (this.value instanceof Uint8Array && encoding === 'hex') {\n return bytesToHex(this.value);\n } else if (this.value instanceof Uint8Array && encoding === 'base64') {\n // For large files, this is quite slow, instead use `encodeBase64()`\n // TODO: Use @metamask/utils for this\n return base64.encode(this.value);\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path = this.path;\n return vfile;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"description": "A collection of utilities for MetaMask Snaps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
"@metamask/rpc-errors": "^7.0.2",
|
|
87
87
|
"@metamask/slip44": "^4.2.0",
|
|
88
88
|
"@metamask/snaps-registry": "^3.2.3",
|
|
89
|
-
"@metamask/snaps-sdk": "^
|
|
89
|
+
"@metamask/snaps-sdk": "^8.0.0",
|
|
90
90
|
"@metamask/superstruct": "^3.2.1",
|
|
91
91
|
"@metamask/utils": "^11.4.0",
|
|
92
92
|
"@noble/hashes": "^1.7.1",
|
|
@@ -100,15 +100,15 @@
|
|
|
100
100
|
"marked": "^12.0.1",
|
|
101
101
|
"rfdc": "^1.3.0",
|
|
102
102
|
"semver": "^7.5.4",
|
|
103
|
-
"ses": "^1.
|
|
103
|
+
"ses": "^1.13.0",
|
|
104
104
|
"validate-npm-package-name": "^5.0.0"
|
|
105
105
|
},
|
|
106
106
|
"devDependencies": {
|
|
107
107
|
"@lavamoat/allow-scripts": "^3.3.3",
|
|
108
108
|
"@metamask/auto-changelog": "^5.0.2",
|
|
109
109
|
"@metamask/post-message-stream": "^10.0.0",
|
|
110
|
-
"@swc/core": "1.
|
|
111
|
-
"@swc/jest": "^0.2.
|
|
110
|
+
"@swc/core": "1.11.31",
|
|
111
|
+
"@swc/jest": "^0.2.38",
|
|
112
112
|
"@testing-library/dom": "^10.4.0",
|
|
113
113
|
"@ts-bridge/cli": "^0.6.1",
|
|
114
114
|
"@types/jest": "^27.5.1",
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
"vitest": "^3.1.1"
|
|
139
139
|
},
|
|
140
140
|
"engines": {
|
|
141
|
-
"node": "^
|
|
141
|
+
"node": "^20 || >=22"
|
|
142
142
|
},
|
|
143
143
|
"publishConfig": {
|
|
144
144
|
"access": "public",
|