@metamask/snaps-utils 7.7.0 → 7.8.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 (202) hide show
  1. package/CHANGELOG.md +19 -1
  2. package/dist/auxiliary-files.js +8 -5
  3. package/dist/auxiliary-files.mjs +9 -6
  4. package/dist/base64.js +4 -4
  5. package/dist/base64.mjs +3 -3
  6. package/dist/bytes.js +3 -3
  7. package/dist/bytes.mjs +2 -2
  8. package/dist/checksum.js +4 -4
  9. package/dist/checksum.mjs +3 -3
  10. package/dist/{chunk-7LOVOG6X.js → chunk-25ESL4NL.js} +27 -27
  11. package/dist/{chunk-6YRUDGNL.mjs → chunk-3S4INAGA.mjs} +2 -2
  12. package/dist/{chunk-I4TRH6VO.mjs → chunk-4AA3TKRJ.mjs} +2 -2
  13. package/dist/chunk-4AA3TKRJ.mjs.map +1 -0
  14. package/dist/{chunk-2LBN5T56.js → chunk-4ZJOU3LR.js} +9 -2
  15. package/dist/chunk-4ZJOU3LR.js.map +1 -0
  16. package/dist/{chunk-ZJKG5Q2C.js → chunk-5I5WPAVD.js} +20 -20
  17. package/dist/chunk-5I5WPAVD.js.map +1 -0
  18. package/dist/chunk-5WKQI22S.js +7 -0
  19. package/dist/chunk-5WKQI22S.js.map +1 -0
  20. package/dist/{chunk-J5B5YRG5.js → chunk-6ATNGN44.js} +35 -2
  21. package/dist/chunk-6ATNGN44.js.map +1 -0
  22. package/dist/{chunk-G6BVXNNZ.js → chunk-6HXIPMBN.js} +3 -3
  23. package/dist/{chunk-5ZIWKLVB.mjs → chunk-7HLSXEHK.mjs} +35 -2
  24. package/dist/chunk-7HLSXEHK.mjs.map +1 -0
  25. package/dist/{chunk-T6FWIDA6.mjs → chunk-AJNKQYKP.mjs} +3 -3
  26. package/dist/chunk-AJNKQYKP.mjs.map +1 -0
  27. package/dist/{chunk-MLOQHUOY.js → chunk-AMW2VJ3N.js} +3 -3
  28. package/dist/{chunk-K2OTEZZZ.mjs → chunk-ANYNWSCA.mjs} +11 -4
  29. package/dist/chunk-ANYNWSCA.mjs.map +1 -0
  30. package/dist/{chunk-R5DO7T2D.js → chunk-B5GNTDE2.js} +2 -2
  31. package/dist/chunk-B5GNTDE2.js.map +1 -0
  32. package/dist/{chunk-WZ457PEQ.mjs → chunk-CJK7DDV2.mjs} +2 -2
  33. package/dist/chunk-CJK7DDV2.mjs.map +1 -0
  34. package/dist/{chunk-ZJRWU4AJ.mjs → chunk-DE22V5AO.mjs} +4 -1
  35. package/dist/{chunk-ZJRWU4AJ.mjs.map → chunk-DE22V5AO.mjs.map} +1 -1
  36. package/dist/{chunk-HJYRGKCX.js → chunk-DEQUNIME.js} +4 -1
  37. package/dist/chunk-DEQUNIME.js.map +1 -0
  38. package/dist/chunk-F6G67XQ4.js +1 -0
  39. package/dist/{chunk-NHGIT2SZ.mjs → chunk-FLSK6ISS.mjs} +6 -6
  40. package/dist/chunk-FLSK6ISS.mjs.map +1 -0
  41. package/dist/{chunk-YCIPY3UP.mjs → chunk-H35ZUVQT.mjs} +7 -7
  42. package/dist/chunk-H35ZUVQT.mjs.map +1 -0
  43. package/dist/{chunk-IPOE62V4.js → chunk-I2SWLZ34.js} +2 -2
  44. package/dist/chunk-I2SWLZ34.js.map +1 -0
  45. package/dist/{chunk-FOWIC2SO.mjs → chunk-JJTIVHFX.mjs} +2 -2
  46. package/dist/{chunk-NN2HXPZN.mjs → chunk-KCECQM3L.mjs} +11 -11
  47. package/dist/{chunk-EXUEHPZ4.mjs → chunk-LHQP7CUJ.mjs} +2 -2
  48. package/dist/chunk-LHQP7CUJ.mjs.map +1 -0
  49. package/dist/{chunk-6LOYTBS3.js → chunk-LOJA2ZS4.js} +2 -2
  50. package/dist/chunk-LOJA2ZS4.js.map +1 -0
  51. package/dist/{chunk-QSCKTRRU.js → chunk-MKFQAXRF.js} +3 -3
  52. package/dist/{chunk-UMZNVWEM.mjs → chunk-N5HVDE3P.mjs} +2 -2
  53. package/dist/chunk-N5HVDE3P.mjs.map +1 -0
  54. package/dist/{chunk-IHQPAJ2B.js → chunk-OBN2WDFU.js} +3 -3
  55. package/dist/chunk-OYZPBNHS.mjs +1 -0
  56. package/dist/chunk-PLNIERDY.js +34 -0
  57. package/dist/chunk-PLNIERDY.js.map +1 -0
  58. package/dist/{chunk-CMOSYNZR.js → chunk-PZNIU23I.js} +3 -3
  59. package/dist/chunk-PZNIU23I.js.map +1 -0
  60. package/dist/{chunk-K4B37F3L.js → chunk-RD6BN2TQ.js} +12 -12
  61. package/dist/chunk-RD6BN2TQ.js.map +1 -0
  62. package/dist/{chunk-F72OSQD6.js → chunk-SB43G2RI.js} +8 -8
  63. package/dist/chunk-SPCIIRSB.mjs +7 -0
  64. package/dist/chunk-SPCIIRSB.mjs.map +1 -0
  65. package/dist/{chunk-37VHIRUJ.js → chunk-T2VA4YJE.js} +11 -4
  66. package/dist/chunk-T2VA4YJE.js.map +1 -0
  67. package/dist/{chunk-473MIETW.js → chunk-UAINMOUU.js} +3 -3
  68. package/dist/chunk-VIAHMNTA.mjs +34 -0
  69. package/dist/chunk-VIAHMNTA.mjs.map +1 -0
  70. package/dist/{chunk-HEXZ4DFV.js → chunk-VOPTAKVG.js} +4 -4
  71. package/dist/chunk-VOPTAKVG.js.map +1 -0
  72. package/dist/{chunk-OJM2SKJZ.js → chunk-X2GGUOMY.js} +2 -2
  73. package/dist/chunk-X2GGUOMY.js.map +1 -0
  74. package/dist/{chunk-LBCPJOAV.mjs → chunk-X3UZCGO5.mjs} +2 -2
  75. package/dist/{chunk-HJRCBSNA.mjs → chunk-XF2AZMWG.mjs} +2 -2
  76. package/dist/{chunk-VDJTPG6F.js → chunk-XWYJQWHG.js} +3 -3
  77. package/dist/{chunk-PWLXPALG.mjs → chunk-XZ7362GQ.mjs} +2 -2
  78. package/dist/{chunk-IGMAXVPP.mjs → chunk-XZNJFDBF.mjs} +2 -2
  79. package/dist/{chunk-EA2FOAEG.mjs → chunk-YJHQZFR4.mjs} +9 -2
  80. package/dist/chunk-YJHQZFR4.mjs.map +1 -0
  81. package/dist/{chunk-BT2AEFEP.mjs → chunk-YNOZU43P.mjs} +6 -6
  82. package/dist/{chunk-EXEEDJ6N.mjs → chunk-ZXHR322P.mjs} +19 -19
  83. package/dist/chunk-ZXHR322P.mjs.map +1 -0
  84. package/dist/constants.js +8 -0
  85. package/dist/constants.js.map +1 -0
  86. package/dist/constants.mjs +8 -0
  87. package/dist/constants.mjs.map +1 -0
  88. package/dist/cronjob.js +2 -2
  89. package/dist/cronjob.mjs +1 -1
  90. package/dist/eval-worker.js +1 -1
  91. package/dist/eval-worker.mjs +1 -1
  92. package/dist/eval.js +5 -5
  93. package/dist/eval.mjs +4 -4
  94. package/dist/fs.js +4 -4
  95. package/dist/fs.mjs +3 -3
  96. package/dist/handlers.js +2 -2
  97. package/dist/handlers.mjs +1 -1
  98. package/dist/index.executionenv.js +4 -4
  99. package/dist/index.executionenv.mjs +15 -15
  100. package/dist/index.js +37 -29
  101. package/dist/index.mjs +74 -66
  102. package/dist/json-rpc.js +2 -2
  103. package/dist/json-rpc.mjs +1 -1
  104. package/dist/localization.js +2 -2
  105. package/dist/localization.mjs +1 -1
  106. package/dist/manifest/index.js +11 -11
  107. package/dist/manifest/index.mjs +10 -10
  108. package/dist/manifest/manifest.js +15 -15
  109. package/dist/manifest/manifest.mjs +14 -14
  110. package/dist/manifest/node.js +15 -15
  111. package/dist/manifest/node.mjs +14 -14
  112. package/dist/manifest/validation.js +11 -11
  113. package/dist/manifest/validation.mjs +10 -10
  114. package/dist/namespace.js +2 -2
  115. package/dist/namespace.mjs +1 -1
  116. package/dist/node.js +41 -33
  117. package/dist/node.mjs +78 -70
  118. package/dist/npm.js +15 -15
  119. package/dist/npm.mjs +14 -14
  120. package/dist/snaps.js +6 -6
  121. package/dist/snaps.mjs +5 -5
  122. package/dist/structs.js +2 -2
  123. package/dist/structs.mjs +1 -1
  124. package/dist/tsconfig.build.tsbuildinfo +1 -1
  125. package/dist/types/auxiliary-files.d.ts +7 -0
  126. package/dist/types/constants.d.ts +1 -0
  127. package/dist/types/cronjob.d.ts +22 -22
  128. package/dist/types/handlers.d.ts +39 -39
  129. package/dist/types/index.d.ts +1 -0
  130. package/dist/types/json-rpc.d.ts +7 -7
  131. package/dist/types/localization.d.ts +4 -4
  132. package/dist/types/manifest/validation.d.ts +1 -1
  133. package/dist/types/namespace.d.ts +18 -18
  134. package/dist/types/snaps.d.ts +1 -1
  135. package/dist/types/structs.d.ts +2 -3
  136. package/dist/types/types.d.ts +1 -1
  137. package/dist/types/ui.d.ts +10 -1
  138. package/dist/types/virtual-file/VirtualFile.d.ts +1 -0
  139. package/dist/types.js +2 -2
  140. package/dist/types.mjs +1 -1
  141. package/dist/ui.js +4 -2
  142. package/dist/ui.mjs +3 -1
  143. package/dist/validation.js +13 -13
  144. package/dist/validation.mjs +12 -12
  145. package/dist/versions.js +2 -2
  146. package/dist/versions.mjs +1 -1
  147. package/dist/virtual-file/VirtualFile.js +2 -2
  148. package/dist/virtual-file/VirtualFile.mjs +1 -1
  149. package/dist/virtual-file/index.js +2 -2
  150. package/dist/virtual-file/index.mjs +1 -1
  151. package/dist/virtual-file/node.js +3 -3
  152. package/dist/virtual-file/node.mjs +2 -2
  153. package/dist/virtual-file/toVirtualFile.js +3 -3
  154. package/dist/virtual-file/toVirtualFile.mjs +2 -2
  155. package/package.json +9 -9
  156. package/dist/chunk-2LBN5T56.js.map +0 -1
  157. package/dist/chunk-37VHIRUJ.js.map +0 -1
  158. package/dist/chunk-5ZIWKLVB.mjs.map +0 -1
  159. package/dist/chunk-6LOYTBS3.js.map +0 -1
  160. package/dist/chunk-7LOABHVW.js +0 -1
  161. package/dist/chunk-7MTAHOWC.mjs +0 -1
  162. package/dist/chunk-CMOSYNZR.js.map +0 -1
  163. package/dist/chunk-EA2FOAEG.mjs.map +0 -1
  164. package/dist/chunk-EXEEDJ6N.mjs.map +0 -1
  165. package/dist/chunk-EXUEHPZ4.mjs.map +0 -1
  166. package/dist/chunk-HEXZ4DFV.js.map +0 -1
  167. package/dist/chunk-HJYRGKCX.js.map +0 -1
  168. package/dist/chunk-I4TRH6VO.mjs.map +0 -1
  169. package/dist/chunk-IPOE62V4.js.map +0 -1
  170. package/dist/chunk-IXBJNVHK.js +0 -22
  171. package/dist/chunk-IXBJNVHK.js.map +0 -1
  172. package/dist/chunk-J5B5YRG5.js.map +0 -1
  173. package/dist/chunk-K2OTEZZZ.mjs.map +0 -1
  174. package/dist/chunk-K4B37F3L.js.map +0 -1
  175. package/dist/chunk-KMLVVVK3.mjs +0 -22
  176. package/dist/chunk-KMLVVVK3.mjs.map +0 -1
  177. package/dist/chunk-NHGIT2SZ.mjs.map +0 -1
  178. package/dist/chunk-OJM2SKJZ.js.map +0 -1
  179. package/dist/chunk-R5DO7T2D.js.map +0 -1
  180. package/dist/chunk-T6FWIDA6.mjs.map +0 -1
  181. package/dist/chunk-UMZNVWEM.mjs.map +0 -1
  182. package/dist/chunk-WZ457PEQ.mjs.map +0 -1
  183. package/dist/chunk-YCIPY3UP.mjs.map +0 -1
  184. package/dist/chunk-ZJKG5Q2C.js.map +0 -1
  185. /package/dist/{chunk-7LOVOG6X.js.map → chunk-25ESL4NL.js.map} +0 -0
  186. /package/dist/{chunk-6YRUDGNL.mjs.map → chunk-3S4INAGA.mjs.map} +0 -0
  187. /package/dist/{chunk-G6BVXNNZ.js.map → chunk-6HXIPMBN.js.map} +0 -0
  188. /package/dist/{chunk-MLOQHUOY.js.map → chunk-AMW2VJ3N.js.map} +0 -0
  189. /package/dist/{chunk-7LOABHVW.js.map → chunk-F6G67XQ4.js.map} +0 -0
  190. /package/dist/{chunk-FOWIC2SO.mjs.map → chunk-JJTIVHFX.mjs.map} +0 -0
  191. /package/dist/{chunk-NN2HXPZN.mjs.map → chunk-KCECQM3L.mjs.map} +0 -0
  192. /package/dist/{chunk-QSCKTRRU.js.map → chunk-MKFQAXRF.js.map} +0 -0
  193. /package/dist/{chunk-IHQPAJ2B.js.map → chunk-OBN2WDFU.js.map} +0 -0
  194. /package/dist/{chunk-7MTAHOWC.mjs.map → chunk-OYZPBNHS.mjs.map} +0 -0
  195. /package/dist/{chunk-F72OSQD6.js.map → chunk-SB43G2RI.js.map} +0 -0
  196. /package/dist/{chunk-473MIETW.js.map → chunk-UAINMOUU.js.map} +0 -0
  197. /package/dist/{chunk-LBCPJOAV.mjs.map → chunk-X3UZCGO5.mjs.map} +0 -0
  198. /package/dist/{chunk-HJRCBSNA.mjs.map → chunk-XF2AZMWG.mjs.map} +0 -0
  199. /package/dist/{chunk-PWLXPALG.mjs.map → chunk-XWYJQWHG.js.map} +0 -0
  200. /package/dist/{chunk-VDJTPG6F.js.map → chunk-XZ7362GQ.mjs.map} +0 -0
  201. /package/dist/{chunk-IGMAXVPP.mjs.map → chunk-XZNJFDBF.mjs.map} +0 -0
  202. /package/dist/{chunk-BT2AEFEP.mjs.map → chunk-YNOZU43P.mjs.map} +0 -0
