@metamask/snaps-utils 11.3.0 → 11.5.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 (148) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/dist/base64.cjs +3 -2
  3. package/dist/base64.cjs.map +1 -1
  4. package/dist/base64.d.cts.map +1 -1
  5. package/dist/base64.d.mts.map +1 -1
  6. package/dist/base64.mjs +3 -2
  7. package/dist/base64.mjs.map +1 -1
  8. package/dist/manifest/manifest.cjs +18 -11
  9. package/dist/manifest/manifest.cjs.map +1 -1
  10. package/dist/manifest/manifest.d.cts +10 -2
  11. package/dist/manifest/manifest.d.cts.map +1 -1
  12. package/dist/manifest/manifest.d.mts +10 -2
  13. package/dist/manifest/manifest.d.mts.map +1 -1
  14. package/dist/manifest/manifest.mjs +19 -12
  15. package/dist/manifest/manifest.mjs.map +1 -1
  16. package/dist/manifest/validation.cjs +8 -4
  17. package/dist/manifest/validation.cjs.map +1 -1
  18. package/dist/manifest/validation.d.cts +4 -0
  19. package/dist/manifest/validation.d.cts.map +1 -1
  20. package/dist/manifest/validation.d.mts +4 -0
  21. package/dist/manifest/validation.d.mts.map +1 -1
  22. package/dist/manifest/validation.mjs +7 -3
  23. package/dist/manifest/validation.mjs.map +1 -1
  24. package/dist/manifest/validator-types.cjs.map +1 -1
  25. package/dist/manifest/validator-types.d.cts +4 -2
  26. package/dist/manifest/validator-types.d.cts.map +1 -1
  27. package/dist/manifest/validator-types.d.mts +4 -2
  28. package/dist/manifest/validator-types.d.mts.map +1 -1
  29. package/dist/manifest/validator-types.mjs.map +1 -1
  30. package/dist/manifest/validator.cjs +26 -5
  31. package/dist/manifest/validator.cjs.map +1 -1
  32. package/dist/manifest/validator.d.cts +10 -1
  33. package/dist/manifest/validator.d.cts.map +1 -1
  34. package/dist/manifest/validator.d.mts +10 -1
  35. package/dist/manifest/validator.d.mts.map +1 -1
  36. package/dist/manifest/validator.mjs +24 -4
  37. package/dist/manifest/validator.mjs.map +1 -1
  38. package/dist/manifest/validators/checksum.cjs +1 -1
  39. package/dist/manifest/validators/checksum.cjs.map +1 -1
  40. package/dist/manifest/validators/checksum.d.cts.map +1 -1
  41. package/dist/manifest/validators/checksum.d.mts.map +1 -1
  42. package/dist/manifest/validators/checksum.mjs +1 -1
  43. package/dist/manifest/validators/checksum.mjs.map +1 -1
  44. package/dist/manifest/validators/expected-files.cjs +1 -1
  45. package/dist/manifest/validators/expected-files.cjs.map +1 -1
  46. package/dist/manifest/validators/expected-files.d.cts.map +1 -1
  47. package/dist/manifest/validators/expected-files.d.mts.map +1 -1
  48. package/dist/manifest/validators/expected-files.mjs +1 -1
  49. package/dist/manifest/validators/expected-files.mjs.map +1 -1
  50. package/dist/manifest/validators/icon-declared.cjs +1 -1
  51. package/dist/manifest/validators/icon-declared.cjs.map +1 -1
  52. package/dist/manifest/validators/icon-declared.d.cts.map +1 -1
  53. package/dist/manifest/validators/icon-declared.d.mts.map +1 -1
  54. package/dist/manifest/validators/icon-declared.mjs +1 -1
  55. package/dist/manifest/validators/icon-declared.mjs.map +1 -1
  56. package/dist/manifest/validators/icon-dimensions.cjs +1 -1
  57. package/dist/manifest/validators/icon-dimensions.cjs.map +1 -1
  58. package/dist/manifest/validators/icon-dimensions.d.cts.map +1 -1
  59. package/dist/manifest/validators/icon-dimensions.d.mts.map +1 -1
  60. package/dist/manifest/validators/icon-dimensions.mjs +1 -1
  61. package/dist/manifest/validators/icon-dimensions.mjs.map +1 -1
  62. package/dist/manifest/validators/icon-missing.cjs +1 -1
  63. package/dist/manifest/validators/icon-missing.cjs.map +1 -1
  64. package/dist/manifest/validators/icon-missing.mjs +1 -1
  65. package/dist/manifest/validators/icon-missing.mjs.map +1 -1
  66. package/dist/manifest/validators/is-localization-file.cjs +1 -1
  67. package/dist/manifest/validators/is-localization-file.cjs.map +1 -1
  68. package/dist/manifest/validators/is-localization-file.d.cts.map +1 -1
  69. package/dist/manifest/validators/is-localization-file.d.mts.map +1 -1
  70. package/dist/manifest/validators/is-localization-file.mjs +1 -1
  71. package/dist/manifest/validators/is-localization-file.mjs.map +1 -1
  72. package/dist/manifest/validators/is-package-json.cjs +1 -1
  73. package/dist/manifest/validators/is-package-json.cjs.map +1 -1
  74. package/dist/manifest/validators/is-package-json.d.cts.map +1 -1
  75. package/dist/manifest/validators/is-package-json.d.mts.map +1 -1
  76. package/dist/manifest/validators/is-package-json.mjs +1 -1
  77. package/dist/manifest/validators/is-package-json.mjs.map +1 -1
  78. package/dist/manifest/validators/is-snap-icon.cjs +1 -1
  79. package/dist/manifest/validators/is-snap-icon.cjs.map +1 -1
  80. package/dist/manifest/validators/is-snap-icon.mjs +1 -1
  81. package/dist/manifest/validators/is-snap-icon.mjs.map +1 -1
  82. package/dist/manifest/validators/is-snap-manifest.cjs +1 -1
  83. package/dist/manifest/validators/is-snap-manifest.cjs.map +1 -1
  84. package/dist/manifest/validators/is-snap-manifest.d.cts.map +1 -1
  85. package/dist/manifest/validators/is-snap-manifest.d.mts.map +1 -1
  86. package/dist/manifest/validators/is-snap-manifest.mjs +1 -1
  87. package/dist/manifest/validators/is-snap-manifest.mjs.map +1 -1
  88. package/dist/manifest/validators/manifest-localization.cjs +1 -1
  89. package/dist/manifest/validators/manifest-localization.cjs.map +1 -1
  90. package/dist/manifest/validators/manifest-localization.d.cts.map +1 -1
  91. package/dist/manifest/validators/manifest-localization.d.mts.map +1 -1
  92. package/dist/manifest/validators/manifest-localization.mjs +1 -1
  93. package/dist/manifest/validators/manifest-localization.mjs.map +1 -1
  94. package/dist/manifest/validators/package-json-recommended-fields.cjs +1 -1
  95. package/dist/manifest/validators/package-json-recommended-fields.cjs.map +1 -1
  96. package/dist/manifest/validators/package-json-recommended-fields.d.cts.map +1 -1
  97. package/dist/manifest/validators/package-json-recommended-fields.d.mts.map +1 -1
  98. package/dist/manifest/validators/package-json-recommended-fields.mjs +1 -1
  99. package/dist/manifest/validators/package-json-recommended-fields.mjs.map +1 -1
  100. package/dist/manifest/validators/package-name-match.cjs +1 -1
  101. package/dist/manifest/validators/package-name-match.cjs.map +1 -1
  102. package/dist/manifest/validators/package-name-match.d.cts.map +1 -1
  103. package/dist/manifest/validators/package-name-match.d.mts.map +1 -1
  104. package/dist/manifest/validators/package-name-match.mjs +1 -1
  105. package/dist/manifest/validators/package-name-match.mjs.map +1 -1
  106. package/dist/manifest/validators/platform-version.cjs +2 -2
  107. package/dist/manifest/validators/platform-version.cjs.map +1 -1
  108. package/dist/manifest/validators/platform-version.d.cts.map +1 -1
  109. package/dist/manifest/validators/platform-version.d.mts.map +1 -1
  110. package/dist/manifest/validators/platform-version.mjs +2 -2
  111. package/dist/manifest/validators/platform-version.mjs.map +1 -1
  112. package/dist/manifest/validators/production-platform-version.cjs +1 -1
  113. package/dist/manifest/validators/production-platform-version.cjs.map +1 -1
  114. package/dist/manifest/validators/production-platform-version.d.cts.map +1 -1
  115. package/dist/manifest/validators/production-platform-version.d.mts.map +1 -1
  116. package/dist/manifest/validators/production-platform-version.mjs +1 -1
  117. package/dist/manifest/validators/production-platform-version.mjs.map +1 -1
  118. package/dist/manifest/validators/repository-match.cjs +1 -1
  119. package/dist/manifest/validators/repository-match.cjs.map +1 -1
  120. package/dist/manifest/validators/repository-match.d.cts.map +1 -1
  121. package/dist/manifest/validators/repository-match.d.mts.map +1 -1
  122. package/dist/manifest/validators/repository-match.mjs +1 -1
  123. package/dist/manifest/validators/repository-match.mjs.map +1 -1
  124. package/dist/manifest/validators/unused-exports.cjs +2 -2
  125. package/dist/manifest/validators/unused-exports.cjs.map +1 -1
  126. package/dist/manifest/validators/unused-exports.d.cts.map +1 -1
  127. package/dist/manifest/validators/unused-exports.d.mts.map +1 -1
  128. package/dist/manifest/validators/unused-exports.mjs +2 -2
  129. package/dist/manifest/validators/unused-exports.mjs.map +1 -1
  130. package/dist/manifest/validators/version-match.cjs +1 -1
  131. package/dist/manifest/validators/version-match.cjs.map +1 -1
  132. package/dist/manifest/validators/version-match.d.cts.map +1 -1
  133. package/dist/manifest/validators/version-match.d.mts.map +1 -1
  134. package/dist/manifest/validators/version-match.mjs +1 -1
  135. package/dist/manifest/validators/version-match.mjs.map +1 -1
  136. package/dist/platform-version.cjs.map +1 -1
  137. package/dist/platform-version.d.cts +2 -1
  138. package/dist/platform-version.d.cts.map +1 -1
  139. package/dist/platform-version.d.mts +2 -1
  140. package/dist/platform-version.d.mts.map +1 -1
  141. package/dist/platform-version.mjs.map +1 -1
  142. package/dist/snaps.cjs.map +1 -1
  143. package/dist/snaps.d.cts +6 -2
  144. package/dist/snaps.d.cts.map +1 -1
  145. package/dist/snaps.d.mts +6 -2
  146. package/dist/snaps.d.mts.map +1 -1
  147. package/dist/snaps.mjs.map +1 -1
  148. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -7,6 +7,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [11.5.0]
