wrangler 0.0.0-de73fa2 → 0.0.0-e151223

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.
Files changed (48) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +12 -9
  3. package/pages/functions/buildWorker.ts +1 -1
  4. package/pages/functions/filepath-routing.test.ts +21 -13
  5. package/pages/functions/filepath-routing.ts +38 -28
  6. package/pages/functions/routes.ts +8 -6
  7. package/pages/functions/template-worker.ts +16 -24
  8. package/src/__tests__/dev.test.tsx +7 -3
  9. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  10. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  11. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +10 -8
  12. package/src/__tests__/helpers/mock-console.ts +56 -0
  13. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  14. package/src/__tests__/helpers/mock-kv.ts +40 -0
  15. package/src/__tests__/helpers/mock-user.ts +27 -0
  16. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  17. package/src/__tests__/helpers/run-wrangler.ts +14 -0
  18. package/src/__tests__/index.test.ts +173 -54
  19. package/src/__tests__/jest.setup.ts +14 -2
  20. package/src/__tests__/kv.test.ts +327 -289
  21. package/src/__tests__/logout.test.ts +50 -0
  22. package/src/__tests__/publish.test.ts +1222 -0
  23. package/src/__tests__/secret.test.ts +210 -0
  24. package/src/__tests__/whoami.test.tsx +86 -0
  25. package/src/api/form_data.ts +10 -7
  26. package/src/api/preview.ts +2 -2
  27. package/src/api/worker.ts +3 -3
  28. package/src/cfetch/index.ts +13 -4
  29. package/src/cfetch/internal.ts +10 -7
  30. package/src/config.ts +162 -8
  31. package/src/dev.tsx +105 -57
  32. package/src/dialogs.tsx +2 -2
  33. package/src/index.tsx +388 -302
  34. package/src/inspect.ts +80 -23
  35. package/src/kv.tsx +1 -1
  36. package/src/module-collection.ts +5 -6
  37. package/src/npm-installer.ts +20 -0
  38. package/src/pages.tsx +42 -28
  39. package/src/paths.ts +26 -0
  40. package/src/proxy.ts +78 -10
  41. package/src/publish.ts +97 -51
  42. package/src/sites.tsx +175 -53
  43. package/src/user.tsx +51 -59
  44. package/src/whoami.tsx +69 -0
  45. package/wrangler-dist/cli.js +111330 -110202
  46. package/wrangler-dist/cli.js.map +3 -3
  47. package/src/__tests__/run-in-tmp.ts +0 -19
  48. package/src/__tests__/run-wrangler.ts +0 -32
package/src/dev.tsx CHANGED
@@ -1,32 +1,32 @@
1
- import esbuild from "esbuild";
2
1
  import assert from "node:assert";
3
2
  import { spawn } from "node:child_process";
4
- import { readFile } from "node:fs/promises";
5
3
  import { existsSync } from "node:fs";
4
+ import { readFile } from "node:fs/promises";
6
5
  import path from "node:path";
7
- import { Box, Text, useApp, useInput } from "ink";
8
6
  import { watch } from "chokidar";
9
7
  import clipboardy from "clipboardy";
10
8
  import commandExists from "command-exists";
11
- import { execa } from "execa";
12
- import fetch from "node-fetch";
9
+ import * as esbuild from "esbuild";
10
+ import { execaCommand } from "execa";
11
+ import { Box, Text, useApp, useInput } from "ink";
13
12
  import open from "open";
14
13
  import React, { useState, useEffect, useRef } from "react";
15
14
  import { withErrorBoundary, useErrorHandler } from "react-error-boundary";
16
15
  import onExit from "signal-exit";
17
16
  import tmp from "tmp-promise";
18
- import type { DirectoryResult } from "tmp-promise";
17
+ import { fetch } from "undici";
19
18
 
20
- import type { CfPreviewToken } from "./api/preview";
21
- import type { CfModule } from "./api/worker";
22
19
  import { createWorker } from "./api/worker";