@@ -253,6 +253,39 @@ function walkJsx(node, callback, depth = 0) {
253
253
  }
254
254
  return void 0;
255
255
  }
256
+ function serialiseProp(prop) {
257
+ if (typeof prop === "string") {
258
+ return `"${prop}"`;
259
+ }
260
+ return `{${JSON.stringify(prop)}}`;
261
+ }
262
+ function serialiseProps(props) {
263
+ return Object.entries(props).filter(([key]) => key !== "children").sort(([a], [b]) => a.localeCompare(b)).map(([key, value]) => ` ${key}=${serialiseProp(value)}`).join("");
264
+ }
265
+ function serialiseJsx(node, indentation = 0) {
266
+ if (Array.isArray(node)) {
267
+ return node.map((child) => serialiseJsx(child, indentation)).join("");
268
+ }
269
+ const indent = " ".repeat(indentation);
270
+ if (typeof node === "string") {
271
+ return `${indent}${node}
272
+ `;
273
+ }
274
+ if (!node) {
275
+ return "";
276
+ }
277
+ const { type, props } = node;
278
+ const trailingNewline = indentation > 0 ? "\n" : "";
279
+ if (_utils.hasProperty.call(void 0, props, "children")) {
280
+ const children = serialiseJsx(props.children, indentation + 1);
281
+ return `${indent}<${type}${serialiseProps(
282
+ props
283
+ )}>
284
+ ${children}${indent}</${type}>${trailingNewline}`;
285
+ }
286
+ return `${indent}<${type}${serialiseProps(props)} />${trailingNewline}`;
287
+ }
288
+
256
289
 
257
290
 
258
291
 