11
+
12
+ ### Added
13
+
14
+ - Merge manifest validation reports to include latest validation errors and warnings ([#3567](https://github.com/MetaMask/snaps/pull/3567))
15
+
16
+ ### Fixed
17
+
18
+ - Disable base64 optimization in React Native ([#3564](https://github.com/MetaMask/snaps/pull/3564))
19
+
20
+ ## [11.4.0]
21
+
22
+ ### Added
23
+
24
+ - Add support for eval in watch mode for Snaps CLI ([#3553](https://github.com/MetaMask/snaps/pull/3553))
25
+
26
+ ### Changed
27
+
28
+ - Bump ses from `1.13.1` to `1.14.0` ([#3557](https://github.com/MetaMask/snaps/pull/3557))
29
+
30
+ ### Fixed
31
+
32
+ - Fix typos in `SnapAuxiliaryFilesStruct` variable and `SnapAuxiliaryFile` type ([#3545](https://github.com/MetaMask/snaps/pull/3545))
33
+ - `SnapAuxilaryFilesStruct` and `SnapAuxilaryFile` are still exported for
34
+ backwards compatibility, but will be removed in a future release.
35
+
10
36
  ## [11.3.0]
11
37
 
12
38
  ### Changed
@@ -678,7 +704,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
678
704
  - The version of the package no longer needs to match the version of all other
679
705
  MetaMask Snaps packages.
680
706
 
681
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.3.0...HEAD
707
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.5.0...HEAD
708
+ [11.5.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.4.0...@metamask/snaps-utils@11.5.0
709
+ [11.4.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.3.0...@metamask/snaps-utils@11.4.0
682
710
  [11.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.2.0...@metamask/snaps-utils@11.3.0
683
711
  [11.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.1.0...@metamask/snaps-utils@11.2.0
684
712
  [11.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.0.0...@metamask/snaps-utils@11.1.0
package/dist/base64.cjs CHANGED
@@ -11,8 +11,9 @@ const bytes_1 = require("./bytes.cjs");
11
11
  */
12
12
  async function encodeBase64(input) {
13
13
  const bytes = (0, bytes_1.getBytes)(input);
14
- // In the browser, FileReader is much faster than bytesToBase64.
15
- if ('FileReader' in globalThis) {
14
+ const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
15
+ // In the browser, FileReader is much faster than bytesToBase64. This is not supported in React Native however.
16
+ if ('FileReader' in globalThis && !isReactNative) {
16
17
  return await new Promise((resolve, reject) => {
17
18
  const reader = Object.assign(new FileReader(), {
18
19
  onload: () => resolve(reader.result.replace('data:application/octet-stream;base64,', '')),
@@ -1 +1 @@
1
- {"version":3,"file":"base64.cjs","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":";;;AAAA,2CAAgD;AAEhD,uCAAmC;AAGnC;;;;;GAKG;AACI,KAAK,UAAU,YAAY,CAAC,KAAwC;IACzE,MAAM,KAAK,GAAG,IAAA,gBAAQ,EAAC,KAAK,CAAC,CAAC;IAC9B,gEAAgE;IAChE,IAAI,YAAY,IAAI,UAAU,EAAE,CAAC;QAC/B,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,UAAU,EAAE,EAAE;gBAC7C,MAAM,EAAE,GAAG,EAAE,CACX,OAAO,CACJ,MAAM,CAAC,MAAiB,CAAC,OAAO,CAC/B,uCAAuC,EACvC,EAAE,CACH,CACF;gBACH,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;aACpC,CAAC,CAAC;YACH,MAAM,CAAC,aAAa,CAClB,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,0BAA0B,EAAE,CAAC,CAC5D,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,IAAA,qBAAa,EAAC,KAAK,CAAC,CAAC;AAC9B,CAAC;AArBD,oCAqBC;AAED;;;;;GAKG;AACI,KAAK,UAAU,YAAY,CAAC,MAAc;IAC/C,MAAM,QAAQ,GAAG,MAAM,KAAK,CAC1B,wCAAwC,MAAM,EAAE,CACjD,CAAC;IACF,OAAO,IAAI,UAAU,CAAC,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;AACtD,CAAC;AALD,oCAKC","sourcesContent":["import { bytesToBase64 } from '@metamask/utils';\n\nimport { getBytes } from './bytes';\nimport type { VirtualFile } from './virtual-file';\n\n/**\n * Provides fast, asynchronous base64 encoding.\n *\n * @param input - The input value, assumed to be coercible to bytes.\n * @returns A base64 string.\n */\nexport async function encodeBase64(input: Uint8Array | VirtualFile | string) {\n const bytes = getBytes(input);\n // In the browser, FileReader is much faster than bytesToBase64.\n if ('FileReader' in globalThis) {\n return await new Promise((resolve, reject) => {\n const reader = Object.assign(new FileReader(), {\n onload: () =>\n resolve(\n (reader.result as string).replace(\n 'data:application/octet-stream;base64,',\n '',\n ),\n ),\n onerror: () => reject(reader.error),\n });\n reader.readAsDataURL(\n new File([bytes], '', { type: 'application/octet-stream' }),\n );\n });\n }\n return bytesToBase64(bytes);\n}\n\n/**\n * Provides fast, asynchronous base64 decoding.\n *\n * @param base64 - A base64 string.\n * @returns A Uint8Array of bytes.\n */\nexport async function decodeBase64(base64: string) {\n const response = await fetch(\n `data:application/octet-stream;base64,${base64}`,\n );\n return new Uint8Array(await response.arrayBuffer());\n}\n"]}
1
+ {"version":3,"file":"base64.cjs","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":";;;AAAA,2CAAgD;AAEhD,uCAAmC;AAGnC;;;;;GAKG;AACI,KAAK,UAAU,YAAY,CAAC,KAAwC;IACzE,MAAM,KAAK,GAAG,IAAA,gBAAQ,EAAC,KAAK,CAAC,CAAC;IAC9B,MAAM,aAAa,GACjB,OAAO,SAAS,KAAK,WAAW,IAAI,SAAS,CAAC,OAAO,KAAK,aAAa,CAAC;IAC1E,+GAA+G;IAC/G,IAAI,YAAY,IAAI,UAAU,IAAI,CAAC,aAAa,EAAE,CAAC;QACjD,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,UAAU,EAAE,EAAE;gBAC7C,MAAM,EAAE,GAAG,EAAE,CACX,OAAO,CACJ,MAAM,CAAC,MAAiB,CAAC,OAAO,CAC/B,uCAAuC,EACvC,EAAE,CACH,CACF;gBACH,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;aACpC,CAAC,CAAC;YACH,MAAM,CAAC,aAAa,CAClB,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,0BAA0B,EAAE,CAAC,CAC5D,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,IAAA,qBAAa,EAAC,KAAK,CAAC,CAAC;AAC9B,CAAC;AAvBD,oCAuBC;AAED;;;;;GAKG;AACI,KAAK,UAAU,YAAY,CAAC,MAAc;IAC/C,MAAM,QAAQ,GAAG,MAAM,KAAK,CAC1B,wCAAwC,MAAM,EAAE,CACjD,CAAC;IACF,OAAO,IAAI,UAAU,CAAC,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;AACtD,CAAC;AALD,oCAKC","sourcesContent":["import { bytesToBase64 } from '@metamask/utils';\n\nimport { getBytes } from './bytes';\nimport type { VirtualFile } from './virtual-file';\n\n/**\n * Provides fast, asynchronous base64 encoding.\n *\n * @param input - The input value, assumed to be coercible to bytes.\n * @returns A base64 string.\n */\nexport async function encodeBase64(input: Uint8Array | VirtualFile | string) {\n const bytes = getBytes(input);\n const isReactNative =\n typeof navigator !== 'undefined' && navigator.product === 'ReactNative';\n // In the browser, FileReader is much faster than bytesToBase64. This is not supported in React Native however.\n if ('FileReader' in globalThis && !isReactNative) {\n return await new Promise((resolve, reject) => {\n const reader = Object.assign(new FileReader(), {\n onload: () =>\n resolve(\n (reader.result as string).replace(\n 'data:application/octet-stream;base64,',\n '',\n ),\n ),\n onerror: () => reject(reader.error),\n });\n reader.readAsDataURL(\n new File([bytes], '', { type: 'application/octet-stream' }),\n );\n });\n }\n return bytesToBase64(bytes);\n}\n\n/**\n * Provides fast, asynchronous base64 decoding.\n *\n * @param base64 - A base64 string.\n * @returns A Uint8Array of bytes.\n */\nexport async function decodeBase64(base64: string) {\n const response = await fetch(\n `data:application/octet-stream;base64,${base64}`,\n );\n return new Uint8Array(await response.arrayBuffer());\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"base64.d.cts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,iCAAuB;AAElD;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,oBAqB1E;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,MAAM,EAAE,MAAM,uBAKhD"}
1
+ {"version":3,"file":"base64.d.cts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,iCAAuB;AAElD;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,oBAuB1E;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,MAAM,EAAE,MAAM,uBAKhD"}
@@ -1 +1 @@
1
- {"version":3,"file":"base64.d.mts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,iCAAuB;AAElD;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,oBAqB1E;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,MAAM,EAAE,MAAM,uBAKhD"}
1
+ {"version":3,"file":"base64.d.mts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,iCAAuB;AAElD;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,oBAuB1E;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,MAAM,EAAE,MAAM,uBAKhD"}
package/dist/base64.mjs CHANGED
@@ -8,8 +8,9 @@ import { getBytes } from "./bytes.mjs";
8
8
  */
9
9
  export async function encodeBase64(input) {
10
10
  const bytes = getBytes(input);
11
- // In the browser, FileReader is much faster than bytesToBase64.
12
- if ('FileReader' in globalThis) {
11
+ const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
12
+ // In the browser, FileReader is much faster than bytesToBase64. This is not supported in React Native however.
13
+ if ('FileReader' in globalThis && !isReactNative) {
13
14
  return await new Promise((resolve, reject) => {
14
15
  const reader = Object.assign(new FileReader(), {
15
16
  onload: () => resolve(reader.result.replace('data:application/octet-stream;base64,', '')),
@@ -1 +1 @@
1
- {"version":3,"file":"base64.mjs","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,wBAAwB;AAEhD,OAAO,EAAE,QAAQ,EAAE,oBAAgB;AAGnC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,KAAwC;IACzE,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC9B,gEAAgE;IAChE,IAAI,YAAY,IAAI,UAAU,EAAE,CAAC;QAC/B,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,UAAU,EAAE,EAAE;gBAC7C,MAAM,EAAE,GAAG,EAAE,CACX,OAAO,CACJ,MAAM,CAAC,MAAiB,CAAC,OAAO,CAC/B,uCAAuC,EACvC,EAAE,CACH,CACF;gBACH,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;aACpC,CAAC,CAAC;YACH,MAAM,CAAC,aAAa,CAClB,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,0BAA0B,EAAE,CAAC,CAC5D,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,aAAa,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,MAAc;IAC/C,MAAM,QAAQ,GAAG,MAAM,KAAK,CAC1B,wCAAwC,MAAM,EAAE,CACjD,CAAC;IACF,OAAO,IAAI,UAAU,CAAC,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;AACtD,CAAC","sourcesContent":["import { bytesToBase64 } from '@metamask/utils';\n\nimport { getBytes } from './bytes';\nimport type { VirtualFile } from './virtual-file';\n\n/**\n * Provides fast, asynchronous base64 encoding.\n *\n * @param input - The input value, assumed to be coercible to bytes.\n * @returns A base64 string.\n */\nexport async function encodeBase64(input: Uint8Array | VirtualFile | string) {\n const bytes = getBytes(input);\n // In the browser, FileReader is much faster than bytesToBase64.\n if ('FileReader' in globalThis) {\n return await new Promise((resolve, reject) => {\n const reader = Object.assign(new FileReader(), {\n onload: () =>\n resolve(\n (reader.result as string).replace(\n 'data:application/octet-stream;base64,',\n '',\n ),\n ),\n onerror: () => reject(reader.error),\n });\n reader.readAsDataURL(\n new File([bytes], '', { type: 'application/octet-stream' }),\n );\n });\n }\n return bytesToBase64(bytes);\n}\n\n/**\n * Provides fast, asynchronous base64 decoding.\n *\n * @param base64 - A base64 string.\n * @returns A Uint8Array of bytes.\n */\nexport async function decodeBase64(base64: string) {\n const response = await fetch(\n `data:application/octet-stream;base64,${base64}`,\n );\n return new Uint8Array(await response.arrayBuffer());\n}\n"]}
1
+ {"version":3,"file":"base64.mjs","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,wBAAwB;AAEhD,OAAO,EAAE,QAAQ,EAAE,oBAAgB;AAGnC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,KAAwC;IACzE,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC9B,MAAM,aAAa,GACjB,OAAO,SAAS,KAAK,WAAW,IAAI,SAAS,CAAC,OAAO,KAAK,aAAa,CAAC;IAC1E,+GAA+G;IAC/G,IAAI,YAAY,IAAI,UAAU,IAAI,CAAC,aAAa,EAAE,CAAC;QACjD,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,UAAU,EAAE,EAAE;gBAC7C,MAAM,EAAE,GAAG,EAAE,CACX,OAAO,CACJ,MAAM,CAAC,MAAiB,CAAC,OAAO,CAC/B,uCAAuC,EACvC,EAAE,CACH,CACF;gBACH,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;aACpC,CAAC,CAAC;YACH,MAAM,CAAC,aAAa,CAClB,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,0BAA0B,EAAE,CAAC,CAC5D,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,aAAa,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,MAAc;IAC/C,MAAM,QAAQ,GAAG,MAAM,KAAK,CAC1B,wCAAwC,MAAM,EAAE,CACjD,CAAC;IACF,OAAO,IAAI,UAAU,CAAC,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;AACtD,CAAC","sourcesContent":["import { bytesToBase64 } from '@metamask/utils';\n\nimport { getBytes } from './bytes';\nimport type { VirtualFile } from './virtual-file';\n\n/**\n * Provides fast, asynchronous base64 encoding.\n *\n * @param input - The input value, assumed to be coercible to bytes.\n * @returns A base64 string.\n */\nexport async function encodeBase64(input: Uint8Array | VirtualFile | string) {\n const bytes = getBytes(input);\n const isReactNative =\n typeof navigator !== 'undefined' && navigator.product === 'ReactNative';\n // In the browser, FileReader is much faster than bytesToBase64. This is not supported in React Native however.\n if ('FileReader' in globalThis && !isReactNative) {\n return await new Promise((resolve, reject) => {\n const reader = Object.assign(new FileReader(), {\n onload: () =>\n resolve(\n (reader.result as string).replace(\n 'data:application/octet-stream;base64,',\n '',\n ),\n ),\n onerror: () => reject(reader.error),\n });\n reader.readAsDataURL(\n new File([bytes], '', { type: 'application/octet-stream' }),\n );\n });\n }\n return bytesToBase64(bytes);\n}\n\n/**\n * Provides fast, asynchronous base64 decoding.\n *\n * @param base64 - A base64 string.\n * @returns A Uint8Array of bytes.\n */\nexport async function decodeBase64(base64: string) {\n const response = await fetch(\n `data:application/octet-stream;base64,${base64}`,\n );\n return new Uint8Array(await response.arrayBuffer());\n}\n"]}
@@ -68,10 +68,12 @@ const MANIFEST_SORT_ORDER = {
68
68
  * handlers and their respective permission name. This must be provided to avoid
69
69
  * circular dependencies between `@metamask/snaps-utils` and
70
70
  * `@metamask/snaps-rpc-methods`.
71
+ * @param options.watchMode - Whether the compiler is running in watch mode.
72
+ * This is used to determine whether to fix warnings or errors only.
71
73
  * @returns Whether the manifest was updated, and an array of warnings that
72
74
  * were encountered during processing of the manifest files.
73
75
  */
74
- async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs_1.promises.writeFile, exports, handlerEndowments, } = {}) {
76
+ async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs_1.promises.writeFile, exports, handlerEndowments, watchMode = false, } = {}) {
75
77
  const manifestPath = path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest);
76
78
  const manifestFile = await (0, fs_2.readJsonFile)(manifestPath);
77
79
  const unvalidatedManifest = manifestFile.result;
@@ -103,8 +105,8 @@ async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCo
103
105
  files: validatorResults.files,
104
106
  reports: validatorResults.reports,
105
107
  };
106
- if (updateAndWriteManifest && (0, validator_1.hasFixes)(manifestResults)) {
107
- const fixedResults = await runFixes(validatorResults);
108
+ if (updateAndWriteManifest && (0, validator_1.hasFixes)(manifestResults, watchMode)) {
109
+ const fixedResults = await runFixes(validatorResults, undefined, watchMode);
108
110
  if (fixedResults.updated) {
109
111
  manifestResults = fixedResults;
110
112
  (0, utils_1.assert)(manifestResults.files);
@@ -132,19 +134,21 @@ exports.checkManifest = checkManifest;
132
134
  *
133
135
  * @param results - Results of the initial run of validation.
134
136
  * @param rules - Optional list of rules to run the fixes with.
137
+ * @param errorsOnly - Whether to only run fixes for errors, not warnings.
135
138
  * @returns The updated manifest and whether it was updated.
136
139
  */
137
- async function runFixes(results, rules) {
140
+ async function runFixes(results, rules, errorsOnly = false) {
138
141
  let shouldRunFixes = true;
139
142
  const MAX_ATTEMPTS = 10;
140
143
  (0, utils_1.assert)(results.files);
141
144
  let fixResults = results;
142
145
  (0, utils_1.assert)(fixResults.files);
143
146
  fixResults.files.manifest = fixResults.files.manifest.clone();
147
+ const mergedReports = (0, deep_clone_1.deepClone)(fixResults.reports);
144
148
  for (let attempts = 1; shouldRunFixes && attempts <= MAX_ATTEMPTS; attempts++) {
145
149
  (0, utils_1.assert)(fixResults.files);
146
150
  let manifest = fixResults.files.manifest.result;
147
- const fixable = fixResults.reports.filter((report) => report.fix);
151
+ const fixable = fixResults.reports.filter((report) => (0, validator_1.isReportFixable)(report, errorsOnly));
148
152
  for (const report of fixable) {
149
153
  (0, utils_1.assert)(report.fix);
150
154
  ({ manifest } = await report.fix({ manifest }));
@@ -152,12 +156,15 @@ async function runFixes(results, rules) {
152
156
  fixResults.files.manifest.value = `${JSON.stringify(getWritableManifest(manifest), null, 2)}\n`;
153
157
  fixResults.files.manifest.result = manifest;
154
158
  fixResults = await (0, validator_1.runValidators)(fixResults.files, rules);
155
- shouldRunFixes = (0, validator_1.hasFixes)(fixResults);
159
+ shouldRunFixes = (0, validator_1.hasFixes)(fixResults, errorsOnly);
160
+ fixResults.reports
161
+ .filter((report) => !mergedReports.some((mergedReport) => mergedReport.id === report.id))
162
+ .forEach((report) => mergedReports.push(report));
156
163
  }
157
- const initialReports = (0, deep_clone_1.deepClone)(results.reports);
164
+ const allReports = (0, deep_clone_1.deepClone)(mergedReports);
158
165
  // Was fixed
159
166
  if (!shouldRunFixes) {
160
- for (const report of initialReports) {
167
+ for (const report of allReports) {
161
168
  if (report.fix) {
162
169
  report.wasFixed = true;
163
170
  delete report.fix;
@@ -166,16 +173,16 @@ async function runFixes(results, rules) {
166
173
  return {
167
174
  files: fixResults.files,
168
175
  updated: true,
169
- reports: initialReports,
176
+ reports: allReports,
170
177
  };
171
178
  }
172
- for (const report of initialReports) {
179
+ for (const report of allReports) {
173
180
  delete report.fix;
174
181
  }
175
182
  return {
176
183
  files: results.files,
177
184
  updated: false,
178
- reports: initialReports,
185
+ reports: allReports,
179
186
  };
180
187
  }
181
188
  exports.runFixes = runFixes;
@@ -1 +1 @@
1
- {"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAsD;AAEtD,2CAAwD;AACxD,2BAAoC;AACpC,gDAA6B;AAI7B,+CAAsD;AAEtD,0EAAkD;AAClD,kDAA0C;AAC1C,kCAAqC;AACrC,sCAAoC;AAEpC,wCAA4C;AAC5C,mDAAoE;AAEpE,MAAM,mBAAmB,GAAuC;IAC9D,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,CAAC;IACb,MAAM,EAAE,CAAC;IACT,kBAAkB,EAAE,CAAC;IACrB,kBAAkB,EAAE,CAAC;IACrB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,EAAE;CACpB,CAAC;AAsDF;;;;;;;;;;;;;;;;;;;;GAoBG;AACI,KAAK,UAAU,aAAa,CACjC,QAAgB,EAChB,EACE,sBAAsB,GAAG,IAAI,EAC7B,UAAU,EACV,WAAW,GAAG,aAAE,CAAC,SAAS,EAC1B,OAAO,EACP,iBAAiB,MACO,EAAE;IAE5B,MAAM,YAAY,GAAG,cAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,wBAAgB,CAAC,QAAQ,CAAC,CAAC;IACzE,MAAM,YAAY,GAAG,MAAM,IAAA,iBAAY,EAAC,YAAY,CAAC,CAAC;IACtD,MAAM,mBAAmB,GAAG,YAAY,CAAC,M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ask/utils';\nimport { assert, isPlainObject } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport type { SnapManifest } from './validation';\nimport type { ValidatorResults } from './validator';\nimport { hasFixes, runValidators } from './validator';\nimport type { ValidatorMeta, ValidatorReport } from './validator-types';\nimport * as defaultValidators from './validators';\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { parseJson } from '../json';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\nimport { NpmSnapFileNames } from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file/node';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n version: 2,\n description: 3,\n proposedName: 4,\n repository: 5,\n source: 6,\n initialConnections: 7,\n initialPermissions: 8,\n platformVersion: 9,\n manifestVersion: 10,\n};\n\nexport type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {\n wasFixed?: boolean;\n};\n\n/**\n * The options for the `checkManifest` function.\n */\nexport type CheckManifestOptions = {\n /**\n * Whether to auto-magically try to fix errors and then write the manifest to\n * disk.\n */\n updateAndWriteManifest?: boolean;\n\n /**\n * The source code of the Snap.\n */\n sourceCode?: string;\n\n /**\n * The function to use to write the manifest to disk.\n */\n writeFileFn?: WriteFileFunction;\n\n /**\n * The exports detected by evaluating the bundle. This may be used by one or\n * more validators to determine whether the Snap is valid.\n */\n exports?: string[];\n\n /**\n * An object containing the names of the handlers and their respective\n * permission name. This must be provided to avoid circular dependencies\n * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.\n */\n handlerEndowments?: Record<string, string | null>;\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was written and updated.\n */\nexport type CheckManifestResult = {\n files?: SnapFiles;\n updated: boolean;\n reports: CheckManifestReport[];\n};\n\nexport type WriteFileFunction = (path: string, data: string) => Promise<void>;\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param options - Additional options for the function.\n * @param options.sourceCode - The source code of the Snap.\n * @param options.writeFileFn - The function to use to write the manifest to\n * disk.\n * @param options.updateAndWriteManifest - Whether to auto-magically try to fix\n * errors and then write the manifest to disk.\n * @param options.exports - The exports detected by evaluating the bundle. This\n * may be used by one or more validators to determine whether the Snap is valid.\n * @param options.handlerEndowments - An object containing the names of the\n * handlers and their respective permission name. This must be provided to avoid\n * circular dependencies between `@metamask/snaps-utils` and\n * `@metamask/snaps-rpc-methods`.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n {\n updateAndWriteManifest = true,\n sourceCode,\n writeFileFn = fs.writeFile,\n exports,\n handlerEndowments,\n }: CheckManifestOptions = {},\n): Promise<CheckManifestResult> {\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const auxiliaryFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.files,\n );\n\n const localizationFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.locales,\n );\n const localizationFiles =\n (await getSnapFiles(basePath, localizationFilePaths)) ?? [];\n for (const localization of localizationFiles) {\n try {\n localization.result = parseJson(localization.toString());\n } catch (error) {\n assert(error instanceof SyntaxError, error);\n throw new Error(\n `Failed to parse localization file \"${localization.path}\" as JSON.`,\n );\n }\n }\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n // Intentionally pass null as the encoding here since the files may be binary\n auxiliaryFiles:\n (await getSnapFiles(basePath, auxiliaryFilePaths, null)) ?? [],\n localizationFiles,\n };\n\n const validatorResults = await runValidators(\n snapFiles,\n Object.values(defaultValidators),\n { exports, handlerEndowments },\n );\n\n let manifestResults: CheckManifestResult = {\n updated: false,\n files: validatorResults.files,\n reports: validatorResults.reports,\n };\n\n if (updateAndWriteManifest && hasFixes(manifestResults)) {\n const fixedResults = await runFixes(validatorResults);\n\n if (fixedResults.updated) {\n manifestResults = fixedResults;\n\n assert(manifestResults.files);\n\n try {\n await writeFileFn(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n manifestResults.files.manifest.toString(),\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(\n `Failed to update \"snap.manifest.json\": ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n return manifestResults;\n}\n\n/**\n * Run the algorithm for automatically fixing errors in manifest.\n *\n * The algorithm updates the manifest by fixing all fixable problems,\n * and then run validation again to check if the new manifest is now correct.\n * If not correct, the algorithm will use the manifest from previous iteration\n * and try again `MAX_ATTEMPTS` times to update it before bailing and\n * resulting in failure.\n *\n * @param results - Results of the initial run of validation.\n * @param rules - Optional list of rules to run the fixes with.\n * @returns The updated manifest and whether it was updated.\n */\nexport async function runFixes(\n results: ValidatorResults,\n rules?: ValidatorMeta[],\n): Promise<CheckManifestResult> {\n let shouldRunFixes = true;\n const MAX_ATTEMPTS = 10;\n\n assert(results.files);\n\n let fixResults: ValidatorResults = results;\n assert(fixResults.files);\n fixResults.files.manifest = fixResults.files.manifest.clone();\n\n for (\n let attempts = 1;\n shouldRunFixes && attempts <= MAX_ATTEMPTS;\n attempts++\n ) {\n assert(fixResults.files);\n\n let manifest = fixResults.files.manifest.result;\n\n const fixable = fixResults.reports.filter((report) => report.fix);\n for (const report of fixable) {\n assert(report.fix);\n ({ manifest } = await report.fix({ manifest }));\n }\n\n fixResults.files.manifest.value = `${JSON.stringify(\n getWritableManifest(manifest),\n null,\n 2,\n )}\\n`;\n fixResults.files.manifest.result = manifest;\n\n fixResults = await runValidators(fixResults.files, rules);\n shouldRunFixes = hasFixes(fixResults);\n }\n\n const initialReports: (CheckManifestReport & ValidatorReport)[] = deepClone(\n results.reports,\n );\n\n // Was fixed\n if (!shouldRunFixes) {\n for (const report of initialReports) {\n if (report.fix) {\n report.wasFixed = true;\n delete report.fix;\n }\n }\n\n return {\n files: fixResults.files,\n updated: true,\n reports: initialReports,\n };\n }\n\n for (const report of initialReports) {\n delete report.fix;\n }\n\n return {\n files: results.files,\n updated: false,\n reports: initialReports,\n };\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(\n `Failed to read snap bundle file: ${getErrorMessage(error)}`,\n );\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read snap icon file: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Get an array of paths from an unvalidated Snap manifest.\n *\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param selector - A function that returns the paths to the files.\n * @returns The paths to the files, if any.\n */\nexport function getSnapFilePaths(\n manifest: Json,\n selector: (manifest: Partial<SnapManifest>) => string[] | undefined,\n) {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const snapManifest = manifest as Partial<SnapManifest>;\n const paths = selector(snapManifest);\n\n if (!Array.isArray(paths)) {\n return undefined;\n }\n\n return paths;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the files with the\n * given paths and read them.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param paths - The paths to the files.\n * @param encoding - An optional encoding to pass down to readVirtualFile.\n * @returns A list of auxiliary files and their contents, if any.\n */\nexport async function getSnapFiles(\n basePath: string,\n paths: string[] | undefined,\n encoding: BufferEncoding | null = 'utf8',\n): Promise<VirtualFile[] | undefined> {\n if (!paths) {\n return undefined;\n }\n\n try {\n return await Promise.all(\n paths.map(async (filePath) =>\n readVirtualFile(pathUtils.join(basePath, filePath), encoding),\n ),\n );\n } catch (error) {\n throw new Error(`Failed to read snap files: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n"]}
1
+ {"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAsD;AAEtD,2CAAwD;AACxD,2BAAoC;AACpC,gDAA6B;AAI7B,+CAAuE;AAEvE,0EAAkD;AAClD,kDAA0C;AAC1C,kCAAqC;AACrC,sCAAoC;AAEpC,wCAA4C;AAC5C,mDAAoE;AAEpE,MAAM,mBAAmB,GAAuC;IAC9D,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,CAAC;IACb,MAAM,EAAE,CAAC;IACT,kBAAkB,EAAE,CAAC;IACrB,kBAAkB,EAAE,CAAC;IACrB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,EAAE;CACpB,CAAC;AA4DF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACI,KAAK,UAAU,aAAa,CACjC,QAAgB,EAChB,EACE,sBAAsB,GAAG,IAAI,EAC7B,UAAU,EACV,WAAW,GAAG,aAAE,CAAC,SAAS,EAC1B,OAAO,EACP,iBAAiB,EACjB,SAAS,GAAG,KAAK,MACO,EAAE;IAE5B,MAAM,YAAY,GAAG,cAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,wBAAgB,CAAC,QAAQ,CAAC,CAAC;IACzE,MAAM,YAAY,GAAG,MAAM,IAAA,iBAAY,EAAC,YAAY,CAAC,CAAC;IACtD,MAAM,mBAAmB,GAAG,YAAY,CAAC,MAAM,CAAC;IAEhD,MAAM,WAAW,GAAG,MAAM,IAAA,iBAAY,EACpC,cAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,wBAAgB,CAAC,WAAW,CAAC,CACvD,CAAC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,CAAC,CAAC,CAAC;SAC/D,MAAM,CACL,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QAChB,GAAG,MAAM;QACT,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;KACrB,CAAC,EACF,EAAE,CACH,CAAC;IAEJ,OAAO,gBAAgC,CAAC;AAC1C,CAAC;AAlBD,kDAkBC","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\nimport { assert, isPlainObject } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport type { SnapManifest } from './validation';\nimport type { ValidatorResults } from './validator';\nimport { isReportFixable, hasFixes, runValidators } from './validator';\nimport type { ValidatorMeta, ValidatorReport } from './validator-types';\nimport * as defaultValidators from './validators';\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { parseJson } from '../json';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\nimport { NpmSnapFileNames } from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file/node';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n version: 2,\n description: 3,\n proposedName: 4,\n repository: 5,\n source: 6,\n initialConnections: 7,\n initialPermissions: 8,\n platformVersion: 9,\n manifestVersion: 10,\n};\n\nexport type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {\n wasFixed?: boolean;\n};\n\n/**\n * The options for the `checkManifest` function.\n */\nexport type CheckManifestOptions = {\n /**\n * Whether to auto-magically try to fix errors and then write the manifest to\n * disk.\n */\n updateAndWriteManifest?: boolean;\n\n /**\n * The source code of the Snap.\n */\n sourceCode?: string;\n\n /**\n * The function to use to write the manifest to disk.\n */\n writeFileFn?: WriteFileFunction;\n\n /**\n * The exports detected by evaluating the bundle. This may be used by one or\n * more validators to determine whether the Snap is valid.\n */\n exports?: string[];\n\n /**\n * An object containing the names of the handlers and their respective\n * permission name. This must be provided to avoid circular dependencies\n * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.\n */\n handlerEndowments?: Record<string, string | null>;\n\n /**\n * Whether the compiler is running in watch mode. This is used to determine\n * whether to fix warnings or errors only.\n */\n watchMode?: boolean;\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was written and updated.\n */\nexport type CheckManifestResult = {\n files?: SnapFiles;\n updated: boolean;\n reports: CheckManifestReport[];\n};\n\nexport type WriteFileFunction = (path: string, data: string) => Promise<void>;\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param options - Additional options for the function.\n * @param options.sourceCode - The source code of the Snap.\n * @param options.writeFileFn - The function to use to write the manifest to\n * disk.\n * @param options.updateAndWriteManifest - Whether to auto-magically try to fix\n * errors and then write the manifest to disk.\n * @param options.exports - The exports detected by evaluating the bundle. This\n * may be used by one or more validators to determine whether the Snap is valid.\n * @param options.handlerEndowments - An object containing the names of the\n * handlers and their respective permission name. This must be provided to avoid\n * circular dependencies between `@metamask/snaps-utils` and\n * `@metamask/snaps-rpc-methods`.\n * @param options.watchMode - Whether the compiler is running in watch mode.\n * This is used to determine whether to fix warnings or errors only.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n {\n updateAndWriteManifest = true,\n sourceCode,\n writeFileFn = fs.writeFile,\n exports,\n handlerEndowments,\n watchMode = false,\n }: CheckManifestOptions = {},\n): Promise<CheckManifestResult> {\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const auxiliaryFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.files,\n );\n\n const localizationFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.locales,\n );\n const localizationFiles =\n (await getSnapFiles(basePath, localizationFilePaths)) ?? [];\n for (const localization of localizationFiles) {\n try {\n localization.result = parseJson(localization.toString());\n } catch (error) {\n assert(error instanceof SyntaxError, error);\n throw new Error(\n `Failed to parse localization file \"${localization.path}\" as JSON.`,\n );\n }\n }\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n // Intentionally pass null as the encoding here since the files may be binary\n auxiliaryFiles:\n (await getSnapFiles(basePath, auxiliaryFilePaths, null)) ?? [],\n localizationFiles,\n };\n\n const validatorResults = await runValidators(\n snapFiles,\n Object.values(defaultValidators),\n { exports, handlerEndowments },\n );\n\n let manifestResults: CheckManifestResult = {\n updated: false,\n files: validatorResults.files,\n reports: validatorResults.reports,\n };\n\n if (updateAndWriteManifest && hasFixes(manifestResults, watchMode)) {\n const fixedResults = await runFixes(validatorResults, undefined, watchMode);\n\n if (fixedResults.updated) {\n manifestResults = fixedResults;\n\n assert(manifestResults.files);\n\n try {\n await writeFileFn(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n manifestResults.files.manifest.toString(),\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(\n `Failed to update \"snap.manifest.json\": ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n return manifestResults;\n}\n\n/**\n * Run the algorithm for automatically fixing errors in manifest.\n *\n * The algorithm updates the manifest by fixing all fixable problems,\n * and then run validation again to check if the new manifest is now correct.\n * If not correct, the algorithm will use the manifest from previous iteration\n * and try again `MAX_ATTEMPTS` times to update it before bailing and\n * resulting in failure.\n *\n * @param results - Results of the initial run of validation.\n * @param rules - Optional list of rules to run the fixes with.\n * @param errorsOnly - Whether to only run fixes for errors, not warnings.\n * @returns The updated manifest and whether it was updated.\n */\nexport async function runFixes(\n results: ValidatorResults,\n rules?: ValidatorMeta[],\n errorsOnly = false,\n): Promise<CheckManifestResult> {\n let shouldRunFixes = true;\n const MAX_ATTEMPTS = 10;\n\n assert(results.files);\n\n let fixResults: ValidatorResults = results;\n assert(fixResults.files);\n fixResults.files.manifest = fixResults.files.manifest.clone();\n\n const mergedReports: ValidatorReport[] = deepClone(fixResults.reports);\n\n for (\n let attempts = 1;\n shouldRunFixes && attempts <= MAX_ATTEMPTS;\n attempts++\n ) {\n assert(fixResults.files);\n\n let manifest = fixResults.files.manifest.result;\n\n const fixable = fixResults.reports.filter((report) =>\n isReportFixable(report, errorsOnly),\n );\n\n for (const report of fixable) {\n assert(report.fix);\n ({ manifest } = await report.fix({ manifest }));\n }\n\n fixResults.files.manifest.value = `${JSON.stringify(\n getWritableManifest(manifest),\n null,\n 2,\n )}\\n`;\n fixResults.files.manifest.result = manifest;\n\n fixResults = await runValidators(fixResults.files, rules);\n shouldRunFixes = hasFixes(fixResults, errorsOnly);\n\n fixResults.reports\n .filter(\n (report) =>\n !mergedReports.some((mergedReport) => mergedReport.id === report.id),\n )\n .forEach((report) => mergedReports.push(report));\n }\n\n const allReports: (CheckManifestReport & ValidatorReport)[] =\n deepClone(mergedReports);\n\n // Was fixed\n if (!shouldRunFixes) {\n for (const report of allReports) {\n if (report.fix) {\n report.wasFixed = true;\n delete report.fix;\n }\n }\n\n return {\n files: fixResults.files,\n updated: true,\n reports: allReports,\n };\n }\n\n for (const report of allReports) {\n delete report.fix;\n }\n\n return {\n files: results.files,\n updated: false,\n reports: allReports,\n };\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(\n `Failed to read snap bundle file: ${getErrorMessage(error)}`,\n );\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read snap icon file: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Get an array of paths from an unvalidated Snap manifest.\n *\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param selector - A function that returns the paths to the files.\n * @returns The paths to the files, if any.\n */\nexport function getSnapFilePaths(\n manifest: Json,\n selector: (manifest: Partial<SnapManifest>) => string[] | undefined,\n) {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const snapManifest = manifest as Partial<SnapManifest>;\n const paths = selector(snapManifest);\n\n if (!Array.isArray(paths)) {\n return undefined;\n }\n\n return paths;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the files with the\n * given paths and read them.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param paths - The paths to the files.\n * @param encoding - An optional encoding to pass down to readVirtualFile.\n * @returns A list of auxiliary files and their contents, if any.\n */\nexport async function getSnapFiles(\n basePath: string,\n paths: string[] | undefined,\n encoding: BufferEncoding | null = 'utf8',\n): Promise<VirtualFile[] | undefined> {\n if (!paths) {\n return undefined;\n }\n\n try {\n return await Promise.all(\n paths.map(async (filePath) =>\n readVirtualFile(pathUtils.join(basePath, filePath), encoding),\n ),\n );\n } catch (error) {\n throw new Error(`Failed to read snap files: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n"]}
@@ -36,6 +36,11 @@ export type CheckManifestOptions = {
36
36
  * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.
37
37
  */
38
38
  handlerEndowments?: Record<string, string | null>;
39
+ /**
40
+ * Whether the compiler is running in watch mode. This is used to determine
41
+ * whether to fix warnings or errors only.
42
+ */
43
+ watchMode?: boolean;
39
44
  };
40
45
  /**
41
46
  * The result from the `checkManifest` function.
@@ -67,10 +72,12 @@ export type WriteFileFunction = (path: string, data: string) => Promise<void>;
67
72
  * handlers and their respective permission name. This must be provided to avoid
68
73
  * circular dependencies between `@metamask/snaps-utils` and
69
74
  * `@metamask/snaps-rpc-methods`.
75
+ * @param options.watchMode - Whether the compiler is running in watch mode.
76
+ * This is used to determine whether to fix warnings or errors only.
70
77
  * @returns Whether the manifest was updated, and an array of warnings that
71
78
  * were encountered during processing of the manifest files.
72
79
  */
73
- export declare function checkManifest(basePath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, }?: CheckManifestOptions): Promise<CheckManifestResult>;
80
+ export declare function checkManifest(basePath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, watchMode, }?: CheckManifestOptions): Promise<CheckManifestResult>;
74
81
  /**
75
82
  * Run the algorithm for automatically fixing errors in manifest.
76
83
  *
@@ -82,9 +89,10 @@ export declare function checkManifest(basePath: string, { updateAndWriteManifest
82
89
  *
83
90
  * @param results - Results of the initial run of validation.
84
91
  * @param rules - Optional list of rules to run the fixes with.
92
+ * @param errorsOnly - Whether to only run fixes for errors, not warnings.
85
93
  * @returns The updated manifest and whether it was updated.
86
94
  */
87
- export declare function runFixes(results: ValidatorResults, rules?: ValidatorMeta[]): Promise<CheckManifestResult>;
95
+ export declare function runFixes(results: ValidatorResults, rules?: ValidatorMeta[], errorsOnly?: boolean): Promise<CheckManifestResult>;
88
96
  /**
89
97
  * Given an unvalidated Snap manifest, attempts to extract the location of the
90
98
  * bundle source file location and read the file.
@@ -1 +1 @@
1
- {"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAK5C,OAAO,KAAK,EAAE,YAAY,EAAE,yBAAqB;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,wBAAoB;AAEpD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,8BAA0B;AAKxE,OAAO,KAAK,EAAE,SAAS,EAAwB,qBAAiB;AAEhE,OAAO,EAAmB,WAAW,EAAE,iCAA6B;AAepE,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,eAAe,EAAE,KAAK,CAAC,GAAG;IAC/D,QAAQ,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;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,IAAI,EACd,QAAQ,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,YAAY,CAAC,KAAK,MAAM,EAAE,GAAG,SAAS,wBAcpE;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EAAE,GAAG,SAAS,EAC3B,QAAQ,GAAE,cAAc,GAAG,IAAa,GACvC,OAAO,CAAC,WAAW,EAAE,GAAG,SAAS,CAAC,CAcpC;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,YAAY,GAAG,YAAY,CAkBxE"}
@@ -36,6 +36,11 @@ export type CheckManifestOptions = {
36
36
  * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.
37
37
  */
38
38
  handlerEndowments?: Record<string, string | null>;
39
+ /**
40
+ * Whether the compiler is running in watch mode. This is used to determine
41
+ * whether to fix warnings or errors only.
42
+ */
43
+ watchMode?: boolean;
39
44
  };
40
45
  /**
41
46
  * The result from the `checkManifest` function.
@@ -67,10 +72,12 @@ export type WriteFileFunction = (path: string, data: string) => Promise<void>;
67
72
  * handlers and their respective permission name. This must be provided to avoid
68
73
  * circular dependencies between `@metamask/snaps-utils` and
69
74
  * `@metamask/snaps-rpc-methods`.
75
+ * @param options.watchMode - Whether the compiler is running in watch mode.
76
+ * This is used to determine whether to fix warnings or errors only.
70
77
  * @returns Whether the manifest was updated, and an array of warnings that
71
78
  * were encountered during processing of the manifest files.
72
79
  */
73
- export declare function checkManifest(basePath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, }?: CheckManifestOptions): Promise<CheckManifestResult>;
80
+ export declare function checkManifest(basePath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, watchMode, }?: CheckManifestOptions): Promise<CheckManifestResult>;
74
81
  /**
75
82
  * Run the algorithm for automatically fixing errors in manifest.
76
83
  *
@@ -82,9 +89,10 @@ export declare function checkManifest(basePath: string, { updateAndWriteManifest
82
89
  *
83
90
  * @param results - Results of the initial run of validation.
84
91
  * @param rules - Optional list of rules to run the fixes with.
92
+ * @param errorsOnly - Whether to only run fixes for errors, not warnings.
85
93
  * @returns The updated manifest and whether it was updated.
86
94
  */
87
- export declare function runFixes(results: ValidatorResults, rules?: ValidatorMeta[]): Promise<CheckManifestResult>;
95
+ export declare function runFixes(results: ValidatorResults, rules?: ValidatorMeta[], errorsOnly?: boolean): Promise<CheckManifestResult>;
88
96
  /**
89
97
  * Given an unvalidated Snap manifest, attempts to extract the location of the
90
98
  * bundle source file location and read the file.
@@ -1 +1 @@
1
- {"version":3,"file":"manifest.d.mts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAK5C,OAAO,KAAK,EAAE,YAAY,EAAE,yBAAqB;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,wBAAoB;AAEpD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,8BAA0B;AAKxE,OAAO,KAAK,EAAE,SAAS,EAAwB,qBAAiB;AAEhE,OAAO,EAAmB,WAAW,EAAE,iCAA6B;AAepE,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,eAAe,EAAE,KAAK,CAAC,GAAG;IAC/D,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;OAGG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;IAEjC;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAEhC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE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pC;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,YAAY,GAAG,YAAY,CAkBxE"}
@@ -2,7 +2,7 @@ import { getErrorMessage } from "@metamask/snaps-sdk";
2
2
  import { assert, isPlainObject } from "@metamask/utils";
3
3
  import { promises as fs } from "fs";
4
4
  import pathUtils from "path";
5
- import { hasFixes, runValidators } from "./validator.mjs";
5
+ import { isReportFixable, hasFixes, runValidators } from "./validator.mjs";
6
6
  import * as defaultValidators from "./validators/index.mjs";
7
7
  import { deepClone } from "../deep-clone.mjs";
8
8
  import { readJsonFile } from "../fs.mjs";
@@ -39,10 +39,12 @@ const MANIFEST_SORT_ORDER = {
39
39
  * handlers and their respective permission name. This must be provided to avoid
40
40
  * circular dependencies between `@metamask/snaps-utils` and
41
41
  * `@metamask/snaps-rpc-methods`.
42
+ * @param options.watchMode - Whether the compiler is running in watch mode.
43
+ * This is used to determine whether to fix warnings or errors only.
42
44
  * @returns Whether the manifest was updated, and an array of warnings that
43
45
  * were encountered during processing of the manifest files.
44
46
  */
45
- export async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs.writeFile, exports, handlerEndowments, } = {}) {
47
+ export async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs.writeFile, exports, handlerEndowments, watchMode = false, } = {}) {
46
48
  const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);
47
49
  const manifestFile = await readJsonFile(manifestPath);
48
50
  const unvalidatedManifest = manifestFile.result;
@@ -74,8 +76,8 @@ export async function checkManifest(basePath, { updateAndWriteManifest = true, s
74
76
  files: validatorResults.files,
75
77
  reports: validatorResults.reports,
76
78
  };
77
- if (updateAndWriteManifest && hasFixes(manifestResults)) {
78
- const fixedResults = await runFixes(validatorResults);
79
+ if (updateAndWriteManifest && hasFixes(manifestResults, watchMode)) {
80
+ const fixedResults = await runFixes(validatorResults, undefined, watchMode);
79
81
  if (fixedResults.updated) {
80
82
  manifestResults = fixedResults;
81
83
  assert(manifestResults.files);
@@ -102,19 +104,21 @@ export async function checkManifest(basePath, { updateAndWriteManifest = true, s
102
104
  *
103
105
  * @param results - Results of the initial run of validation.
104
106
  * @param rules - Optional list of rules to run the fixes with.
107
+ * @param errorsOnly - Whether to only run fixes for errors, not warnings.
105
108
  * @returns The updated manifest and whether it was updated.
106
109
  */
107
- export async function runFixes(results, rules) {
110
+ export async function runFixes(results, rules, errorsOnly = false) {
108
111
  let shouldRunFixes = true;
109
112
  const MAX_ATTEMPTS = 10;
110
113
  assert(results.files);
111
114
  let fixResults = results;
112
115
  assert(fixResults.files);
113
116
  fixResults.files.manifest = fixResults.files.manifest.clone();
117
+ const mergedReports = deepClone(fixResults.reports);
114
118
  for (let attempts = 1; shouldRunFixes && attempts <= MAX_ATTEMPTS; attempts++) {
115
119
  assert(fixResults.files);
116
120
  let manifest = fixResults.files.manifest.result;
117
- const fixable = fixResults.reports.filter((report) => report.fix);
121
+ const fixable = fixResults.reports.filter((report) => isReportFixable(report, errorsOnly));
118
122
  for (const report of fixable) {
119
123
  assert(report.fix);
120
124
  ({ manifest } = await report.fix({ manifest }));
@@ -122,12 +126,15 @@ export async function runFixes(results, rules) {
122
126
  fixResults.files.manifest.value = `${JSON.stringify(getWritableManifest(manifest), null, 2)}\n`;
123
127
  fixResults.files.manifest.result = manifest;
124
128
  fixResults = await runValidators(fixResults.files, rules);
125
- shouldRunFixes = hasFixes(fixResults);
129
+ shouldRunFixes = hasFixes(fixResults, errorsOnly);
130
+ fixResults.reports
131
+ .filter((report) => !mergedReports.some((mergedReport) => mergedReport.id === report.id))
132
+ .forEach((report) => mergedReports.push(report));
126
133
  }
127
- const initialReports = deepClone(results.reports);
134
+ const allReports = deepClone(mergedReports);
128
135
  // Was fixed
129
136
  if (!shouldRunFixes) {
130
- for (const report of initialReports) {
137
+ for (const report of allReports) {
131
138
  if (report.fix) {
132
139
  report.wasFixed = true;
133
140
  delete report.fix;
@@ -136,16 +143,16 @@ export async function runFixes(results, rules) {
136
143
  return {
137
144
  files: fixResults.files,
138
145
  updated: true,
139
- reports: initialReports,
146
+ reports: allReports,
140
147
  };
141
148
  }
142
- for (const report of initialReports) {
149
+ for (const report of allReports) {
143
150
  delete report.fix;
144
151
  }
145
152
  return {
146
153
  files: results.files,
147
154
  updated: false,
148
- reports: initialReports,
155
+ reports: allReports,
149
156
  };
150
157
  }
151
158
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"manifest.mjs","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,4BAA4B;AAEtD,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,wBAAwB;AACxD,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,WAAW;AACpC,OAAO,SAAS,aAAa;AAI7B,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,wBAAoB;AAEtD,OAAO,KAAK,iBAAiB,+BAAqB;AAClD,OAAO,EAAE,SAAS,EAAE,0BAAsB;AAC1C,OAAO,EAAE,YAAY,EAAE,kBAAc;AACrC,OAAO,EAAE,SAAS,EAAE,oBAAgB;AAEpC,OAAO,EAAE,gBAAgB,EAAE,qBAAiB;AAC5C,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,iCAA6B;AAEpE,MAAM,mBAAmB,GAAuC;IAC9D,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,CAAC;IACb,MAAM,EAAE,CAAC;IACT,kBAAkB,EAAE,CAAC;IACrB,kBAAkB,EAAE,CAAC;IACrB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,EAAE;CACpB,CAAC;AAsDF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAgB,EAChB,EACE,sBAAsB,GAAG,IAAI,EAC7B,UAAU,EACV,WAAW,GAAG,EA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Message } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\nimport { assert, isPlainObject } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport type { SnapManifest } from './validation';\nimport type { ValidatorResults } from './validator';\nimport { hasFixes, runValidators } from './validator';\nimport type { ValidatorMeta, ValidatorReport } from './validator-types';\nimport * as defaultValidators from './validators';\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { parseJson } from '../json';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\nimport { NpmSnapFileNames } from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file/node';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n version: 2,\n description: 3,\n proposedName: 4,\n repository: 5,\n source: 6,\n initialConnections: 7,\n initialPermissions: 8,\n platformVersion: 9,\n manifestVersion: 10,\n};\n\nexport type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {\n wasFixed?: boolean;\n};\n\n/**\n * The options for the `checkManifest` function.\n */\nexport type CheckManifestOptions = {\n /**\n * Whether to auto-magically try to fix errors and then write the manifest to\n * disk.\n */\n updateAndWriteManifest?: boolean;\n\n /**\n * The source code of the Snap.\n */\n sourceCode?: string;\n\n /**\n * The function to use to write the manifest to disk.\n */\n writeFileFn?: WriteFileFunction;\n\n /**\n * The exports detected by evaluating the bundle. This may be used by one or\n * more validators to determine whether the Snap is valid.\n */\n exports?: string[];\n\n /**\n * An object containing the names of the handlers and their respective\n * permission name. This must be provided to avoid circular dependencies\n * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.\n */\n handlerEndowments?: Record<string, string | null>;\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was written and updated.\n */\nexport type CheckManifestResult = {\n files?: SnapFiles;\n updated: boolean;\n reports: CheckManifestReport[];\n};\n\nexport type WriteFileFunction = (path: string, data: string) => Promise<void>;\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param options - Additional options for the function.\n * @param options.sourceCode - The source code of the Snap.\n * @param options.writeFileFn - The function to use to write the manifest to\n * disk.\n * @param options.updateAndWriteManifest - Whether to auto-magically try to fix\n * errors and then write the manifest to disk.\n * @param options.exports - The exports detected by evaluating the bundle. This\n * may be used by one or more validators to determine whether the Snap is valid.\n * @param options.handlerEndowments - An object containing the names of the\n * handlers and their respective permission name. This must be provided to avoid\n * circular dependencies between `@metamask/snaps-utils` and\n * `@metamask/snaps-rpc-methods`.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n {\n updateAndWriteManifest = true,\n sourceCode,\n writeFileFn = fs.writeFile,\n exports,\n handlerEndowments,\n }: CheckManifestOptions = {},\n): Promise<CheckManifestResult> {\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const auxiliaryFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.files,\n );\n\n const localizationFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.locales,\n );\n const localizationFiles =\n (await getSnapFiles(basePath, localizationFilePaths)) ?? [];\n for (const localization of localizationFiles) {\n try {\n localization.result = parseJson(localization.toString());\n } catch (error) {\n assert(error instanceof SyntaxError, error);\n throw new Error(\n `Failed to parse localization file \"${localization.path}\" as JSON.`,\n );\n }\n }\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n // Intentionally pass null as the encoding here since the files may be binary\n auxiliaryFiles:\n (await getSnapFiles(basePath, auxiliaryFilePaths, null)) ?? [],\n localizationFiles,\n };\n\n const validatorResults = await runValidators(\n snapFiles,\n Object.values(defaultValidators),\n { exports, handlerEndowments },\n );\n\n let manifestResults: CheckManifestResult = {\n updated: false,\n files: validatorResults.files,\n reports: validatorResults.reports,\n };\n\n if (updateAndWriteManifest && hasFixes(manifestResults)) {\n const fixedResults = await runFixes(validatorResults);\n\n if (fixedResults.updated) {\n manifestResults = fixedResults;\n\n assert(manifestResults.files);\n\n try {\n await writeFileFn(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n manifestResults.files.manifest.toString(),\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(\n `Failed to update \"snap.manifest.json\": ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n return manifestResults;\n}\n\n/**\n * Run the algorithm for automatically fixing errors in manifest.\n *\n * The algorithm updates the manifest by fixing all fixable problems,\n * and then run validation again to check if the new manifest is now correct.\n * If not correct, the algorithm will use the manifest from previous iteration\n * and try again `MAX_ATTEMPTS` times to update it before bailing and\n * resulting in failure.\n *\n * @param results - Results of the initial run of validation.\n * @param rules - Optional list of rules to run the fixes with.\n * @returns The updated manifest and whether it was updated.\n */\nexport async function runFixes(\n results: ValidatorResults,\n rules?: ValidatorMeta[],\n): Promise<CheckManifestResult> {\n let shouldRunFixes = true;\n const MAX_ATTEMPTS = 10;\n\n assert(results.files);\n\n let fixResults: ValidatorResults = results;\n assert(fixResults.files);\n fixResults.files.manifest = fixResults.files.manifest.clone();\n\n for (\n let attempts = 1;\n shouldRunFixes && attempts <= MAX_ATTEMPTS;\n attempts++\n ) {\n assert(fixResults.files);\n\n let manifest = fixResults.files.manifest.result;\n\n const fixable = fixResults.reports.filter((report) => report.fix);\n for (const report of fixable) {\n assert(report.fix);\n ({ manifest } = await report.fix({ manifest }));\n }\n\n fixResults.files.manifest.value = `${JSON.stringify(\n getWritableManifest(manifest),\n null,\n 2,\n )}\\n`;\n fixResults.files.manifest.result = manifest;\n\n fixResults = await runValidators(fixResults.files, rules);\n shouldRunFixes = hasFixes(fixResults);\n }\n\n const initialReports: (CheckManifestReport & ValidatorReport)[] = deepClone(\n results.reports,\n );\n\n // Was fixed\n if (!shouldRunFixes) {\n for (const report of initialReports) {\n if (report.fix) {\n report.wasFixed = true;\n delete report.fix;\n }\n }\n\n return {\n files: fixResults.files,\n updated: true,\n reports: initialReports,\n };\n }\n\n for (const report of initialReports) {\n delete report.fix;\n }\n\n return {\n files: results.files,\n updated: false,\n reports: initialReports,\n };\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(\n `Failed to read snap bundle file: ${getErrorMessage(error)}`,\n );\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read snap icon file: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Get an array of paths from an unvalidated Snap manifest.\n *\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param selector - A function that returns the paths to the files.\n * @returns The paths to the files, if any.\n */\nexport function getSnapFilePaths(\n manifest: Json,\n selector: (manifest: Partial<SnapManifest>) => string[] | undefined,\n) {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const snapManifest = manifest as Partial<SnapManifest>;\n const paths = selector(snapManifest);\n\n if (!Array.isArray(paths)) {\n return undefined;\n }\n\n return paths;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the files with the\n * given paths and read them.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param paths - The paths to the files.\n * @param encoding - An optional encoding to pass down to readVirtualFile.\n * @returns A list of auxiliary files and their contents, if any.\n */\nexport async function getSnapFiles(\n basePath: string,\n paths: string[] | undefined,\n encoding: BufferEncoding | null = 'utf8',\n): Promise<VirtualFile[] | undefined> {\n if (!paths) {\n return undefined;\n }\n\n try {\n return await Promise.all(\n paths.map(async (filePath) =>\n readVirtualFile(pathUtils.join(basePath, filePath), encoding),\n ),\n );\n } catch (error) {\n throw new Error(`Failed to read snap files: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n"]}
1
+ {"version":3,"file":"manifest.mjs","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,4BAA4B;AAEtD,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,wBAAwB;AACxD,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,WAAW;AACpC,OAAO,SAAS,aAAa;AAI7B,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,aAAa,EAAE,wBAAoB;AAEvE,OAAO,KAAK,iBAAiB,+BAAqB;AAClD,OAAO,EAAE,SAAS,EAAE,0BAAsB;AAC1C,OAAO,EAAE,YAAY,EAAE,kBAAc;AACrC,OAAO,EAAE,SAAS,EAAE,oBAAgB;AAEpC,OAAO,EAAE,gBAAgB,EAAE,qBAAiB;AAC5C,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,iCAA6B;AAEpE,MAAM,mBAAmB,GAAuC;IAC9D,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,CAAC;IACb,MAAM,EAAE,CAAC;IACT,kBAAkB,EAAE,CAAC;IACrB,kBAAkB,EAAE,CAAC;IACrB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,EAAE;CACpB,CAAC;AA4DF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAgB,EAChB,EACE,sBAAsB,GAAG,IAAI,EAC7B,UAAU,EACV,WAAW,GAAG,EAAE,CAAC,SAAS,EAC1B,OAAO,EACP,iBAAiB,EACjB,SAAS,GAAG,KAAK,MACO,EAAE;IAE5B,MAAM,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;SAC1B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAAC;SAC/D,MAAM,CACL,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QAChB,GAAG,MAAM;QACT,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;KACrB,CAAC,EACF,EAAE,CACH,CAAC;IAEJ,OAAO,gBAAgC,CAAC;AAC1C,CAAC","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\nimport { assert, isPlainObject } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport type { SnapManifest } from './validation';\nimport type { ValidatorResults } from './validator';\nimport { isReportFixable, hasFixes, runValidators } from './validator';\nimport type { ValidatorMeta, ValidatorReport } from './validator-types';\nimport * as defaultValidators from './validators';\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { parseJson } from '../json';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\nimport { NpmSnapFileNames } from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file/node';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n version: 2,\n description: 3,\n proposedName: 4,\n repository: 5,\n source: 6,\n initialConnections: 7,\n initialPermissions: 8,\n platformVersion: 9,\n manifestVersion: 10,\n};\n\nexport type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {\n wasFixed?: boolean;\n};\n\n/**\n * The options for the `checkManifest` function.\n */\nexport type CheckManifestOptions = {\n /**\n * Whether to auto-magically try to fix errors and then write the manifest to\n * disk.\n */\n updateAndWriteManifest?: boolean;\n\n /**\n * The source code of the Snap.\n */\n sourceCode?: string;\n\n /**\n * The function to use to write the manifest to disk.\n */\n writeFileFn?: WriteFileFunction;\n\n /**\n * The exports detected by evaluating the bundle. This may be used by one or\n * more validators to determine whether the Snap is valid.\n */\n exports?: string[];\n\n /**\n * An object containing the names of the handlers and their respective\n * permission name. This must be provided to avoid circular dependencies\n * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.\n */\n handlerEndowments?: Record<string, string | null>;\n\n /**\n * Whether the compiler is running in watch mode. This is used to determine\n * whether to fix warnings or errors only.\n */\n watchMode?: boolean;\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was written and updated.\n */\nexport type CheckManifestResult = {\n files?: SnapFiles;\n updated: boolean;\n reports: CheckManifestReport[];\n};\n\nexport type WriteFileFunction = (path: string, data: string) => Promise<void>;\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param options - Additional options for the function.\n * @param options.sourceCode - The source code of the Snap.\n * @param options.writeFileFn - The function to use to write the manifest to\n * disk.\n * @param options.updateAndWriteManifest - Whether to auto-magically try to fix\n * errors and then write the manifest to disk.\n * @param options.exports - The exports detected by evaluating the bundle. This\n * may be used by one or more validators to determine whether the Snap is valid.\n * @param options.handlerEndowments - An object containing the names of the\n * handlers and their respective permission name. This must be provided to avoid\n * circular dependencies between `@metamask/snaps-utils` and\n * `@metamask/snaps-rpc-methods`.\n * @param options.watchMode - Whether the compiler is running in watch mode.\n * This is used to determine whether to fix warnings or errors only.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n {\n updateAndWriteManifest = true,\n sourceCode,\n writeFileFn = fs.writeFile,\n exports,\n handlerEndowments,\n watchMode = false,\n }: CheckManifestOptions = {},\n): Promise<CheckManifestResult> {\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const auxiliaryFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.files,\n );\n\n const localizationFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.locales,\n );\n const localizationFiles =\n (await getSnapFiles(basePath, localizationFilePaths)) ?? [];\n for (const localization of localizationFiles) {\n try {\n localization.result = parseJson(localization.toString());\n } catch (error) {\n assert(error instanceof SyntaxError, error);\n throw new Error(\n `Failed to parse localization file \"${localization.path}\" as JSON.`,\n );\n }\n }\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n // Intentionally pass null as the encoding here since the files may be binary\n auxiliaryFiles:\n (await getSnapFiles(basePath, auxiliaryFilePaths, null)) ?? [],\n localizationFiles,\n };\n\n const validatorResults = await runValidators(\n snapFiles,\n Object.values(defaultValidators),\n { exports, handlerEndowments },\n );\n\n let manifestResults: CheckManifestResult = {\n updated: false,\n files: validatorResults.files,\n reports: validatorResults.reports,\n };\n\n if (updateAndWriteManifest && hasFixes(manifestResults, watchMode)) {\n const fixedResults = await runFixes(validatorResults, undefined, watchMode);\n\n if (fixedResults.updated) {\n manifestResults = fixedResults;\n\n assert(manifestResults.files);\n\n try {\n await writeFileFn(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n manifestResults.files.manifest.toString(),\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(\n `Failed to update \"snap.manifest.json\": ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n return manifestResults;\n}\n\n/**\n * Run the algorithm for automatically fixing errors in manifest.\n *\n * The algorithm updates the manifest by fixing all fixable problems,\n * and then run validation again to check if the new manifest is now correct.\n * If not correct, the algorithm will use the manifest from previous iteration\n * and try again `MAX_ATTEMPTS` times to update it before bailing and\n * resulting in failure.\n *\n * @param results - Results of the initial run of validation.\n * @param rules - Optional list of rules to run the fixes with.\n * @param errorsOnly - Whether to only run fixes for errors, not warnings.\n * @returns The updated manifest and whether it was updated.\n */\nexport async function runFixes(\n results: ValidatorResults,\n rules?: ValidatorMeta[],\n errorsOnly = false,\n): Promise<CheckManifestResult> {\n let shouldRunFixes = true;\n const MAX_ATTEMPTS = 10;\n\n assert(results.files);\n\n let fixResults: ValidatorResults = results;\n assert(fixResults.files);\n fixResults.files.manifest = fixResults.files.manifest.clone();\n\n const mergedReports: ValidatorReport[] = deepClone(fixResults.reports);\n\n for (\n let attempts = 1;\n shouldRunFixes && attempts <= MAX_ATTEMPTS;\n attempts++\n ) {\n assert(fixResults.files);\n\n let manifest = fixResults.files.manifest.result;\n\n const fixable = fixResults.reports.filter((report) =>\n isReportFixable(report, errorsOnly),\n );\n\n for (const report of fixable) {\n assert(report.fix);\n ({ manifest } = await report.fix({ manifest }));\n }\n\n fixResults.files.manifest.value = `${JSON.stringify(\n getWritableManifest(manifest),\n null,\n 2,\n )}\\n`;\n fixResults.files.manifest.result = manifest;\n\n fixResults = await runValidators(fixResults.files, rules);\n shouldRunFixes = hasFixes(fixResults, errorsOnly);\n\n fixResults.reports\n .filter(\n (report) =>\n !mergedReports.some((mergedReport) => mergedReport.id === report.id),\n )\n .forEach((report) => mergedReports.push(report));\n }\n\n const allReports: (CheckManifestReport & ValidatorReport)[] =\n deepClone(mergedReports);\n\n // Was fixed\n if (!shouldRunFixes) {\n for (const report of allReports) {\n if (report.fix) {\n report.wasFixed = true;\n delete report.fix;\n }\n }\n\n return {\n files: fixResults.files,\n updated: true,\n reports: allReports,\n };\n }\n\n for (const report of allReports) {\n delete report.fix;\n }\n\n return {\n files: results.files,\n updated: false,\n reports: allReports,\n };\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(\n `Failed to read snap bundle file: ${getErrorMessage(error)}`,\n );\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read snap icon file: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Get an array of paths from an unvalidated Snap manifest.\n *\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param selector - A function that returns the paths to the files.\n * @returns The paths to the files, if any.\n */\nexport function getSnapFilePaths(\n manifest: Json,\n selector: (manifest: Partial<SnapManifest>) => string[] | undefined,\n) {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const snapManifest = manifest as Partial<SnapManifest>;\n const paths = selector(snapManifest);\n\n if (!Array.isArray(paths)) {\n return undefined;\n }\n\n return paths;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the files with the\n * given paths and read them.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param paths - The paths to the files.\n * @param encoding - An optional encoding to pass down to readVirtualFile.\n * @returns A list of auxiliary files and their contents, if any.\n */\nexport async function getSnapFiles(\n basePath: string,\n paths: string[] | undefined,\n encoding: BufferEncoding | null = 'utf8',\n): Promise<VirtualFile[] | undefined> {\n if (!paths) {\n return undefined;\n }\n\n try {\n return await Promise.all(\n paths.map(async (filePath) =>\n readVirtualFile(pathUtils.join(basePath, filePath), encoding),\n ),\n );\n } catch (error) {\n throw new Error(`Failed to read snap files: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n"]}