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/dev.tsx CHANGED
@@ -1,4 +1,5 @@
1
1
  import path from "node:path";
2
+ import chalk from "chalk";
2
3
  import { watch } from "chokidar";
3
4
  import getPort from "get-port";
4
5
  import { render } from "ink";
@@ -13,12 +14,11 @@ import { getEntry } from "./entry";
13
14
  import { logger } from "./logger";
14
15
  import * as metrics from "./metrics";
15
16
  import { getAssetPaths, getSiteAssetPaths } from "./sites";
16
- import { getAccountFromCache } from "./user";
17
+ import { getAccountFromCache, loginOrRefreshIfRequired } from "./user";
17
18
  import { collectKeyValues } from "./utils/collectKeyValues";
18
19
  import { identifyD1BindingsAsBeta } from "./worker";
19
20
  import { getHostFromRoute, getZoneForRoute, getZoneIdFromHost } from "./zones";
20
21
  import {
21
- type ConfigPath,
22
22
  DEFAULT_INSPECTOR_PORT,
23
23
  DEFAULT_LOCAL_PORT,
24
24
  getDevCompatibilityDate,
@@ -29,58 +29,15 @@ import {
29
29
  } from "./index";
30
30
  import type { Config, Environment } from "./config";
31
31
  import type { Route } from "./config/environment";
32
- import type { EnablePagesAssetsServiceBindingOptions } from "./miniflare-cli";
32
+ import type { LoggerLevel } from "./logger";
33
+ import type { EnablePagesAssetsServiceBindingOptions } from "./miniflare-cli/types";
33
34
  import type { CfWorkerInit } from "./worker";
34
- import type { CommonYargsOptions } from "./yargs-types";
35
- import type { Argv, ArgumentsCamelCase } from "yargs";
36
-
37
- interface DevArgs {
38
- config?: string;
39
- script?: string;
40
- name?: string;
41
- bundle?: boolean;
42
- build?: boolean;
43
- format?: string;
44
- env?: string;
45
- "compatibility-date"?: string;
46
- "compatibility-flags"?: string[];
47
- latest?: boolean;
48
- ip?: string;
49
- inspect?: boolean;
50
- port?: number;
51
- "inspector-port"?: number;
52
- routes?: string[];
53
- host?: string;
54
- "local-protocol"?: "http" | "https";
55
- "local-upstream"?: string | undefined;
56
- "experimental-public"?: string;
57
- public?: string;
58
- assets?: string;
59
- site?: string;
60
- "site-include"?: string[];
61
- "site-exclude"?: string[];
62
- "upstream-protocol"?: "http" | "https";
63
- "jsx-factory"?: string;
64
- "jsx-fragment"?: string;
65
- tsconfig?: string;
66
- local?: boolean;
67
- "experimental-local"?: boolean;
68
- minify?: boolean;
69
- var?: string[];
70
- define?: string[];
71
- "node-compat"?: boolean;
72
- "experimental-enable-local-persistence"?: boolean;
73
- persist?: boolean;
74
- "persist-to"?: string;
75
- "live-reload"?: boolean;
76
- onReady?: (ip: string, port: number) => void;
77
- logLevel?: "none" | "info" | "error" | "log" | "warn" | "debug";
78
- logPrefix?: string;
79
- showInteractiveDevSession?: boolean;
80
- "test-scheduled"?: boolean;
81
- }
35
+ import type {
36
+ CommonYargsArgv,
37
+ StrictYargsOptionsToInterface,
38
+ } from "./yargs-types";
82
39
 
83
- export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
40
+ export function devOptions(yargs: CommonYargsArgv) {
84
41
  return (
85
42
  yargs
86
43
  .positional("script", {
@@ -175,6 +132,13 @@ export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
175
132
  type: "string",
176
133
  requiresArg: true,
177
134
  })
135
+ .option("public", {
136
+ describe: "(Deprecated) Static assets to be served",
137
+ type: "string",
138
+ requiresArg: true,
139
+ deprecated: true,
140
+ hidden: true,
141
+ })
178
142
  .option("site", {
179
143
  describe: "Root folder of static assets for Workers Sites",
180
144
  type: "string",
@@ -237,11 +201,25 @@ export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
237
201
  type: "boolean",
238
202
  default: false,
239
203
  })
204
+ .option("experimental-local-remote-kv", {
205
+ describe:
206
+ "Read/write KV data from/to real namespaces on the Cloudflare network",
207
+ type: "boolean",
208
+ default: false,
209
+ })
240
210
  .check((argv) => {
241
211
  if (argv.local && argv["experimental-local"]) {
242
212
  throw new Error(
243
213
  "--local and --experimental-local are mutually exclusive. " +
244
- "Please select one or the other."
214
+ "Please enable one or the other."
215
+ );
216
+ }
217
+ if (
218
+ argv["experimental-local-remote-kv"] &&
219
+ !argv["experimental-local"]
220
+ ) {
221
+ throw new Error(
222
+ "--experimental-local-remote-kv requires --experimental-local to be enabled."
245
223
  );
246
224
  }
247
225
  return true;
@@ -251,7 +229,7 @@ export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
251
229
  type: "boolean",
252
230
  })
253
231
  .option("node-compat", {
254
- describe: "Enable node.js compatibility",
232
+ describe: "Enable Node.js compatibility",
255
233
  type: "boolean",
256
234
  })
257
235
  .option("experimental-enable-local-persistence", {
@@ -273,11 +251,20 @@ export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
273
251
  requiresArg: true,
274
252
  })
275
253
  .option("live-reload", {
276
- // TODO: Add back in once we have remote `--live-reload`
277
- hidden: true,
278
- // describe: "Auto reload HTML pages when change is detected",
254
+ describe:
255
+ "Auto reload HTML pages when change is detected in local mode",
279
256
  type: "boolean",
280
257
  })
258
+ .check((argv) => {
259
+ const local = argv["local"] || argv["experimental-local"];
260
+ if (argv["live-reload"] && !local) {
261
+ throw new Error(
262
+ "--live-reload is only supported in local mode. " +
263
+ "Please enable either --local or --experimental-local."
264
+ );
265
+ }
266
+ return true;
267
+ })
281
268
  .option("inspect", {
282
269
  describe: "Enable dev tools",
283
270
  type: "boolean",
@@ -297,12 +284,24 @@ export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
297
284
  .option("log-level", {
298
285
  choices: ["debug", "info", "log", "warn", "error", "none"] as const,
299
286
  describe: "Specify logging level",
300
- default: "log",
287
+ // Yargs requires this to type log-level properly
288
+ default: "log" as LoggerLevel,
301
289
  })
302
290
  );
303
291
  }
304
292
 
305
- export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
293
+ type DevArguments = StrictYargsOptionsToInterface<typeof devOptions>;
294
+
295
+ export async function devHandler(args: DevArguments) {
296
+ if (!(args.local || args.experimentalLocal)) {
297
+ const isLoggedIn = await loginOrRefreshIfRequired();
298
+ if (!isLoggedIn) {
299
+ throw new Error(
300
+ "You must be logged in to use wrangler dev in remote mode. Try logging in, or run wrangler dev --local."
301
+ );
302
+ }
303
+ }
304
+
306
305
  let watcher;
307
306
  try {
308
307
  const devInstance = await startDev(args);
@@ -337,12 +336,15 @@ export type AdditionalDevProps = {
337
336
  d1Databases?: Environment["d1_databases"];
338
337
  };
339
338
 
340
- type StartDevOptions = ArgumentsCamelCase<DevArgs> &
339
+ type StartDevOptions = DevArguments &
341
340
  // These options can be passed in directly when called with the `wrangler.dev()` API.
342
341
  // They aren't exposed as CLI arguments.
343
342
  AdditionalDevProps & {
344
343
  forceLocal?: boolean;
344
+ disableDevRegistry?: boolean;
345
345
  enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
346
+ onReady?: (ip: string, port: number) => void;
347
+ showInteractiveDevSession?: boolean;
346
348
  };
347
349
 
348
350
  export async function startDev(args: StartDevOptions) {
@@ -354,10 +356,19 @@ export async function startDev(args: StartDevOptions) {
354
356
  }
355
357
  await printWranglerBanner();
356
358
 
359
+ if (args.local && process.platform !== "win32") {
360
+ logger.info(
361
+ chalk.magenta(
362
+ `Want to try out the next version of local mode using the open-source Workers runtime?\nSwitch out --local for ${chalk.bold(
363
+ "--experimental-local"
364
+ )} and let us know what you think at https://discord.gg/cloudflaredev !`
365
+ )
366
+ );
367
+ }
368
+
357
369
  const configPath =
358
- (args.config as ConfigPath) ||
359
- ((args.script &&
360
- findWranglerToml(path.dirname(args.script))) as ConfigPath);
370
+ args.config ||
371
+ (args.script && findWranglerToml(path.dirname(args.script)));
361
372
  let config = readConfig(configPath, args);
362
373
 
363
374
  if (config.configPath) {
@@ -376,7 +387,8 @@ export async function startDev(args: StartDevOptions) {
376
387
 
377
388
  const {
378
389
  entry,
379
- nodeCompat,
390
+ legacyNodeCompat,
391
+ nodejsCompat,
380
392
  upstreamProtocol,
381
393
  zoneId,
382
394
  host,
@@ -398,7 +410,7 @@ export async function startDev(args: StartDevOptions) {
398
410
 
399
411
  // eslint-disable-next-line no-inner-declarations
400
412
  async function getDevReactElement(configParam: Config) {
401
- const { assetPaths, bindings } = await getBindingsAndAssetPaths(
413
+ const { assetPaths, bindings } = getBindingsAndAssetPaths(
402
414
  args,
403
415
  configParam
404
416
  );
@@ -415,28 +427,31 @@ export async function startDev(args: StartDevOptions) {
415
427
  rules={getRules(configParam)}
416
428
  legacyEnv={isLegacyEnv(configParam)}
417
429
  minify={args.minify ?? configParam.minify}
418
- nodeCompat={nodeCompat}
430
+ legacyNodeCompat={legacyNodeCompat}
431
+ nodejsCompat={nodejsCompat}
419
432
  build={configParam.build || {}}
420
433
  define={{ ...configParam.define, ...cliDefines }}
421
434
  initialMode={
422
435
  args.local || args.experimentalLocal ? "local" : "remote"
423
436
  }
424
- jsxFactory={args["jsx-factory"] || configParam.jsx_factory}
425
- jsxFragment={args["jsx-fragment"] || configParam.jsx_fragment}
437
+ jsxFactory={args.jsxFactory || configParam.jsx_factory}
438
+ jsxFragment={args.jsxFragment || configParam.jsx_fragment}
426
439
  tsconfig={args.tsconfig ?? configParam.tsconfig}
427
440
  upstreamProtocol={upstreamProtocol}
428
441
  localProtocol={args.localProtocol || configParam.dev.local_protocol}
429
- localUpstream={args["local-upstream"] || host}
442
+ localUpstream={args.localUpstream ?? host}
430
443
  localPersistencePath={localPersistencePath}
431
444
  liveReload={args.liveReload || false}
432
445
  accountId={configParam.account_id || getAccountFromCache()?.id}
433
446
  assetPaths={assetPaths}
434
447
  assetsConfig={configParam.assets}
435
- port={args.port || configParam.dev.port || (await getLocalPort())}
436
- ip={args.ip || configParam.dev.ip}
448
+ initialPort={
449
+ args.port ?? configParam.dev.port ?? (await getLocalPort())
450
+ }
451
+ initialIp={args.ip || configParam.dev.ip}
437
452
  inspectorPort={
438
- args.inspectorPort ||
439
- configParam.dev.inspector_port ||
453
+ args.inspectorPort ??
454
+ configParam.dev.inspector_port ??
440
455
  (await getInspectorPort())
441
456
  }
442
457
  isWorkersSite={Boolean(args.site || configParam.site)}
@@ -451,7 +466,6 @@ export async function startDev(args: StartDevOptions) {
451
466
  bindings={bindings}
452
467
  crons={configParam.triggers.crons}
453
468
  queueConsumers={configParam.queues.consumers}
454
- logPrefix={args.logPrefix}
455
469
  onReady={args.onReady}
456
470
  inspect={args.inspect ?? true}
457
471
  showInteractiveDevSession={args.showInteractiveDevSession}
@@ -459,17 +473,35 @@ export async function startDev(args: StartDevOptions) {
459
473
  enablePagesAssetsServiceBinding={args.enablePagesAssetsServiceBinding}
460
474
  firstPartyWorker={configParam.first_party_worker}
461
475
  sendMetrics={configParam.send_metrics}
462
- testScheduled={args["test-scheduled"]}
476
+ testScheduled={args.testScheduled}
463
477
  experimentalLocal={args.experimentalLocal}
478
+ experimentalLocalRemoteKv={args.experimentalLocalRemoteKv}
464
479
  />
465
480
  );
466
481
  }
467
482
  const devReactElement = render(await getDevReactElement(config));
483
+
484
+ // In the bootstrapper script `bin/wrangler.js`, we open an IPC channel, so
485
+ // IPC messages from this process are propagated through the bootstrapper.
486
+ // Normally, Node's SIGINT handler would close this for us, but interactive
487
+ // mode enables raw mode on stdin which disables the built-in handler. The
488
+ // following line disconnects from the IPC channel when we press `x` or
489
+ // CTRL-C in interactive mode, ensuring no open handles, and allowing for a
490
+ // clean exit. Note, if we called `stop()` using the dev API, we don't want
491
+ // to disconnect here, as the user may still need IPC. We also don't want
492
+ // to disconnect if this file was imported in Jest (not the case with E2E
493
+ // tests), as that would stop communication with the test runner.
494
+ let apiStopped = false;
495
+ void devReactElement.waitUntilExit().then(() => {
496
+ if (!apiStopped && typeof jest === "undefined") process.disconnect?.();
497
+ });
498
+
468
499
  rerender = devReactElement.rerender;
469
500
  return {
470
501
  devReactElement,
471
502
  watcher,
472
503
  stop: async () => {
504
+ apiStopped = true;
473
505
  devReactElement.unmount();
474
506
  await watcher?.close();
475
507
  },
@@ -486,14 +518,13 @@ export async function startApiDev(args: StartDevOptions) {
486
518
  await printWranglerBanner();
487
519
 
488
520
  const configPath =
489
- (args.config as ConfigPath) ||
490
- ((args.script &&
491
- findWranglerToml(path.dirname(args.script))) as ConfigPath);
521
+ args.config || (args.script && findWranglerToml(path.dirname(args.script)));
492
522
  const config = readConfig(configPath, args);
493
523
 
494
524
  const {
495
525
  entry,
496
- nodeCompat,
526
+ legacyNodeCompat,
527
+ nodejsCompat,
497
528
  upstreamProtocol,
498
529
  zoneId,
499
530
  host,
@@ -512,7 +543,7 @@ export async function startApiDev(args: StartDevOptions) {
512
543
 
513
544
  // eslint-disable-next-line no-inner-declarations
514
545
  async function getDevServer(configParam: Config) {
515
- const { assetPaths, bindings } = await getBindingsAndAssetPaths(
546
+ const { assetPaths, bindings } = getBindingsAndAssetPaths(
516
547
  args,
517
548
  configParam
518
549
  );
@@ -532,46 +563,41 @@ export async function startApiDev(args: StartDevOptions) {
532
563
  rules: getRules(configParam),
533
564
  legacyEnv: isLegacyEnv(configParam),
534
565
  minify: args.minify ?? configParam.minify,
535
- nodeCompat: nodeCompat,
566
+ legacyNodeCompat,
567
+ nodejsCompat,
536
568
  build: configParam.build || {},
537
569
  define: { ...config.define, ...cliDefines },
538
570
  initialMode: args.local ? "local" : "remote",
539
- jsxFactory: args["jsx-factory"] || configParam.jsx_factory,
540
- jsxFragment: args["jsx-fragment"] || configParam.jsx_fragment,
571
+ jsxFactory: args.jsxFactory ?? configParam.jsx_factory,
572
+ jsxFragment: args.jsxFragment ?? configParam.jsx_fragment,
541
573
  tsconfig: args.tsconfig ?? configParam.tsconfig,
542
574
  upstreamProtocol: upstreamProtocol,
543
- localProtocol: args.localProtocol || configParam.dev.local_protocol,
544
- localUpstream: args["local-upstream"] || host,
575
+ localProtocol: args.localProtocol ?? configParam.dev.local_protocol,
576
+ localUpstream: args.localUpstream ?? host,
545
577
  localPersistencePath,
546
- liveReload: args.liveReload || false,
547
- accountId: configParam.account_id || getAccountFromCache()?.id,
578
+ liveReload: args.liveReload ?? false,
579
+ accountId: configParam.account_id ?? getAccountFromCache()?.id,
548
580
  assetPaths: assetPaths,
549
581
  assetsConfig: configParam.assets,
550
582
  //port can be 0, which means to use a random port
551
- port:
552
- args.port === 0
553
- ? args.port
554
- : args.port || configParam.dev.port || (await getLocalPort()),
555
- ip: args.ip || configParam.dev.ip,
583
+ initialPort: args.port ?? configParam.dev.port ?? (await getLocalPort()),
584
+ initialIp: args.ip ?? configParam.dev.ip,
556
585
  inspectorPort:
557
- args["inspector-port"] ||
558
- configParam.dev.inspector_port ||
586
+ args.inspectorPort ??
587
+ configParam.dev.inspector_port ??
559
588
  (await getInspectorPort()),
560
589
  isWorkersSite: Boolean(args.site || configParam.site),
561
590
  compatibilityDate: getDevCompatibilityDate(
562
591
  config,
563
592
  // Only `compatibilityDate` will be set when using `unstable_dev`
564
- args["compatibility-date"] ?? args.compatibilityDate
593
+ args.compatibilityDate
565
594
  ),
566
595
  compatibilityFlags:
567
- args["compatibility-flags"] ??
568
- args.compatibilityFlags ??
569
- configParam.compatibility_flags,
596
+ args.compatibilityFlags ?? configParam.compatibility_flags,
570
597
  usageModel: configParam.usage_model,
571
598
  bindings: bindings,
572
599
  crons: configParam.triggers.crons,
573
600
  queueConsumers: configParam.queues.consumers,
574
- logPrefix: args.logPrefix,
575
601
  onReady: args.onReady,
576
602
  inspect: args.inspect ?? true,
577
603
  showInteractiveDevSession: args.showInteractiveDevSession,
@@ -582,6 +608,8 @@ export async function startApiDev(args: StartDevOptions) {
582
608
  sendMetrics: configParam.send_metrics,
583
609
  testScheduled: args.testScheduled,
584
610
  experimentalLocal: args.experimentalLocal,
611
+ experimentalLocalRemoteKv: args.experimentalLocalRemoteKv,
612
+ disableDevRegistry: args.disableDevRegistry ?? false,
585
613
  });
586
614
  }
587
615
 
@@ -687,8 +715,7 @@ async function validateDevServerSettings(
687
715
  "Passing --inspect is unnecessary, now you can always connect to devtools."
688
716
  );
689
717
  }
690
-
691
- if (args["experimental-public"]) {
718
+ if (args.experimentalPublic) {
692
719
  throw new Error(
693
720
  "The --experimental-public field has been renamed to --assets"
694
721
  );
@@ -698,7 +725,7 @@ async function validateDevServerSettings(
698
725
  throw new Error("The --public field has been renamed to --assets");
699
726
  }
700
727
 
701
- if ((args.assets || config.assets) && (args.site || config.site)) {
728
+ if ((args.assets ?? config.assets) && (args.site ?? config.site)) {
702
729
  throw new Error("Cannot use Assets and Workers Sites in the same Worker.");
703
730
  }
704
731
 
@@ -708,18 +735,27 @@ async function validateDevServerSettings(
708
735
  );
709
736
  }
710
737
  const upstreamProtocol =
711
- args["upstream-protocol"] || config.dev.upstream_protocol;
738
+ args.upstreamProtocol ?? config.dev.upstream_protocol;
712
739
  if (upstreamProtocol === "http") {
713
740
  logger.warn(
714
741
  "Setting upstream-protocol to http is not currently implemented.\n" +
715
742
  "If this is required in your project, please add your use case to the following issue:\n" +
716
- "https://github.com/cloudflare/wrangler2/issues/583."
743
+ "https://github.com/cloudflare/workers-sdk/issues/583."
717
744
  );
718
745
  }
719
- const nodeCompat = args.nodeCompat ?? config.node_compat;
720
- if (nodeCompat) {
746
+ const legacyNodeCompat = args.nodeCompat ?? config.node_compat;
747
+ if (legacyNodeCompat) {
721
748
  logger.warn(
722
- "Enabling node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
749
+ "Enabling Node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
750
+ );
751
+ }
752
+
753
+ const compatibilityFlags =
754
+ args.compatibilityFlags ?? config.compatibility_flags;
755
+ const nodejsCompat = compatibilityFlags?.includes("nodejs_compat");
756
+ if (legacyNodeCompat && nodejsCompat) {
757
+ throw new Error(
758
+ "The `nodejs_compat` compatibility flag cannot be used in conjunction with the legacy `--node-compat` flag. If you want to use the Workers runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command or `node_compat = true` from your config file."
723
759
  );
724
760
  }
725
761
 
@@ -742,7 +778,8 @@ async function validateDevServerSettings(
742
778
  return {
743
779
  entry,
744
780
  upstreamProtocol,
745
- nodeCompat,
781
+ legacyNodeCompat,
782
+ nodejsCompat,
746
783
  getLocalPort,
747
784
  getInspectorPort,
748
785
  zoneId,
@@ -753,14 +790,11 @@ async function validateDevServerSettings(
753
790
  };
754
791
  }
755
792
 
756
- async function getBindingsAndAssetPaths(
757
- args: StartDevOptions,
758
- configParam: Config
759
- ) {
793
+ function getBindingsAndAssetPaths(args: StartDevOptions, configParam: Config) {
760
794
  const cliVars = collectKeyValues(args.var);
761
795
 
762
796
  // now log all available bindings into the terminal
763
- const bindings = await getBindings(configParam, args.env, {
797
+ const bindings = getBindings(configParam, args.env, args.local ?? false, {
764
798
  kv: args.kv,
765
799
  vars: { ...args.vars, ...cliVars },
766
800
  durableObjects: args.durableObjects,
@@ -787,11 +821,12 @@ async function getBindingsAndAssetPaths(
787
821
  return { assetPaths, bindings };
788
822
  }
789
823
 
790
- async function getBindings(
824
+ function getBindings(
791
825
  configParam: Config,
792
826
  env: string | undefined,
827
+ local: boolean,
793
828
  args: AdditionalDevProps
794
- ): Promise<CfWorkerInit["bindings"]> {
829
+ ): CfWorkerInit["bindings"] {
795
830
  const bindings = {
796
831
  kv_namespaces: [
797
832
  ...(configParam.kv_namespaces || []).map(
@@ -817,12 +852,12 @@ async function getBindings(
817
852
  ),
818
853
  ...(args.kv || []),
819
854
  ],
855
+ send_email: configParam.send_email,
820
856
  // Use a copy of combinedVars since we're modifying it later
821
857
  vars: {
822
858
  ...getVarsForDev(configParam, env),
823
859
  ...args.vars,
824
860
  },
825
- metadata_binding: configParam.metadata_binding,
826
861
  wasm_modules: configParam.wasm_modules,
827
862
  text_blobs: configParam.text_blobs,
828
863
  data_blobs: configParam.data_blobs,
@@ -856,11 +891,36 @@ async function getBindings(
856
891
  ...(args.r2 || []),
857
892
  ],
858
893
  dispatch_namespaces: configParam.dispatch_namespaces,
894
+ mtls_certificates: configParam.mtls_certificates,
859
895
  services: configParam.services,
860
- unsafe: configParam.unsafe?.bindings,
896
+ analytics_engine_datasets: configParam.analytics_engine_datasets,
897
+ unsafe: {
898
+ bindings: configParam.unsafe.bindings,
899
+ metadata: configParam.unsafe.metadata,
900
+ },
861
901
  logfwdr: configParam.logfwdr,
862
902
  d1_databases: identifyD1BindingsAsBeta([
863
- ...configParam.d1_databases,
903
+ ...(configParam.d1_databases ?? []).map((d1Db) => {
904
+ //in local dev, bindings don't matter
905
+ if (local) {
906
+ return {
907
+ ...d1Db,
908
+ database_id: "local",
909
+ };
910
+ }
911
+ // if you have a preview_database_id, we'll use it, but we shouldn't force people to use it.
912
+ if (!d1Db.preview_database_id && !process.env.NO_D1_WARNING) {
913
+ logger.log(
914
+ `--------------------\nšŸ’” Recommendation: for development, use a preview D1 database rather than the one you'd use in production.\nšŸ’” Create a new D1 database with "wrangler d1 create <name>" and add its id as preview_database_id to the d1_database "${d1Db.binding}" in your wrangler.toml\n--------------------\n`
915
+ );
916
+ }
917
+ return {
918
+ ...d1Db,
919
+ database_id: d1Db.preview_database_id
920
+ ? d1Db.preview_database_id
921
+ : d1Db.database_id,
922
+ };
923
+ }),
864
924
  ...(args.d1Databases || []),
865
925
  ]),
866
926
  };