@@ -263,5 +296,5 @@ function walkJsx(node, callback, depth = 0) {
263
296
 
264
297
 
265
298
 
266
- exports.getTextChildren = getTextChildren; exports.getJsxElementFromComponent = getJsxElementFromComponent; exports.validateTextLinks = validateTextLinks; exports.validateJsxLinks = validateJsxLinks; exports.getTotalTextLength = getTotalTextLength; exports.hasChildren = hasChildren; exports.getJsxChildren = getJsxChildren; exports.walkJsx = walkJsx;
267
- //# sourceMappingURL=chunk-J5B5YRG5.js.map
299
+ exports.getTextChildren = getTextChildren; exports.getJsxElementFromComponent = getJsxElementFromComponent; exports.validateTextLinks = validateTextLinks; exports.validateJsxLinks = validateJsxLinks; exports.getTotalTextLength = getTotalTextLength; exports.hasChildren = hasChildren; exports.getJsxChildren = getJsxChildren; exports.walkJsx = walkJsx; exports.serialiseJsx = serialiseJsx;
300
+ //# sourceMappingURL=chunk-6ATNGN44.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/ui.tsx"],"names":[],"mappings":";AACA,SAAS,gBAAgB;AAazB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,OAAO,kBAAkB;AAuErB;AApEb,IAAM,kBAAkB;AACxB,IAAM,oBAAoB,CAAC,UAAU,SAAS;AAQ9C,SAAS,iBAAiB,SAA+C;AACvE,UAAQ,SAAS;AAAA,IACf,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT;AAC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M,QAAQ,GAAG,MAAM,KAAK,IAAI,IAAI,eAAe;AAAA,EACtD;AAEA,SAAO,GAAG,MAAM,IAAI,IAAI,GAAG,eAAe,KAAK,CAAC,MAAM,eAAe;AACvE","sourcesContent":["import type { Component } from '@metamask/snaps-sdk';\nimport { NodeType } from '@metamask/snaps-sdk';\nimport type {\n BoldChildren,\n GenericSnapElement,\n ItalicChildren,\n JSXElement,\n LinkElement,\n Nestable,\n RowChildren,\n SnapNode,\n StandardFormattingElement,\n TextChildren,\n} from '@metamask/snaps-sdk/jsx';\nimport {\n Italic,\n Link,\n Bold,\n Row,\n Text,\n Field,\n Image,\n Input,\n Heading,\n Form,\n Divider,\n Spinner,\n Copyable,\n Box,\n Button,\n Address,\n} from '@metamask/snaps-sdk/jsx';\nimport {\n assert,\n assertExhaustive,\n AssertionError,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { lexer, walkTokens } from 'marked';\nimport type { Token, Tokens } from 'marked';\n\nconst MAX_TEXT_LENGTH = 50_000; // 50 kb\nconst ALLOWED_PROTOCOLS = ['https:', 'mailto:'];\n\n/**\n * Get the button variant from a legacy button component variant.\n *\n * @param variant - The legacy button component variant.\n * @returns The button variant.\n */\nfunction getButtonVariant(variant?: 'primary' | 'secondary' | undefined) {\n switch (variant) {\n case 'primary':\n return 'primary';\n case 'secondary':\n return 'destructive';\n default:\n return undefined;\n }\n}\n\n/**\n * Get the children of a JSX element. If there is only one child, the child is\n * returned directly. Otherwise, the children are returned as an array.\n *\n * @param elements - The JSX elements.\n * @returns The child or children.\n */\nfunction getChildren<Type>(elements: Type[]) {\n if (elements.length === 1) {\n return elements[0];\n }\n\n return elements;\n}\n\n/**\n * Get the text of a link token.\n *\n * @param token - The link token.\n * @returns The text of the link token.\n */\nfunction getLinkText(token: Tokens.Link | Tokens.Generic) {\n if (token.tokens && token.tokens.length > 0) {\n return getChildren(token.tokens.flatMap(getTextChildFromToken));\n }\n\n return token.href;\n}\n\n/**\n * Get the text child from a list of markdown tokens.\n *\n * @param tokens - The markdown tokens.\n * @returns The text child.\n */\nfunction getTextChildFromTokens(tokens: Token[]) {\n return getChildren(tokens.flatMap(getTextChildFromToken));\n}\n\n/**\n * Get the text child from a markdown token.\n *\n * @param token - The markdown token.\n * @returns The text child.\n */\nfunction getTextChildFromToken(token: Token): TextChildren {\n switch (token.type) {\n case 'link': {\n return <Link href={token.href} children={getLinkText(token)} />;\n }\n\n case 'text':\n return token.text;\n\n case 'strong':\n return (\n <Bold>\n {\n getTextChildFromTokens(\n // Due to the way `marked` is typed, `token.tokens` can be\n // `undefined`, but it's a required field of `Tokens.Bold`, so we\n // can safely cast it to `Token[]`.\n token.tokens as Token[],\n ) as BoldChildren\n }\n </Bold>\n );\n\n case 'em':\n return (\n <Italic>\n {\n getTextChildFromTokens(\n // Due to the way `marked` is typed, `token.tokens` can be\n // `undefined`, but it's a required field of `Tokens.Bold`, so we\n // can safely cast it to `Token[]`.\n token.tokens as Token[],\n ) as ItalicChildren\n }\n </Italic>\n );\n\n default:\n return null;\n }\n}\n\n/**\n * Get all text children from a markdown string.\n *\n * @param value - The markdown string.\n * @returns The text children.\n */\nexport function getTextChildren(\n value: string,\n): (string | StandardFormattingElement | LinkElement)[] {\n const rootTokens = lexer(value, { gfm: false });\n const children: (string | StandardFormattingElement | LinkElement | null)[] =\n [];\n\n walkTokens(rootTokens, (token) => {\n if (token.type === 'paragraph') {\n if (children.length > 0) {\n children.push('\\n\\n');\n }\n\n const { tokens } = token as Tokens.Paragraph;\n // We do not need to consider nesting deeper than 1 level here and we can therefore cast.\n children.push(\n ...(tokens.flatMap(getTextChildFromToken) as (\n | string\n | StandardFormattingElement\n | LinkElement\n | null\n )[]),\n );\n }\n });\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n return children.filter((child) => child !== null) as (\n | string\n | StandardFormattingElement\n | LinkElement\n )[];\n}\n\n/**\n * Validate the text size of a component. The text size is the total length of\n * all text in the component.\n *\n * @param component - The component to validate.\n * @throws An error if the text size exceeds the maximum allowed size.\n */\nfunction validateComponentTextSize(component: Component) {\n const textSize = getTotalTextLength(component);\n assert(\n textSize <= MAX_TEXT_LENGTH,\n `The text in a Snap UI may not be larger than ${\n MAX_TEXT_LENGTH / 1000\n } kB.`,\n );\n}\n\n/**\n * Get a JSX element from a legacy UI component. This supports all legacy UI\n * components, and maps them to their JSX equivalents where possible.\n *\n * This function validates the text size of the component, but does not validate\n * the total size. The total size of the component should be validated before\n * calling this function.\n *\n * @param legacyComponent - The legacy UI component.\n * @returns The JSX element.\n */\nexport function getJsxElementFromComponent(\n legacyComponent: Component,\n): JSXElement {\n validateComponentTextSize(legacyComponent);\n\n /**\n * Get the JSX element for a component. This function is recursive and will\n * call itself for child components.\n *\n * @param component - The component to convert to a JSX element.\n * @returns The JSX element.\n */\n function getElement(component: Component) {\n switch (component.type) {\n case NodeType.Address:\n return <Address address={component.value} />;\n\n case NodeType.Button:\n return (\n <Button\n name={component.name}\n variant={getButtonVariant(component.variant)}\n type={component.buttonType}\n >\n {component.value}\n </Button>\n );\n\n case NodeType.Copyable:\n return (\n <Copyable value={component.value} sensitive={component.sensitive} />\n );\n\n case NodeType.Divider:\n return <Divider />;\n\n case NodeType.Form:\n return (\n <Form name={component.name}>\n {getChildren(component.children.map(getElement))}\n </Form>\n );\n\n case NodeType.Heading:\n return <Heading children={component.value} />;\n\n case NodeType.Image:\n // `Image` supports `alt`, but the legacy `Image` component does not.\n return <Image src={component.value} />;\n\n case NodeType.Input:\n return (\n <Field label={component.label} error={component.error}>\n <Input\n name={component.name}\n type={component.inputType}\n value={component.value}\n placeholder={component.placeholder}\n />\n </Field>\n );\n\n case NodeType.Panel:\n // `Panel` is renamed to `Box` in JSX.\n return (\n <Box children={getChildren(component.children.map(getElement))} />\n );\n\n case NodeType.Row:\n return (\n <Row label={component.label} variant={component.variant}>\n {getElement(component.value) as RowChildren}\n </Row>\n );\n\n case NodeType.Spinner:\n return <Spinner />;\n\n case NodeType.Text:\n return <Text>{getChildren(getTextChildren(component.value))}</Text>;\n\n /* istanbul ignore next 2 */\n default:\n return assertExhaustive(component);\n }\n }\n\n return getElement(legacyComponent);\n}\n\n/**\n * Extract all links from a Markdown text string using the `marked` lexer.\n *\n * @param text - The markdown text string.\n * @returns A list of URLs linked to in the string.\n */\nfunction getMarkdownLinks(text: string) {\n const tokens = lexer(text, { gfm: false });\n const links: Tokens.Link[] = [];\n\n // Walk the lexed tokens and collect all link tokens\n walkTokens(tokens, (token) => {\n if (token.type === 'link') {\n links.push(token as Tokens.Link);\n }\n });\n\n return links;\n}\n\n/**\n * Validate a link against the phishing list.\n *\n * @param link - The link to validate.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n */\nfunction validateLink(\n link: string,\n isOnPhishingList: (url: string) => boolean,\n) {\n try {\n const url = new URL(link);\n assert(\n ALLOWED_PROTOCOLS.includes(url.protocol),\n `Protocol must be one of: ${ALLOWED_PROTOCOLS.join(', ')}.`,\n );\n\n const hostname =\n url.protocol === 'mailto:' ? url.pathname.split('@')[1] : url.hostname;\n\n assert(!isOnPhishingList(hostname), 'The specified URL is not allowed.');\n } catch (error) {\n throw new Error(\n `Invalid URL: ${\n error instanceof AssertionError ? error.message : 'Unable to parse URL.'\n }`,\n );\n }\n}\n\n/**\n * Search for Markdown links in a string and checks them against the phishing\n * list.\n *\n * @param text - The text to verify.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n * @throws If the text contains a link that is not allowed.\n */\nexport function validateTextLinks(\n text: string,\n isOnPhishingList: (url: string) => boolean,\n) {\n const links = getMarkdownLinks(text);\n\n for (const link of links) {\n validateLink(link.href, isOnPhishingList);\n }\n}\n\n/**\n * Walk a JSX tree and validate each {@link LinkElement} node against the\n * phishing list.\n *\n * @param node - The JSX node to walk.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n */\nexport function validateJsxLinks(\n node: JSXElement,\n isOnPhishingList: (url: string) => boolean,\n) {\n walkJsx(node, (childNode) => {\n if (childNode.type !== 'Link') {\n return;\n }\n\n validateLink(childNode.props.href, isOnPhishingList);\n });\n}\n\n/**\n * Calculate the total length of all text in the component.\n *\n * @param component - A custom UI component.\n * @returns The total length of all text components in the component.\n */\nexport function getTotalTextLength(component: Component): number {\n const { type } = component;\n\n switch (type) {\n case NodeType.Panel:\n return component.children.reduce<number>(\n // This is a bug in TypeScript: https://github.com/microsoft/TypeScript/issues/48313\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n (sum, node) => sum + getTotalTextLength(node),\n 0,\n );\n\n case NodeType.Row:\n return getTotalTextLength(component.value);\n\n case NodeType.Text:\n return component.value.length;\n\n default:\n return 0;\n }\n}\n\n/**\n * Check if a JSX element has children.\n *\n * @param element - A JSX element.\n * @returns `true` if the element has children, `false` otherwise.\n */\nexport function hasChildren<Element extends JSXElement>(\n element: Element,\n): element is Element & {\n props: { children: Nestable<JSXElement | string> };\n} {\n return hasProperty(element.props, 'children');\n}\n\n/**\n * Filter a JSX child to remove `null`, `undefined`, plain booleans, and empty\n * strings.\n *\n * @param child - The JSX child to filter.\n * @returns `true` if the child is not `null`, `undefined`, a plain boolean, or\n * an empty string, `false` otherwise.\n */\nfunction filterJsxChild(child: JSXElement | string | boolean | null): boolean {\n return Boolean(child) && child !== true;\n}\n\n/**\n * Get the children of a JSX element as an array. If the element has only one\n * child, the child is returned as an array.\n *\n * @param element - A JSX element.\n * @returns The children of the element.\n */\nexport function getJsxChildren(element: JSXElement): (JSXElement | string)[] {\n if (hasChildren(element)) {\n if (Array.isArray(element.props.children)) {\n // @ts-expect-error - Each member of the union type has signatures, but\n // none of those signatures are compatible with each other.\n return element.props.children.filter(filterJsxChild).flat(Infinity);\n }\n\n if (element.props.children) {\n return [element.props.children];\n }\n }\n\n return [];\n}\n\n/**\n * Walk a JSX tree and call a callback on each node.\n *\n * @param node - The JSX node to walk.\n * @param callback - The callback to call on each node.\n * @param depth - The current depth in the JSX tree for a walk.\n * @returns The result of the callback, if any.\n */\nexport function walkJsx<Value>(\n node: JSXElement | JSXElement[],\n callback: (node: JSXElement, depth: number) => Value | undefined,\n depth = 0,\n): Value | undefined {\n if (Array.isArray(node)) {\n for (const child of node) {\n const childResult = walkJsx(child as JSXElement, callback, depth);\n if (childResult !== undefined) {\n return childResult;\n }\n }\n\n return undefined;\n }\n\n const result = callback(node, depth);\n if (result !== undefined) {\n return result;\n }\n\n if (\n hasProperty(node, 'props') &&\n isPlainObject(node.props) &&\n hasProperty(node.props, 'children')\n ) {\n const children = getJsxChildren(node);\n for (const child of children) {\n if (isPlainObject(child)) {\n const childResult = walkJsx(child, callback, depth + 1);\n if (childResult !== undefined) {\n return childResult;\n }\n }\n }\n }\n\n return undefined;\n}\n\n/**\n * Serialise a JSX prop to a string.\n *\n * @param prop - The JSX prop.\n * @returns The serialised JSX prop.\n */\nfunction serialiseProp(prop: unknown): string {\n if (typeof prop === 'string') {\n return `\"${prop}\"`;\n }\n\n return `{${JSON.stringify(prop)}}`;\n}\n\n/**\n * Serialise JSX props to a string.\n *\n * @param props - The JSX props.\n * @returns The serialised JSX props.\n */\nfunction serialiseProps(props: Record<string, unknown>): string {\n return Object.entries(props)\n .filter(([key]) => key !== 'children')\n .sort(([a], [b]) => a.localeCompare(b))\n .map(([key, value]) => ` ${key}=${serialiseProp(value)}`)\n .join('');\n}\n\n/**\n * Serialise a JSX node to a string.\n *\n * @param node - The JSX node.\n * @param indentation - The indentation level. Defaults to `0`. This should not\n * be set by the caller, as it is used for recursion.\n * @returns The serialised JSX node.\n */\nexport function serialiseJsx(node: SnapNode, indentation = 0): string {\n if (Array.isArray(node)) {\n return node.map((child) => serialiseJsx(child, indentation)).join('');\n }\n\n const indent = ' '.repeat(indentation);\n if (typeof node === 'string') {\n return `${indent}${node}\\n`;\n }\n\n if (!node) {\n return '';\n }\n\n const { type, props } = node as GenericSnapElement;\n const trailingNewline = indentation > 0 ? '\\n' : '';\n\n if (hasProperty(props, 'children')) {\n const children = serialiseJsx(props.children as SnapNode, indentation + 1);\n return `${indent}<${type}${serialiseProps(\n props,\n )}>\\n${children}${indent}</${type}>${trailingNewline}`;\n }\n\n return `${indent}<${type}${serialiseProps(props)} />${trailingNewline}`;\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2
2
 
3
- var _chunkQSCKTRRUjs = require('./chunk-QSCKTRRU.js');
3
+ var _chunkMKFQAXRFjs = require('./chunk-MKFQAXRF.js');
4
4
 
5
5
 
6
6
  var _chunk7VJ2BOVUjs = require('./chunk-7VJ2BOVU.js');
@@ -38,7 +38,7 @@ async function readJsonFile(pathString) {
38
38
  }
39
39
  let file;
40
40
  try {
41
- file = await _chunkQSCKTRRUjs.readVirtualFile.call(void 0, pathString, "utf8");
41
+ file = await _chunkMKFQAXRFjs.readVirtualFile.call(void 0, pathString, "utf8");
42
42
  } catch (error) {
43
43
  if (error.code === "ENOENT") {
44
44
  throw new Error(
@@ -100,4 +100,4 @@ async function useTemporaryFile(fileName, fileContents, fn) {
100
100
 
101
101
 
102
102
  exports.isDirectory = isDirectory; exports.isFile = isFile; exports.readJsonFile = readJsonFile; exports.getOutfilePath = getOutfilePath; exports.validateOutfileName = validateOutfileName; exports.validateFilePath = validateFilePath; exports.validateDirPath = validateDirPath; exports.useTemporaryFile = useTemporaryFile;
103
- //# sourceMappingURL=chunk-G6BVXNNZ.js.map
103
+ //# sourceMappingURL=chunk-6HXIPMBN.js.map
@@ -253,6 +253,38 @@ function walkJsx(node, callback, depth = 0) {
253
253
  }
254
254
  return void 0;
255
255
  }
256
+ function serialiseProp(prop) {
257
+ if (typeof prop === "string") {
258
+ return `"${prop}"`;
259
+ }
260
+ return `{${JSON.stringify(prop)}}`;
261
+ }
262
+ function serialiseProps(props) {
263
+ return Object.entries(props).filter(([key]) => key !== "children").sort(([a], [b]) => a.localeCompare(b)).map(([key, value]) => ` ${key}=${serialiseProp(value)}`).join("");
264
+ }
265
+ function serialiseJsx(node, indentation = 0) {
266
+ if (Array.isArray(node)) {
267
+ return node.map((child) => serialiseJsx(child, indentation)).join("");
268
+ }
269
+ const indent = " ".repeat(indentation);
270
+ if (typeof node === "string") {
271
+ return `${indent}${node}
272
+ `;
273
+ }
274
+ if (!node) {
275
+ return "";
276
+ }
277
+ const { type, props } = node;
278
+ const trailingNewline = indentation > 0 ? "\n" : "";
279
+ if (hasProperty(props, "children")) {
280
+ const children = serialiseJsx(props.children, indentation + 1);
281
+ return `${indent}<${type}${serialiseProps(
282
+ props
283
+ )}>
284
+ ${children}${indent}</${type}>${trailingNewline}`;
285
+ }
286
+ return `${indent}<${type}${serialiseProps(props)} />${trailingNewline}`;
287
+ }
256
288
 
257
289
  export {
258
290
  getTextChildren,
@@ -262,6 +294,7 @@ export {
262
294
  getTotalTextLength,
263
295
  hasChildren,
264
296
  getJsxChildren,
265
- walkJsx
297
+ walkJsx,
298
+ serialiseJsx
266
299
  };
267
- //# sourceMappingURL=chunk-5ZIWKLVB.mjs.map
300
+ //# sourceMappingURL=chunk-7HLSXEHK.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/ui.tsx"],"sourcesContent":["import type { Component } from '@metamask/snaps-sdk';\nimport { NodeType } from '@metamask/snaps-sdk';\nimport type {\n BoldChildren,\n GenericSnapElement,\n ItalicChildren,\n JSXElement,\n LinkElement,\n Nestable,\n RowChildren,\n SnapNode,\n StandardFormattingElement,\n TextChildren,\n} from '@metamask/snaps-sdk/jsx';\nimport {\n Italic,\n Link,\n Bold,\n Row,\n Text,\n Field,\n Image,\n Input,\n Heading,\n Form,\n Divider,\n Spinner,\n Copyable,\n Box,\n Button,\n Address,\n} from '@metamask/snaps-sdk/jsx';\nimport {\n assert,\n assertExhaustive,\n AssertionError,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { lexer, walkTokens } from 'marked';\nimport type { Token, Tokens } from 'marked';\n\nconst MAX_TEXT_LENGTH = 50_000; // 50 kb\nconst ALLOWED_PROTOCOLS = ['https:', 'mailto:'];\n\n/**\n * Get the button variant from a legacy button component variant.\n *\n * @param variant - The legacy button component variant.\n * @returns The button variant.\n */\nfunction getButtonVariant(variant?: 'primary' | 'secondary' | undefined) {\n switch (variant) {\n case 'primary':\n return 'primary';\n case 'secondary':\n return 'destructive';\n default:\n return undefined;\n }\n}\n\n/**\n * Get the children of a JSX element. If there is only one child, the child is\n * returned directly. Otherwise, the children are returned as an array.\n *\n * @param elements - The JSX elements.\n * @returns The child or children.\n */\nfunction getChildren<Type>(elements: Type[]) {\n if (elements.length === 1) {\n return elements[0];\n }\n\n return elements;\n}\n\n/**\n * Get the text of a link token.\n *\n * @param token - The link token.\n * @returns The text of the link token.\n */\nfunction getLinkText(token: Tokens.Link | Tokens.Generic) {\n if (token.tokens && token.tokens.length > 0) {\n return getChildren(token.tokens.flatMap(getTextChildFromToken));\n }\n\n return token.href;\n}\n\n/**\n * Get the text child from a list of markdown tokens.\n *\n * @param tokens - The markdown tokens.\n * @returns The text child.\n */\nfunction getTextChildFromTokens(tokens: Token[]) {\n return getChildren(tokens.flatMap(getTextChildFromToken));\n}\n\n/**\n * Get the text child from a markdown token.\n *\n * @param token - The markdown token.\n * @returns The text child.\n */\nfunction getTextChildFromToken(token: Token): TextChildren {\n switch (token.type) {\n case 'link': {\n return <Link href={token.href} children={getLinkText(token)} />;\n }\n\n case 'text':\n return token.text;\n\n case 'strong':\n return (\n <Bold>\n {\n getTextChildFromTokens(\n // Due to the way `marked` is typed, `token.tokens` can be\n // `undefined`, but it's a required field of `Tokens.Bold`, so we\n // can safely cast it to `Token[]`.\n token.tokens as Token[],\n ) as BoldChildren\n }\n </Bold>\n );\n\n case 'em':\n return (\n <Italic>\n {\n getTextChildFromTokens(\n // Due to the way `marked` is typed, `token.tokens` can be\n // `undefined`, but it's a required field of `Tokens.Bold`, so we\n // can safely cast it to `Token[]`.\n token.tokens as Token[],\n ) as ItalicChildren\n }\n </Italic>\n );\n\n default:\n return null;\n }\n}\n\n/**\n * Get all text children from a markdown string.\n *\n * @param value - The markdown string.\n * @returns The text children.\n */\nexport function getTextChildren(\n value: string,\n): (string | StandardFormattingElement | LinkElement)[] {\n const rootTokens = lexer(value, { gfm: false });\n const children: (string | StandardFormattingElement | LinkElement | null)[] =\n [];\n\n walkTokens(rootTokens, (token) => {\n if (token.type === 'paragraph') {\n if (children.length > 0) {\n children.push('\\n\\n');\n }\n\n const { tokens } = token as Tokens.Paragraph;\n // We do not need to consider nesting deeper than 1 level here and we can therefore cast.\n children.push(\n ...(tokens.flatMap(getTextChildFromToken) as (\n | string\n | StandardFormattingElement\n | LinkElement\n | null\n )[]),\n );\n }\n });\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n return children.filter((child) => child !== null) as (\n | string\n | StandardFormattingElement\n | LinkElement\n )[];\n}\n\n/**\n * Validate the text size of a component. The text size is the total length of\n * all text in the component.\n *\n * @param component - The component to validate.\n * @throws An error if the text size exceeds the maximum allowed size.\n */\nfunction validateComponentTextSize(component: Component) {\n const textSize = getTotalTextLength(component);\n assert(\n textSize <= MAX_TEXT_LENGTH,\n `The text in a Snap UI may not be larger than ${\n MAX_TEXT_LENGTH / 1000\n } kB.`,\n );\n}\n\n/**\n * Get a JSX element from a legacy UI component. This supports all legacy UI\n * components, and maps them to their JSX equivalents where possible.\n *\n * This function validates the text size of the component, but does not validate\n * the total size. The total size of the component should be validated before\n * calling this function.\n *\n * @param legacyComponent - The legacy UI component.\n * @returns The JSX element.\n */\nexport function getJsxElementFromComponent(\n legacyComponent: Component,\n): JSXElement {\n validateComponentTextSize(legacyComponent);\n\n /**\n * Get the JSX element for a component. This function is recursive and will\n * call itself for child components.\n *\n * @param component - The component to convert to a JSX element.\n * @returns The JSX element.\n */\n function getElement(component: Component) {\n switch (component.type) {\n case NodeType.Address:\n return <Address address={component.value} />;\n\n case NodeType.Button:\n return (\n <Button\n name={component.name}\n variant={getButtonVariant(component.variant)}\n type={component.buttonType}\n >\n {component.value}\n </Button>\n );\n\n case NodeType.Copyable:\n return (\n <Copyable value={component.value} sensitive={component.sensitive} />\n );\n\n case NodeType.Divider:\n return <Divider />;\n\n case NodeType.Form:\n return (\n <Form name={component.name}>\n {getChildren(component.children.map(getElement))}\n </Form>\n );\n\n case NodeType.Heading:\n return <Heading children={component.value} />;\n\n case NodeType.Image:\n // `Image` supports `alt`, but the legacy `Image` component does not.\n return <Image src={component.value} />;\n\n case NodeType.Input:\n return (\n <Field label={component.label} error={component.error}>\n <Input\n name={component.name}\n type={component.inputType}\n value={component.value}\n placeholder={component.placeholder}\n />\n </Field>\n );\n\n case NodeType.Panel:\n // `Panel` is renamed to `Box` in JSX.\n return (\n <Box children={getChildren(component.children.map(getElement))} />\n );\n\n case NodeType.Row:\n return (\n <Row label={component.label} variant={component.variant}>\n {getElement(component.value) as RowChildren}\n </Row>\n );\n\n case NodeType.Spinner:\n return <Spinner />;\n\n case NodeType.Text:\n return <Text>{getChildren(getTextChildren(component.value))}</Text>;\n\n /* istanbul ignore next 2 */\n default:\n return assertExhaustive(component);\n }\n }\n\n return getElement(legacyComponent);\n}\n\n/**\n * Extract all links from a Markdown text string using the `marked` lexer.\n *\n * @param text - The markdown text string.\n * @returns A list of URLs linked to in the string.\n */\nfunction getMarkdownLinks(text: string) {\n const tokens = lexer(text, { gfm: false });\n const links: Tokens.Link[] = [];\n\n // Walk the lexed tokens and collect all link tokens\n walkTokens(tokens, (token) => {\n if (token.type === 'link') {\n links.push(token as Tokens.Link);\n }\n });\n\n return links;\n}\n\n/**\n * Validate a link against the phishing list.\n *\n * @param link - The link to validate.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n */\nfunction validateLink(\n link: string,\n isOnPhishingList: (url: string) => boolean,\n) {\n try {\n const url = new URL(link);\n assert(\n ALLOWED_PROTOCOLS.includes(url.protocol),\n `Protocol must be one of: ${ALLOWED_PROTOCOLS.join(', ')}.`,\n );\n\n const hostname =\n url.protocol === 'mailto:' ? url.pathname.split('@')[1] : url.hostname;\n\n assert(!isOnPhishingList(hostname), 'The specified URL is not allowed.');\n } catch (error) {\n throw new Error(\n `Invalid URL: ${\n error instanceof AssertionError ? error.message : 'Unable to parse URL.'\n }`,\n );\n }\n}\n\n/**\n * Search for Markdown links in a string and checks them against the phishing\n * list.\n *\n * @param text - The text to verify.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n * @throws If the text contains a link that is not allowed.\n */\nexport function validateTextLinks(\n text: string,\n isOnPhishingList: (url: string) => boolean,\n) {\n const links = getMarkdownLinks(text);\n\n for (const link of links) {\n validateLink(link.href, isOnPhishingList);\n }\n}\n\n/**\n * Walk a JSX tree and validate each {@link LinkElement} node against the\n * phishing list.\n *\n * @param node - The JSX node to walk.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n */\nexport function validateJsxLinks(\n node: JSXElement,\n isOnPhishingList: (url: string) => boolean,\n) {\n walkJsx(node, (childNode) => {\n if (childNode.type !== 'Link') {\n return;\n }\n\n validateLink(childNode.props.href, isOnPhishingList);\n });\n}\n\n/**\n * Calculate the total length of all text in the component.\n *\n * @param component - A custom UI component.\n * @returns The total length of all text components in the component.\n */\nexport function getTotalTextLength(component: Component): number {\n const { type } = component;\n\n switch (type) {\n case NodeType.Panel:\n return component.children.reduce<number>(\n // This is a bug in TypeScript: https://github.com/microsoft/TypeScript/issues/48313\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n (sum, node) => sum + getTotalTextLength(node),\n 0,\n );\n\n case NodeType.Row:\n return getTotalTextLength(component.value);\n\n case NodeType.Text:\n return component.value.length;\n\n default:\n return 0;\n }\n}\n\n/**\n * Check if a JSX element has children.\n *\n * @param element - A JSX element.\n * @returns `true` if the element has children, `false` otherwise.\n */\nexport function hasChildren<Element extends JSXElement>(\n element: Element,\n): element is Element & {\n props: { children: Nestable<JSXElement | string> };\n} {\n return hasProperty(element.props, 'children');\n}\n\n/**\n * Filter a JSX child to remove `null`, `undefined`, plain booleans, and empty\n * strings.\n *\n * @param child - The JSX child to filter.\n * @returns `true` if the child is not `null`, `undefined`, a plain boolean, or\n * an empty string, `false` otherwise.\n */\nfunction filterJsxChild(child: JSXElement | string | boolean | null): boolean {\n return Boolean(child) && child !== true;\n}\n\n/**\n * Get the children of a JSX element as an array. If the element has only one\n * child, the child is returned as an array.\n *\n * @param element - A JSX element.\n * @returns The children of the element.\n */\nexport function getJsxChildren(element: JSXElement): (JSXElement | string)[] {\n if (hasChildren(element)) {\n if (Array.isArray(element.props.children)) {\n // @ts-expect-error - Each member of the union type has signatures, but\n // none of those signatures are compatible with each other.\n return element.props.children.filter(filterJsxChild).flat(Infinity);\n }\n\n if (element.props.children) {\n return [element.props.children];\n }\n }\n\n return [];\n}\n\n/**\n * Walk a JSX tree and call a callback on each node.\n *\n * @param node - The JSX node to walk.\n * @param callback - The callback to call on each node.\n * @param depth - The current depth in the JSX tree for a walk.\n * @returns The result of the callback, if any.\n */\nexport function walkJsx<Value>(\n node: JSXElement | JSXElement[],\n callback: (node: JSXElement, depth: number) => Value | undefined,\n depth = 0,\n): Value | undefined {\n if (Array.isArray(node)) {\n for (const child of node) {\n const childResult = walkJsx(child as JSXElement, callback, depth);\n if (childResult !== undefined) {\n return childResult;\n }\n }\n\n return undefined;\n }\n\n const result = callback(node, depth);\n if (result !== undefined) {\n return result;\n }\n\n if (\n hasProperty(node, 'props') &&\n isPlainObject(node.props) &&\n hasProperty(node.props, 'children')\n ) {\n const children = getJsxChildren(node);\n for (const child of children) {\n if (isPlainObject(child)) {\n const childResult = walkJsx(child, callback, depth + 1);\n if (childResult !== undefined) {\n return childResult;\n }\n }\n }\n }\n\n return undefined;\n}\n\n/**\n * Serialise a JSX prop to a string.\n *\n * @param prop - The JSX prop.\n * @returns The serialised JSX prop.\n */\nfunction serialiseProp(prop: unknown): string {\n if (typeof prop === 'string') {\n return `\"${prop}\"`;\n }\n\n return `{${JSON.stringify(prop)}}`;\n}\n\n/**\n * Serialise JSX props to a string.\n *\n * @param props - The JSX props.\n * @returns The serialised JSX props.\n */\nfunction serialiseProps(props: Record<string, unknown>): string {\n return Object.entries(props)\n .filter(([key]) => key !== 'children')\n .sort(([a], [b]) => a.localeCompare(b))\n .map(([key, value]) => ` ${key}=${serialiseProp(value)}`)\n .join('');\n}\n\n/**\n * Serialise a JSX node to a string.\n *\n * @param node - The JSX node.\n * @param indentation - The indentation level. Defaults to `0`. This should not\n * be set by the caller, as it is used for recursion.\n * @returns The serialised JSX node.\n */\nexport function serialiseJsx(node: SnapNode, indentation = 0): string {\n if (Array.isArray(node)) {\n return node.map((child) => serialiseJsx(child, indentation)).join('');\n }\n\n const indent = ' '.repeat(indentation);\n if (typeof node === 'string') {\n return `${indent}${node}\\n`;\n }\n\n if (!node) {\n return '';\n }\n\n const { type, props } = node as GenericSnapElement;\n const trailingNewline = indentation > 0 ? '\\n' : '';\n\n if (hasProperty(props, 'children')) {\n const children = serialiseJsx(props.children as SnapNode, indentation + 1);\n return `${indent}<${type}${serialiseProps(\n props,\n )}>\\n${children}${indent}</${type}>${trailingNewline}`;\n }\n\n return `${indent}<${type}${serialiseProps(props)} />${trailingNewline}`;\n}\n"],"mappings":";AACA,SAAS,gBAAgB;AAazB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,OAAO,kBAAkB;AAuErB;AApEb,IAAM,kBAAkB;AACxB,IAAM,oBAAoB,CAAC,UAAU,SAAS;AAQ9C,SAAS,iBAAiB,SAA+C;AACvE,UAAQ,SAAS;AAAA,IACf,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT;AACE,aAAO;AAAA,EACX;AACF;AASA,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AAI,IAAI,eAAe;AAAA,EACtD;AAEA,SAAO,GAAG,MAAM,IAAI,IAAI,GAAG,eAAe,KAAK,CAAC,MAAM,eAAe;AACvE;","names":[]}
@@ -1,5 +1,4 @@
1
1
  // src/types.ts
2
- import { assertStruct, VersionStruct } from "@metamask/utils";
3
2
  import {
4
3
  instance,
5
4
  is,
@@ -12,7 +11,8 @@ import {
12
11
  type,
13
12
  union,
14
13
  assert as assertSuperstruct
15
- } from "superstruct";
14
+ } from "@metamask/superstruct";
15
+ import { assertStruct, VersionStruct } from "@metamask/utils";
16
16
  var NpmSnapFileNames = /* @__PURE__ */ ((NpmSnapFileNames2) => {
17
17
  NpmSnapFileNames2["PackageJson"] = "package.json";
18
18
  NpmSnapFileNames2["Manifest"] = "snap.manifest.json";
@@ -92,4 +92,4 @@ export {
92
92
  isValidUrl,
93
93
  WALLET_SNAP_PERMISSION_KEY
94
94
  };
95
- //# sourceMappingURL=chunk-T6FWIDA6.mjs.map
95
+ //# sourceMappingURL=chunk-AJNKQYKP.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { Infer, Struct } from '@metamask/superstruct';\nimport {\n instance,\n is,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n type,\n union,\n assert as assertSuperstruct,\n} from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { assertStruct, VersionStruct } from '@metamask/utils';\n\nimport type { SnapCaveatType } from './caveats';\nimport type { SnapFunctionExports, SnapRpcHookArgs } from './handlers';\nimport type { LocalizationFile } from './localization';\nimport type { SnapManifest } from './manifest';\nimport type { VirtualFile } from './virtual-file';\n\nexport enum NpmSnapFileNames {\n PackageJson = 'package.json',\n Manifest = 'snap.manifest.json',\n}\n\nexport const NameStruct = size(\n pattern(\n string(),\n /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$/u,\n ),\n 1,\n 214,\n);\n\n// Note we use `type` instead of `object` here, because the latter does not\n// allow unknown keys.\nexport const NpmSnapPackageJsonStruct = type({\n version: VersionStruct,\n name: NameStruct,\n main: optional(size(string(), 1, Infinity)),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n});\n\nexport type NpmSnapPackageJson = Infer<typeof NpmSnapPackageJsonStruct> &\n Record<string, any>;\n\n/**\n * Check if the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link NpmSnapPackageJson} object.\n */\nexport function isNpmSnapPackageJson(\n value: unknown,\n): value is NpmSnapPackageJson {\n return is(value, NpmSnapPackageJsonStruct);\n}\n\n/**\n * Asserts that the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link NpmSnapPackageJson} object.\n */\nexport function assertIsNpmSnapPackageJson(\n value: unknown,\n): asserts value is NpmSnapPackageJson {\n assertStruct(\n value,\n NpmSnapPackageJsonStruct,\n `\"${NpmSnapFileNames.PackageJson}\" is invalid`,\n );\n}\n\n/**\n * An object for storing parsed but unvalidated Snap file contents.\n */\nexport type UnvalidatedSnapFiles = {\n manifest?: VirtualFile<Json>;\n packageJson?: VirtualFile<Json>;\n sourceCode?: VirtualFile;\n svgIcon?: VirtualFile;\n auxiliaryFiles: VirtualFile[];\n localizationFiles: VirtualFile[];\n};\n\n/**\n * An object for storing the contents of Snap files that have passed JSON\n * Schema validation, or are non-empty if they are strings.\n */\nexport type SnapFiles = {\n manifest: VirtualFile<SnapManifest>;\n packageJson: VirtualFile<NpmSnapPackageJson>;\n sourceCode: VirtualFile;\n svgIcon?: VirtualFile;\n auxiliaryFiles: VirtualFile[];\n localizationFiles: VirtualFile<LocalizationFile>[];\n};\n\n/**\n * A subset of snap files extracted from a fetched snap.\n */\nexport type FetchedSnapFiles = Pick<\n SnapFiles,\n 'manifest' | 'sourceCode' | 'svgIcon' | 'auxiliaryFiles' | 'localizationFiles'\n>;\n\n/**\n * The possible prefixes for snap ids.\n */\n/* eslint-disable @typescript-eslint/naming-convention */\nexport enum SnapIdPrefixes {\n npm = 'npm:',\n local = 'local:',\n}\n/* eslint-enable @typescript-eslint/naming-convention */\n\n/**\n * Snap validation failure reason codes that are programmatically fixable\n * if validation occurs during development.\n */\nexport enum SnapValidationFailureReason {\n NameMismatch = '\"name\" field mismatch',\n VersionMismatch = '\"version\" field mismatch',\n RepositoryMismatch = '\"repository\" field mismatch',\n ShasumMismatch = '\"shasum\" field mismatch',\n}\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport enum SNAP_STREAM_NAMES {\n JSON_RPC = 'jsonRpc',\n COMMAND = 'command',\n}\n/* eslint-enable @typescript-eslint/naming-convention */\n\n// The snap is the callee\nexport type SnapRpcHook = (options: SnapRpcHookArgs) => Promise<unknown>;\n\ntype ObjectParameters<\n Type extends Record<string, (...args: any[]) => unknown>,\n> = Parameters<Type[keyof Type]>;\n\nexport type SnapExportsParameters = ObjectParameters<SnapFunctionExports>;\n\ntype UriOptions<Type extends string> = {\n protocol?: Struct<Type>;\n hash?: Struct<Type>;\n port?: Struct<Type>;\n hostname?: Struct<Type>;\n pathname?: Struct<Type>;\n search?: Struct<Type>;\n};\n\nexport const uri = (opts: UriOptions<any> = {}) =>\n refine(union([string(), instance(URL)]), 'uri', (value) => {\n try {\n const url = new URL(value);\n\n const UrlStruct = type(opts);\n assertSuperstruct(url, UrlStruct);\n return true;\n } catch {\n return `Expected URL, got \"${value.toString()}\".`;\n }\n });\n\n/**\n * Returns whether a given value is a valid URL.\n *\n * @param url - The value to check.\n * @param opts - Optional constraints for url checking.\n * @returns Whether `url` is valid URL or not.\n */\nexport function isValidUrl(\n url: unknown,\n opts: UriOptions<any> = {},\n): url is string | URL {\n return is(url, uri(opts));\n}\n\n// redefining here to avoid circular dependency\nexport const WALLET_SNAP_PERMISSION_KEY = 'wallet_snap';\n\nexport type SnapsPermissionRequest = {\n [WALLET_SNAP_PERMISSION_KEY]: {\n caveats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AA,mBAAA,cAAW;AACX,EAAAA,mBAAA,aAAU;AAFA,SAAAA;AAAA,GAAA;AAwBL,IAAM,MAAM,CAAC,OAAwB,CAAC,MAC3C,OAAO,MAAM,CAAC,OAAO,GAAG,SAAS,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,UAAU;AACzD,MAAI;AACF,UAAM,MAAM,IAAI,IAAI,KAAK;AAEzB,UAAM,YAAY,KAAK,IAAI;AAC3B,sBAAkB,KAAK,SAAS;AAChC,WAAO;AAAA,EACT,QAAQ;AACN,WAAO,sBAAsB,MAAM,SAAS,CAAC;AAAA,EAC/C;AACF,CAAC;AASI,SAAS,WACd,KACA,OAAwB,CAAC,GACJ;AACrB,SAAO,GAAG,KAAK,IAAI,IAAI,CAAC;AAC1B;AAGO,IAAM,6BAA6B;","names":["NpmSnapFileNames","SnapIdPrefixes","SnapValidationFailureReason","SNAP_STREAM_NAMES"]}
@@ -1,12 +1,12 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunk473MIETWjs = require('./chunk-473MIETW.js');
3
+ var _chunkUAINMOUUjs = require('./chunk-UAINMOUU.js');
4
4
 
5
5
  // src/checksum.ts
6
6
  var _utils = require('@metamask/utils');
7
7
  var _sha256 = require('@noble/hashes/sha256');
8
8
  async function checksum(bytes) {
9
- const value = _chunk473MIETWjs.getBytes.call(void 0, bytes);
9
+ const value = _chunkUAINMOUUjs.getBytes.call(void 0, bytes);
10
10
  if ("crypto" in globalThis && typeof globalThis.crypto === "object" && crypto.subtle?.digest) {
11
11
  return new Uint8Array(await crypto.subtle.digest("SHA-256", value));
12
12
  }
@@ -29,4 +29,4 @@ async function checksumFiles(files) {
29
29
 
30
30
 
31
31
  exports.checksum = checksum; exports.checksumFiles = checksumFiles;
32
- //# sourceMappingURL=chunk-MLOQHUOY.js.map
32
+ //# sourceMappingURL=chunk-AMW2VJ3N.js.map
@@ -1,11 +1,18 @@
1
1
  // src/json-rpc.ts
2
2
  import { SubjectType } from "@metamask/permission-controller";
3
+ import {
4
+ array,
5
+ boolean,
6
+ object,
7
+ optional,
8
+ refine,
9
+ string
10
+ } from "@metamask/superstruct";
3
11
  import {
4
12
  assertStruct,
5
13
  isJsonRpcFailure,
6
14
  isJsonRpcSuccess
7
15
  } from "@metamask/utils";
8
- import { array, boolean, object, optional, refine, string } from "superstruct";
9
16
  var AllowedOriginsStruct = array(
10
17
  refine(string(), "Allowed origin", (value) => {
11
18
  const wildcards = value.split("*").length - 1;
@@ -53,8 +60,8 @@ function assertIsKeyringOrigins(value, ErrorWrapper) {
53
60
  }
54
61
  function createOriginRegExp(matcher) {
55
62
  const escaped = matcher.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
56
- const regex = escaped.replace(/\*/gu, ".*");
57
- return RegExp(regex, "u");
63
+ const regex = escaped.replace(/\\\*/gu, ".*");
64
+ return RegExp(`${regex}$`, "u");
58
65
  }
59
66
  function checkAllowedOrigin(matcher, origin) {
60
67
  if (matcher === "*" || matcher === origin) {
@@ -94,4 +101,4 @@ export {
94
101
  isOriginAllowed,
95
102
  assertIsJsonRpcSuccess
96
103
  };
97
- //# sourceMappingURL=chunk-K2OTEZZZ.mjs.map
104
+ //# sourceMappingURL=chunk-ANYNWSCA.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/json-rpc.ts"],"sourcesContent":["import { SubjectType } from '@metamask/permission-controller';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n array,\n boolean,\n object,\n optional,\n refine,\n string,\n} from '@metamask/superstruct';\nimport type {\n AssertionErrorConstructor,\n Json,\n JsonRpcSuccess,\n} from '@metamask/utils';\nimport {\n assertStruct,\n isJsonRpcFailure,\n isJsonRpcSuccess,\n} from '@metamask/utils';\n\nconst AllowedOriginsStruct = array(\n refine(string(), 'Allowed origin', (value) => {\n const wildcards = value.split('*').length - 1;\n if (wildcards > 2) {\n return 'No more than two wildcards (\"*\") are allowed in an origin specifier.';\n }\n\n return true;\n }),\n);\n\nexport const RpcOriginsStruct = refine(\n object({\n dapps: optional(boolean()),\n snaps: optional(boolean()),\n allowedOrigins: optional(AllowedOriginsStruct),\n }),\n 'RPC origins',\n (value) => {\n const hasOrigins = Boolean(\n value.snaps === true ||\n value.dapps === true ||\n (value.allowedOrigins && value.allowedOrigins.length > 0),\n );\n\n if (hasOrigins) {\n return true;\n }\n\n return 'Must specify at least one JSON-RPC origin.';\n },\n);\n\nexport type RpcOrigins = Infer<typeof RpcOriginsStruct>;\n\n/**\n * Asserts that the given value is a valid {@link RpcOrigins} object.\n *\n * @param value - The value to assert.\n * @param ErrorWrapper - An optional error wrapper to use. Defaults to\n * {@link AssertionError}.\n * @throws If the value is not a valid {@link RpcOrigins} object.\n */\nexport function assertIsRpcOrigins(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is RpcOrigins {\n assertStruct(\n value,\n RpcOriginsStruct,\n 'Invalid JSON-RPC origins',\n ErrorWrapper,\n );\n}\n\nexport const KeyringOriginsStruct = object({\n allowedOrigins: optional(AllowedOriginsStruct),\n});\n\nexport type KeyringOrigins = Infer<typeof KeyringOriginsStruct>;\n\n/**\n * Assert that the given value is a valid {@link KeyringOrigins} object.\n *\n * @param value - The value to assert.\n * @param ErrorWrapper - An optional error wrapper to use. Defaults to\n * {@link AssertionError}.\n * @throws If the value is not a valid {@link KeyringOrigins} object.\n */\nexport function assertIsKeyringOrigins(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is KeyringOrigins {\n assertStruct(\n value,\n KeyringOriginsStruct,\n 'Invalid keyring origins',\n ErrorWrapper,\n );\n}\n\n/**\n * Create regular expression for matching against an origin while allowing wildcards.\n *\n * The \"*\" symbol is treated as a wildcard and will match 0 or more characters.\n *\n * @param matcher - The string to create the regular expression with.\n * @returns The regular expression.\n */\nfunction createOriginRegExp(matcher: string) {\n // Escape potential Regex characters\n const escaped = matcher.replace(/[.*+?^${}()|[\\]\\\\]/gu, '\\\\$&');\n // Support wildcards\n const regex = escaped.replace(/\\\\\\*/gu, '.*');\n return RegExp(`${regex}$`, 'u');\n}\n\n/**\n * Check whether an origin is allowed or not using a matcher string.\n *\n * The matcher string may be a specific origin to match or include wildcards.\n * The \"*\" symbol is treated as a wildcard and will match 0 or more characters.\n * Note: this means that https://*metamask.io matches both https://metamask.io\n * and https://snaps.metamask.io.\n *\n * @param matcher - The matcher string.\n * @param origin - The origin.\n * @returns Whether the origin is allowed.\n */\nfunction checkAllowedOrigin(matcher: string, origin: string) {\n // If the matcher is a single wildcard or identical to the origin we can return true immediately.\n if (matcher === '*' || matcher === origin) {\n return true;\n }\n\n const regex = createOriginRegExp(matcher);\n return regex.test(origin);\n}\n\n/**\n * Check if the given origin is allowed by the given JSON-RPC origins object.\n *\n * @param origins - The JSON-RPC origins object.\n * @param subjectType - The type of the origin.\n * @param origin - The origin to check.\n * @returns Whether the origin is allowed.\n */\nexport function isOriginAllowed(\n origins: RpcOrigins,\n subjectType: SubjectType,\n origin: string,\n) {\n // The MetaMask client is always allowed.\n if (origin === 'metamask') {\n return true;\n }\n\n // If the origin is in the `allowedOrigins` list, it is allowed.\n if (\n origins.allowedOrigins?.some((matcher) =>\n checkAllowedOrigin(matcher, origin),\n )\n ) {\n return true;\n }\n\n // If the origin is a website and `dapps` is true, it is allowed.\n if (subjectType === SubjectType.Website && origins.dapps) {\n return true;\n }\n\n // If the origin is a snap and `snaps` is true, it is allowed.\n return Boolean(subjectType === SubjectType.Snap && origins.snaps);\n}\n\n/**\n * Assert that the given value is a successful JSON-RPC response. If the value\n * is not a success response, an error is thrown. If the value is an JSON-RPC\n * error, the error message is included in the thrown error.\n *\n * @param value - The value to check.\n * @throws If the value is not a JSON-RPC success response.\n */\nexport function assertIsJsonRpcSuccess(\n value: unknown,\n): asserts value is JsonRpcSuccess<Json> {\n if (!isJsonRpcSuccess(value)) {\n if (isJsonRpcFailure(value)) {\n throw new Error(`JSON-RPC request failed: ${value.error.message}`);\n }\n\n throw new Error('Invalid JSON-RPC response.');\n }\n}\n"],"mappings":";AAAA,SAAS,mBAAmB;AAE5B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAMP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,IAAM,uBAAuB;AAAA,EAC3B,OAAO,OAAO,GAAG,kBAAkB,CAAC,UAAU;AAC5C,UAAM,YAAY,MAAM,MAAM,GAAG,EAAE,SAAS;AAC5C,QAAI,YAAY,GAAG;AACjB,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,EACT,CAAC;AACH;AAEO,IAAM,mBAAmB;AAAA,EAC9B,OAAO;AAAA,IACL,OAAO,SAAS,QAAQ,CAAC;AAAA,IACzB,OAAO,SAAS,QAAQ,CAAC;AAAA,IACzB,gBAAgB,SAAS,oBAAoB;AAAA,EAC/C,CAAC;AAAA,EACD;AAAA,EACA,CAAC,UAAU;AACT,UAAM,aAAa;AAAA,MACjB,MAAM,UAAU,QACd,MAAM,UAAU,QACf,MAAM,kBAAkB,MAAM,eAAe,SAAS;AAAA,IAC3D;AAEA,QAAI,YAAY;AACd,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,EACT;AACF;AAYO,SAAS,mBACd,OAEA,cAC6B;AAC7B;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,IAAM,uBAAuB,OAAO;AAAA,EACzC,gBAAgB,SAAS,oBAAoB;AAC/C,CAAC;AAYM,SAAS,uBACd,OAEA,cACiC;AACjC;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAUA,SAAS,mBAAmB,SAAiB;AAE3C,QAAM,UAAU,QAAQ,QAAQ,wBAAwB,MAAM;AAE9D,QAAM,QAAQ,QAAQ,QAAQ,UAAU,IAAI;AAC5C,SAAO,OAAO,GAAG,KAAK,KAAK,GAAG;AAChC;AAcA,SAAS,mBAAmB,SAAiB,QAAgB;AAE3D,MAAI,YAAY,OAAO,YAAY,QAAQ;AACzC,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,mBAAmB,OAAO;AACxC,SAAO,MAAM,KAAK,MAAM;AAC1B;AAUO,SAAS,gBACd,SACA,aACA,QACA;AAEA,MAAI,WAAW,YAAY;AACzB,WAAO;AAAA,EACT;AAGA,MACE,QAAQ,gBAAgB;AAAA,IAAK,CAAC,YAC5B,mBAAmB,SAAS,MAAM;AAAA,EACpC,GACA;AACA,WAAO;AAAA,EACT;AAGA,MAAI,gBAAgB,YAAY,WAAW,QAAQ,OAAO;AACxD,WAAO;AAAA,EACT;AAGA,SAAO,QAAQ,gBAAgB,YAAY,QAAQ,QAAQ,KAAK;AAClE;AAUO,SAAS,uBACd,OACuC;AACvC,MAAI,CAAC,iBAAiB,KAAK,GAAG;AAC5B,QAAI,iBAAiB,KAAK,GAAG;AAC3B,YAAM,IAAI,MAAM,4BAA4B,MAAM,MAAM,OAAO,EAAE;AAAA,IACnE;AAEA,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAC9C;AACF;","names":[]}
@@ -11,7 +11,7 @@ var _snapssdk = require('@metamask/snaps-sdk');
11
11
 
12
12
 
13
13
 
14
- var _superstruct = require('superstruct');
14
+ var _superstruct = require('@metamask/superstruct');
15
15
  var LOCALIZABLE_FIELDS = ["description", "proposedName"];
16
16
  var LocalizationFileStruct = _superstruct.object.call(void 0, {
17
17
  locale: _superstruct.string.call(void 0, ),
@@ -106,4 +106,4 @@ function validateSnapManifestLocalizations(snapManifest, localizationFiles) {
106
106
 
107
107
 
108
108
  exports.LOCALIZABLE_FIELDS = LOCALIZABLE_FIELDS; exports.LocalizationFileStruct = LocalizationFileStruct; exports.getValidatedLocalizationFiles = getValidatedLocalizationFiles; exports.getLocalizationFile = getLocalizationFile; exports.TRANSLATION_REGEX = TRANSLATION_REGEX; exports.translate = translate; exports.getLocalizedSnapManifest = getLocalizedSnapManifest; exports.validateSnapManifestLocalizations = validateSnapManifestLocalizations;
109
- //# sourceMappingURL=chunk-R5DO7T2D.js.map
109
+ //# sourceMappingURL=chunk-B5GNTDE2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/localization.ts"],"names":[],"mappings":";;;;;AAAA,SAAS,uBAAuB;AAEhC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAMA,IAAM,qBAAqB,CAAC,eAAe,cAAc;AAEzD,IAAM,yBAAyB,OAAO;AAAA,EAC3C,QAAQ,OAAO;AAAA,EACf,UAAU;AAAA,IACR,OAAO;AAAA,IACP,OAAO;AAAA,MACL,SAAS,OAAO;AAAA,MAChB,aAAa,SAAS,OAAO,CAAC;AAAA,IAChC,CAAC;AAAA,EACH;AACF,CAAC;AAWM,SAAS,8BACd,mBACiC;AACjC,aAAW,QAAQ,mBAAmB;AACpC,QAAI;AACF,WAAK,SAAS,OAAO,UAAU,KAAK,SAAS,CAAC,GAAG,sBAAsB;AAAA,IACzE,SAAS,OAAO;AACd,UAAI,iBAAiB,aAAa;AAChC,cAAM,IAAI;AAAA,UACR,yCAAyC,KAAK,IAAI,MAAM,MAAM,OAAO;AAAA,QACvE;AAAA,MACF;AAEA,UAAI,iBAAiB,aAAa;AAChC,cAAM,IAAI;AAAA,UACR,sCAAsC,KAAK,IAAI;AAAA,QACjD;AAAA,MACF;AAEA,YAAM;AAAA,IACR;AAAA,EACF;AAEA,SAAO;AACT;AAWO,SAAS,oBACd,QACA,mBACA;AACA,QAAM,OAAO,kBAAkB;AAAA,IAC7B,CAAC,qBAAqB,iBAAiB,WAAW;AAAA,EACpD;AAEA,MAAI,CAAC,MAAM;AACT,WAAO,kBAAkB;AAAA,MACvB,CAAC,qBAAqB,iBAAiB,WAAW;AAAA,IACpD;AAAA,EACF;AAEA,SAAO;AACT;AAEO,IAAM,oBAAoB;AAa1B,SAAS,UAAU,OAAe,MAAoC;AAC3E,QAAM,UAAU,MAAM,SAAS,iBAAiB;AAChD,QAAM,QAAQ,MAAM,KAAK,OAAO;AAEhC,SAAO,MAAM,OAAe,CAAC,QAAQ,CAAC,OAAO,GAAG,MAAM;AACpD,QAAI,CAAC,MAAM;AACT,YAAM,IAAI;AAAA,QACR,wBAAwB,KAAK;AAAA,MAC/B;AAAA,IACF;AAEA,UAAM,cAAc,KAAK,SAAS,IAAI,KAAK,CAAC;AAC5C,QAAI,CAAC,aAAa;AAChB,YAAM,IAAI;AAAA,QACR,wBAAwB,KAAK,gCAAgC,IAAI,KAAK,CAAC,SACrE,KAAK,MACP;AAAA,MACF;AAAA,IACF;AAEA,WAAO,OAAO,QAAQ,OAAO,YAAY,OAAO;AAAA,EAClD,GAAG,KAAK;AACV;AAWO,SAAS,yBACd,cACA,QACA,mBACA;AACA,QAAM,OAAO,oBAAoB,QAAQ,iBAAiB;AAE1D,SAAO,mBAAmB,OAAO,CAAC,UAAU,UAAU;AACpD,UAAM,cAAc,UAAU,SAAS,KAAK,GAAG,IAAI;AACnD,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,KAAK,GAAG;AAAA,IACX;AAAA,EACF,GAAG,YAAY;AACjB;AASO,SAAS,kCACd,cACA,mBACA;AACA,MAAI;AAGF,sBACG,OAAO,CAAC,SAAS,KAAK,WAAW,IAAI,EACrC,QAAQ,CAAC,SAAS;AACjB,+BAAyB,cAAc,KAAK,QAAQ,iBAAiB;AAAA,IACvE,CAAC;AAGH,6BAAyB,cAAc,MAAM,iBAAiB;AAAA,EAChE,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR,qCAAqC,gBAAgB,KAAK,CAAC;AAAA,IAC7D;AAAA,EACF;AACF","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n create,\n object,\n optional,\n record,\n string,\n StructError,\n} from '@metamask/superstruct';\n\nimport { parseJson } from './json';\nimport type { SnapManifest } from './manifest';\nimport type { VirtualFile } from './virtual-file';\n\nexport const LOCALIZABLE_FIELDS = ['description', 'proposedName'] as const;\n\nexport const LocalizationFileStruct = object({\n locale: string(),\n messages: record(\n string(),\n object({\n message: string(),\n description: optional(string()),\n }),\n ),\n});\n\nexport type LocalizationFile = Infer<typeof LocalizationFileStruct>;\n\n/**\n * Validate a list of localization files.\n *\n * @param localizationFiles - The localization files to validate.\n * @returns The validated localization files.\n * @throws If any of the files are considered invalid.\n */\nexport function getValidatedLocalizationFiles(\n localizationFiles: VirtualFile[],\n): VirtualFile<LocalizationFile>[] {\n for (const file of localizationFiles) {\n try {\n file.result = create(parseJson(file.toString()), LocalizationFileStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Failed to validate localization file \"${file.path}\": ${error.message}.`,\n );\n }\n\n if (error instanceof SyntaxError) {\n throw new Error(\n `Failed to parse localization file \"${file.path}\" as JSON.`,\n );\n }\n\n throw error;\n }\n }\n\n return localizationFiles as VirtualFile<LocalizationFile>[];\n}\n\n/**\n * Get the localization file for a given locale. If the locale is not found,\n * the English localization file will be returned.\n *\n * @param locale - The locale to use.\n * @param localizationFiles - The localization files to use.\n * @returns The localization file, or `undefined` if no localization file was\n * found.\n */\nexport function getLocalizationFile(\n locale: string,\n localizationFiles: LocalizationFile[],\n) {\n const file = localizationFiles.find(\n (localizationFile) => localizationFile.locale === locale,\n );\n\n if (!file) {\n return localizationFiles.find(\n (localizationFile) => localizationFile.locale === 'en',\n );\n }\n\n return file;\n}\n\nexport const TRANSLATION_REGEX = /\\{\\{\\s?([a-zA-Z0-9-_\\s]+)\\s?\\}\\}/gu;\n\n/**\n * Translate a string using a localization file. This will replace all instances\n * of `{{key}}` with the localized version of `key`.\n *\n * @param value - The string to translate.\n * @param file - The localization file to use, or `undefined` if no localization\n * file was found.\n * @returns The translated string.\n * @throws If the string contains a key that is not present in the localization\n * file, or if no localization file was found.\n */\nexport function translate(value: string, file: LocalizationFile | undefined) {\n const matches = value.matchAll(TRANSLATION_REGEX);\n const array = Array.from(matches);\n\n return array.reduce<string>((result, [match, key]) => {\n if (!file) {\n throw new Error(\n `Failed to translate \"${value}\": No localization file found.`,\n );\n }\n\n const translation = file.messages[key.trim()];\n if (!translation) {\n throw new Error(\n `Failed to translate \"${value}\": No translation found for \"${key.trim()}\" in \"${\n file.locale\n }\" file.`,\n );\n }\n\n return result.replace(match, translation.message);\n }, value);\n}\n\n/**\n * Get the localized Snap manifest for a given locale. This will replace all\n * localized strings in the manifest with the localized version.\n *\n * @param snapManifest - The Snap manifest to localize.\n * @param locale - The locale to use.\n * @param localizationFiles - The localization files to use.\n * @returns The localized Snap manifest.\n */\nexport function getLocalizedSnapManifest(\n snapManifest: SnapManifest,\n locale: string,\n localizationFiles: LocalizationFile[],\n) {\n const file = getLocalizationFile(locale, localizationFiles);\n\n return LOCALIZABLE_FIELDS.reduce((manifest, field) => {\n const translation = translate(manifest[field], file);\n return {\n ...manifest,\n [field]: translation,\n };\n }, snapManifest);\n}\n\n/**\n * Validate the localization files for a Snap manifest.\n *\n * @param snapManifest - The Snap manifest to validate.\n * @param localizationFiles - The localization files to validate.\n * @throws If the manifest cannot be localized.\n */\nexport function validateSnapManifestLocalizations(\n snapManifest: SnapManifest,\n localizationFiles: LocalizationFile[],\n) {\n try {\n // `translate` throws if the manifest cannot be localized, so we just attempt\n // to translate the manifest using all localization files.\n localizationFiles\n .filter((file) => file.locale !== 'en')\n .forEach((file) => {\n getLocalizedSnapManifest(snapManifest, file.locale, localizationFiles);\n });\n\n // The manifest must be localizable in English.\n getLocalizedSnapManifest(snapManifest, 'en', localizationFiles);\n } catch (error) {\n throw new Error(\n `Failed to localize Snap manifest: ${getErrorMessage(error)}`,\n );\n }\n}\n"]}
@@ -11,7 +11,7 @@ import {
11
11
  record,
12
12
  string,
13
13
  StructError
14
- } from "superstruct";
14
+ } from "@metamask/superstruct";
15
15
  var LOCALIZABLE_FIELDS = ["description", "proposedName"];
16
16
  var LocalizationFileStruct = object({
17
17
  locale: string(),
@@ -106,4 +106,4 @@ export {
106
106
  getLocalizedSnapManifest,
107
107
  validateSnapManifestLocalizations
108
108
  };
109
- //# sourceMappingURL=chunk-WZ457PEQ.mjs.map
109
+ //# sourceMappingURL=chunk-CJK7DDV2.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/localization.ts"],"sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n create,\n object,\n optional,\n record,\n string,\n StructError,\n} from '@metamask/superstruct';\n\nimport { parseJson } from './json';\nimport type { SnapManifest } from './manifest';\nimport type { VirtualFile } from './virtual-file';\n\nexport const LOCALIZABLE_FIELDS = ['description', 'proposedName'] as const;\n\nexport const LocalizationFileStruct = object({\n locale: string(),\n messages: record(\n string(),\n object({\n message: string(),\n description: optional(string()),\n }),\n ),\n});\n\nexport type LocalizationFile = Infer<typeof LocalizationFileStruct>;\n\n/**\n * Validate a list of localization files.\n *\n * @param localizationFiles - The localization files to validate.\n * @returns The validated localization files.\n * @throws If any of the files are considered invalid.\n */\nexport function getValidatedLocalizationFiles(\n localizationFiles: VirtualFile[],\n): VirtualFile<LocalizationFile>[] {\n for (const file of localizationFiles) {\n try {\n file.result = create(parseJson(file.toString()), LocalizationFileStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Failed to validate localization file \"${file.path}\": ${error.message}.`,\n );\n }\n\n if (error instanceof SyntaxError) {\n throw new Error(\n `Failed to parse localization file \"${file.path}\" as JSON.`,\n );\n }\n\n throw error;\n }\n }\n\n return localizationFiles as VirtualFile<LocalizationFile>[];\n}\n\n/**\n * Get the localization file for a given locale. If the locale is not found,\n * the English localization file will be returned.\n *\n * @param locale - The locale to use.\n * @param localizationFiles - The localization files to use.\n * @returns The localization file, or `undefined` if no localization file was\n * found.\n */\nexport function getLocalizationFile(\n locale: string,\n localizationFiles: LocalizationFile[],\n) {\n const file = localizationFiles.find(\n (localizationFile) => localizationFile.locale === locale,\n );\n\n if (!file) {\n return localizationFiles.find(\n (localizationFile) => localizationFile.locale === 'en',\n );\n }\n\n return file;\n}\n\nexport const TRANSLATION_REGEX = /\\{\\{\\s?([a-zA-Z0-9-_\\s]+)\\s?\\}\\}/gu;\n\n/**\n * Translate a string using a localization file. This will replace all instances\n * of `{{key}}` with the localized version of `key`.\n *\n * @param value - The string to translate.\n * @param file - The localization file to use, or `undefined` if no localization\n * file was found.\n * @returns The translated string.\n * @throws If the string contains a key that is not present in the localization\n * file, or if no localization file was found.\n */\nexport function translate(value: string, file: LocalizationFile | undefined) {\n const matches = value.matchAll(TRANSLATION_REGEX);\n const array = Array.from(matches);\n\n return array.reduce<string>((result, [match, key]) => {\n if (!file) {\n throw new Error(\n `Failed to translate \"${value}\": No localization file found.`,\n );\n }\n\n const translation = file.messages[key.trim()];\n if (!translation) {\n throw new Error(\n `Failed to translate \"${value}\": No translation found for \"${key.trim()}\" in \"${\n file.locale\n }\" file.`,\n );\n }\n\n return result.replace(match, translation.message);\n }, value);\n}\n\n/**\n * Get the localized Snap manifest for a given locale. This will replace all\n * localized strings in the manifest with the localized version.\n *\n * @param snapManifest - The Snap manifest to localize.\n * @param locale - The locale to use.\n * @param localizationFiles - The localization files to use.\n * @returns The localized Snap manifest.\n */\nexport function getLocalizedSnapManifest(\n snapManifest: SnapManifest,\n locale: string,\n localizationFiles: LocalizationFile[],\n) {\n const file = getLocalizationFile(locale, localizationFiles);\n\n return LOCALIZABLE_FIELDS.reduce((manifest, field) => {\n const translation = translate(manifest[field], file);\n return {\n ...manifest,\n [field]: translation,\n };\n }, snapManifest);\n}\n\n/**\n * Validate the localization files for a Snap manifest.\n *\n * @param snapManifest - The Snap manifest to validate.\n * @param localizationFiles - The localization files to validate.\n * @throws If the manifest cannot be localized.\n */\nexport function validateSnapManifestLocalizations(\n snapManifest: SnapManifest,\n localizationFiles: LocalizationFile[],\n) {\n try {\n // `translate` throws if the manifest cannot be localized, so we just attempt\n // to translate the manifest using all localization files.\n localizationFiles\n .filter((file) => file.locale !== 'en')\n .forEach((file) => {\n getLocalizedSnapManifest(snapManifest, file.locale, localizationFiles);\n });\n\n // The manifest must be localizable in English.\n getLocalizedSnapManifest(snapManifest, 'en', localizationFiles);\n } catch (error) {\n throw new Error(\n `Failed to localize Snap manifest: ${getErrorMessage(error)}`,\n );\n }\n}\n"],"mappings":";;;;;AAAA,SAAS,uBAAuB;AAEhC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAMA,IAAM,qBAAqB,CAAC,eAAe,cAAc;AAEzD,IAAM,yBAAyB,OAAO;AAAA,EAC3C,QAAQ,OAAO;AAAA,EACf,UAAU;AAAA,IACR,OAAO;AAAA,IACP,OAAO;AAAA,MACL,SAAS,OAAO;AAAA,MAChB,aAAa,SAAS,OAAO,CAAC;AAAA,IAChC,CAAC;AAAA,EACH;AACF,CAAC;AAWM,SAAS,8BACd,mBACiC;AACjC,aAAW,QAAQ,mBAAmB;AACpC,QAAI;AACF,WAAK,SAAS,OAAO,UAAU,KAAK,SAAS,CAAC,GAAG,sBAAsB;AAAA,IACzE,SAAS,OAAO;AACd,UAAI,iBAAiB,aAAa;AAChC,cAAM,IAAI;AAAA,UACR,yCAAyC,KAAK,IAAI,MAAM,MAAM,OAAO;AAAA,QACvE;AAAA,MACF;AAEA,UAAI,iBAAiB,aAAa;AAChC,cAAM,IAAI;AAAA,UACR,sCAAsC,KAAK,IAAI;AAAA,QACjD;AAAA,MACF;AAEA,YAAM;AAAA,IACR;AAAA,EACF;AAEA,SAAO;AACT;AAWO,SAAS,oBACd,QACA,mBACA;AACA,QAAM,OAAO,kBAAkB;AAAA,IAC7B,CAAC,qBAAqB,iBAAiB,WAAW;AAAA,EACpD;AAEA,MAAI,CAAC,MAAM;AACT,WAAO,kBAAkB;AAAA,MACvB,CAAC,qBAAqB,iBAAiB,WAAW;AAAA,IACpD;AAAA,EACF;AAEA,SAAO;AACT;AAEO,IAAM,oBAAoB;AAa1B,SAAS,UAAU,OAAe,MAAoC;AAC3E,QAAM,UAAU,MAAM,SAAS,iBAAiB;AAChD,QAAM,QAAQ,MAAM,KAAK,OAAO;AAEhC,SAAO,MAAM,OAAe,CAAC,QAAQ,CAAC,OAAO,GAAG,MAAM;AACpD,QAAI,CAAC,MAAM;AACT,YAAM,IAAI;AAAA,QACR,wBAAwB,KAAK;AAAA,MAC/B;AAAA,IACF;AAEA,UAAM,cAAc,KAAK,SAAS,IAAI,KAAK,CAAC;AAC5C,QAAI,CAAC,aAAa;AAChB,YAAM,IAAI;AAAA,QACR,wBAAwB,KAAK,gCAAgC,IAAI,KAAK,CAAC,SACrE,KAAK,MACP;AAAA,MACF;AAAA,IACF;AAEA,WAAO,OAAO,QAAQ,OAAO,YAAY,OAAO;AAAA,EAClD,GAAG,KAAK;AACV;AAWO,SAAS,yBACd,cACA,QACA,mBACA;AACA,QAAM,OAAO,oBAAoB,QAAQ,iBAAiB;AAE1D,SAAO,mBAAmB,OAAO,CAAC,UAAU,UAAU;AACpD,UAAM,cAAc,UAAU,SAAS,KAAK,GAAG,IAAI;AACnD,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,KAAK,GAAG;AAAA,IACX;AAAA,EACF,GAAG,YAAY;AACjB;AASO,SAAS,kCACd,cACA,mBACA;AACA,MAAI;AAGF,sBACG,OAAO,CAAC,SAAS,KAAK,WAAW,IAAI,EACrC,QAAQ,CAAC,SAAS;AACjB,+BAAyB,cAAc,KAAK,QAAQ,iBAAiB;AAAA,IACvE,CAAC;AAGH,6BAAyB,cAAc,MAAM,iBAAiB;AAAA,EAChE,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR,qCAAqC,gBAAgB,KAAK,CAAC;AAAA,IAC7D;AAAA,EACF;AACF;","names":[]}
@@ -18,6 +18,9 @@ var VirtualFile = class _VirtualFile {
18
18
  this.data = options?.data ?? {};
19
19
  this.path = options?.path ?? "/";
20
20
  }
21
+ get size() {
22
+ return typeof this.value === "string" ? this.value.length : this.value.byteLength;
23
+ }
21
24
  toString(encoding) {
22
25
  if (typeof this.value === "string") {
23
26
  assert(encoding === void 0, "Tried to encode string.");
@@ -47,4 +50,4 @@ var VirtualFile = class _VirtualFile {
47
50
  export {
48
51
  VirtualFile
49
52
  };
50
- //# sourceMappingURL=chunk-ZJRWU4AJ.mjs.map
53
+ //# sourceMappingURL=chunk-DE22V5AO.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/virtual-file/VirtualFile.ts"],"sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert, bytesToHex } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-interface\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n } else if (this.value instanceof Uint8Array && encoding === 'hex') {\n return bytesToHex(this.value);\n } else if (this.value instanceof Uint8Array && encoding === 'base64') {\n // For large files, this is quite slow, instead use `encodeBase64()`\n // TODO: Use @metamask/utils for this\n return base64.encode(this.value);\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path = this.path;\n return vfile;\n }\n}\n"],"mappings":";;;;;AAQA,SAAS,QAAQ,kBAAkB;AACnC,SAAS,cAAc;AAiChB,IAAM,cAAN,MAAM,aAA8B;AAAA,EACzC,YAAY,OAA4B;AACtC,QAAI;AACJ,QAAI,OAAO,UAAU,YAAY,iBAAiB,YAAY;AAC5D,gBAAU,EAAE,MAAM;AAAA,IACpB,OAAO;AACL,gBAAU;AAAA,IACZ;AAEA,SAAK,QAAQ,SAAS,SAAS;AAU/B,SAAK,SAAS,SAAS,UAAW;AAClC,SAAK,OAAO,SAAS,QAAQ,CAAC;AAC9B,SAAK,OAAO,SAAS,QAAQ;AAAA,EAC/B;AAAA,EAUA,SAAS,UAAmB;AAC1B,QAAI,OAAO,KAAK,UAAU,UAAU;AAClC,aAAO,aAAa,QAAW,yBAAyB;AACxD,aAAO,KAAK;AAAA,IACd,WAAW,KAAK,iBAAiB,cAAc,aAAa,OAAO;AACjE,aAAO,WAAW,KAAK,KAAK;AAAA,IAC9B,WAAW,KAAK,iBAAiB,cAAc,aAAa,UAAU;AAGpE,aAAO,OAAO,OAAO,KAAK,KAAK;AAAA,IACjC;AACA,UAAM,UAAU,IAAI,YAAY,QAAQ;AACxC,WAAO,QAAQ,OAAO,KAAK,KAAK;AAAA,EAClC;AAAA,EAEA,QAAQ;AACN,UAAM,QAAQ,IAAI,aAAoB;AACtC,QAAI,OAAO,KAAK,UAAU,UAAU;AAClC,YAAM,QAAQ,KAAK;AAAA,IACrB,OAAO;AAEL,YAAM,QAAQ,KAAK,MAAM,MAAM,CAAC;AAAA,IAClC;AACA,UAAM,SAAS,UAAU,KAAK,MAAM;AACpC,UAAM,OAAO,UAAU,KAAK,IAAI;AAChC,UAAM,OAAO,KAAK;AAClB,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/virtual-file/VirtualFile.ts"],"sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert, bytesToHex } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-interface\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n get size() {\n return typeof this.value === 'string'\n ? this.value.length\n : this.value.byteLength;\n }\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n } else if (this.value instanceof Uint8Array && encoding === 'hex') {\n return bytesToHex(this.value);\n } else if (this.value instanceof Uint8Array && encoding === 'base64') {\n // For large files, this is quite slow, instead use `encodeBase64()`\n // TODO: Use @metamask/utils for this\n return base64.encode(this.value);\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path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ualFile = class _VirtualFile {
18
18
  this.data = options?.data ?? {};
19
19
  this.path = options?.path ?? "/";
20
20
  }
21
+ get size() {
22
+ return typeof this.value === "string" ? this.value.length : this.value.byteLength;
23
+ }
21
24
  toString(encoding) {
22
25
  if (typeof this.value === "string") {
23
26
  _utils.assert.call(void 0, encoding === void 0, "Tried to encode string.");
@@ -47,4 +50,4 @@ var VirtualFile = class _VirtualFile {
47
50
 
48
51
 
49
52
  exports.VirtualFile = VirtualFile;
50
- //# sourceMappingURL=chunk-HJYRGKCX.js.map
53
+ //# sourceMappingURL=chunk-DEQUNIME.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/virtual-file/VirtualFile.ts"],"names":[],"mappings":";;;;;AAQA,SAAS,QAAQ,kBAAkB;AACnC,SAAS,cAAc;AAiChB,IAAM,cAAN,MAAM,aAA8B;AAAA,EACzC,YAAY,OAA4B;AACtC,QAAI;AACJ,QAAI,OAAO,UAAU,YAAY,iBAAiB,YAAY;AAC5D,gBAAU,EAAE,MAAM;AAAA,IACpB,OAAO;AACL,gBAAU;AAAA,IACZ;AAEA,SAAK,QAAQ,SAAS,SAAS;AAU/B,SAAK,SAAS,SAAS,UAAW;AAClC,SAAK,OAAO,SAAS,QAAQ,CAAC;AAC9B,SAAK,OAAO,SAAS,QAAQ;AAAA,EAC/B;AAAA,EAUA,IAAI,OAAO;AACT,WAAO,OAAO,KAAK,UAAU,WACzB,KAAK,MAAM,SACX,KAAK,MAAM;AAAA,EACjB;AAAA,EAEA,SAAS,UAAmB;AAC1B,QAAI,OAAO,KAAK,UAAU,UAAU;AAClC,aAAO,aAAa,QAAW,yBAAyB;AACxD,aAAO,KAAK;AAAA,IACd,WAAW,KAAK,iBAAiB,cAAc,aAAa,OAAO;AACjE,aAAO,WAAW,KAAK,KAAK;AAAA,IAC9B,WAAW,KAAK,iBAAiB,cAAc,aAAa,UAAU;AAGpE,aAAO,OAAO,OAAO,KAAK,KAAK;AAAA,IACjC;AACA,UAAM,UAAU,IAAI,YAAY,QAAQ;AACxC,WAAO,QAAQ,OAAO,KAAK,KAAK;AAAA,EAClC;AAAA,EAEA,QAAQ;AACN,UAAM,QAAQ,IAAI,aAAoB;AACtC,QAAI,OAAO,KAAK,UAAU,UAAU;AAClC,YAAM,QAAQ,KAAK;AAAA,IACrB,OAAO;AAEL,YAAM,QAAQ,KAAK,MAAM,MAAM,CAAC;AAAA,IAClC;AACA,UAAM,SAAS,UAAU,KAAK,MAAM;AACpC,UAAM,OAAO,UAAU,KAAK,IAAI;AAChC,UAAM,OAAO,KAAK;AAClB,WAAO;AAAA,EACT;AACF","sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert, bytesToHex } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-interface\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n get size() {\n return typeof this.value === 'string'\n ? this.value.length\n : this.value.byteLength;\n }\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n } else if (this.value instanceof Uint8Array && encoding === 'hex') {\n return bytesToHex(this.value);\n } else if (this.value instanceof Uint8Array && encoding === 'base64') {\n // For large files, this is quite slow, instead use `encodeBase64()`\n // TODO: Use @metamask/utils for this\n return base64.encode(this.value);\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path = this.path;\n return vfile;\n }\n}\n"]}
@@ -0,0 +1 @@
1
+ "use strict";//# sourceMappingURL=chunk-F6G67XQ4.js.map
@@ -4,17 +4,17 @@ import {
4
4
 
5
5
  // src/structs.ts
6
6
  import { union } from "@metamask/snaps-sdk";
7
- import { assert, isObject } from "@metamask/utils";
8
- import { bold, green, red } from "chalk";
9
7
  import {
8
+ assign,
10
9
  is,
11
10
  validate,
12
11
  type as superstructType,
13
12
  Struct,
14
13
  StructError,
15
- create,
16
- assign
17
- } from "superstruct";
14
+ create
15
+ } from "@metamask/superstruct";
16
+ import { assert, isObject } from "@metamask/utils";
17
+ import { bold, green, red } from "chalk";
18
18
  function color(value, colorFunction, enabled) {
19
19
  if (enabled) {
20
20
  return colorFunction(value);
@@ -223,4 +223,4 @@ export {
223
223
  createUnion,
224
224
  mergeStructs
225
225
  };
226
- //# sourceMappingURL=chunk-NHGIT2SZ.mjs.map
226
+ //# sourceMappingURL=chunk-FLSK6ISS.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/structs.ts"],"sourcesContent":["import { union } from '@metamask/snaps-sdk';\nimport type {\n AnyStruct,\n Assign,\n Failure,\n ObjectSchema,\n ObjectType,\n} from '@metamask/superstruct';\nimport {\n assign,\n is,\n validate,\n type as superstructType,\n Struct,\n StructError,\n create,\n} from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assert, isObject } from '@metamask/utils';\nimport { bold, green, red } from 'chalk';\n\nimport { indent } from './strings';\n\n/**\n * Infer a struct type, only if it matches the specified type. This is useful\n * for defining types and structs that are related to each other in separate\n * files.\n *\n * @example\n * ```typescript\n * // In file A\n * export type GetFileArgs = {\n * path: string;\n * encoding?: EnumToUnion<AuxiliaryFileEncoding>;\n * };\n *\n * // In file B\n * export const GetFileArgsStruct = object(...);\n *\n * // If the type and struct are in the same file, this will return the type.\n * // Otherwise, it will return `never`.\n * export type GetFileArgs =\n * InferMatching<typeof GetFileArgsStruct, GetFileArgs>;\n * ```\n */\nexport type InferMatching<\n StructType extends Struct<any, any>,\n Type,\n> = StructType['TYPE'] extends Type ? Type : never;\n\n/**\n * Colorize a value with a color function. This is useful for colorizing values\n * in error messages. If colorization is disabled, the original value is\n * returned.\n *\n * @param value - The value to colorize.\n * @param colorFunction - The color function to use.\n * @param enabled - Whether to colorize the value.\n * @returns The colorized value, or the original value if colorization is\n * disabled.\n */\nfunction color(\n value: string,\n colorFunction: (value: string) => string,\n enabled: boolean,\n) {\n if (enabled) {\n return colorFunction(value);\n }\n\n return value;\n}\n\n/**\n * Define a struct, and also define the name of the struct as the given name.\n *\n * This is useful for improving the error messages returned by `superstruct`.\n *\n * @param name - The name of the struct.\n * @param struct - The struct.\n * @returns The struct.\n */\nexport function named<Type, Schema>(\n name: string,\n struct: Struct<Type, Schema>,\n) {\n return new Struct({\n ...struct,\n type: name,\n });\n}\n\nexport class SnapsStructError<Type, Schema> extends StructError {\n constructor(\n struct: Struct<Type, Schema>,\n prefix: string,\n suffix: string,\n failure: StructError,\n failures: () => Generator<Failure>,\n colorize = true,\n ) {\n super(failure, failures);\n\n this.name = 'SnapsStructError';\n this.message = `${prefix}.\\n\\n${getStructErrorMessage(\n struct,\n [...failures()],\n colorize,\n )}${suffix ? `\\n\\n${suffix}` : ''}`;\n }\n}\n\ntype GetErrorOptions<Type, Schema> = {\n struct: Struct<Type, Schema>;\n prefix: string;\n suffix?: string;\n error: StructError;\n colorize?: boolean;\n};\n\n/**\n * Converts an array to a generator function that yields the items in the\n * array.\n *\n * @param array - The array.\n * @returns A generator function.\n * @yields The items in the array.\n */\nexport function* arrayToGenerator<Type>(\n array: Type[],\n): Generator<Type, void, undefined> {\n for (const item of array) {\n yield item;\n }\n}\n\n/**\n * Returns a `SnapsStructError` with the given prefix and suffix.\n *\n * @param options - The options.\n * @param options.struct - The struct that caused the error.\n * @param options.prefix - The prefix to add to the error message.\n * @param options.suffix - The suffix to add to the error message. Defaults to\n * an empty string.\n * @param options.error - The `superstruct` error to wrap.\n * @param options.colorize - Whether to colorize the value. Defaults to `true`.\n * @returns The `SnapsStructError`.\n */\nexport function getError<Type, Schema>({\n struct,\n prefix,\n suffix = '',\n error,\n colorize,\n}: GetErrorOptions<Type, Schema>) {\n return new SnapsStructError(\n struct,\n prefix,\n suffix,\n error,\n () => arrayToGenerator(error.failures()),\n colorize,\n );\n}\n\n/**\n * A wrapper of `superstruct`'s `create` function that throws a\n * `SnapsStructError` instead of a `StructError`. This is useful for improving\n * the error messages returned by `superstruct`.\n *\n * @param value - The value to validate.\n * @param struct - The `superstruct` struct to validate the value against.\n * @param prefix - The prefix to add to the error message.\n * @param suffix - The suffix to add to the error message. Defaults to an empty\n * string.\n * @returns The validated value.\n */\nexport function createFromStruct<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n prefix: string,\n suffix = '',\n) {\n try {\n return create(value, struct);\n } catch (error) {\n if (error instanceof StructError) {\n throw getError({ struct, prefix, suffix, error });\n }\n\n throw error;\n }\n}\n\n/**\n * Get a struct from a failure path.\n *\n * @param struct - The struct.\n * @param path - The failure path.\n * @returns The struct at the failure path.\n */\nexport function getStructFromPath<Type, Schema>(\n struct: Struct<Type, Schema>,\n path: string[],\n) {\n return path.reduce<AnyStruct>((result, key) => {\n if (isObject(struct.schema) && struct.schema[key]) {\n return struct.schema[key] as AnyStruct;\n }\n\n return result;\n }, struct);\n}\n\n/**\n * Get the union struct names from a struct.\n *\n * @param struct - The struct.\n * @param colorize - Whether to colorize the value. Defaults to `true`.\n * @returns The union struct names, or `null` if the struct is not a union\n * struct.\n */\nexport function getUnionStructNames<Type, Schema>(\n struct: Struct<Type, Schema>,\n colorize = true,\n) {\n if (Array.isArray(struct.schema)) {\n return struct.schema.map(({ type }) => color(type, green, colorize));\n }\n\n return null;\n}\n\n/**\n * Get an error prefix from a `superstruct` failure. This is useful for\n * formatting the error message returned by `superstruct`.\n *\n * @param failure - The `superstruct` failure.\n * @param colorize - Whether to colorize the value. Defaults to `true`.\n * @returns The error prefix.\n */\nexport function getStructErrorPrefix(failure: Failure, colorize = true) {\n if (failure.type === 'never' || failure.path.length === 0) {\n return '';\n }\n\n return `At path: ${color(failure.path.join('.'), bold, colorize)} — `;\n}\n\n/**\n * Get a string describing the failure. This is similar to the `message`\n * property of `superstruct`'s `Failure` type, but formats the value in a more\n * readable way.\n *\n * @param struct - The struct that caused the failure.\n * @param failure - The `superstruct` failure.\n * @param colorize - Whether to colorize the value. Defaults to `true`.\n * @returns A string describing the failure.\n */\nexport function getStructFailureMessage<Type, Schema>(\n struct: Struct<Type, Schema>,\n failure: Failure,\n colorize = true,\n) {\n const received = color(JSON.stringify(failure.value), red, colorize);\n const prefix = getStructErrorPrefix(failure, colorize);\n\n if (failure.type === 'union') {\n const childStruct = getStructFromPath(struct, failure.path);\n const unionNames = getUnionStructNames(childStruct, colorize);\n\n if (unionNames) {\n return `${prefix}Expected the value to be one of: ${unionNames.join(\n ', ',\n )}, but received: ${received}.`;\n }\n\n return `${prefix}${failure.message}.`;\n }\n\n if (failure.type === 'literal') {\n // Superstruct's failure does not provide information about which literal\n // value was expected, so we need to parse the message to get the literal.\n const message = failure.message\n .replace(\n /the literal `(.+)`,/u,\n `the value to be \\`${color('$1', green, colorize)}\\`,`,\n )\n .replace(\n /, but received: (.+)/u,\n `, but received: ${color('$1', red, colorize)}`,\n );\n\n return `${prefix}${message}.`;\n }\n\n if (failure.type === 'never') {\n return `Unknown key: ${color(\n failure.path.join('.'),\n bold,\n colorize,\n )}, received: ${received}.`;\n }\n\n if (failure.refinement === 'size') {\n const message = failure.message\n .replace(\n /length between `(\\d+)` and `(\\d+)`/u,\n `length between ${color('$1', green, colorize)} and ${color(\n '$2',\n green,\n colorize,\n )},`,\n )\n .replace(/length of `(\\d+)`/u, `length of ${color('$1', red, colorize)}`)\n .replace(/a array/u, 'an array');\n\n return `${prefix}${message}.`;\n }\n\n return `${prefix}Expected a value of type ${color(\n failure.type,\n green,\n colorize,\n )}, but received: ${received}.`;\n}\n\n/**\n * Get a string describing the errors. This formats all the errors in a\n * human-readable way.\n *\n * @param struct - The struct that caused the failures.\n * @param failures - The `superstruct` failures.\n * @param colorize - Whether to colorize the value. Defaults to `true`.\n * @returns A string describing the errors.\n */\nexport function getStructErrorMessage<Type, Schema>(\n struct: Struct<Type, Schema>,\n failures: Failure[],\n colorize = true,\n) {\n const formattedFailures = failures.map((failure) =>\n indent(`• ${getStructFailureMessage(struct, failure, colorize)}`),\n );\n\n return formattedFailures.join('\\n');\n}\n\n/**\n * Validate a union struct, and throw readable errors if the value does not\n * satisfy the struct. This is useful for improving the error messages returned\n * by `superstruct`.\n *\n * @param value - The value to validate.\n * @param struct - The `superstruct` union struct to validate the value against.\n * This struct must be a union of object structs, and must have at least one\n * shared key to validate against.\n * @param structKey - The key to validate against. This key must be present in\n * all the object structs in the union struct, and is expected to be a literal\n * value.\n * @param coerce - Whether to coerce the value to satisfy the struct. Defaults\n * to `false`.\n * @returns The validated value.\n * @throws If the value does not satisfy the struct.\n * @example\n * const struct = union([\n * object({ type: literal('a'), value: string() }),\n * object({ type: literal('b'), value: number() }),\n * object({ type: literal('c'), value: boolean() }),\n * // ...\n * ]);\n *\n * // At path: type — Expected the value to be one of: \"a\", \"b\", \"c\", but received: \"d\".\n * validateUnion({ type: 'd', value: 'foo' }, struct, 'type');\n *\n * // At path: value — Expected a value of type string, but received: 42.\n * validateUnion({ type: 'a', value: 42 }, struct, 'value');\n */\nexport function validateUnion<Type, Schema extends readonly Struct<any, any>[]>(\n value: unknown,\n struct: Struct<Type, Schema>,\n structKey: keyof Type,\n coerce = false,\n) {\n assert(\n struct.schema,\n 'Expected a struct with a schema. Make sure to use `union` from `@metamask/snaps-sdk`.',\n );\n assert(struct.schema.length > 0, 'Expected a non-empty array of structs.');\n\n const keyUnion = struct.schema.map(\n (innerStruct) => innerStruct.schema[structKey],\n // This is guaranteed to be a non-empty array by the assertion above. We\n // need to cast it since `superstruct` requires a non-empty array of structs\n // for the `union` struct.\n ) as NonEmptyArray<Struct>;\n\n const key = superstructType({\n [structKey]: union(keyUnion),\n });\n\n const [keyError] = validate(value, key, { coerce });\n if (keyError) {\n throw new Error(\n getStructFailureMessage(key, keyError.failures()[0], false),\n );\n }\n\n // At this point it's guaranteed that the value is an object, so we can safely\n // cast it to a Record.\n const objectValue = value as Record<PropertyKey, unknown>;\n const objectStructs = struct.schema.filter((innerStruct) =>\n is(objectValue[structKey], innerStruct.schema[structKey]),\n );\n\n assert(objectStructs.length > 0, 'Expected a struct to match the value.');\n\n // We need to validate the value against all the object structs that match the\n // struct key, and return the first validated value.\n const validationResults = objectStructs.map((objectStruct) =>\n validate(objectValue, objectStruct, { coerce }),\n );\n\n const validatedValue = validationResults.find(([error]) => !error);\n if (validatedValue) {\n return validatedValue[1];\n }\n\n assert(validationResults[0][0], 'Expected at least one error.');\n\n // If there is no validated value, we need to find the error with the least\n // number of failures (with the assumption that it's the most specific error).\n const validationError = validationResults.reduce((error, [innerError]) => {\n assert(innerError, 'Expected an error.');\n if (innerError.failures().length < error.failures().length) {\n return innerError;\n }\n\n return error;\n }, validationResults[0][0]);\n\n throw new Error(\n getStructFailureMessage(struct, validationError.failures()[0], false),\n );\n}\n\n/**\n * Create a value with the coercion logic of a union struct, and throw readable\n * errors if the value does not satisfy the struct. This is useful for improving\n * the error messages returned by `superstruct`.\n *\n * @param value - The value to validate.\n * @param struct - The `superstruct` union struct to validate the value against.\n * This struct must be a union of object structs, and must have at least one\n * shared key to validate against.\n * @param structKey - The key to validate against. This key must be present in\n * all the object structs in the union struct, and is expected to be a literal\n * value.\n * @returns The validated value.\n * @throws If the value does not satisfy the struct.\n * @see validateUnion\n */\nexport function createUnion<Type, Schema extends readonly Struct<any, any>[]>(\n value: unknown,\n struct: Struct<Type, Schema>,\n structKey: keyof Type,\n) {\n return validateUnion(value, struct, structKey, true);\n}\n\n// These types are copied from Superstruct, to mirror `assign`.\nexport function mergeStructs<\n ObjectA extends ObjectSchema,\n ObjectB extends ObjectSchema,\n>(\n A: Struct<ObjectType<ObjectA>, ObjectA>,\n B: Struct<ObjectType<ObjectB>, ObjectB>,\n): Struct<ObjectType<Assign<ObjectA, ObjectB>>, Assign<ObjectA, ObjectB>>;\nexport function mergeStructs<\n ObjectA extends ObjectSchema,\n ObjectB extends ObjectSchema,\n ObjectC extends ObjectSchema,\n>(\n A: Struct<ObjectType<ObjectA>, ObjectA>,\n B: Struct<ObjectType<ObjectB>, ObjectB>,\n C: Struct<ObjectType<ObjectC>, ObjectC>,\n): Struct<\n ObjectType<Assign<Assign<ObjectA, ObjectB>, ObjectC>>,\n Assign<Assign<ObjectA, ObjectB>, ObjectC>\n>;\nexport function mergeStructs<\n ObjectA extends ObjectSchema,\n ObjectB extends ObjectSchema,\n ObjectC extends ObjectSchema,\n ObjectD extends ObjectSchema,\n>(\n A: Struct<ObjectType<ObjectA>, ObjectA>,\n B: Struct<ObjectType<ObjectB>, ObjectB>,\n C: Struct<ObjectType<ObjectC>, ObjectC>,\n D: Struct<ObjectType<ObjectD>, ObjectD>,\n): Struct<\n ObjectType<Assign<Assign<Assign<ObjectA, ObjectB>, ObjectC>, ObjectD>>,\n Assign<Assign<Assign<ObjectA, ObjectB>, ObjectC>, ObjectD>\n>;\nexport function mergeStructs<\n ObjectA extends ObjectSchema,\n ObjectB extends ObjectSchema,\n ObjectC extends ObjectSchema,\n ObjectD extends ObjectSchema,\n ObjectE extends ObjectSchema,\n>(\n A: Struct<ObjectType<ObjectA>, ObjectA>,\n B: Struct<ObjectType<ObjectB>, ObjectB>,\n C: Struct<ObjectType<ObjectC>, ObjectC>,\n D: Struct<ObjectType<ObjectD>, ObjectD>,\n E: Struct<ObjectType<ObjectE>, ObjectE>,\n): Struct<\n ObjectType<\n Assign<Assign<Assign<Assign<ObjectA, ObjectB>, ObjectC>, ObjectD>, ObjectE>\n >,\n Assign<Assign<Assign<Assign<ObjectA, ObjectB>, ObjectC>, ObjectD>, ObjectE>\n>;\n\n/**\n * Merge multiple structs into one, using superstruct `assign`.\n *\n * Differently from plain `assign`, this function also copies over refinements from each struct.\n *\n * @param structs - The `superstruct` structs to merge.\n * @returns The merged struct.\n */\nexport function mergeStructs(...structs: Struct<any>[]): Struct<any> {\n const mergedStruct = (assign as (...structs: Struct<any>[]) => Struct)(\n ...structs,\n );\n return new Struct({\n ...mergedStruct,\n *refiner(value, ctx) {\n for (const struct of structs) {\n yield* struct.refiner(value, ctx);\n }\n },\n });\n}\n"],"mappings":";;;;;AAAA,SAAS,aAAa;AAQtB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA,Q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