23
- import type { CfWorkerInit } from "./api/worker";
24
20
 
25
21
  import useInspector from "./inspect";
26
22
  import makeModuleCollector from "./module-collection";
27
- import { usePreviewServer } from "./proxy";
23
+ import { usePreviewServer, waitForPortToBeAvailable } from "./proxy";
28
24
  import { syncAssets } from "./sites";
29
25
  import { getAPIToken } from "./user";
26
+ import type { CfPreviewToken } from "./api/preview";
27
+ import type { CfModule, CfWorkerInit } from "./api/worker";
28
+ import type { AssetPaths } from "./sites";
29
+ import type { DirectoryResult } from "tmp-promise";
30
30
 
31
31
  type CfScriptFormat = undefined | "modules" | "service-worker";
32
32
 
@@ -39,9 +39,10 @@ export type DevProps = {
39
39
  initialMode: "local" | "remote";
40
40
  jsxFactory: undefined | string;
41
41
  jsxFragment: undefined | string;
42
+ enableLocalPersistence: boolean;
42
43
  bindings: CfWorkerInit["bindings"];
43
44
  public: undefined | string;
44
- site: undefined | string;
45
+ assetPaths: undefined | AssetPaths;
45
46
  compatibilityDate: undefined | string;
46
47
  compatibilityFlags: undefined | string[];
47
48
  usageModel: undefined | "bundled" | "unbound";
@@ -50,6 +51,7 @@ export type DevProps = {
50
51
  cwd?: undefined | string;
51
52
  watch_dir?: undefined | string;
52
53
  };
54
+ env: string | undefined;
53
55
  };
54
56
 
