wrangler 0.0.0-e6733a3 → 0.0.0-e6ada079

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of wrangler might be problematic. Click here for more details.

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