wrangler 0.0.2 → 0.0.6
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 +51 -55
- package/bin/wrangler.js +36 -0
- package/import_meta_url.js +3 -0
- package/miniflare-config-stubs/.env.empty +0 -0
- package/miniflare-config-stubs/package.empty.json +1 -0
- package/miniflare-config-stubs/wrangler.empty.toml +0 -0
- package/package.json +111 -9
- package/src/__tests__/clipboardy-mock.js +4 -0
- package/src/__tests__/index.test.ts +391 -0
- package/src/__tests__/jest.setup.ts +17 -0
- package/src/__tests__/mock-cfetch.js +42 -0
- package/src/__tests__/mock-dialogs.ts +65 -0
- package/src/api/form_data.ts +141 -0
- package/src/api/inspect.ts +430 -0
- package/src/api/preview.ts +128 -0
- package/src/api/worker.ts +161 -0
- package/src/cfetch.ts +72 -0
- package/src/cli.ts +10 -0
- package/src/config.ts +122 -0
- package/src/dev.tsx +867 -0
- package/src/dialogs.tsx +77 -0
- package/src/index.tsx +1875 -0
- package/src/kv.tsx +211 -0
- package/src/module-collection.ts +64 -0
- package/src/pages.tsx +818 -0
- package/src/proxy.ts +104 -0
- package/src/publish.ts +358 -0
- package/src/sites.tsx +115 -0
- package/src/tail.tsx +71 -0
- package/src/user.tsx +1029 -0
- package/static-asset-facade.js +47 -0
- package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +332 -0
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +176 -0
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +25 -0
- package/vendor/@cloudflare/kv-asset-handler/README.md +245 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +32 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +354 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +13 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +148 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +1 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +436 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +1 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +40 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +1 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +42 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +26 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/types.js +31 -0
- package/vendor/@cloudflare/kv-asset-handler/package.json +52 -0
- package/vendor/@cloudflare/kv-asset-handler/src/index.ts +296 -0
- package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +136 -0
- package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +464 -0
- package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +33 -0
- package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +42 -0
- package/vendor/@cloudflare/kv-asset-handler/src/types.ts +39 -0
- package/vendor/wrangler-mime/CHANGELOG.md +289 -0
- package/vendor/wrangler-mime/LICENSE +21 -0
- package/vendor/wrangler-mime/Mime.js +97 -0
- package/vendor/wrangler-mime/README.md +187 -0
- package/vendor/wrangler-mime/cli.js +46 -0
- package/vendor/wrangler-mime/index.js +4 -0
- package/vendor/wrangler-mime/lite.js +4 -0
- package/vendor/wrangler-mime/package.json +52 -0
- package/vendor/wrangler-mime/types/other.js +1 -0
- package/vendor/wrangler-mime/types/standard.js +1 -0
- package/wrangler-dist/cli.js +125758 -0
- package/wrangler-dist/cli.js.map +7 -0
- package/.npmignore +0 -15
- package/index.js +0 -250
- package/tests/is.spec.js +0 -1155
package/src/dev.tsx
ADDED
|
@@ -0,0 +1,867 @@
|
|
|
1
|
+
import esbuild from "esbuild";
|
|
2
|
+
import { readFile } from "fs/promises";
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
import type { DirectoryResult } from "tmp-promise";
|
|
5
|
+
import tmp from "tmp-promise";
|
|
6
|
+
import type { CfPreviewToken } from "./api/preview";
|
|
7
|
+
import { Box, Text, useInput } from "ink";
|
|
8
|
+
import React, { useState, useEffect, useRef } from "react";
|
|
9
|
+
import path from "path";
|
|
10
|
+
import open from "open";
|
|
11
|
+
import { DtInspector } from "./api/inspect";
|
|
12
|
+
import type { CfModule, CfVariable } from "./api/worker";
|
|
13
|
+
import { createWorker } from "./api/worker";
|
|
14
|
+
import type { CfWorkerInit } from "./api/worker";
|
|
15
|
+
import { spawn } from "child_process";
|
|
16
|
+
import onExit from "signal-exit";
|
|
17
|
+
import { syncAssets } from "./sites";
|
|
18
|
+
import clipboardy from "clipboardy";
|
|
19
|
+
import http from "node:http";
|
|
20
|
+
import commandExists from "command-exists";
|
|
21
|
+
import assert from "assert";
|
|
22
|
+
import { getAPIToken } from "./user";
|
|
23
|
+
import fetch from "node-fetch";
|
|
24
|
+
import makeModuleCollector from "./module-collection";
|
|
25
|
+
import { withErrorBoundary, useErrorHandler } from "react-error-boundary";
|
|
26
|
+
import { createHttpProxy } from "./proxy";
|
|
27
|
+
import { execa } from "execa";
|
|
28
|
+
import { watch } from "chokidar";
|
|
29
|
+
|
|
30
|
+
type CfScriptFormat = void | "modules" | "service-worker";
|
|
31
|
+
|
|
32
|
+
type Props = {
|
|
33
|
+
name?: string;
|
|
34
|
+
entry: string;
|
|
35
|
+
port?: number;
|
|
36
|
+
format: CfScriptFormat;
|
|
37
|
+
accountId: void | string;
|
|
38
|
+
initialMode: "local" | "remote";
|
|
39
|
+
jsxFactory: void | string;
|
|
40
|
+
jsxFragment: void | string;
|
|
41
|
+
variables: { [name: string]: CfVariable };
|
|
42
|
+
public: void | string;
|
|
43
|
+
site: void | string;
|
|
44
|
+
compatibilityDate: void | string;
|
|
45
|
+
compatibilityFlags: void | string[];
|
|
46
|
+
usageModel: void | "bundled" | "unbound";
|
|
47
|
+
buildCommand: {
|
|
48
|
+
command?: undefined | string;
|
|
49
|
+
cwd?: undefined | string;
|
|
50
|
+
watch_dir?: undefined | string;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function Dev(props: Props): JSX.Element {
|
|
55
|
+
if (props.public && props.format === "service-worker") {
|
|
56
|
+
throw new Error(
|
|
57
|
+
"You cannot use the service worker format with a `public` directory."
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const port = props.port || 8787;
|
|
61
|
+
const apiToken = getAPIToken();
|
|
62
|
+
const directory = useTmpDir();
|
|
63
|
+
|
|
64
|
+
// if there isn't a build command, we just return the entry immediately
|
|
65
|
+
// ideally there would be a conditional here, but the rules of hooks
|
|
66
|
+
// kinda forbid that, so we thread the entry through useCustomBuild
|
|
67
|
+
const entry = useCustomBuild(props.entry, props.buildCommand);
|
|
68
|
+
|
|
69
|
+
const bundle = useEsbuild({
|
|
70
|
+
entry,
|
|
71
|
+
destination: directory,
|
|
72
|
+
staticRoot: props.public,
|
|
73
|
+
jsxFactory: props.jsxFactory,
|
|
74
|
+
jsxFragment: props.jsxFragment,
|
|
75
|
+
});
|
|
76
|
+
if (bundle && bundle.type === "commonjs" && !props.format && props.public) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
"You cannot use the service worker format with a `public` directory."
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// @ts-expect-error whack
|
|
83
|
+
useDevtoolsRefresh(bundle?.id ?? 0);
|
|
84
|
+
|
|
85
|
+
const toggles = useHotkeys(
|
|
86
|
+
{
|
|
87
|
+
local: props.initialMode === "local",
|
|
88
|
+
tunnel: false,
|
|
89
|
+
},
|
|
90
|
+
port
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
useTunnel(toggles.tunnel);
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<>
|
|
97
|
+
{toggles.local ? (
|
|
98
|
+
<Local
|
|
99
|
+
name={props.name}
|
|
100
|
+
bundle={bundle}
|
|
101
|
+
format={props.format}
|
|
102
|
+
variables={props.variables}
|
|
103
|
+
site={props.site}
|
|
104
|
+
public={props.public}
|
|
105
|
+
port={props.port}
|
|
106
|
+
/>
|
|
107
|
+
) : (
|
|
108
|
+
<Remote
|
|
109
|
+
name={props.name}
|
|
110
|
+
bundle={bundle}
|
|
111
|
+
format={props.format}
|
|
112
|
+
accountId={props.accountId}
|
|
113
|
+
apiToken={apiToken}
|
|
114
|
+
variables={props.variables}
|
|
115
|
+
site={props.site}
|
|
116
|
+
public={props.public}
|
|
117
|
+
port={props.port}
|
|
118
|
+
compatibilityDate={props.compatibilityDate}
|
|
119
|
+
compatibilityFlags={props.compatibilityFlags}
|
|
120
|
+
usageModel={props.usageModel}
|
|
121
|
+
/>
|
|
122
|
+
)}
|
|
123
|
+
<Box borderStyle="round" paddingLeft={1} paddingRight={1}>
|
|
124
|
+
<Text>
|
|
125
|
+
{`B to open a browser, D to open Devtools, S to ${
|
|
126
|
+
toggles.tunnel ? "turn off" : "turn on"
|
|
127
|
+
} (experimental) sharing, L to ${
|
|
128
|
+
toggles.local ? "turn off" : "turn on"
|
|
129
|
+
} local mode, X to exit`}
|
|
130
|
+
</Text>
|
|
131
|
+
</Box>
|
|
132
|
+
</>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function useDevtoolsRefresh(bundleId: number) {
|
|
137
|
+
// TODO: this is a hack while we figure out
|
|
138
|
+
// a better cleaner solution to get devtools to reconnect
|
|
139
|
+
// without having to do a full refresh
|
|
140
|
+
const ref = useRef();
|
|
141
|
+
// @ts-expect-error whack
|
|
142
|
+
ref.current = bundleId;
|
|
143
|
+
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
const server = http.createServer((req, res) => {
|
|
146
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
147
|
+
res.setHeader("Access-Control-Request-Method", "*");
|
|
148
|
+
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET");
|
|
149
|
+
res.setHeader("Access-Control-Allow-Headers", "*");
|
|
150
|
+
if (req.method === "OPTIONS") {
|
|
151
|
+
res.writeHead(200);
|
|
152
|
+
res.end();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
156
|
+
res.end(JSON.stringify({ value: ref.current }));
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
server.listen(3142);
|
|
160
|
+
return () => {
|
|
161
|
+
server.close();
|
|
162
|
+
};
|
|
163
|
+
}, []);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function Remote(props: {
|
|
167
|
+
name: void | string;
|
|
168
|
+
bundle: EsbuildBundle | void;
|
|
169
|
+
format: CfScriptFormat;
|
|
170
|
+
public: void | string;
|
|
171
|
+
site: void | string;
|
|
172
|
+
port: number;
|
|
173
|
+
accountId: void | string;
|
|
174
|
+
apiToken: void | string;
|
|
175
|
+
variables: { [name: string]: CfVariable };
|
|
176
|
+
compatibilityDate: string | void;
|
|
177
|
+
compatibilityFlags: void | string[];
|
|
178
|
+
usageModel: void | "bundled" | "unbound";
|
|
179
|
+
}) {
|
|
180
|
+
assert(props.accountId, "accountId is required");
|
|
181
|
+
assert(props.apiToken, "apiToken is required");
|
|
182
|
+
const token = useWorker({
|
|
183
|
+
name: props.name,
|
|
184
|
+
bundle: props.bundle,
|
|
185
|
+
format: props.format,
|
|
186
|
+
modules: props.bundle ? props.bundle.modules : [],
|
|
187
|
+
accountId: props.accountId,
|
|
188
|
+
apiToken: props.apiToken,
|
|
189
|
+
variables: props.variables,
|
|
190
|
+
sitesFolder: props.site,
|
|
191
|
+
port: props.port,
|
|
192
|
+
compatibilityDate: props.compatibilityDate,
|
|
193
|
+
compatibilityFlags: props.compatibilityFlags,
|
|
194
|
+
usageModel: props.usageModel,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
useProxy({ token, publicRoot: props.public, port: props.port });
|
|
198
|
+
|
|
199
|
+
useInspector(token ? token.inspectorUrl.href : undefined);
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
function Local(props: {
|
|
203
|
+
name: void | string;
|
|
204
|
+
bundle: EsbuildBundle | void;
|
|
205
|
+
format: CfScriptFormat;
|
|
206
|
+
variables: { [name: string]: CfVariable };
|
|
207
|
+
public: void | string;
|
|
208
|
+
site: void | string;
|
|
209
|
+
port: number;
|
|
210
|
+
}) {
|
|
211
|
+
const { inspectorUrl } = useLocalWorker({
|
|
212
|
+
name: props.name,
|
|
213
|
+
bundle: props.bundle,
|
|
214
|
+
format: props.format,
|
|
215
|
+
variables: props.variables,
|
|
216
|
+
port: props.port,
|
|
217
|
+
});
|
|
218
|
+
useInspector(inspectorUrl);
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function useLocalWorker(props: {
|
|
223
|
+
name: void | string;
|
|
224
|
+
bundle: EsbuildBundle | void;
|
|
225
|
+
format: CfScriptFormat;
|
|
226
|
+
variables: { [name: string]: CfVariable };
|
|
227
|
+
port: number;
|
|
228
|
+
}) {
|
|
229
|
+
// TODO: pass vars via command line
|
|
230
|
+
const { bundle, format, variables, port } = props;
|
|
231
|
+
const local = useRef<ReturnType<typeof spawn>>();
|
|
232
|
+
const removeSignalExitListener = useRef<() => void>();
|
|
233
|
+
const [inspectorUrl, setInspectorUrl] = useState<string | void>();
|
|
234
|
+
useEffect(() => {
|
|
235
|
+
async function startLocalWorker() {
|
|
236
|
+
if (!bundle) return;
|
|
237
|
+
if (format === "modules" && bundle.type === "commonjs") {
|
|
238
|
+
console.error("⎔ Cannot use modules with a commonjs bundle.");
|
|
239
|
+
// TODO: a much better error message here, with what to do next
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (format === "service-worker" && bundle.type !== "esm") {
|
|
243
|
+
console.error("⎔ Cannot use service-worker with a esm bundle.");
|
|
244
|
+
// TODO: a much better error message here, with what to do next
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
console.log("⎔ Starting a local server...");
|
|
249
|
+
// TODO: just use execa for this
|
|
250
|
+
local.current = spawn("node", [
|
|
251
|
+
"--experimental-vm-modules",
|
|
252
|
+
"--inspect",
|
|
253
|
+
require.resolve("miniflare/cli"),
|
|
254
|
+
bundle.path,
|
|
255
|
+
"--watch",
|
|
256
|
+
"--wrangler-config",
|
|
257
|
+
path.join(__dirname, "../miniflare-config-stubs/wrangler.empty.toml"),
|
|
258
|
+
"--env",
|
|
259
|
+
path.join(__dirname, "../miniflare-config-stubs/.env.empty"),
|
|
260
|
+
"--package",
|
|
261
|
+
path.join(__dirname, "../miniflare-config-stubs/package.empty.json"),
|
|
262
|
+
"--port",
|
|
263
|
+
port.toString(),
|
|
264
|
+
"--kv-persist",
|
|
265
|
+
"--cache-persist",
|
|
266
|
+
"--do-persist",
|
|
267
|
+
...Object.entries(variables)
|
|
268
|
+
.map(([varKey, varVal]) => {
|
|
269
|
+
if (typeof varVal === "string") {
|
|
270
|
+
return `--binding ${varKey}=${varVal}`;
|
|
271
|
+
} else if (
|
|
272
|
+
"namespaceId" in varVal &&
|
|
273
|
+
typeof varVal.namespaceId === "string"
|
|
274
|
+
) {
|
|
275
|
+
return `--kv ${varKey}`;
|
|
276
|
+
} else if ("class_name" in varVal) {
|
|
277
|
+
return `--do ${varKey}=${varVal.class_name}`;
|
|
278
|
+
}
|
|
279
|
+
})
|
|
280
|
+
.filter(Boolean),
|
|
281
|
+
"--modules",
|
|
282
|
+
format ||
|
|
283
|
+
(bundle.type === "esm" ? "modules" : "service-worker") === "modules"
|
|
284
|
+
? "true"
|
|
285
|
+
: "false",
|
|
286
|
+
]);
|
|
287
|
+
console.log(`⬣ Listening at http://localhost:${port}`);
|
|
288
|
+
|
|
289
|
+
local.current.on("close", (code) => {
|
|
290
|
+
if (code !== null) {
|
|
291
|
+
console.log(`miniflare process exited with code ${code}`);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
local.current.stdout.on("data", (_data: string) => {
|
|
296
|
+
// console.log(`stdout: ${data}`);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
local.current.stderr.on("data", (data: string) => {
|
|
300
|
+
// console.error(`stderr: ${data}`);
|
|
301
|
+
const matches =
|
|
302
|
+
/Debugger listening on (ws:\/\/127\.0\.0\.1:9229\/[A-Za-z0-9-]+)/.exec(
|
|
303
|
+
data
|
|
304
|
+
);
|
|
305
|
+
if (matches) {
|
|
306
|
+
setInspectorUrl(matches[1]);
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
local.current.on("exit", (code) => {
|
|
311
|
+
if (code !== 0) {
|
|
312
|
+
console.error(`miniflare process exited with code ${code}`);
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
local.current.on("error", (error: Error) => {
|
|
317
|
+
console.error(`miniflare process failed to spawn`);
|
|
318
|
+
console.error(error);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
removeSignalExitListener.current = onExit((_code, _signal) => {
|
|
322
|
+
console.log("⎔ Shutting down local server.");
|
|
323
|
+
local.current?.kill();
|
|
324
|
+
local.current = undefined;
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
startLocalWorker().catch((err) => {
|
|
329
|
+
console.error("local worker:", err);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
return () => {
|
|
333
|
+
if (local.current) {
|
|
334
|
+
console.log("⎔ Shutting down local server.");
|
|
335
|
+
local.current?.kill();
|
|
336
|
+
local.current = undefined;
|
|
337
|
+
removeSignalExitListener.current && removeSignalExitListener.current();
|
|
338
|
+
removeSignalExitListener.current = undefined;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}, [bundle, format, port]);
|
|
342
|
+
return { inspectorUrl };
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function useTmpDir(): string | void {
|
|
346
|
+
const [directory, setDirectory] = useState<DirectoryResult>();
|
|
347
|
+
const handleError = useErrorHandler();
|
|
348
|
+
useEffect(() => {
|
|
349
|
+
let dir: DirectoryResult;
|
|
350
|
+
async function create() {
|
|
351
|
+
try {
|
|
352
|
+
dir = await tmp.dir({ unsafeCleanup: true });
|
|
353
|
+
setDirectory(dir);
|
|
354
|
+
return;
|
|
355
|
+
} catch (err) {
|
|
356
|
+
console.error("failed to create tmp dir");
|
|
357
|
+
throw err;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
create().catch((err) => {
|
|
361
|
+
// we want to break here
|
|
362
|
+
// we can't do much without a temp dir anyway
|
|
363
|
+
handleError(err);
|
|
364
|
+
});
|
|
365
|
+
return () => {
|
|
366
|
+
dir.cleanup().catch(() => {
|
|
367
|
+
// extremely unlikely,
|
|
368
|
+
// but it's 2021 after all
|
|
369
|
+
console.error("failed to cleanup tmp dir");
|
|
370
|
+
});
|
|
371
|
+
};
|
|
372
|
+
}, [handleError]);
|
|
373
|
+
return directory?.path;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function runCommand() {}
|
|
377
|
+
|
|
378
|
+
function useCustomBuild(
|
|
379
|
+
expectedEntry: string,
|
|
380
|
+
props: {
|
|
381
|
+
command?: undefined | string;
|
|
382
|
+
cwd?: undefined | string;
|
|
383
|
+
watch_dir?: undefined | string;
|
|
384
|
+
}
|
|
385
|
+
): void | string {
|
|
386
|
+
const [entry, setEntry] = useState<string | void>(
|
|
387
|
+
// if there's no build command, just return the expected entry
|
|
388
|
+
props.command ? null : expectedEntry
|
|
389
|
+
);
|
|
390
|
+
const { command, cwd, watch_dir } = props;
|
|
391
|
+
useEffect(() => {
|
|
392
|
+
if (!command) return;
|
|
393
|
+
let cmd, interval;
|
|
394
|
+
console.log("running:", command);
|
|
395
|
+
const commandPieces = command.split(" ");
|
|
396
|
+
cmd = execa(commandPieces[0], commandPieces.slice(1), {
|
|
397
|
+
...(cwd && { cwd }),
|
|
398
|
+
stderr: "inherit",
|
|
399
|
+
stdout: "inherit",
|
|
400
|
+
});
|
|
401
|
+
if (watch_dir) {
|
|
402
|
+
watch(watch_dir, { persistent: true, ignoreInitial: true }).on(
|
|
403
|
+
"all",
|
|
404
|
+
(_event, _path) => {
|
|
405
|
+
console.log(`The file ${path} changed, restarting build...`);
|
|
406
|
+
cmd.kill();
|
|
407
|
+
cmd = execa(commandPieces[0], commandPieces.slice(1), {
|
|
408
|
+
...(cwd && { cwd }),
|
|
409
|
+
stderr: "inherit",
|
|
410
|
+
stdout: "inherit",
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// check every so often whether `expectedEntry` exists
|
|
417
|
+
// if it does, we're done
|
|
418
|
+
const startedAt = Date.now();
|
|
419
|
+
interval = setInterval(() => {
|
|
420
|
+
if (existsSync(expectedEntry)) {
|
|
421
|
+
clearInterval(interval);
|
|
422
|
+
setEntry(expectedEntry);
|
|
423
|
+
} else {
|
|
424
|
+
const elapsed = Date.now() - startedAt;
|
|
425
|
+
// timeout after 30 seconds of waiting
|
|
426
|
+
if (elapsed > 1000 * 60 * 30) {
|
|
427
|
+
console.error("⎔ Build timed out.");
|
|
428
|
+
clearInterval(interval);
|
|
429
|
+
cmd.kill();
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}, 200);
|
|
433
|
+
// TODO: we could probably timeout here after a while
|
|
434
|
+
|
|
435
|
+
return () => {
|
|
436
|
+
if (cmd) {
|
|
437
|
+
cmd.kill();
|
|
438
|
+
cmd = undefined;
|
|
439
|
+
}
|
|
440
|
+
clearInterval(interval);
|
|
441
|
+
interval = undefined;
|
|
442
|
+
};
|
|
443
|
+
}, [command, cwd, expectedEntry, watch_dir]);
|
|
444
|
+
return entry;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
type EsbuildBundle = {
|
|
448
|
+
id: number;
|
|
449
|
+
path: string;
|
|
450
|
+
entry: string;
|
|
451
|
+
type: "esm" | "commonjs";
|
|
452
|
+
exports: string[];
|
|
453
|
+
modules: CfModule[];
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
function useEsbuild(props: {
|
|
457
|
+
entry: void | string;
|
|
458
|
+
destination: string | void;
|
|
459
|
+
staticRoot: void | string;
|
|
460
|
+
jsxFactory: string | void;
|
|
461
|
+
jsxFragment: string | void;
|
|
462
|
+
}): EsbuildBundle | void {
|
|
463
|
+
const { entry, destination, staticRoot, jsxFactory, jsxFragment } = props;
|
|
464
|
+
const [bundle, setBundle] = useState<EsbuildBundle>();
|
|
465
|
+
useEffect(() => {
|
|
466
|
+
let result: esbuild.BuildResult;
|
|
467
|
+
async function build() {
|
|
468
|
+
if (!destination || !entry) return;
|
|
469
|
+
const moduleCollector = makeModuleCollector();
|
|
470
|
+
result = await esbuild.build({
|
|
471
|
+
entryPoints: [entry],
|
|
472
|
+
bundle: true,
|
|
473
|
+
outdir: destination,
|
|
474
|
+
metafile: true,
|
|
475
|
+
format: "esm",
|
|
476
|
+
sourcemap: true,
|
|
477
|
+
loader: {
|
|
478
|
+
".js": "jsx",
|
|
479
|
+
},
|
|
480
|
+
...(jsxFactory && { jsxFactory }),
|
|
481
|
+
...(jsxFragment && { jsxFragment }),
|
|
482
|
+
external: ["__STATIC_CONTENT_MANIFEST"],
|
|
483
|
+
conditions: ["worker", "browser"],
|
|
484
|
+
plugins: [moduleCollector.plugin],
|
|
485
|
+
// TODO: import.meta.url
|
|
486
|
+
watch: {
|
|
487
|
+
async onRebuild(error) {
|
|
488
|
+
if (error) console.error("watch build failed:", error);
|
|
489
|
+
else {
|
|
490
|
+
// nothing really changes here, so let's increment the id
|
|
491
|
+
// to change the return object's identity
|
|
492
|
+
setBundle((bundle) => ({ ...bundle, id: bundle.id + 1 }));
|
|
493
|
+
}
|
|
494
|
+
},
|
|
495
|
+
},
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
const chunks = Object.entries(result.metafile.outputs).find(
|
|
499
|
+
([_path, { entryPoint }]) => entryPoint === entry
|
|
500
|
+
); // assumedly only one entry point
|
|
501
|
+
|
|
502
|
+
setBundle({
|
|
503
|
+
id: 0,
|
|
504
|
+
entry,
|
|
505
|
+
path: chunks[0],
|
|
506
|
+
type: chunks[1].exports.length > 0 ? "esm" : "commonjs",
|
|
507
|
+
exports: chunks[1].exports,
|
|
508
|
+
modules: moduleCollector.modules,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
build().catch((_err) => {
|
|
512
|
+
// esbuild already logs errors to stderr
|
|
513
|
+
// and we don't want to end the process
|
|
514
|
+
// on build errors anyway
|
|
515
|
+
// so this is a no-op error handler
|
|
516
|
+
});
|
|
517
|
+
return () => {
|
|
518
|
+
result?.stop();
|
|
519
|
+
};
|
|
520
|
+
}, [entry, destination, staticRoot, jsxFactory, jsxFragment]);
|
|
521
|
+
return bundle;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function useWorker(props: {
|
|
525
|
+
name: void | string;
|
|
526
|
+
bundle: EsbuildBundle | void;
|
|
527
|
+
format: CfScriptFormat;
|
|
528
|
+
modules: CfModule[];
|
|
529
|
+
accountId: string;
|
|
530
|
+
apiToken: string;
|
|
531
|
+
variables: { [name: string]: CfVariable };
|
|
532
|
+
sitesFolder: void | string;
|
|
533
|
+
port: number;
|
|
534
|
+
compatibilityDate: string | void;
|
|
535
|
+
compatibilityFlags: string[] | void;
|
|
536
|
+
usageModel: void | "bundled" | "unbound";
|
|
537
|
+
}): CfPreviewToken | void {
|
|
538
|
+
const {
|
|
539
|
+
name,
|
|
540
|
+
bundle,
|
|
541
|
+
format,
|
|
542
|
+
modules,
|
|
543
|
+
accountId,
|
|
544
|
+
apiToken,
|
|
545
|
+
variables,
|
|
546
|
+
sitesFolder,
|
|
547
|
+
compatibilityDate,
|
|
548
|
+
compatibilityFlags,
|
|
549
|
+
usageModel,
|
|
550
|
+
port,
|
|
551
|
+
} = props;
|
|
552
|
+
const [token, setToken] = useState<CfPreviewToken>();
|
|
553
|
+
useEffect(() => {
|
|
554
|
+
async function start() {
|
|
555
|
+
if (!bundle) return;
|
|
556
|
+
if (format === "modules" && bundle.type === "commonjs") {
|
|
557
|
+
console.error("⎔ Cannot use modules with a commonjs bundle.");
|
|
558
|
+
// TODO: a much better error message here, with what to do next
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
if (format === "service-worker" && bundle.type !== "esm") {
|
|
562
|
+
console.error("⎔ Cannot use service-worker with a esm bundle.");
|
|
563
|
+
// TODO: a much better error message here, with what to do next
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (token) {
|
|
568
|
+
console.log("⎔ Detected changes, restarting server...");
|
|
569
|
+
} else {
|
|
570
|
+
console.log("⎔ Starting server...");
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const assets = sitesFolder
|
|
574
|
+
? await syncAssets(
|
|
575
|
+
accountId,
|
|
576
|
+
path.basename(bundle.path),
|
|
577
|
+
sitesFolder,
|
|
578
|
+
true,
|
|
579
|
+
undefined // TODO: env
|
|
580
|
+
)
|
|
581
|
+
: {
|
|
582
|
+
manifest: undefined,
|
|
583
|
+
namespace: undefined,
|
|
584
|
+
}; // TODO: cancellable?
|
|
585
|
+
|
|
586
|
+
const content = await readFile(bundle.path, "utf-8");
|
|
587
|
+
const init: CfWorkerInit = {
|
|
588
|
+
name,
|
|
589
|
+
main: {
|
|
590
|
+
name: path.basename(bundle.path),
|
|
591
|
+
type: format || bundle.type === "esm" ? "esm" : "commonjs",
|
|
592
|
+
content,
|
|
593
|
+
},
|
|
594
|
+
modules: assets.manifest
|
|
595
|
+
? modules.concat({
|
|
596
|
+
name: "__STATIC_CONTENT_MANIFEST",
|
|
597
|
+
content: JSON.stringify(assets.manifest),
|
|
598
|
+
type: "text",
|
|
599
|
+
})
|
|
600
|
+
: modules,
|
|
601
|
+
variables: assets.namespace
|
|
602
|
+
? {
|
|
603
|
+
...variables,
|
|
604
|
+
__STATIC_CONTENT: { namespaceId: assets.namespace },
|
|
605
|
+
}
|
|
606
|
+
: variables,
|
|
607
|
+
migrations: undefined, // no migrations in dev
|
|
608
|
+
compatibility_date: compatibilityDate,
|
|
609
|
+
compatibility_flags: compatibilityFlags,
|
|
610
|
+
usage_model: usageModel,
|
|
611
|
+
};
|
|
612
|
+
setToken(
|
|
613
|
+
await createWorker(init, {
|
|
614
|
+
accountId,
|
|
615
|
+
apiToken,
|
|
616
|
+
})
|
|
617
|
+
);
|
|
618
|
+
console.log(`⬣ Listening at http://localhost:${port}`);
|
|
619
|
+
}
|
|
620
|
+
start().catch((err) => {
|
|
621
|
+
// we want to log the error, but not end the process
|
|
622
|
+
// since it could recover after the developer fixes whatever's wrong
|
|
623
|
+
console.error("remote worker:", err);
|
|
624
|
+
});
|
|
625
|
+
}, [
|
|
626
|
+
name,
|
|
627
|
+
bundle,
|
|
628
|
+
format,
|
|
629
|
+
accountId,
|
|
630
|
+
apiToken,
|
|
631
|
+
port,
|
|
632
|
+
sitesFolder,
|
|
633
|
+
compatibilityDate,
|
|
634
|
+
compatibilityFlags,
|
|
635
|
+
usageModel,
|
|
636
|
+
]);
|
|
637
|
+
return token;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function useProxy({
|
|
641
|
+
token,
|
|
642
|
+
publicRoot,
|
|
643
|
+
port,
|
|
644
|
+
}: {
|
|
645
|
+
token: CfPreviewToken | void;
|
|
646
|
+
publicRoot: void | string;
|
|
647
|
+
port: number;
|
|
648
|
+
}) {
|
|
649
|
+
useEffect(() => {
|
|
650
|
+
if (!token) return;
|
|
651
|
+
// TODO(soon): since headers are added in callbacks, the server
|
|
652
|
+
// does not need to restart when changes are made.
|
|
653
|
+
const host = token.host;
|
|
654
|
+
const proxy = createHttpProxy({
|
|
655
|
+
host,
|
|
656
|
+
assetPath: typeof publicRoot === "string" ? publicRoot : null,
|
|
657
|
+
onRequest: (headers) => {
|
|
658
|
+
headers["cf-workers-preview-token"] = token.value;
|
|
659
|
+
},
|
|
660
|
+
onResponse: (headers) => {
|
|
661
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
662
|
+
// Rewrite the remote host to the local host.
|
|
663
|
+
if (typeof value === "string" && value.includes(host)) {
|
|
664
|
+
headers[name] = value
|
|
665
|
+
.replaceAll(`https://${host}`, `http://localhost:${port}`)
|
|
666
|
+
.replaceAll(host, `localhost:${port}`);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
},
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
const server = proxy.listen(port);
|
|
673
|
+
|
|
674
|
+
// TODO(soon): refactor logging format into its own function
|
|
675
|
+
proxy.on("request", function (req, res) {
|
|
676
|
+
// log all requests
|
|
677
|
+
console.log(
|
|
678
|
+
new Date().toLocaleTimeString(),
|
|
679
|
+
req.method,
|
|
680
|
+
req.url,
|
|
681
|
+
res.statusCode
|
|
682
|
+
);
|
|
683
|
+
});
|
|
684
|
+
proxy.on("upgrade", (req) => {
|
|
685
|
+
console.log(
|
|
686
|
+
new Date().toLocaleTimeString(),
|
|
687
|
+
req.method,
|
|
688
|
+
req.url,
|
|
689
|
+
101,
|
|
690
|
+
"(WebSocket)"
|
|
691
|
+
);
|
|
692
|
+
});
|
|
693
|
+
proxy.on("error", (err) => {
|
|
694
|
+
console.error(new Date().toLocaleTimeString(), err);
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
return () => {
|
|
698
|
+
proxy.close();
|
|
699
|
+
server.close();
|
|
700
|
+
};
|
|
701
|
+
}, [token, publicRoot, port]);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function useInspector(inspectorUrl: string | void) {
|
|
705
|
+
useEffect(() => {
|
|
706
|
+
if (!inspectorUrl) return;
|
|
707
|
+
|
|
708
|
+
const inspector = new DtInspector(inspectorUrl);
|
|
709
|
+
const abortController = inspector.proxyTo(9229);
|
|
710
|
+
return () => {
|
|
711
|
+
inspector.close();
|
|
712
|
+
abortController.abort();
|
|
713
|
+
};
|
|
714
|
+
}, [inspectorUrl]);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function sleep(period: number) {
|
|
718
|
+
return new Promise((resolve) => setTimeout(resolve, period));
|
|
719
|
+
}
|
|
720
|
+
const SLEEP_DURATION = 2000;
|
|
721
|
+
// really need a first class api for this
|
|
722
|
+
const hostNameRegex = /userHostname="(.*)"/g;
|
|
723
|
+
async function findTunnelHostname() {
|
|
724
|
+
let hostName: string;
|
|
725
|
+
while (!hostName) {
|
|
726
|
+
try {
|
|
727
|
+
const resp = await fetch("http://localhost:8789/metrics");
|
|
728
|
+
const data = await resp.text();
|
|
729
|
+
const matches = Array.from(data.matchAll(hostNameRegex));
|
|
730
|
+
hostName = matches[0][1];
|
|
731
|
+
} catch (err) {
|
|
732
|
+
await sleep(SLEEP_DURATION);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
return hostName;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function useTunnel(toggle: boolean) {
|
|
739
|
+
const tunnel = useRef<ReturnType<typeof spawn>>();
|
|
740
|
+
const removeSignalExitListener = useRef<() => void>();
|
|
741
|
+
// TODO: test if cloudflared is available, if not
|
|
742
|
+
// point them to a url where they can get docs to install it
|
|
743
|
+
useEffect(() => {
|
|
744
|
+
async function startTunnel() {
|
|
745
|
+
if (toggle) {
|
|
746
|
+
try {
|
|
747
|
+
await commandExists("cloudflared");
|
|
748
|
+
} catch (e) {
|
|
749
|
+
console.error(
|
|
750
|
+
"To share your worker on the internet, please install `cloudflared` from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation"
|
|
751
|
+
);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
console.log("⎔ Starting a tunnel...");
|
|
755
|
+
tunnel.current = spawn("cloudflared", [
|
|
756
|
+
"tunnel",
|
|
757
|
+
"--url",
|
|
758
|
+
"http://localhost:8787",
|
|
759
|
+
"--metrics",
|
|
760
|
+
"localhost:8789",
|
|
761
|
+
]);
|
|
762
|
+
|
|
763
|
+
tunnel.current.on("close", (code) => {
|
|
764
|
+
if (code !== 0) {
|
|
765
|
+
console.log(`Tunnel process exited with code ${code}`);
|
|
766
|
+
}
|
|
767
|
+
});
|
|
768
|
+
|
|
769
|
+
removeSignalExitListener.current = onExit((_code, _signal) => {
|
|
770
|
+
console.log("⎔ Shutting down local tunnel.");
|
|
771
|
+
tunnel.current?.kill();
|
|
772
|
+
tunnel.current = undefined;
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
const hostName = await findTunnelHostname();
|
|
776
|
+
await clipboardy.write(hostName);
|
|
777
|
+
console.log(`⬣ Sharing at ${hostName}, copied to clipboard.`);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
startTunnel().catch((err) => {
|
|
782
|
+
console.error("tunnel:", err);
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
return () => {
|
|
786
|
+
if (tunnel.current) {
|
|
787
|
+
console.log("⎔ Shutting down tunnel.");
|
|
788
|
+
tunnel.current?.kill();
|
|
789
|
+
tunnel.current = undefined;
|
|
790
|
+
removeSignalExitListener.current && removeSignalExitListener.current();
|
|
791
|
+
removeSignalExitListener.current = undefined;
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
}, [toggle]);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
type useHotkeysInitialState = {
|
|
798
|
+
local: boolean;
|
|
799
|
+
tunnel: boolean;
|
|
800
|
+
};
|
|
801
|
+
function useHotkeys(initial: useHotkeysInitialState, port: number) {
|
|
802
|
+
// UGH, we should put port in context instead
|
|
803
|
+
const [toggles, setToggles] = useState(initial);
|
|
804
|
+
useInput(
|
|
805
|
+
async (
|
|
806
|
+
input,
|
|
807
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
808
|
+
key
|
|
809
|
+
) => {
|
|
810
|
+
switch (input) {
|
|
811
|
+
case "b": // open browser
|
|
812
|
+
await open(
|
|
813
|
+
`http://localhost:${port}/`
|
|
814
|
+
// {
|
|
815
|
+
// app: {
|
|
816
|
+
// name: open.apps.chrome, // TODO: fallback on other browsers
|
|
817
|
+
// },
|
|
818
|
+
// }
|
|
819
|
+
);
|
|
820
|
+
break;
|
|
821
|
+
case "d": // toggle inspector
|
|
822
|
+
await open(
|
|
823
|
+
`https://built-devtools.pages.dev/js_app?experiments=true&v8only=true&ws=localhost:9229/ws`
|
|
824
|
+
// {
|
|
825
|
+
// app: {
|
|
826
|
+
// name: open.apps.chrome,
|
|
827
|
+
// // todo - add firefox and edge fallbacks
|
|
828
|
+
// },
|
|
829
|
+
// }
|
|
830
|
+
);
|
|
831
|
+
break;
|
|
832
|
+
case "s": // toggle tunnel
|
|
833
|
+
setToggles((toggles) => ({ ...toggles, tunnel: !toggles.tunnel }));
|
|
834
|
+
break;
|
|
835
|
+
case "l": // toggle local
|
|
836
|
+
setToggles((toggles) => ({ ...toggles, local: !toggles.local }));
|
|
837
|
+
break;
|
|
838
|
+
case "q": // shut down
|
|
839
|
+
case "x": // shut down
|
|
840
|
+
process.exit(0);
|
|
841
|
+
break;
|
|
842
|
+
default:
|
|
843
|
+
// nothing?
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
);
|
|
848
|
+
return toggles;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
function ErrorFallback(props: {
|
|
852
|
+
error: Error;
|
|
853
|
+
resetErrorBoundary: () => void;
|
|
854
|
+
}) {
|
|
855
|
+
useEffect(() => {
|
|
856
|
+
console.error(props.error);
|
|
857
|
+
process.exit(1);
|
|
858
|
+
});
|
|
859
|
+
return (
|
|
860
|
+
<Box>
|
|
861
|
+
<Text>Something went wrong:</Text>
|
|
862
|
+
<Text>{props.error.message}</Text>
|
|
863
|
+
</Box>
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
export default withErrorBoundary(Dev, { FallbackComponent: ErrorFallback });
|