wrangler 0.0.0-e7a0cd5b → 0.0.0-e7cf0caa

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 (237) hide show
  1. package/README.md +3 -3
  2. package/bin/wrangler.js +30 -10
  3. package/miniflare-dist/index.mjs +126 -74
  4. package/package.json +27 -19
  5. package/src/__tests__/api-dev.test.ts +65 -36
  6. package/src/__tests__/api-devregistry.test.ts +121 -0
  7. package/src/__tests__/api.test.ts +27 -5
  8. package/src/__tests__/configuration.test.ts +567 -86
  9. package/src/__tests__/d1/d1.test.ts +82 -0
  10. package/src/__tests__/d1/execute.test.ts +66 -0
  11. package/src/__tests__/d1/migrate.test.ts +157 -0
  12. package/src/__tests__/d1/splitter.test.ts +255 -0
  13. package/src/__tests__/delete.test.ts +91 -49
  14. package/src/__tests__/deployments.test.ts +330 -56
  15. package/src/__tests__/dev.test.tsx +198 -90
  16. package/src/__tests__/generate.test.ts +12 -10
  17. package/src/__tests__/guess-worker-format.test.ts +35 -0
  18. package/src/__tests__/helpers/end-event-loop.ts +6 -0
  19. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  20. package/src/__tests__/helpers/mock-console.ts +6 -0
  21. package/src/__tests__/helpers/mock-dialogs.ts +71 -85
  22. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +25 -0
  23. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -5
  24. package/src/__tests__/helpers/mock-known-routes.ts +7 -2
  25. package/src/__tests__/helpers/mock-kv.ts +29 -16
  26. package/src/__tests__/helpers/mock-oauth-flow.ts +103 -92
  27. package/src/__tests__/helpers/mock-process.ts +8 -13
  28. package/src/__tests__/helpers/mock-set-timeout.ts +16 -0
  29. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  30. package/src/__tests__/helpers/msw/handlers/deployments.ts +128 -27
  31. package/src/__tests__/helpers/msw/handlers/namespaces.ts +18 -41
  32. package/src/__tests__/helpers/msw/handlers/oauth.ts +13 -18
  33. package/src/__tests__/helpers/msw/handlers/r2.ts +14 -34
  34. package/src/__tests__/helpers/msw/handlers/script.ts +9 -28
  35. package/src/__tests__/helpers/msw/handlers/user.ts +13 -24
  36. package/src/__tests__/helpers/msw/handlers/zones.ts +6 -8
  37. package/src/__tests__/helpers/msw/index.ts +32 -1
  38. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  39. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +28 -0
  40. package/src/__tests__/helpers/worker-scripts/parent-worker.js +4 -1
  41. package/src/__tests__/index.test.ts +90 -72
  42. package/src/__tests__/init.test.ts +927 -631
  43. package/src/__tests__/jest.setup.ts +19 -42
  44. package/src/__tests__/kv.test.ts +355 -231
  45. package/src/__tests__/logger.test.ts +68 -2
  46. package/src/__tests__/logout.test.ts +47 -0
  47. package/src/__tests__/metrics.test.ts +94 -51
  48. package/src/__tests__/middleware.scheduled.test.ts +42 -32
  49. package/src/__tests__/middleware.test.ts +212 -122
  50. package/src/__tests__/mtls-certificates.test.ts +588 -0
  51. package/src/__tests__/pages/deployment-list.test.ts +80 -0
  52. package/src/__tests__/pages/functions-build.test.ts +452 -0
  53. package/src/__tests__/pages/pages.test.ts +81 -0
  54. package/src/__tests__/pages/project-create.test.ts +63 -0
  55. package/src/__tests__/pages/project-list.test.ts +110 -0
  56. package/src/__tests__/pages/project-upload.test.ts +500 -0
  57. package/src/__tests__/pages/publish.test.ts +2864 -0
  58. package/src/__tests__/pages-deployment-tail.test.ts +955 -0
  59. package/src/__tests__/parse.test.ts +106 -0
  60. package/src/__tests__/publish.test.ts +1615 -852
  61. package/src/__tests__/pubsub.test.ts +226 -100
  62. package/src/__tests__/queues.test.ts +238 -77
  63. package/src/__tests__/r2.test.ts +55 -57
  64. package/src/__tests__/secret.test.ts +179 -130
  65. package/src/__tests__/tail.test.ts +267 -109
  66. package/src/__tests__/test-old-node-version.js +3 -3
  67. package/src/__tests__/traverse-module-graph.test.ts +220 -0
  68. package/src/__tests__/tsconfig-sanity.ts +12 -0
  69. package/src/__tests__/tsconfig.json +8 -0
  70. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  71. package/src/__tests__/type-generation.test.ts +43 -26
  72. package/src/__tests__/user.test.ts +35 -72
  73. package/src/__tests__/whoami.test.tsx +8 -114
  74. package/src/__tests__/worker-namespace.test.ts +83 -69
  75. package/src/api/dev.ts +168 -60
  76. package/src/api/index.ts +10 -1
  77. package/src/api/mtls-certificate.ts +148 -0
  78. package/src/api/pages/create-worker-bundle-contents.ts +76 -0
  79. package/src/api/pages/index.ts +5 -0
  80. package/src/api/pages/publish.tsx +355 -0
  81. package/src/bundle.ts +263 -140
  82. package/src/cfetch/index.ts +0 -2
  83. package/src/cfetch/internal.ts +16 -21
  84. package/src/cli.ts +4 -4
  85. package/src/config/config.ts +14 -7
  86. package/src/config/environment.ts +79 -24
  87. package/src/config/index.ts +99 -38
  88. package/src/config/validation-helpers.ts +1 -1
  89. package/src/config/validation.ts +248 -19
  90. package/src/create-worker-preview.ts +19 -8
  91. package/src/create-worker-upload-form.ts +60 -22
  92. package/src/d1/backups.tsx +43 -42
  93. package/src/d1/constants.ts +2 -0
  94. package/src/d1/create.tsx +44 -30
  95. package/src/d1/{delete.tsx → delete.ts} +11 -14
  96. package/src/d1/execute.tsx +209 -151
  97. package/src/d1/index.ts +31 -7
  98. package/src/d1/list.tsx +22 -10
  99. package/src/d1/migrations/apply.tsx +212 -0
  100. package/src/d1/migrations/create.tsx +79 -0
  101. package/src/d1/migrations/helpers.ts +172 -0
  102. package/src/d1/migrations/index.ts +3 -0
  103. package/src/d1/migrations/list.tsx +95 -0
  104. package/src/d1/migrations/options.ts +23 -0
  105. package/src/d1/options.ts +12 -2
  106. package/src/d1/splitter.ts +161 -0
  107. package/src/d1/{types.tsx → types.ts} +11 -0
  108. package/src/d1/utils.ts +12 -2
  109. package/src/delete.ts +8 -16
  110. package/src/deployments.ts +340 -46
  111. package/src/deprecated/index.ts +8 -302
  112. package/src/dev/dev.tsx +57 -44
  113. package/src/dev/local.tsx +294 -91
  114. package/src/dev/remote.tsx +32 -3
  115. package/src/dev/start-server.ts +89 -68
  116. package/src/dev/use-esbuild.ts +17 -18
  117. package/src/dev/validate-dev-props.ts +9 -0
  118. package/src/{dev-registry.tsx → dev-registry.ts} +2 -1
  119. package/src/dev.tsx +178 -118
  120. package/src/dialogs.ts +136 -0
  121. package/src/dispatch-namespace.ts +21 -17
  122. package/src/docs/index.ts +97 -0
  123. package/src/entry.ts +19 -11
  124. package/src/environment-variables/factory.ts +88 -0
  125. package/src/environment-variables/misc-variables.ts +30 -0
  126. package/src/generate/index.ts +298 -0
  127. package/src/git-client.ts +15 -4
  128. package/src/{index.tsx → index.ts} +137 -63
  129. package/src/init.ts +249 -187
  130. package/src/inspect.ts +16 -1
  131. package/src/jest.d.ts +4 -0
  132. package/src/kv/index.ts +15 -17
  133. package/src/logger.ts +32 -5
  134. package/src/metrics/metrics-config.ts +3 -3
  135. package/src/metrics/send-event.ts +6 -1
  136. package/src/miniflare-cli/assets.ts +64 -28
  137. package/src/miniflare-cli/index.ts +4 -7
  138. package/src/miniflare-cli/tsconfig.json +9 -0
  139. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  140. package/src/miniflare-cli/types.ts +11 -0
  141. package/src/module-collection.ts +91 -25
  142. package/src/mtls-certificate/cli.ts +155 -0
  143. package/src/pages/build.ts +410 -0
  144. package/src/pages/buildFunctions.ts +140 -0
  145. package/src/pages/deployment-tails.ts +281 -0
  146. package/src/pages/deployments.tsx +11 -33
  147. package/src/pages/{dev.tsx → dev.ts} +147 -124
  148. package/src/pages/errors.ts +1 -0
  149. package/src/pages/functions/buildPlugin.ts +72 -69
  150. package/src/pages/functions/buildWorker.ts +249 -114
  151. package/src/pages/functions/tsconfig.json +8 -0
  152. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  153. package/src/pages/{functions.tsx → functions.ts} +8 -4
  154. package/src/pages/{index.tsx → index.ts} +11 -3
  155. package/src/pages/projects.tsx +12 -22
  156. package/src/pages/prompt-select-project.tsx +31 -0
  157. package/src/pages/publish.tsx +45 -214
  158. package/src/pages/types.ts +6 -9
  159. package/src/pages/upload.tsx +58 -14
  160. package/src/pages/utils.ts +14 -3
  161. package/src/parse.ts +23 -1
  162. package/src/paths.ts +1 -1
  163. package/src/proxy.ts +5 -0
  164. package/src/publish/index.ts +20 -16
  165. package/src/publish/publish.ts +72 -47
  166. package/src/pubsub/{pubsub-commands.tsx → pubsub-commands.ts} +19 -20
  167. package/src/queues/cli/commands/consumer/add.ts +24 -24
  168. package/src/queues/cli/commands/consumer/index.ts +3 -6
  169. package/src/queues/cli/commands/consumer/remove.ts +11 -18
  170. package/src/queues/cli/commands/create.ts +8 -8
  171. package/src/queues/cli/commands/delete.ts +8 -8
  172. package/src/queues/cli/commands/index.ts +6 -4
  173. package/src/queues/cli/commands/list.ts +8 -8
  174. package/src/queues/client.ts +1 -0
  175. package/src/queues/utils.ts +18 -0
  176. package/src/r2/constants.ts +4 -0
  177. package/src/r2/helpers.ts +1 -0
  178. package/src/r2/index.ts +40 -28
  179. package/src/secret/index.ts +14 -17
  180. package/src/{sites.tsx → sites.ts} +1 -1
  181. package/src/tail/createTail.ts +94 -3
  182. package/src/tail/filters.ts +1 -1
  183. package/src/tail/index.ts +8 -11
  184. package/src/tail/printing.ts +15 -0
  185. package/src/traverse-module-graph.ts +53 -0
  186. package/src/tsconfig-sanity.ts +16 -0
  187. package/src/type-generation.ts +79 -57
  188. package/src/user/access.ts +0 -1
  189. package/src/user/auth-variables.ts +113 -0
  190. package/src/user/choose-account.tsx +1 -31
  191. package/src/user/index.ts +0 -1
  192. package/src/user/{user.tsx → user.ts} +123 -82
  193. package/src/utils/render.ts +93 -0
  194. package/src/{whoami.tsx → whoami.ts} +37 -71
  195. package/src/worker.ts +35 -3
  196. package/src/yargs-types.ts +22 -5
  197. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  198. package/templates/__tests__/tsconfig.json +8 -0
  199. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  200. package/templates/checked-fetch.js +9 -3
  201. package/templates/d1-beta-facade.js +95 -25
  202. package/templates/facade.d.ts +14 -0
  203. package/templates/first-party-worker-module-facade.ts +4 -3
  204. package/templates/format-dev-errors.ts +8 -6
  205. package/templates/gitignore +1 -0
  206. package/templates/init-tests/test-jest-new-worker.js +3 -5
  207. package/templates/init-tests/test-vitest-new-worker.js +3 -5
  208. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  209. package/templates/middleware/loader-modules.ts +2 -2
  210. package/templates/middleware/loader-sw.ts +51 -31
  211. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  212. package/templates/new-worker.ts +3 -0
  213. package/templates/pages-dev-pipeline.ts +4 -1
  214. package/templates/pages-shim.ts +4 -1
  215. package/templates/pages-template-plugin.ts +19 -10
  216. package/templates/serve-static-assets.ts +17 -14
  217. package/templates/service-bindings-module-facade.js +1 -0
  218. package/templates/tsconfig-sanity.ts +11 -0
  219. package/templates/tsconfig.init.json +105 -0
  220. package/templates/tsconfig.json +5 -103
  221. package/templates/tsconfig.tsbuildinfo +1 -0
  222. package/wrangler-dist/cli.d.ts +826 -77
  223. package/wrangler-dist/cli.js +130734 -140838
  224. package/wrangler-dist/wasm-sync.wasm +0 -0
  225. package/src/__tests__/api-devregistry.test.js +0 -57
  226. package/src/__tests__/d1.test.ts +0 -72
  227. package/src/__tests__/dialogs.test.tsx +0 -40
  228. package/src/__tests__/helpers/mock-cfetch.ts +0 -333
  229. package/src/__tests__/pages.test.ts +0 -2223
  230. package/src/dialogs.tsx +0 -163
  231. package/src/environment-variables.ts +0 -50
  232. package/src/pages/build.tsx +0 -238
  233. package/src/user/env-vars.ts +0 -46
  234. package/templates/experimental-local-cache-stubs.js +0 -27
  235. /package/src/{bundle-reporter.tsx → bundle-reporter.ts} +0 -0
  236. /package/src/dev/{get-local-persistence-path.tsx → get-local-persistence-path.ts} +0 -0
  237. /package/src/pages/{hash.tsx → hash.ts} +0 -0
