mcp-browser-dev-tools 0.1.0 → 0.2.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.
@@ -0,0 +1,260 @@
1
+ import vm from "node:vm";
2
+
3
+ // Firefox evaluates a classic script, where top-level await is a syntax error.
4
+ // Like a browser console, code that needs it runs inside an async function
5
+ // that returns the value of its last expression statement. Chromium's REPL
6
+ // mode handles this natively, so only the Firefox client uses it.
7
+
8
+ const AsyncFunction = (async () => {}).constructor;
9
+
10
+ function compiles(compile) {
11
+ try {
12
+ compile();
13
+ return true;
14
+ } catch {
15
+ return false;
16
+ }
17
+ }
18
+
19
+ const REGEX_AFTER_WORDS = new Set([
20
+ "return",
21
+ "typeof",
22
+ "instanceof",
23
+ "in",
24
+ "of",
25
+ "new",
26
+ "delete",
27
+ "void",
28
+ "throw",
29
+ "case",
30
+ "do",
31
+ "else",
32
+ "await",
33
+ "yield",
34
+ ]);
35
+
36
+ // Code starting with one of these continues what comes before it instead of
37
+ // starting a statement, so the newline or `}` before it is not a statement
38
+ // boundary. `++` and `--` do start a statement after a newline.
39
+ const CONTINUATION =
40
+ /^(?:\+(?!\+)|-(?!-)|\/(?![/*])|[([`*%.,?:&|^<>=]|in\b|instanceof\b)/;
41
+
42
+ // A final statement starting like this is a block or a declaration in a
43
+ // script, not an expression, so it has no value to return.
44
+ const NOT_AN_EXPRESSION =
45
+ /^(?:\{|function\b|class\b|async(?:\s|\/\*[\s\S]*?\*\/|\/\/[^\n]*)+function\b)/;
46
+
47
+ const LEADING_COMMENTS = /^(?:\s+|\/\/[^\n]*|\/\*[\s\S]*?\*\/)+/;
48
+
49
+ function stripLeadingComments(code) {
50
+ return code.replace(LEADING_COMMENTS, "");
51
+ }
52
+
53
+ // Returns the offsets where a top-level statement may end (after a `;` or a
54
+ // block's `}`, or at a newline, outside strings, comments, templates, regex
55
+ // literals, and brackets), and the offset just past the last code that is
56
+ // not a `;`, so trailing semicolons and comments can be dropped.
57
+ export function statementBoundaries(source) {
58
+ const boundaries = [];
59
+ let end = 0;
60
+ // Each entry is "{", "(", "[", or "${" for a template substitution.
61
+ const stack = [];
62
+ let lastSignificant = "";
63
+ let lastWord = "";
64
+ let index = 0;
65
+
66
+ const regexAllowed = () =>
67
+ lastSignificant === "" ||
68
+ "(,=:[!&|?{};+-*%<>~^".includes(lastSignificant) ||
69
+ REGEX_AFTER_WORDS.has(lastWord);
70
+
71
+ const skipTemplate = () => {
72
+ // index is just past the opening backtick or a closing substitution.
73
+ while (index < source.length) {
74
+ const char = source[index];
75
+ if (char === "\\") {
76
+ index += 2;
77
+ } else if (char === "`") {
78
+ index += 1;
79
+ return;
80
+ } else if (char === "$" && source[index + 1] === "{") {
81
+ stack.push("${");
82
+ index += 2;
83
+ return;
84
+ } else {
85
+ index += 1;
86
+ }
87
+ }
88
+ };
89
+
90
+ while (index < source.length) {
91
+ const char = source[index];
92
+ const next = source[index + 1];
93
+
94
+ if (char === "\n") {
95
+ if (stack.length === 0) {
96
+ boundaries.push(index);
97
+ }
98
+ index += 1;
99
+ continue;
100
+ }
101
+ if (/\s/.test(char)) {
102
+ index += 1;
103
+ continue;
104
+ }
105
+ if (char === "/" && next === "/") {
106
+ const end = source.indexOf("\n", index);
107
+ index = end === -1 ? source.length : end;
108
+ continue;
109
+ }
110
+ if (char === "/" && next === "*") {
111
+ const end = source.indexOf("*/", index + 2);
112
+ index = end === -1 ? source.length : end + 2;
113
+ continue;
114
+ }
115
+ if (char === "'" || char === '"') {
116
+ index += 1;
117
+ while (index < source.length && source[index] !== char) {
118
+ index += source[index] === "\\" ? 2 : 1;
119
+ }
120
+ index += 1;
121
+ lastSignificant = char;
122
+ lastWord = "";
123
+ end = index;
124
+ continue;
125
+ }
126
+ if (char === "`") {
127
+ index += 1;
128
+ skipTemplate();
129
+ lastSignificant = "`";
130
+ lastWord = "";
131
+ end = index;
132
+ continue;
133
+ }
134
+ if (char === "/" && regexAllowed()) {
135
+ let inClass = false;
136
+ index += 1;
137
+ while (index < source.length && source[index] !== "\n") {
138
+ const current = source[index];
139
+ if (current === "\\") {
140
+ index += 2;
141
+ continue;
142
+ }
143
+ if (current === "[") {
144
+ inClass = true;
145
+ } else if (current === "]") {
146
+ inClass = false;
147
+ } else if (current === "/" && !inClass) {
148
+ break;
149
+ }
150
+ index += 1;
151
+ }
152
+ index += 1;
153
+ while (index < source.length && /[a-z]/i.test(source[index])) {
154
+ index += 1;
155
+ }
156
+ lastSignificant = "/";
157
+ lastWord = "";
158
+ end = index;
159
+ continue;
160
+ }
161
+ if (/[A-Za-z_$0-9]/.test(char)) {
162
+ const start = index;
163
+ while (index < source.length && /[A-Za-z_$0-9]/.test(source[index])) {
164
+ index += 1;
165
+ }
166
+ lastWord = source.slice(start, index);
167
+ lastSignificant = "a";
168
+ end = index;
169
+ continue;
170
+ }
171
+
172
+ if (char === "(" || char === "[" || char === "{") {
173
+ stack.push(char);
174
+ } else if (char === ")" || char === "]") {
175
+ stack.pop();
176
+ } else if (char === "}") {
177
+ if (stack.pop() === "${") {
178
+ index += 1;
179
+ skipTemplate();
180
+ lastSignificant = "`";
181
+ lastWord = "";
182
+ end = index;
183
+ continue;
184
+ }
185
+ if (stack.length === 0) {
186
+ boundaries.push(index + 1);
187
+ }
188
+ } else if (char === ";") {
189
+ if (stack.length === 0) {
190
+ boundaries.push(index + 1);
191
+ }
192
+ lastSignificant = char;
193
+ lastWord = "";
194
+ index += 1;
195
+ continue;
196
+ }
197
+ lastSignificant = char;
198
+ lastWord = "";
199
+ index += 1;
200
+ end = index;
201
+ }
202
+
203
+ return { boundaries, end };
204
+ }
205
+
206
+ function asyncCall(body) {
207
+ return `(async () => {\n${body}\n})()`;
208
+ }
209
+
210
+ // Returns the expression rewritten to run inside an async function, or null
211
+ // when it needs no rewrite: it already runs as a script, it does not use
212
+ // await (a stray top-level return stays an error, as on Chromium), or it
213
+ // does not compile even inside an async function.
214
+ export function wrapTopLevelAwait(expression) {
215
+ if (compiles(() => new vm.Script(expression))) {
216
+ return null;
217
+ }
218
+ if (!compiles(() => new AsyncFunction(expression))) {
219
+ return null;
220
+ }
221
+ if (compiles(() => new Function(expression))) {
222
+ return null;
223
+ }
224
+
225
+ const { boundaries, end } = statementBoundaries(expression);
226
+ const code = expression.slice(0, end);
227
+ const returning = (head, tail) => {
228
+ // The code before the split must compile on its own, so the split falls
229
+ // between top-level statements and never inside an unbraced loop body.
230
+ if (
231
+ NOT_AN_EXPRESSION.test(tail) ||
232
+ (head && !compiles(() => new AsyncFunction(head)))
233
+ ) {
234
+ return null;
235
+ }
236
+ const body = `${head ? `${head}\n` : ""}return (\n${tail}\n);`;
237
+ return compiles(() => new AsyncFunction(body)) ? asyncCall(body) : null;
238
+ };
239
+
240
+ const single = returning("", stripLeadingComments(code));
241
+ if (single) {
242
+ return single;
243
+ }
244
+ for (const boundary of boundaries.filter((index) => index < end).reverse()) {
245
+ const tail = stripLeadingComments(code.slice(boundary));
246
+ // After a newline or a block, a tail that continues the code before it
247
+ // is not a statement of its own.
248
+ if (!tail || (code[boundary - 1] !== ";" && CONTINUATION.test(tail))) {
249
+ continue;
250
+ }
251
+ const wrapped = returning(code.slice(0, boundary), tail);
252
+ if (wrapped) {
253
+ return wrapped;
254
+ }
255
+ }
256
+
257
+ // The last statement is not an expression, such as a declaration, so there
258
+ // is no value to return.
259
+ return asyncCall(expression);
260
+ }