@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.
Files changed (268) hide show
  1. package/CHANGELOG.md +222 -11
  2. package/dist/{esm/array.js → array.js} +7 -3
  3. package/dist/array.js.map +1 -0
  4. package/dist/{types/caveats.d.ts → caveats.d.ts} +4 -0
  5. package/dist/caveats.js +35 -0
  6. package/dist/caveats.js.map +1 -0
  7. package/dist/checksum.js +42 -0
  8. package/dist/checksum.js.map +1 -0
  9. package/dist/{types/cronjob.d.ts → cronjob.d.ts} +1 -1
  10. package/dist/cronjob.js +71 -0
  11. package/dist/cronjob.js.map +1 -0
  12. package/dist/deep-clone.js +9 -0
  13. package/dist/deep-clone.js.map +1 -0
  14. package/dist/{esm/default-endowments.js → default-endowments.js} +6 -3
  15. package/dist/default-endowments.js.map +1 -0
  16. package/dist/entropy.js +8 -0
  17. package/dist/entropy.js.map +1 -0
  18. package/dist/{cjs/eval-worker.js → eval-worker.js} +21 -22
  19. package/dist/eval-worker.js.map +1 -0
  20. package/dist/{types/eval.d.ts → eval.d.ts} +1 -9
  21. package/dist/eval.js +27 -0
  22. package/dist/eval.js.map +1 -0
  23. package/dist/{types/fs.d.ts → fs.d.ts} +2 -2
  24. package/dist/{esm/fs.js → fs.js} +56 -33
  25. package/dist/fs.js.map +1 -0
  26. package/dist/handlers.d.ts +105 -0
  27. package/dist/handlers.js +3 -0
  28. package/dist/handlers.js.map +1 -0
  29. package/dist/{types/iframe.d.ts → iframe.d.ts} +2 -3
  30. package/dist/{esm/iframe.js → iframe.js} +18 -16
  31. package/dist/iframe.js.map +1 -0
  32. package/dist/iframe.test.browser.js +15 -0
  33. package/dist/iframe.test.browser.js.map +1 -0
  34. package/dist/{types/index.browser.d.ts → index.browser.d.ts} +1 -5
  35. package/dist/index.browser.js +37 -0
  36. package/dist/index.browser.js.map +1 -0
  37. package/dist/{types/index.d.ts → index.d.ts} +1 -5
  38. package/dist/index.executionenv.js +22 -0
  39. package/dist/index.executionenv.js.map +1 -0
  40. package/dist/index.js +42 -0
  41. package/dist/index.js.map +1 -0
  42. package/dist/{types/json-rpc.d.ts → json-rpc.d.ts} +2 -2
  43. package/dist/json-rpc.js +46 -0
  44. package/dist/json-rpc.js.map +1 -0
  45. package/dist/json.d.ts +9 -0
  46. package/dist/json.js +18 -0
  47. package/dist/json.js.map +1 -0
  48. package/dist/{esm/logging.js → logging.js} +14 -6
  49. package/dist/logging.js.map +1 -0
  50. package/dist/manifest/index.browser.js +18 -0
  51. package/dist/manifest/index.browser.js.map +1 -0
  52. package/dist/manifest/index.js +19 -0
  53. package/dist/manifest/index.js.map +1 -0
  54. package/dist/{types/manifest → manifest}/manifest.d.ts +4 -6
  55. package/dist/{esm/manifest → manifest}/manifest.js +115 -101
  56. package/dist/manifest/manifest.js.map +1 -0
  57. package/dist/{types/manifest → manifest}/validation.d.ts +76 -34
  58. package/dist/manifest/validation.js +141 -0
  59. package/dist/manifest/validation.js.map +1 -0
  60. package/dist/{esm/mock.js → mock.js} +40 -42
  61. package/dist/mock.js.map +1 -0
  62. package/dist/namespace.d.ts +275 -0
  63. package/dist/namespace.js +225 -0
  64. package/dist/namespace.js.map +1 -0
  65. package/dist/notification.d.ts +66 -0
  66. package/dist/notification.js +58 -0
  67. package/dist/notification.js.map +1 -0
  68. package/dist/{types/npm.d.ts → npm.d.ts} +3 -2
  69. package/dist/npm.js +74 -0
  70. package/dist/npm.js.map +1 -0
  71. package/dist/path.js +21 -0
  72. package/dist/path.js.map +1 -0
  73. package/dist/{esm/post-process.js → post-process.js} +99 -110
  74. package/dist/post-process.js.map +1 -0
  75. package/dist/{types/snaps.d.ts → snaps.d.ts} +9 -17
  76. package/dist/snaps.js +202 -0
  77. package/dist/snaps.js.map +1 -0
  78. package/dist/{types/types.d.ts → types.d.ts} +22 -15
  79. package/dist/types.js +103 -0
  80. package/dist/types.js.map +1 -0
  81. package/dist/{types/versions.d.ts → versions.d.ts} +1 -1
  82. package/dist/{esm/versions.js → versions.js} +18 -15
  83. package/dist/versions.js.map +1 -0
  84. package/dist/{esm/virtual-file → virtual-file}/VirtualFile.js +33 -47
  85. package/dist/virtual-file/VirtualFile.js.map +1 -0
  86. package/dist/virtual-file/index.browser.js +18 -0
  87. package/dist/virtual-file/index.browser.js.map +1 -0
  88. package/dist/virtual-file/index.js +19 -0
  89. package/dist/virtual-file/index.js.map +1 -0
  90. package/dist/virtual-file/toVirtualFile.js +30 -0
  91. package/dist/virtual-file/toVirtualFile.js.map +1 -0
  92. package/package.json +40 -59
  93. package/dist/cjs/array.js +0 -23
  94. package/dist/cjs/array.js.map +0 -1
  95. package/dist/cjs/caveats.js +0 -33
  96. package/dist/cjs/caveats.js.map +0 -1
  97. package/dist/cjs/checksum.js +0 -38
  98. package/dist/cjs/checksum.js.map +0 -1
  99. package/dist/cjs/cronjob.js +0 -84
  100. package/dist/cjs/cronjob.js.map +0 -1
  101. package/dist/cjs/deep-clone.js +0 -22
  102. package/dist/cjs/deep-clone.js.map +0 -1
  103. package/dist/cjs/default-endowments.js +0 -49
  104. package/dist/cjs/default-endowments.js.map +0 -1
  105. package/dist/cjs/entropy.js +0 -23
  106. package/dist/cjs/entropy.js.map +0 -1
  107. package/dist/cjs/enum.js +0 -16
  108. package/dist/cjs/enum.js.map +0 -1
  109. package/dist/cjs/errors.js +0 -19
  110. package/dist/cjs/errors.js.map +0 -1
  111. package/dist/cjs/eval-worker.js.map +0 -1
  112. package/dist/cjs/eval.js +0 -77
  113. package/dist/cjs/eval.js.map +0 -1
  114. package/dist/cjs/fs.js +0 -126
  115. package/dist/cjs/fs.js.map +0 -1
  116. package/dist/cjs/handlers.js +0 -65
  117. package/dist/cjs/handlers.js.map +0 -1
  118. package/dist/cjs/icon.js +0 -37
  119. package/dist/cjs/icon.js.map +0 -1
  120. package/dist/cjs/iframe.js +0 -59
  121. package/dist/cjs/iframe.js.map +0 -1
  122. package/dist/cjs/index.browser.js +0 -43
  123. package/dist/cjs/index.browser.js.map +0 -1
  124. package/dist/cjs/index.executionenv.js +0 -24
  125. package/dist/cjs/index.executionenv.js.map +0 -1
  126. package/dist/cjs/index.js +0 -48
  127. package/dist/cjs/index.js.map +0 -1
  128. package/dist/cjs/json-rpc.js +0 -46
  129. package/dist/cjs/json-rpc.js.map +0 -1
  130. package/dist/cjs/json.js +0 -16
  131. package/dist/cjs/json.js.map +0 -1
  132. package/dist/cjs/logging.js +0 -40
  133. package/dist/cjs/logging.js.map +0 -1
  134. package/dist/cjs/manifest/index.browser.js +0 -20
  135. package/dist/cjs/manifest/index.browser.js.map +0 -1
  136. package/dist/cjs/manifest/index.js +0 -21
  137. package/dist/cjs/manifest/index.js.map +0 -1
  138. package/dist/cjs/manifest/manifest.js +0 -239
  139. package/dist/cjs/manifest/manifest.js.map +0 -1
  140. package/dist/cjs/manifest/validation.js +0 -183
  141. package/dist/cjs/manifest/validation.js.map +0 -1
  142. package/dist/cjs/mock.js +0 -128
  143. package/dist/cjs/mock.js.map +0 -1
  144. package/dist/cjs/namespace.js +0 -124
  145. package/dist/cjs/namespace.js.map +0 -1
  146. package/dist/cjs/npm.js +0 -81
  147. package/dist/cjs/npm.js.map +0 -1
  148. package/dist/cjs/path.js +0 -21
  149. package/dist/cjs/path.js.map +0 -1
  150. package/dist/cjs/post-process.js +0 -328
  151. package/dist/cjs/post-process.js.map +0 -1
  152. package/dist/cjs/snaps.js +0 -230
  153. package/dist/cjs/snaps.js.map +0 -1
  154. package/dist/cjs/strings.js +0 -21
  155. package/dist/cjs/strings.js.map +0 -1
  156. package/dist/cjs/structs.js +0 -163
  157. package/dist/cjs/structs.js.map +0 -1
  158. package/dist/cjs/types.js +0 -109
  159. package/dist/cjs/types.js.map +0 -1
  160. package/dist/cjs/validation.js +0 -22
  161. package/dist/cjs/validation.js.map +0 -1
  162. package/dist/cjs/versions.js +0 -47
  163. package/dist/cjs/versions.js.map +0 -1
  164. package/dist/cjs/virtual-file/VirtualFile.js +0 -85
  165. package/dist/cjs/virtual-file/VirtualFile.js.map +0 -1
  166. package/dist/cjs/virtual-file/index.browser.js +0 -20
  167. package/dist/cjs/virtual-file/index.browser.js.map +0 -1
  168. package/dist/cjs/virtual-file/index.js +0 -21
  169. package/dist/cjs/virtual-file/index.js.map +0 -1
  170. package/dist/cjs/virtual-file/toVirtualFile.js +0 -33
  171. package/dist/cjs/virtual-file/toVirtualFile.js.map +0 -1
  172. package/dist/esm/array.js.map +0 -1
  173. package/dist/esm/caveats.js +0 -23
  174. package/dist/esm/caveats.js.map +0 -1
  175. package/dist/esm/checksum.js +0 -36
  176. package/dist/esm/checksum.js.map +0 -1
  177. package/dist/esm/cronjob.js +0 -66
  178. package/dist/esm/cronjob.js.map +0 -1
  179. package/dist/esm/deep-clone.js +0 -7
  180. package/dist/esm/deep-clone.js.map +0 -1
  181. package/dist/esm/default-endowments.js.map +0 -1
  182. package/dist/esm/entropy.js +0 -6
  183. package/dist/esm/entropy.js.map +0 -1
  184. package/dist/esm/enum.js +0 -12
  185. package/dist/esm/enum.js.map +0 -1
  186. package/dist/esm/errors.js +0 -17
  187. package/dist/esm/errors.js.map +0 -1
  188. package/dist/esm/eval-worker.js +0 -47
  189. package/dist/esm/eval-worker.js.map +0 -1
  190. package/dist/esm/eval.js +0 -65
  191. package/dist/esm/eval.js.map +0 -1
  192. package/dist/esm/fs.js.map +0 -1
  193. package/dist/esm/handlers.js +0 -47
  194. package/dist/esm/handlers.js.map +0 -1
  195. package/dist/esm/icon.js +0 -11
  196. package/dist/esm/icon.js.map +0 -1
  197. package/dist/esm/iframe.js.map +0 -1
  198. package/dist/esm/index.browser.js +0 -26
  199. package/dist/esm/index.browser.js.map +0 -1
  200. package/dist/esm/index.executionenv.js +0 -7
  201. package/dist/esm/index.executionenv.js.map +0 -1
  202. package/dist/esm/index.js +0 -31
  203. package/dist/esm/index.js.map +0 -1
  204. package/dist/esm/json-rpc.js +0 -39
  205. package/dist/esm/json-rpc.js.map +0 -1
  206. package/dist/esm/json.js +0 -17
  207. package/dist/esm/json.js.map +0 -1
  208. package/dist/esm/logging.js.map +0 -1
  209. package/dist/esm/manifest/index.browser.js +0 -3
  210. package/dist/esm/manifest/index.browser.js.map +0 -1
  211. package/dist/esm/manifest/index.js +0 -4
  212. package/dist/esm/manifest/index.js.map +0 -1
  213. package/dist/esm/manifest/manifest.js.map +0 -1
  214. package/dist/esm/manifest/validation.js +0 -152
  215. package/dist/esm/manifest/validation.js.map +0 -1
  216. package/dist/esm/mock.js.map +0 -1
  217. package/dist/esm/namespace.js +0 -110
  218. package/dist/esm/namespace.js.map +0 -1
  219. package/dist/esm/npm.js +0 -70
  220. package/dist/esm/npm.js.map +0 -1
  221. package/dist/esm/path.js +0 -17
  222. package/dist/esm/path.js.map +0 -1
  223. package/dist/esm/post-process.js.map +0 -1
  224. package/dist/esm/snaps.js +0 -215
  225. package/dist/esm/snaps.js.map +0 -1
  226. package/dist/esm/strings.js +0 -11
  227. package/dist/esm/strings.js.map +0 -1
  228. package/dist/esm/structs.js +0 -230
  229. package/dist/esm/structs.js.map +0 -1
  230. package/dist/esm/types.js +0 -80
  231. package/dist/esm/types.js.map +0 -1
  232. package/dist/esm/validation.js +0 -17
  233. package/dist/esm/validation.js.map +0 -1
  234. package/dist/esm/versions.js.map +0 -1
  235. package/dist/esm/virtual-file/VirtualFile.js.map +0 -1
  236. package/dist/esm/virtual-file/index.browser.js +0 -3
  237. package/dist/esm/virtual-file/index.browser.js.map +0 -1
  238. package/dist/esm/virtual-file/index.js +0 -4
  239. package/dist/esm/virtual-file/index.js.map +0 -1
  240. package/dist/esm/virtual-file/toVirtualFile.js +0 -26
  241. package/dist/esm/virtual-file/toVirtualFile.js.map +0 -1
  242. package/dist/types/enum.d.ts +0 -30
  243. package/dist/types/errors.d.ts +0 -10
  244. package/dist/types/handlers.d.ts +0 -144
  245. package/dist/types/icon.d.ts +0 -4
  246. package/dist/types/json.d.ts +0 -13
  247. package/dist/types/namespace.d.ts +0 -124
  248. package/dist/types/strings.d.ts +0 -8
  249. package/dist/types/structs.d.ts +0 -158
  250. package/dist/types/validation.d.ts +0 -8
  251. /package/dist/{types/array.d.ts → array.d.ts} +0 -0
  252. /package/dist/{types/checksum.d.ts → checksum.d.ts} +0 -0
  253. /package/dist/{types/deep-clone.d.ts → deep-clone.d.ts} +0 -0
  254. /package/dist/{types/default-endowments.d.ts → default-endowments.d.ts} +0 -0
  255. /package/dist/{types/entropy.d.ts → entropy.d.ts} +0 -0
  256. /package/dist/{types/eval-worker.d.ts → eval-worker.d.ts} +0 -0
  257. /package/dist/{types/iframe.test.browser.d.ts → iframe.test.browser.d.ts} +0 -0
  258. /package/dist/{types/index.executionenv.d.ts → index.executionenv.d.ts} +0 -0
  259. /package/dist/{types/logging.d.ts → logging.d.ts} +0 -0
  260. /package/dist/{types/manifest → manifest}/index.browser.d.ts +0 -0
  261. /package/dist/{types/manifest → manifest}/index.d.ts +0 -0
  262. /package/dist/{types/mock.d.ts → mock.d.ts} +0 -0
  263. /package/dist/{types/path.d.ts → path.d.ts} +0 -0
  264. /package/dist/{types/post-process.d.ts → post-process.d.ts} +0 -0
  265. /package/dist/{types/virtual-file → virtual-file}/VirtualFile.d.ts +0 -0
  266. /package/dist/{types/virtual-file → virtual-file}/index.browser.d.ts +0 -0
  267. /package/dist/{types/virtual-file → virtual-file}/index.d.ts +0 -0
  268. /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 type { Json, JsonRpcSuccess, AssertionErrorConstructor } from '@metamask/utils';