package/src/init.ts CHANGED
@@ -19,13 +19,18 @@ import { CommandLineArgsError, printWranglerBanner } from "./index";
19
19
 
20
20
  import type { RawConfig } from "./config";
21
21
  import type { Route, SimpleRoute } from "./config/environment";
22
- import type { WorkerMetadata } from "./create-worker-upload-form";
23
- import type { ConfigPath } from "./index";
22
+ import type {
23
+ WorkerMetadata,
24
+ WorkerMetadataBinding,
25
+ } from "./create-worker-upload-form";
24
26
  import type { PackageManager } from "./package-manager";
25
- import type { CommonYargsOptions } from "./yargs-types";
26
- import type { Argv, ArgumentsCamelCase } from "yargs";
27
+ import type { PackageJSON } from "./parse";
28
+ import type {
29
+ CommonYargsArgv,
30
+ StrictYargsOptionsToInterface,
31
+ } from "./yargs-types";
27
32
 
28
- export async function initOptions(yargs: Argv<CommonYargsOptions>) {
33
+ export function initOptions(yargs: CommonYargsArgv) {
29
34
  return yargs
30
35
  .positional("name", {
31
36
  describe: "The name of your worker",
@@ -49,18 +54,14 @@ export async function initOptions(yargs: Argv<CommonYargsOptions>) {
49
54
  alias: "y",
50
55
  })
51
56
  .option("from-dash", {
52
- describe: "Download script from the dashboard for local development",
57
+ describe:
58
+ "The name of the Worker you wish to download from the Cloudflare dashboard for local development.",
53
59
  type: "string",
54
60
  requiresArg: true,
55
61
  });
56
62
  }
57
63
 
58
- interface InitArgs {
59
- name: string;
60
- type?: string;
61
- site?: boolean;
62
- yes?: boolean;
63
- }
64
+ type InitArgs = StrictYargsOptionsToInterface<typeof initOptions>;
64
65
 
65
66
  export type ServiceMetadataRes = {
66
67
  id: string;
@@ -109,7 +110,7 @@ export type CronTriggersRes = {
109
110
  ];
110
111
  };
111
112
 
112
- export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
113
+ export async function initHandler(args: InitArgs) {
113
114
  await printWranglerBanner();
114
115
  if (args.type) {
115
116
  let message = "The --type option is no longer supported.";
@@ -124,7 +125,7 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
124
125
  const devDepsToInstall: string[] = [];
125
126
  const instructions: string[] = [];
126
127
  let shouldRunPackageManagerInstall = false;
127
- const fromDashScriptName = args["from-dash"] as string;
128
+ const fromDashScriptName = args.fromDash;
128
129
  const creationDirectory = path.resolve(
129
130
  process.cwd(),
130
131
  (args.name ? args.name : fromDashScriptName) ?? ""
@@ -165,6 +166,27 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
165
166
  );
166
167
  let justCreatedWranglerToml = false;
167
168
 
169
+ let accountId = "";
170
+ let serviceMetadata: undefined | ServiceMetadataRes;
171
+
172
+ // If --from-dash, check that script actually exists
173
+ if (fromDashScriptName) {
174
+ const config = readConfig(args.config, args);
175
+ accountId = await requireAuth(config);
176
+ try {
177
+ serviceMetadata = await fetchResult<ServiceMetadataRes>(
178
+ `/accounts/${accountId}/workers/services/${fromDashScriptName}`
179
+ );
180
+ } catch (err) {
181
+ if ((err as { code?: number }).code === 10090) {
182
+ throw new Error(
183
+ "wrangler couldn't find a Worker script with that name in your account.\nRun `wrangler whoami` to confirm you're logged into the correct account."
184
+ );
185
+ }
186
+ throw err;
187
+ }
188
+ }
189
+
168
190
  if (fs.existsSync(wranglerTomlDestination)) {
169
191
  let shouldContinue = false;
170
192
  logger.warn(
@@ -309,7 +331,7 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
309
331
  isTypescriptProject = true;
310
332
  await writeFile(
311
333
  path.join(creationDirectory, "./tsconfig.json"),
312
- readFileSync(path.join(getBasePath(), "templates/tsconfig.json"))
334
+ readFileSync(path.join(getBasePath(), "templates/tsconfig.init.json"))
313
335
  );
314
336
  devDepsToInstall.push("@cloudflare/workers-types");
315
337
  devDepsToInstall.push("typescript");
@@ -411,7 +433,7 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
411
433
  : `wrangler publish ${scriptPath}`,
412
434
  ...(isAddingTestScripts && { test: testRunner }),
413
435
  },
414
- },
436
+ } as PackageJSON,
415
437
  null,
416
438
  2
417
439
  ) + "\n"
@@ -449,19 +471,15 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
449
471
  );
450
472
  if (fromDashScriptName) {
451
473
  logger.warn(
452
- `After running "wrangler init --from-dash", modifying your worker via the Cloudflare dashboard is discouraged.
453
- Edits made via the Dashboard will not be synchronized locally and will be overridden by your local code and config when you publish.`
474
+ "After running `wrangler init --from-dash`, modifying your worker via the Cloudflare dashboard is discouraged.\nEdits made via the Dashboard will not be synchronized locally and will be overridden by your local code and config when you publish."
454
475
  );
455
- const config = readConfig(args.config as ConfigPath, args);
456
- const accountId = await requireAuth(config);
476
+
457
477
  await mkdir(path.join(creationDirectory, "./src"), {
458
478
  recursive: true,
459
479
  });
460
- const serviceMetaData = await fetchResult<ServiceMetadataRes>(
461
- `/accounts/${accountId}/workers/services/${fromDashScriptName}`
462
- );
480
+
463
481
  const defaultEnvironment =
464
- serviceMetaData.default_environment.environment;
482
+ serviceMetadata?.default_environment.environment;
465
483
  // I want the default environment, assuming it's the most up to date code.
466
484
  const dashScript = await fetchDashboardScript(
467
485
  `/accounts/${accountId}/workers/services/${fromDashScriptName}/environments/${defaultEnvironment}/content`
@@ -479,7 +497,7 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
479
497
  scriptPath: "src/index.ts",
480
498
  extraToml: (await getWorkerConfig(accountId, fromDashScriptName, {
481
499
  defaultEnvironment,
482
- environments: serviceMetaData.environments,
500
+ environments: serviceMetadata?.environments,
483
501
  })) as TOML.JsonMap,
484
502
  });
485
503
  } else {
@@ -493,6 +511,7 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
493
511
  await mkdir(path.join(creationDirectory, "./src"), {
494
512
  recursive: true,
495
513
  });
514
+
496
515
  await writeFile(
497
516
  path.join(creationDirectory, "./src/index.ts"),
498
517
  readFileSync(path.join(getBasePath(), `templates/${template}`))
@@ -505,10 +524,43 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
505
524
  )}`
506
525
  );
507
526
 
527
+ shouldCreateTests =
528
+ yesFlag ||
529
+ (await confirm(
530
+ "Would you like us to write your first test with Vitest?"
531
+ ));
532
+
533
+ if (shouldCreateTests) {
534
+ if (yesFlag) {
535
+ logger.info("Your project will use Vitest to run your tests.");
536
+ }
537
+
538
+ newWorkerTestType = "vitest";
539
+ devDepsToInstall.push(newWorkerTestType);
540
+
541
+ await writeFile(
542
+ path.join(creationDirectory, "./src/index.test.ts"),
543
+ readFileSync(
544
+ path.join(
545
+ getBasePath(),
546
+ `templates/init-tests/test-${newWorkerTestType}-new-worker.ts`
547
+ )
548
+ )
549
+ );
550
+ logger.log(
551
+ `✨ Created ${path.relative(
552
+ process.cwd(),
553
+ path.join(creationDirectory, "./src/index.test.ts")
554
+ )}`
555
+ );
556
+ }
557
+
508
558
  await writePackageJsonScriptsAndUpdateWranglerToml({
509
559
  isWritingScripts: shouldWritePackageJsonScripts,
560
+ isAddingTests: shouldCreateTests,
510
561
  isCreatingWranglerToml: justCreatedWranglerToml,
511
562
  packagePath: pathToPackageJson,
563
+ testRunner: newWorkerTestType,
512
564
  scriptPath: "src/index.ts",
513
565
  extraToml: getNewWorkerToml(newWorkerType),
514
566
  });
@@ -524,20 +576,15 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
524
576
 
525
577
  if (fromDashScriptName) {
526
578
  logger.warn(
527
- `After running "wrangler init --from-dash", modifying your worker via the Cloudflare dashboard is discouraged.
528
- Edits made via the Dashboard will not be synchronized locally and will be overridden by your local code and config when you publish.`
579
+ "After running `wrangler init --from-dash`, modifying your worker via the Cloudflare dashboard is discouraged.\nEdits made via the Dashboard will not be synchronized locally and will be overridden by your local code and config when you publish."
529
580
  );
530
- const config = readConfig(args.config as ConfigPath, args);
531
- const accountId = await requireAuth(config);
581
+
532
582
  await mkdir(path.join(creationDirectory, "./src"), {
533
583
  recursive: true,
534
584
  });
535
585
 
536
- const serviceMetaData = await fetchResult<ServiceMetadataRes>(
537
- `/accounts/${accountId}/workers/services/${fromDashScriptName}`
538
- );
539
586
  const defaultEnvironment =
540
- serviceMetaData.default_environment.environment;
587
+ serviceMetadata?.default_environment.environment;
541
588
 
542
589
  // I want the default environment, assuming it's the most up to date code.
543
590
  const dashScript = await fetchDashboardScript(
@@ -553,11 +600,11 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
553
600
  isWritingScripts: shouldWritePackageJsonScripts,
554
601
  isCreatingWranglerToml: justCreatedWranglerToml,
555
602
  packagePath: pathToPackageJson,
556
- scriptPath: "src/index.ts",
603
+ scriptPath: "src/index.js",
557
604
  //? Should we have Environment argument for `wrangler init --from-dash` - Jacob
558
605
  extraToml: (await getWorkerConfig(accountId, fromDashScriptName, {
559
606
  defaultEnvironment,
560
- environments: serviceMetaData.environments,
607
+ environments: serviceMetadata?.environments,
561
608
  })) as TOML.JsonMap,
562
609
  });
563
610
  } else {
@@ -588,7 +635,7 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
588
635
  (await confirm("Would you like us to write your first test?"));
589
636
 
590
637
  if (shouldCreateTests) {
591
- newWorkerTestType = await getNewWorkerTestType();
638
+ newWorkerTestType = await getNewWorkerTestType(yesFlag);
592
639
  devDepsToInstall.push(newWorkerTestType);
593
640
  await writeFile(
594
641
  path.join(creationDirectory, "./src/index.test.js"),
@@ -678,41 +725,41 @@ async function installPackages(
678
725
  }
679
726
 
680
727
  async function getNewWorkerType(newWorkerFilename: string) {
681
- return select(
682
- `Would you like to create a Worker at ${newWorkerFilename}?`,
683
- [
728
+ return select(`Would you like to create a Worker at ${newWorkerFilename}?`, {
729
+ choices: [
684
730
  {
685
731
  value: "none",
686
- label: "None",
732
+ title: "None",
687
733
  },
688
734
  {
689
735
  value: "fetch",
690
- label: "Fetch handler",
736
+ title: "Fetch handler",
691
737
  },
692
738
  {
693
739
  value: "scheduled",
694
- label: "Scheduled handler",
740
+ title: "Scheduled handler",
695
741
  },
696
742
  ],
697
- 1
698
- ) as Promise<"none" | "fetch" | "scheduled">;
743
+ defaultOption: 1,
744
+ });
699
745
  }
700
746
 
701
- async function getNewWorkerTestType() {
702
- return select(
703
- `Which test runner would you like to use?`,
704
- [
705
- {
706
- value: "vitest",
707
- label: "Vitest",
708
- },
709
- {
710
- value: "jest",
711
- label: "Jest",
712
- },
713
- ],
714
- 1
715
- ) as Promise<"jest" | "vitest">;
747
+ async function getNewWorkerTestType(yesFlag?: boolean) {
748
+ return yesFlag
749
+ ? "jest"
750
+ : select(`Which test runner would you like to use?`, {
751
+ choices: [
752
+ {
753
+ value: "vitest",
754
+ title: "Vitest",
755
+ },
756
+ {
757
+ value: "jest",
758
+ title: "Jest",
759
+ },
760
+ ],
761
+ defaultOption: 1,
762
+ });
716
763
  }
717
764
 
718
765
  function getNewWorkerTemplate(
@@ -770,8 +817,8 @@ async function getWorkerConfig(
770
817
  defaultEnvironment,
771
818
  environments,
772
819
  }: {
773
- defaultEnvironment: string;
774
- environments: ServiceMetadataRes["environments"];
820
+ defaultEnvironment: string | undefined;
821
+ environments: ServiceMetadataRes["environments"] | undefined;
775
822
  }
776
823
  ): Promise<RawConfig> {
777
824
  const [bindings, routes, serviceEnvMetadata, cronTriggers] =
@@ -794,131 +841,7 @@ async function getWorkerConfig(
794
841
  );
795
842
  });
796
843
 
797
- const mappedBindings = bindings
798
- .filter((binding) => (binding.type as string) !== "secret_text")
799
- // Combine the same types into {[type]: [binding]}
800
- .reduce((configObj, binding) => {
801
- // Some types have different names in wrangler.toml
802
- // I want the type safety of the binding being destructured after the case narrowing the union but type is unused
803
-
804
- switch (binding.type) {
805
- case "plain_text":
806
- {
807
- configObj.vars = {
808
- ...(configObj.vars ?? {}),
809
- [binding.name]: binding.text,
810
- };
811
- }
812
- break;
813
- case "json":
814
- {
815
- configObj.vars = {
816
- ...(configObj.vars ?? {}),
817
- name: binding.name,
818
- json: binding.json,
819
- };
820
- }
821
- break;
822
- case "kv_namespace":
823
- {
824
- configObj.kv_namespaces = [
825
- ...(configObj.kv_namespaces ?? []),
826
- { id: binding.namespace_id, binding: binding.name },
827
- ];
828
- }
829
- break;
830
- case "durable_object_namespace":
831
- {
832
- configObj.durable_objects = {
833
- bindings: [
834
- ...(configObj.durable_objects?.bindings ?? []),
835
- {
836
- name: binding.name,
837
- class_name: binding.class_name,
838
- script_name: binding.script_name,
839
- environment: binding.environment,
840
- },
841
- ],
842
- };
843
- }
844
- break;
845
- case "r2_bucket":
846
- {
847
- configObj.r2_buckets = [
848
- ...(configObj.r2_buckets ?? []),
849
- { binding: binding.name, bucket_name: binding.bucket_name },
850
- ];
851
- }
852
- break;
853
- case "service":
854
- {
855
- configObj.services = [
856
- ...(configObj.services ?? []),
857
- {
858
- binding: binding.name,
859
- service: binding.service,
860
- environment: binding.environment,
861
- },
862
- ];
863
- }
864
- break;
865
- case "namespace":
866
- {
867
- configObj.dispatch_namespaces = [
868
- ...(configObj.dispatch_namespaces ?? []),
869
- { binding: binding.name, namespace: binding.namespace },
870
- ];
871
- }
872
- break;
873
- case "logfwdr":
874
- {
875
- configObj.logfwdr = {
876
- // TODO: Messaging about adding schema file path
877
- schema: "",
878
- bindings: [
879
- ...(configObj.logfwdr?.bindings ?? []),
880
- { name: binding.name, destination: binding.destination },
881
- ],
882
- };
883
- }
884
- break;
885
- case "wasm_module":
886
- {
887
- configObj.wasm_modules = {
888
- ...(configObj.wasm_modules ?? {}),
889
- [binding.name]: binding.part,
890
- };
891
- }
892
- break;
893
- case "text_blob":
894
- {
895
- configObj.text_blobs = {
896
- ...(configObj.text_blobs ?? {}),
897
- [binding.name]: binding.part,
898
- };
899
- }
900
- break;
901
- case "data_blob":
902
- {
903
- configObj.data_blobs = {
904
- ...(configObj.data_blobs ?? {}),
905
- [binding.name]: binding.part,
906
- };
907
- }
908
- break;
909
- default: {
910
- // If we don't know what the type is, its an unsafe binding
911
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
912
- if (!(binding as any)?.type) break;
913
- configObj.unsafe = {
914
- bindings: [...(configObj.unsafe?.bindings ?? []), binding],
915
- };
916
- }
917
- }
918
-
919
- return configObj;
920
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
921
- }, {} as RawConfig);
844
+ const mappedBindings = mapBindings(bindings);
922
845
 
923
846
  const durableObjectClassNames = bindings
924
847
  .filter((binding) => binding.type === "durable_object_namespace")
@@ -959,7 +882,7 @@ async function getWorkerConfig(
959
882
  crons: cronTriggers.schedules.map((scheduled) => scheduled.cron),
960
883
  },
961
884
  env: environments
962
- .filter((env) => env.environment !== "production")
885
+ ?.filter((env) => env.environment !== "production")
963
886
  // `env` can have multiple Environments, with different configs.
964
887
  .reduce((envObj, { environment }) => {
965
888
  return { ...envObj, [environment]: {} };
@@ -968,3 +891,142 @@ async function getWorkerConfig(
968
891
  ...mappedBindings,
969
892
  };
970
893
  }
894
+
895
+ export function mapBindings(bindings: WorkerMetadataBinding[]): RawConfig {
896
+ return (
897
+ bindings
898
+ .filter((binding) => (binding.type as string) !== "secret_text")
899
+ // Combine the same types into {[type]: [binding]}
900
+ .reduce((configObj, binding) => {
901
+ // Some types have different names in wrangler.toml
902
+ // I want the type safety of the binding being destructured after the case narrowing the union but type is unused
903
+
904
+ switch (binding.type) {
905
+ case "plain_text":
906
+ {
907
+ configObj.vars = {
908
+ ...(configObj.vars ?? {}),
909
+ [binding.name]: binding.text,
910
+ };
911
+ }
912
+ break;
913
+ case "json":
914
+ {
915
+ configObj.vars = {
916
+ ...(configObj.vars ?? {}),
917
+ name: binding.name,
918
+ json: binding.json,
919
+ };
920
+ }
921
+ break;
922
+ case "kv_namespace":
923
+ {
924
+ configObj.kv_namespaces = [
925
+ ...(configObj.kv_namespaces ?? []),
926
+ { id: binding.namespace_id, binding: binding.name },
927
+ ];
928
+ }
929
+ break;
930
+ case "durable_object_namespace":
931
+ {
932
+ configObj.durable_objects = {
933
+ bindings: [
934
+ ...(configObj.durable_objects?.bindings ?? []),
935
+ {
936
+ name: binding.name,
937
+ class_name: binding.class_name,
938
+ script_name: binding.script_name,
939
+ environment: binding.environment,
940
+ },
941
+ ],
942
+ };
943
+ }
944
+ break;
945
+ case "r2_bucket":
946
+ {
947
+ configObj.r2_buckets = [
948
+ ...(configObj.r2_buckets ?? []),
949
+ { binding: binding.name, bucket_name: binding.bucket_name },
950
+ ];
951
+ }
952
+ break;
953
+ case "service":
954
+ {
955
+ configObj.services = [
956
+ ...(configObj.services ?? []),
957
+ {
958
+ binding: binding.name,
959
+ service: binding.service,
960
+ environment: binding.environment,
961
+ },
962
+ ];
963
+ }
964
+ break;
965
+ case "analytics_engine":
966
+ {
967
+ configObj.analytics_engine_datasets = [
968
+ ...(configObj.analytics_engine_datasets ?? []),
969
+ { binding: binding.name, dataset: binding.dataset },
970
+ ];
971
+ }
972
+ break;
973
+ case "dispatch_namespace":
974
+ {
975
+ configObj.dispatch_namespaces = [
976
+ ...(configObj.dispatch_namespaces ?? []),
977
+ { binding: binding.name, namespace: binding.namespace },
978
+ ];
979
+ }
980
+ break;
981
+ case "logfwdr":
982
+ {
983
+ configObj.logfwdr = {
984
+ // TODO: Messaging about adding schema file path
985
+ schema: "",
986
+ bindings: [
987
+ ...(configObj.logfwdr?.bindings ?? []),
988
+ { name: binding.name, destination: binding.destination },
989
+ ],
990
+ };
991
+ }
992
+ break;
993
+ case "wasm_module":
994
+ {
995
+ configObj.wasm_modules = {
996
+ ...(configObj.wasm_modules ?? {}),
997
+ [binding.name]: binding.part,
998
+ };
999
+ }
1000
+ break;
1001
+ case "text_blob":
1002
+ {
1003
+ configObj.text_blobs = {
1004
+ ...(configObj.text_blobs ?? {}),
1005
+ [binding.name]: binding.part,
1006
+ };
1007
+ }
1008
+ break;
1009
+ case "data_blob":
1010
+ {
1011
+ configObj.data_blobs = {
1012
+ ...(configObj.data_blobs ?? {}),
1013
+ [binding.name]: binding.part,
1014
+ };
1015
+ }
1016
+ break;
1017
+ default: {
1018
+ // If we don't know what the type is, its an unsafe binding
1019
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1020
+ if (!(binding as any)?.type) break;
1021
+ configObj.unsafe = {
1022
+ bindings: [...(configObj.unsafe?.bindings ?? []), binding],
1023
+ metadata: configObj.unsafe?.metadata ?? undefined,
1024
+ };
1025
+ }
1026
+ }
1027
+
1028
+ return configObj;
1029
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1030
+ }, {} as RawConfig)
1031
+ );
1032
+ }
package/src/inspect.ts CHANGED
@@ -151,6 +151,18 @@ export default function useInspector(props: InspectorProps) {
151
151
  );
152
152
  ws.close(1013, "Too many clients; only one can be connected at a time");
153
153
  } else {
154
+ // Since Wrangler proxies the inspector, reloading Chrome DevTools won't trigger debugger initialisation events (because it's connecting to an extant session).
155
+ // This sends a `Debugger.disable` message to the remote when a new WebSocket connection is initialised,
156
+ // with the assumption that the new connection will shortly send a `Debugger.enable` event and trigger re-initialisation.
157
+ // The key initialisation messages that are needed are the `Debugger.scriptParsed events`.
158
+ remoteWebSocket?.send(
159
+ JSON.stringify({
160
+ // This number is arbitrary, and is chosen to be high so as not to conflict with messages that DevTools might actually send.
161
+ // For completeness, these options don't work: 0, -1, or Number.MAX_SAFE_INTEGER
162
+ id: 100_000_000,
163
+ method: "Debugger.disable",
164
+ })
165
+ );
154
166
  // As promised, save the created websocket in a state hook
155
167
  setLocalWebSocket(ws);
156
168
 
@@ -733,7 +745,7 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
733
745
  * Opens the chrome debugger
734
746
  */
735
747
  export const openInspector = async (inspectorPort: number) => {
736
- const url = `https://built-devtools.pages.dev/js_app?experiments=true&v8only=true&ws=localhost:${inspectorPort}/ws`;
748
+ const url = `https://devtools.devprod.cloudflare.dev/js_app?theme=systemPreferred&ws=localhost:${inspectorPort}/ws`;
737
749
  const errorMessage =
738
750
  "Failed to open inspector.\nInspector depends on having a Chromium-based browser installed, maybe you need to install one?";
739
751
 
@@ -759,6 +771,9 @@ export const openInspector = async (inspectorPort: number) => {
759
771
  {
760
772
  name: open.apps.edge,
761
773
  },
774
+ {
775
+ name: open.apps.firefox,
776
+ },
762
777
  ],
763
778
  });
764
779
  childProcess.on("error", () => {
package/src/jest.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ declare global {
2
+ const jest: unknown;
3
+ }
4
+ export {};