wrangler 0.0.0-ece06ea → 0.0.0-ed4f2ef
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/README.md +3 -3
- package/bin/wrangler.js +12 -3
- package/miniflare-dist/index.mjs +79 -0
- package/miniflare-dist/index.mjs.map +7 -0
- package/package.json +49 -22
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +223 -0
- package/pages/functions/filepath-routing.ts +181 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +151 -0
- package/pages/functions/template-worker.ts +130 -0
- package/src/__tests__/configuration.test.ts +2874 -0
- package/src/__tests__/dev.test.tsx +588 -0
- package/src/__tests__/guess-worker-format.test.ts +83 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +48 -0
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/helpers/mock-cfetch.ts +207 -0
- package/src/__tests__/helpers/mock-console.ts +70 -0
- package/src/__tests__/helpers/mock-dialogs.ts +65 -0
- package/src/__tests__/helpers/mock-kv.ts +33 -0
- package/src/__tests__/helpers/mock-oauth-flow.ts +178 -0
- package/src/__tests__/helpers/mock-stdin.ts +103 -0
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +32 -0
- package/src/__tests__/helpers/run-wrangler.ts +16 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/https-options.test.ts +127 -0
- package/src/__tests__/index.test.ts +953 -119
- package/src/__tests__/jest.setup.ts +74 -0
- package/src/__tests__/kv.test.ts +1380 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/pages.test.ts +180 -0
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +3946 -0
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +425 -0
- package/src/__tests__/sentry.test.ts +146 -0
- package/src/__tests__/tail.test.ts +638 -0
- package/src/__tests__/user.test.ts +83 -0
- package/src/__tests__/whoami.test.tsx +95 -0
- package/src/bundle.ts +155 -0
- package/src/cfetch/index.ts +104 -0
- package/src/cfetch/internal.ts +139 -0
- package/src/cli.ts +18 -7
- package/src/config/README.md +107 -0
- package/src/config/config.ts +229 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +371 -0
- package/src/config/index.ts +62 -0
- package/src/config/validation-helpers.ts +557 -0
- package/src/config/validation.ts +1240 -0
- package/src/create-worker-preview.ts +173 -0
- package/src/create-worker-upload-form.ts +240 -0
- package/src/dev/dev.tsx +413 -0
- package/src/dev/local.tsx +287 -0
- package/src/dev/remote.tsx +209 -0
- package/src/dev/use-esbuild.ts +109 -0
- package/src/dialogs.tsx +6 -14
- package/src/entry.ts +256 -0
- package/src/environment-variables.ts +33 -0
- package/src/errors.ts +11 -0
- package/src/https-options.ts +122 -0
- package/src/index.tsx +1503 -959
- package/src/inspect.ts +624 -0
- package/src/intl-polyfill.d.ts +139 -0
- package/src/kv.ts +319 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +38 -0
- package/src/module-collection.ts +257 -0
- package/src/open-in-browser.ts +25 -0
- package/src/package-manager.ts +126 -0
- package/src/pages.tsx +1074 -212
- package/src/parse.ts +183 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +455 -0
- package/src/publish.ts +326 -295
- package/src/r2.ts +50 -0
- package/src/reporting.ts +131 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +224 -59
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +62 -0
- package/src/user.tsx +340 -203
- package/src/whoami.tsx +69 -0
- package/src/worker.ts +172 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +136988 -131174
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/form_data.ts +0 -158
- package/src/api/inspect.ts +0 -441
- package/src/api/preview.ts +0 -123
- package/src/api/worker.ts +0 -161
- package/src/cfetch.ts +0 -72
- package/src/config.ts +0 -124
- package/src/dev.tsx +0 -736
- package/src/kv.tsx +0 -211
- package/src/tail.tsx +0 -71
- package/src/util/fetch.ts +0 -74
package/src/inspect.ts
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { URL } from "node:url";
|
|
4
|
+
|
|
5
|
+
import { useEffect, useRef, useState } from "react";
|
|
6
|
+
import WebSocket, { WebSocketServer } from "ws";
|
|
7
|
+
import { version } from "../package.json";
|
|
8
|
+
import { waitForPortToBeAvailable } from "./proxy";
|
|
9
|
+
import type Protocol from "devtools-protocol";
|
|
10
|
+
import type { IncomingMessage, Server, ServerResponse } from "node:http";
|
|
11
|
+
import type { MessageEvent } from "ws";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `useInspector` is a hook for debugging Workers applications
|
|
15
|
+
* when using `wrangler dev`.
|
|
16
|
+
*
|
|
17
|
+
* When we start a session with `wrangler dev`, the Workers platform
|
|
18
|
+
* also exposes a debugging websocket that implements the DevTools
|
|
19
|
+
* Protocol. While we could just start up DevTools and connect to this
|
|
20
|
+
* URL, that URL changes every time we make a change to the
|
|
21
|
+
* worker, or when the session expires. Instead, we start up a proxy
|
|
22
|
+
* server locally that acts as a bridge between the remote DevTools
|
|
23
|
+
* server and the local DevTools instance. So whenever the URL changes,
|
|
24
|
+
* we can can silently connect to it and keep the local DevTools instance
|
|
25
|
+
* up to date. Further, we also intercept these messages and selectively
|
|
26
|
+
* log them directly to the terminal (namely, calls to `console.<x>`,
|
|
27
|
+
* and exceptions)
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* TODO:
|
|
32
|
+
* - clear devtools whenever we save changes to the worker
|
|
33
|
+
* - clear devtools when we switch between local/remote modes
|
|
34
|
+
* - handle more methods from console
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
interface InspectorProps {
|
|
38
|
+
/**
|
|
39
|
+
* The port that the local proxy server should listen on.
|
|
40
|
+
*/
|
|
41
|
+
port: number;
|
|
42
|
+
/**
|
|
43
|
+
* The websocket URL exposed by Workers that the inspector should connect to.
|
|
44
|
+
*/
|
|
45
|
+
inspectorUrl: undefined | string;
|
|
46
|
+
/**
|
|
47
|
+
* Whether console statements and exceptions should be logged to the terminal.
|
|
48
|
+
* (We don't log them in local mode because they're already getting
|
|
49
|
+
* logged to the terminal by nature of them actually running in node locally.)
|
|
50
|
+
*/
|
|
51
|
+
logToTerminal: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default function useInspector(props: InspectorProps) {
|
|
55
|
+
/** A unique ID for this session. */
|
|
56
|
+
const inspectorIdRef = useRef(randomId());
|
|
57
|
+
|
|
58
|
+
/** The websocket from the devtools instance. */
|
|
59
|
+
const [localWebSocket, setLocalWebSocket] = useState<WebSocket>();
|
|
60
|
+
/** The websocket from the edge */
|
|
61
|
+
const [remoteWebSocket, setRemoteWebSocket] = useState<WebSocket>();
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The local proxy server that acts as the bridge between
|
|
65
|
+
* the remote websocket and the local DevTools instance.
|
|
66
|
+
*/
|
|
67
|
+
const serverRef = useRef<Server>();
|
|
68
|
+
if (serverRef.current === undefined) {
|
|
69
|
+
serverRef.current = createServer(
|
|
70
|
+
(req: IncomingMessage, res: ServerResponse) => {
|
|
71
|
+
switch (req.url) {
|
|
72
|
+
// We implement a couple of well known end points
|
|
73
|
+
// that are queried for metadata by chrome://inspect
|
|
74
|
+
case "/json/version":
|
|
75
|
+
res.setHeader("Content-Type", "application/json");
|
|
76
|
+
res.end(
|
|
77
|
+
JSON.stringify({
|
|
78
|
+
Browser: `wrangler/v${version}`,
|
|
79
|
+
// TODO: (someday): The DevTools protocol should match that of Edge Worker.
|
|
80
|
+
// This could be exposed by the preview API.
|
|
81
|
+
"Protocol-Version": "1.3",
|
|
82
|
+
})
|
|
83
|
+
);
|
|
84
|
+
return;
|
|
85
|
+
case "/json":
|
|
86
|
+
case "/json/list":
|
|
87
|
+
{
|
|
88
|
+
res.setHeader("Content-Type", "application/json");
|
|
89
|
+
const localHost = `localhost:${props.port}/ws`;
|
|
90
|
+
const devtoolsFrontendUrl = `devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=${localHost}`;
|
|
91
|
+
const devtoolsFrontendUrlCompat = `devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=${localHost}`;
|
|
92
|
+
res.end(
|
|
93
|
+
JSON.stringify([
|
|
94
|
+
{
|
|
95
|
+
id: inspectorIdRef.current,
|
|
96
|
+
type: "node",
|
|
97
|
+
description: "workers",
|
|
98
|
+
webSocketDebuggerUrl: `ws://${localHost}`,
|
|
99
|
+
devtoolsFrontendUrl,
|
|
100
|
+
devtoolsFrontendUrlCompat,
|
|
101
|
+
// Below are fields that are visible in the DevTools UI.
|
|
102
|
+
title: "Cloudflare Worker",
|
|
103
|
+
faviconUrl: "https://workers.cloudflare.com/favicon.ico",
|
|
104
|
+
url:
|
|
105
|
+
"https://" +
|
|
106
|
+
(remoteWebSocket
|
|
107
|
+
? new URL(remoteWebSocket.url).host
|
|
108
|
+
: "workers.dev"),
|
|
109
|
+
},
|
|
110
|
+
])
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
default:
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const server = serverRef.current;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The websocket server that runs on top of the proxy server.
|
|
124
|
+
*/
|
|
125
|
+
const wsServerRef = useRef<WebSocketServer>();
|
|
126
|
+
if (wsServerRef.current === undefined) {
|
|
127
|
+
wsServerRef.current = new WebSocketServer({
|
|
128
|
+
server,
|
|
129
|
+
clientTracking: true,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const wsServer = wsServerRef.current;
|
|
133
|
+
|
|
134
|
+
wsServer.on("connection", (ws: WebSocket) => {
|
|
135
|
+
if (wsServer.clients.size > 1) {
|
|
136
|
+
/** We only want to have one active Devtools instance at a time. */
|
|
137
|
+
console.error(
|
|
138
|
+
"Tried to open a new devtools window when a previous one was already open."
|
|
139
|
+
);
|
|
140
|
+
ws.close(1013, "Too many clients; only one can be connected at a time");
|
|
141
|
+
} else {
|
|
142
|
+
// As promised, save the created websocket in a state hook
|
|
143
|
+
setLocalWebSocket(ws);
|
|
144
|
+
|
|
145
|
+
ws.addEventListener("close", () => {
|
|
146
|
+
// And and cleanup when devtools closes
|
|
147
|
+
setLocalWebSocket(undefined);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* We start and stop the server in an effect to take advantage
|
|
154
|
+
* of the component lifecycle. Convenient.
|
|
155
|
+
*/
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
const abortController = new AbortController();
|
|
158
|
+
async function startInspectorProxy() {
|
|
159
|
+
await waitForPortToBeAvailable(props.port, {
|
|
160
|
+
retryPeriod: 200,
|
|
161
|
+
timeout: 2000,
|
|
162
|
+
abortSignal: abortController.signal,
|
|
163
|
+
});
|
|
164
|
+
server.listen(props.port);
|
|
165
|
+
}
|
|
166
|
+
startInspectorProxy().catch((err) => {
|
|
167
|
+
if ((err as { code: string }).code !== "ABORT_ERR") {
|
|
168
|
+
console.error(err);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
return () => {
|
|
172
|
+
server.close();
|
|
173
|
+
// Also disconnect any open websockets/devtools connections
|
|
174
|
+
wsServer.clients.forEach((ws) => ws.close());
|
|
175
|
+
wsServer.close();
|
|
176
|
+
abortController.abort();
|
|
177
|
+
};
|
|
178
|
+
}, [props.port, server, wsServer]);
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* When connecting to the remote websocket, if we don't start either
|
|
182
|
+
* the devtools instance or make an actual request to the worker in time,
|
|
183
|
+
* then the connecting process can error out. When this happens, we
|
|
184
|
+
* want to simply retry the connection. We use a state hook to trigger retries
|
|
185
|
+
* of the effect that connects to the remote websocket.
|
|
186
|
+
*/
|
|
187
|
+
const [
|
|
188
|
+
retryRemoteWebSocketConnectionSigil,
|
|
189
|
+
setRetryRemoteWebSocketConnectionSigil,
|
|
190
|
+
] = useState<number>(0);
|
|
191
|
+
function retryRemoteWebSocketConnection() {
|
|
192
|
+
setRetryRemoteWebSocketConnectionSigil((x) => x + 1);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A simple incrementing id to attach to messages we send to devtools */
|
|
196
|
+
const messageCounterRef = useRef(1);
|
|
197
|
+
|
|
198
|
+
// This effect tracks the connection to the remote websocket
|
|
199
|
+
// (stored in, no surprises here, `remoteWebSocket`)
|
|
200
|
+
useEffect(() => {
|
|
201
|
+
if (!props.inspectorUrl) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
// The actual websocket instance
|
|
205
|
+
const ws = new WebSocket(props.inspectorUrl);
|
|
206
|
+
setRemoteWebSocket(ws);
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* A handle to the interval we run to keep the websocket alive
|
|
210
|
+
*/
|
|
211
|
+
let keepAliveInterval: NodeJS.Timer;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Test if the websocket is closed
|
|
215
|
+
*/
|
|
216
|
+
function isClosed() {
|
|
217
|
+
return (
|
|
218
|
+
ws.readyState === WebSocket.CLOSED ||
|
|
219
|
+
ws.readyState === WebSocket.CLOSING
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Send a message to the remote websocket
|
|
225
|
+
*/
|
|
226
|
+
function send(event: Record<string, unknown>): void {
|
|
227
|
+
if (!isClosed()) {
|
|
228
|
+
ws.send(JSON.stringify(event));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Closes the inspector.
|
|
234
|
+
*/
|
|
235
|
+
function close(): void {
|
|
236
|
+
if (!isClosed()) {
|
|
237
|
+
try {
|
|
238
|
+
ws.close();
|
|
239
|
+
} catch (err) {
|
|
240
|
+
// Closing before the websocket is ready will throw an error.
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Since we have a handle on the remote websocket, we can tap
|
|
247
|
+
* into its events, and log any pertinent ones directly to
|
|
248
|
+
* the terminal (which means you have insight into your worker
|
|
249
|
+
* without having to open the devtools).
|
|
250
|
+
*/
|
|
251
|
+
if (props.logToTerminal) {
|
|
252
|
+
ws.addEventListener("message", (event: MessageEvent) => {
|
|
253
|
+
if (typeof event.data === "string") {
|
|
254
|
+
const evt = JSON.parse(event.data);
|
|
255
|
+
if (evt.method === "Runtime.exceptionThrown") {
|
|
256
|
+
const params = evt.params as Protocol.Runtime.ExceptionThrownEvent;
|
|
257
|
+
console.error(
|
|
258
|
+
"🚨", // cheesy, but it works
|
|
259
|
+
// maybe we could use color here too.
|
|
260
|
+
params.exceptionDetails.text,
|
|
261
|
+
params.exceptionDetails.exception?.description ?? ""
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
if (evt.method === "Runtime.consoleAPICalled") {
|
|
265
|
+
logConsoleMessage(
|
|
266
|
+
evt.params as Protocol.Runtime.ConsoleAPICalledEvent
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
// We should never get here, but who know is 2022...
|
|
271
|
+
console.error("unrecognised devtools event:", event);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
ws.addEventListener("open", () => {
|
|
277
|
+
send({ method: "Runtime.enable", id: messageCounterRef.current });
|
|
278
|
+
// TODO: This doesn't actually work. Must fix.
|
|
279
|
+
send({ method: "Network.enable", id: messageCounterRef.current++ });
|
|
280
|
+
|
|
281
|
+
keepAliveInterval = setInterval(() => {
|
|
282
|
+
send({
|
|
283
|
+
method: "Runtime.getIsolateId",
|
|
284
|
+
id: messageCounterRef.current++,
|
|
285
|
+
});
|
|
286
|
+
}, 10_000);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
ws.on("unexpected-response", () => {
|
|
290
|
+
console.log("waiting for connection...");
|
|
291
|
+
/**
|
|
292
|
+
* This usually means the worker is not "ready" yet
|
|
293
|
+
* so we'll just retry the connection process
|
|
294
|
+
*/
|
|
295
|
+
retryRemoteWebSocketConnection();
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
ws.addEventListener("close", () => {
|
|
299
|
+
clearInterval(keepAliveInterval);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
return () => {
|
|
303
|
+
// clean up! Let's first stop the heartbeat interval
|
|
304
|
+
clearInterval(keepAliveInterval);
|
|
305
|
+
// Then we'll send a message to the devtools instance to
|
|
306
|
+
// tell it to clear the console.
|
|
307
|
+
wsServer.clients.forEach((client) => {
|
|
308
|
+
// We could've used `localSocket` here, but
|
|
309
|
+
// then we would have had to add it to the effect
|
|
310
|
+
// change detection array, which would have made a
|
|
311
|
+
// bunch of other stuff complicated. So we'll just
|
|
312
|
+
// cycle through all of the server's connected clients
|
|
313
|
+
// (in practice, there should only be one or zero) and send
|
|
314
|
+
// the Log.clear message.
|
|
315
|
+
client.send(
|
|
316
|
+
JSON.stringify({
|
|
317
|
+
// TODO: This doesn't actually work. Must fix.
|
|
318
|
+
method: "Log.clear",
|
|
319
|
+
// we can disable the next eslint warning since
|
|
320
|
+
// we're referencing a ref that stays alive
|
|
321
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
322
|
+
id: messageCounterRef.current++,
|
|
323
|
+
params: {},
|
|
324
|
+
})
|
|
325
|
+
);
|
|
326
|
+
});
|
|
327
|
+
// Finally, we'll close the websocket
|
|
328
|
+
close();
|
|
329
|
+
// And we'll clear `remoteWebsocket`
|
|
330
|
+
setRemoteWebSocket(undefined);
|
|
331
|
+
};
|
|
332
|
+
}, [
|
|
333
|
+
props.inspectorUrl,
|
|
334
|
+
props.logToTerminal,
|
|
335
|
+
wsServer,
|
|
336
|
+
// We use a state value as a sigil to trigger a retry of the
|
|
337
|
+
// remote websocket connection. It's not used inside the effect,
|
|
338
|
+
// so react-hooks/exhaustive-deps doesn't complain if it's not
|
|
339
|
+
// included in the dependency array. But its presence is critical,
|
|
340
|
+
// so do NOT remove it from the dependency list.
|
|
341
|
+
retryRemoteWebSocketConnectionSigil,
|
|
342
|
+
]);
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* We want to make sure we don't lose any messages we receive from the
|
|
346
|
+
* remote websocket before devtools connects. So we use a ref to buffer
|
|
347
|
+
* messages, and flush them whenever devtools connects.
|
|
348
|
+
*/
|
|
349
|
+
const messageBufferRef = useRef<MessageEvent[]>([]);
|
|
350
|
+
|
|
351
|
+
// This effect tracks the state changes _between_ the local
|
|
352
|
+
// and remote websockets, and handles how messages flow between them.
|
|
353
|
+
useEffect(() => {
|
|
354
|
+
/**
|
|
355
|
+
* This event listener is used for buffering messages from
|
|
356
|
+
* the remote websocket, and flushing them
|
|
357
|
+
* when the local websocket connects.
|
|
358
|
+
*/
|
|
359
|
+
function bufferMessageFromRemoteSocket(event: MessageEvent) {
|
|
360
|
+
messageBufferRef.current.push(event);
|
|
361
|
+
// TODO: maybe we should have a max limit on this?
|
|
362
|
+
// if so, we should be careful when removing messages
|
|
363
|
+
// from the front, because they could be critical for
|
|
364
|
+
// devtools (like execution context creation, etc)
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (remoteWebSocket && !localWebSocket) {
|
|
368
|
+
// The local websocket hasn't connected yet, so we'll
|
|
369
|
+
// buffer messages until it does.
|
|
370
|
+
remoteWebSocket.addEventListener(
|
|
371
|
+
"message",
|
|
372
|
+
bufferMessageFromRemoteSocket
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Send a message from the local websocket to the remote websocket */
|
|
377
|
+
function sendMessageToRemoteWebSocket(event: MessageEvent) {
|
|
378
|
+
try {
|
|
379
|
+
assert(
|
|
380
|
+
remoteWebSocket,
|
|
381
|
+
"Trying to send a message to an undefined `remoteWebSocket`"
|
|
382
|
+
);
|
|
383
|
+
remoteWebSocket.send(event.data);
|
|
384
|
+
} catch (e) {
|
|
385
|
+
if (
|
|
386
|
+
(e as Error).message !==
|
|
387
|
+
"WebSocket is not open: readyState 0 (CONNECTING)"
|
|
388
|
+
) {
|
|
389
|
+
/**
|
|
390
|
+
* ^ this just means we haven't opened a websocket yet
|
|
391
|
+
* usually happens until there's at least one request
|
|
392
|
+
* which is weird, because we may miss something that
|
|
393
|
+
* happens on the first request. Maybe we should buffer
|
|
394
|
+
* these messages too?
|
|
395
|
+
*/
|
|
396
|
+
console.error(e);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Send a message from the local websocket to the remote websocket */
|
|
402
|
+
function sendMessageToLocalWebSocket(event: MessageEvent) {
|
|
403
|
+
assert(
|
|
404
|
+
localWebSocket,
|
|
405
|
+
"Trying to send a message to an undefined `localWebSocket`"
|
|
406
|
+
);
|
|
407
|
+
localWebSocket.send(event.data);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (localWebSocket && remoteWebSocket) {
|
|
411
|
+
// Both the remote and local websockets are connected, so let's
|
|
412
|
+
// start sending messages between them.
|
|
413
|
+
localWebSocket.addEventListener("message", sendMessageToRemoteWebSocket);
|
|
414
|
+
remoteWebSocket.addEventListener("message", sendMessageToLocalWebSocket);
|
|
415
|
+
|
|
416
|
+
// Also, let's flush any buffered messages
|
|
417
|
+
messageBufferRef.current.forEach(sendMessageToLocalWebSocket);
|
|
418
|
+
messageBufferRef.current = [];
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return () => {
|
|
422
|
+
// Cleanup like good citizens
|
|
423
|
+
if (remoteWebSocket) {
|
|
424
|
+
remoteWebSocket.removeEventListener(
|
|
425
|
+
"message",
|
|
426
|
+
bufferMessageFromRemoteSocket
|
|
427
|
+
);
|
|
428
|
+
remoteWebSocket.removeEventListener(
|
|
429
|
+
"message",
|
|
430
|
+
sendMessageToLocalWebSocket
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
if (localWebSocket) {
|
|
434
|
+
localWebSocket.removeEventListener(
|
|
435
|
+
"message",
|
|
436
|
+
sendMessageToRemoteWebSocket
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
}, [localWebSocket, remoteWebSocket]);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Credit: https://stackoverflow.com/a/2117523
|
|
444
|
+
function randomId(): string {
|
|
445
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
446
|
+
const r = (Math.random() * 16) | 0,
|
|
447
|
+
v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
448
|
+
return v.toString(16);
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* This function converts a message serialised as a devtools event
|
|
454
|
+
* into arguments suitable to be called by a console method, and
|
|
455
|
+
* then actually calls the method with those arguments. Effectively,
|
|
456
|
+
* we're just doing a little bit of the work of the devtools console,
|
|
457
|
+
* directly in the terminal.
|
|
458
|
+
*/
|
|
459
|
+
|
|
460
|
+
export const mapConsoleAPIMessageTypeToConsoleMethod: {
|
|
461
|
+
[key in Protocol.Runtime.ConsoleAPICalledEvent["type"]]: Exclude<
|
|
462
|
+
keyof Console,
|
|
463
|
+
"Console"
|
|
464
|
+
>;
|
|
465
|
+
} = {
|
|
466
|
+
log: "log",
|
|
467
|
+
debug: "debug",
|
|
468
|
+
info: "info",
|
|
469
|
+
warning: "warn",
|
|
470
|
+
error: "error",
|
|
471
|
+
dir: "dir",
|
|
472
|
+
dirxml: "dirxml",
|
|
473
|
+
table: "table",
|
|
474
|
+
trace: "trace",
|
|
475
|
+
clear: "clear",
|
|
476
|
+
count: "count",
|
|
477
|
+
assert: "assert",
|
|
478
|
+
profile: "profile",
|
|
479
|
+
profileEnd: "profileEnd",
|
|
480
|
+
timeEnd: "timeEnd",
|
|
481
|
+
startGroup: "group",
|
|
482
|
+
startGroupCollapsed: "groupCollapsed",
|
|
483
|
+
endGroup: "groupEnd",
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
|
|
487
|
+
const args: string[] = [];
|
|
488
|
+
for (const ro of evt.args) {
|
|
489
|
+
switch (ro.type) {
|
|
490
|
+
case "string":
|
|
491
|
+
case "number":
|
|
492
|
+
case "boolean":
|
|
493
|
+
case "undefined":
|
|
494
|
+
case "symbol":
|
|
495
|
+
case "bigint":
|
|
496
|
+
args.push(ro.value);
|
|
497
|
+
break;
|
|
498
|
+
case "function":
|
|
499
|
+
args.push(`[Function: ${ro.description ?? "<no-description>"}]`);
|
|
500
|
+
break;
|
|
501
|
+
case "object":
|
|
502
|
+
if (!ro.preview) {
|
|
503
|
+
args.push(
|
|
504
|
+
ro.subtype === "null"
|
|
505
|
+
? "null"
|
|
506
|
+
: ro.description ?? "<no-description>"
|
|
507
|
+
);
|
|
508
|
+
} else {
|
|
509
|
+
args.push(ro.preview.description ?? "<no-description>");
|
|
510
|
+
|
|
511
|
+
switch (ro.preview.subtype) {
|
|
512
|
+
case "array":
|
|
513
|
+
args.push(
|
|
514
|
+
"[ " +
|
|
515
|
+
ro.preview.properties
|
|
516
|
+
.map(({ value }) => {
|
|
517
|
+
return value;
|
|
518
|
+
})
|
|
519
|
+
.join(", ") +
|
|
520
|
+
(ro.preview.overflow ? "..." : "") +
|
|
521
|
+
" ]"
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
break;
|
|
525
|
+
case "weakmap":
|
|
526
|
+
case "map":
|
|
527
|
+
args.push(
|
|
528
|
+
"{\n" +
|
|
529
|
+
// Maps always have entries
|
|
530
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
531
|
+
ro.preview
|
|
532
|
+
.entries!.map(({ key, value }) => {
|
|
533
|
+
return ` ${key?.description ?? "<unknown>"} => ${
|
|
534
|
+
value.description
|
|
535
|
+
}`;
|
|
536
|
+
})
|
|
537
|
+
.join(",\n") +
|
|
538
|
+
(ro.preview.overflow ? "\n ..." : "") +
|
|
539
|
+
"\n}"
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
break;
|
|
543
|
+
case "weakset":
|
|
544
|
+
case "set":
|
|
545
|
+
args.push(
|
|
546
|
+
"{ " +
|
|
547
|
+
// Sets always have entries
|
|
548
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
549
|
+
ro.preview
|
|
550
|
+
.entries!.map(({ value }) => {
|
|
551
|
+
return `${value.description}`;
|
|
552
|
+
})
|
|
553
|
+
.join(", ") +
|
|
554
|
+
(ro.preview.overflow ? ", ..." : "") +
|
|
555
|
+
" }"
|
|
556
|
+
);
|
|
557
|
+
break;
|
|
558
|
+
case "regexp":
|
|
559
|
+
break;
|
|
560
|
+
case "date":
|
|
561
|
+
break;
|
|
562
|
+
case "generator":
|
|
563
|
+
args.push(ro.preview.properties[0].value || "");
|
|
564
|
+
break;
|
|
565
|
+
case "promise":
|
|
566
|
+
if (ro.preview.properties[0].value === "pending") {
|
|
567
|
+
args.push(`{<${ro.preview.properties[0].value}>}`);
|
|
568
|
+
} else {
|
|
569
|
+
args.push(
|
|
570
|
+
`{<${ro.preview.properties[0].value}>: ${ro.preview.properties[1].value}}`
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
break;
|
|
574
|
+
case "node":
|
|
575
|
+
case "iterator":
|
|
576
|
+
case "proxy":
|
|
577
|
+
case "typedarray":
|
|
578
|
+
case "arraybuffer":
|
|
579
|
+
case "dataview":
|
|
580
|
+
case "webassemblymemory":
|
|
581
|
+
case "wasmvalue":
|
|
582
|
+
break;
|
|
583
|
+
case "error":
|
|
584
|
+
default:
|
|
585
|
+
// just a pojo
|
|
586
|
+
args.push(
|
|
587
|
+
"{\n" +
|
|
588
|
+
ro.preview.properties
|
|
589
|
+
.map(({ name, value }) => {
|
|
590
|
+
return ` ${name}: ${value}`;
|
|
591
|
+
})
|
|
592
|
+
.join(",\n") +
|
|
593
|
+
(ro.preview.overflow ? "\n ..." : "") +
|
|
594
|
+
"\n}"
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
break;
|
|
599
|
+
default:
|
|
600
|
+
args.push(ro.description || ro.unserializableValue || "🦋");
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const method = mapConsoleAPIMessageTypeToConsoleMethod[evt.type];
|
|
606
|
+
|
|
607
|
+
if (method in console) {
|
|
608
|
+
switch (method) {
|
|
609
|
+
case "dir":
|
|
610
|
+
console.dir(args);
|
|
611
|
+
break;
|
|
612
|
+
case "table":
|
|
613
|
+
console.table(args);
|
|
614
|
+
break;
|
|
615
|
+
default:
|
|
616
|
+
// eslint-disable-next-line prefer-spread
|
|
617
|
+
console[method].apply(console, args);
|
|
618
|
+
break;
|
|
619
|
+
}
|
|
620
|
+
} else {
|
|
621
|
+
console.warn(`Unsupported console method: ${method}`);
|
|
622
|
+
console.log("console event:", evt);
|
|
623
|
+
}
|
|
624
|
+
}
|