@depup/wrangler 4.75.0-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +33 -0
  2. package/bin/wrangler.js +93 -0
  3. package/changes.json +18 -0
  4. package/config-schema.json +3222 -0
  5. package/kv-asset-handler.js +1 -0
  6. package/package.json +221 -0
  7. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  8. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  9. package/templates/__tests__/tsconfig.json +8 -0
  10. package/templates/checked-fetch.js +28 -0
  11. package/templates/facade.d.ts +19 -0
  12. package/templates/middleware/common.ts +67 -0
  13. package/templates/middleware/loader-modules.ts +134 -0
  14. package/templates/middleware/loader-sw.ts +229 -0
  15. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  16. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  17. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  18. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  19. package/templates/middleware/middleware-pretty-error.ts +40 -0
  20. package/templates/middleware/middleware-scheduled.ts +29 -0
  21. package/templates/modules-watch-stub.js +4 -0
  22. package/templates/new-worker-scheduled.js +17 -0
  23. package/templates/new-worker-scheduled.ts +32 -0
  24. package/templates/new-worker.js +15 -0
  25. package/templates/new-worker.ts +33 -0
  26. package/templates/no-op-worker.js +10 -0
  27. package/templates/pages-dev-pipeline.ts +33 -0
  28. package/templates/pages-dev-util.ts +55 -0
  29. package/templates/pages-shim.ts +9 -0
  30. package/templates/pages-template-plugin.ts +190 -0
  31. package/templates/pages-template-worker.ts +198 -0
  32. package/templates/remoteBindings/ProxyServerWorker.ts +143 -0
  33. package/templates/remoteBindings/wrangler.jsonc +4 -0
  34. package/templates/startDevWorker/InspectorProxyWorker.ts +699 -0
  35. package/templates/startDevWorker/ProxyWorker.ts +340 -0
  36. package/templates/tsconfig-sanity.ts +11 -0
  37. package/templates/tsconfig.init.json +22 -0
  38. package/templates/tsconfig.json +14 -0
  39. package/wrangler-dist/InspectorProxyWorker.js +486 -0
  40. package/wrangler-dist/ProxyServerWorker.js +3314 -0
  41. package/wrangler-dist/ProxyWorker.js +238 -0
  42. package/wrangler-dist/cli.d.ts +3154 -0
  43. package/wrangler-dist/cli.js +303399 -0
  44. package/wrangler-dist/metafile-cjs.json +1 -0
