wrangler 0.0.0-e9ab55a → 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 +33 -16
- package/pages/functions/buildWorker.ts +1 -1
- package/pages/functions/filepath-routing.test.ts +212 -28
- package/pages/functions/filepath-routing.ts +83 -130
- package/pages/functions/routes.ts +11 -18
- package/pages/functions/template-worker.ts +3 -9
- package/src/__tests__/configuration.test.ts +2874 -0
- package/src/__tests__/dev.test.tsx +582 -61
- 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__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +52 -16
- package/src/__tests__/helpers/mock-console.ts +70 -0
- package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
- 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 +777 -77
- package/src/__tests__/jest.setup.ts +58 -6
- package/src/__tests__/kv.test.ts +716 -462
- 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 +3915 -71
- 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 +21 -19
- package/src/cfetch/internal.ts +92 -22
- package/src/cli.ts +14 -2
- 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/{api/preview.ts → create-worker-preview.ts} +69 -24
- 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 +2 -2
- 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 +1310 -849
- package/src/inspect.ts +102 -21
- package/src/intl-polyfill.d.ts +139 -0
- package/src/{kv.tsx → kv.ts} +104 -61
- 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 +228 -35
- package/src/open-in-browser.ts +25 -0
- package/src/package-manager.ts +126 -0
- package/src/pages.tsx +214 -48
- package/src/parse.ts +183 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +192 -39
- package/src/publish.ts +321 -318
- package/src/r2.ts +50 -0
- package/src/reporting.ts +131 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +224 -62
- 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 +317 -178
- package/src/whoami.tsx +69 -0
- package/src/{api/worker.ts → worker.ts} +54 -37
- 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 +131262 -112648
- package/wrangler-dist/cli.js.map +3 -3
- 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/config.ts +0 -646
- package/src/dev.tsx +0 -784
- package/src/tail.tsx +0 -73
package/src/proxy.ts
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
|
+
import { createServer as createHttpServer } from "node:http";
|
|
1
2
|
import { connect } from "node:http2";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
3
|
+
import { createServer as createHttpsServer } from "node:https";
|
|
4
|
+
import WebSocket from "faye-websocket";
|
|
5
|
+
import { useEffect, useRef, useState } from "react";
|
|
6
|
+
import serveStatic from "serve-static";
|
|
7
|
+
import { getHttpsOptions } from "./https-options";
|
|
8
|
+
import { reportError } from "./reporting";
|
|
9
|
+
import type { CfPreviewToken } from "./create-worker-preview";
|
|
4
10
|
import type {
|
|
5
11
|
IncomingHttpHeaders,
|
|
6
12
|
RequestListener,
|
|
7
13
|
IncomingMessage,
|
|
8
14
|
ServerResponse,
|
|
9
|
-
Server,
|
|
15
|
+
Server as HttpServer,
|
|
10
16
|
} from "node:http";
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import type
|
|
14
|
-
|
|
17
|
+
import type { ClientHttp2Session, ServerHttp2Stream } from "node:http2";
|
|
18
|
+
import type { Server as HttpsServer } from "node:https";
|
|
19
|
+
import type ws from "ws";
|
|
20
|
+
|
|
21
|
+
interface IWebsocket extends ws {
|
|
22
|
+
// Pipe implements .on("message", ...)
|
|
23
|
+
pipe<T>(fn: T): IWebsocket;
|
|
24
|
+
}
|
|
15
25
|
|
|
16
26
|
/**
|
|
17
27
|
* `usePreviewServer` is a React hook that creates a local development
|
|
@@ -41,13 +51,17 @@ function addCfPreviewTokenHeader(
|
|
|
41
51
|
function rewriteRemoteHostToLocalHostInHeaders(
|
|
42
52
|
headers: IncomingHttpHeaders,
|
|
43
53
|
remoteHost: string,
|
|
44
|
-
localPort: number
|
|
54
|
+
localPort: number,
|
|
55
|
+
localProtocol: "https" | "http"
|
|
45
56
|
) {
|
|
46
57
|
for (const [name, value] of Object.entries(headers)) {
|
|
47
58
|
// Rewrite the remote host to the local host.
|
|
48
59
|
if (typeof value === "string" && value.includes(remoteHost)) {
|
|
49
60
|
headers[name] = value
|
|
50
|
-
.replaceAll(
|
|
61
|
+
.replaceAll(
|
|
62
|
+
`https://${remoteHost}`,
|
|
63
|
+
`${localProtocol}://localhost:${localPort}`
|
|
64
|
+
)
|
|
51
65
|
.replaceAll(remoteHost, `localhost:${localPort}`);
|
|
52
66
|
}
|
|
53
67
|
}
|
|
@@ -56,15 +70,33 @@ function rewriteRemoteHostToLocalHostInHeaders(
|
|
|
56
70
|
export function usePreviewServer({
|
|
57
71
|
previewToken,
|
|
58
72
|
publicRoot,
|
|
59
|
-
|
|
73
|
+
localProtocol,
|
|
74
|
+
localPort: port,
|
|
75
|
+
ip,
|
|
60
76
|
}: {
|
|
61
77
|
previewToken: CfPreviewToken | undefined;
|
|
62
78
|
publicRoot: undefined | string;
|
|
63
|
-
|
|
79
|
+
localProtocol: "https" | "http";
|
|
80
|
+
localPort: number;
|
|
81
|
+
ip: string;
|
|
64
82
|
}) {
|
|
65
83
|
/** Creates an HTTP/1 proxy that sends requests over HTTP/2. */
|
|
66
|
-
const proxyServer =
|
|
67
|
-
|
|
84
|
+
const [proxyServer, setProxyServer] = useState<HttpServer | HttpsServer>();
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Create the instance of the local proxy server that will pass on
|
|
88
|
+
* requests to the preview worker.
|
|
89
|
+
*/
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (proxyServer === undefined) {
|
|
92
|
+
createProxyServer(localProtocol)
|
|
93
|
+
.then((proxy) => setProxyServer(proxy))
|
|
94
|
+
.catch(async (err) => {
|
|
95
|
+
console.error("Failed to create proxy server.", err);
|
|
96
|
+
await reportError(err);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}, [proxyServer, localProtocol]);
|
|
68
100
|
|
|
69
101
|
/**
|
|
70
102
|
* When we're not connected / getting a fresh token on changes,
|
|
@@ -80,7 +112,22 @@ export function usePreviewServer({
|
|
|
80
112
|
{ request: IncomingMessage; response: ServerResponse }[]
|
|
81
113
|
>([]);
|
|
82
114
|
|
|
115
|
+
/**
|
|
116
|
+
* The session doesn't last forever, and will eventually drop
|
|
117
|
+
* (usually within 5-15 minutes). When that happens, we simply
|
|
118
|
+
* restart the effect, effectively restarting the server. We use
|
|
119
|
+
* a state sigil as an effect dependency to do so.
|
|
120
|
+
*/
|
|
121
|
+
const [retryServerSetupSigil, setRetryServerSetupSigil] = useState<number>(0);
|
|
122
|
+
function retryServerSetup() {
|
|
123
|
+
setRetryServerSetupSigil((x) => x + 1);
|
|
124
|
+
}
|
|
125
|
+
|
|
83
126
|
useEffect(() => {
|
|
127
|
+
if (proxyServer === undefined) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
84
131
|
// If we don't have a token, that means either we're just starting up,
|
|
85
132
|
// or we're refreshing the token.
|
|
86
133
|
if (!previewToken) {
|
|
@@ -92,8 +139,8 @@ export function usePreviewServer({
|
|
|
92
139
|
// store the stream in a buffer so we can replay it later
|
|
93
140
|
streamBufferRef.current.push({ stream, headers });
|
|
94
141
|
};
|
|
95
|
-
|
|
96
|
-
cleanupListeners.push(() =>
|
|
142
|
+
proxyServer.on("stream", bufferStream);
|
|
143
|
+
cleanupListeners.push(() => proxyServer.off("stream", bufferStream));
|
|
97
144
|
|
|
98
145
|
const bufferRequestResponse = (
|
|
99
146
|
request: IncomingMessage,
|
|
@@ -103,8 +150,10 @@ export function usePreviewServer({
|
|
|
103
150
|
requestResponseBufferRef.current.push({ request, response });
|
|
104
151
|
};
|
|
105
152
|
|
|
106
|
-
|
|
107
|
-
cleanupListeners.push(() =>
|
|
153
|
+
proxyServer.on("request", bufferRequestResponse);
|
|
154
|
+
cleanupListeners.push(() =>
|
|
155
|
+
proxyServer.off("request", bufferRequestResponse)
|
|
156
|
+
);
|
|
108
157
|
return () => {
|
|
109
158
|
cleanupListeners.forEach((cleanup) => cleanup());
|
|
110
159
|
};
|
|
@@ -119,10 +168,20 @@ export function usePreviewServer({
|
|
|
119
168
|
const remote = connect(`https://${previewToken.host}`);
|
|
120
169
|
cleanupListeners.push(() => remote.destroy());
|
|
121
170
|
|
|
171
|
+
// As mentioned above, the session may die at any point,
|
|
172
|
+
// so we need to restart the effect.
|
|
173
|
+
remote.on("close", retryServerSetup);
|
|
174
|
+
cleanupListeners.push(() => remote.off("close", retryServerSetup));
|
|
175
|
+
|
|
122
176
|
/** HTTP/2 -> HTTP/2 */
|
|
123
|
-
const handleStream = createStreamHandler(
|
|
124
|
-
|
|
125
|
-
|
|
177
|
+
const handleStream = createStreamHandler(
|
|
178
|
+
previewToken,
|
|
179
|
+
remote,
|
|
180
|
+
port,
|
|
181
|
+
localProtocol
|
|
182
|
+
);
|
|
183
|
+
proxyServer.on("stream", handleStream);
|
|
184
|
+
cleanupListeners.push(() => proxyServer.off("stream", handleStream));
|
|
126
185
|
|
|
127
186
|
// flush and replay buffered streams
|
|
128
187
|
streamBufferRef.current.forEach((buffer) =>
|
|
@@ -155,7 +214,8 @@ export function usePreviewServer({
|
|
|
155
214
|
rewriteRemoteHostToLocalHostInHeaders(
|
|
156
215
|
responseHeaders,
|
|
157
216
|
previewToken.host,
|
|
158
|
-
port
|
|
217
|
+
port,
|
|
218
|
+
localProtocol
|
|
159
219
|
);
|
|
160
220
|
for (const name of Object.keys(responseHeaders)) {
|
|
161
221
|
if (name.startsWith(":")) {
|
|
@@ -173,8 +233,10 @@ export function usePreviewServer({
|
|
|
173
233
|
? createHandleAssetsRequest(assetPath, handleRequest)
|
|
174
234
|
: handleRequest;
|
|
175
235
|
|
|
176
|
-
|
|
177
|
-
cleanupListeners.push(() =>
|
|
236
|
+
proxyServer.on("request", actualHandleRequest);
|
|
237
|
+
cleanupListeners.push(() =>
|
|
238
|
+
proxyServer.off("request", actualHandleRequest)
|
|
239
|
+
);
|
|
178
240
|
|
|
179
241
|
// flush and replay buffered requests
|
|
180
242
|
requestResponseBufferRef.current.forEach(({ request, response }) =>
|
|
@@ -191,38 +253,67 @@ export function usePreviewServer({
|
|
|
191
253
|
const { headers, url } = message;
|
|
192
254
|
addCfPreviewTokenHeader(headers, previewToken.value);
|
|
193
255
|
headers["host"] = previewToken.host;
|
|
194
|
-
const localWebsocket = new WebSocket(message, socket, body);
|
|
256
|
+
const localWebsocket = new WebSocket(message, socket, body) as IWebsocket;
|
|
195
257
|
// TODO(soon): Custom WebSocket protocol is not working?
|
|
196
258
|
const remoteWebsocketClient = new WebSocket.Client(
|
|
197
259
|
`wss://${previewToken.host}${url}`,
|
|
198
260
|
[],
|
|
199
261
|
{ headers }
|
|
200
|
-
);
|
|
262
|
+
) as IWebsocket;
|
|
201
263
|
localWebsocket.pipe(remoteWebsocketClient).pipe(localWebsocket);
|
|
202
264
|
// We close down websockets whenever we refresh the token.
|
|
203
265
|
cleanupListeners.push(() => {
|
|
204
|
-
localWebsocket.
|
|
205
|
-
remoteWebsocketClient.
|
|
266
|
+
localWebsocket.close();
|
|
267
|
+
remoteWebsocketClient.close();
|
|
206
268
|
});
|
|
207
269
|
};
|
|
208
|
-
|
|
209
|
-
cleanupListeners.push(() =>
|
|
270
|
+
proxyServer.on("upgrade", handleUpgrade);
|
|
271
|
+
cleanupListeners.push(() => proxyServer.off("upgrade", handleUpgrade));
|
|
210
272
|
|
|
211
273
|
return () => {
|
|
212
|
-
cleanupListeners.forEach((
|
|
274
|
+
cleanupListeners.forEach((cleanup) => cleanup());
|
|
213
275
|
};
|
|
214
|
-
}, [
|
|
276
|
+
}, [
|
|
277
|
+
previewToken,
|
|
278
|
+
publicRoot,
|
|
279
|
+
port,
|
|
280
|
+
localProtocol,
|
|
281
|
+
proxyServer,
|
|
282
|
+
// We use a state value as a sigil to trigger reconnecting the server.
|
|
283
|
+
// It's not used inside the effect, so react-hooks/exhaustive-deps
|
|
284
|
+
// doesn't complain if it's not included in the dependency array.
|
|
285
|
+
// But its presence is critical, so Do NOT remove it from the dependency list.
|
|
286
|
+
retryServerSetupSigil,
|
|
287
|
+
]);
|
|
215
288
|
|
|
216
289
|
// Start/stop the server whenever the
|
|
217
290
|
// containing component is mounted/unmounted.
|
|
218
291
|
useEffect(() => {
|
|
219
|
-
|
|
220
|
-
|
|
292
|
+
const abortController = new AbortController();
|
|
293
|
+
if (proxyServer === undefined) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
waitForPortToBeAvailable(port, {
|
|
298
|
+
retryPeriod: 200,
|
|
299
|
+
timeout: 2000,
|
|
300
|
+
abortSignal: abortController.signal,
|
|
301
|
+
})
|
|
302
|
+
.then(() => {
|
|
303
|
+
proxyServer.listen(port, ip);
|
|
304
|
+
console.log(`⬣ Listening at ${localProtocol}://${ip}:${port}`);
|
|
305
|
+
})
|
|
306
|
+
.catch((err) => {
|
|
307
|
+
if ((err as { code: string }).code !== "ABORT_ERR") {
|
|
308
|
+
console.error(`⬣ Failed to start server: ${err}`);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
221
311
|
|
|
222
312
|
return () => {
|
|
223
|
-
|
|
313
|
+
proxyServer.close();
|
|
314
|
+
abortController.abort();
|
|
224
315
|
};
|
|
225
|
-
}, [port,
|
|
316
|
+
}, [port, ip, proxyServer, localProtocol]);
|
|
226
317
|
}
|
|
227
318
|
|
|
228
319
|
function createHandleAssetsRequest(
|
|
@@ -250,8 +341,15 @@ const HTTP1_HEADERS = new Set([
|
|
|
250
341
|
"http2-settings",
|
|
251
342
|
]);
|
|
252
343
|
|
|
253
|
-
function createProxyServer(
|
|
254
|
-
|
|
344
|
+
async function createProxyServer(
|
|
345
|
+
localProtocol: "https" | "http"
|
|
346
|
+
): Promise<HttpServer | HttpsServer> {
|
|
347
|
+
const server: HttpServer | HttpsServer =
|
|
348
|
+
localProtocol === "https"
|
|
349
|
+
? createHttpsServer(await getHttpsOptions())
|
|
350
|
+
: createHttpServer();
|
|
351
|
+
|
|
352
|
+
return server
|
|
255
353
|
.on("request", function (req, res) {
|
|
256
354
|
// log all requests
|
|
257
355
|
console.log(
|
|
@@ -280,7 +378,8 @@ function createProxyServer() {
|
|
|
280
378
|
function createStreamHandler(
|
|
281
379
|
previewToken: CfPreviewToken,
|
|
282
380
|
remote: ClientHttp2Session,
|
|
283
|
-
|
|
381
|
+
localPort: number,
|
|
382
|
+
localProtocol: "https" | "http"
|
|
284
383
|
) {
|
|
285
384
|
return function handleStream(
|
|
286
385
|
stream: ServerHttp2Stream,
|
|
@@ -293,10 +392,64 @@ function createStreamHandler(
|
|
|
293
392
|
rewriteRemoteHostToLocalHostInHeaders(
|
|
294
393
|
responseHeaders,
|
|
295
394
|
previewToken.host,
|
|
296
|
-
|
|
395
|
+
localPort,
|
|
396
|
+
localProtocol
|
|
297
397
|
);
|
|
298
398
|
stream.respond(responseHeaders);
|
|
299
399
|
request.pipe(stream, { end: true });
|
|
300
400
|
});
|
|
301
401
|
};
|
|
302
402
|
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* A helper function that waits for a port to be available.
|
|
406
|
+
*/
|
|
407
|
+
export async function waitForPortToBeAvailable(
|
|
408
|
+
port: number,
|
|
409
|
+
options: { retryPeriod: number; timeout: number; abortSignal: AbortSignal }
|
|
410
|
+
): Promise<void> {
|
|
411
|
+
return new Promise((resolve, reject) => {
|
|
412
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
413
|
+
(options.abortSignal as any).addEventListener("abort", () => {
|
|
414
|
+
const abortError = new Error("waitForPortToBeAvailable() aborted");
|
|
415
|
+
(abortError as Error & { code: string }).code = "ABORT_ERR";
|
|
416
|
+
doReject(abortError);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
const timeout = setTimeout(() => {
|
|
420
|
+
doReject(new Error(`Timed out waiting for port ${port}`));
|
|
421
|
+
}, options.timeout);
|
|
422
|
+
|
|
423
|
+
const interval = setInterval(checkPort, options.retryPeriod);
|
|
424
|
+
checkPort();
|
|
425
|
+
|
|
426
|
+
function doResolve() {
|
|
427
|
+
clearTimeout(timeout);
|
|
428
|
+
clearInterval(interval);
|
|
429
|
+
resolve();
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function doReject(err: unknown) {
|
|
433
|
+
clearInterval(interval);
|
|
434
|
+
clearTimeout(timeout);
|
|
435
|
+
reject(err);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function checkPort() {
|
|
439
|
+
// Testing whether a port is 'available' involves simply
|
|
440
|
+
// trying to make a server listen on that port, and retrying
|
|
441
|
+
// until it succeeds.
|
|
442
|
+
const server = createHttpServer();
|
|
443
|
+
server.on("error", (err) => {
|
|
444
|
+
// @ts-expect-error non standard property on Error
|
|
445
|
+
if (err.code !== "EADDRINUSE") {
|
|
446
|
+
doReject(err);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
server.listen(port, () => {
|
|
450
|
+
server.close();
|
|
451
|
+
doResolve();
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
}
|