wrangler 0.0.0-e1c615f → 0.0.0-e22f9d7

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/src/pages.tsx CHANGED
@@ -4,7 +4,7 @@ import assert from "assert";
4
4
  import type { BuilderCallback } from "yargs";
5
5
  import { join } from "path";
6
6
  import { tmpdir } from "os";
7
- import { existsSync, lstatSync, readFileSync } from "fs";
7
+ import { existsSync, lstatSync, readFileSync, writeFileSync } from "fs";
8
8
  import { execSync, spawn } from "child_process";
9
9
  import { URL } from "url";
10
10
  import { getType } from "mime";
@@ -642,16 +642,20 @@ const RUNNING_BUILDERS: BuildResult[] = [];
642
642
 
643
643
  async function buildFunctions({
644
644
  scriptPath,
645
+ outputConfigPath,
645
646
  functionsDirectory,
646
647
  minify = false,
647
648
  sourcemap = false,
649
+ fallbackService = "ASSETS",
648
650
  watch = false,
649
651
  onEnd,
650
652
  }: {
651
653
  scriptPath: string;
654
+ outputConfigPath?: string;
652
655
  functionsDirectory: string;
653
656
  minify?: boolean;
654
657
  sourcemap?: boolean;
658
+ fallbackService?: string;
655
659
  watch?: boolean;
656
660
  onEnd?: () => void;
657
661
  }) {
@@ -666,6 +670,13 @@ async function buildFunctions({
666
670
  baseURL: "/",
667
671
  });
668
672
 
673
+ if (outputConfigPath) {
674
+ writeFileSync(
675
+ outputConfigPath,
676
+ JSON.stringify({ ...config, baseURL: "/" }, null, 2)
677
+ );
678
+ }
679
+
669
680
  await writeRoutesModule({
670
681
  config,
671
682
  srcDir: functionsDirectory,
@@ -678,6 +689,7 @@ async function buildFunctions({
678
689
  outfile: scriptPath,
679
690
  minify,
680
691
  sourcemap,
692
+ fallbackService,
681
693
  watch,
682
694
  onEnd,
683
695
  })
@@ -738,6 +750,11 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
738
750
  description: "Durable Object to bind (NAME=CLASS)",
739
751
  alias: "o",
740
752
  },
753
+ "live-reload": {
754
+ type: "boolean",
755
+ default: false,
756
+ description: "Auto reload HTML pages when change is detected",
757
+ },
741
758
  // TODO: Miniflare user options
742
759
  });
743
760
  },
@@ -750,6 +767,7 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
750
767
  binding: bindings = [],
751
768
  kv: kvs = [],
752
769
  do: durableObjects = [],
770
+ "live-reload": liveReload,
753
771
  "--": remaining = [],
754
772
  }) => {
755
773
  if (!local) {
@@ -774,7 +792,10 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
774
792
 
775
793
  let miniflareArgs: MiniflareOptions = {};
776
794
 
777
- let scriptReady = true;
795
+ let scriptReadyResolve;
796
+ const scriptReadyPromise = new Promise(
797
+ (resolve) => (scriptReadyResolve = resolve)
798
+ );
778
799
 
779
800
  if (usingFunctions) {
780
801
  const scriptPath = join(tmpdir(), "./functionsWorker.js");
@@ -786,22 +807,19 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
786
807
  functionsDirectory,
787
808
  sourcemap: true,
788
809
  watch: true,
789
- onEnd: () => {
790
- scriptReady = true;
791
- },
810
+ onEnd: () => scriptReadyResolve(),
792
811
  });
793
812
 
794
813
  watch([functionsDirectory], {
795
814
  persistent: true,
815
+ ignoreInitial: true,
796
816
  }).on("all", async () => {
797
817
  await buildFunctions({
798
818
  scriptPath,
799
819
  functionsDirectory,
800
820
  sourcemap: true,
801
821
  watch: true,
802
- onEnd: () => {
803
- scriptReady = true;
804
- },
822
+ onEnd: () => scriptReadyResolve(),
805
823
  });
806
824
  });
807
825
 
@@ -838,6 +856,12 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
838
856
  const { Miniflare, Log, LogLevel } = await import("miniflare");
839
857
  const { Response, fetch } = await import("@miniflare/core");
840
858
 
859
+ // Wait for esbuild to finish building before starting Miniflare.
860
+ // This must be before the call to `new Miniflare`, as that will
861
+ // asynchronously start loading the script. `await startServer()`
862
+ // internally just waits for that promise to resolve.
863
+ await scriptReadyPromise;
864
+
841
865
  // Should only be called if no proxyPort, using `assert.fail()` here
842
866
  // means the type of `assetsFetch` is still `typeof fetch`
843
867
  const assetsFetch = proxyPort
@@ -903,22 +927,29 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
903
927
  kvPersist: true,
904
928
  durableObjectsPersist: true,
905
929
  cachePersist: true,
930
+ liveReload,
906
931
 
907
932
  ...miniflareArgs,
908
933
  });
