wrangler 0.0.0-e043479 → 0.0.0-e0a0509
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 +27 -14
- package/bin/wrangler.js +13 -1
- package/miniflare-dist/index.mjs +4886 -0
- package/package.json +38 -25
- package/pages/functions/buildPlugin.ts +92 -0
- package/pages/functions/buildWorker.ts +80 -4
- package/pages/functions/filepath-routing.test.ts +136 -25
- package/pages/functions/filepath-routing.ts +14 -3
- package/pages/functions/routes.ts +6 -1
- package/pages/functions/template-plugin.ts +146 -0
- package/pages/functions/template-worker.ts +21 -8
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +1294 -355
- package/src/__tests__/dev.test.tsx +592 -12
- package/src/__tests__/guess-worker-format.test.ts +37 -18
- package/src/__tests__/helpers/mock-account-id.ts +4 -0
- package/src/__tests__/helpers/mock-cfetch.ts +8 -4
- package/src/__tests__/helpers/mock-console.ts +20 -3
- package/src/__tests__/helpers/mock-dialogs.ts +26 -1
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +27 -0
- package/src/__tests__/helpers/mock-kv.ts +9 -16
- package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
- package/src/__tests__/helpers/run-in-tmp.ts +10 -0
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/https-options.test.ts +130 -0
- package/src/__tests__/index.test.ts +129 -753
- package/src/__tests__/init.test.ts +1516 -0
- package/src/__tests__/jest.setup.ts +13 -5
- package/src/__tests__/kv.test.ts +208 -174
- package/src/__tests__/logger.test.ts +115 -0
- package/src/__tests__/pages.test.ts +327 -0
- package/src/__tests__/parse.test.ts +4 -0
- package/src/__tests__/publish.test.ts +3313 -505
- package/src/__tests__/r2.test.ts +38 -26
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +55 -34
- package/src/__tests__/tail.test.ts +146 -20
- package/src/__tests__/user.test.ts +131 -0
- package/src/__tests__/whoami.test.tsx +26 -7
- package/src/bundle.ts +43 -7
- package/src/cfetch/internal.ts +9 -5
- package/src/cli.ts +5 -11
- package/src/config/README.md +7 -7
- package/src/config/config.ts +69 -105
- package/src/config/environment.ts +93 -4
- package/src/config/index.ts +15 -4
- package/src/config/validation-helpers.ts +228 -23
- package/src/config/validation.ts +559 -154
- package/src/config-cache.ts +45 -0
- package/src/{api/preview.ts → create-worker-preview.ts} +41 -13
- package/src/{api/form_data.ts → create-worker-upload-form.ts} +33 -10
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +418 -0
- package/src/dev/local.tsx +312 -0
- package/src/dev/remote.tsx +213 -0
- package/src/dev/use-esbuild.ts +118 -0
- package/src/dialogs.tsx +16 -6
- package/src/entry.ts +222 -12
- package/src/environment-variables.ts +3 -1
- package/src/errors.ts +11 -0
- package/src/https-options.ts +123 -0
- package/src/index.tsx +878 -482
- package/src/inspect.ts +25 -11
- package/src/intl-polyfill.d.ts +139 -0
- package/src/{kv.tsx → kv.ts} +7 -27
- package/src/logger.ts +66 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +41 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +94 -6
- package/src/open-in-browser.ts +18 -5
- package/src/package-manager.ts +9 -8
- package/src/pages.tsx +892 -62
- package/src/parse.ts +4 -2
- package/src/proxy.ts +128 -55
- package/src/publish.ts +305 -199
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +88 -33
- package/src/tail/index.ts +2 -2
- package/src/tail/printing.ts +13 -8
- package/src/update-check.ts +19 -0
- package/src/user.tsx +244 -104
- package/src/whoami.tsx +2 -1
- package/src/{api/worker.ts → worker.ts} +15 -29
- package/templates/gitignore +171 -0
- package/templates/static-asset-facade.js +1 -1
- package/wrangler-dist/cli.js +23568 -50489
- package/src/__tests__/helpers/mock-user.ts +0 -27
- package/src/__tests__/logout.test.ts +0 -50
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/dev.tsx +0 -960
- package/src/guess-worker-format.ts +0 -68
- package/src/reporting.ts +0 -115
- package/wrangler-dist/cli.js.map +0 -7
package/src/parse.ts
CHANGED
|
@@ -75,9 +75,11 @@ type TomlError = Error & {
|
|
|
75
75
|
/**
|
|
76
76
|
* A wrapper around `TOML.parse` that throws a `ParseError`.
|
|
77
77
|
*/
|
|
78
|
-
export function parseTOML(input: string, file?: string):
|
|
78
|
+
export function parseTOML(input: string, file?: string): TOML.JsonMap | never {
|
|
79
79
|
try {
|
|
80
|
-
|
|
80
|
+
// Normalize CRLF to LF to avoid hitting https://github.com/iarna/iarna-toml/issues/33.
|
|
81
|
+
const normalizedInput = input.replace(/\r\n$/g, "\n");
|
|
82
|
+
return TOML.parse(normalizedInput);
|
|
81
83
|
} catch (err) {
|
|
82
84
|
const { name, message, line, col } = err as TomlError;
|
|
83
85
|
if (name !== TOML_ERROR_NAME) {
|
package/src/proxy.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import { createServer } from "node:http";
|
|
1
|
+
import { createServer as createHttpServer } from "node:http";
|
|
2
2
|
import { connect } from "node:http2";
|
|
3
|
+
import { createServer as createHttpsServer } from "node:https";
|
|
3
4
|
import WebSocket from "faye-websocket";
|
|
4
5
|
import { useEffect, useRef, useState } from "react";
|
|
5
6
|
import serveStatic from "serve-static";
|
|
6
|
-
import
|
|
7
|
+
import { getHttpsOptions } from "./https-options";
|
|
8
|
+
import { logger } from "./logger";
|
|
9
|
+
import type { CfPreviewToken } from "./create-worker-preview";
|
|
7
10
|
import type {
|
|
8
11
|
IncomingHttpHeaders,
|
|
9
12
|
RequestListener,
|
|
10
13
|
IncomingMessage,
|
|
11
14
|
ServerResponse,
|
|
12
|
-
Server,
|
|
15
|
+
Server as HttpServer,
|
|
13
16
|
} from "node:http";
|
|
14
17
|
import type { ClientHttp2Session, ServerHttp2Stream } from "node:http2";
|
|
18
|
+
import type { Server as HttpsServer } from "node:https";
|
|
15
19
|
import type ws from "ws";
|
|
16
20
|
|
|
17
21
|
interface IWebsocket extends ws {
|
|
@@ -47,13 +51,17 @@ function addCfPreviewTokenHeader(
|
|
|
47
51
|
function rewriteRemoteHostToLocalHostInHeaders(
|
|
48
52
|
headers: IncomingHttpHeaders,
|
|
49
53
|
remoteHost: string,
|
|
50
|
-
localPort: number
|
|
54
|
+
localPort: number,
|
|
55
|
+
localProtocol: "https" | "http"
|
|
51
56
|
) {
|
|
52
57
|
for (const [name, value] of Object.entries(headers)) {
|
|
53
58
|
// Rewrite the remote host to the local host.
|
|
54
59
|
if (typeof value === "string" && value.includes(remoteHost)) {
|
|
55
60
|
headers[name] = value
|
|
56
|
-
.replaceAll(
|
|
61
|
+
.replaceAll(
|
|
62
|
+
`https://${remoteHost}`,
|
|
63
|
+
`${localProtocol}://localhost:${localPort}`
|
|
64
|
+
)
|
|
57
65
|
.replaceAll(remoteHost, `localhost:${localPort}`);
|
|
58
66
|
}
|
|
59
67
|
}
|
|
@@ -62,15 +70,32 @@ function rewriteRemoteHostToLocalHostInHeaders(
|
|
|
62
70
|
export function usePreviewServer({
|
|
63
71
|
previewToken,
|
|
64
72
|
publicRoot,
|
|
65
|
-
|
|
73
|
+
localProtocol,
|
|
74
|
+
localPort: port,
|
|
75
|
+
ip,
|
|
66
76
|
}: {
|
|
67
77
|
previewToken: CfPreviewToken | undefined;
|
|
68
|
-
publicRoot:
|
|
69
|
-
|
|
78
|
+
publicRoot: string | undefined;
|
|
79
|
+
localProtocol: "https" | "http";
|
|
80
|
+
localPort: number;
|
|
81
|
+
ip: string;
|
|
70
82
|
}) {
|
|
71
83
|
/** Creates an HTTP/1 proxy that sends requests over HTTP/2. */
|
|
72
|
-
const proxyServer =
|
|
73
|
-
|
|
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
|
+
logger.error("Failed to create proxy server:", err);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}, [proxyServer, localProtocol]);
|
|
74
99
|
|
|
75
100
|
/**
|
|
76
101
|
* When we're not connected / getting a fresh token on changes,
|
|
@@ -98,6 +123,10 @@ export function usePreviewServer({
|
|
|
98
123
|
}
|
|
99
124
|
|
|
100
125
|
useEffect(() => {
|
|
126
|
+
if (proxyServer === undefined) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
101
130
|
// If we don't have a token, that means either we're just starting up,
|
|
102
131
|
// or we're refreshing the token.
|
|
103
132
|
if (!previewToken) {
|
|
@@ -109,8 +138,8 @@ export function usePreviewServer({
|
|
|
109
138
|
// store the stream in a buffer so we can replay it later
|
|
110
139
|
streamBufferRef.current.push({ stream, headers });
|
|
111
140
|
};
|
|
112
|
-
|
|
113
|
-
cleanupListeners.push(() =>
|
|
141
|
+
proxyServer.on("stream", bufferStream);
|
|
142
|
+
cleanupListeners.push(() => proxyServer.off("stream", bufferStream));
|
|
114
143
|
|
|
115
144
|
const bufferRequestResponse = (
|
|
116
145
|
request: IncomingMessage,
|
|
@@ -120,8 +149,10 @@ export function usePreviewServer({
|
|
|
120
149
|
requestResponseBufferRef.current.push({ request, response });
|
|
121
150
|
};
|
|
122
151
|
|
|
123
|
-
|
|
124
|
-
cleanupListeners.push(() =>
|
|
152
|
+
proxyServer.on("request", bufferRequestResponse);
|
|
153
|
+
cleanupListeners.push(() =>
|
|
154
|
+
proxyServer.off("request", bufferRequestResponse)
|
|
155
|
+
);
|
|
125
156
|
return () => {
|
|
126
157
|
cleanupListeners.forEach((cleanup) => cleanup());
|
|
127
158
|
};
|
|
@@ -142,9 +173,14 @@ export function usePreviewServer({
|
|
|
142
173
|
cleanupListeners.push(() => remote.off("close", retryServerSetup));
|
|
143
174
|
|
|
144
175
|
/** HTTP/2 -> HTTP/2 */
|
|
145
|
-
const handleStream = createStreamHandler(
|
|
146
|
-
|
|
147
|
-
|
|
176
|
+
const handleStream = createStreamHandler(
|
|
177
|
+
previewToken,
|
|
178
|
+
remote,
|
|
179
|
+
port,
|
|
180
|
+
localProtocol
|
|
181
|
+
);
|
|
182
|
+
proxyServer.on("stream", handleStream);
|
|
183
|
+
cleanupListeners.push(() => proxyServer.off("stream", handleStream));
|
|
148
184
|
|
|
149
185
|
// flush and replay buffered streams
|
|
150
186
|
streamBufferRef.current.forEach((buffer) =>
|
|
@@ -174,10 +210,15 @@ export function usePreviewServer({
|
|
|
174
210
|
const request = message.pipe(remote.request(headers));
|
|
175
211
|
request.on("response", (responseHeaders) => {
|
|
176
212
|
const status = responseHeaders[":status"] ?? 500;
|
|
213
|
+
|
|
214
|
+
// log all requests to terminal
|
|
215
|
+
logger.log(new Date().toLocaleTimeString(), method, url, status);
|
|
216
|
+
|
|
177
217
|
rewriteRemoteHostToLocalHostInHeaders(
|
|
178
218
|
responseHeaders,
|
|
179
219
|
previewToken.host,
|
|
180
|
-
port
|
|
220
|
+
port,
|
|
221
|
+
localProtocol
|
|
181
222
|
);
|
|
182
223
|
for (const name of Object.keys(responseHeaders)) {
|
|
183
224
|
if (name.startsWith(":")) {
|
|
@@ -195,8 +236,10 @@ export function usePreviewServer({
|
|
|
195
236
|
? createHandleAssetsRequest(assetPath, handleRequest)
|
|
196
237
|
: handleRequest;
|
|
197
238
|
|
|
198
|
-
|
|
199
|
-
cleanupListeners.push(() =>
|
|
239
|
+
proxyServer.on("request", actualHandleRequest);
|
|
240
|
+
cleanupListeners.push(() =>
|
|
241
|
+
proxyServer.off("request", actualHandleRequest)
|
|
242
|
+
);
|
|
200
243
|
|
|
201
244
|
// flush and replay buffered requests
|
|
202
245
|
requestResponseBufferRef.current.forEach(({ request, response }) =>
|
|
@@ -227,8 +270,8 @@ export function usePreviewServer({
|
|
|
227
270
|
remoteWebsocketClient.close();
|
|
228
271
|
});
|
|
229
272
|
};
|
|
230
|
-
|
|
231
|
-
cleanupListeners.push(() =>
|
|
273
|
+
proxyServer.on("upgrade", handleUpgrade);
|
|
274
|
+
cleanupListeners.push(() => proxyServer.off("upgrade", handleUpgrade));
|
|
232
275
|
|
|
233
276
|
return () => {
|
|
234
277
|
cleanupListeners.forEach((cleanup) => cleanup());
|
|
@@ -237,7 +280,8 @@ export function usePreviewServer({
|
|
|
237
280
|
previewToken,
|
|
238
281
|
publicRoot,
|
|
239
282
|
port,
|
|
240
|
-
|
|
283
|
+
localProtocol,
|
|
284
|
+
proxyServer,
|
|
241
285
|
// We use a state value as a sigil to trigger reconnecting the server.
|
|
242
286
|
// It's not used inside the effect, so react-hooks/exhaustive-deps
|
|
243
287
|
// doesn't complain if it's not included in the dependency array.
|
|
@@ -248,19 +292,31 @@ export function usePreviewServer({
|
|
|
248
292
|
// Start/stop the server whenever the
|
|
249
293
|
// containing component is mounted/unmounted.
|
|
250
294
|
useEffect(() => {
|
|
251
|
-
|
|
295
|
+
const abortController = new AbortController();
|
|
296
|
+
if (proxyServer === undefined) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
waitForPortToBeAvailable(port, {
|
|
301
|
+
retryPeriod: 200,
|
|
302
|
+
timeout: 2000,
|
|
303
|
+
abortSignal: abortController.signal,
|
|
304
|
+
})
|
|
252
305
|
.then(() => {
|
|
253
|
-
|
|
254
|
-
|
|
306
|
+
proxyServer.listen(port, ip);
|
|
307
|
+
logger.log(`⬣ Listening at ${localProtocol}://${ip}:${port}`);
|
|
255
308
|
})
|
|
256
309
|
.catch((err) => {
|
|
257
|
-
|
|
310
|
+
if ((err as { code: string }).code !== "ABORT_ERR") {
|
|
311
|
+
logger.error(`Failed to start server: ${err}`);
|
|
312
|
+
}
|
|
258
313
|
});
|
|
259
314
|
|
|
260
315
|
return () => {
|
|
261
|
-
|
|
316
|
+
proxyServer.close();
|
|
317
|
+
abortController.abort();
|
|
262
318
|
};
|
|
263
|
-
}, [port,
|
|
319
|
+
}, [port, ip, proxyServer, localProtocol]);
|
|
264
320
|
}
|
|
265
321
|
|
|
266
322
|
function createHandleAssetsRequest(
|
|
@@ -288,20 +344,18 @@ const HTTP1_HEADERS = new Set([
|
|
|
288
344
|
"http2-settings",
|
|
289
345
|
]);
|
|
290
346
|
|
|
291
|
-
function createProxyServer(
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
);
|
|
301
|
-
})
|
|
347
|
+
async function createProxyServer(
|
|
348
|
+
localProtocol: "https" | "http"
|
|
349
|
+
): Promise<HttpServer | HttpsServer> {
|
|
350
|
+
const server: HttpServer | HttpsServer =
|
|
351
|
+
localProtocol === "https"
|
|
352
|
+
? createHttpsServer(await getHttpsOptions())
|
|
353
|
+
: createHttpServer();
|
|
354
|
+
|
|
355
|
+
return server
|
|
302
356
|
.on("upgrade", (req) => {
|
|
303
357
|
// log all websocket connections
|
|
304
|
-
|
|
358
|
+
logger.log(
|
|
305
359
|
new Date().toLocaleTimeString(),
|
|
306
360
|
req.method,
|
|
307
361
|
req.url,
|
|
@@ -311,14 +365,15 @@ function createProxyServer() {
|
|
|
311
365
|
})
|
|
312
366
|
.on("error", (err) => {
|
|
313
367
|
// log all connection errors
|
|
314
|
-
|
|
368
|
+
logger.error(new Date().toLocaleTimeString(), err);
|
|
315
369
|
});
|
|
316
370
|
}
|
|
317
371
|
|
|
318
372
|
function createStreamHandler(
|
|
319
373
|
previewToken: CfPreviewToken,
|
|
320
374
|
remote: ClientHttp2Session,
|
|
321
|
-
|
|
375
|
+
localPort: number,
|
|
376
|
+
localProtocol: "https" | "http"
|
|
322
377
|
) {
|
|
323
378
|
return function handleStream(
|
|
324
379
|
stream: ServerHttp2Stream,
|
|
@@ -331,7 +386,8 @@ function createStreamHandler(
|
|
|
331
386
|
rewriteRemoteHostToLocalHostInHeaders(
|
|
332
387
|
responseHeaders,
|
|
333
388
|
previewToken.host,
|
|
334
|
-
|
|
389
|
+
localPort,
|
|
390
|
+
localProtocol
|
|
335
391
|
);
|
|
336
392
|
stream.respond(responseHeaders);
|
|
337
393
|
request.pipe(stream, { end: true });
|
|
@@ -344,33 +400,50 @@ function createStreamHandler(
|
|
|
344
400
|
*/
|
|
345
401
|
export async function waitForPortToBeAvailable(
|
|
346
402
|
port: number,
|
|
347
|
-
options: { retryPeriod: number; timeout: number }
|
|
403
|
+
options: { retryPeriod: number; timeout: number; abortSignal: AbortSignal }
|
|
348
404
|
): Promise<void> {
|
|
349
405
|
return new Promise((resolve, reject) => {
|
|
406
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
407
|
+
(options.abortSignal as any).addEventListener("abort", () => {
|
|
408
|
+
const abortError = new Error("waitForPortToBeAvailable() aborted");
|
|
409
|
+
(abortError as Error & { code: string }).code = "ABORT_ERR";
|
|
410
|
+
doReject(abortError);
|
|
411
|
+
});
|
|
412
|
+
|
|
350
413
|
const timeout = setTimeout(() => {
|
|
351
|
-
|
|
414
|
+
doReject(new Error(`Timed out waiting for port ${port}`));
|
|
352
415
|
}, options.timeout);
|
|
353
416
|
|
|
417
|
+
const interval = setInterval(checkPort, options.retryPeriod);
|
|
418
|
+
checkPort();
|
|
419
|
+
|
|
420
|
+
function doResolve() {
|
|
421
|
+
clearTimeout(timeout);
|
|
422
|
+
clearInterval(interval);
|
|
423
|
+
resolve();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function doReject(err: unknown) {
|
|
427
|
+
clearInterval(interval);
|
|
428
|
+
clearTimeout(timeout);
|
|
429
|
+
reject(err);
|
|
430
|
+
}
|
|
431
|
+
|
|
354
432
|
function checkPort() {
|
|
355
433
|
// Testing whether a port is 'available' involves simply
|
|
356
434
|
// trying to make a server listen on that port, and retrying
|
|
357
435
|
// until it succeeds.
|
|
358
|
-
const server =
|
|
436
|
+
const server = createHttpServer();
|
|
359
437
|
server.on("error", (err) => {
|
|
360
438
|
// @ts-expect-error non standard property on Error
|
|
361
|
-
if (err.code
|
|
362
|
-
|
|
363
|
-
} else {
|
|
364
|
-
reject(err);
|
|
439
|
+
if (err.code !== "EADDRINUSE") {
|
|
440
|
+
doReject(err);
|
|
365
441
|
}
|
|
366
442
|
});
|
|
367
443
|
server.listen(port, () => {
|
|
368
444
|
server.close();
|
|
369
|
-
|
|
370
|
-
resolve();
|
|
445
|
+
doResolve();
|
|
371
446
|
});
|
|
372
447
|
}
|
|
373
|
-
|
|
374
|
-
checkPort();
|
|
375
448
|
});
|
|
376
449
|
}
|