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