909
934
 
910
- // Wait for esbuild to finish building the script
911
- while (!scriptReady) {}
912
-
913
935
  try {
914
936
  // `startServer` might throw if user code contains errors
915
937
  const server = await miniflare.startServer();
916
938
  console.log(`Serving at http://127.0.0.1:${port}/`);
917
939
 
918
940
  if (process.env.BROWSER !== "none") {
919
- try {
920
- await open(`http://127.0.0.1:${port}/`);
921
- } catch {}
941
+ const childProcess = await open(`http://127.0.0.1:${port}/`);
942
+ // fail silently if the open command doesn't work (e.g. in GitHub Codespaces)
943
+ childProcess.on("error", (_err) => {});
944
+ }
945
+
946
+ if (directory !== undefined && liveReload) {
947
+ watch([directory], {
948
+ persistent: true,
949
+ ignoreInitial: true,
950
+ }).on("all", async () => {
951
+ await miniflare.reload();
952
+ });
922
953
  }
923
954
 
924
955
  EXIT_CALLBACKS.push(() => {
@@ -933,34 +964,66 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
933
964
  )
934
965
  .command("functions", "Cloudflare Pages Functions", (yargs) =>
935
966
  yargs.command(
936
- "build",
967
+ "build [directory]",
937
968
  "Compile a folder of Cloudflare Pages Functions into a single Worker",
938
969
  (yargs) =>
939
- yargs.options({
940
- "script-path": {
970
+ yargs
971
+ .positional("directory", {
941
972
  type: "string",
942
- default: "_worker.js",
943
- description: "The location of the output Worker script",
944
- },
945
- minify: {
946
- type: "boolean",
947
- default: false,
948
- description: "Minify the output Worker script",
949
- },
950
- sourcemap: {
951
- type: "boolean",
952
- default: false,
953
- description: "Generate a sourcemap for the output Worker script",
954
- },
955
- }),
956
- async ({ "script-path": scriptPath, minify, sourcemap }) => {
957
- const functionsDirectory = "./functions";
958
-
973
+ default: "functions",
974
+ description: "The directory of Pages Functions",
975
+ })
976
+ .options({
977
+ "script-path": {
978
+ type: "string",
979
+ default: "_worker.js",
980
+ description: "The location of the output Worker script",
981
+ },
982
+ "output-config-path": {
983
+ type: "string",
984
+ description: "The location for the output config file",
985
+ },
986
+ minify: {
987
+ type: "boolean",
988
+ default: false,
989
+ description: "Minify the output Worker script",
990
+ },
991
+ sourcemap: {
992
+ type: "boolean",
993
+ default: false,
994
+ description:
995
+ "Generate a sourcemap for the output Worker script",
996
+ },
997
+ "fallback-service": {
998
+ type: "string",
999
+ default: "ASSETS",
1000
+ description:
1001
+ "The service to fallback to at the end of the `next` chain. Setting to '' will fallback to the global `fetch`.",
1002
+ },
1003
+ watch: {
1004
+ type: "boolean",
1005
+ default: false,
1006
+ description:
1007
+ "Watch for changes to the functions and automatically rebuild the Worker script",
1008
+ },
1009
+ }),
1010
+ async ({
1011
+ directory,
1012
+ "script-path": scriptPath,
1013
+ "output-config-path": outputConfigPath,
1014
+ minify,
1015
+ sourcemap,
1016
+ fallbackService,
1017
+ watch,
1018
+ }) => {
959
1019
  await buildFunctions({
960
1020
  scriptPath,
961
- functionsDirectory,
1021
+ outputConfigPath,
1022
+ functionsDirectory: directory,
962
1023
  minify,
963
1024
  sourcemap,
1025
+ fallbackService,
1026
+ watch,
964
1027
  });
965
1028
  }
966
1029
  )
package/src/proxy.ts CHANGED
@@ -1,97 +1,245 @@
1
1
  import { connect } from "node:http2";
2
+ import type { ClientHttp2Session, ServerHttp2Stream } from "node:http2";
2
3
  import { createServer } from "node:http";
3
- import type { Server, IncomingHttpHeaders, RequestListener } from "node:http";
4
+ import type {
5
+ IncomingHttpHeaders,
6
+ RequestListener,
7
+ IncomingMessage,
8
+ ServerResponse,
9
+ Server,
10
+ } from "node:http";
4
11
  import WebSocket from "faye-websocket";
5
12
  import serveStatic from "serve-static";
13
+ import type { CfPreviewToken } from "./api/preview";
14
+ import { useEffect, useRef } from "react";
6
15
 
7
- export interface HttpProxyInit {
8
- host: string;
9
- assetPath?: string | null;
10
- onRequest?: (headers: IncomingHttpHeaders) => void;
11
- onResponse?: (headers: IncomingHttpHeaders) => void;
16
+ /**
17
+ * `usePreviewServer` is a React hook that creates a local development
18
+ * server that can be used to develop a Worker.
19
+ *
20
+ * When we run `wrangler dev`, we start by uploading the compiled worker
21
+ * to the preview service, which responds with a preview token.
22
+ * (see `useWorker`/`createWorker` for details.)
23
+ * We can then use that token to connect to the preview server for a
24
+ * great local development experience. Further, as we change the worker,
25
+ * we can update the preview token transparently without having to restart
26
+ * the development server.
27
+ */
28
+
29
+ /** Rewrite request headers to add the preview token. */
30
+ function addCfPreviewTokenHeader(
31
+ headers: IncomingHttpHeaders,
32
+ previewTokenValue: string
33
+ ) {
34
+ headers["cf-workers-preview-token"] = previewTokenValue;
12
35
  }
13
36
 
14
37
  /**
15
- * Creates a HTTP/1 proxy that sends requests over HTTP/2.
38
+ * Rewrite references in request headers
39
+ * from the preview host to the local host.
16
40
  */
17
- export function createHttpProxy(init: HttpProxyInit): Server {
18
- const { host, assetPath, onRequest = () => {}, onResponse = () => {} } = init;
19
- const remote = connect(`https://${host}`);
20
- const local = createServer();
21
- // HTTP/2 -> HTTP/2
22
- local.on("stream", (stream, headers: IncomingHttpHeaders) => {
23
- onRequest(headers);
24
- headers[":authority"] = host;
25
- const request = stream.pipe(remote.request(headers));
26
- request.on("response", (responseHeaders: IncomingHttpHeaders) => {
27
- onResponse(responseHeaders);
28
- stream.respond(responseHeaders);
29
- request.pipe(stream, { end: true });
30
- });
31
- });
32
- // HTTP/1 -> HTTP/2
33
- const handleRequest: RequestListener = (message, response) => {
34
- const { httpVersionMajor, headers, method, url } = message;
35
- if (httpVersionMajor >= 2) {
36
- return; // Already handled by the "stream" event.
41
+ function rewriteRemoteHostToLocalHostInHeaders(
42
+ headers: IncomingHttpHeaders,
43
+ remoteHost: string,
44
+ localPort: number
45
+ ) {
46
+ for (const [name, value] of Object.entries(headers)) {
47
+ // Rewrite the remote host to the local host.
48
+ if (typeof value === "string" && value.includes(remoteHost)) {
49
+ headers[name] = value
50
+ .replaceAll(`https://${remoteHost}`, `http://localhost:${localPort}`)
51
+ .replaceAll(remoteHost, `localhost:${localPort}`);
37
52
  }
38
- onRequest(headers);
39
- headers[":method"] = method;
40
- headers[":path"] = url;
41
- headers[":authority"] = host;
42
- headers[":scheme"] = "https";
43
- for (const name of Object.keys(headers)) {
44
- if (HTTP1_HEADERS.has(name.toLowerCase())) {
45
- delete headers[name];
46
- }
53
+ }
54
+ }
55
+
56
+ export function usePreviewServer({
57
+ previewToken,
58
+ publicRoot,
59
+ port,
60
+ }: {
61
+ previewToken: CfPreviewToken | undefined;
62
+ publicRoot: undefined | string;
63
+ port: number;
64
+ }) {
65
+ /** Creates an HTTP/1 proxy that sends requests over HTTP/2. */
66
+ const proxyServer = useRef<Server>();
67
+ const proxy = (proxyServer.current ??= createProxyServer());
68
+
69
+ /**
70
+ * When we're not connected / getting a fresh token on changes,
71
+ * we'd like to buffer streams/requests until we're connected.
72
+ * Once connected, we can flush the buffered streams/requests.
73
+ * streamBufferRef is used to buffer http/2 streams, while
74
+ * requestResponseBufferRef is used to buffer http/1 requests.
75
+ */
76
+ const streamBufferRef = useRef<
77
+ { stream: ServerHttp2Stream; headers: IncomingHttpHeaders }[]
78
+ >([]);
79
+ const requestResponseBufferRef = useRef<
80
+ { request: IncomingMessage; response: ServerResponse }[]
81
+ >([]);
82
+
83
+ useEffect(() => {
84
+ // If we don't have a token, that means either we're just starting up,
85
+ // or we're refreshing the token.
86
+ if (!previewToken) {
87
+ const cleanupListeners: (() => void)[] = [];
88
+ const bufferStream = (
89
+ stream: ServerHttp2Stream,
90
+ headers: IncomingHttpHeaders
91
+ ) => {
92
+ // store the stream in a buffer so we can replay it later
93
+ streamBufferRef.current.push({ stream, headers });
94
+ };
95
+ proxy.on("stream", bufferStream);
96
+ cleanupListeners.push(() => proxy.off("stream", bufferStream));
97
+
98
+ const bufferRequestResponse = (
99
+ request: IncomingMessage,
100
+ response: ServerResponse
101
+ ) => {
102
+ // store the request and response in a buffer so we can replay it later
103
+ requestResponseBufferRef.current.push({ request, response });
104
+ };
105
+
106
+ proxy.on("request", bufferRequestResponse);
107
+ cleanupListeners.push(() => proxy.off("request", bufferRequestResponse));
108
+ return () => {
109
+ cleanupListeners.forEach((cleanup) => cleanup());
110
+ };
47
111
  }
48
- const request = message.pipe(remote.request(headers));
49
- request.on("response", (responseHeaders) => {
50
- const status = responseHeaders[":status"];
51
- onResponse(responseHeaders);
52
- for (const name of Object.keys(responseHeaders)) {
53
- if (name.startsWith(":")) {
54
- delete responseHeaders[name];
112
+
113
+ // We have a token. Let's proxy requests to the preview end point.
114
+ const cleanupListeners: (() => void)[] = [];
115
+
116
+ const assetPath = typeof publicRoot === "string" ? publicRoot : null;
117
+
118
+ // create a ClientHttp2Session
119
+ const remote = connect(`https://${previewToken.host}`);
120
+ cleanupListeners.push(() => remote.destroy());
121
+
122
+ /** HTTP/2 -> HTTP/2 */
123
+ const handleStream = createStreamHandler(previewToken, remote, port);
124
+ proxy.on("stream", handleStream);
125
+ cleanupListeners.push(() => proxy.off("stream", handleStream));
126
+
127
+ // flush and replay buffered streams
128
+ streamBufferRef.current.forEach((buffer) =>
129
+ handleStream(buffer.stream, buffer.headers)
130
+ );
131
+ streamBufferRef.current = [];
132
+
133
+ /** HTTP/1 -> HTTP/2 */
134
+ const handleRequest: RequestListener = (
135
+ message: IncomingMessage,
136
+ response: ServerResponse
137
+ ) => {
138
+ const { httpVersionMajor, headers, method, url } = message;
139
+ if (httpVersionMajor >= 2) {
140
+ return; // Already handled by the "stream" event.
141
+ }
142
+ addCfPreviewTokenHeader(headers, previewToken.value);
143
+ headers[":method"] = method;
144
+ headers[":path"] = url;
145
+ headers[":authority"] = previewToken.host;
146
+ headers[":scheme"] = "https";
147
+ for (const name of Object.keys(headers)) {
148
+ if (HTTP1_HEADERS.has(name.toLowerCase())) {
149
+ delete headers[name];
55
150
  }
56
151
  }
57
- response.writeHead(status, responseHeaders);
58
- request.pipe(response, { end: true });
59
- });
60
- };
61
- // If an asset path is defined, check the file system
62
- // for a file first and serve if it exists.
63
- if (assetPath) {
64
- const handleAsset = serveStatic(assetPath, {
65
- cacheControl: false,
66
- });
67
- local.on("request", (request, response) => {
68
- handleAsset(request, response, () => {
69
- handleRequest(request, response);
152
+ const request = message.pipe(remote.request(headers));
153
+ request.on("response", (responseHeaders) => {
154
+ const status = responseHeaders[":status"] ?? 500;
155
+ rewriteRemoteHostToLocalHostInHeaders(
156
+ responseHeaders,
157
+ previewToken.host,
158
+ port
159
+ );
160
+ for (const name of Object.keys(responseHeaders)) {
161
+ if (name.startsWith(":")) {
162
+ delete responseHeaders[name];
163
+ }
164
+ }
165
+ response.writeHead(status, responseHeaders);
166
+ request.pipe(response, { end: true });
70
167
  });
71
- });
72
- } else {
73
- local.on("request", handleRequest);
74
- }
75
- // HTTP/1 -> WebSocket (over HTTP/1)
76
- local.on("upgrade", (message, socket, body) => {
77
- const { headers, url } = message;
78
- onRequest(headers);
79
- headers["host"] = host;
80
- const localWebsocket = new WebSocket(message, socket, body);
81
- // TODO(soon): Custom WebSocket protocol is not working?
82
- const remoteWebsocketClient = new WebSocket.Client(
83
- `wss://${host}${url}`,
84
- [],
85
- { headers }
168
+ };
169
+
170
+ // If an asset path is defined, check the file system
171
+ // for a file first and serve if it exists.
172
+ const actualHandleRequest = assetPath
173
+ ? createHandleAssetsRequest(assetPath, handleRequest)
174
+ : handleRequest;
175
+
176
+ proxy.on("request", actualHandleRequest);
177
+ cleanupListeners.push(() => proxy.off("request", actualHandleRequest));
178
+
179
+ // flush and replay buffered requests
180
+ requestResponseBufferRef.current.forEach(({ request, response }) =>
181
+ actualHandleRequest(request, response)
86
182
  );
87
- localWebsocket.pipe(remoteWebsocketClient).pipe(localWebsocket);
88
- });
89
- remote.on("close", () => {
90
- local.close();
183
+ requestResponseBufferRef.current = [];
184
+
185
+ /** HTTP/1 -> WebSocket (over HTTP/1) */
186
+ const handleUpgrade = (
187
+ message: IncomingMessage,
188
+ socket: WebSocket,
189
+ body: Buffer
190
+ ) => {
191
+ const { headers, url } = message;
192
+ addCfPreviewTokenHeader(headers, previewToken.value);
193
+ headers["host"] = previewToken.host;
194
+ const localWebsocket = new WebSocket(message, socket, body);
195
+ // TODO(soon): Custom WebSocket protocol is not working?
196
+ const remoteWebsocketClient = new WebSocket.Client(
197
+ `wss://${previewToken.host}${url}`,
198
+ [],
199
+ { headers }
200
+ );
201
+ localWebsocket.pipe(remoteWebsocketClient).pipe(localWebsocket);
202
+ // We close down websockets whenever we refresh the token.
203
+ cleanupListeners.push(() => {
204
+ localWebsocket.destroy();
205
+ remoteWebsocketClient.destroy();
206
+ });
207
+ };
208
+ proxy.on("upgrade", handleUpgrade);
209
+ cleanupListeners.push(() => proxy.off("upgrade", handleUpgrade));
210
+
211
+ return () => {
212
+ cleanupListeners.forEach((d) => d());
213
+ };
214
+ }, [previewToken, publicRoot, port, proxy]);
215
+
216
+ // Start/stop the server whenever the
217
+ // containing component is mounted/unmounted.
218
+ useEffect(() => {
219
+ proxy.listen(port);
220
+ console.log(`⬣ Listening at http://localhost:${port}`);
221
+
222
+ return () => {
223
+ proxy.close();
224
+ };
225
+ }, [port, proxy]);
226
+ }
227
+
228
+ function createHandleAssetsRequest(
229
+ assetPath: string,
230
+ handleRequest: RequestListener
231
+ ) {
232
+ const handleAsset = serveStatic(assetPath, {
233
+ cacheControl: false,
91
234
  });
92
- return local;
235
+ return (request: IncomingMessage, response: ServerResponse) => {
236
+ handleAsset(request, response, () => {
237
+ handleRequest(request, response);
238
+ });
239
+ };
93
240
  }
94
241
 
242
+ /** A Set of headers we want to remove from HTTP/1 requests. */
95
243
  const HTTP1_HEADERS = new Set([
96
244
  "host",
97
245
  "connection",
@@ -101,3 +249,54 @@ const HTTP1_HEADERS = new Set([
101
249
  "transfer-encoding",
102
250
  "http2-settings",
103
251
  ]);
252
+
253
+ function createProxyServer() {
254
+ return createServer()
255
+ .on("request", function (req, res) {
256
+ // log all requests
257
+ console.log(
258
+ new Date().toLocaleTimeString(),
259
+ req.method,
260
+ req.url,
261
+ res.statusCode
262
+ );
263
+ })
264
+ .on("upgrade", (req) => {
265
+ // log all websocket connections
266
+ console.log(
267
+ new Date().toLocaleTimeString(),
268
+ req.method,
269
+ req.url,
270
+ 101,
271
+ "(WebSocket)"
272
+ );
273
+ })
274
+ .on("error", (err) => {
275
+ // log all connection errors
276
+ console.error(new Date().toLocaleTimeString(), err);
277
+ });
278
+ }
279
+
280
+ function createStreamHandler(
281
+ previewToken: CfPreviewToken,
282
+ remote: ClientHttp2Session,
283
+ port: number
284
+ ) {
285
+ return function handleStream(
286
+ stream: ServerHttp2Stream,
287
+ headers: IncomingHttpHeaders
288
+ ) {
289
+ addCfPreviewTokenHeader(headers, previewToken.value);
290
+ headers[":authority"] = previewToken.host;
291
+ const request = stream.pipe(remote.request(headers));
292
+ request.on("response", (responseHeaders: IncomingHttpHeaders) => {
293
+ rewriteRemoteHostToLocalHostInHeaders(
294
+ responseHeaders,
295
+ previewToken.host,
296
+ port
297
+ );
298
+ stream.respond(responseHeaders);
299
+ request.pipe(stream, { end: true });
300
+ });
301
+ };
302
+ }