wrangler 2.0.27 → 2.1.0

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 (65) hide show
  1. package/bin/wrangler.js +1 -1
  2. package/miniflare-dist/index.mjs +1141 -369
  3. package/package.json +6 -4
  4. package/src/__tests__/api-dev.test.ts +19 -0
  5. package/src/__tests__/configuration.test.ts +27 -27
  6. package/src/__tests__/dev.test.tsx +8 -6
  7. package/src/__tests__/helpers/hello-world-worker.js +5 -0
  8. package/src/__tests__/helpers/mock-cfetch.ts +4 -4
  9. package/src/__tests__/helpers/mock-console.ts +11 -2
  10. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  11. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  12. package/src/__tests__/index.test.ts +37 -37
  13. package/src/__tests__/init.test.ts +356 -5
  14. package/src/__tests__/jest.setup.ts +13 -0
  15. package/src/__tests__/middleware.test.ts +768 -0
  16. package/src/__tests__/pages.test.ts +829 -104
  17. package/src/__tests__/paths.test.ts +17 -0
  18. package/src/__tests__/publish.test.ts +512 -445
  19. package/src/__tests__/tail.test.ts +79 -72
  20. package/src/__tests__/test-old-node-version.js +3 -3
  21. package/src/__tests__/worker-namespace.test.ts +37 -35
  22. package/src/api/dev.ts +93 -28
  23. package/src/bundle.ts +239 -12
  24. package/src/cfetch/internal.ts +64 -3
  25. package/src/cli.ts +1 -1
  26. package/src/config/environment.ts +1 -1
  27. package/src/config/index.ts +4 -4
  28. package/src/config/validation.ts +3 -3
  29. package/src/create-worker-upload-form.ts +29 -26
  30. package/src/dev/dev.tsx +3 -1
  31. package/src/dev/local.tsx +319 -171
  32. package/src/dev/remote.tsx +16 -4
  33. package/src/dev/start-server.ts +416 -0
  34. package/src/dev/use-esbuild.ts +4 -0
  35. package/src/dev.tsx +340 -166
  36. package/src/dialogs.tsx +12 -0
  37. package/src/{worker-namespace.ts → dispatch-namespace.ts} +18 -18
  38. package/src/entry.ts +2 -1
  39. package/src/index.tsx +59 -12
  40. package/src/init.ts +291 -16
  41. package/src/metrics/send-event.ts +6 -5
  42. package/src/miniflare-cli/assets.ts +130 -476
  43. package/src/miniflare-cli/index.ts +39 -33
  44. package/src/pages/constants.ts +3 -0
  45. package/src/pages/dev.tsx +8 -3
  46. package/src/pages/functions/buildPlugin.ts +2 -1
  47. package/src/pages/functions/buildWorker.ts +2 -1
  48. package/src/pages/functions/routes-transformation.test.ts +12 -1
  49. package/src/pages/functions/routes-transformation.ts +7 -1
  50. package/src/pages/hash.tsx +13 -0
  51. package/src/pages/publish.tsx +82 -38
  52. package/src/pages/upload.tsx +3 -18
  53. package/src/paths.ts +20 -1
  54. package/src/publish.ts +49 -8
  55. package/src/tail/filters.ts +1 -5
  56. package/src/tail/index.ts +6 -3
  57. package/src/worker.ts +10 -9
  58. package/src/zones.ts +91 -0
  59. package/templates/middleware/common.ts +62 -0
  60. package/templates/middleware/loader-modules.ts +84 -0
  61. package/templates/middleware/loader-sw.ts +213 -0
  62. package/templates/middleware/middleware-pretty-error.ts +40 -0
  63. package/templates/middleware/middleware-scheduled.ts +14 -0
  64. package/wrangler-dist/cli.d.ts +22 -8
  65. package/wrangler-dist/cli.js +71020 -65212
@@ -34,7 +34,7 @@ async function createWorkerNamespace(accountId: string, name: string) {
34
34
  }
35
35
  );
36
36
  logger.log(
37
- `Created Worker namespace "${name}" with ID "${namespace.namespace_id}"`
37
+ `Created dispatch namespace "${name}" with ID "${namespace.namespace_id}"`
38
38
  );
39
39
  }
40
40
 
@@ -46,7 +46,7 @@ async function deleteWorkerNamespace(accountId: string, name: string) {
46
46
  `/accounts/${accountId}/workers/dispatch/namespaces/${name}`,
47
47
  { method: "DELETE" }
48
48
  );
