@metamask/snaps-utils 7.6.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.
- package/CHANGELOG.md +28 -1
- package/dist/auxiliary-files.js +8 -5
- package/dist/auxiliary-files.mjs +9 -6
- package/dist/base64.js +4 -4
- package/dist/base64.mjs +3 -3
- package/dist/bytes.js +3 -3
- package/dist/bytes.mjs +2 -2
- package/dist/checksum.js +4 -4
- package/dist/checksum.mjs +3 -3
- package/dist/{chunk-AD2MMGAX.js → chunk-25ESL4NL.js} +27 -27
- package/dist/{chunk-6YRUDGNL.mjs → chunk-3S4INAGA.mjs} +2 -2
- package/dist/{chunk-I4TRH6VO.mjs → chunk-4AA3TKRJ.mjs} +2 -2
- package/dist/chunk-4AA3TKRJ.mjs.map +1 -0
- package/dist/{chunk-2LBN5T56.js → chunk-4ZJOU3LR.js} +9 -2
- package/dist/chunk-4ZJOU3LR.js.map +1 -0
- package/dist/{chunk-LNZLPDBB.js → chunk-5I5WPAVD.js} +24 -20
- package/dist/chunk-5I5WPAVD.js.map +1 -0
- package/dist/chunk-5WKQI22S.js +7 -0
- package/dist/chunk-5WKQI22S.js.map +1 -0
- package/dist/{chunk-RS5E2NXN.js → chunk-6ATNGN44.js} +43 -7
- package/dist/chunk-6ATNGN44.js.map +1 -0
- package/dist/{chunk-G6BVXNNZ.js → chunk-6HXIPMBN.js} +3 -3
- package/dist/{chunk-NQMQIAWE.mjs → chunk-7HLSXEHK.mjs} +43 -7
- package/dist/chunk-7HLSXEHK.mjs.map +1 -0
- package/dist/{chunk-T6FWIDA6.mjs → chunk-AJNKQYKP.mjs} +3 -3
- package/dist/chunk-AJNKQYKP.mjs.map +1 -0
- package/dist/{chunk-MLOQHUOY.js → chunk-AMW2VJ3N.js} +3 -3
- package/dist/{chunk-K2OTEZZZ.mjs → chunk-ANYNWSCA.mjs} +11 -4
- package/dist/chunk-ANYNWSCA.mjs.map +1 -0
- package/dist/{chunk-R5DO7T2D.js → chunk-B5GNTDE2.js} +2 -2
- package/dist/chunk-B5GNTDE2.js.map +1 -0
- package/dist/{chunk-WZ457PEQ.mjs → chunk-CJK7DDV2.mjs} +2 -2
- package/dist/chunk-CJK7DDV2.mjs.map +1 -0
- package/dist/{chunk-ZJRWU4AJ.mjs → chunk-DE22V5AO.mjs} +4 -1
- package/dist/{chunk-ZJRWU4AJ.mjs.map → chunk-DE22V5AO.mjs.map} +1 -1
- package/dist/{chunk-HJYRGKCX.js → chunk-DEQUNIME.js} +4 -1
- package/dist/chunk-DEQUNIME.js.map +1 -0
- package/dist/chunk-F6G67XQ4.js +1 -0
- package/dist/{chunk-GTAYOKI4.mjs → chunk-FLSK6ISS.mjs} +20 -5
- package/dist/chunk-FLSK6ISS.mjs.map +1 -0
- package/dist/{chunk-YCIPY3UP.mjs → chunk-H35ZUVQT.mjs} +7 -7
- package/dist/chunk-H35ZUVQT.mjs.map +1 -0
- package/dist/{chunk-IPOE62V4.js → chunk-I2SWLZ34.js} +2 -2
- package/dist/chunk-I2SWLZ34.js.map +1 -0
- package/dist/{chunk-FOWIC2SO.mjs → chunk-JJTIVHFX.mjs} +2 -2
- package/dist/{chunk-7SFY75BI.mjs → chunk-KCECQM3L.mjs} +11 -11
- package/dist/{chunk-EXUEHPZ4.mjs → chunk-LHQP7CUJ.mjs} +2 -2
- package/dist/chunk-LHQP7CUJ.mjs.map +1 -0
- package/dist/{chunk-6LOYTBS3.js → chunk-LOJA2ZS4.js} +2 -2
- package/dist/chunk-LOJA2ZS4.js.map +1 -0
- package/dist/{chunk-QSCKTRRU.js → chunk-MKFQAXRF.js} +3 -3
- package/dist/{chunk-UMZNVWEM.mjs → chunk-N5HVDE3P.mjs} +2 -2
- package/dist/chunk-N5HVDE3P.mjs.map +1 -0
- package/dist/{chunk-IHQPAJ2B.js → chunk-OBN2WDFU.js} +3 -3
- package/dist/chunk-OYZPBNHS.mjs +1 -0
- package/dist/chunk-PLNIERDY.js +34 -0
- package/dist/chunk-PLNIERDY.js.map +1 -0
- package/dist/{chunk-CMOSYNZR.js → chunk-PZNIU23I.js} +3 -3
- package/dist/chunk-PZNIU23I.js.map +1 -0
- package/dist/{chunk-K4B37F3L.js → chunk-RD6BN2TQ.js} +12 -12
- package/dist/chunk-RD6BN2TQ.js.map +1 -0
- package/dist/{chunk-7Y4VHT3E.js → chunk-SB43G2RI.js} +8 -8
- package/dist/chunk-SPCIIRSB.mjs +7 -0
- package/dist/chunk-SPCIIRSB.mjs.map +1 -0
- package/dist/{chunk-37VHIRUJ.js → chunk-T2VA4YJE.js} +11 -4
- package/dist/chunk-T2VA4YJE.js.map +1 -0
- package/dist/{chunk-473MIETW.js → chunk-UAINMOUU.js} +3 -3
- package/dist/chunk-VIAHMNTA.mjs +34 -0
- package/dist/chunk-VIAHMNTA.mjs.map +1 -0
- package/dist/{chunk-A6E325SZ.js → chunk-VOPTAKVG.js} +20 -5
- package/dist/chunk-VOPTAKVG.js.map +1 -0
- package/dist/{chunk-OJM2SKJZ.js → chunk-X2GGUOMY.js} +2 -2
- package/dist/chunk-X2GGUOMY.js.map +1 -0
- package/dist/{chunk-LBCPJOAV.mjs → chunk-X3UZCGO5.mjs} +2 -2
- package/dist/{chunk-HJRCBSNA.mjs → chunk-XF2AZMWG.mjs} +2 -2
- package/dist/{chunk-VDJTPG6F.js → chunk-XWYJQWHG.js} +3 -3
- package/dist/{chunk-PWLXPALG.mjs → chunk-XZ7362GQ.mjs} +2 -2
- package/dist/{chunk-IGMAXVPP.mjs → chunk-XZNJFDBF.mjs} +2 -2
- package/dist/{chunk-EA2FOAEG.mjs → chunk-YJHQZFR4.mjs} +9 -2
- package/dist/chunk-YJHQZFR4.mjs.map +1 -0
- package/dist/{chunk-G4LY5HGU.mjs → chunk-YNOZU43P.mjs} +6 -6
- package/dist/{chunk-4I22UHIU.mjs → chunk-ZXHR322P.mjs} +28 -24
- package/dist/chunk-ZXHR322P.mjs.map +1 -0
- package/dist/constants.js +8 -0
- package/dist/constants.js.map +1 -0
- package/dist/constants.mjs +8 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/cronjob.js +2 -2
- package/dist/cronjob.mjs +1 -1
- package/dist/eval-worker.js +1 -1
- package/dist/eval-worker.mjs +1 -1
- package/dist/eval.js +5 -5
- package/dist/eval.mjs +4 -4
- package/dist/fs.js +4 -4
- package/dist/fs.mjs +3 -3
- package/dist/handlers.js +2 -2
- package/dist/handlers.mjs +1 -1
- package/dist/index.executionenv.js +4 -4
- package/dist/index.executionenv.mjs +15 -15
- package/dist/index.js +40 -30
- package/dist/index.mjs +76 -66
- package/dist/json-rpc.js +2 -2
- package/dist/json-rpc.mjs +1 -1
- package/dist/localization.js +2 -2
- package/dist/localization.mjs +1 -1
- package/dist/manifest/index.js +12 -10
- package/dist/manifest/index.mjs +11 -9
- package/dist/manifest/manifest.js +16 -14
- package/dist/manifest/manifest.mjs +15 -13
- package/dist/manifest/node.js +16 -14
- package/dist/manifest/node.mjs +15 -13
- package/dist/manifest/validation.js +12 -10
- package/dist/manifest/validation.mjs +11 -9
- package/dist/namespace.js +2 -2
- package/dist/namespace.mjs +1 -1
- package/dist/node.js +44 -34
- package/dist/node.mjs +80 -70
- package/dist/npm.js +16 -14
- package/dist/npm.mjs +15 -13
- package/dist/snaps.js +6 -6
- package/dist/snaps.mjs +5 -5
- package/dist/structs.js +4 -2
- package/dist/structs.mjs +3 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/auxiliary-files.d.ts +7 -0
- package/dist/types/constants.d.ts +1 -0
- package/dist/types/cronjob.d.ts +22 -22
- package/dist/types/handlers.d.ts +39 -144
- package/dist/types/index.d.ts +1 -0
- package/dist/types/json-rpc.d.ts +7 -7
- package/dist/types/localization.d.ts +4 -4
- package/dist/types/manifest/validation.d.ts +2 -2
- package/dist/types/namespace.d.ts +18 -18
- package/dist/types/snaps.d.ts +1 -1
- package/dist/types/structs.d.ts +6 -3
- package/dist/types/types.d.ts +1 -1
- package/dist/types/ui.d.ts +13 -3
- package/dist/types/virtual-file/VirtualFile.d.ts +1 -0
- package/dist/types.js +2 -2
- package/dist/types.mjs +1 -1
- package/dist/ui.js +4 -2
- package/dist/ui.mjs +3 -1
- package/dist/validation.js +14 -12
- package/dist/validation.mjs +13 -11
- package/dist/versions.js +2 -2
- package/dist/versions.mjs +1 -1
- package/dist/virtual-file/VirtualFile.js +2 -2
- package/dist/virtual-file/VirtualFile.mjs +1 -1
- package/dist/virtual-file/index.js +2 -2
- package/dist/virtual-file/index.mjs +1 -1
- package/dist/virtual-file/node.js +3 -3
- package/dist/virtual-file/node.mjs +2 -2
- package/dist/virtual-file/toVirtualFile.js +3 -3
- package/dist/virtual-file/toVirtualFile.mjs +2 -2
- package/package.json +9 -9
- package/dist/chunk-2LBN5T56.js.map +0 -1
- package/dist/chunk-37VHIRUJ.js.map +0 -1
- package/dist/chunk-4I22UHIU.mjs.map +0 -1
- package/dist/chunk-6LOYTBS3.js.map +0 -1
- package/dist/chunk-7LOABHVW.js +0 -1
- package/dist/chunk-7MTAHOWC.mjs +0 -1
- package/dist/chunk-A6E325SZ.js.map +0 -1
- package/dist/chunk-CMOSYNZR.js.map +0 -1
- package/dist/chunk-EA2FOAEG.mjs.map +0 -1
- package/dist/chunk-EXUEHPZ4.mjs.map +0 -1
- package/dist/chunk-GTAYOKI4.mjs.map +0 -1
- package/dist/chunk-HJYRGKCX.js.map +0 -1
- package/dist/chunk-I4TRH6VO.mjs.map +0 -1
- package/dist/chunk-IPOE62V4.js.map +0 -1
- package/dist/chunk-IXBJNVHK.js +0 -22
- package/dist/chunk-IXBJNVHK.js.map +0 -1
- package/dist/chunk-K2OTEZZZ.mjs.map +0 -1
- package/dist/chunk-K4B37F3L.js.map +0 -1
- package/dist/chunk-KMLVVVK3.mjs +0 -22
- package/dist/chunk-KMLVVVK3.mjs.map +0 -1
- package/dist/chunk-LNZLPDBB.js.map +0 -1
- package/dist/chunk-NQMQIAWE.mjs.map +0 -1
- package/dist/chunk-OJM2SKJZ.js.map +0 -1
- package/dist/chunk-R5DO7T2D.js.map +0 -1
- package/dist/chunk-RS5E2NXN.js.map +0 -1
- package/dist/chunk-T6FWIDA6.mjs.map +0 -1
- package/dist/chunk-UMZNVWEM.mjs.map +0 -1
- package/dist/chunk-WZ457PEQ.mjs.map +0 -1
- package/dist/chunk-YCIPY3UP.mjs.map +0 -1
- /package/dist/{chunk-AD2MMGAX.js.map → chunk-25ESL4NL.js.map} +0 -0
- /package/dist/{chunk-6YRUDGNL.mjs.map → chunk-3S4INAGA.mjs.map} +0 -0
- /package/dist/{chunk-G6BVXNNZ.js.map → chunk-6HXIPMBN.js.map} +0 -0
- /package/dist/{chunk-MLOQHUOY.js.map → chunk-AMW2VJ3N.js.map} +0 -0
- /package/dist/{chunk-7LOABHVW.js.map → chunk-F6G67XQ4.js.map} +0 -0
- /package/dist/{chunk-FOWIC2SO.mjs.map → chunk-JJTIVHFX.mjs.map} +0 -0
- /package/dist/{chunk-7SFY75BI.mjs.map → chunk-KCECQM3L.mjs.map} +0 -0
- /package/dist/{chunk-QSCKTRRU.js.map → chunk-MKFQAXRF.js.map} +0 -0
- /package/dist/{chunk-IHQPAJ2B.js.map → chunk-OBN2WDFU.js.map} +0 -0
- /package/dist/{chunk-7MTAHOWC.mjs.map → chunk-OYZPBNHS.mjs.map} +0 -0
- /package/dist/{chunk-7Y4VHT3E.js.map → chunk-SB43G2RI.js.map} +0 -0
- /package/dist/{chunk-473MIETW.js.map → chunk-UAINMOUU.js.map} +0 -0
- /package/dist/{chunk-LBCPJOAV.mjs.map → chunk-X3UZCGO5.mjs.map} +0 -0
- /package/dist/{chunk-HJRCBSNA.mjs.map → chunk-XF2AZMWG.mjs.map} +0 -0
- /package/dist/{chunk-PWLXPALG.mjs.map → chunk-XWYJQWHG.js.map} +0 -0
- /package/dist/{chunk-VDJTPG6F.js.map → chunk-XZ7362GQ.mjs.map} +0 -0
- /package/dist/{chunk-IGMAXVPP.mjs.map → chunk-XZNJFDBF.mjs.map} +0 -0
- /package/dist/{chunk-G4LY5HGU.mjs.map → chunk-YNOZU43P.mjs.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/constants.ts"],"names":[],"mappings":";AAGO,IAAM,gBAAgB","sourcesContent":["// The max file size for Snap source code and auxiliary files.\n// 64 MB - we chose this number because it is the size limit for postMessage\n// between the extension and the offscreen document enforced by Chrome.\nexport const MAX_FILE_SIZE = 64_000_000;\n"]}
|
|
@@ -212,10 +212,13 @@ function getTotalTextLength(component) {
|
|
|
212
212
|
function hasChildren(element) {
|
|
213
213
|
return _utils.hasProperty.call(void 0, element.props, "children");
|
|
214
214
|
}
|
|
215
|
+
function filterJsxChild(child) {
|
|
216
|
+
return Boolean(child) && child !== true;
|
|
217
|
+
}
|
|
215
218
|
function getJsxChildren(element) {
|
|
216
219
|
if (hasChildren(element)) {
|
|
217
220
|
if (Array.isArray(element.props.children)) {
|
|
218
|
-
return element.props.children.filter(
|
|
221
|
+
return element.props.children.filter(filterJsxChild).flat(Infinity);
|
|
219
222
|
}
|
|
220
223
|
if (element.props.children) {
|
|
221
224
|
return [element.props.children];
|
|
@@ -223,17 +226,17 @@ function getJsxChildren(element) {
|
|
|
223
226
|
}
|
|
224
227
|
return [];
|
|
225
228
|
}
|
|
226
|
-
function walkJsx(node, callback) {
|
|
229
|
+
function walkJsx(node, callback, depth = 0) {
|
|
227
230
|
if (Array.isArray(node)) {
|
|
228
231
|
for (const child of node) {
|
|
229
|
-
const childResult = walkJsx(child, callback);
|
|
232
|
+
const childResult = walkJsx(child, callback, depth);
|
|
230
233
|
if (childResult !== void 0) {
|
|
231
234
|
return childResult;
|
|
232
235
|
}
|
|
233
236
|
}
|
|
234
237
|
return void 0;
|
|
235
238
|
}
|
|
236
|
-
const result = callback(node);
|
|
239
|
+
const result = callback(node, depth);
|
|
237
240
|
if (result !== void 0) {
|
|
238
241
|
return result;
|
|
239
242
|
}
|
|
@@ -241,7 +244,7 @@ function walkJsx(node, callback) {
|
|
|
241
244
|
const children = getJsxChildren(node);
|
|
242
245
|
for (const child of children) {
|
|
243
246
|
if (_utils.isPlainObject.call(void 0, child)) {
|
|
244
|
-
const childResult = walkJsx(child, callback);
|
|
247
|
+
const childResult = walkJsx(child, callback, depth + 1);
|
|
245
248
|
if (childResult !== void 0) {
|
|
246
249
|
return childResult;
|
|
247
250
|
}
|
|
@@ -250,6 +253,39 @@ function walkJsx(node, callback) {
|
|
|
250
253
|
}
|
|
251
254
|
return void 0;
|
|
252
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
|
+
|
|
253
289
|
|
|
254
290
|
|
|
255
291
|
|
|
@@ -260,5 +296,5 @@ function walkJsx(node, callback) {
|
|
|
260
296
|
|
|
261
297
|
|
|
262
298
|
|
|
263
|
-
exports.getTextChildren = getTextChildren; exports.getJsxElementFromComponent = getJsxElementFromComponent; exports.validateTextLinks = validateTextLinks; exports.validateJsxLinks = validateJsxLinks; exports.getTotalTextLength = getTotalTextLength; exports.hasChildren = hasChildren; exports.getJsxChildren = getJsxChildren; exports.walkJsx = walkJsx;
|
|
264
|
-
//# sourceMappingURL=chunk-
|
|
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;AACE,aAAO;AAAA,EACX;AACF;AASA,SAAS,Y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AI,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
|
|
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
|
|
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-
|
|
103
|
+
//# sourceMappingURL=chunk-6HXIPMBN.js.map
|
|
@@ -212,10 +212,13 @@ function getTotalTextLength(component) {
|
|
|
212
212
|
function hasChildren(element) {
|
|
213
213
|
return hasProperty(element.props, "children");
|
|
214
214
|
}
|
|
215
|
+
function filterJsxChild(child) {
|
|
216
|
+
return Boolean(child) && child !== true;
|
|
217
|
+
}
|
|
215
218
|
function getJsxChildren(element) {
|
|
216
219
|
if (hasChildren(element)) {
|
|
217
220
|
if (Array.isArray(element.props.children)) {
|
|
218
|
-
return element.props.children.filter(
|
|
221
|
+
return element.props.children.filter(filterJsxChild).flat(Infinity);
|
|
219
222
|
}
|
|
220
223
|
if (element.props.children) {
|
|
221
224
|
return [element.props.children];
|
|
@@ -223,17 +226,17 @@ function getJsxChildren(element) {
|
|
|
223
226
|
}
|
|
224
227
|
return [];
|
|
225
228
|
}
|
|
226
|
-
function walkJsx(node, callback) {
|
|
229
|
+
function walkJsx(node, callback, depth = 0) {
|
|
227
230
|
if (Array.isArray(node)) {
|
|
228
231
|
for (const child of node) {
|
|
229
|
-
const childResult = walkJsx(child, callback);
|
|
232
|
+
const childResult = walkJsx(child, callback, depth);
|
|
230
233
|
if (childResult !== void 0) {
|
|
231
234
|
return childResult;
|
|
232
235
|
}
|
|
233
236
|
}
|
|
234
237
|
return void 0;
|
|
235
238
|
}
|
|
236
|
-
const result = callback(node);
|
|
239
|
+
const result = callback(node, depth);
|
|
237
240
|
if (result !== void 0) {
|
|
238
241
|
return result;
|
|
239
242
|
}
|
|
@@ -241,7 +244,7 @@ function walkJsx(node, callback) {
|
|
|
241
244
|
const children = getJsxChildren(node);
|
|
242
245
|
for (const child of children) {
|
|
243
246
|
if (isPlainObject(child)) {
|
|
244
|
-
const childResult = walkJsx(child, callback);
|
|
247
|
+
const childResult = walkJsx(child, callback, depth + 1);
|
|
245
248
|
if (childResult !== void 0) {
|
|
246
249
|
return childResult;
|
|
247
250
|
}
|
|
@@ -250,6 +253,38 @@ function walkJsx(node, callback) {
|
|
|
250
253
|
}
|
|
251
254
|
return void 0;
|
|
252
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
|
+
}
|
|
253
288
|
|
|
254
289
|
export {
|
|
255
290
|
getTextChildren,
|
|
@@ -259,6 +294,7 @@ export {
|
|
|
259
294
|
getTotalTextLength,
|
|
260
295
|
hasChildren,
|
|
261
296
|
getJsxChildren,
|
|
262
|
-
walkJsx
|
|
297
|
+
walkJsx,
|
|
298
|
+
serialiseJsx
|
|
263
299
|
};
|
|
264
|
-
//# sourceMappingURL=chunk-
|
|
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;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,EAAE,MAAM,MAAM,IAAI;AACxB,QAAM,kBAAkB,cAAc,IAAI,OAAO;AAEjD,MAAI,YAAY,OAAO,UAAU,GAAG;AAClC,UAAM,WAAW,aAAa,MAAM,UAAsB,cAAc,CAAC;AACzE,WAAO,GAAG,MAAM,IAAI,IAAI,GAAG;AAAA,MACzB;AAAA,IACF,CAAC;AAAA,EAAM,QAAQ,GAAG,MAAM,KAAK,IAAI,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-
|
|
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: [\n {\n type: SnapCaveatType.SnapIds;\n value: Record<string, Json>;\n },\n ];\n };\n};\n"],"mappings":";AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,UAAU;AAAA,OACL;AAEP,SAAS,cAAc,qBAAqB;AAQrC,IAAK,mBAAL,kBAAKA,sBAAL;AACL,EAAAA,kBAAA,iBAAc;AACd,EAAAA,kBAAA,cAAW;AAFD,SAAAA;AAAA,GAAA;AAKL,IAAM,aAAa;AAAA,EACxB;AAAA,IACE,OAAO;AAAA,IACP;AAAA,EACF;AAAA,EACA;AAAA,EACA;AACF;AAIO,IAAM,2BAA2B,KAAK;AAAA,EAC3C,SAAS;AAAA,EACT,MAAM;AAAA,EACN,MAAM,SAAS,KAAK,OAAO,GAAG,GAAG,QAAQ,CAAC;AAAA,EAC1C,YAAY;AAAA,IACV,OAAO;AAAA,MACL,MAAM,KAAK,OAAO,GAAG,GAAG,QAAQ;AAAA,MAChC,KAAK,KAAK,OAAO,GAAG,GAAG,QAAQ;AAAA,IACjC,CAAC;AAAA,EACH;AACF,CAAC;AAWM,SAAS,qBACd,OAC6B;AAC7B,SAAO,GAAG,OAAO,wBAAwB;AAC3C;AAQO,SAAS,2BACd,OACqC;AACrC;AAAA,IACE;AAAA,IACA;AAAA,IACA,IAAI,gCAA4B;AAAA,EAClC;AACF;AAuCO,IAAK,iBAAL,kBAAKC,oBAAL;AACL,EAAAA,gBAAA,SAAM;AACN,EAAAA,gBAAA,WAAQ;AAFE,SAAAA;AAAA,GAAA;AAUL,IAAK,8BAAL,kBAAKC,iCAAL;AACL,EAAAA,6BAAA,kBAAe;AACf,EAAAA,6BAAA,qBAAkB;AAClB,EAAAA,6BAAA,wBAAqB;AACrB,EAAAA,6BAAA,oBAAiB;AAJP,SAAAA;AAAA,GAAA;AAQL,IAAK,oBAAL,kBAAKC,uBAAL;AACL,EAAAA,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
|
|
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 =
|
|
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-
|
|
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(
|
|
57
|
-
return RegExp(regex
|
|
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-
|
|
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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,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-
|
|
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,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O,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-
|
|
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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I,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-
|
|
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 = 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,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;","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
|
_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-
|
|
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
|