@@ -0,0 +1,699 @@
1
+ import assert from "node:assert";
2
+ import {
3
+ DevToolsCommandRequest,
4
+ DevToolsCommandRequests,
5
+ DevToolsCommandResponses,
6
+ DevToolsEvent,
7
+ DevToolsEvents,
8
+ serialiseError,
9
+ } from "../../src/api/startDevWorker/events";
10
+ import {
11
+ createDeferred,
12
+ DeferredPromise,
13
+ MaybePromise,
14
+ urlFromParts,
15
+ } from "../../src/api/startDevWorker/utils";
16
+ import { assertNever } from "../../src/utils/assert-never";
17
+ import type {
18
+ InspectorProxyWorkerIncomingWebSocketMessage,
19
+ InspectorProxyWorkerOutgoingRequestBody,
20
+ InspectorProxyWorkerOutgoingWebsocketMessage,
21
+ ProxyData,
22
+ } from "../../src/api/startDevWorker/events";
23
+
24
+ const ALLOWED_HOST_HOSTNAMES = ["127.0.0.1", "[::1]", "localhost"];
25
+ const ALLOWED_ORIGIN_HOSTNAMES = [
26
+ "devtools.devprod.cloudflare.dev",
27
+ // Workers + Assets (current deployment)
28
+ "cloudflare-devtools.devprod.workers.dev",
29
+ /^[a-z0-9]+-cloudflare-devtools\.devprod\.workers\.dev$/,
30
+ // Cloudflare Pages (legacy deployment)
31
+ "cloudflare-devtools.pages.dev",
32
+ /^[a-z0-9]+\.cloudflare-devtools\.pages\.dev$/,
33
+ "127.0.0.1",
34
+ "[::1]",
35
+ "localhost",
36
+ ];
37
+
38
+ interface Env {
39
+ PROXY_CONTROLLER: Fetcher;
40
+ PROXY_CONTROLLER_AUTH_SECRET: string;
41
+ WRANGLER_VERSION: string;
42
+ DURABLE_OBJECT: DurableObjectNamespace;
43
+ }
44
+
45
+ export default {
46
+ fetch(req, env) {
47
+ const singleton = env.DURABLE_OBJECT.idFromName("");
48
+ const inspectorProxy = env.DURABLE_OBJECT.get(singleton);
49
+
50
+ return inspectorProxy.fetch(req);
51
+ },
52
+ } as ExportedHandler<Env>;
53
+
54
+ function isDevToolsEvent<Method extends DevToolsEvents["method"]>(
55
+ event: unknown,
56
+ name: Method
57
+ ): event is DevToolsEvent<Method> {
58
+ return (
59
+ typeof event === "object" &&
60
+ event !== null &&
61
+ "method" in event &&
62
+ event.method === name
63
+ );
64
+ }
65
+
66
+ export class InspectorProxyWorker implements DurableObject {
67
+ constructor(
68
+ _state: DurableObjectState,
69
+ readonly env: Env
70
+ ) {}
71
+
72
+ websockets: {
73
+ proxyController?: WebSocket;
74
+ runtime?: WebSocket;
75
+ devtools?: WebSocket;
76
+
77
+ // Browser DevTools cannot read the filesystem,
78
+ // instead they fetch via `Network.loadNetworkResource` messages.
79
+ // IDE DevTools can read the filesystem and expect absolute paths.
80
+ devtoolsHasFileSystemAccess?: boolean;
81
+
82
+ // We want to be able to delay devtools connection response
83
+ // until we've connected to the runtime inspector server
84
+ // so this deferred holds a promise to websockets.runtime
85
+ runtimeDeferred: DeferredPromise<WebSocket>;
86
+ } = {
87
+ runtimeDeferred: createDeferred<WebSocket>(),
88
+ };
89
+ proxyData?: ProxyData;
90
+ runtimeMessageBuffer: (DevToolsCommandResponses | DevToolsEvents)[] = [];
91
+
92
+ // Only allow a limited number of error-based reconnections, so as not to infinite loop
93
+ reconnectionsRemaining = 3;
94
+
95
+ async fetch(req: Request) {
96
+ if (
97
+ req.headers.get("Authorization") === this.env.PROXY_CONTROLLER_AUTH_SECRET
98
+ ) {
99
+ return this.handleProxyControllerRequest(req);
100
+ }
101
+
102
+ if (req.headers.get("Upgrade") === "websocket") {
103
+ return this.handleDevToolsWebSocketUpgradeRequest(req);
104
+ }
105
+
106
+ return this.handleDevToolsJsonRequest(req);
107
+ }
108
+
109
+ // ************************
110
+ // ** PROXY CONTROLLER **
111
+ // ************************
112
+
113
+ handleProxyControllerRequest(req: Request) {
114
+ assert(
115
+ req.headers.get("Upgrade") === "websocket",
116
+ "Expected proxy controller data request to be WebSocket upgrade"
117
+ );
118
+
119
+ const { 0: response, 1: proxyController } = new WebSocketPair();
120
+ proxyController.accept();
121
+ proxyController.addEventListener("close", (event) => {
122
+ // don't reconnect the proxyController websocket
123
+ // ProxyController can detect this event and reconnect itself
124
+
125
+ this.sendDebugLog(
126
+ "PROXY CONTROLLER WEBSOCKET CLOSED",
127
+ event.code,
128
+ event.reason
129
+ );
130
+
131
+ if (this.websockets.proxyController === proxyController) {
132
+ this.websockets.proxyController = undefined;
133
+ }
134
+ });
135
+ proxyController.addEventListener("error", (event) => {
136
+ // don't reconnect the proxyController websocket
137
+ // ProxyController can detect this event and reconnect itself
138
+
139
+ const error = serialiseError(event.error);
140
+ this.sendDebugLog("PROXY CONTROLLER WEBSOCKET ERROR", error);
141
+
142
+ if (this.websockets.proxyController === proxyController) {
143
+ this.websockets.proxyController = undefined;
144
+ }
145
+ });
146
+ proxyController.addEventListener(
147
+ "message",
148
+ this.handleProxyControllerIncomingMessage
149
+ );
150
+
151
+ this.websockets.proxyController = proxyController;
152
+
153
+ return new Response(null, {
154
+ status: 101,
155
+ webSocket: response,
156
+ });
157
+ }
158
+
159
+ handleProxyControllerIncomingMessage = (event: MessageEvent) => {
160
+ assert(
161
+ typeof event.data === "string",
162
+ "Expected event.data from proxy controller to be string"
163
+ );
164
+
165
+ const message: InspectorProxyWorkerIncomingWebSocketMessage = JSON.parse(
166
+ event.data
167
+ );
168
+
169
+ this.sendDebugLog("handleProxyControllerIncomingMessage", event.data);
170
+
171
+ switch (message.type) {
172
+ case "reloadStart": {
173
+ this.sendRuntimeDiscardConsoleEntries();
174
+
175
+ break;
176
+ }
177
+ case "reloadComplete": {
178
+ this.proxyData = message.proxyData;
179
+
180
+ this.reconnectRuntimeWebSocket();
181
+
182
+ break;
183
+ }
184
+ default: {
185
+ assertNever(message);
186
+ }
187
+ }
188
+ };
189
+
190
+ sendProxyControllerMessage(
191
+ message: string | InspectorProxyWorkerOutgoingWebsocketMessage
192
+ ) {
193
+ message = typeof message === "string" ? message : JSON.stringify(message);
194
+
195
+ // if the proxyController websocket is disconnected, throw away the message
196
+ this.websockets.proxyController?.send(message);
197
+ }
198
+
199
+ async sendProxyControllerRequest(
200
+ message: InspectorProxyWorkerOutgoingRequestBody
201
+ ) {
202
+ try {
203
+ const res = await this.env.PROXY_CONTROLLER.fetch("http://dummy", {
204
+ method: "POST",
205
+ body: JSON.stringify(message),
206
+ });
207
+ return res.ok ? await res.text() : undefined;
208
+ } catch (e) {
209
+ this.sendDebugLog(
210
+ "FAILED TO SEND PROXY CONTROLLER REQUEST",
211
+ serialiseError(e)
212
+ );
213
+ return undefined;
214
+ }
215
+ }
216
+
217
+ sendDebugLog: typeof console.debug = (...args) => {
218
+ this.sendProxyControllerRequest({ type: "debug-log", args });
219
+ };
220
+
221
+ // ***************
222
+ // ** RUNTIME **
223
+ // ***************
224
+
225
+ handleRuntimeIncomingMessage = (event: MessageEvent) => {
226
+ assert(typeof event.data === "string");
227
+
228
+ const msg = JSON.parse(event.data) as
229
+ | DevToolsCommandResponses
230
+ | DevToolsEvents;
231
+ this.sendDebugLog("RUNTIME INCOMING MESSAGE", msg);
232
+
233
+ if (isDevToolsEvent(msg, "Runtime.exceptionThrown")) {
234
+ this.sendProxyControllerMessage(event.data);
235
+ }
236
+ if (
237
+ this.proxyData?.proxyLogsToController &&
238
+ isDevToolsEvent(msg, "Runtime.consoleAPICalled")
239
+ ) {
240
+ this.sendProxyControllerMessage(event.data);
241
+ }
242
+
243
+ this.runtimeMessageBuffer.push(msg);
244
+ this.tryDrainRuntimeMessageBuffer();
245
+ };
246
+
247
+ handleRuntimeScriptParsed(msg: DevToolsEvent<"Debugger.scriptParsed">) {
248
+ // If the devtools does not have filesystem access,
249
+ // rewrite the sourceMapURL to use a special scheme.
250
+ // This special scheme is used to indicate whether
251
+ // to intercept each loadNetworkResource message.
252
+
253
+ if (
254
+ !this.websockets.devtoolsHasFileSystemAccess &&
255
+ msg.params.sourceMapURL !== undefined &&
256
+ // Don't try to find a sourcemap for e.g. node-internal: scripts
257
+ msg.params.url.startsWith("file:")
258
+ ) {
259
+ const url = new URL(msg.params.sourceMapURL, msg.params.url);
260
+ // Check for file: in case msg.params.sourceMapURL has a different
261
+ // protocol (e.g. data). In that case we should ignore this file
262
+ if (url.protocol === "file:") {
263
+ msg.params.sourceMapURL = url.href.replace("file:", "wrangler-file:");
264
+ }
265
+ }
266
+
267
+ void this.sendDevToolsMessage(msg);
268
+ }
269
+
270
+ tryDrainRuntimeMessageBuffer = () => {
271
+ // If we don't have a DevTools WebSocket, try again later
272
+ if (this.websockets.devtools === undefined) return;
273
+
274
+ // clear the buffer and replay each message to devtools
275
+ for (const msg of this.runtimeMessageBuffer.splice(0)) {
276
+ if (isDevToolsEvent(msg, "Debugger.scriptParsed")) {
277
+ this.handleRuntimeScriptParsed(msg);
278
+ } else {
279
+ void this.sendDevToolsMessage(msg);
280
+ }
281
+ }
282
+ };
283
+
284
+ runtimeAbortController = new AbortController(); // will abort the in-flight websocket upgrade request to the remote runtime
285
+ runtimeKeepAliveInterval: number | null = null;
286
+ async reconnectRuntimeWebSocket() {
287
+ assert(this.proxyData, "Expected this.proxyData to be defined");
288
+
289
+ this.sendDebugLog("reconnectRuntimeWebSocket");
290
+
291
+ this.websockets.runtime?.close();
292
+ this.websockets.runtime = undefined;
293
+ this.runtimeAbortController.abort();
294
+ this.runtimeAbortController = new AbortController();
295
+ this.websockets.runtimeDeferred = createDeferred<WebSocket>(
296
+ this.websockets.runtimeDeferred
297
+ );
298
+
299
+ const runtimeWebSocketUrl = urlFromParts(
300
+ this.proxyData.userWorkerInspectorUrl
301
+ );
302
+ runtimeWebSocketUrl.protocol = this.proxyData.userWorkerUrl.protocol; // http: or https:
303
+
304
+ this.sendDebugLog("NEW RUNTIME WEBSOCKET", runtimeWebSocketUrl);
305
+
306
+ // Make sure DevTools re-fetches script contents,
307
+ // and uses the newly created execution context
308
+ this.sendDevToolsMessage({
309
+ method: "Runtime.executionContextsCleared",
310
+ params: undefined,
311
+ });
312
+
313
+ const upgrade = await fetch(runtimeWebSocketUrl, {
314
+ headers: {
315
+ ...this.proxyData.headers,
316
+ "User-Agent": `wrangler/${this.env.WRANGLER_VERSION}`,
317
+ Upgrade: "websocket",
318
+ },
319
+ signal: this.runtimeAbortController.signal,
320
+ });
321
+
322
+ const runtime = upgrade.webSocket;
323
+ if (!runtime) {
324
+ const error = new Error(
325
+ `Failed to establish the WebSocket connection: expected server to reply with HTTP status code 101 (switching protocols), but received ${upgrade.status} instead.`
326
+ );
327
+
328
+ // Sometimes the backend will fail to connect the runtime websocket with a 502 error. These are usually transient, so try and reconnect
329
+ if (upgrade.status === 502 && this.reconnectionsRemaining >= 0) {
330
+ await scheduler.wait((3 - this.reconnectionsRemaining) * 1000);
331
+ this.sendDebugLog(
332
+ "RECONNECTING RUNTIME WEBSOCKET after 502. Reconnections remaining:",
333
+ this.reconnectionsRemaining
334
+ );
335
+
336
+ return this.reconnectRuntimeWebSocket();
337
+ }
338
+
339
+ this.websockets.runtimeDeferred.reject(error);
340
+ this.sendProxyControllerRequest({
341
+ type: "runtime-websocket-error",
342
+ error: serialiseError(error),
343
+ });
344
+
345
+ return;
346
+ }
347
+
348
+ this.websockets.runtime = runtime;
349
+
350
+ runtime.addEventListener("message", this.handleRuntimeIncomingMessage);
351
+
352
+ runtime.addEventListener("close", (event) => {
353
+ this.sendDebugLog("RUNTIME WEBSOCKET CLOSED", event.code, event.reason);
354
+
355
+ clearInterval(this.runtimeKeepAliveInterval);
356
+
357
+ if (this.websockets.runtime === runtime) {
358
+ this.websockets.runtime = undefined;
359
+ }
360
+
361
+ // don't reconnect the runtime websocket
362
+ // if it closes unexpectedly (very rare or a case where reconnecting won't succeed anyway)
363
+ // wait for a new proxy-data message or manual restart
364
+ });
365
+
366
+ runtime.addEventListener("error", (event) => {
367
+ const error = serialiseError(event.error);
368
+ this.sendDebugLog("RUNTIME WEBSOCKET ERROR", error);
369
+
370
+ clearInterval(this.runtimeKeepAliveInterval);
371
+
372
+ if (this.websockets.runtime === runtime) {
373
+ this.websockets.runtime = undefined;
374
+ }
375
+
376
+ this.sendProxyControllerRequest({
377
+ type: "runtime-websocket-error",
378
+ error,
379
+ });
380
+
381
+ // don't reconnect the runtime websocket
382
+ // if it closes unexpectedly (very rare or a case where reconnecting won't succeed anyway)
383
+ // wait for a new proxy-data message or manual restart
384
+ });
385
+
386
+ runtime.accept();
387
+
388
+ // fetch(Upgrade: websocket) resolves when the websocket is open
389
+ // therefore the open event will not fire, so just trigger the handler
390
+ this.handleRuntimeWebSocketOpen(runtime);
391
+ }
392
+
393
+ #runtimeMessageCounter = 1e8;
394
+ nextCounter() {
395
+ return ++this.#runtimeMessageCounter;
396
+ }
397
+ handleRuntimeWebSocketOpen(runtime: WebSocket) {
398
+ this.sendDebugLog("RUNTIME WEBSOCKET OPENED");
399
+ this.reconnectionsRemaining = 3;
400
+
401
+ this.sendRuntimeMessage(
402
+ { method: "Runtime.enable", id: this.nextCounter() },
403
+ runtime
404
+ );
405
+ // Only send Debugger.enable if DevTools is already attached.
406
+ // When DevTools first connects, it sends its own Debugger.enable message.
407
+ // However, on runtime reconnect (e.g., after worker reload), DevTools won't
408
+ // re-send Debugger.enable since it considers the session still active.
409
+ // Without this, Debugger.scriptParsed events won't be emitted on the new
410
+ // runtime connection, breaking source maps, breakpoints, and debugger pausing.
411
+ if (this.websockets.devtools !== undefined) {
412
+ this.sendRuntimeMessage(
413
+ { method: "Debugger.enable", id: this.nextCounter() },
414
+ runtime
415
+ );
416
+ }
417
+ this.sendRuntimeMessage(
418
+ { method: "Network.enable", id: this.nextCounter() },
419
+ runtime
420
+ );
421
+
422
+ clearInterval(this.runtimeKeepAliveInterval);
423
+ this.runtimeKeepAliveInterval = setInterval(() => {
424
+ this.sendRuntimeMessage(
425
+ { method: "Runtime.getIsolateId", id: this.nextCounter() },
426
+ runtime
427
+ );
428
+ }, 10_000) as any;
429
+
430
+ this.websockets.runtimeDeferred.resolve(runtime);
431
+ }
432
+
433
+ sendRuntimeDiscardConsoleEntries() {
434
+ // by default, sendRuntimeMessage waits for the runtime websocket to connect
435
+ // but we only want to send this message now or never
436
+ // if we schedule it to send later (like waiting for the websocket, by default)
437
+ // then we risk clearing logs that have occured since we scheduled it too
438
+ // which is worse than leaving logs from the previous version on screen
439
+ if (this.websockets.runtime) {
440
+ this.sendRuntimeMessage(
441
+ {
442
+ method: "Runtime.discardConsoleEntries",
443
+ id: this.nextCounter(),
444
+ },
445
+ this.websockets.runtime
446
+ );
447
+ }
448
+ }
449
+
450
+ async sendRuntimeMessage(
451
+ message: string | DevToolsCommandRequests,
452
+ runtime: MaybePromise<WebSocket> = this.websockets.runtimeDeferred.promise
453
+ ) {
454
+ runtime = await runtime;
455
+ message = typeof message === "string" ? message : JSON.stringify(message);
456
+
457
+ this.sendDebugLog("SEND TO RUNTIME", message);
458
+
459
+ runtime.send(message);
460
+ }
461
+
462
+ // ****************
463
+ // ** DEVTOOLS **
464
+ // ****************
465
+
466
+ #inspectorId = crypto.randomUUID();
467
+ async handleDevToolsJsonRequest(req: Request) {
468
+ const url = new URL(req.url);
469
+
470
+ if (url.pathname === "/json/version") {
471
+ return Response.json({
472
+ Browser: `wrangler/v${this.env.WRANGLER_VERSION}`,
473
+ // TODO: (someday): The DevTools protocol should match that of workerd.
474
+ // This could be exposed by the preview API.
475
+ "Protocol-Version": "1.3",
476
+ });
477
+ }
478
+
479
+ if (url.pathname === "/json" || url.pathname === "/json/list") {
480
+ // TODO: can we remove the `/ws` here if we only have a single worker?
481
+ const localHost = `${url.host}/ws`;
482
+ const devtoolsFrontendUrl = `https://devtools.devprod.cloudflare.dev/js_app?theme=systemPreferred&debugger=true&ws=${localHost}`;
483
+
484
+ return Response.json([
485
+ {
486
+ id: this.#inspectorId,
487
+ type: "node", // TODO: can we specify different type?
488
+ description: "workers",
489
+ webSocketDebuggerUrl: `ws://${localHost}`,
490
+ devtoolsFrontendUrl,
491
+ devtoolsFrontendUrlCompat: devtoolsFrontendUrl,
492
+ // Below are fields that are visible in the DevTools UI.
493
+ title: "Cloudflare Worker",
494
+ faviconUrl: "https://workers.cloudflare.com/favicon.ico",
495
+ // url: "http://" + localHost, // looks unnecessary
496
+ },
497
+ ]);
498
+ }
499
+
500
+ return new Response(null, { status: 404 });
501
+ }
502
+
503
+ async handleDevToolsWebSocketUpgradeRequest(req: Request) {
504
+ // Validate `Host` header
505
+ let hostHeader = req.headers.get("Host");
506
+ if (hostHeader == null) return new Response(null, { status: 400 });
507
+ try {
508
+ const host = new URL(`http://${hostHeader}`);
509
+ if (!ALLOWED_HOST_HOSTNAMES.includes(host.hostname)) {
510
+ return new Response("Disallowed `Host` header", { status: 401 });
511
+ }
512
+ } catch {
513
+ return new Response("Expected `Host` header", { status: 400 });
514
+ }
515
+ // Validate `Origin` header
516
+ let originHeader = req.headers.get("Origin");
517
+ if (originHeader === null && !req.headers.has("User-Agent")) {
518
+ // VSCode doesn't send an `Origin` header, but also doesn't send a
519
+ // `User-Agent` header, so allow an empty origin in this case.
520
+ originHeader = "http://localhost";
521
+ }
522
+ if (originHeader === null) {
523
+ return new Response("Expected `Origin` header", { status: 400 });
524
+ }
525
+ try {
526
+ const origin = new URL(originHeader);
527
+ const allowed = ALLOWED_ORIGIN_HOSTNAMES.some((rule) => {
528
+ if (typeof rule === "string") return origin.hostname === rule;
529
+ else return rule.test(origin.hostname);
530
+ });
531
+ if (!allowed) {
532
+ return new Response("Disallowed `Origin` header", { status: 401 });
533
+ }
534
+ } catch {
535
+ return new Response("Expected `Origin` header", { status: 400 });
536
+ }
537
+
538
+ // DevTools attempting to connect
539
+ this.sendDebugLog("DEVTOOLS WEBSOCKET TRYING TO CONNECT");
540
+
541
+ // Delay devtools connection response until we've connected to the runtime inspector server
542
+ await this.websockets.runtimeDeferred.promise;
543
+
544
+ this.sendDebugLog("DEVTOOLS WEBSOCKET CAN NOW CONNECT");
545
+
546
+ assert(
547
+ req.headers.get("Upgrade") === "websocket",
548
+ "Expected DevTools connection to be WebSocket upgrade"
549
+ );
550
+ const { 0: response, 1: devtools } = new WebSocketPair();
551
+ devtools.accept();
552
+
553
+ if (this.websockets.devtools !== undefined) {
554
+ /** We only want to have one active Devtools instance at a time. */
555
+ // TODO(consider): prioritise new websocket over previous
556
+ devtools.close(
557
+ 1013,
558
+ "Too many clients; only one can be connected at a time"
559
+ );
560
+ } else {
561
+ devtools.addEventListener("message", this.handleDevToolsIncomingMessage);
562
+ const disconnectDevtools = () => {
563
+ if (this.websockets.devtools === devtools) {
564
+ this.websockets.devtools = undefined;
565
+
566
+ // Notify the runtime to disable the debugger when DevTools disconnects.
567
+ if (this.websockets.runtime) {
568
+ this.sendRuntimeMessage({
569
+ id: this.nextCounter(),
570
+ method: "Debugger.disable",
571
+ });
572
+ }
573
+ }
574
+ };
575
+ devtools.addEventListener("close", (event) => {
576
+ this.sendDebugLog(
577
+ "DEVTOOLS WEBSOCKET CLOSED",
578
+ event.code,
579
+ event.reason
580
+ );
581
+ disconnectDevtools();
582
+ });
583
+ devtools.addEventListener("error", (event) => {
584
+ const error = serialiseError(event.error);
585
+ this.sendDebugLog("DEVTOOLS WEBSOCKET ERROR", error);
586
+ disconnectDevtools();
587
+ });
588
+
589
+ // Since Wrangler proxies the inspector, reloading Chrome DevTools won't trigger debugger initialisation events (because it's connecting to an extant session).
590
+ // This sends a `Debugger.disable` message to the remote when a new WebSocket connection is initialised,
591
+ // with the assumption that the new connection will shortly send a `Debugger.enable` event and trigger re-initialisation.
592
+ // The key initialisation messages that are needed are the `Debugger.scriptParsed events`.
593
+ this.sendRuntimeMessage({
594
+ id: this.nextCounter(),
595
+ method: "Debugger.disable",
596
+ });
597
+
598
+ this.sendDebugLog("DEVTOOLS WEBSOCKET CONNECTED");
599
+
600
+ // Our patched DevTools are hosted on a `https://` URL. These cannot
601
+ // access `file://` URLs, meaning local source maps cannot be fetched.
602
+ // To get around this, we can rewrite `Debugger.scriptParsed` events to
603
+ // include a special `worker:` scheme for source maps, and respond to
604
+ // `Network.loadNetworkResource` commands for these. Unfortunately, this
605
+ // breaks IDE's built-in debuggers (e.g. VSCode and WebStorm), so we only
606
+ // want to enable this transformation when we detect hosted DevTools has
607
+ // connected. We do this by looking at the WebSocket handshake headers:
608
+ //
609
+ // DevTools
610
+ //
611
+ // Upgrade: websocket
612
+ // Host: localhost:9229
613
+ // (from Chrome) User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
614
+ // (from Firefox) User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/116.0
615
+ // Origin: https://devtools.devprod.cloudflare.dev
616
+ // ...
617
+ //
618
+ // VSCode
619
+ //
620
+ // Upgrade: websocket
621
+ // Host: localhost
622
+ // ...
623
+ //
624
+ // WebStorm
625
+ //
626
+ // Upgrade: websocket
627
+ // Host: localhost:9229
628
+ // Origin: http://localhost:9229
629
+ // ...
630
+ //
631
+ // From this, we could just use the presence of a `User-Agent` header to
632
+ // determine if DevTools connected, but VSCode/WebStorm could very well
633
+ // add this in future versions. We could also look for an `Origin` header
634
+ // matching the hosted DevTools URL, but this would prevent preview/local
635
+ // versions working. Instead, we look for a browser-like `User-Agent`.
636
+ const userAgent = req.headers.get("User-Agent") ?? "";
637
+ const hasFileSystemAccess = !/mozilla/i.test(userAgent);
638
+
639
+ this.websockets.devtools = devtools;
640
+ this.websockets.devtoolsHasFileSystemAccess = hasFileSystemAccess;
641
+
642
+ this.tryDrainRuntimeMessageBuffer();
643
+ }
644
+
645
+ return new Response(null, { status: 101, webSocket: response });
646
+ }
647
+
648
+ handleDevToolsIncomingMessage = (event: MessageEvent) => {
649
+ assert(
650
+ typeof event.data === "string",
651
+ "Expected devtools incoming message to be of type string"
652
+ );
653
+
654
+ const message = JSON.parse(event.data) as DevToolsCommandRequests;
655
+ this.sendDebugLog("DEVTOOLS INCOMING MESSAGE", message);
656
+
657
+ if (message.method === "Network.loadNetworkResource") {
658
+ return void this.handleDevToolsLoadNetworkResource(message);
659
+ }
660
+
661
+ this.sendRuntimeMessage(JSON.stringify(message));
662
+ };
663
+
664
+ async handleDevToolsLoadNetworkResource(
665
+ message: DevToolsCommandRequest<"Network.loadNetworkResource">
666
+ ) {
667
+ const response = await this.sendProxyControllerRequest({
668
+ type: "load-network-resource",
669
+ url: message.params.url,
670
+ });
671
+ if (response === undefined) {
672
+ this.sendDebugLog(
673
+ `ProxyController could not resolve Network.loadNetworkResource for "${message.params.url}"`
674
+ );
675
+
676
+ // When the ProxyController cannot resolve a resource, let the runtime handle the request
677
+ this.sendRuntimeMessage(JSON.stringify(message));
678
+ } else {
679
+ // this.websockets.devtools can be undefined here
680
+ // the incoming message implies we have a devtools connection, but after
681
+ // the await it could've dropped in which case we can safely not respond
682
+ this.sendDevToolsMessage({
683
+ id: message.id,
684
+ // @ts-expect-error DevTools Protocol type does not match our patched devtools -- result.resource.text was added
685
+ result: { resource: { success: true, text: response } },
686
+ });
687
+ }
688
+ }
689
+
690
+ sendDevToolsMessage(
691
+ message: string | DevToolsCommandResponses | DevToolsEvents
692
+ ) {
693
+ message = typeof message === "string" ? message : JSON.stringify(message);
694
+
695
+ this.sendDebugLog("SEND TO DEVTOOLS", message);
696
+
697
+ this.websockets.devtools?.send(message);
698
+ }
699
+ }