55
57
  function Dev(props: DevProps): JSX.Element {
@@ -98,9 +100,10 @@ function Dev(props: DevProps): JSX.Element {
98
100
  bundle={bundle}
99
101
  format={props.format}
100
102
  bindings={props.bindings}
101
- site={props.site}
103
+ assetPaths={props.assetPaths}
102
104
  public={props.public}
103
105
  port={port}
106
+ enableLocalPersistence={props.enableLocalPersistence}
104
107
  />
105
108
  ) : (
106
109
  <Remote
@@ -110,12 +113,13 @@ function Dev(props: DevProps): JSX.Element {
110
113
  accountId={props.accountId}
111
114
  apiToken={apiToken}
112
115
  bindings={props.bindings}
113
- site={props.site}
116
+ assetPaths={props.assetPaths}
114
117
  public={props.public}
115
118
  port={port}
116
119
  compatibilityDate={props.compatibilityDate}
117
120
  compatibilityFlags={props.compatibilityFlags}
118
121
  usageModel={props.usageModel}
122
+ env={props.env}
119
123
  />
120
124
  )}
121
125
  <Box borderStyle="round" paddingLeft={1} paddingRight={1}>
@@ -136,7 +140,7 @@ function Remote(props: {
136
140
  bundle: EsbuildBundle | undefined;
137
141
  format: CfScriptFormat;
138
142
  public: undefined | string;
139
- site: undefined | string;
143
+ assetPaths: undefined | AssetPaths;
140
144
  port: number;
141
145
  accountId: undefined | string;
142
146
  apiToken: undefined | string;
@@ -144,6 +148,7 @@ function Remote(props: {
144
148
  compatibilityDate: string | undefined;
145
149
  compatibilityFlags: undefined | string[];
146
150
  usageModel: undefined | "bundled" | "unbound";
151
+ env: string | undefined;
147
152
  }) {
148
153
  assert(props.accountId, "accountId is required");
149
154
  assert(props.apiToken, "apiToken is required");
@@ -155,11 +160,12 @@ function Remote(props: {
155
160
  accountId: props.accountId,
156
161
  apiToken: props.apiToken,
157
162
  bindings: props.bindings,
158
- sitesFolder: props.site,
163
+ assetPaths: props.assetPaths,
159
164
  port: props.port,
160
165
  compatibilityDate: props.compatibilityDate,
161
166
  compatibilityFlags: props.compatibilityFlags,
162
167
  usageModel: props.usageModel,
168
+ env: props.env,
163
169
  });
164
170
 
165
171
  usePreviewServer({
@@ -180,16 +186,20 @@ function Local(props: {
180
186
  bundle: EsbuildBundle | undefined;
181
187
  format: CfScriptFormat;
182
188
  bindings: CfWorkerInit["bindings"];
189
+ assetPaths: undefined | AssetPaths;
183
190
  public: undefined | string;
184
- site: undefined | string;
185
191
  port: number;
192
+ enableLocalPersistence: boolean;
186
193
  }) {
187
194
  const { inspectorUrl } = useLocalWorker({
188
195
  name: props.name,
189
196
  bundle: props.bundle,
190
197
  format: props.format,
191
198
  bindings: props.bindings,
199
+ assetPaths: props.assetPaths,
200
+ public: props.public,
192
201
  port: props.port,
202
+ enableLocalPersistence: props.enableLocalPersistence,
193
203
  });
194
204
  useInspector({ inspectorUrl, port: 9229, logToTerminal: false });
195
205
  return null;
@@ -200,10 +210,13 @@ function useLocalWorker(props: {
200
210
  bundle: EsbuildBundle | undefined;
201
211
  format: CfScriptFormat;
202
212
  bindings: CfWorkerInit["bindings"];
213
+ assetPaths: undefined | AssetPaths;
214
+ public: undefined | string;
203
215
  port: number;
216
+ enableLocalPersistence: boolean;
204
217
  }) {
205
218
  // TODO: pass vars via command line
206
- const { bundle, format, bindings, port } = props;
219
+ const { bundle, format, bindings, port, assetPaths } = props;
207
220
  const local = useRef<ReturnType<typeof spawn>>();
208
221
  const removeSignalExitListener = useRef<() => void>();
209
222
  const [inspectorUrl, setInspectorUrl] = useState<string | undefined>();
@@ -221,6 +234,13 @@ function useLocalWorker(props: {
221
234
  return;
222
235
  }
223
236
 
237
+ await waitForPortToBeAvailable(port, { retryPeriod: 200, timeout: 2000 });
238
+ if (props.public) {
239
+ throw new Error(
240
+ '⎔ A "public" folder is not yet supported in local mode.'
241
+ );
242
+ }
243
+
224
244
  console.log("⎔ Starting a local server...");
225
245
  // TODO: just use execa for this
226
246
  local.current = spawn("node", [
@@ -237,9 +257,23 @@ function useLocalWorker(props: {
237
257
  path.join(__dirname, "../miniflare-config-stubs/package.empty.json"),
238
258
  "--port",
239
259
  port.toString(),
240
- "--kv-persist",
241
- "--cache-persist",
242
- "--do-persist",
260
+ ...(assetPaths
261
+ ? [
262
+ "--site",
263
+ assetPaths.baseDirectory,
264
+ ...assetPaths.includePatterns.map((pattern) => [
265
+ "--site-include",
266
+ pattern,
267
+ ]),
268
+ ...assetPaths.excludePatterns.map((pattern) => [
269
+ "--site-exclude",
270
+ pattern,
271
+ ]),
272
+ ].flatMap((x) => x)
273
+ : []),
274
+ ...(props.enableLocalPersistence
275
+ ? ["--kv-persist", "--cache-persist", "--do-persist"]
276
+ : []),
243
277
  ...Object.entries(bindings.vars || {}).flatMap(([key, value]) => {
244
278
  return ["--binding", `${key}=${value}`];
245
279
  }),
@@ -318,6 +352,9 @@ function useLocalWorker(props: {
318
352
  bindings.durable_objects?.bindings,
319
353
  bindings.kv_namespaces,
320
354
  bindings.vars,
355
+ props.enableLocalPersistence,
356
+ assetPaths,
357
+ props.public,
321
358
  ]);
322
359
  return { inspectorUrl };
323
360
  }
@@ -345,7 +382,7 @@ function useTmpDir(): string | undefined {
345
382
  return () => {
346
383
  dir.cleanup().catch(() => {
347
384
  // extremely unlikely,
348
- // but it's 2021 after all
385
+ // but it's 2022 after all
349
386
  console.error("failed to cleanup tmp dir");
350
387
  });
351
388
  };
@@ -370,20 +407,21 @@ function useCustomBuild(
370
407
  if (!command) return;
371
408
  let cmd, interval;
372
409
  console.log("running:", command);
373
- const commandPieces = command.split(" ");
374
- cmd = execa(commandPieces[0], commandPieces.slice(1), {
410
+ cmd = execaCommand(command, {
375
411
  ...(cwd && { cwd }),
412
+ shell: true,
376
413
  stderr: "inherit",
377
414
  stdout: "inherit",
378
415
  });
379
416
  if (watch_dir) {
380
417
  watch(watch_dir, { persistent: true, ignoreInitial: true }).on(
381
418
  "all",
382
- (_event, _path) => {
383
- console.log(`The file ${path} changed, restarting build...`);
419
+ (_event, filePath) => {
420
+ console.log(`The file ${filePath} changed, restarting build...`);
384
421
  cmd.kill();
385
- cmd = execa(commandPieces[0], commandPieces.slice(1), {
422
+ cmd = execaCommand(command, {
386
423
  ...(cwd && { cwd }),
424
+ shell: true,
387
425
  stderr: "inherit",
388
426
  stdout: "inherit",
389
427
  });
@@ -441,7 +479,7 @@ function useEsbuild(props: {
441
479
  const { entry, destination, staticRoot, jsxFactory, jsxFragment } = props;
442
480
  const [bundle, setBundle] = useState<EsbuildBundle>();
443
481
  useEffect(() => {
444
- let result: esbuild.BuildResult;
482
+ let result: esbuild.BuildResult | undefined;
445
483
  async function build() {
446
484
  if (!destination || !entry) return;
447
485
  const moduleCollector = makeModuleCollector();
@@ -454,6 +492,9 @@ function useEsbuild(props: {
454
492
  sourcemap: true,
455
493
  loader: {
456
494
  ".js": "jsx",
495
+ ".html": "text",
496
+ ".pem": "text",
497
+ ".txt": "text",
457
498
  },
458
499
  ...(jsxFactory && { jsxFactory }),
459
500
  ...(jsxFragment && { jsxFragment }),
@@ -508,9 +549,7 @@ function useEsbuild(props: {
508
549
  // so this is a no-op error handler
509
550
  });
510
551
  return () => {
511
- if (result && result.stop) {
512
- result.stop();
513
- }
552
+ result?.stop?.();
514
553
  };
515
554
  }, [entry, destination, staticRoot, jsxFactory, jsxFragment]);
516
555
  return bundle;
@@ -524,11 +563,12 @@ function useWorker(props: {
524
563
  accountId: string;
525
564
  apiToken: string;
526
565
  bindings: CfWorkerInit["bindings"];
527
- sitesFolder: undefined | string;
566
+ assetPaths: undefined | AssetPaths;
528
567
  port: number;
529
568
  compatibilityDate: string | undefined;
530
569
  compatibilityFlags: string[] | undefined;
531
570
  usageModel: undefined | "bundled" | "unbound";
571
+ env: string | undefined;
532
572
  }): CfPreviewToken | undefined {
533
573
  const {
534
574
  name,
@@ -538,7 +578,7 @@ function useWorker(props: {
538
578
  accountId,
539
579
  apiToken,
540
580
  bindings,
541
- sitesFolder,
581
+ assetPaths,
542
582
  compatibilityDate,
543
583
  compatibilityFlags,
544
584
  usageModel,
@@ -570,32 +610,34 @@ function useWorker(props: {
570
610
  if (!startedRef.current) {
571
611
  startedRef.current = true;
572
612
  } else {
573
- console.log("⎔ Detected changes, restarting server...");
613
+ console.log("⎔ Detected changes, restarted server.");
614
+ }
615
+
616
+ const assets = await syncAssets(
617
+ accountId,
618
+ name || path.basename(bundle.path),
619
+ assetPaths,
620
+ true,
621
+ props.env
622
+ ); // TODO: cancellable?
623
+
624
+ const workerType = format || bundle.type === "esm" ? "esm" : "commonjs";
625
+ let content = await readFile(bundle.path, "utf-8");
626
+ if (workerType !== "esm" && assets.manifest) {
627
+ content = `const __STATIC_CONTENT_MANIFEST = ${JSON.stringify(
628
+ assets.manifest
629
+ )};\n${content}`;
574
630
  }
575
631
 
576
- const assets = sitesFolder
577
- ? await syncAssets(
578
- accountId,
579
- path.basename(bundle.path),
580
- sitesFolder,
581
- true,
582
- undefined // TODO: env
583
- )
584
- : {
585
- manifest: undefined,
586
- namespace: undefined,
587
- }; // TODO: cancellable?
588
-
589
- const content = await readFile(bundle.path, "utf-8");
590
632
  const init: CfWorkerInit = {
591
633
  name,
592
634
  main: {
593
635
  name: path.basename(bundle.path),
594
- type: format || bundle.type === "esm" ? "esm" : "commonjs",
636
+ type: workerType,
595
637
  content,
596
638
  },
597
639
  modules: modules.concat(
598
- assets.manifest
640
+ assets.manifest && workerType === "esm"
599
641
  ? {
600
642
  name: "__STATIC_CONTENT_MANIFEST",
601
643
  content: JSON.stringify(assets.manifest),
@@ -635,12 +677,13 @@ function useWorker(props: {
635
677
  accountId,
636
678
  apiToken,
637
679
  port,
638
- sitesFolder,
680
+ assetPaths,
639
681
  compatibilityDate,
640
682
  compatibilityFlags,
641
683
  usageModel,
642
684
  bindings,
643
685
  modules,
686
+ props.env,
644
687
  ]);
645
688
  return token;
646
689
  }
@@ -738,29 +781,34 @@ function useHotkeys(initial: useHotkeysInitialState, port: number) {
738
781
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
739
782
  key
740
783
  ) => {
741
- switch (input) {
742
- case "b": // open browser
784
+ switch (input.toLowerCase()) {
785
+ // open browser
786
+ case "b":
743
787
  await open(`http://localhost:${port}/`);
744
788
  break;
745
- case "d": // toggle inspector
789
+ // toggle inspector
790
+ case "d":
746
791
  await open(
747
792
  `https://built-devtools.pages.dev/js_app?experiments=true&v8only=true&ws=localhost:9229/ws`
748
793
  );
749
794
  break;
750
- case "s": // toggle tunnel
795
+ // toggle tunnel
796
+ case "s":
751
797
  setToggles((previousToggles) => ({
752
798
  ...previousToggles,
753
799
  tunnel: !previousToggles.tunnel,
754
800
  }));
755
801
  break;
756
- case "l": // toggle local
802
+ // toggle local
803
+ case "l":
757
804
  setToggles((previousToggles) => ({
758
805
  ...previousToggles,
759
806
  local: !previousToggles.local,
760
807
  }));
761
808
  break;
762
- case "q": // shut down
763
- case "x": // shut down
809
+ // shut down
810
+ case "q":
811
+ case "x":
764
812
  process.exit(0);
765
813
  break;
766
814
  default:
package/src/dialogs.tsx CHANGED
@@ -1,7 +1,7 @@
1
- import * as React from "react";
2
- import { useState } from "react";
3
1
  import { Box, Text, useInput, render } from "ink";
4
2
  import TextInput from "ink-text-input";
3
+ import * as React from "react";
4
+ import { useState } from "react";
5
5
  type ConfirmProps = {
6
6
  text: string;
7
7
  onConfirm: (answer: boolean) => void;