@metamask/snaps-utils 0.35.1-flask.1 → 1.0.0-prerelease.1
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 +5 -46
- package/dist/{esm/array.js → array.js} +7 -3
- package/dist/array.js.map +1 -0
- package/dist/{types/caveats.d.ts → caveats.d.ts} +4 -0
- package/dist/caveats.js +35 -0
- package/dist/caveats.js.map +1 -0
- package/dist/checksum.js +42 -0
- package/dist/checksum.js.map +1 -0
- package/dist/cronjob.js +71 -0
- package/dist/cronjob.js.map +1 -0
- package/dist/deep-clone.js +9 -0
- package/dist/deep-clone.js.map +1 -0
- package/dist/{esm/default-endowments.js → default-endowments.js} +6 -3
- package/dist/default-endowments.js.map +1 -0
- package/dist/entropy.js +8 -0
- package/dist/entropy.js.map +1 -0
- package/dist/{cjs/eval-worker.js → eval-worker.js} +21 -22
- package/dist/eval-worker.js.map +1 -0
- package/dist/eval.js +27 -0
- package/dist/eval.js.map +1 -0
- package/dist/{esm/fs.js → fs.js} +56 -33
- package/dist/fs.js.map +1 -0
- package/dist/{types/handlers.d.ts → handlers.d.ts} +42 -5
- package/dist/handlers.js +3 -0
- package/dist/handlers.js.map +1 -0
- package/dist/{types/iframe.d.ts → iframe.d.ts} +2 -3
- package/dist/{esm/iframe.js → iframe.js} +18 -16
- package/dist/iframe.js.map +1 -0
- package/dist/iframe.test.browser.js +15 -0
- package/dist/iframe.test.browser.js.map +1 -0
- package/dist/{types/index.browser.d.ts → index.browser.d.ts} +1 -2
- package/dist/index.browser.js +37 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/{types/index.d.ts → index.d.ts} +1 -2
- package/dist/index.executionenv.js +22 -0
- package/dist/index.executionenv.js.map +1 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +1 -0
- package/dist/json-rpc.js +46 -0
- package/dist/json-rpc.js.map +1 -0
- package/dist/json.d.ts +9 -0
- package/dist/json.js +18 -0
- package/dist/json.js.map +1 -0
- package/dist/{esm/logging.js → logging.js} +14 -6
- package/dist/logging.js.map +1 -0
- package/dist/manifest/index.browser.js +18 -0
- package/dist/manifest/index.browser.js.map +1 -0
- package/dist/manifest/index.js +19 -0
- package/dist/manifest/index.js.map +1 -0
- package/dist/{esm/manifest → manifest}/manifest.js +113 -98
- package/dist/manifest/manifest.js.map +1 -0
- package/dist/{types/manifest → manifest}/validation.d.ts +73 -31
- package/dist/manifest/validation.js +141 -0
- package/dist/manifest/validation.js.map +1 -0
- package/dist/{esm/mock.js → mock.js} +40 -42
- package/dist/mock.js.map +1 -0
- package/dist/namespace.d.ts +275 -0
- package/dist/namespace.js +225 -0
- package/dist/namespace.js.map +1 -0
- package/dist/notification.d.ts +66 -0
- package/dist/notification.js +58 -0
- package/dist/notification.js.map +1 -0
- package/dist/{types/npm.d.ts → npm.d.ts} +2 -0
- package/dist/npm.js +74 -0
- package/dist/npm.js.map +1 -0
- package/dist/path.js +21 -0
- package/dist/path.js.map +1 -0
- package/dist/{esm/post-process.js → post-process.js} +99 -110
- package/dist/post-process.js.map +1 -0
- package/dist/{types/snaps.d.ts → snaps.d.ts} +2 -9
- package/dist/snaps.js +202 -0
- package/dist/snaps.js.map +1 -0
- package/dist/{types/types.d.ts → types.d.ts} +5 -2
- package/dist/types.js +103 -0
- package/dist/types.js.map +1 -0
- package/dist/{esm/versions.js → versions.js} +18 -15
- package/dist/versions.js.map +1 -0
- package/dist/{esm/virtual-file → virtual-file}/VirtualFile.js +33 -47
- package/dist/virtual-file/VirtualFile.js.map +1 -0
- package/dist/virtual-file/index.browser.js +18 -0
- package/dist/virtual-file/index.browser.js.map +1 -0
- package/dist/virtual-file/index.js +19 -0
- package/dist/virtual-file/index.js.map +1 -0
- package/dist/virtual-file/toVirtualFile.js +30 -0
- package/dist/virtual-file/toVirtualFile.js.map +1 -0
- package/package.json +32 -61
- package/dist/cjs/array.js +0 -23
- package/dist/cjs/array.js.map +0 -1
- package/dist/cjs/caveats.js +0 -33
- package/dist/cjs/caveats.js.map +0 -1
- package/dist/cjs/checksum.js +0 -38
- package/dist/cjs/checksum.js.map +0 -1
- package/dist/cjs/cronjob.js +0 -84
- package/dist/cjs/cronjob.js.map +0 -1
- package/dist/cjs/deep-clone.js +0 -22
- package/dist/cjs/deep-clone.js.map +0 -1
- package/dist/cjs/default-endowments.js +0 -49
- package/dist/cjs/default-endowments.js.map +0 -1
- package/dist/cjs/entropy.js +0 -23
- package/dist/cjs/entropy.js.map +0 -1
- package/dist/cjs/enum.js +0 -16
- package/dist/cjs/enum.js.map +0 -1
- package/dist/cjs/eval-worker.js.map +0 -1
- package/dist/cjs/eval.js +0 -29
- package/dist/cjs/eval.js.map +0 -1
- package/dist/cjs/fs.js +0 -126
- package/dist/cjs/fs.js.map +0 -1
- package/dist/cjs/handlers.js +0 -6
- package/dist/cjs/handlers.js.map +0 -1
- package/dist/cjs/icon.js +0 -37
- package/dist/cjs/icon.js.map +0 -1
- package/dist/cjs/iframe.js +0 -59
- package/dist/cjs/iframe.js.map +0 -1
- package/dist/cjs/index.browser.js +0 -40
- package/dist/cjs/index.browser.js.map +0 -1
- package/dist/cjs/index.executionenv.js +0 -24
- package/dist/cjs/index.executionenv.js.map +0 -1
- package/dist/cjs/index.js +0 -45
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/json-rpc.js +0 -46
- package/dist/cjs/json-rpc.js.map +0 -1
- package/dist/cjs/json.js +0 -16
- package/dist/cjs/json.js.map +0 -1
- package/dist/cjs/logging.js +0 -40
- package/dist/cjs/logging.js.map +0 -1
- package/dist/cjs/manifest/index.browser.js +0 -20
- package/dist/cjs/manifest/index.browser.js.map +0 -1
- package/dist/cjs/manifest/index.js +0 -21
- package/dist/cjs/manifest/index.js.map +0 -1
- package/dist/cjs/manifest/manifest.js +0 -239
- package/dist/cjs/manifest/manifest.js.map +0 -1
- package/dist/cjs/manifest/validation.js +0 -183
- package/dist/cjs/manifest/validation.js.map +0 -1
- package/dist/cjs/mock.js +0 -128
- package/dist/cjs/mock.js.map +0 -1
- package/dist/cjs/namespace.js +0 -124
- package/dist/cjs/namespace.js.map +0 -1
- package/dist/cjs/npm.js +0 -81
- package/dist/cjs/npm.js.map +0 -1
- package/dist/cjs/path.js +0 -21
- package/dist/cjs/path.js.map +0 -1
- package/dist/cjs/post-process.js +0 -328
- package/dist/cjs/post-process.js.map +0 -1
- package/dist/cjs/snaps.js +0 -230
- package/dist/cjs/snaps.js.map +0 -1
- package/dist/cjs/types.js +0 -117
- package/dist/cjs/types.js.map +0 -1
- package/dist/cjs/validation.js +0 -22
- package/dist/cjs/validation.js.map +0 -1
- package/dist/cjs/versions.js +0 -47
- package/dist/cjs/versions.js.map +0 -1
- package/dist/cjs/virtual-file/VirtualFile.js +0 -85
- package/dist/cjs/virtual-file/VirtualFile.js.map +0 -1
- package/dist/cjs/virtual-file/index.browser.js +0 -20
- package/dist/cjs/virtual-file/index.browser.js.map +0 -1
- package/dist/cjs/virtual-file/index.js +0 -21
- package/dist/cjs/virtual-file/index.js.map +0 -1
- package/dist/cjs/virtual-file/toVirtualFile.js +0 -33
- package/dist/cjs/virtual-file/toVirtualFile.js.map +0 -1
- package/dist/esm/array.js.map +0 -1
- package/dist/esm/caveats.js +0 -23
- package/dist/esm/caveats.js.map +0 -1
- package/dist/esm/checksum.js +0 -36
- package/dist/esm/checksum.js.map +0 -1
- package/dist/esm/cronjob.js +0 -66
- package/dist/esm/cronjob.js.map +0 -1
- package/dist/esm/deep-clone.js +0 -7
- package/dist/esm/deep-clone.js.map +0 -1
- package/dist/esm/default-endowments.js.map +0 -1
- package/dist/esm/entropy.js +0 -6
- package/dist/esm/entropy.js.map +0 -1
- package/dist/esm/enum.js +0 -12
- package/dist/esm/enum.js.map +0 -1
- package/dist/esm/eval-worker.js +0 -47
- package/dist/esm/eval-worker.js.map +0 -1
- package/dist/esm/eval.js +0 -25
- package/dist/esm/eval.js.map +0 -1
- package/dist/esm/fs.js.map +0 -1
- package/dist/esm/handlers.js +0 -3
- package/dist/esm/handlers.js.map +0 -1
- package/dist/esm/icon.js +0 -11
- package/dist/esm/icon.js.map +0 -1
- package/dist/esm/iframe.js.map +0 -1
- package/dist/esm/index.browser.js +0 -23
- package/dist/esm/index.browser.js.map +0 -1
- package/dist/esm/index.executionenv.js +0 -7
- package/dist/esm/index.executionenv.js.map +0 -1
- package/dist/esm/index.js +0 -28
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/json-rpc.js +0 -39
- package/dist/esm/json-rpc.js.map +0 -1
- package/dist/esm/json.js +0 -17
- package/dist/esm/json.js.map +0 -1
- package/dist/esm/logging.js.map +0 -1
- package/dist/esm/manifest/index.browser.js +0 -3
- package/dist/esm/manifest/index.browser.js.map +0 -1
- package/dist/esm/manifest/index.js +0 -4
- package/dist/esm/manifest/index.js.map +0 -1
- package/dist/esm/manifest/manifest.js.map +0 -1
- package/dist/esm/manifest/validation.js +0 -152
- package/dist/esm/manifest/validation.js.map +0 -1
- package/dist/esm/mock.js.map +0 -1
- package/dist/esm/namespace.js +0 -110
- package/dist/esm/namespace.js.map +0 -1
- package/dist/esm/npm.js +0 -70
- package/dist/esm/npm.js.map +0 -1
- package/dist/esm/path.js +0 -17
- package/dist/esm/path.js.map +0 -1
- package/dist/esm/post-process.js.map +0 -1
- package/dist/esm/snaps.js +0 -215
- package/dist/esm/snaps.js.map +0 -1
- package/dist/esm/types.js +0 -85
- package/dist/esm/types.js.map +0 -1
- package/dist/esm/validation.js +0 -17
- package/dist/esm/validation.js.map +0 -1
- package/dist/esm/versions.js.map +0 -1
- package/dist/esm/virtual-file/VirtualFile.js.map +0 -1
- package/dist/esm/virtual-file/index.browser.js +0 -3
- package/dist/esm/virtual-file/index.browser.js.map +0 -1
- package/dist/esm/virtual-file/index.js +0 -4
- package/dist/esm/virtual-file/index.js.map +0 -1
- package/dist/esm/virtual-file/toVirtualFile.js +0 -26
- package/dist/esm/virtual-file/toVirtualFile.js.map +0 -1
- package/dist/types/enum.d.ts +0 -30
- package/dist/types/icon.d.ts +0 -4
- package/dist/types/json.d.ts +0 -13
- package/dist/types/namespace.d.ts +0 -124
- package/dist/types/validation.d.ts +0 -8
- /package/dist/{types/array.d.ts → array.d.ts} +0 -0
- /package/dist/{types/checksum.d.ts → checksum.d.ts} +0 -0
- /package/dist/{types/cronjob.d.ts → cronjob.d.ts} +0 -0
- /package/dist/{types/deep-clone.d.ts → deep-clone.d.ts} +0 -0
- /package/dist/{types/default-endowments.d.ts → default-endowments.d.ts} +0 -0
- /package/dist/{types/entropy.d.ts → entropy.d.ts} +0 -0
- /package/dist/{types/eval-worker.d.ts → eval-worker.d.ts} +0 -0
- /package/dist/{types/eval.d.ts → eval.d.ts} +0 -0
- /package/dist/{types/fs.d.ts → fs.d.ts} +0 -0
- /package/dist/{types/iframe.test.browser.d.ts → iframe.test.browser.d.ts} +0 -0
- /package/dist/{types/index.executionenv.d.ts → index.executionenv.d.ts} +0 -0
- /package/dist/{types/json-rpc.d.ts → json-rpc.d.ts} +0 -0
- /package/dist/{types/logging.d.ts → logging.d.ts} +0 -0
- /package/dist/{types/manifest → manifest}/index.browser.d.ts +0 -0
- /package/dist/{types/manifest → manifest}/index.d.ts +0 -0
- /package/dist/{types/manifest → manifest}/manifest.d.ts +0 -0
- /package/dist/{types/mock.d.ts → mock.d.ts} +0 -0
- /package/dist/{types/path.d.ts → path.d.ts} +0 -0
- /package/dist/{types/post-process.d.ts → post-process.d.ts} +0 -0
- /package/dist/{types/versions.d.ts → versions.d.ts} +0 -0
- /package/dist/{types/virtual-file → virtual-file}/VirtualFile.d.ts +0 -0
- /package/dist/{types/virtual-file → virtual-file}/index.browser.d.ts +0 -0
- /package/dist/{types/virtual-file → virtual-file}/index.d.ts +0 -0
- /package/dist/{types/virtual-file → virtual-file}/toVirtualFile.d.ts +0 -0
package/dist/json.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseJson = void 0;
|
|
4
|
+
const utils_1 = require("@metamask/utils");
|
|
5
|
+
// TODO: Upstream this to @metamask/utils
|
|
6
|
+
/**
|
|
7
|
+
* Parses JSON safely.
|
|
8
|
+
*
|
|
9
|
+
* Does multiple kinds of validation and strips unwanted properties like __proto__.
|
|
10
|
+
*
|
|
11
|
+
* @param json - A JSON string to be parsed.
|
|
12
|
+
* @returns The parsed JSON object.
|
|
13
|
+
*/
|
|
14
|
+
function parseJson(json) {
|
|
15
|
+
return (0, utils_1.getSafeJson)(JSON.parse(json));
|
|
16
|
+
}
|
|
17
|
+
exports.parseJson = parseJson;
|
|
18
|
+
//# sourceMappingURL=json.js.map
|
package/dist/json.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";;;AAAA,2CAA8C;AAE9C,yCAAyC;AAEzC;;;;;;;GAOG;AACH,SAAgB,SAAS,CAAC,IAAY;IACpC,OAAO,IAAA,mBAAW,EAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;AACvC,CAAC;AAFD,8BAEC","sourcesContent":["import { getSafeJson } from '@metamask/utils';\n\n// TODO: Upstream this to @metamask/utils\n\n/**\n * Parses JSON safely.\n *\n * Does multiple kinds of validation and strips unwanted properties like __proto__.\n *\n * @param json - A JSON string to be parsed.\n * @returns The parsed JSON object.\n */\nexport function parseJson(json: string) {\n return getSafeJson(JSON.parse(json));\n}\n"]}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logWarning = exports.logError = exports.logInfo = exports.snapsLogger = void 0;
|
|
4
|
+
const utils_1 = require("@metamask/utils");
|
|
2
5
|
// The global logger used across the monorepo. Other projects should use this
|
|
3
6
|
// to create a module logger.
|
|
4
|
-
|
|
7
|
+
exports.snapsLogger = (0, utils_1.createProjectLogger)('snaps');
|
|
5
8
|
/**
|
|
6
9
|
* Log a message. Currently, this is just a wrapper around `console.log`, but
|
|
7
10
|
* the implementation may change in the future. These logs will be included in
|
|
@@ -13,10 +16,12 @@ export const snapsLogger = createProjectLogger('snaps');
|
|
|
13
16
|
*
|
|
14
17
|
* @param message - The message to log.
|
|
15
18
|
* @param optionalParams - Additional parameters to pass to the logging.
|
|
16
|
-
*/
|
|
19
|
+
*/
|
|
20
|
+
function logInfo(message, ...optionalParams) {
|
|
17
21
|
// eslint-disable-next-line no-console
|
|
18
22
|
console.log(message, ...optionalParams);
|
|
19
23
|
}
|
|
24
|
+
exports.logInfo = logInfo;
|
|
20
25
|
/**
|
|
21
26
|
* Log an error. Currently, this is just a wrapper around `console.error`, but
|
|
22
27
|
* the implementation may change in the future. These logs will be included in
|
|
@@ -32,10 +37,12 @@ export const snapsLogger = createProjectLogger('snaps');
|
|
|
32
37
|
*
|
|
33
38
|
* @param error - The error to log.
|
|
34
39
|
* @param optionalParams - Additional parameters to pass to the logging.
|
|
35
|
-
*/
|
|
40
|
+
*/
|
|
41
|
+
function logError(error, ...optionalParams) {
|
|
36
42
|
// eslint-disable-next-line no-console
|
|
37
43
|
console.error(error, ...optionalParams);
|
|
38
44
|
}
|
|
45
|
+
exports.logError = logError;
|
|
39
46
|
/**
|
|
40
47
|
* Log a warning. Currently, this is just a wrapper around `console.warn`, but
|
|
41
48
|
* the implementation may change in the future. These logs will be included in
|
|
@@ -51,9 +58,10 @@ export const snapsLogger = createProjectLogger('snaps');
|
|
|
51
58
|
*
|
|
52
59
|
* @param message - The message to log.
|
|
53
60
|
* @param optionalParams - Additional parameters to pass to the logging.
|
|
54
|
-
*/
|
|
61
|
+
*/
|
|
62
|
+
function logWarning(message, ...optionalParams) {
|
|
55
63
|
// eslint-disable-next-line no-console
|
|
56
64
|
console.warn(message, ...optionalParams);
|
|
57
65
|
}
|
|
58
|
-
|
|
66
|
+
exports.logWarning = logWarning;
|
|
59
67
|
//# sourceMappingURL=logging.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging.js","sourceRoot":"","sources":["../src/logging.ts"],"names":[],"mappings":";;;AAAA,2CAAsD;AAEtD,6EAA6E;AAC7E,6BAA6B;AAChB,QAAA,WAAW,GAAG,IAAA,2BAAmB,EAAC,OAAO,CAAC,CAAC;AAExD;;;;;;;;;;;GAWG;AACH,SAAgB,OAAO,CAAC,OAAe,EAAE,GAAG,cAAyB;IACnE,sCAAsC;IACtC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,cAAc,CAAC,CAAC;AAC1C,CAAC;AAHD,0BAGC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,QAAQ,CAAC,KAAc,EAAE,GAAG,cAAyB;IACnE,sCAAsC;IACtC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,cAAc,CAAC,CAAC;AAC1C,CAAC;AAHD,4BAGC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,UAAU,CACxB,OAAe,EACf,GAAG,cAAyB;IAE5B,sCAAsC;IACtC,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,cAAc,CAAC,CAAC;AAC3C,CAAC;AAND,gCAMC","sourcesContent":["import { createProjectLogger } from '@metamask/utils';\n\n// The global logger used across the monorepo. Other projects should use this\n// to create a module logger.\nexport const snapsLogger = createProjectLogger('snaps');\n\n/**\n * Log a message. Currently, this is just a wrapper around `console.log`, but\n * the implementation may change in the future. These logs will be included in\n * production builds, so they should be used sparingly, and not contain any\n * sensitive information.\n *\n * This function makes it easy to swap out the logging implementation in all\n * files at once.\n *\n * @param message - The message to log.\n * @param optionalParams - Additional parameters to pass to the logging.\n */\nexport function logInfo(message: string, ...optionalParams: unknown[]): void {\n // eslint-disable-next-line no-console\n console.log(message, ...optionalParams);\n}\n\n/**\n * Log an error. Currently, this is just a wrapper around `console.error`, but\n * the implementation may change in the future. These logs will be included in\n * production builds, so they should be used sparingly, and not contain any\n * sensitive information.\n *\n * These logs should always be visible, without requiring the user to enable\n * verbose logging (like setting a `DEBUG` environment variable), as they are\n * important for debugging snaps.\n *\n * This function makes it easy to swap out the logging implementation in all\n * files at once.\n *\n * @param error - The error to log.\n * @param optionalParams - Additional parameters to pass to the logging.\n */\nexport function logError(error: unknown, ...optionalParams: unknown[]): void {\n // eslint-disable-next-line no-console\n console.error(error, ...optionalParams);\n}\n\n/**\n * Log a warning. Currently, this is just a wrapper around `console.warn`, but\n * the implementation may change in the future. These logs will be included in\n * production builds, so they should be used sparingly, and not contain any\n * sensitive information.\n *\n * These logs should always be visible, without requiring the user to enable\n * verbose logging (like setting a `DEBUG` environment variable), as they are\n * important for debugging snaps.\n *\n * This function makes it easy to swap out the logging implementation in all\n * files at once.\n *\n * @param message - The message to log.\n * @param optionalParams - Additional parameters to pass to the logging.\n */\nexport function logWarning(\n message: string,\n ...optionalParams: unknown[]\n): void {\n // eslint-disable-next-line no-console\n console.warn(message, ...optionalParams);\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./validation"), exports);
|
|
18
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../../src/manifest/index.browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B","sourcesContent":["export * from './validation';\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./manifest"), exports);
|
|
18
|
+
__exportStar(require("./validation"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/manifest/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,+CAA6B","sourcesContent":["export * from './manifest';\nexport * from './validation';\n"]}
|
|
@@ -1,22 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateNpmSnapManifest = exports.getWritableManifest = exports.getSnapIcon = exports.getSnapSourceCode = exports.fixManifest = exports.checkManifest = void 0;
|
|
7
|
+
const utils_1 = require("@metamask/utils");
|
|
8
|
+
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
9
|
+
const fs_1 = require("fs");
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const deep_clone_1 = require("../deep-clone");
|
|
12
|
+
const fs_2 = require("../fs");
|
|
13
|
+
const npm_1 = require("../npm");
|
|
14
|
+
const snaps_1 = require("../snaps");
|
|
15
|
+
const types_1 = require("../types");
|
|
16
|
+
const virtual_file_1 = require("../virtual-file");
|
|
11
17
|
const MANIFEST_SORT_ORDER = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
manifestVersion: 8
|
|
18
|
+
version: 1,
|
|
19
|
+
description: 2,
|
|
20
|
+
proposedName: 3,
|
|
21
|
+
repository: 4,
|
|
22
|
+
source: 5,
|
|
23
|
+
initialPermissions: 6,
|
|
24
|
+
manifestVersion: 7,
|
|
20
25
|
};
|
|
21
26
|
/**
|
|
22
27
|
* Validates a snap.manifest.json file. Attempts to fix the manifest and write
|
|
@@ -28,70 +33,70 @@ const MANIFEST_SORT_ORDER = {
|
|
|
28
33
|
* @param sourceCode - The source code of the Snap.
|
|
29
34
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
30
35
|
* were encountered during processing of the manifest files.
|
|
31
|
-
*/
|
|
36
|
+
*/
|
|
37
|
+
async function checkManifest(basePath, writeManifest = true, sourceCode) {
|
|
32
38
|
const warnings = [];
|
|
33
39
|
const errors = [];
|
|
34
40
|
let updated = false;
|
|
35
|
-
const manifestPath =
|
|
36
|
-
const manifestFile = await readJsonFile(manifestPath);
|
|
41
|
+
const manifestPath = path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest);
|
|
42
|
+
const manifestFile = await (0, fs_2.readJsonFile)(manifestPath);
|
|
37
43
|
const unvalidatedManifest = manifestFile.result;
|
|
38
|
-
const packageFile = await readJsonFile(
|
|
44
|
+
const packageFile = await (0, fs_2.readJsonFile)(path_1.default.join(basePath, types_1.NpmSnapFileNames.PackageJson));
|
|
39
45
|
const snapFiles = {
|
|
40
46
|
manifest: manifestFile,
|
|
41
47
|
packageJson: packageFile,
|
|
42
48
|
sourceCode: await getSnapSourceCode(basePath, unvalidatedManifest, sourceCode),
|
|
43
|
-
svgIcon: await getSnapIcon(basePath, unvalidatedManifest)
|
|
49
|
+
svgIcon: await getSnapIcon(basePath, unvalidatedManifest),
|
|
44
50
|
};
|
|
45
51
|
let manifest;
|
|
46
52
|
try {
|
|
47
|
-
({ manifest } = validateNpmSnap(snapFiles));
|
|
48
|
-
}
|
|
49
|
-
|
|
53
|
+
({ manifest } = (0, npm_1.validateNpmSnap)(snapFiles));
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error instanceof snaps_1.ProgrammaticallyFixableSnapError) {
|
|
50
57
|
errors.push(error.message);
|
|
51
58
|
// If we get here, the files at least have the correct shape.
|
|
52
59
|
const partiallyValidatedFiles = snapFiles;
|
|
53
60
|
let isInvalid = true;
|
|
54
61
|
let currentError = error;
|
|
55
|
-
const maxAttempts = Object.keys(SnapValidationFailureReason).length;
|
|
62
|
+
const maxAttempts = Object.keys(types_1.SnapValidationFailureReason).length;
|
|
56
63
|
// Attempt to fix all fixable validation failure reasons. All such reasons
|
|
57
64
|
// are enumerated by the `SnapValidationFailureReason` enum, so we only
|
|
58
65
|
// attempt to fix the manifest the same amount of times as there are
|
|
59
66
|
// reasons in the enum.
|
|
60
|
-
for(let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++){
|
|
61
|
-
manifest = fixManifest(manifest
|
|
62
|
-
...partiallyValidatedFiles,
|
|
63
|
-
|
|
64
|
-
} : partiallyValidatedFiles, currentError);
|
|
67
|
+
for (let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++) {
|
|
68
|
+
manifest = fixManifest(manifest
|
|
69
|
+
? { ...partiallyValidatedFiles, manifest }
|
|
70
|
+
: partiallyValidatedFiles, currentError);
|
|
65
71
|
try {
|
|
66
|
-
validateNpmSnapManifest({
|
|
67
|
-
...partiallyValidatedFiles,
|
|
68
|
-
manifest
|
|
69
|
-
});
|
|
72
|
+
validateNpmSnapManifest({ ...partiallyValidatedFiles, manifest });
|
|
70
73
|
isInvalid = false;
|
|
71
|
-
}
|
|
74
|
+
}
|
|
75
|
+
catch (nextValidationError) {
|
|
72
76
|
currentError = nextValidationError;
|
|
73
|
-
/* istanbul ignore next: this should be impossible */
|
|
77
|
+
/* istanbul ignore next: this should be impossible */
|
|
78
|
+
if (!(nextValidationError instanceof snaps_1.ProgrammaticallyFixableSnapError) ||
|
|
79
|
+
(attempts === maxAttempts && !isInvalid)) {
|
|
74
80
|
throw new Error(`Internal error: Failed to fix manifest. This is a bug, please report it. Reason:\n${error.message}`);
|
|
75
81
|
}
|
|
76
82
|
errors.push(currentError.message);
|
|
77
83
|
}
|
|
78
84
|
}
|
|
79
85
|
updated = true;
|
|
80
|
-
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
81
88
|
throw error;
|
|
82
89
|
}
|
|
83
90
|
}
|
|
84
91
|
// TypeScript assumes `manifest` can still be undefined, that is not the case.
|
|
85
92
|
// But we assert to keep TypeScript happy.
|
|
86
|
-
assert(manifest);
|
|
93
|
+
(0, utils_1.assert)(manifest);
|
|
87
94
|
const validatedManifest = manifest.result;
|
|
88
95
|
// Check presence of recommended keys
|
|
89
|
-
const recommendedFields = [
|
|
90
|
-
|
|
91
|
-
];
|
|
92
|
-
const missingRecommendedFields = recommendedFields.filter((key)=>!validatedManifest[key]);
|
|
96
|
+
const recommendedFields = ['repository'];
|
|
97
|
+
const missingRecommendedFields = recommendedFields.filter((key) => !validatedManifest[key]);
|
|
93
98
|
if (missingRecommendedFields.length > 0) {
|
|
94
|
-
warnings.push(`Missing recommended package.json properties:\n${missingRecommendedFields.reduce((allMissing, currentField)=>{
|
|
99
|
+
warnings.push(`Missing recommended package.json properties:\n${missingRecommendedFields.reduce((allMissing, currentField) => {
|
|
95
100
|
return `${allMissing}\t${currentField}\n`;
|
|
96
101
|
}, '')}`);
|
|
97
102
|
}
|
|
@@ -99,21 +104,18 @@ const MANIFEST_SORT_ORDER = {
|
|
|
99
104
|
try {
|
|
100
105
|
const newManifest = `${JSON.stringify(getWritableManifest(validatedManifest), null, 2)}\n`;
|
|
101
106
|
if (updated || newManifest !== manifestFile.value) {
|
|
102
|
-
await
|
|
107
|
+
await fs_1.promises.writeFile(path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest), newManifest);
|
|
103
108
|
}
|
|
104
|
-
}
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
105
111
|
// Note: This error isn't pushed to the errors array, because it's not an
|
|
106
112
|
// error in the manifest itself.
|
|
107
113
|
throw new Error(`Failed to update snap.manifest.json: ${error.message}`);
|
|
108
114
|
}
|
|
109
115
|
}
|
|
110
|
-
return {
|
|
111
|
-
manifest: validatedManifest,
|
|
112
|
-
updated,
|
|
113
|
-
warnings,
|
|
114
|
-
errors
|
|
115
|
-
};
|
|
116
|
+
return { manifest: validatedManifest, updated, warnings, errors };
|
|
116
117
|
}
|
|
118
|
+
exports.checkManifest = checkManifest;
|
|
117
119
|
/**
|
|
118
120
|
* Given the relevant Snap files (manifest, `package.json`, and bundle) and a
|
|
119
121
|
* Snap manifest validation error, fixes the fault in the manifest that caused
|
|
@@ -122,30 +124,35 @@ const MANIFEST_SORT_ORDER = {
|
|
|
122
124
|
* @param snapFiles - The contents of all Snap files.
|
|
123
125
|
* @param error - The {@link ProgrammaticallyFixableSnapError} that was thrown.
|
|
124
126
|
* @returns A copy of the manifest file where the cause of the error is fixed.
|
|
125
|
-
*/
|
|
127
|
+
*/
|
|
128
|
+
function fixManifest(snapFiles, error) {
|
|
126
129
|
const { manifest, packageJson } = snapFiles;
|
|
127
130
|
const clonedFile = manifest.clone();
|
|
128
131
|
const manifestCopy = clonedFile.result;
|
|
129
|
-
switch(error.reason){
|
|
130
|
-
case SnapValidationFailureReason.NameMismatch:
|
|
132
|
+
switch (error.reason) {
|
|
133
|
+
case types_1.SnapValidationFailureReason.NameMismatch:
|
|
131
134
|
manifestCopy.source.location.npm.packageName = packageJson.result.name;
|
|
132
135
|
break;
|
|
133
|
-
case SnapValidationFailureReason.VersionMismatch:
|
|
136
|
+
case types_1.SnapValidationFailureReason.VersionMismatch:
|
|
134
137
|
manifestCopy.version = packageJson.result.version;
|
|
135
138
|
break;
|
|
136
|
-
case SnapValidationFailureReason.RepositoryMismatch:
|
|
137
|
-
manifestCopy.repository = packageJson.result.repository
|
|
139
|
+
case types_1.SnapValidationFailureReason.RepositoryMismatch:
|
|
140
|
+
manifestCopy.repository = packageJson.result.repository
|
|
141
|
+
? (0, deep_clone_1.deepClone)(packageJson.result.repository)
|
|
142
|
+
: undefined;
|
|
138
143
|
break;
|
|
139
|
-
case SnapValidationFailureReason.ShasumMismatch:
|
|
140
|
-
manifestCopy.source.shasum = getSnapChecksum(snapFiles);
|
|
144
|
+
case types_1.SnapValidationFailureReason.ShasumMismatch:
|
|
145
|
+
manifestCopy.source.shasum = (0, snaps_1.getSnapChecksum)(snapFiles);
|
|
141
146
|
break;
|
|
142
|
-
/* istanbul ignore next */
|
|
143
|
-
|
|
147
|
+
/* istanbul ignore next */
|
|
148
|
+
default:
|
|
149
|
+
(0, utils_1.assertExhaustive)(error.reason);
|
|
144
150
|
}
|
|
145
151
|
clonedFile.result = manifestCopy;
|
|
146
152
|
clonedFile.value = JSON.stringify(manifestCopy);
|
|
147
153
|
return clonedFile;
|
|
148
154
|
}
|
|
155
|
+
exports.fixManifest = fixManifest;
|
|
149
156
|
/**
|
|
150
157
|
* Given an unvalidated Snap manifest, attempts to extract the location of the
|
|
151
158
|
* bundle source file location and read the file.
|
|
@@ -154,27 +161,31 @@ const MANIFEST_SORT_ORDER = {
|
|
|
154
161
|
* @param manifest - The unvalidated Snap manifest file contents.
|
|
155
162
|
* @param sourceCode - Override source code for plugins.
|
|
156
163
|
* @returns The contents of the bundle file, if any.
|
|
157
|
-
*/
|
|
158
|
-
|
|
164
|
+
*/
|
|
165
|
+
async function getSnapSourceCode(basePath, manifest, sourceCode) {
|
|
166
|
+
if (!(0, utils_1.isPlainObject)(manifest)) {
|
|
159
167
|
return undefined;
|
|
160
168
|
}
|
|
161
|
-
const sourceFilePath = manifest.source?.location
|
|
169
|
+
const sourceFilePath = manifest.source?.location
|
|
170
|
+
?.npm?.filePath;
|
|
162
171
|
if (!sourceFilePath) {
|
|
163
172
|
return undefined;
|
|
164
173
|
}
|
|
165
174
|
if (sourceCode) {
|
|
166
|
-
return new VirtualFile({
|
|
167
|
-
path:
|
|
168
|
-
value: sourceCode
|
|
175
|
+
return new virtual_file_1.VirtualFile({
|
|
176
|
+
path: path_1.default.join(basePath, sourceFilePath),
|
|
177
|
+
value: sourceCode,
|
|
169
178
|
});
|
|
170
179
|
}
|
|
171
180
|
try {
|
|
172
|
-
const virtualFile = await readVirtualFile(
|
|
181
|
+
const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, sourceFilePath), 'utf8');
|
|
173
182
|
return virtualFile;
|
|
174
|
-
}
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
175
185
|
throw new Error(`Failed to read Snap bundle file: ${error.message}`);
|
|
176
186
|
}
|
|
177
187
|
}
|
|
188
|
+
exports.getSnapSourceCode = getSnapSourceCode;
|
|
178
189
|
/**
|
|
179
190
|
* Given an unvalidated Snap manifest, attempts to extract the location of the
|
|
180
191
|
* icon and read the file.
|
|
@@ -182,39 +193,44 @@ const MANIFEST_SORT_ORDER = {
|
|
|
182
193
|
* @param basePath - The path to the folder with the manifest files.
|
|
183
194
|
* @param manifest - The unvalidated Snap manifest file contents.
|
|
184
195
|
* @returns The contents of the icon, if any.
|
|
185
|
-
*/
|
|
186
|
-
|
|
196
|
+
*/
|
|
197
|
+
async function getSnapIcon(basePath, manifest) {
|
|
198
|
+
if (!(0, utils_1.isPlainObject)(manifest)) {
|
|
187
199
|
return undefined;
|
|
188
200
|
}
|
|
189
|
-
const iconPath = manifest.source?.location?.npm
|
|
201
|
+
const iconPath = manifest.source?.location?.npm
|
|
202
|
+
?.iconPath;
|
|
190
203
|
if (!iconPath) {
|
|
191
204
|
return undefined;
|
|
192
205
|
}
|
|
193
206
|
try {
|
|
194
|
-
const virtualFile = await readVirtualFile(
|
|
207
|
+
const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, iconPath), 'utf8');
|
|
195
208
|
return virtualFile;
|
|
196
|
-
}
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
197
211
|
throw new Error(`Failed to read Snap icon file: ${error.message}`);
|
|
198
212
|
}
|
|
199
213
|
}
|
|
214
|
+
exports.getSnapIcon = getSnapIcon;
|
|
200
215
|
/**
|
|
201
216
|
* Sorts the given manifest in our preferred sort order and removes the
|
|
202
217
|
* `repository` field if it is falsy (it may be `null`).
|
|
203
218
|
*
|
|
204
219
|
* @param manifest - The manifest to sort and modify.
|
|
205
220
|
* @returns The disk-ready manifest.
|
|
206
|
-
*/
|
|
221
|
+
*/
|
|
222
|
+
function getWritableManifest(manifest) {
|
|
207
223
|
const { repository, ...remaining } = manifest;
|
|
208
|
-
const keys = Object.keys(repository ? {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}), {});
|
|
224
|
+
const keys = Object.keys(repository ? { ...remaining, repository } : remaining);
|
|
225
|
+
const writableManifest = keys
|
|
226
|
+
.sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])
|
|
227
|
+
.reduce((result, key) => ({
|
|
228
|
+
...result,
|
|
229
|
+
[key]: manifest[key],
|
|
230
|
+
}), {});
|
|
216
231
|
return writableManifest;
|
|
217
232
|
}
|
|
233
|
+
exports.getWritableManifest = getWritableManifest;
|
|
218
234
|
/**
|
|
219
235
|
* Validates the fields of an npm Snap manifest that has already passed JSON
|
|
220
236
|
* Schema validation.
|
|
@@ -224,7 +240,8 @@ const MANIFEST_SORT_ORDER = {
|
|
|
224
240
|
* @param snapFiles.packageJson - The npm Snap's `package.json`.
|
|
225
241
|
* @param snapFiles.sourceCode - The Snap's source code.
|
|
226
242
|
* @param snapFiles.svgIcon - The Snap's optional icon.
|
|
227
|
-
*/
|
|
243
|
+
*/
|
|
244
|
+
function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, }) {
|
|
228
245
|
const packageJsonName = packageJson.result.name;
|
|
229
246
|
const packageJsonVersion = packageJson.result.version;
|
|
230
247
|
const packageJsonRepository = packageJson.result.repository;
|
|
@@ -232,21 +249,19 @@ const MANIFEST_SORT_ORDER = {
|
|
|
232
249
|
const manifestPackageVersion = manifest.result.version;
|
|
233
250
|
const manifestRepository = manifest.result.repository;
|
|
234
251
|
if (packageJsonName !== manifestPackageName) {
|
|
235
|
-
throw new ProgrammaticallyFixableSnapError(`"${NpmSnapFileNames.Manifest}" npm package name ("${manifestPackageName}") does not match the "${NpmSnapFileNames.PackageJson}" "name" field ("${packageJsonName}").`, SnapValidationFailureReason.NameMismatch);
|
|
252
|
+
throw new snaps_1.ProgrammaticallyFixableSnapError(`"${types_1.NpmSnapFileNames.Manifest}" npm package name ("${manifestPackageName}") does not match the "${types_1.NpmSnapFileNames.PackageJson}" "name" field ("${packageJsonName}").`, types_1.SnapValidationFailureReason.NameMismatch);
|
|
236
253
|
}
|
|
237
254
|
if (packageJsonVersion !== manifestPackageVersion) {
|
|
238
|
-
throw new ProgrammaticallyFixableSnapError(`"${NpmSnapFileNames.Manifest}" npm package version ("${manifestPackageVersion}") does not match the "${NpmSnapFileNames.PackageJson}" "version" field ("${packageJsonVersion}").`, SnapValidationFailureReason.VersionMismatch);
|
|
255
|
+
throw new snaps_1.ProgrammaticallyFixableSnapError(`"${types_1.NpmSnapFileNames.Manifest}" npm package version ("${manifestPackageVersion}") does not match the "${types_1.NpmSnapFileNames.PackageJson}" "version" field ("${packageJsonVersion}").`, types_1.SnapValidationFailureReason.VersionMismatch);
|
|
239
256
|
}
|
|
240
|
-
if (
|
|
257
|
+
if (
|
|
258
|
+
// The repository may be `undefined` in package.json but can only be defined
|
|
241
259
|
// or `null` in the Snap manifest due to TS@<4.4 issues.
|
|
242
|
-
(packageJsonRepository || manifestRepository) &&
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
sourceCode,
|
|
248
|
-
svgIcon
|
|
249
|
-
}, `"${NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
|
|
260
|
+
(packageJsonRepository || manifestRepository) &&
|
|
261
|
+
!(0, fast_deep_equal_1.default)(packageJsonRepository, manifestRepository)) {
|
|
262
|
+
throw new snaps_1.ProgrammaticallyFixableSnapError(`"${types_1.NpmSnapFileNames.Manifest}" "repository" field does not match the "${types_1.NpmSnapFileNames.PackageJson}" "repository" field.`, types_1.SnapValidationFailureReason.RepositoryMismatch);
|
|
263
|
+
}
|
|
264
|
+
(0, snaps_1.validateSnapShasum)({ manifest, sourceCode, svgIcon }, `"${types_1.NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
|
|
250
265
|
}
|
|
251
|
-
|
|
266
|
+
exports.validateNpmSnapManifest = validateNpmSnapManifest;
|
|
252
267
|
//# sourceMappingURL=manifest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manifest.js","sourceRoot":"","sources":["../../src/manifest/manif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CAAC,QAAQ,wBAAwB,mBAAmB,0BAA0B,wBAAgB,CAAC,WAAW,oBAAoB,eAAe,KAAK,EACtK,mCAA2B,CAAC,YAAY,CACzC,CAAC;KACH;IAED,IAAI,kBAAkB,KAAK,sBAAsB,EAAE;QACjD,MAAM,IAAI,wCAAgC,CACxC,IAAI,wBAAgB,CAAC,QAAQ,2BAA2B,sBAAsB,0BAA0B,wBAAgB,CAAC,WAAW,uBAAuB,kBAAkB,KAAK,EAClL,mCAA2B,CAAC,eAAe,CAC5C,CAAC;KACH;IAED;IACE,4EAA4E;IAC5E,wDAAwD;IACxD,CAAC,qBAAqB,IAAI,kBAAkB,CAAC;QAC7C,CAAC,IAAA,yBAAS,EAAC,qBAAqB,EAAE,kBAAkB,CAAC,EACrD;QACA,MAAM,IAAI,wCAAgC,CACxC,IAAI,wBAAgB,CAAC,QAAQ,4CAA4C,wBAAgB,CAAC,WAAW,uBAAuB,EAC5H,mCAA2B,CAAC,kBAAkB,CAC/C,CAAC;KACH;IAED,IAAA,0BAAkB,EAChB,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,EACjC,IAAI,wBAAgB,CAAC,QAAQ,kDAAkD,CAChF,CAAC;AACJ,CAAC;AA5CD,0DA4CC","sourcesContent":["import { Json, assertExhaustive, assert, isPlainObject } from '@metamask/utils';\nimport deepEqual from 'fast-deep-equal';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { validateNpmSnap } from '../npm';\nimport {\n getSnapChecksum,\n ProgrammaticallyFixableSnapError,\n validateSnapShasum,\n} from '../snaps';\nimport {\n NpmSnapFileNames,\n SnapFiles,\n SnapValidationFailureReason,\n UnvalidatedSnapFiles,\n} from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file';\nimport { SnapManifest } from './validation';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n version: 1,\n description: 2,\n proposedName: 3,\n repository: 4,\n source: 5,\n initialPermissions: 6,\n manifestVersion: 7,\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 updated.\n * @property warnings - An array of warnings that were encountered during\n * processing of the manifest files. These warnings are not logged to the\n * console automatically, so depending on the environment the function is called\n * in, a different method for logging can be used.\n * @property errors - An array of errors that were encountered during\n * processing of the manifest files. These errors are not logged to the\n * console automatically, so depending on the environment the function is called\n * in, a different method for logging can be used.\n */\nexport type CheckManifestResult = {\n manifest: SnapManifest;\n updated?: boolean;\n warnings: string[];\n errors: string[];\n};\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 writeManifest - Whether to write the fixed manifest to disk.\n * @param sourceCode - The source code of the Snap.\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 writeManifest = true,\n sourceCode?: string,\n): Promise<CheckManifestResult> {\n const warnings: string[] = [];\n const errors: string[] = [];\n\n let updated = false;\n\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 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 };\n\n let manifest: VirtualFile<SnapManifest> | undefined;\n try {\n ({ manifest } = validateNpmSnap(snapFiles));\n } catch (error) {\n if (error instanceof ProgrammaticallyFixableSnapError) {\n errors.push(error.message);\n\n // If we get here, the files at least have the correct shape.\n const partiallyValidatedFiles = snapFiles as SnapFiles;\n\n let isInvalid = true;\n let currentError = error;\n const maxAttempts = Object.keys(SnapValidationFailureReason).length;\n\n // Attempt to fix all fixable validation failure reasons. All such reasons\n // are enumerated by the `SnapValidationFailureReason` enum, so we only\n // attempt to fix the manifest the same amount of times as there are\n // reasons in the enum.\n for (let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++) {\n manifest = fixManifest(\n manifest\n ? { ...partiallyValidatedFiles, manifest }\n : partiallyValidatedFiles,\n currentError,\n );\n\n try {\n validateNpmSnapManifest({ ...partiallyValidatedFiles, manifest });\n\n isInvalid = false;\n } catch (nextValidationError) {\n currentError = nextValidationError;\n /* istanbul ignore next: this should be impossible */\n if (\n !(\n nextValidationError instanceof ProgrammaticallyFixableSnapError\n ) ||\n (attempts === maxAttempts && !isInvalid)\n ) {\n throw new Error(\n `Internal error: Failed to fix manifest. This is a bug, please report it. Reason:\\n${error.message}`,\n );\n }\n\n errors.push(currentError.message);\n }\n }\n\n updated = true;\n } else {\n throw error;\n }\n }\n\n // TypeScript assumes `manifest` can still be undefined, that is not the case.\n // But we assert to keep TypeScript happy.\n assert(manifest);\n\n const validatedManifest = manifest.result;\n\n // Check presence of recommended keys\n const recommendedFields = ['repository'] as const;\n\n const missingRecommendedFields = recommendedFields.filter(\n (key) => !validatedManifest[key],\n );\n\n if (missingRecommendedFields.length > 0) {\n warnings.push(\n `Missing recommended package.json properties:\\n${missingRecommendedFields.reduce(\n (allMissing, currentField) => {\n return `${allMissing}\\t${currentField}\\n`;\n },\n '',\n )}`,\n );\n }\n\n if (writeManifest) {\n try {\n const newManifest = `${JSON.stringify(\n getWritableManifest(validatedManifest),\n null,\n 2,\n )}\\n`;\n\n if (updated || newManifest !== manifestFile.value) {\n await fs.writeFile(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n newManifest,\n );\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(`Failed to update snap.manifest.json: ${error.message}`);\n }\n }\n\n return { manifest: validatedManifest, updated, warnings, errors };\n}\n\n/**\n * Given the relevant Snap files (manifest, `package.json`, and bundle) and a\n * Snap manifest validation error, fixes the fault in the manifest that caused\n * the error.\n *\n * @param snapFiles - The contents of all Snap files.\n * @param error - The {@link ProgrammaticallyFixableSnapError} that was thrown.\n * @returns A copy of the manifest file where the cause of the error is fixed.\n */\nexport function fixManifest(\n snapFiles: SnapFiles,\n error: ProgrammaticallyFixableSnapError,\n): VirtualFile<SnapManifest> {\n const { manifest, packageJson } = snapFiles;\n const clonedFile = manifest.clone();\n const manifestCopy = clonedFile.result;\n\n switch (error.reason) {\n case SnapValidationFailureReason.NameMismatch:\n manifestCopy.source.location.npm.packageName = packageJson.result.name;\n break;\n\n case SnapValidationFailureReason.VersionMismatch:\n manifestCopy.version = packageJson.result.version;\n break;\n\n case SnapValidationFailureReason.RepositoryMismatch:\n manifestCopy.repository = packageJson.result.repository\n ? deepClone(packageJson.result.repository)\n : undefined;\n break;\n\n case SnapValidationFailureReason.ShasumMismatch:\n manifestCopy.source.shasum = getSnapChecksum(snapFiles);\n break;\n\n /* istanbul ignore next */\n default:\n assertExhaustive(error.reason);\n }\n\n clonedFile.result = manifestCopy;\n clonedFile.value = JSON.stringify(manifestCopy);\n return clonedFile;\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(`Failed to read Snap bundle file: ${error.message}`);\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: ${error.message}`);\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\n/**\n * Validates the fields of an npm Snap manifest that has already passed JSON\n * Schema validation.\n *\n * @param snapFiles - The relevant snap files to validate.\n * @param snapFiles.manifest - The npm Snap manifest to validate.\n * @param snapFiles.packageJson - The npm Snap's `package.json`.\n * @param snapFiles.sourceCode - The Snap's source code.\n * @param snapFiles.svgIcon - The Snap's optional icon.\n */\nexport function validateNpmSnapManifest({\n manifest,\n packageJson,\n sourceCode,\n svgIcon,\n}: SnapFiles) {\n const packageJsonName = packageJson.result.name;\n const packageJsonVersion = packageJson.result.version;\n const packageJsonRepository = packageJson.result.repository;\n\n const manifestPackageName = manifest.result.source.location.npm.packageName;\n const manifestPackageVersion = manifest.result.version;\n const manifestRepository = manifest.result.repository;\n\n if (packageJsonName !== manifestPackageName) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" npm package name (\"${manifestPackageName}\") does not match the \"${NpmSnapFileNames.PackageJson}\" \"name\" field (\"${packageJsonName}\").`,\n SnapValidationFailureReason.NameMismatch,\n );\n }\n\n if (packageJsonVersion !== manifestPackageVersion) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" npm package version (\"${manifestPackageVersion}\") does not match the \"${NpmSnapFileNames.PackageJson}\" \"version\" field (\"${packageJsonVersion}\").`,\n SnapValidationFailureReason.VersionMismatch,\n );\n }\n\n if (\n // The repository may be `undefined` in package.json but can only be defined\n // or `null` in the Snap manifest due to TS@<4.4 issues.\n (packageJsonRepository || manifestRepository) &&\n !deepEqual(packageJsonRepository, manifestRepository)\n ) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" \"repository\" field does not match the \"${NpmSnapFileNames.PackageJson}\" \"repository\" field.`,\n SnapValidationFailureReason.RepositoryMismatch,\n );\n }\n\n validateSnapShasum(\n { manifest, sourceCode, svgIcon },\n `\"${NpmSnapFileNames.Manifest}\" \"shasum\" field does not match computed shasum.`,\n );\n}\n"]}
|