@metamask/snaps-utils 0.38.0-flask.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +222 -11
- 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/{types/cronjob.d.ts → cronjob.d.ts} +1 -1
- 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/{types/eval.d.ts → eval.d.ts} +1 -9
- package/dist/eval.js +27 -0
- package/dist/eval.js.map +1 -0
- package/dist/{types/fs.d.ts → fs.d.ts} +2 -2
- package/dist/{esm/fs.js → fs.js} +56 -33
- package/dist/fs.js.map +1 -0
- package/dist/handlers.d.ts +105 -0
- 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 -5
- 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 -5
- 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/{types/json-rpc.d.ts → json-rpc.d.ts} +2 -2
- 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/{types/manifest → manifest}/manifest.d.ts +4 -6
- package/dist/{esm/manifest → manifest}/manifest.js +115 -101
- package/dist/manifest/manifest.js.map +1 -0
- package/dist/{types/manifest → manifest}/validation.d.ts +76 -34
- 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} +3 -2
- 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} +9 -17
- package/dist/snaps.js +202 -0
- package/dist/snaps.js.map +1 -0
- package/dist/{types/types.d.ts → types.d.ts} +22 -15
- package/dist/types.js +103 -0
- package/dist/types.js.map +1 -0
- package/dist/{types/versions.d.ts → versions.d.ts} +1 -1
- 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 +40 -59
- 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/errors.js +0 -19
- package/dist/cjs/errors.js.map +0 -1
- package/dist/cjs/eval-worker.js.map +0 -1
- package/dist/cjs/eval.js +0 -77
- 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 -65
- 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 -43
- 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 -48
- 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/strings.js +0 -21
- package/dist/cjs/strings.js.map +0 -1
- package/dist/cjs/structs.js +0 -163
- package/dist/cjs/structs.js.map +0 -1
- package/dist/cjs/types.js +0 -109
- 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/errors.js +0 -17
- package/dist/esm/errors.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 -65
- package/dist/esm/eval.js.map +0 -1
- package/dist/esm/fs.js.map +0 -1
- package/dist/esm/handlers.js +0 -47
- 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 -26
- 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 -31
- 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/strings.js +0 -11
- package/dist/esm/strings.js.map +0 -1
- package/dist/esm/structs.js +0 -230
- package/dist/esm/structs.js.map +0 -1
- package/dist/esm/types.js +0 -80
- 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/errors.d.ts +0 -10
- package/dist/types/handlers.d.ts +0 -144
- 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/strings.d.ts +0 -8
- package/dist/types/structs.d.ts +0 -158
- 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/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/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/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/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/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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,4CAA0B;AAC1B,4CAA0B;AAC1B,6CAA2B;AAC3B,+CAA6B;AAC7B,uDAAqC;AACrC,4CAA0B;AAC1B,yCAAuB;AACvB,uCAAqB;AACrB,6CAA2B;AAC3B,2CAAyB;AACzB,yCAAuB;AACvB,6CAA2B;AAC3B,4CAA0B;AAC1B,6CAA2B;AAC3B,yCAAuB;AACvB,8CAA4B;AAC5B,iDAA+B;AAC/B,wCAAsB;AACtB,yCAAuB;AACvB,iDAA+B;AAC/B,0CAAwB;AACxB,0CAAwB;AACxB,6CAA2B;AAC3B,iDAA+B","sourcesContent":["export * from './array';\nexport * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './eval';\nexport * from './fs';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './notification';\nexport * from './npm';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './types';\nexport * from './versions';\nexport * from './virtual-file';\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { Json, JsonRpcSuccess, AssertionErrorConstructor } from '@metamask/utils';
|
|
2
|
+
import { Infer } from 'superstruct';
|
|
3
3
|
export declare const RpcOriginsStruct: import("superstruct").Struct<{
|
|
4
4
|
dapps?: boolean | undefined;
|
|
5
5
|
snaps?: boolean | undefined;
|
package/dist/json-rpc.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertIsJsonRpcSuccess = exports.assertIsRpcOrigins = exports.RpcOriginsStruct = void 0;
|
|
4
|
+
const utils_1 = require("@metamask/utils");
|
|
5
|
+
const superstruct_1 = require("superstruct");
|
|
6
|
+
exports.RpcOriginsStruct = (0, superstruct_1.refine)((0, superstruct_1.object)({
|
|
7
|
+
dapps: (0, superstruct_1.optional)((0, superstruct_1.boolean)()),
|
|
8
|
+
snaps: (0, superstruct_1.optional)((0, superstruct_1.boolean)()),
|
|
9
|
+
}), 'RPC origins', (value) => {
|
|
10
|
+
if (!Object.values(value).some(Boolean)) {
|
|
11
|
+
throw new Error('Must specify at least one JSON-RPC origin');
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Asserts that the given value is a valid {@link RpcOrigins} object.
|
|
17
|
+
*
|
|
18
|
+
* @param value - The value to assert.
|
|
19
|
+
* @param ErrorWrapper - An optional error wrapper to use. Defaults to
|
|
20
|
+
* {@link AssertionError}.
|
|
21
|
+
* @throws If the value is not a valid {@link RpcOrigins} object.
|
|
22
|
+
*/
|
|
23
|
+
function assertIsRpcOrigins(value,
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
25
|
+
ErrorWrapper) {
|
|
26
|
+
(0, utils_1.assertStruct)(value, exports.RpcOriginsStruct, 'Invalid JSON-RPC origins', ErrorWrapper);
|
|
27
|
+
}
|
|
28
|
+
exports.assertIsRpcOrigins = assertIsRpcOrigins;
|
|
29
|
+
/**
|
|
30
|
+
* Assert that the given value is a successful JSON-RPC response. If the value
|
|
31
|
+
* is not a success response, an error is thrown. If the value is an JSON-RPC
|
|
32
|
+
* error, the error message is included in the thrown error.
|
|
33
|
+
*
|
|
34
|
+
* @param value - The value to check.
|
|
35
|
+
* @throws If the value is not a JSON-RPC success response.
|
|
36
|
+
*/
|
|
37
|
+
function assertIsJsonRpcSuccess(value) {
|
|
38
|
+
if (!(0, utils_1.isJsonRpcSuccess)(value)) {
|
|
39
|
+
if ((0, utils_1.isJsonRpcFailure)(value)) {
|
|
40
|
+
throw new Error(`JSON-RPC request failed: ${value.error.message}`);
|
|
41
|
+
}
|
|
42
|
+
throw new Error('Invalid JSON-RPC response.');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.assertIsJsonRpcSuccess = assertIsJsonRpcSuccess;
|
|
46
|
+
//# sourceMappingURL=json-rpc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-rpc.js","sourceRoot":"","sources":["../src/json-rpc.ts"],"names":[],"mappings":";;;AAAA,2CAOyB;AACzB,6CAAuE;AAE1D,QAAA,gBAAgB,GAAG,IAAA,oBAAM,EACpC,IAAA,oBAAM,EAAC;IACL,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,qBAAO,GAAE,CAAC;IAC1B,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,qBAAO,GAAE,CAAC;CAC3B,CAAC,EACF,aAAa,EACb,CAAC,KAAK,EAAE,EAAE;IACR,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE;QACvC,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;KAC9D;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CACF,CAAC;AAIF;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAChC,KAAc;AACd,gEAAgE;AAChE,YAAwC;IAExC,IAAA,oBAAY,EACV,KAAK,EACL,wBAAgB,EAChB,0BAA0B,EAC1B,YAAY,CACb,CAAC;AACJ,CAAC;AAXD,gDAWC;AAED;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,KAAc;IAEd,IAAI,CAAC,IAAA,wBAAgB,EAAC,KAAK,CAAC,EAAE;QAC5B,IAAI,IAAA,wBAAgB,EAAC,KAAK,CAAC,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,4BAA4B,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;SACpE;QAED,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;KAC/C;AACH,CAAC;AAVD,wDAUC","sourcesContent":["import {\n isJsonRpcFailure,\n isJsonRpcSuccess,\n Json,\n JsonRpcSuccess,\n AssertionErrorConstructor,\n assertStruct,\n} from '@metamask/utils';\nimport { boolean, Infer, object, optional, refine } from 'superstruct';\n\nexport const RpcOriginsStruct = refine(\n object({\n dapps: optional(boolean()),\n snaps: optional(boolean()),\n }),\n 'RPC origins',\n (value) => {\n if (!Object.values(value).some(Boolean)) {\n throw new Error('Must specify at least one JSON-RPC origin');\n }\n\n return true;\n },\n);\n\nexport type RpcOrigins = Infer<typeof RpcOriginsStruct>;\n\n/**\n * Asserts that the given value is a valid {@link RpcOrigins} object.\n *\n * @param value - The value to assert.\n * @param ErrorWrapper - An optional error wrapper to use. Defaults to\n * {@link AssertionError}.\n * @throws If the value is not a valid {@link RpcOrigins} object.\n */\nexport function assertIsRpcOrigins(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is RpcOrigins {\n assertStruct(\n value,\n RpcOriginsStruct,\n 'Invalid JSON-RPC origins',\n ErrorWrapper,\n );\n}\n\n/**\n * Assert that the given value is a successful JSON-RPC response. If the value\n * is not a success response, an error is thrown. If the value is an JSON-RPC\n * error, the error message is included in the thrown error.\n *\n * @param value - The value to check.\n * @throws If the value is not a JSON-RPC success response.\n */\nexport function assertIsJsonRpcSuccess(\n value: unknown,\n): asserts value is JsonRpcSuccess<Json> {\n if (!isJsonRpcSuccess(value)) {\n if (isJsonRpcFailure(value)) {\n throw new Error(`JSON-RPC request failed: ${value.error.message}`);\n }\n\n throw new Error('Invalid JSON-RPC response.');\n }\n}\n"]}
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses JSON safely.
|
|
3
|
+
*
|
|
4
|
+
* Does multiple kinds of validation and strips unwanted properties like __proto__.
|
|
5
|
+
*
|
|
6
|
+
* @param json - A JSON string to be parsed.
|
|
7
|
+
* @returns The parsed JSON object.
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseJson(json: string): import("@metamask/utils").Json;
|
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,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Json } from '@metamask/utils';
|
|
2
2
|
import { ProgrammaticallyFixableSnapError } from '../snaps';
|
|
3
|
-
import
|
|
3
|
+
import { SnapFiles } from '../types';
|
|
4
4
|
import { VirtualFile } from '../virtual-file';
|
|
5
|
-
import
|
|
5
|
+
import { SnapManifest } from './validation';
|
|
6
6
|
/**
|
|
7
7
|
* The result from the `checkManifest` function.
|
|
8
8
|
*
|
|
@@ -23,7 +23,6 @@ export declare type CheckManifestResult = {
|
|
|
23
23
|
warnings: string[];
|
|
24
24
|
errors: string[];
|
|
25
25
|
};
|
|
26
|
-
export declare type WriteFileFunction = (path: string, data: string) => Promise<void>;
|
|
27
26
|
/**
|
|
28
27
|
* Validates a snap.manifest.json file. Attempts to fix the manifest and write
|
|
29
28
|
* the fixed version to disk if `writeManifest` is true. Throws if validation
|
|
@@ -32,11 +31,10 @@ export declare type WriteFileFunction = (path: string, data: string) => Promise<
|
|
|
32
31
|
* @param basePath - The path to the folder with the manifest files.
|
|
33
32
|
* @param writeManifest - Whether to write the fixed manifest to disk.
|
|
34
33
|
* @param sourceCode - The source code of the Snap.
|
|
35
|
-
* @param writeFileFn - The function to use to write the manifest to disk.
|
|
36
34
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
37
35
|
* were encountered during processing of the manifest files.
|
|
38
36
|
*/
|
|
39
|
-
export declare function checkManifest(basePath: string, writeManifest?: boolean, sourceCode?: string
|
|
37
|
+
export declare function checkManifest(basePath: string, writeManifest?: boolean, sourceCode?: string): Promise<CheckManifestResult>;
|
|
40
38
|
/**
|
|
41
39
|
* Given the relevant Snap files (manifest, `package.json`, and bundle) and a
|
|
42
40
|
* Snap manifest validation error, fixes the fault in the manifest that caused
|
|
@@ -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
|
|
@@ -26,73 +31,72 @@ const MANIFEST_SORT_ORDER = {
|
|
|
26
31
|
* @param basePath - The path to the folder with the manifest files.
|
|
27
32
|
* @param writeManifest - Whether to write the fixed manifest to disk.
|
|
28
33
|
* @param sourceCode - The source code of the Snap.
|
|
29
|
-
* @param writeFileFn - The function to use to write the manifest to disk.
|
|
30
34
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
31
35
|
* were encountered during processing of the manifest files.
|
|
32
|
-
*/
|
|
36
|
+
*/
|
|
37
|
+
async function checkManifest(basePath, writeManifest = true, sourceCode) {
|
|
33
38
|
const warnings = [];
|
|
34
39
|
const errors = [];
|
|
35
40
|
let updated = false;
|
|
36
|
-
const manifestPath =
|
|
37
|
-
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);
|
|
38
43
|
const unvalidatedManifest = manifestFile.result;
|
|
39
|
-
const packageFile = await readJsonFile(
|
|
44
|
+
const packageFile = await (0, fs_2.readJsonFile)(path_1.default.join(basePath, types_1.NpmSnapFileNames.PackageJson));
|
|
40
45
|
const snapFiles = {
|
|
41
46
|
manifest: manifestFile,
|
|
42
47
|
packageJson: packageFile,
|
|
43
48
|
sourceCode: await getSnapSourceCode(basePath, unvalidatedManifest, sourceCode),
|
|
44
|
-
svgIcon: await getSnapIcon(basePath, unvalidatedManifest)
|
|
49
|
+
svgIcon: await getSnapIcon(basePath, unvalidatedManifest),
|
|
45
50
|
};
|
|
46
51
|
let manifest;
|
|
47
52
|
try {
|
|
48
|
-
({ manifest } = validateNpmSnap(snapFiles));
|
|
49
|
-
}
|
|
50
|
-
|
|
53
|
+
({ manifest } = (0, npm_1.validateNpmSnap)(snapFiles));
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error instanceof snaps_1.ProgrammaticallyFixableSnapError) {
|
|
51
57
|
errors.push(error.message);
|
|
52
58
|
// If we get here, the files at least have the correct shape.
|
|
53
59
|
const partiallyValidatedFiles = snapFiles;
|
|
54
60
|
let isInvalid = true;
|
|
55
61
|
let currentError = error;
|
|
56
|
-
const maxAttempts = Object.keys(SnapValidationFailureReason).length;
|
|
62
|
+
const maxAttempts = Object.keys(types_1.SnapValidationFailureReason).length;
|
|
57
63
|
// Attempt to fix all fixable validation failure reasons. All such reasons
|
|
58
64
|
// are enumerated by the `SnapValidationFailureReason` enum, so we only
|
|
59
65
|
// attempt to fix the manifest the same amount of times as there are
|
|
60
66
|
// reasons in the enum.
|
|
61
|
-
for(let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++){
|
|
62
|
-
manifest = fixManifest(manifest
|
|
63
|
-
...partiallyValidatedFiles,
|
|
64
|
-
|
|
65
|
-
} : partiallyValidatedFiles, currentError);
|
|
67
|
+
for (let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++) {
|
|
68
|
+
manifest = fixManifest(manifest
|
|
69
|
+
? { ...partiallyValidatedFiles, manifest }
|
|
70
|
+
: partiallyValidatedFiles, currentError);
|
|
66
71
|
try {
|
|
67
|
-
validateNpmSnapManifest({
|
|
68
|
-
...partiallyValidatedFiles,
|
|
69
|
-
manifest
|
|
70
|
-
});
|
|
72
|
+
validateNpmSnapManifest({ ...partiallyValidatedFiles, manifest });
|
|
71
73
|
isInvalid = false;
|
|
72
|
-
}
|
|
74
|
+
}
|
|
75
|
+
catch (nextValidationError) {
|
|
73
76
|
currentError = nextValidationError;
|
|
74
|
-
/* 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)) {
|
|
75
80
|
throw new Error(`Internal error: Failed to fix manifest. This is a bug, please report it. Reason:\n${error.message}`);
|
|
76
81
|
}
|
|
77
82
|
errors.push(currentError.message);
|
|
78
83
|
}
|
|
79
84
|
}
|
|
80
85
|
updated = true;
|
|
81
|
-
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
82
88
|
throw error;
|
|
83
89
|
}
|
|
84
90
|
}
|
|
85
91
|
// TypeScript assumes `manifest` can still be undefined, that is not the case.
|
|
86
92
|
// But we assert to keep TypeScript happy.
|
|
87
|
-
assert(manifest);
|
|
93
|
+
(0, utils_1.assert)(manifest);
|
|
88
94
|
const validatedManifest = manifest.result;
|
|
89
95
|
// Check presence of recommended keys
|
|
90
|
-
const recommendedFields = [
|
|
91
|
-
|
|
92
|
-
];
|
|
93
|
-
const missingRecommendedFields = recommendedFields.filter((key)=>!validatedManifest[key]);
|
|
96
|
+
const recommendedFields = ['repository'];
|
|
97
|
+
const missingRecommendedFields = recommendedFields.filter((key) => !validatedManifest[key]);
|
|
94
98
|
if (missingRecommendedFields.length > 0) {
|
|
95
|
-
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) => {
|
|
96
100
|
return `${allMissing}\t${currentField}\n`;
|
|
97
101
|
}, '')}`);
|
|
98
102
|
}
|
|
@@ -100,21 +104,18 @@ const MANIFEST_SORT_ORDER = {
|
|
|
100
104
|
try {
|
|
101
105
|
const newManifest = `${JSON.stringify(getWritableManifest(validatedManifest), null, 2)}\n`;
|
|
102
106
|
if (updated || newManifest !== manifestFile.value) {
|
|
103
|
-
await
|
|
107
|
+
await fs_1.promises.writeFile(path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest), newManifest);
|
|
104
108
|
}
|
|
105
|
-
}
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
106
111
|
// Note: This error isn't pushed to the errors array, because it's not an
|
|
107
112
|
// error in the manifest itself.
|
|
108
113
|
throw new Error(`Failed to update snap.manifest.json: ${error.message}`);
|
|
109
114
|
}
|
|
110
115
|
}
|
|
111
|
-
return {
|
|
112
|
-
manifest: validatedManifest,
|
|
113
|
-
updated,
|
|
114
|
-
warnings,
|
|
115
|
-
errors
|
|
116
|
-
};
|
|
116
|
+
return { manifest: validatedManifest, updated, warnings, errors };
|
|
117
117
|
}
|
|
118
|
+
exports.checkManifest = checkManifest;
|
|
118
119
|
/**
|
|
119
120
|
* Given the relevant Snap files (manifest, `package.json`, and bundle) and a
|
|
120
121
|
* Snap manifest validation error, fixes the fault in the manifest that caused
|
|
@@ -123,30 +124,35 @@ const MANIFEST_SORT_ORDER = {
|
|
|
123
124
|
* @param snapFiles - The contents of all Snap files.
|
|
124
125
|
* @param error - The {@link ProgrammaticallyFixableSnapError} that was thrown.
|
|
125
126
|
* @returns A copy of the manifest file where the cause of the error is fixed.
|
|
126
|
-
*/
|
|
127
|
+
*/
|
|
128
|
+
function fixManifest(snapFiles, error) {
|
|
127
129
|
const { manifest, packageJson } = snapFiles;
|
|
128
130
|
const clonedFile = manifest.clone();
|
|
129
131
|
const manifestCopy = clonedFile.result;
|
|
130
|
-
switch(error.reason){
|
|
131
|
-
case SnapValidationFailureReason.NameMismatch:
|
|
132
|
+
switch (error.reason) {
|
|
133
|
+
case types_1.SnapValidationFailureReason.NameMismatch:
|
|
132
134
|
manifestCopy.source.location.npm.packageName = packageJson.result.name;
|
|
133
135
|
break;
|
|
134
|
-
case SnapValidationFailureReason.VersionMismatch:
|
|
136
|
+
case types_1.SnapValidationFailureReason.VersionMismatch:
|
|
135
137
|
manifestCopy.version = packageJson.result.version;
|
|
136
138
|
break;
|
|
137
|
-
case SnapValidationFailureReason.RepositoryMismatch:
|
|
138
|
-
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;
|
|
139
143
|
break;
|
|
140
|
-
case SnapValidationFailureReason.ShasumMismatch:
|
|
141
|
-
manifestCopy.source.shasum = getSnapChecksum(snapFiles);
|
|
144
|
+
case types_1.SnapValidationFailureReason.ShasumMismatch:
|
|
145
|
+
manifestCopy.source.shasum = (0, snaps_1.getSnapChecksum)(snapFiles);
|
|
142
146
|
break;
|
|
143
|
-
/* istanbul ignore next */
|
|
144
|
-
|
|
147
|
+
/* istanbul ignore next */
|
|
148
|
+
default:
|
|
149
|
+
(0, utils_1.assertExhaustive)(error.reason);
|
|
145
150
|
}
|
|
146
151
|
clonedFile.result = manifestCopy;
|
|
147
152
|
clonedFile.value = JSON.stringify(manifestCopy);
|
|
148
153
|
return clonedFile;
|
|
149
154
|
}
|
|
155
|
+
exports.fixManifest = fixManifest;
|
|
150
156
|
/**
|
|
151
157
|
* Given an unvalidated Snap manifest, attempts to extract the location of the
|
|
152
158
|
* bundle source file location and read the file.
|
|
@@ -155,27 +161,31 @@ const MANIFEST_SORT_ORDER = {
|
|
|
155
161
|
* @param manifest - The unvalidated Snap manifest file contents.
|
|
156
162
|
* @param sourceCode - Override source code for plugins.
|
|
157
163
|
* @returns The contents of the bundle file, if any.
|
|
158
|
-
*/
|
|
159
|
-
|
|
164
|
+
*/
|
|
165
|
+
async function getSnapSourceCode(basePath, manifest, sourceCode) {
|
|
166
|
+
if (!(0, utils_1.isPlainObject)(manifest)) {
|
|
160
167
|
return undefined;
|
|
161
168
|
}
|
|
162
|
-
const sourceFilePath = manifest.source?.location
|
|
169
|
+
const sourceFilePath = manifest.source?.location
|
|
170
|
+
?.npm?.filePath;
|
|
163
171
|
if (!sourceFilePath) {
|
|
164
172
|
return undefined;
|
|
165
173
|
}
|
|
166
174
|
if (sourceCode) {
|
|
167
|
-
return new VirtualFile({
|
|
168
|
-
path:
|
|
169
|
-
value: sourceCode
|
|
175
|
+
return new virtual_file_1.VirtualFile({
|
|
176
|
+
path: path_1.default.join(basePath, sourceFilePath),
|
|
177
|
+
value: sourceCode,
|
|
170
178
|
});
|
|
171
179
|
}
|
|
172
180
|
try {
|
|
173
|
-
const virtualFile = await readVirtualFile(
|
|
181
|
+
const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, sourceFilePath), 'utf8');
|
|
174
182
|
return virtualFile;
|
|
175
|
-
}
|
|
176
|
-
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
throw new Error(`Failed to read Snap bundle file: ${error.message}`);
|
|
177
186
|
}
|
|
178
187
|
}
|
|
188
|
+
exports.getSnapSourceCode = getSnapSourceCode;
|
|
179
189
|
/**
|
|
180
190
|
* Given an unvalidated Snap manifest, attempts to extract the location of the
|
|
181
191
|
* icon and read the file.
|
|
@@ -183,39 +193,44 @@ const MANIFEST_SORT_ORDER = {
|
|
|
183
193
|
* @param basePath - The path to the folder with the manifest files.
|
|
184
194
|
* @param manifest - The unvalidated Snap manifest file contents.
|
|
185
195
|
* @returns The contents of the icon, if any.
|
|
186
|
-
*/
|
|
187
|
-
|
|
196
|
+
*/
|
|
197
|
+
async function getSnapIcon(basePath, manifest) {
|
|
198
|
+
if (!(0, utils_1.isPlainObject)(manifest)) {
|
|
188
199
|
return undefined;
|
|
189
200
|
}
|
|
190
|
-
const iconPath = manifest.source?.location?.npm
|
|
201
|
+
const iconPath = manifest.source?.location?.npm
|
|
202
|
+
?.iconPath;
|
|
191
203
|
if (!iconPath) {
|
|
192
204
|
return undefined;
|
|
193
205
|
}
|
|
194
206
|
try {
|
|
195
|
-
const virtualFile = await readVirtualFile(
|
|
207
|
+
const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, iconPath), 'utf8');
|
|
196
208
|
return virtualFile;
|
|
197
|
-
}
|
|
198
|
-
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
throw new Error(`Failed to read Snap icon file: ${error.message}`);
|
|
199
212
|
}
|
|
200
213
|
}
|
|
214
|
+
exports.getSnapIcon = getSnapIcon;
|
|
201
215
|
/**
|
|
202
216
|
* Sorts the given manifest in our preferred sort order and removes the
|
|
203
217
|
* `repository` field if it is falsy (it may be `null`).
|
|
204
218
|
*
|
|
205
219
|
* @param manifest - The manifest to sort and modify.
|
|
206
220
|
* @returns The disk-ready manifest.
|
|
207
|
-
*/
|
|
221
|
+
*/
|
|
222
|
+
function getWritableManifest(manifest) {
|
|
208
223
|
const { repository, ...remaining } = manifest;
|
|
209
|
-
const keys = Object.keys(repository ? {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}), {});
|
|
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
|
+
}), {});
|
|
217
231
|
return writableManifest;
|
|
218
232
|
}
|
|
233
|
+
exports.getWritableManifest = getWritableManifest;
|
|
219
234
|
/**
|
|
220
235
|
* Validates the fields of an npm Snap manifest that has already passed JSON
|
|
221
236
|
* Schema validation.
|
|
@@ -225,7 +240,8 @@ const MANIFEST_SORT_ORDER = {
|
|
|
225
240
|
* @param snapFiles.packageJson - The npm Snap's `package.json`.
|
|
226
241
|
* @param snapFiles.sourceCode - The Snap's source code.
|
|
227
242
|
* @param snapFiles.svgIcon - The Snap's optional icon.
|
|
228
|
-
*/
|
|
243
|
+
*/
|
|
244
|
+
function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, }) {
|
|
229
245
|
const packageJsonName = packageJson.result.name;
|
|
230
246
|
const packageJsonVersion = packageJson.result.version;
|
|
231
247
|
const packageJsonRepository = packageJson.result.repository;
|
|
@@ -233,21 +249,19 @@ const MANIFEST_SORT_ORDER = {
|
|
|
233
249
|
const manifestPackageVersion = manifest.result.version;
|
|
234
250
|
const manifestRepository = manifest.result.repository;
|
|
235
251
|
if (packageJsonName !== manifestPackageName) {
|
|
236
|
-
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);
|
|
237
253
|
}
|
|
238
254
|
if (packageJsonVersion !== manifestPackageVersion) {
|
|
239
|
-
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);
|
|
240
256
|
}
|
|
241
|
-
if (
|
|
257
|
+
if (
|
|
258
|
+
// The repository may be `undefined` in package.json but can only be defined
|
|
242
259
|
// or `null` in the Snap manifest due to TS@<4.4 issues.
|
|
243
|
-
(packageJsonRepository || manifestRepository) &&
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
sourceCode,
|
|
249
|
-
svgIcon
|
|
250
|
-
}, `"${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.`);
|
|
251
265
|
}
|
|
252
|
-
|
|
266
|
+
exports.validateNpmSnapManifest = validateNpmSnapManifest;
|
|
253
267
|
//# sourceMappingURL=manifest.js.map
|