2
- import type { Infer } from 'superstruct';
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;
@@ -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
@@ -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
- import { createProjectLogger } from '@metamask/utils';
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
- export const snapsLogger = createProjectLogger('snaps');
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
- */ export function logInfo(message, ...optionalParams) {
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
- */ export function logError(error, ...optionalParams) {
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
- */ export function logWarning(message, ...optionalParams) {
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 type { Json } from '@metamask/utils';
1
+ import { Json } from '@metamask/utils';
2
2
  import { ProgrammaticallyFixableSnapError } from '../snaps';
3
- import type { SnapFiles } from '../types';
3
+ import { SnapFiles } from '../types';
4
4
  import { VirtualFile } from '../virtual-file';
5
- import type { SnapManifest } from './validation';
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, writeFileFn?: WriteFileFunction): Promise<CheckManifestResult>;
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
- import { assertExhaustive, assert, isPlainObject } from '@metamask/utils';
2
- import deepEqual from 'fast-deep-equal';
3
- import { promises as fs } from 'fs';
4
- import pathUtils from 'path';
5
- import { deepClone } from '../deep-clone';
6
- import { readJsonFile } from '../fs';
7
- import { validateNpmSnap } from '../npm';
8
- import { getSnapChecksum, ProgrammaticallyFixableSnapError, validateSnapShasum } from '../snaps';
9
- import { NpmSnapFileNames, SnapValidationFailureReason } from '../types';
10
- import { readVirtualFile, VirtualFile } from '../virtual-file';
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
- $schema: 1,
13
- version: 2,
14
- description: 3,
15
- proposedName: 4,
16
- repository: 5,
17
- source: 6,
18
- initialPermissions: 7,
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
- */ export async function checkManifest(basePath, writeManifest = true, sourceCode, writeFileFn = fs.writeFile) {
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 = pathUtils.join(basePath, NpmSnapFileNames.Manifest);
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(pathUtils.join(basePath, NpmSnapFileNames.PackageJson));
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
- } catch (error) {
50
- if (error instanceof ProgrammaticallyFixableSnapError) {
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
- manifest
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
- } catch (nextValidationError) {
74
+ }
75
+ catch (nextValidationError) {
73
76
  currentError = nextValidationError;
74
- /* istanbul ignore next: this should be impossible */ if (!(nextValidationError instanceof ProgrammaticallyFixableSnapError) || attempts === maxAttempts && !isInvalid) {
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
- } else {
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
- 'repository'
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 writeFileFn(pathUtils.join(basePath, NpmSnapFileNames.Manifest), newManifest);
107
+ await fs_1.promises.writeFile(path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest), newManifest);
104
108
  }
105
- } catch (error) {
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
- */ export function fixManifest(snapFiles, error) {
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 ? deepClone(packageJson.result.repository) : undefined;
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 */ default:
144
- assertExhaustive(error.reason);
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
- */ export async function getSnapSourceCode(basePath, manifest, sourceCode) {
159
- if (!isPlainObject(manifest)) {
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?.npm?.filePath;
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: pathUtils.join(basePath, sourceFilePath),
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(pathUtils.join(basePath, sourceFilePath), 'utf8');
181
+ const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, sourceFilePath), 'utf8');
174
182
  return virtualFile;
175
- } catch (error) {
176
- throw new Error(`Failed to read snap bundle file: ${error.message}`);
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
- */ export async function getSnapIcon(basePath, manifest) {
187
- if (!isPlainObject(manifest)) {
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?.iconPath;
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(pathUtils.join(basePath, iconPath), 'utf8');
207
+ const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, iconPath), 'utf8');
196
208
  return virtualFile;
197
- } catch (error) {
198
- throw new Error(`Failed to read snap icon file: ${error.message}`);
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
- */ export function getWritableManifest(manifest) {
221
+ */
222
+ function getWritableManifest(manifest) {
208
223
  const { repository, ...remaining } = manifest;
209
- const keys = Object.keys(repository ? {
210
- ...remaining,
211
- repository
212
- } : remaining);
213
- const writableManifest = keys.sort((a, b)=>MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b]).reduce((result, key)=>({
214
- ...result,
215
- [key]: manifest[key]
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
- */ export function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon }) {
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 (// The repository may be `undefined` in package.json but can only be defined
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) && !deepEqual(packageJsonRepository, manifestRepository)) {
244
- throw new ProgrammaticallyFixableSnapError(`"${NpmSnapFileNames.Manifest}" "repository" field does not match the "${NpmSnapFileNames.PackageJson}" "repository" field.`, SnapValidationFailureReason.RepositoryMismatch);
245
- }
246
- validateSnapShasum({
247
- manifest,
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