@metamask/snaps-utils 11.2.0 → 11.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -1
- package/dist/errors.cjs +8 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +3 -1
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts +3 -1
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +7 -0
- package/dist/errors.mjs.map +1 -1
- package/dist/manifest/manifest.cjs +9 -6
- package/dist/manifest/manifest.cjs.map +1 -1
- package/dist/manifest/manifest.d.cts +10 -2
- package/dist/manifest/manifest.d.cts.map +1 -1
- package/dist/manifest/manifest.d.mts +10 -2
- package/dist/manifest/manifest.d.mts.map +1 -1
- package/dist/manifest/manifest.mjs +10 -7
- package/dist/manifest/manifest.mjs.map +1 -1
- package/dist/manifest/validation.cjs +8 -4
- package/dist/manifest/validation.cjs.map +1 -1
- package/dist/manifest/validation.d.cts +4 -0
- package/dist/manifest/validation.d.cts.map +1 -1
- package/dist/manifest/validation.d.mts +4 -0
- package/dist/manifest/validation.d.mts.map +1 -1
- package/dist/manifest/validation.mjs +7 -3
- package/dist/manifest/validation.mjs.map +1 -1
- package/dist/manifest/validator.cjs +15 -3
- package/dist/manifest/validator.cjs.map +1 -1
- package/dist/manifest/validator.d.cts +10 -1
- package/dist/manifest/validator.d.cts.map +1 -1
- package/dist/manifest/validator.d.mts +10 -1
- package/dist/manifest/validator.d.mts.map +1 -1
- package/dist/manifest/validator.mjs +13 -2
- package/dist/manifest/validator.mjs.map +1 -1
- package/dist/snaps.cjs.map +1 -1
- package/dist/snaps.d.cts +6 -2
- package/dist/snaps.d.cts.map +1 -1
- package/dist/snaps.d.mts +6 -2
- package/dist/snaps.d.mts.map +1 -1
- package/dist/snaps.mjs.map +1 -1
- package/dist/types.d.cts +1 -1
- package/dist/types.d.mts +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [11.4.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add support for eval in watch mode for Snaps CLI ([#3553](https://github.com/MetaMask/snaps/pull/3553))
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- Bump ses from `1.13.1` to `1.14.0` ([#3557](https://github.com/MetaMask/snaps/pull/3557))
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
|
|
22
|
+
- Fix typos in `SnapAuxiliaryFilesStruct` variable and `SnapAuxiliaryFile` type ([#3545](https://github.com/MetaMask/snaps/pull/3545))
|
|
23
|
+
- `SnapAuxilaryFilesStruct` and `SnapAuxilaryFile` are still exported for
|
|
24
|
+
backwards compatibility, but will be removed in a future release.
|
|
25
|
+
|
|
26
|
+
## [11.3.0]
|
|
27
|
+
|
|
28
|
+
### Changed
|
|
29
|
+
|
|
30
|
+
- Move `TrackableErrorStruct` from `@metamask/snaps-rpc-methods` for reusability ([#3546](https://github.com/MetaMask/snaps/pull/3546))
|
|
31
|
+
|
|
10
32
|
## [11.2.0]
|
|
11
33
|
|
|
12
34
|
### Added
|
|
@@ -672,7 +694,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
672
694
|
- The version of the package no longer needs to match the version of all other
|
|
673
695
|
MetaMask Snaps packages.
|
|
674
696
|
|
|
675
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.
|
|
697
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.4.0...HEAD
|
|
698
|
+
[11.4.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.3.0...@metamask/snaps-utils@11.4.0
|
|
699
|
+
[11.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.2.0...@metamask/snaps-utils@11.3.0
|
|
676
700
|
[11.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.1.0...@metamask/snaps-utils@11.2.0
|
|
677
701
|
[11.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.0.0...@metamask/snaps-utils@11.1.0
|
|
678
702
|
[11.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@10.1.0...@metamask/snaps-utils@11.0.0
|
package/dist/errors.cjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.unwrapError = exports.isWrappedSnapError = exports.isSerializedSnapError = exports.isSnapError = exports.WrappedSnapError = exports.SNAP_ERROR_WRAPPER_MESSAGE = exports.SNAP_ERROR_WRAPPER_CODE = void 0;
|
|
3
|
+
exports.TrackableErrorStruct = exports.unwrapError = exports.isWrappedSnapError = exports.isSerializedSnapError = exports.isSnapError = exports.WrappedSnapError = exports.SNAP_ERROR_WRAPPER_MESSAGE = exports.SNAP_ERROR_WRAPPER_CODE = void 0;
|
|
4
4
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
5
|
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
6
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
6
7
|
const utils_1 = require("@metamask/utils");
|
|
7
8
|
exports.SNAP_ERROR_WRAPPER_CODE = -31001;
|
|
8
9
|
exports.SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
|
|
@@ -184,4 +185,10 @@ function unwrapError(error) {
|
|
|
184
185
|
];
|
|
185
186
|
}
|
|
186
187
|
exports.unwrapError = unwrapError;
|
|
188
|
+
exports.TrackableErrorStruct = (0, superstruct_1.object)({
|
|
189
|
+
name: (0, superstruct_1.string)(),
|
|
190
|
+
message: (0, superstruct_1.string)(),
|
|
191
|
+
stack: (0, superstruct_1.nullable)((0, superstruct_1.string)()),
|
|
192
|
+
cause: (0, superstruct_1.nullable)((0, superstruct_1.lazy)(() => exports.TrackableErrorStruct)),
|
|
193
|
+
});
|
|
187
194
|
//# sourceMappingURL=errors.cjs.map
|
package/dist/errors.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,qDAI8B;AAG9B,mDAK6B;AAE7B,2CAA2D;AAE9C,QAAA,uBAAuB,GAAG,CAAC,KAAK,CAAC;AACjC,QAAA,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAa,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAU;IAEhB,QAAQ,CAAS;IAEjB,MAAM,CAAU;IAEzB;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,IAAA,yBAAa,EAAC,KAAK,CAAC,IAAI,SAAS,CAAC;IAClD,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,kBAAkB,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC;YACpC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;YACzB,CAAC,CAAC,IAAA,2BAAc,EAAC,IAAI,CAA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iD;YACjD,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;YACxD,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;QAC9D,CAAC;QAED,sEAAsE;QACtE,WAAW;QACX,OAAO;YACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EACjC,IAAA,yBAAa,EAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAChC;YACD,KAAK;SACN,CAAC;IACJ,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IACrD,IAAI,IAAA,sBAAc,EAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;QAC7C,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,EACtB,IAAA,yBAAa,EAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC;AAjED,kCAiEC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error) ?? undefined;\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 | null,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack ?? undefined;\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"]}
|
|
1
|
+
{"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,qDAI8B;AAO9B,mDAK6B;AAE7B,uDAAuE;AAEvE,2CAA2D;AAE9C,QAAA,uBAAuB,GAAG,CAAC,KAAK,CAAC;AACjC,QAAA,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAa,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAU;IAEhB,QAAQ,CAAS;IAEjB,MAAM,CAAU;IAEzB;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QAC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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;AAEY,QAAA,oBAAoB,GAA2B,IAAA,oBAAM,EAAC;IACjE,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,OAAO,EAAE,IAAA,oBAAM,GAAE;IACjB,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;IACzB,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAiB,GAAG,EAAE,CAAC,4BAAoB,CAAC,CAAC;CAClE,CAAC,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 {\n SerializedSnapError,\n SnapError,\n TrackableError,\n} from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Struct } from '@metamask/superstruct';\nimport { lazy, nullable, object, string } from '@metamask/superstruct';\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) ?? undefined;\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 | null,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack ?? undefined;\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\nexport const TrackableErrorStruct: Struct<TrackableError> = object({\n name: string(),\n message: string(),\n stack: nullable(string()),\n cause: nullable(lazy<TrackableError>(() => TrackableErrorStruct)),\n});\n"]}
|
package/dist/errors.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { JsonRpcError as RpcError } from "@metamask/rpc-errors";
|
|
2
2
|
import type { DataWithOptionalCause } from "@metamask/rpc-errors";
|
|
3
|
-
import type { SerializedSnapError, SnapError } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { SerializedSnapError, SnapError, TrackableError } from "@metamask/snaps-sdk";
|
|
4
|
+
import type { Struct } from "@metamask/superstruct";
|
|
4
5
|
import type { Json, JsonRpcError } from "@metamask/utils";
|
|
5
6
|
export declare const SNAP_ERROR_WRAPPER_CODE = -31001;
|
|
6
7
|
export declare const SNAP_ERROR_WRAPPER_MESSAGE = "Wrapped Snap Error";
|
|
@@ -82,4 +83,5 @@ export declare function isWrappedSnapError(error: unknown): error is SerializedS
|
|
|
82
83
|
* whether the error was handled.
|
|
83
84
|
*/
|
|
84
85
|
export declare function unwrapError(error: unknown): [error: RpcError<DataWithOptionalCause>, isHandled: boolean];
|
|
86
|
+
export declare const TrackableErrorStruct: Struct<TrackableError>;
|
|
85
87
|
//# sourceMappingURL=errors.d.cts.map
|
package/dist/errors.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.cts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,IAAI,QAAQ,EAEzB,6BAA6B;AAC9B,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B;AAClE,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"errors.d.cts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,IAAI,QAAQ,EAEzB,6BAA6B;AAC9B,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B;AAClE,OAAO,KAAK,EACV,mBAAmB,EACnB,SAAS,EACT,cAAc,EACf,4BAA4B;AAO7B,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAEpD,OAAO,KAAK,EAAE,IAAI,EAAE,YAAY,EAAE,wBAAwB;AAG1D,eAAO,MAAM,uBAAuB,SAAS,CAAC;AAC9C,eAAO,MAAM,0BAA0B,uBAAuB,CAAC;AAE/D,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,OAAO,uBAAuB,CAAC;IACrC,OAAO,EAAE,OAAO,0BAA0B,CAAC;IAC3C,IAAI,EAAE;QACJ,KAAK,EAAE,IAAI,CAAC;KACb,CAAC;CACH,CAAC;AAEF,qBAAa,gBAAiB,SAAQ,KAAK;;IAOzC;;;;OAIG;gBACS,KAAK,EAAE,OAAO;IAS1B;;;;OAIG;IACH,IAAI,IAAI,WAEP;IAED;;;;OAIG;IACH,IAAI,OAAO,WAEV;IAED;;;;OAIG;IACH,IAAI,KAAK,uBAER;IAED;;;;OAIG;IACH,MAAM,IAAI,0BAA0B;IAcpC;;;;;OAKG;IACH,SAAS;CAGV;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAW9D;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,YAAY,GAClB,KAAK,IAAI,mBAAmB,CAE9B;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,0BAA0B,CAMrC;AAuBD;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,OAAO,GACb,CAAC,KAAK,EAAE,QAAQ,CAAC,qBAAqB,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,CA+D9D;AAED,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,cAAc,CAKtD,CAAC"}
|
package/dist/errors.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { JsonRpcError as RpcError } from "@metamask/rpc-errors";
|
|
2
2
|
import type { DataWithOptionalCause } from "@metamask/rpc-errors";
|
|
3
|
-
import type { SerializedSnapError, SnapError } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { SerializedSnapError, SnapError, TrackableError } from "@metamask/snaps-sdk";
|
|
4
|
+
import type { Struct } from "@metamask/superstruct";
|
|
4
5
|
import type { Json, JsonRpcError } from "@metamask/utils";
|
|
5
6
|
export declare const SNAP_ERROR_WRAPPER_CODE = -31001;
|
|
6
7
|
export declare const SNAP_ERROR_WRAPPER_MESSAGE = "Wrapped Snap Error";
|
|
@@ -82,4 +83,5 @@ export declare function isWrappedSnapError(error: unknown): error is SerializedS
|
|
|
82
83
|
* whether the error was handled.
|
|
83
84
|
*/
|
|
84
85
|
export declare function unwrapError(error: unknown): [error: RpcError<DataWithOptionalCause>, isHandled: boolean];
|
|
86
|
+
export declare const TrackableErrorStruct: Struct<TrackableError>;
|
|
85
87
|
//# sourceMappingURL=errors.d.mts.map
|
package/dist/errors.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.mts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,IAAI,QAAQ,EAEzB,6BAA6B;AAC9B,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B;AAClE,OAAO,KA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AC,KAAK,EAAE,QAAQ,CAAC,qBAAqB,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,CA+D9D;AAED,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,cAAc,CAKtD,CAAC"}
|
package/dist/errors.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { errorCodes, JsonRpcError as RpcError, serializeCause } from "@metamask/rpc-errors";
|
|
2
2
|
import { getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE } from "@metamask/snaps-sdk";
|
|
3
|
+
import { lazy, nullable, object, string } from "@metamask/superstruct";
|
|
3
4
|
import { isObject, isJsonRpcError } from "@metamask/utils";
|
|
4
5
|
export const SNAP_ERROR_WRAPPER_CODE = -31001;
|
|
5
6
|
export const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
|
|
@@ -176,4 +177,10 @@ export function unwrapError(error) {
|
|
|
176
177
|
false,
|
|
177
178
|
];
|
|
178
179
|
}
|
|
180
|
+
export const TrackableErrorStruct = object({
|
|
181
|
+
name: string(),
|
|
182
|
+
message: string(),
|
|
183
|
+
stack: nullable(string()),
|
|
184
|
+
cause: nullable(lazy(() => TrackableErrorStruct)),
|
|
185
|
+
});
|
|
179
186
|
//# sourceMappingURL=errors.mjs.map
|
package/dist/errors.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,YAAY,IAAI,QAAQ,EACxB,cAAc,EACf,6BAA6B;AAG9B,OAAO,EACL,eAAe,EACf,aAAa,EACb,eAAe,EACf,kBAAkB,EACnB,4BAA4B;AAE7B,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,wBAAwB;AAE3D,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,KAAK,CAAC;AAC9C,MAAM,CAAC,MAAM,0BAA0B,GAAG,oB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C,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) ?? undefined;\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 | null,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack ?? undefined;\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"]}
|
|
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;AAO9B,OAAO,EACL,eAAe,EACf,aAAa,EACb,eAAe,EACf,kBAAkB,EACnB,4BAA4B;AAE7B,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B;AAEvE,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,wBAAwB;AAE3D,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,KAAK,CAAC;AAC9C,MAAM,CAAC,MAAM,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAU;IAEhB,QAAQ,CAAS;IAEjB,MAAM,CAAU;IAEzB;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC;IAClD,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,kBAAkB,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;OAIG;IAC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AC,CAAC;CAClE,CAAC,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 {\n SerializedSnapError,\n SnapError,\n TrackableError,\n} from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Struct } from '@metamask/superstruct';\nimport { lazy, nullable, object, string } from '@metamask/superstruct';\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) ?? undefined;\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 | null,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack ?? undefined;\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\nexport const TrackableErrorStruct: Struct<TrackableError> = object({\n name: string(),\n message: string(),\n stack: nullable(string()),\n cause: nullable(lazy<TrackableError>(() => TrackableErrorStruct)),\n});\n"]}
|
|
@@ -68,10 +68,12 @@ const MANIFEST_SORT_ORDER = {
|
|
|
68
68
|
* handlers and their respective permission name. This must be provided to avoid
|
|
69
69
|
* circular dependencies between `@metamask/snaps-utils` and
|
|
70
70
|
* `@metamask/snaps-rpc-methods`.
|
|
71
|
+
* @param options.watchMode - Whether the compiler is running in watch mode.
|
|
72
|
+
* This is used to determine whether to fix warnings or errors only.
|
|
71
73
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
72
74
|
* were encountered during processing of the manifest files.
|
|
73
75
|
*/
|
|
74
|
-
async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs_1.promises.writeFile, exports, handlerEndowments, } = {}) {
|
|
76
|
+
async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs_1.promises.writeFile, exports, handlerEndowments, watchMode = false, } = {}) {
|
|
75
77
|
const manifestPath = path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest);
|
|
76
78
|
const manifestFile = await (0, fs_2.readJsonFile)(manifestPath);
|
|
77
79
|
const unvalidatedManifest = manifestFile.result;
|
|
@@ -103,8 +105,8 @@ async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCo
|
|
|
103
105
|
files: validatorResults.files,
|
|
104
106
|
reports: validatorResults.reports,
|
|
105
107
|
};
|
|
106
|
-
if (updateAndWriteManifest && (0, validator_1.hasFixes)(manifestResults)) {
|
|
107
|
-
const fixedResults = await runFixes(validatorResults);
|
|
108
|
+
if (updateAndWriteManifest && (0, validator_1.hasFixes)(manifestResults, watchMode)) {
|
|
109
|
+
const fixedResults = await runFixes(validatorResults, undefined, watchMode);
|
|
108
110
|
if (fixedResults.updated) {
|
|
109
111
|
manifestResults = fixedResults;
|
|
110
112
|
(0, utils_1.assert)(manifestResults.files);
|
|
@@ -132,9 +134,10 @@ exports.checkManifest = checkManifest;
|
|
|
132
134
|
*
|
|
133
135
|
* @param results - Results of the initial run of validation.
|
|
134
136
|
* @param rules - Optional list of rules to run the fixes with.
|
|
137
|
+
* @param errorsOnly - Whether to only run fixes for errors, not warnings.
|
|
135
138
|
* @returns The updated manifest and whether it was updated.
|
|
136
139
|
*/
|
|
137
|
-
async function runFixes(results, rules) {
|
|
140
|
+
async function runFixes(results, rules, errorsOnly = false) {
|
|
138
141
|
let shouldRunFixes = true;
|
|
139
142
|
const MAX_ATTEMPTS = 10;
|
|
140
143
|
(0, utils_1.assert)(results.files);
|
|
@@ -144,7 +147,7 @@ async function runFixes(results, rules) {
|
|
|
144
147
|
for (let attempts = 1; shouldRunFixes && attempts <= MAX_ATTEMPTS; attempts++) {
|
|
145
148
|
(0, utils_1.assert)(fixResults.files);
|
|
146
149
|
let manifest = fixResults.files.manifest.result;
|
|
147
|
-
const fixable = fixResults.reports.filter((report) =>
|
|
150
|
+
const fixable = fixResults.reports.filter((report) => (0, validator_1.isReportFixable)(report, errorsOnly));
|
|
148
151
|
for (const report of fixable) {
|
|
149
152
|
(0, utils_1.assert)(report.fix);
|
|
150
153
|
({ manifest } = await report.fix({ manifest }));
|
|
@@ -152,7 +155,7 @@ async function runFixes(results, rules) {
|
|
|
152
155
|
fixResults.files.manifest.value = `${JSON.stringify(getWritableManifest(manifest), null, 2)}\n`;
|
|
153
156
|
fixResults.files.manifest.result = manifest;
|
|
154
157
|
fixResults = await (0, validator_1.runValidators)(fixResults.files, rules);
|
|
155
|
-
shouldRunFixes = (0, validator_1.hasFixes)(fixResults);
|
|
158
|
+
shouldRunFixes = (0, validator_1.hasFixes)(fixResults, errorsOnly);
|
|
156
159
|
}
|
|
157
160
|
const initialReports = (0, deep_clone_1.deepClone)(results.reports);
|
|
158
161
|
// Was fixed
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAsD;AAEtD,2CAAwD;AACxD,2BAAoC;AACpC,gDAA6B;AAI7B,+CAAsD;AAEtD,0EAAkD;AAClD,kDAA0C;AAC1C,kCAAqC;AACrC,sCAAoC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;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAC,QAAsB;IACxD,MAAM,EAAE,UAAU,EAAE,GAAG,SAAS,EAAE,GAAG,QAAQ,CAAC;IAE9C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CACtB,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,SAAS,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,SAAS,CAC5B,CAAC;IAE5B,MAAM,gBAAgB,GAAG,IAAI;SAC1B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAAC;SAC/D,MAAM,CACL,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QAChB,GAAG,MAAM;QACT,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;KACrB,CAAC,EACF,EAAE,CACH,CAAC;IAEJ,OAAO,gBAAgC,CAAC;AAC1C,CAAC;AAlBD,kDAkBC","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\nimport { assert, isPlainObject } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport type { SnapManifest } from './validation';\nimport type { ValidatorResults } from './validator';\nimport { hasFixes, runValidators } from './validator';\nimport type { ValidatorMeta, ValidatorReport } from './validator-types';\nimport * as defaultValidators from './validators';\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { parseJson } from '../json';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\nimport { NpmSnapFileNames } from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file/node';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n version: 2,\n description: 3,\n proposedName: 4,\n repository: 5,\n source: 6,\n initialConnections: 7,\n initialPermissions: 8,\n platformVersion: 9,\n manifestVersion: 10,\n};\n\nexport type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {\n wasFixed?: boolean;\n};\n\n/**\n * The options for the `checkManifest` function.\n */\nexport type CheckManifestOptions = {\n /**\n * Whether to auto-magically try to fix errors and then write the manifest to\n * disk.\n */\n updateAndWriteManifest?: boolean;\n\n /**\n * The source code of the Snap.\n */\n sourceCode?: string;\n\n /**\n * The function to use to write the manifest to disk.\n */\n writeFileFn?: WriteFileFunction;\n\n /**\n * The exports detected by evaluating the bundle. This may be used by one or\n * more validators to determine whether the Snap is valid.\n */\n exports?: string[];\n\n /**\n * An object containing the names of the handlers and their respective\n * permission name. This must be provided to avoid circular dependencies\n * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.\n */\n handlerEndowments?: Record<string, string | null>;\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was written and updated.\n */\nexport type CheckManifestResult = {\n files?: SnapFiles;\n updated: boolean;\n reports: CheckManifestReport[];\n};\n\nexport type WriteFileFunction = (path: string, data: string) => Promise<void>;\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param options - Additional options for the function.\n * @param options.sourceCode - The source code of the Snap.\n * @param options.writeFileFn - The function to use to write the manifest to\n * disk.\n * @param options.updateAndWriteManifest - Whether to auto-magically try to fix\n * errors and then write the manifest to disk.\n * @param options.exports - The exports detected by evaluating the bundle. This\n * may be used by one or more validators to determine whether the Snap is valid.\n * @param options.handlerEndowments - An object containing the names of the\n * handlers and their respective permission name. This must be provided to avoid\n * circular dependencies between `@metamask/snaps-utils` and\n * `@metamask/snaps-rpc-methods`.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n {\n updateAndWriteManifest = true,\n sourceCode,\n writeFileFn = fs.writeFile,\n exports,\n handlerEndowments,\n }: CheckManifestOptions = {},\n): Promise<CheckManifestResult> {\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const auxiliaryFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.files,\n );\n\n const localizationFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.locales,\n );\n const localizationFiles =\n (await getSnapFiles(basePath, localizationFilePaths)) ?? [];\n for (const localization of localizationFiles) {\n try {\n localization.result = parseJson(localization.toString());\n } catch (error) {\n assert(error instanceof SyntaxError, error);\n throw new Error(\n `Failed to parse localization file \"${localization.path}\" as JSON.`,\n );\n }\n }\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n // Intentionally pass null as the encoding here since the files may be binary\n auxiliaryFiles:\n (await getSnapFiles(basePath, auxiliaryFilePaths, null)) ?? [],\n localizationFiles,\n };\n\n const validatorResults = await runValidators(\n snapFiles,\n Object.values(defaultValidators),\n { exports, handlerEndowments },\n );\n\n let manifestResults: CheckManifestResult = {\n updated: false,\n files: validatorResults.files,\n reports: validatorResults.reports,\n };\n\n if (updateAndWriteManifest && hasFixes(manifestResults)) {\n const fixedResults = await runFixes(validatorResults);\n\n if (fixedResults.updated) {\n manifestResults = fixedResults;\n\n assert(manifestResults.files);\n\n try {\n await writeFileFn(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n manifestResults.files.manifest.toString(),\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(\n `Failed to update \"snap.manifest.json\": ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n return manifestResults;\n}\n\n/**\n * Run the algorithm for automatically fixing errors in manifest.\n *\n * The algorithm updates the manifest by fixing all fixable problems,\n * and then run validation again to check if the new manifest is now correct.\n * If not correct, the algorithm will use the manifest from previous iteration\n * and try again `MAX_ATTEMPTS` times to update it before bailing and\n * resulting in failure.\n *\n * @param results - Results of the initial run of validation.\n * @param rules - Optional list of rules to run the fixes with.\n * @returns The updated manifest and whether it was updated.\n */\nexport async function runFixes(\n results: ValidatorResults,\n rules?: ValidatorMeta[],\n): Promise<CheckManifestResult> {\n let shouldRunFixes = true;\n const MAX_ATTEMPTS = 10;\n\n assert(results.files);\n\n let fixResults: ValidatorResults = results;\n assert(fixResults.files);\n fixResults.files.manifest = fixResults.files.manifest.clone();\n\n for (\n let attempts = 1;\n shouldRunFixes && attempts <= MAX_ATTEMPTS;\n attempts++\n ) {\n assert(fixResults.files);\n\n let manifest = fixResults.files.manifest.result;\n\n const fixable = fixResults.reports.filter((report) => report.fix);\n for (const report of fixable) {\n assert(report.fix);\n ({ manifest } = await report.fix({ manifest }));\n }\n\n fixResults.files.manifest.value = `${JSON.stringify(\n getWritableManifest(manifest),\n null,\n 2,\n )}\\n`;\n fixResults.files.manifest.result = manifest;\n\n fixResults = await runValidators(fixResults.files, rules);\n shouldRunFixes = hasFixes(fixResults);\n }\n\n const initialReports: (CheckManifestReport & ValidatorReport)[] = deepClone(\n results.reports,\n );\n\n // Was fixed\n if (!shouldRunFixes) {\n for (const report of initialReports) {\n if (report.fix) {\n report.wasFixed = true;\n delete report.fix;\n }\n }\n\n return {\n files: fixResults.files,\n updated: true,\n reports: initialReports,\n };\n }\n\n for (const report of initialReports) {\n delete report.fix;\n }\n\n return {\n files: results.files,\n updated: false,\n reports: initialReports,\n };\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(\n `Failed to read snap bundle file: ${getErrorMessage(error)}`,\n );\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read snap icon file: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Get an array of paths from an unvalidated Snap manifest.\n *\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param selector - A function that returns the paths to the files.\n * @returns The paths to the files, if any.\n */\nexport function getSnapFilePaths(\n manifest: Json,\n selector: (manifest: Partial<SnapManifest>) => string[] | undefined,\n) {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const snapManifest = manifest as Partial<SnapManifest>;\n const paths = selector(snapManifest);\n\n if (!Array.isArray(paths)) {\n return undefined;\n }\n\n return paths;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the files with the\n * given paths and read them.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param paths - The paths to the files.\n * @param encoding - An optional encoding to pass down to readVirtualFile.\n * @returns A list of auxiliary files and their contents, if any.\n */\nexport async function getSnapFiles(\n basePath: string,\n paths: string[] | undefined,\n encoding: BufferEncoding | null = 'utf8',\n): Promise<VirtualFile[] | undefined> {\n if (!paths) {\n return undefined;\n }\n\n try {\n return await Promise.all(\n paths.map(async (filePath) =>\n readVirtualFile(pathUtils.join(basePath, filePath), encoding),\n ),\n );\n } catch (error) {\n throw new Error(`Failed to read snap files: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAsD;AAEtD,2CAAwD;AACxD,2BAAoC;AACpC,gDAA6B;AAI7B,+CAAuE;AAEvE,0EAAkD;AAClD,kDAA0C;AAC1C,kCAAqC;AACrC,sCAAoC;AAEpC,wCAA4C;AAC5C,mDAAoE;AAEpE,MAAM,mBAAmB,GAAuC;IAC9D,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;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mB,CAAC,QAAsB;IACxD,MAAM,EAAE,UAAU,EAAE,GAAG,SAAS,EAAE,GAAG,QAAQ,CAAC;IAE9C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CACtB,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,SAAS,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,SAAS,CAC5B,CAAC;IAE5B,MAAM,gBAAgB,GAAG,IAAI;SAC1B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAAC;SAC/D,MAAM,CACL,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QAChB,GAAG,MAAM;QACT,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;KACrB,CAAC,EACF,EAAE,CACH,CAAC;IAEJ,OAAO,gBAAgC,CAAC;AAC1C,CAAC;AAlBD,kDAkBC","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\nimport { assert, isPlainObject } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport type { SnapManifest } from './validation';\nimport type { ValidatorResults } from './validator';\nimport { isReportFixable, hasFixes, runValidators } from './validator';\nimport type { ValidatorMeta, ValidatorReport } from './validator-types';\nimport * as defaultValidators from './validators';\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { parseJson } from '../json';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\nimport { NpmSnapFileNames } from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file/node';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n version: 2,\n description: 3,\n proposedName: 4,\n repository: 5,\n source: 6,\n initialConnections: 7,\n initialPermissions: 8,\n platformVersion: 9,\n manifestVersion: 10,\n};\n\nexport type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {\n wasFixed?: boolean;\n};\n\n/**\n * The options for the `checkManifest` function.\n */\nexport type CheckManifestOptions = {\n /**\n * Whether to auto-magically try to fix errors and then write the manifest to\n * disk.\n */\n updateAndWriteManifest?: boolean;\n\n /**\n * The source code of the Snap.\n */\n sourceCode?: string;\n\n /**\n * The function to use to write the manifest to disk.\n */\n writeFileFn?: WriteFileFunction;\n\n /**\n * The exports detected by evaluating the bundle. This may be used by one or\n * more validators to determine whether the Snap is valid.\n */\n exports?: string[];\n\n /**\n * An object containing the names of the handlers and their respective\n * permission name. This must be provided to avoid circular dependencies\n * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.\n */\n handlerEndowments?: Record<string, string | null>;\n\n /**\n * Whether the compiler is running in watch mode. This is used to determine\n * whether to fix warnings or errors only.\n */\n watchMode?: boolean;\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was written and updated.\n */\nexport type CheckManifestResult = {\n files?: SnapFiles;\n updated: boolean;\n reports: CheckManifestReport[];\n};\n\nexport type WriteFileFunction = (path: string, data: string) => Promise<void>;\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param options - Additional options for the function.\n * @param options.sourceCode - The source code of the Snap.\n * @param options.writeFileFn - The function to use to write the manifest to\n * disk.\n * @param options.updateAndWriteManifest - Whether to auto-magically try to fix\n * errors and then write the manifest to disk.\n * @param options.exports - The exports detected by evaluating the bundle. This\n * may be used by one or more validators to determine whether the Snap is valid.\n * @param options.handlerEndowments - An object containing the names of the\n * handlers and their respective permission name. This must be provided to avoid\n * circular dependencies between `@metamask/snaps-utils` and\n * `@metamask/snaps-rpc-methods`.\n * @param options.watchMode - Whether the compiler is running in watch mode.\n * This is used to determine whether to fix warnings or errors only.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n {\n updateAndWriteManifest = true,\n sourceCode,\n writeFileFn = fs.writeFile,\n exports,\n handlerEndowments,\n watchMode = false,\n }: CheckManifestOptions = {},\n): Promise<CheckManifestResult> {\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const auxiliaryFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.files,\n );\n\n const localizationFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.locales,\n );\n const localizationFiles =\n (await getSnapFiles(basePath, localizationFilePaths)) ?? [];\n for (const localization of localizationFiles) {\n try {\n localization.result = parseJson(localization.toString());\n } catch (error) {\n assert(error instanceof SyntaxError, error);\n throw new Error(\n `Failed to parse localization file \"${localization.path}\" as JSON.`,\n );\n }\n }\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n // Intentionally pass null as the encoding here since the files may be binary\n auxiliaryFiles:\n (await getSnapFiles(basePath, auxiliaryFilePaths, null)) ?? [],\n localizationFiles,\n };\n\n const validatorResults = await runValidators(\n snapFiles,\n Object.values(defaultValidators),\n { exports, handlerEndowments },\n );\n\n let manifestResults: CheckManifestResult = {\n updated: false,\n files: validatorResults.files,\n reports: validatorResults.reports,\n };\n\n if (updateAndWriteManifest && hasFixes(manifestResults, watchMode)) {\n const fixedResults = await runFixes(validatorResults, undefined, watchMode);\n\n if (fixedResults.updated) {\n manifestResults = fixedResults;\n\n assert(manifestResults.files);\n\n try {\n await writeFileFn(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n manifestResults.files.manifest.toString(),\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(\n `Failed to update \"snap.manifest.json\": ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n return manifestResults;\n}\n\n/**\n * Run the algorithm for automatically fixing errors in manifest.\n *\n * The algorithm updates the manifest by fixing all fixable problems,\n * and then run validation again to check if the new manifest is now correct.\n * If not correct, the algorithm will use the manifest from previous iteration\n * and try again `MAX_ATTEMPTS` times to update it before bailing and\n * resulting in failure.\n *\n * @param results - Results of the initial run of validation.\n * @param rules - Optional list of rules to run the fixes with.\n * @param errorsOnly - Whether to only run fixes for errors, not warnings.\n * @returns The updated manifest and whether it was updated.\n */\nexport async function runFixes(\n results: ValidatorResults,\n rules?: ValidatorMeta[],\n errorsOnly = false,\n): Promise<CheckManifestResult> {\n let shouldRunFixes = true;\n const MAX_ATTEMPTS = 10;\n\n assert(results.files);\n\n let fixResults: ValidatorResults = results;\n assert(fixResults.files);\n fixResults.files.manifest = fixResults.files.manifest.clone();\n\n for (\n let attempts = 1;\n shouldRunFixes && attempts <= MAX_ATTEMPTS;\n attempts++\n ) {\n assert(fixResults.files);\n\n let manifest = fixResults.files.manifest.result;\n\n const fixable = fixResults.reports.filter((report) =>\n isReportFixable(report, errorsOnly),\n );\n\n for (const report of fixable) {\n assert(report.fix);\n ({ manifest } = await report.fix({ manifest }));\n }\n\n fixResults.files.manifest.value = `${JSON.stringify(\n getWritableManifest(manifest),\n null,\n 2,\n )}\\n`;\n fixResults.files.manifest.result = manifest;\n\n fixResults = await runValidators(fixResults.files, rules);\n shouldRunFixes = hasFixes(fixResults, errorsOnly);\n }\n\n const initialReports: (CheckManifestReport & ValidatorReport)[] = deepClone(\n results.reports,\n );\n\n // Was fixed\n if (!shouldRunFixes) {\n for (const report of initialReports) {\n if (report.fix) {\n report.wasFixed = true;\n delete report.fix;\n }\n }\n\n return {\n files: fixResults.files,\n updated: true,\n reports: initialReports,\n };\n }\n\n for (const report of initialReports) {\n delete report.fix;\n }\n\n return {\n files: results.files,\n updated: false,\n reports: initialReports,\n };\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(\n `Failed to read snap bundle file: ${getErrorMessage(error)}`,\n );\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read snap icon file: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Get an array of paths from an unvalidated Snap manifest.\n *\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param selector - A function that returns the paths to the files.\n * @returns The paths to the files, if any.\n */\nexport function getSnapFilePaths(\n manifest: Json,\n selector: (manifest: Partial<SnapManifest>) => string[] | undefined,\n) {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const snapManifest = manifest as Partial<SnapManifest>;\n const paths = selector(snapManifest);\n\n if (!Array.isArray(paths)) {\n return undefined;\n }\n\n return paths;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the files with the\n * given paths and read them.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param paths - The paths to the files.\n * @param encoding - An optional encoding to pass down to readVirtualFile.\n * @returns A list of auxiliary files and their contents, if any.\n */\nexport async function getSnapFiles(\n basePath: string,\n paths: string[] | undefined,\n encoding: BufferEncoding | null = 'utf8',\n): Promise<VirtualFile[] | undefined> {\n if (!paths) {\n return undefined;\n }\n\n try {\n return await Promise.all(\n paths.map(async (filePath) =>\n readVirtualFile(pathUtils.join(basePath, filePath), encoding),\n ),\n );\n } catch (error) {\n throw new Error(`Failed to read snap files: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n"]}
|
|
@@ -36,6 +36,11 @@ export type CheckManifestOptions = {
|
|
|
36
36
|
* between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.
|
|
37
37
|
*/
|
|
38
38
|
handlerEndowments?: Record<string, string | null>;
|
|
39
|
+
/**
|
|
40
|
+
* Whether the compiler is running in watch mode. This is used to determine
|
|
41
|
+
* whether to fix warnings or errors only.
|
|
42
|
+
*/
|
|
43
|
+
watchMode?: boolean;
|
|
39
44
|
};
|
|
40
45
|
/**
|
|
41
46
|
* The result from the `checkManifest` function.
|
|
@@ -67,10 +72,12 @@ export type WriteFileFunction = (path: string, data: string) => Promise<void>;
|
|
|
67
72
|
* handlers and their respective permission name. This must be provided to avoid
|
|
68
73
|
* circular dependencies between `@metamask/snaps-utils` and
|
|
69
74
|
* `@metamask/snaps-rpc-methods`.
|
|
75
|
+
* @param options.watchMode - Whether the compiler is running in watch mode.
|
|
76
|
+
* This is used to determine whether to fix warnings or errors only.
|
|
70
77
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
71
78
|
* were encountered during processing of the manifest files.
|
|
72
79
|
*/
|
|
73
|
-
export declare function checkManifest(basePath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, }?: CheckManifestOptions): Promise<CheckManifestResult>;
|
|
80
|
+
export declare function checkManifest(basePath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, watchMode, }?: CheckManifestOptions): Promise<CheckManifestResult>;
|
|
74
81
|
/**
|
|
75
82
|
* Run the algorithm for automatically fixing errors in manifest.
|
|
76
83
|
*
|
|
@@ -82,9 +89,10 @@ export declare function checkManifest(basePath: string, { updateAndWriteManifest
|
|
|
82
89
|
*
|
|
83
90
|
* @param results - Results of the initial run of validation.
|
|
84
91
|
* @param rules - Optional list of rules to run the fixes with.
|
|
92
|
+
* @param errorsOnly - Whether to only run fixes for errors, not warnings.
|
|
85
93
|
* @returns The updated manifest and whether it was updated.
|
|
86
94
|
*/
|
|
87
|
-
export declare function runFixes(results: ValidatorResults, rules?: ValidatorMeta[]): Promise<CheckManifestResult>;
|
|
95
|
+
export declare function runFixes(results: ValidatorResults, rules?: ValidatorMeta[], errorsOnly?: boolean): Promise<CheckManifestResult>;
|
|
88
96
|
/**
|
|
89
97
|
* Given an unvalidated Snap manifest, attempts to extract the location of the
|
|
90
98
|
* bundle source file location and read the file.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAK5C,OAAO,KAAK,EAAE,YAAY,EAAE,yBAAqB;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,wBAAoB;AAEpD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,8BAA0B;AAKxE,OAAO,KAAK,EAAE,SAAS,EAAwB,qBAAiB;AAEhE,OAAO,EAAmB,WAAW,EAAE,iCAA6B;AAepE,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,eAAe,EAAE,KAAK,CAAC,GAAG;IAC/D,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;OAGG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;IAEjC;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAEhC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAK5C,OAAO,KAAK,EAAE,YAAY,EAAE,yBAAqB;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,wBAAoB;AAEpD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,8BAA0B;AAKxE,OAAO,KAAK,EAAE,SAAS,EAAwB,qBAAiB;AAEh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mBAAmB,CAAC,CAoE9B;AAED;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,IAAI,EACd,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CA8BlC;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,IAAI,GACb,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAqBlC;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,IAAI,EACd,QAAQ,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,YAAY,CAAC,KAAK,MAAM,EAAE,GAAG,SAAS,wBAcpE;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EAAE,GAAG,SAAS,EAC3B,QAAQ,GAAE,cAAc,GAAG,IAAa,GACvC,OAAO,CAAC,WAAW,EAAE,GAAG,SAAS,CAAC,CAcpC;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,YAAY,GAAG,YAAY,CAkBxE"}
|
|
@@ -36,6 +36,11 @@ export type CheckManifestOptions = {
|
|
|
36
36
|
* between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.
|
|
37
37
|
*/
|
|
38
38
|
handlerEndowments?: Record<string, string | null>;
|
|
39
|
+
/**
|
|
40
|
+
* Whether the compiler is running in watch mode. This is used to determine
|
|
41
|
+
* whether to fix warnings or errors only.
|
|
42
|
+
*/
|
|
43
|
+
watchMode?: boolean;
|
|
39
44
|
};
|
|
40
45
|
/**
|
|
41
46
|
* The result from the `checkManifest` function.
|
|
@@ -67,10 +72,12 @@ export type WriteFileFunction = (path: string, data: string) => Promise<void>;
|
|
|
67
72
|
* handlers and their respective permission name. This must be provided to avoid
|
|
68
73
|
* circular dependencies between `@metamask/snaps-utils` and
|
|
69
74
|
* `@metamask/snaps-rpc-methods`.
|
|
75
|
+
* @param options.watchMode - Whether the compiler is running in watch mode.
|
|
76
|
+
* This is used to determine whether to fix warnings or errors only.
|
|
70
77
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
71
78
|
* were encountered during processing of the manifest files.
|
|
72
79
|
*/
|
|
73
|
-
export declare function checkManifest(basePath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, }?: CheckManifestOptions): Promise<CheckManifestResult>;
|
|
80
|
+
export declare function checkManifest(basePath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, watchMode, }?: CheckManifestOptions): Promise<CheckManifestResult>;
|
|
74
81
|
/**
|
|
75
82
|
* Run the algorithm for automatically fixing errors in manifest.
|
|
76
83
|
*
|
|
@@ -82,9 +89,10 @@ export declare function checkManifest(basePath: string, { updateAndWriteManifest
|
|
|
82
89
|
*
|
|
83
90
|
* @param results - Results of the initial run of validation.
|
|
84
91
|
* @param rules - Optional list of rules to run the fixes with.
|
|
92
|
+
* @param errorsOnly - Whether to only run fixes for errors, not warnings.
|
|
85
93
|
* @returns The updated manifest and whether it was updated.
|
|
86
94
|
*/
|
|
87
|
-
export declare function runFixes(results: ValidatorResults, rules?: ValidatorMeta[]): Promise<CheckManifestResult>;
|
|
95
|
+
export declare function runFixes(results: ValidatorResults, rules?: ValidatorMeta[], errorsOnly?: boolean): Promise<CheckManifestResult>;
|
|
88
96
|
/**
|
|
89
97
|
* Given an unvalidated Snap manifest, attempts to extract the location of the
|
|
90
98
|
* bundle source file location and read the file.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.mts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAK5C,OAAO,KAAK,EAAE,YAAY,EAAE,yBAAqB;AACjD,OAAO,KAAK,EAAE,gBAA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QAAQ,EAAE,IAAI,GACb,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAqBlC;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,IAAI,EACd,QAAQ,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,YAAY,CAAC,KAAK,MAAM,EAAE,GAAG,SAAS,wBAcpE;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EAAE,GAAG,SAAS,EAC3B,QAAQ,GAAE,cAAc,GAAG,IAAa,GACvC,OAAO,CAAC,WAAW,EAAE,GAAG,SAAS,CAAC,CAcpC;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,YAAY,GAAG,YAAY,CAkBxE"}
|
|
@@ -2,7 +2,7 @@ import { getErrorMessage } from "@metamask/snaps-sdk";
|
|
|
2
2
|
import { assert, isPlainObject } from "@metamask/utils";
|
|
3
3
|
import { promises as fs } from "fs";
|
|
4
4
|
import pathUtils from "path";
|
|
5
|
-
import { hasFixes, runValidators } from "./validator.mjs";
|
|
5
|
+
import { isReportFixable, hasFixes, runValidators } from "./validator.mjs";
|
|
6
6
|
import * as defaultValidators from "./validators/index.mjs";
|
|
7
7
|
import { deepClone } from "../deep-clone.mjs";
|
|
8
8
|
import { readJsonFile } from "../fs.mjs";
|
|
@@ -39,10 +39,12 @@ const MANIFEST_SORT_ORDER = {
|
|
|
39
39
|
* handlers and their respective permission name. This must be provided to avoid
|
|
40
40
|
* circular dependencies between `@metamask/snaps-utils` and
|
|
41
41
|
* `@metamask/snaps-rpc-methods`.
|
|
42
|
+
* @param options.watchMode - Whether the compiler is running in watch mode.
|
|
43
|
+
* This is used to determine whether to fix warnings or errors only.
|
|
42
44
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
43
45
|
* were encountered during processing of the manifest files.
|
|
44
46
|
*/
|
|
45
|
-
export async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs.writeFile, exports, handlerEndowments, } = {}) {
|
|
47
|
+
export async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs.writeFile, exports, handlerEndowments, watchMode = false, } = {}) {
|
|
46
48
|
const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);
|
|
47
49
|
const manifestFile = await readJsonFile(manifestPath);
|
|
48
50
|
const unvalidatedManifest = manifestFile.result;
|
|
@@ -74,8 +76,8 @@ export async function checkManifest(basePath, { updateAndWriteManifest = true, s
|
|
|
74
76
|
files: validatorResults.files,
|
|
75
77
|
reports: validatorResults.reports,
|
|
76
78
|
};
|
|
77
|
-
if (updateAndWriteManifest && hasFixes(manifestResults)) {
|
|
78
|
-
const fixedResults = await runFixes(validatorResults);
|
|
79
|
+
if (updateAndWriteManifest && hasFixes(manifestResults, watchMode)) {
|
|
80
|
+
const fixedResults = await runFixes(validatorResults, undefined, watchMode);
|
|
79
81
|
if (fixedResults.updated) {
|
|
80
82
|
manifestResults = fixedResults;
|
|
81
83
|
assert(manifestResults.files);
|
|
@@ -102,9 +104,10 @@ export async function checkManifest(basePath, { updateAndWriteManifest = true, s
|
|
|
102
104
|
*
|
|
103
105
|
* @param results - Results of the initial run of validation.
|
|
104
106
|
* @param rules - Optional list of rules to run the fixes with.
|
|
107
|
+
* @param errorsOnly - Whether to only run fixes for errors, not warnings.
|
|
105
108
|
* @returns The updated manifest and whether it was updated.
|
|
106
109
|
*/
|
|
107
|
-
export async function runFixes(results, rules) {
|
|
110
|
+
export async function runFixes(results, rules, errorsOnly = false) {
|
|
108
111
|
let shouldRunFixes = true;
|
|
109
112
|
const MAX_ATTEMPTS = 10;
|
|
110
113
|
assert(results.files);
|
|
@@ -114,7 +117,7 @@ export async function runFixes(results, rules) {
|
|
|
114
117
|
for (let attempts = 1; shouldRunFixes && attempts <= MAX_ATTEMPTS; attempts++) {
|
|
115
118
|
assert(fixResults.files);
|
|
116
119
|
let manifest = fixResults.files.manifest.result;
|
|
117
|
-
const fixable = fixResults.reports.filter((report) => report
|
|
120
|
+
const fixable = fixResults.reports.filter((report) => isReportFixable(report, errorsOnly));
|
|
118
121
|
for (const report of fixable) {
|
|
119
122
|
assert(report.fix);
|
|
120
123
|
({ manifest } = await report.fix({ manifest }));
|
|
@@ -122,7 +125,7 @@ export async function runFixes(results, rules) {
|
|
|
122
125
|
fixResults.files.manifest.value = `${JSON.stringify(getWritableManifest(manifest), null, 2)}\n`;
|
|
123
126
|
fixResults.files.manifest.result = manifest;
|
|
124
127
|
fixResults = await runValidators(fixResults.files, rules);
|
|
125
|
-
shouldRunFixes = hasFixes(fixResults);
|
|
128
|
+
shouldRunFixes = hasFixes(fixResults, errorsOnly);
|
|
126
129
|
}
|
|
127
130
|
const initialReports = deepClone(results.reports);
|
|
128
131
|
// Was fixed
|