49
- logger.log(`Deleted Worker namespace "${name}"`);
49
+ logger.log(`Deleted dispatch namespace "${name}"`);
50
50
  }
51
51
 
52
52
  /**
@@ -101,7 +101,7 @@ async function renameWorkerNamespace(
101
101
  body: JSON.stringify({ name: newName }),
102
102
  }
103
103
  );
104
- logger.log(`Renamed Worker namespace "${oldName}" to "${newName}"`);
104
+ logger.log(`Renamed dispatch namespace "${oldName}" to "${newName}"`);
105
105
  }
106
106
 
107
107
  export function workerNamespaceCommands(
@@ -110,20 +110,20 @@ export function workerNamespaceCommands(
110
110
  ): Argv {
111
111
  return workerNamespaceYargs
112
112
  .command(subHelp)
113
- .command("list", "List all Worker namespaces", {}, async (args) => {
113
+ .command("list", "List all dispatch namespaces", {}, async (args) => {
114
114
  const config = readConfig(args.config as ConfigPath, args);
115
115
  const accountId = await requireAuth(config);
116
116
  await listWorkerNamespaces(accountId);
117
- await metrics.sendMetricsEvent("list worker namespaces", {
117
+ await metrics.sendMetricsEvent("list dispatch namespaces", {
118
118
  sendMetrics: config.send_metrics,
119
119
  });
120
120
  })
121
121
  .command(
122
122
  "get <name>",
123
- "Get information about a Worker namespace",
123
+ "Get information about a dispatch namespace",
124
124
  (yargs) => {
125
125
  return yargs.positional("name", {
126
- describe: "Name of the Worker namespace",
126
+ describe: "Name of the dispatch namespace",
127
127
  type: "string",
128
128
  demandOption: true,
129
129
  });
@@ -132,17 +132,17 @@ export function workerNamespaceCommands(
132
132
  const config = readConfig(args.config as ConfigPath, args);
133
133
  const accountId = await requireAuth(config);
134
134
  await getWorkerNamespaceInfo(accountId, args.name);
135
- await metrics.sendMetricsEvent("view worker namespace", {
135
+ await metrics.sendMetricsEvent("view dispatch namespace", {
136
136
  sendMetrics: config.send_metrics,
137
137
  });
138
138
  }
139
139
  )
140
140
  .command(
141
141
  "create <name>",
142
- "Create a Worker namespace",
142
+ "Create a dispatch namespace",
143
143
  (yargs) => {
144
144
  return yargs.positional("name", {
145
- describe: "Name of the Worker namespace",
145
+ describe: "Name of the dispatch namespace",
146
146
  type: "string",
147
147
  demandOption: true,
148
148
  });
@@ -152,17 +152,17 @@ export function workerNamespaceCommands(
152
152
  const config = readConfig(args.config as ConfigPath, args);
153
153
  const accountId = await requireAuth(config);
154
154
  await createWorkerNamespace(accountId, args.name);
155
- await metrics.sendMetricsEvent("create worker namespace", {
155
+ await metrics.sendMetricsEvent("create dispatch namespace", {
156
156
  sendMetrics: config.send_metrics,
157
157
  });
158
158
  }
159
159
  )
160
160
  .command(
161
161
  "delete <name>",
162
- "Delete a Worker namespace",
162
+ "Delete a dispatch namespace",
163
163
  (yargs) => {
164
164
  return yargs.positional("name", {
165
- describe: "Name of the Worker namespace",
165
+ describe: "Name of the dispatch namespace",
166
166
  type: "string",
167
167
  demandOption: true,
168
168
  });
@@ -172,23 +172,23 @@ export function workerNamespaceCommands(
172
172
  const config = readConfig(args.config as ConfigPath, args);
173
173
  const accountId = await requireAuth(config);
174
174
  await deleteWorkerNamespace(accountId, args.name);
175
- await metrics.sendMetricsEvent("delete worker namespace", {
175
+ await metrics.sendMetricsEvent("delete dispatch namespace", {
176
176
  sendMetrics: config.send_metrics,
177
177
  });
178
178
  }
179
179
  )
180
180
  .command(
181
181
  "rename <old-name> <new-name>",
182
- "Rename a Worker namespace",
182
+ "Rename a dispatch namespace",
183
183
  (yargs) => {
184
184
  return yargs
185
185
  .positional("old-name", {
186
- describe: "Name of the Worker namespace",
186
+ describe: "Name of the dispatch namespace",
187
187
  type: "string",
188
188
  demandOption: true,
189
189
  })
190
190
  .positional("new-name", {
191
- describe: "New name of the Worker namespace",
191
+ describe: "New name of the dispatch namespace",
192
192
  type: "string",
193
193
  demandOption: true,
194
194
  });
@@ -198,7 +198,7 @@ export function workerNamespaceCommands(
198
198
  const config = readConfig(args.config as ConfigPath, args);
199
199
  const accountId = await requireAuth(config);
200
200
  await renameWorkerNamespace(accountId, args.oldName, args.newName);
201
- await metrics.sendMetricsEvent("rename worker namespace", {
201
+ await metrics.sendMetricsEvent("rename dispatch namespace", {
202
202
  sendMetrics: config.send_metrics,
203
203
  });
204
204
  }
package/src/entry.ts CHANGED
@@ -4,6 +4,7 @@ import path from "node:path";
4
4
  import * as esbuild from "esbuild";
5
5
  import { execaCommand } from "execa";
6
6
  import { logger } from "./logger";
7
+ import { getBasePath } from "./paths";
7
8
  import type { Config } from "./config";
8
9
  import type { CfScriptFormat } from "./worker";
9
10
  import type { Metafile } from "esbuild";
@@ -40,7 +41,7 @@ export async function getEntry(
40
41
  : // site.entry-point could be a directory
41
42
  path.resolve(config.site?.["entry-point"], "index.js");
42
43
  } else if (args.assets || config.assets) {
43
- file = path.resolve(__dirname, "../templates/no-op-worker.js");
44
+ file = path.resolve(getBasePath(), "templates/no-op-worker.js");
44
45
  } else {
45
46
  throw new Error(
46
47
  `Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler ${command} path/to/script\`) or the \`main\` config field.`
package/src/index.tsx CHANGED
@@ -15,6 +15,7 @@ import { findWranglerToml, readConfig } from "./config";
15
15
  import { createWorkerUploadForm } from "./create-worker-upload-form";
16
16
  import { devHandler, devOptions } from "./dev";
17
17
  import { confirm, prompt } from "./dialogs";
18
+ import { workerNamespaceCommands } from "./dispatch-namespace";
18
19
  import { getEntry } from "./entry";
19
20
  import { DeprecationError } from "./errors";
20
21
  import { generateHandler, generateOptions } from "./generate";
@@ -73,7 +74,7 @@ import {
73
74
  } from "./user";
74
75
  import { whoami } from "./whoami";
75
76
 
76
- import { workerNamespaceCommands } from "./worker-namespace";
77
+ import { getWorkerForZone } from "./zones";
77
78
  import type { Config } from "./config";
78
79
  import type { KeyValue } from "./kv";
79
80
  import type { TailCLIFilters } from "./tail";
@@ -98,6 +99,9 @@ const proxy =
98
99
 
99
100
  if (proxy) {
100
101
  setGlobalDispatcher(new ProxyAgent(proxy));
102
+ logger.log(
103
+ `Proxy environment variables detected. We'll use your proxy for fetch requests.`
104
+ );
101
105
  }
102
106
 
103
107
  export function getRules(config: Config): Config["rules"] {
@@ -469,6 +473,19 @@ function createCLIParser(argv: string[]) {
469
473
  requiresArg: true,
470
474
  array: true,
471
475
  })
476
+ .option("var", {
477
+ describe:
478
+ "A key-value pair to be injected into the script as a variable",
479
+ type: "string",
480
+ requiresArg: true,
481
+ array: true,
482
+ })
483
+ .option("define", {
484
+ describe: "A key-value pair to be substituted in the script",
485
+ type: "string",
486
+ requiresArg: true,
487
+ array: true,
488
+ })
472
489
  .option("triggers", {
473
490
  describe: "cron schedules to attach",
474
491
  alias: ["schedule", "schedules"],
@@ -562,6 +579,20 @@ function createCLIParser(argv: string[]) {
562
579
  );
563
580
  }
564
581
 
582
+ const cliVars =
583
+ args.var?.reduce<Record<string, string>>((collectVars, v) => {
584
+ const [key, ...value] = v.split(":");
585
+ collectVars[key] = value.join("");
586
+ return collectVars;
587
+ }, {}) || {};
588
+
589
+ const cliDefines =
590
+ args.define?.reduce<Record<string, string>>((collectDefines, d) => {
591
+ const [key, ...value] = d.split(":");
592
+ collectDefines[key] = value.join("");
593
+ return collectDefines;
594
+ }, {}) || {};
595
+
565
596
  const accountId = args.dryRun ? undefined : await requireAuth(config);
566
597
 
567
598
  const assetPaths =
@@ -585,6 +616,8 @@ function createCLIParser(argv: string[]) {
585
616
  ? new Date().toISOString().substring(0, 10)
586
617
  : args["compatibility-date"],
587
618
  compatibilityFlags: args["compatibility-flags"],
619
+ vars: cliVars,
620
+ defines: cliDefines,
588
621
  triggers: args.triggers,
589
622
  jsxFactory: args["jsx-factory"],
590
623
  jsxFragment: args["jsx-fragment"],
@@ -604,12 +637,12 @@ function createCLIParser(argv: string[]) {
604
637
 
605
638
  // tail
606
639
  wrangler.command(
607
- "tail [name]",
640
+ "tail [worker]",
608
641
  "🦚 Starts a log tailing session for a published Worker.",
609
642
  (yargs) => {
610
643
  return yargs
611
- .positional("name", {
612
- describe: "Name of the worker",
644
+ .positional("worker", {
645
+ describe: "Name or route of the worker to tail",
613
646
  type: "string",
614
647
  })
615
648
  .option("format", {
@@ -677,7 +710,22 @@ function createCLIParser(argv: string[]) {
677
710
  sendMetrics: config.send_metrics,
678
711
  });
679
712
 
680
- const scriptName = getLegacyScriptName(args, config);
713
+ let scriptName;
714
+
715
+ // Worker names can't contain "." (and most routes should), so use that as a discriminator
716
+ if (args.worker?.includes(".")) {
717
+ scriptName = await getWorkerForZone(args.worker);
718
+ if (args.format === "pretty") {
719
+ logger.log(
720
+ `Connecting to worker ${scriptName} at route ${args.worker}`
721
+ );
722
+ }
723
+ } else {
724
+ scriptName = getLegacyScriptName(
725
+ { name: args.worker, ...args },
726
+ config
727
+ );
728
+ }
681
729
 
682
730
  if (!scriptName) {
683
731
  throw new Error(
@@ -696,15 +744,13 @@ function createCLIParser(argv: string[]) {
696
744
  clientIp: args.ip,
697
745
  };
698
746
 
699
- const filters = translateCLICommandToFilterMessage(
700
- cliFilters,
701
- args.debug
702
- );
747
+ const filters = translateCLICommandToFilterMessage(cliFilters);
703
748
 
704
749
  const { tail, expiration, deleteTail } = await createTail(
705
750
  accountId,
706
751
  scriptName,
707
752
  filters,
753
+ args.debug,
708
754
  !isLegacyEnv(config) ? args.env : undefined
709
755
  );
710
756
 
@@ -966,7 +1012,7 @@ function createCLIParser(argv: string[]) {
966
1012
  wasm_modules: {},
967
1013
  text_blobs: {},
968
1014
  data_blobs: {},
969
- worker_namespaces: [],
1015
+ dispatch_namespaces: [],
970
1016
  logfwdr: { schema: undefined, bindings: [] },
971
1017
  unsafe: [],
972
1018
  },
@@ -975,6 +1021,7 @@ function createCLIParser(argv: string[]) {
975
1021
  compatibility_date: undefined,
976
1022
  compatibility_flags: undefined,
977
1023
  usage_model: undefined,
1024
+ keep_bindings: false, // this doesn't matter since it's a new script anyway
978
1025
  }),
979
1026
  }
980
1027
  );
@@ -2022,8 +2069,8 @@ function createCLIParser(argv: string[]) {
2022
2069
  });
2023
2070
 
2024
2071
  wrangler.command(
2025
- "worker-namespace",
2026
- "📦 Interact with a worker namespace",
2072
+ "dispatch-namespace",
2073
+ "📦 Interact with a dispatch namespace",
2027
2074
  (workerNamespaceYargs) => {
2028
2075
  return workerNamespaceCommands(workerNamespaceYargs, subHelp);
2029
2076
  }