@metamask/snaps-utils 7.2.0 → 7.3.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 +10 -1
- package/dist/{chunk-I77AVJKV.mjs → chunk-L4DKSTK6.mjs} +5 -5
- package/dist/chunk-L4DKSTK6.mjs.map +1 -0
- package/dist/chunk-YACE3IOJ.mjs +262 -0
- package/dist/chunk-YACE3IOJ.mjs.map +1 -0
- package/dist/chunk-YAKDZ5LP.js +262 -0
- package/dist/chunk-YAKDZ5LP.js.map +1 -0
- package/dist/{chunk-3SOYDY4W.js → chunk-ZUSNAQJU.js} +4 -4
- package/dist/chunk-ZUSNAQJU.js.map +1 -0
- package/dist/handlers.js +2 -2
- package/dist/handlers.mjs +1 -1
- package/dist/index.executionenv.js +2 -2
- package/dist/index.executionenv.mjs +1 -1
- package/dist/index.js +13 -5
- package/dist/index.mjs +17 -9
- package/dist/node.js +13 -5
- package/dist/node.mjs +16 -8
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/handlers.d.ts +89 -89
- package/dist/types/ui.d.ts +51 -11
- package/dist/ui.js +10 -2
- package/dist/ui.mjs +13 -5
- package/package.json +3 -3
- package/dist/chunk-3SOYDY4W.js.map +0 -1
- package/dist/chunk-I77AVJKV.mjs.map +0 -1
- package/dist/chunk-KP4ZAWMP.mjs +0 -80
- package/dist/chunk-KP4ZAWMP.mjs.map +0 -1
- package/dist/chunk-NDIITWO4.js +0 -80
- package/dist/chunk-NDIITWO4.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [7.3.0]
|
|
10
|
+
### Added
|
|
11
|
+
- Add JSX support for custom UI ([#2258](https://github.com/MetaMask/snaps/pull/2258), [#2383](https://github.com/MetaMask/snaps/pull/2383))
|
|
12
|
+
- This adds utility functions for working with JSX in Snaps.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
- Bump `@metamask/base-controller` from `5.0.1` to `5.0.2` ([#2375](https://github.com/MetaMask/snaps/pull/2375))
|
|
16
|
+
|
|
9
17
|
## [7.2.0]
|
|
10
18
|
### Added
|
|
11
19
|
- Add `getJsonSizeUnsafe` ([#2342](https://github.com/MetaMask/snaps/pull/2342))
|
|
@@ -217,7 +225,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
217
225
|
- The version of the package no longer needs to match the version of all other
|
|
218
226
|
MetaMask Snaps packages.
|
|
219
227
|
|
|
220
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.
|
|
228
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.3.0...HEAD
|
|
229
|
+
[7.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.2.0...@metamask/snaps-utils@7.3.0
|
|
221
230
|
[7.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.1.0...@metamask/snaps-utils@7.2.0
|
|
222
231
|
[7.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.0.4...@metamask/snaps-utils@7.1.0
|
|
223
232
|
[7.0.4]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.0.3...@metamask/snaps-utils@7.0.4
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/handlers.ts
|
|
2
|
-
import {
|
|
2
|
+
import { ComponentOrElementStruct, SeverityLevel } from "@metamask/snaps-sdk";
|
|
3
3
|
import {
|
|
4
4
|
assign,
|
|
5
5
|
literal,
|
|
@@ -77,7 +77,7 @@ var SNAP_EXPORTS = {
|
|
|
77
77
|
},
|
|
78
78
|
["onUserInput" /* OnUserInput */]: {
|
|
79
79
|
type: "onUserInput" /* OnUserInput */,
|
|
80
|
-
required:
|
|
80
|
+
required: false,
|
|
81
81
|
validator: (snapExport) => {
|
|
82
82
|
return typeof snapExport === "function";
|
|
83
83
|
}
|
|
@@ -95,7 +95,7 @@ var OnTransactionResponseWithIdStruct = assign(
|
|
|
95
95
|
var OnTransactionResponseWithContentStruct = assign(
|
|
96
96
|
OnTransactionSeverityResponseStruct,
|
|
97
97
|
object({
|
|
98
|
-
content:
|
|
98
|
+
content: ComponentOrElementStruct
|
|
99
99
|
})
|
|
100
100
|
);
|
|
101
101
|
var OnTransactionResponseStruct = nullable(
|
|
@@ -106,7 +106,7 @@ var OnTransactionResponseStruct = nullable(
|
|
|
106
106
|
);
|
|
107
107
|
var OnSignatureResponseStruct = OnTransactionResponseStruct;
|
|
108
108
|
var OnHomePageResponseWithContentStruct = object({
|
|
109
|
-
content:
|
|
109
|
+
content: ComponentOrElementStruct
|
|
110
110
|
});
|
|
111
111
|
var OnHomePageResponseWithIdStruct = object({
|
|
112
112
|
id: string()
|
|
@@ -149,4 +149,4 @@ export {
|
|
|
149
149
|
DomainResolutionResponseStruct,
|
|
150
150
|
OnNameLookupResponseStruct
|
|
151
151
|
};
|
|
152
|
-
//# sourceMappingURL=chunk-
|
|
152
|
+
//# sourceMappingURL=chunk-L4DKSTK6.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/handlers.ts"],"sourcesContent":["import type {\n OnCronjobHandler,\n OnHomePageHandler,\n OnInstallHandler,\n OnKeyringRequestHandler,\n OnNameLookupHandler,\n OnRpcRequestHandler,\n OnSignatureHandler,\n OnTransactionHandler,\n OnUpdateHandler,\n OnUserInputHandler,\n} from '@metamask/snaps-sdk';\nimport { ComponentOrElementStruct, SeverityLevel } from '@metamask/snaps-sdk';\nimport {\n assign,\n literal,\n nullable,\n object,\n optional,\n string,\n array,\n size,\n union,\n} from 'superstruct';\n\nimport type { SnapHandler } from './handler-types';\nimport { HandlerType } from './handler-types';\n\nexport type SnapRpcHookArgs = {\n origin: string;\n handler: HandlerType;\n request: Record<string, unknown>;\n};\n\nexport const SNAP_EXPORTS = {\n [HandlerType.OnRpcRequest]: {\n type: HandlerType.OnRpcRequest,\n required: true,\n validator: (snapExport: unknown): snapExport is OnRpcRequestHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnTransaction]: {\n type: HandlerType.OnTransaction,\n required: true,\n validator: (snapExport: unknown): snapExport is OnTransactionHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnCronjob]: {\n type: HandlerType.OnCronjob,\n required: true,\n validator: (snapExport: unknown): snapExport is OnCronjobHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnNameLookup]: {\n type: HandlerType.OnNameLookup,\n required: true,\n validator: (snapExport: unknown): snapExport is OnNameLookupHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnInstall]: {\n type: HandlerType.OnInstall,\n required: false,\n validator: (snapExport: unknown): snapExport is OnInstallHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnUpdate]: {\n type: HandlerType.OnUpdate,\n required: false,\n validator: (snapExport: unknown): snapExport is OnUpdateHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnKeyringRequest]: {\n type: HandlerType.OnKeyringRequest,\n required: true,\n validator: (snapExport: unknown): snapExport is OnKeyringRequestHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnHomePage]: {\n type: HandlerType.OnHomePage,\n required: true,\n validator: (snapExport: unknown): snapExport is OnHomePageHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnSignature]: {\n type: HandlerType.OnSignature,\n required: true,\n validator: (snapExport: unknown): snapExport is OnSignatureHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnUserInput]: {\n type: HandlerType.OnUserInput,\n required: false,\n validator: (snapExport: unknown): snapExport is OnUserInputHandler => {\n return typeof snapExport === 'function';\n },\n },\n} as const;\n\nexport const OnTransactionSeverityResponseStruct = object({\n severity: optional(literal(SeverityLevel.Critical)),\n});\n\nexport const OnTransactionResponseWithIdStruct = assign(\n OnTransactionSeverityResponseStruct,\n object({\n id: string(),\n }),\n);\n\nexport const OnTransactionResponseWithContentStruct = assign(\n OnTransactionSeverityResponseStruct,\n object({\n content: ComponentOrElementStruct,\n }),\n);\n\nexport const OnTransactionResponseStruct = nullable(\n union([\n OnTransactionResponseWithContentStruct,\n OnTransactionResponseWithIdStruct,\n ]),\n);\n\nexport const OnSignatureResponseStruct = OnTransactionResponseStruct;\n\nexport const OnHomePageResponseWithContentStruct = object({\n content: ComponentOrElementStruct,\n});\n\nexport const OnHomePageResponseWithIdStruct = object({\n id: string(),\n});\n\nexport const OnHomePageResponseStruct = union([\n OnHomePageResponseWithContentStruct,\n OnHomePageResponseWithIdStruct,\n]);\n\nexport const AddressResolutionStruct = object({\n protocol: string(),\n resolvedDomain: string(),\n});\n\nexport const DomainResolutionStruct = object({\n protocol: string(),\n resolvedAddress: string(),\n});\n\nexport const AddressResolutionResponseStruct = object({\n resolvedDomains: size(array(AddressResolutionStruct), 1, Infinity),\n});\n\nexport const DomainResolutionResponseStruct = object({\n resolvedAddresses: size(array(DomainResolutionStruct), 1, Infinity),\n});\n\nexport const OnNameLookupResponseStruct = nullable(\n union([AddressResolutionResponseStruct, DomainResolutionResponseStruct]),\n);\n\n/**\n * Utility type for getting the handler function type from a handler type.\n */\nexport type HandlerFunction<Type extends SnapHandler> =\n Type['validator'] extends (snapExport: unknown) => snapExport is infer Handler\n ? Handler\n : never;\n\n/**\n * All the function-based handlers that a snap can implement.\n */\nexport type SnapFunctionExports = {\n [Key in keyof typeof SNAP_EXPORTS]?: HandlerFunction<\n (typeof SNAP_EXPORTS)[Key]\n >;\n};\n\n/**\n * All handlers that a snap can implement.\n */\nexport type SnapExports = SnapFunctionExports;\n"],"mappings":";AAYA,SAAS,0BAA0B,qBAAqB;AACxD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;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{ NodeType } from "@metamask/snaps-sdk";
|
|
3
|
+
import {
|
|
4
|
+
Italic,
|
|
5
|
+
Link,
|
|
6
|
+
Bold,
|
|
7
|
+
Row,
|
|
8
|
+
Text,
|
|
9
|
+
Field,
|
|
10
|
+
Image,
|
|
11
|
+
Input,
|
|
12
|
+
Heading,
|
|
13
|
+
Form,
|
|
14
|
+
Divider,
|
|
15
|
+
Spinner,
|
|
16
|
+
Copyable,
|
|
17
|
+
Box,
|
|
18
|
+
Button,
|
|
19
|
+
Address
|
|
20
|
+
} from "@metamask/snaps-sdk/jsx";
|
|
21
|
+
import {
|
|
22
|
+
assert,
|
|
23
|
+
assertExhaustive,
|
|
24
|
+
AssertionError,
|
|
25
|
+
hasProperty,
|
|
26
|
+
isPlainObject
|
|
27
|
+
} from "@metamask/utils";
|
|
28
|
+
import { lexer, walkTokens } from "marked";
|
|
29
|
+
import { jsx } from "@metamask/snaps-sdk/jsx-runtime";
|
|
30
|
+
var MAX_TEXT_LENGTH = 5e4;
|
|
31
|
+
var ALLOWED_PROTOCOLS = ["https:", "mailto:"];
|
|
32
|
+
function getButtonVariant(variant) {
|
|
33
|
+
switch (variant) {
|
|
34
|
+
case "primary":
|
|
35
|
+
return "primary";
|
|
36
|
+
case "secondary":
|
|
37
|
+
return "destructive";
|
|
38
|
+
default:
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function getChildren(elements) {
|
|
43
|
+
if (elements.length === 1) {
|
|
44
|
+
return elements[0];
|
|
45
|
+
}
|
|
46
|
+
return elements;
|
|
47
|
+
}
|
|
48
|
+
function getLinkText(token) {
|
|
49
|
+
if (token.tokens && token.tokens.length > 0) {
|
|
50
|
+
return getChildren(token.tokens.flatMap(getTextChildFromToken));
|
|
51
|
+
}
|
|
52
|
+
return token.href;
|
|
53
|
+
}
|
|
54
|
+
function getTextChildFromTokens(tokens) {
|
|
55
|
+
return getChildren(tokens.flatMap(getTextChildFromToken));
|
|
56
|
+
}
|
|
57
|
+
function getTextChildFromToken(token) {
|
|
58
|
+
switch (token.type) {
|
|
59
|
+
case "link": {
|
|
60
|
+
return /* @__PURE__ */ jsx(Link, { href: token.href, children: getLinkText(token) });
|
|
61
|
+
}
|
|
62
|
+
case "text":
|
|
63
|
+
return token.text;
|
|
64
|
+
case "strong":
|
|
65
|
+
return /* @__PURE__ */ jsx(Bold, { children: getTextChildFromTokens(
|
|
66
|
+
// Due to the way `marked` is typed, `token.tokens` can be
|
|
67
|
+
// `undefined`, but it's a required field of `Tokens.Bold`, so we
|
|
68
|
+
// can safely cast it to `Token[]`.
|
|
69
|
+
token.tokens
|
|
70
|
+
) });
|
|
71
|
+
case "em":
|
|
72
|
+
return /* @__PURE__ */ jsx(Italic, { children: getTextChildFromTokens(
|
|
73
|
+
// Due to the way `marked` is typed, `token.tokens` can be
|
|
74
|
+
// `undefined`, but it's a required field of `Tokens.Bold`, so we
|
|
75
|
+
// can safely cast it to `Token[]`.
|
|
76
|
+
token.tokens
|
|
77
|
+
) });
|
|
78
|
+
default:
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function getTextChildren(value) {
|
|
83
|
+
const rootTokens = lexer(value, { gfm: false });
|
|
84
|
+
const children = [];
|
|
85
|
+
walkTokens(rootTokens, (token) => {
|
|
86
|
+
if (token.type === "paragraph") {
|
|
87
|
+
if (children.length > 0) {
|
|
88
|
+
children.push("\n\n");
|
|
89
|
+
}
|
|
90
|
+
const { tokens } = token;
|
|
91
|
+
children.push(...tokens.flatMap(getTextChildFromToken));
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return children.filter((child) => child !== null);
|
|
95
|
+
}
|
|
96
|
+
function validateComponentTextSize(component) {
|
|
97
|
+
const textSize = getTotalTextLength(component);
|
|
98
|
+
assert(
|
|
99
|
+
textSize <= MAX_TEXT_LENGTH,
|
|
100
|
+
`The text in a Snap UI may not be larger than ${MAX_TEXT_LENGTH / 1e3} kB.`
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
function getJsxElementFromComponent(legacyComponent) {
|
|
104
|
+
validateComponentTextSize(legacyComponent);
|
|
105
|
+
function getElement(component) {
|
|
106
|
+
switch (component.type) {
|
|
107
|
+
case NodeType.Address:
|
|
108
|
+
return /* @__PURE__ */ jsx(Address, { address: component.value });
|
|
109
|
+
case NodeType.Button:
|
|
110
|
+
return /* @__PURE__ */ jsx(
|
|
111
|
+
Button,
|
|
112
|
+
{
|
|
113
|
+
name: component.name,
|
|
114
|
+
variant: getButtonVariant(component.variant),
|
|
115
|
+
type: component.buttonType,
|
|
116
|
+
children: component.value
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
case NodeType.Copyable:
|
|
120
|
+
return /* @__PURE__ */ jsx(Copyable, { value: component.value, sensitive: component.sensitive });
|
|
121
|
+
case NodeType.Divider:
|
|
122
|
+
return /* @__PURE__ */ jsx(Divider, {});
|
|
123
|
+
case NodeType.Form:
|
|
124
|
+
return /* @__PURE__ */ jsx(Form, { name: component.name, children: getChildren(component.children.map(getElement)) });
|
|
125
|
+
case NodeType.Heading:
|
|
126
|
+
return /* @__PURE__ */ jsx(Heading, { children: component.value });
|
|
127
|
+
case NodeType.Image:
|
|
128
|
+
return /* @__PURE__ */ jsx(Image, { src: component.value });
|
|
129
|
+
case NodeType.Input:
|
|
130
|
+
return /* @__PURE__ */ jsx(Field, { label: component.label, error: component.error, children: /* @__PURE__ */ jsx(
|
|
131
|
+
Input,
|
|
132
|
+
{
|
|
133
|
+
name: component.name,
|
|
134
|
+
type: component.inputType,
|
|
135
|
+
value: component.value,
|
|
136
|
+
placeholder: component.placeholder
|
|
137
|
+
}
|
|
138
|
+
) });
|
|
139
|
+
case NodeType.Panel:
|
|
140
|
+
return /* @__PURE__ */ jsx(Box, { children: getChildren(component.children.map(getElement)) });
|
|
141
|
+
case NodeType.Row:
|
|
142
|
+
return /* @__PURE__ */ jsx(Row, { label: component.label, children: getElement(component.value) });
|
|
143
|
+
case NodeType.Spinner:
|
|
144
|
+
return /* @__PURE__ */ jsx(Spinner, {});
|
|
145
|
+
case NodeType.Text:
|
|
146
|
+
return /* @__PURE__ */ jsx(Text, { children: getChildren(getTextChildren(component.value)) });
|
|
147
|
+
default:
|
|
148
|
+
return assertExhaustive(component);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return getElement(legacyComponent);
|
|
152
|
+
}
|
|
153
|
+
function getMarkdownLinks(text) {
|
|
154
|
+
const tokens = lexer(text, { gfm: false });
|
|
155
|
+
const links = [];
|
|
156
|
+
walkTokens(tokens, (token) => {
|
|
157
|
+
if (token.type === "link") {
|
|
158
|
+
links.push(token);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return links;
|
|
162
|
+
}
|
|
163
|
+
function validateLink(link, isOnPhishingList) {
|
|
164
|
+
try {
|
|
165
|
+
const url = new URL(link);
|
|
166
|
+
assert(
|
|
167
|
+
ALLOWED_PROTOCOLS.includes(url.protocol),
|
|
168
|
+
`Protocol must be one of: ${ALLOWED_PROTOCOLS.join(", ")}.`
|
|
169
|
+
);
|
|
170
|
+
const hostname = url.protocol === "mailto:" ? url.pathname.split("@")[1] : url.hostname;
|
|
171
|
+
assert(!isOnPhishingList(hostname), "The specified URL is not allowed.");
|
|
172
|
+
} catch (error) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`Invalid URL: ${error instanceof AssertionError ? error.message : "Unable to parse URL."}`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function validateTextLinks(text, isOnPhishingList) {
|
|
179
|
+
const links = getMarkdownLinks(text);
|
|
180
|
+
for (const link of links) {
|
|
181
|
+
validateLink(link.href, isOnPhishingList);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function validateJsxLinks(node, isOnPhishingList) {
|
|
185
|
+
walkJsx(node, (childNode) => {
|
|
186
|
+
if (childNode.type !== "Link") {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
validateLink(childNode.props.href, isOnPhishingList);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function getTotalTextLength(component) {
|
|
193
|
+
const { type } = component;
|
|
194
|
+
switch (type) {
|
|
195
|
+
case NodeType.Panel:
|
|
196
|
+
return component.children.reduce(
|
|
197
|
+
// This is a bug in TypeScript: https://github.com/microsoft/TypeScript/issues/48313
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
199
|
+
(sum, node) => sum + getTotalTextLength(node),
|
|
200
|
+
0
|
|
201
|
+
);
|
|
202
|
+
case NodeType.Row:
|
|
203
|
+
return getTotalTextLength(component.value);
|
|
204
|
+
case NodeType.Text:
|
|
205
|
+
return component.value.length;
|
|
206
|
+
default:
|
|
207
|
+
return 0;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function hasChildren(element) {
|
|
211
|
+
return hasProperty(element.props, "children");
|
|
212
|
+
}
|
|
213
|
+
function getJsxChildren(element) {
|
|
214
|
+
if (hasChildren(element)) {
|
|
215
|
+
if (Array.isArray(element.props.children)) {
|
|
216
|
+
return element.props.children.filter(Boolean);
|
|
217
|
+
}
|
|
218
|
+
if (element.props.children) {
|
|
219
|
+
return [element.props.children];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
function walkJsx(node, callback) {
|
|
225
|
+
if (Array.isArray(node)) {
|
|
226
|
+
for (const child of node) {
|
|
227
|
+
const childResult = walkJsx(child, callback);
|
|
228
|
+
if (childResult !== void 0) {
|
|
229
|
+
return childResult;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return void 0;
|
|
233
|
+
}
|
|
234
|
+
const result = callback(node);
|
|
235
|
+
if (result !== void 0) {
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
if (hasProperty(node, "props") && isPlainObject(node.props) && hasProperty(node.props, "children")) {
|
|
239
|
+
const children = getJsxChildren(node);
|
|
240
|
+
for (const child of children) {
|
|
241
|
+
if (isPlainObject(child)) {
|
|
242
|
+
const childResult = walkJsx(child, callback);
|
|
243
|
+
if (childResult !== void 0) {
|
|
244
|
+
return childResult;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return void 0;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export {
|
|
253
|
+
getTextChildren,
|
|
254
|
+
getJsxElementFromComponent,
|
|
255
|
+
validateTextLinks,
|
|
256
|
+
validateJsxLinks,
|
|
257
|
+
getTotalTextLength,
|
|
258
|
+
hasChildren,
|
|
259
|
+
getJsxChildren,
|
|
260
|
+
walkJsx
|
|
261
|
+
};
|
|
262
|
+
//# sourceMappingURL=chunk-YACE3IOJ.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 FieldElement,\n ItalicChildren,\n JSXElement,\n MaybeArray,\n RowChildren,\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(value: string) {\n const rootTokens = lexer(value, { gfm: false });\n const children: TextChildren = [];\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 children.push(...tokens.flatMap(getTextChildFromToken));\n }\n });\n\n return children.filter((child) => child !== null);\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)) as FieldElement[]}\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}>\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: MaybeArray<JSXElement | string> };\n} {\n return hasProperty(element.props, 'children');\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(Boolean);\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 * @returns The result of the callback, if any.\n */\nexport function walkJsx<Value>(\n node: JSXElement | JSXElement[],\n callback: (node: JSXElement) => Value | undefined,\n): Value | undefined {\n if (Array.isArray(node)) {\n for (const child of node) {\n const childResult = walkJsx(child as JSXElement, callback);\n if (childResult !== undefined) {\n return childResult;\n }\n }\n\n return undefined;\n }\n\n const result = callback(node);\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);\n if (childResult !== undefined) {\n return childResult;\n }\n }\n }\n }\n\n return undefined;\n}\n"],"mappings":";AACA,SAAS,gBAAgB;AAUzB;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,YAAkB,UAAkB;AAC3C,MAAI,SAAS,WAAW,GAAG;AACzB,WAAO,SAAS,CAAC;AAAA,EACnB;AAEA,SAAO;AACT;AAQA,SAAS,YAAY,OAAqC;AACxD,MAAI,MAAM,UAAU,MAAM,OAAO,SAAS,GAAG;AAC3C,WAAO,YAAY,MAAM,OAAO,QAAQ,qBAAqB,CAAC;AAAA,EAChE;AAEA,SAAO,MAAM;AACf;AAQA,SAAS,uBAAuB,QAAiB;AAC/C,SAAO,YAAY,OAAO,QAAQ,qBAAqB,CAAC;AAC1D;AAQA,SAAS,sBAAsB,OAA4B;AACzD,UAAQ,MAAM,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O,SAAS,eAAe,SAA8C;AAC3E,MAAI,YAAY,OAAO,GAAG;AACxB,QAAI,MAAM,QAAQ,QAAQ,MAAM,QAAQ,GAAG;AAGzC,aAAO,QAAQ,MAAM,SAAS,OAAO,OAAO;AAAA,IAC9C;AAEA,QAAI,QAAQ,MAAM,UAAU;AAC1B,aAAO,CAAC,QAAQ,MAAM,QAAQ;AAAA,IAChC;AAAA,EACF;AAEA,SAAO,CAAC;AACV;AASO,SAAS,QACd,MACA,UACmB;AACnB,MAAI,MAAM,QAAQ,IAAI,GAAG;AACvB,eAAW,SAAS,MAAM;AACxB,YAAM,cAAc,QAAQ,OAAqB,QAAQ;AACzD,UAAI,gBAAgB,QAAW;AAC7B,eAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,SAAS,IAAI;AAC5B,MAAI,WAAW,QAAW;AACxB,WAAO;AAAA,EACT;AAEA,MACE,YAAY,MAAM,OAAO,KACzB,cAAc,KAAK,KAAK,KACxB,YAAY,KAAK,OAAO,UAAU,GAClC;AACA,UAAM,WAAW,eAAe,IAAI;AACpC,eAAW,SAAS,UAAU;AAC5B,UAAI,cAAc,KAAK,GAAG;AACxB,cAAM,cAAc,QAAQ,OAAO,QAAQ;AAC3C,YAAI,gBAAgB,QAAW;AAC7B,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/ui.tsx
|
|
2
|
+
var _snapssdk = require('@metamask/snaps-sdk');
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
var _jsx = require('@metamask/snaps-sdk/jsx');
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
var _utils = require('@metamask/utils');
|
|
28
|
+
var _marked = require('marked');
|
|
29
|
+
var _jsxruntime = require('@metamask/snaps-sdk/jsx-runtime');
|
|
30
|
+
var MAX_TEXT_LENGTH = 5e4;
|
|
31
|
+
var ALLOWED_PROTOCOLS = ["https:", "mailto:"];
|
|
32
|
+
function getButtonVariant(variant) {
|
|
33
|
+
switch (variant) {
|
|
34
|
+
case "primary":
|
|
35
|
+
return "primary";
|
|
36
|
+
case "secondary":
|
|
37
|
+
return "destructive";
|
|
38
|
+
default:
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function getChildren(elements) {
|
|
43
|
+
if (elements.length === 1) {
|
|
44
|
+
return elements[0];
|
|
45
|
+
}
|
|
46
|
+
return elements;
|
|
47
|
+
}
|
|
48
|
+
function getLinkText(token) {
|
|
49
|
+
if (token.tokens && token.tokens.length > 0) {
|
|
50
|
+
return getChildren(token.tokens.flatMap(getTextChildFromToken));
|
|
51
|
+
}
|
|
52
|
+
return token.href;
|
|
53
|
+
}
|
|
54
|
+
function getTextChildFromTokens(tokens) {
|
|
55
|
+
return getChildren(tokens.flatMap(getTextChildFromToken));
|
|
56
|
+
}
|
|
57
|
+
function getTextChildFromToken(token) {
|
|
58
|
+
switch (token.type) {
|
|
59
|
+
case "link": {
|
|
60
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Link, { href: token.href, children: getLinkText(token) });
|
|
61
|
+
}
|
|
62
|
+
case "text":
|
|
63
|
+
return token.text;
|
|
64
|
+
case "strong":
|
|
65
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Bold, { children: getTextChildFromTokens(
|
|
66
|
+
// Due to the way `marked` is typed, `token.tokens` can be
|
|
67
|
+
// `undefined`, but it's a required field of `Tokens.Bold`, so we
|
|
68
|
+
// can safely cast it to `Token[]`.
|
|
69
|
+
token.tokens
|
|
70
|
+
) });
|
|
71
|
+
case "em":
|
|
72
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Italic, { children: getTextChildFromTokens(
|
|
73
|
+
// Due to the way `marked` is typed, `token.tokens` can be
|
|
74
|
+
// `undefined`, but it's a required field of `Tokens.Bold`, so we
|
|
75
|
+
// can safely cast it to `Token[]`.
|
|
76
|
+
token.tokens
|
|
77
|
+
) });
|
|
78
|
+
default:
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function getTextChildren(value) {
|
|
83
|
+
const rootTokens = _marked.lexer.call(void 0, value, { gfm: false });
|
|
84
|
+
const children = [];
|
|
85
|
+
_marked.walkTokens.call(void 0, rootTokens, (token) => {
|
|
86
|
+
if (token.type === "paragraph") {
|
|
87
|
+
if (children.length > 0) {
|
|
88
|
+
children.push("\n\n");
|
|
89
|
+
}
|
|
90
|
+
const { tokens } = token;
|
|
91
|
+
children.push(...tokens.flatMap(getTextChildFromToken));
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return children.filter((child) => child !== null);
|
|
95
|
+
}
|
|
96
|
+
function validateComponentTextSize(component) {
|
|
97
|
+
const textSize = getTotalTextLength(component);
|
|
98
|
+
_utils.assert.call(void 0,
|
|
99
|
+
textSize <= MAX_TEXT_LENGTH,
|
|
100
|
+
`The text in a Snap UI may not be larger than ${MAX_TEXT_LENGTH / 1e3} kB.`
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
function getJsxElementFromComponent(legacyComponent) {
|
|
104
|
+
validateComponentTextSize(legacyComponent);
|
|
105
|
+
function getElement(component) {
|
|
106
|
+
switch (component.type) {
|
|
107
|
+
case _snapssdk.NodeType.Address:
|
|
108
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Address, { address: component.value });
|
|
109
|
+
case _snapssdk.NodeType.Button:
|
|
110
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
111
|
+
_jsx.Button,
|
|
112
|
+
{
|
|
113
|
+
name: component.name,
|
|
114
|
+
variant: getButtonVariant(component.variant),
|
|
115
|
+
type: component.buttonType,
|
|
116
|
+
children: component.value
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
case _snapssdk.NodeType.Copyable:
|
|
120
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Copyable, { value: component.value, sensitive: component.sensitive });
|
|
121
|
+
case _snapssdk.NodeType.Divider:
|
|
122
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Divider, {});
|
|
123
|
+
case _snapssdk.NodeType.Form:
|
|
124
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Form, { name: component.name, children: getChildren(component.children.map(getElement)) });
|
|
125
|
+
case _snapssdk.NodeType.Heading:
|
|
126
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Heading, { children: component.value });
|
|
127
|
+
case _snapssdk.NodeType.Image:
|
|
128
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Image, { src: component.value });
|
|
129
|
+
case _snapssdk.NodeType.Input:
|
|
130
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Field, { label: component.label, error: component.error, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
131
|
+
_jsx.Input,
|
|
132
|
+
{
|
|
133
|
+
name: component.name,
|
|
134
|
+
type: component.inputType,
|
|
135
|
+
value: component.value,
|
|
136
|
+
placeholder: component.placeholder
|
|
137
|
+
}
|
|
138
|
+
) });
|
|
139
|
+
case _snapssdk.NodeType.Panel:
|
|
140
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Box, { children: getChildren(component.children.map(getElement)) });
|
|
141
|
+
case _snapssdk.NodeType.Row:
|
|
142
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Row, { label: component.label, children: getElement(component.value) });
|
|
143
|
+
case _snapssdk.NodeType.Spinner:
|
|
144
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Spinner, {});
|
|
145
|
+
case _snapssdk.NodeType.Text:
|
|
146
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsx.Text, { children: getChildren(getTextChildren(component.value)) });
|
|
147
|
+
default:
|
|
148
|
+
return _utils.assertExhaustive.call(void 0, component);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return getElement(legacyComponent);
|
|
152
|
+
}
|
|
153
|
+
function getMarkdownLinks(text) {
|
|
154
|
+
const tokens = _marked.lexer.call(void 0, text, { gfm: false });
|
|
155
|
+
const links = [];
|
|
156
|
+
_marked.walkTokens.call(void 0, tokens, (token) => {
|
|
157
|
+
if (token.type === "link") {
|
|
158
|
+
links.push(token);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return links;
|
|
162
|
+
}
|
|
163
|
+
function validateLink(link, isOnPhishingList) {
|
|
164
|
+
try {
|
|
165
|
+
const url = new URL(link);
|
|
166
|
+
_utils.assert.call(void 0,
|
|
167
|
+
ALLOWED_PROTOCOLS.includes(url.protocol),
|
|
168
|
+
`Protocol must be one of: ${ALLOWED_PROTOCOLS.join(", ")}.`
|
|
169
|
+
);
|
|
170
|
+
const hostname = url.protocol === "mailto:" ? url.pathname.split("@")[1] : url.hostname;
|
|
171
|
+
_utils.assert.call(void 0, !isOnPhishingList(hostname), "The specified URL is not allowed.");
|
|
172
|
+
} catch (error) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`Invalid URL: ${error instanceof _utils.AssertionError ? error.message : "Unable to parse URL."}`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function validateTextLinks(text, isOnPhishingList) {
|
|
179
|
+
const links = getMarkdownLinks(text);
|
|
180
|
+
for (const link of links) {
|
|
181
|
+
validateLink(link.href, isOnPhishingList);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function validateJsxLinks(node, isOnPhishingList) {
|
|
185
|
+
walkJsx(node, (childNode) => {
|
|
186
|
+
if (childNode.type !== "Link") {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
validateLink(childNode.props.href, isOnPhishingList);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function getTotalTextLength(component) {
|
|
193
|
+
const { type } = component;
|
|
194
|
+
switch (type) {
|
|
195
|
+
case _snapssdk.NodeType.Panel:
|
|
196
|
+
return component.children.reduce(
|
|
197
|
+
// This is a bug in TypeScript: https://github.com/microsoft/TypeScript/issues/48313
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
199
|
+
(sum, node) => sum + getTotalTextLength(node),
|
|
200
|
+
0
|
|
201
|
+
);
|
|
202
|
+
case _snapssdk.NodeType.Row:
|
|
203
|
+
return getTotalTextLength(component.value);
|
|
204
|
+
case _snapssdk.NodeType.Text:
|
|
205
|
+
return component.value.length;
|
|
206
|
+
default:
|
|
207
|
+
return 0;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function hasChildren(element) {
|
|
211
|
+
return _utils.hasProperty.call(void 0, element.props, "children");
|
|
212
|
+
}
|
|
213
|
+
function getJsxChildren(element) {
|
|
214
|
+
if (hasChildren(element)) {
|
|
215
|
+
if (Array.isArray(element.props.children)) {
|
|
216
|
+
return element.props.children.filter(Boolean);
|
|
217
|
+
}
|
|
218
|
+
if (element.props.children) {
|
|
219
|
+
return [element.props.children];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
function walkJsx(node, callback) {
|
|
225
|
+
if (Array.isArray(node)) {
|
|
226
|
+
for (const child of node) {
|
|
227
|
+
const childResult = walkJsx(child, callback);
|
|
228
|
+
if (childResult !== void 0) {
|
|
229
|
+
return childResult;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return void 0;
|
|
233
|
+
}
|
|
234
|
+
const result = callback(node);
|
|
235
|
+
if (result !== void 0) {
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
if (_utils.hasProperty.call(void 0, node, "props") && _utils.isPlainObject.call(void 0, node.props) && _utils.hasProperty.call(void 0, node.props, "children")) {
|
|
239
|
+
const children = getJsxChildren(node);
|
|
240
|
+
for (const child of children) {
|
|
241
|
+
if (_utils.isPlainObject.call(void 0, child)) {
|
|
242
|
+
const childResult = walkJsx(child, callback);
|
|
243
|
+
if (childResult !== void 0) {
|
|
244
|
+
return childResult;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return void 0;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
exports.getTextChildren = getTextChildren; exports.getJsxElementFromComponent = getJsxElementFromComponent; exports.validateTextLinks = validateTextLinks; exports.validateJsxLinks = validateJsxLinks; exports.getTotalTextLength = getTotalTextLength; exports.hasChildren = hasChildren; exports.getJsxChildren = getJsxChildren; exports.walkJsx = walkJsx;
|
|
262
|
+
//# sourceMappingURL=chunk-YAKDZ5LP.js.map
|