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
@@ -1,5 +1,7 @@
1
1
  import { readFile } from "node:fs/promises";
2
2
  import path from "node:path";
3
+ import { Text } from "ink";
4
+ import SelectInput from "ink-select-input";
3
5
  import React, { useState, useEffect, useRef } from "react";
4
6
  import { useErrorHandler } from "react-error-boundary";
5
7
  import { printBundleSize } from "../bundle-reporter";
@@ -10,9 +12,9 @@ import {
10
12
  import useInspector from "../inspect";
11
13
  import { logger } from "../logger";
12
14
  import { startPreviewServer, usePreviewServer } from "../proxy";
15
+ import { helpIfErrorIsSizeOrScriptStartup } from "../publish/publish";
13
16
  import { syncAssets } from "../sites";
14
17
  import {
15
- ChooseAccount,
16
18
  getAccountChoices,
17
19
  requireApiToken,
18
20
  saveAccountToCache,
@@ -302,13 +304,13 @@ export function useWorker(
302
304
  // TODO: Once we get service bindings working in the
303
305
  // edge preview server, we can define remote dev service bindings
304
306
  // and you can uncomment this code.
305
- // https://github.com/cloudflare/wrangler2/issues/1182
307
+ // https://github.com/cloudflare/workers-sdk/issues/1182
306
308
 
307
309
  /*
308
310
  if (name) {
309
311
  await registerWorker(name, {
310
312
  mode: "remote",
311
- // upstream protocol is always https (https://github.com/cloudflare/wrangler2/issues/583)
313
+ // upstream protocol is always https (https://github.com/cloudflare/workers-sdk/issues/583)
312
314
  protocol: "https",
313
315
  port: undefined,
314
316
  host: workerPreviewToken.host,
@@ -493,6 +495,7 @@ export async function getRemotePreviewToken(props: RemoteProps) {
493
495
  // code 10049 happens when the preview token expires
494
496
  logger.log("Preview token expired, restart server to fetch a new one");
495
497
  } else {
498
+ helpIfErrorIsSizeOrScriptStartup(err, props.bundle?.dependencies || {});
496
499
  logger.error("Error on remote worker:", err);
497
500
  }
498
501
  }
@@ -601,3 +604,29 @@ function getWorkerAccountAndContext(props: {
601
604
 
602
605
  return { workerAccount, workerContext };
603
606
  }
607
+
608
+ /**
609
+ * A component that allows the user to select from a list of available accounts.
610
+ */
611
+ function ChooseAccount(props: {
612
+ accounts: ChooseAccountItem[];
613
+ onSelect: (account: { name: string; id: string }) => void;
614
+ onError: (error: Error) => void;
615
+ }) {
616
+ return (
617
+ <>
618
+ <Text bold>Select an account from below:</Text>
619
+ <SelectInput
620
+ items={props.accounts.map((item) => ({
621
+ key: item.id,
622
+ label: item.name,
623
+ value: item,
624
+ }))}
625
+ onSelect={(item) => {
626
+ logger.log(`Using account: "${item.value.name} - ${item.value.id}"`);
627
+ props.onSelect({ id: item.value.id, name: item.value.name });
628
+ }}
629
+ />
630
+ </>
631
+ );
632
+ }
@@ -1,6 +1,5 @@
1
1
  import { fork } from "node:child_process";
2
2
  import { realpathSync } from "node:fs";
3
- import { writeFile } from "node:fs/promises";
4
3
  import * as path from "node:path";
5
4
  import * as util from "node:util";
6
5
  import onExit from "signal-exit";
@@ -15,29 +14,33 @@ import {
15
14
  import { runCustomBuild } from "../entry";
16
15
  import { logger } from "../logger";
17
16
  import { waitForPortToBeAvailable } from "../proxy";
17
+ import traverseModuleGraph from "../traverse-module-graph";
18
18
  import {
19
19
  setupBindings,
20
- setupExperimentalLocal,
20
+ getMiniflare3,
21
+ buildMiniflare3Logger,
21
22
  setupMiniflareOptions,
22
23
  setupNodeOptions,
24
+ transformMf2OptionsToMf3Options,
23
25
  } from "./local";
24
26
  import { startRemoteServer } from "./remote";
25
27
  import { validateDevProps } from "./validate-dev-props";
26
28
 
27
29
  import type { Config } from "../config";
30
+ import type { DurableObjectBindings } from "../config/environment";
28
31
  import type { WorkerRegistry } from "../dev-registry";
29
32
  import type { Entry } from "../entry";
30
33
  import type { DevProps, DirectorySyncResult } from "./dev";
31
34
  import type { LocalProps } from "./local";
32
35
  import type { EsbuildBundle } from "./use-esbuild";
33
36
  import type { Miniflare as Miniflare3Type } from "@miniflare/tre";
34
- import type { MiniflareOptions } from "miniflare";
35
37
 
36
38
  import type { ChildProcess } from "node:child_process";
37
39
 
38
40
  export async function startDevServer(
39
41
  props: DevProps & {
40
42
  local: boolean;
43
+ disableDevRegistry: boolean;
41
44
  }
42
45
  ) {
43
46
  try {
@@ -61,20 +64,29 @@ export async function startDevServer(
61
64
  }
62
65
 
63
66
  //start the worker registry
64
- startWorkerRegistry().catch((err) => {
65
- logger.error("failed to start worker registry", err);
66
- });
67
- if (props.local) {
68
- const boundRegisteredWorkers = await getBoundRegisteredWorkers({
69
- services: props.bindings.services,
70
- durableObjects: props.bindings.durable_objects,
71
- });
67
+ logger.log("disableDevRegistry: ", props.disableDevRegistry);
68
+ if (!props.disableDevRegistry) {
69
+ try {
70
+ await startWorkerRegistry();
71
+ if (props.local) {
72
+ const boundRegisteredWorkers = await getBoundRegisteredWorkers({
73
+ services: props.bindings.services,
74
+ durableObjects: props.bindings.durable_objects,
75
+ });
72
76
 
73
- if (!util.isDeepStrictEqual(boundRegisteredWorkers, workerDefinitions)) {
74
- workerDefinitions = boundRegisteredWorkers || {};
77
+ if (
78
+ !util.isDeepStrictEqual(boundRegisteredWorkers, workerDefinitions)
79
+ ) {
80
+ workerDefinitions = boundRegisteredWorkers || {};
81
+ }
82
+ }
83
+ } catch (err) {
84
+ logger.error("failed to start worker registry", err);
75
85
  }
76
86
  }
77
87
 
88
+ const betaD1Shims = props.bindings.d1_databases?.map((db) => db.binding);
89
+
78
90
  //implement a react-free version of useEsbuild
79
91
  const bundle = await runEsbuild({
80
92
  entry: props.entry,
@@ -87,15 +99,19 @@ export async function startDevServer(
87
99
  ),
88
100
  tsconfig: props.tsconfig,
89
101
  minify: props.minify,
90
- nodeCompat: props.nodeCompat,
102
+ legacyNodeCompat: props.legacyNodeCompat,
103
+ nodejsCompat: props.nodejsCompat,
91
104
  define: props.define,
92
105
  noBundle: props.noBundle,
93
106
  assets: props.assetsConfig,
107
+ betaD1Shims,
94
108
  workerDefinitions,
95
109
  services: props.bindings.services,
96
110
  firstPartyWorkerDevFacade: props.firstPartyWorker,
97
111
  testScheduled: props.testScheduled,
98
- experimentalLocalStubCache: props.experimentalLocal,
112
+ local: props.local,
113
+ experimentalLocal: props.experimentalLocal,
114
+ doBindings: props.bindings.durable_objects?.bindings ?? [],
99
115
  });
100
116
 
101
117
  if (props.local) {
@@ -107,8 +123,8 @@ export async function startDevServer(
107
123
  compatibilityFlags: props.compatibilityFlags,
108
124
  bindings: props.bindings,
109
125
  assetPaths: props.assetPaths,
110
- port: props.port,
111
- ip: props.ip,
126
+ initialPort: props.initialPort,
127
+ initialIp: props.initialIp,
112
128
  rules: props.rules,
113
129
  inspectorPort: props.inspectorPort,
114
130
  localPersistencePath: props.localPersistencePath,
@@ -117,13 +133,14 @@ export async function startDevServer(
117
133
  queueConsumers: props.queueConsumers,
118
134
  localProtocol: props.localProtocol,
119
135
  localUpstream: props.localUpstream,
120
- logPrefix: props.logPrefix,
121
136
  inspect: props.inspect,
122
137
  onReady: props.onReady,
123
138
  enablePagesAssetsServiceBinding: props.enablePagesAssetsServiceBinding,
124
139
  usageModel: props.usageModel,
125
140
  workerDefinitions,
126
141
  experimentalLocal: props.experimentalLocal,
142
+ accountId: props.accountId,
143
+ experimentalLocalRemoteKv: props.experimentalLocalRemoteKv,
127
144
  });
128
145
 
129
146
  return {
@@ -142,8 +159,8 @@ export async function startDevServer(
142
159
  bindings: props.bindings,
143
160
  assetPaths: props.assetPaths,
144
161
  isWorkersSite: props.isWorkersSite,
145
- port: props.port,
146
- ip: props.ip,
162
+ port: props.initialPort,
163
+ ip: props.initialIp,
147
164
  localProtocol: props.localProtocol,
148
165
  inspectorPort: props.inspectorPort,
149
166
  inspect: props.inspect,
@@ -190,17 +207,21 @@ async function runEsbuild({
190
207
  jsxFragment,
191
208
  rules,
192
209
  assets,
210
+ betaD1Shims,
193
211
  serveAssetsFromWorker,
194
212
  tsconfig,
195
213
  minify,
196
- nodeCompat,
214
+ legacyNodeCompat,
215
+ nodejsCompat,
197
216
  define,
198
217
  noBundle,
199
218
  workerDefinitions,
200
219
  services,
201
220
  firstPartyWorkerDevFacade,
202
221
  testScheduled,
203
- experimentalLocalStubCache,
222
+ local,
223
+ experimentalLocal,
224
+ doBindings,
204
225
  }: {
205
226
  entry: Entry;
206
227
  destination: string | undefined;
@@ -208,17 +229,21 @@ async function runEsbuild({
208
229
  jsxFragment: string | undefined;
209
230
  rules: Config["rules"];
210
231
  assets: Config["assets"];
232
+ betaD1Shims?: string[];
211
233
  define: Config["define"];
212
234
  services: Config["services"];
213
235
  serveAssetsFromWorker: boolean;
214
236
  tsconfig: string | undefined;
215
237
  minify: boolean | undefined;
216
- nodeCompat: boolean | undefined;
238
+ legacyNodeCompat: boolean | undefined;
239
+ nodejsCompat: boolean | undefined;
217
240
  noBundle: boolean;
218
241
  workerDefinitions: WorkerRegistry;
219
242
  firstPartyWorkerDevFacade: boolean | undefined;
220
243
  testScheduled?: boolean;
221
- experimentalLocalStubCache: boolean | undefined;
244
+ local: boolean;
245
+ experimentalLocal: boolean | undefined;
246
+ doBindings: DurableObjectBindings;
222
247
  }): Promise<EsbuildBundle | undefined> {
223
248
  if (!destination) return;
224
249
 
@@ -229,14 +254,7 @@ async function runEsbuild({
229
254
  dependencies,
230
255
  sourceMapPath,
231
256
  }: Awaited<ReturnType<typeof bundleWorker>> = noBundle
232
- ? {
233
- modules: [],
234
- dependencies: {},
235
- resolvedEntryPointPath: entry.file,
236
- bundleType: entry.format === "modules" ? "esm" : "commonjs",
237
- stop: undefined,
238
- sourceMapPath: undefined,
239
- }
257
+ ? await traverseModuleGraph(entry, rules)
240
258
  : await bundleWorker(entry, destination, {
241
259
  serveAssetsFromWorker,
242
260
  jsxFactory,
@@ -244,7 +262,8 @@ async function runEsbuild({
244
262
  rules,
245
263
  tsconfig,
246
264
  minify,
247
- nodeCompat,
265
+ legacyNodeCompat,
266
+ nodejsCompat,
248
267
  define,
249
268
  checkFetch: true,
250
269
  assets: assets && {
@@ -252,13 +271,15 @@ async function runEsbuild({
252
271
  // disable the cache in dev
253
272
  bypassCache: true,
254
273
  },
274
+ betaD1Shims,
255
275
  workerDefinitions,
256
276
  services,
257
277
  firstPartyWorkerDevFacade,
258
278
  targetConsumer: "dev", // We are starting a dev server
259
- local: false,
260
279
  testScheduled,
261
- experimentalLocalStubCache,
280
+ local,
281
+ experimentalLocal,
282
+ doBindings,
262
283
  });
263
284
 
264
285
  return {
@@ -282,21 +303,22 @@ export async function startLocalServer({
282
303
  bindings,
283
304
  workerDefinitions,
284
305
  assetPaths,
285
- port,
306
+ initialPort,
286
307
  inspectorPort,
287
308
  rules,
288
309
  localPersistencePath,
289
310
  liveReload,
290
- ip,
311
+ initialIp,
291
312
  crons,
292
313
  queueConsumers,
293
314
  localProtocol,
294
315
  localUpstream,
295
316
  inspect,
296
317
  onReady,
297
- logPrefix,
298
318
  enablePagesAssetsServiceBinding,
299
319
  experimentalLocal,
320
+ accountId,
321
+ experimentalLocalRemoteKv,
300
322
  }: LocalProps) {
301
323
  let local: ChildProcess | undefined;
302
324
  let experimentalLocalRef: Miniflare3Type | undefined;
@@ -311,14 +333,11 @@ export async function startLocalServer({
311
333
  if (!bundle || !format) return;
312
334
 
313
335
  // port for the worker
314
-
315
- if (port !== 0) {
316
- await waitForPortToBeAvailable(port, {
317
- retryPeriod: 200,
318
- timeout: 2000,
319
- abortSignal: abortController.signal,
320
- });
321
- }
336
+ await waitForPortToBeAvailable(initialPort, {
337
+ retryPeriod: 200,
338
+ timeout: 2000,
339
+ abortSignal: abortController.signal,
340
+ });
322
341
 
323
342
  if (bindings.services && bindings.services.length > 0) {
324
343
  logger.warn(
@@ -326,15 +345,6 @@ export async function startLocalServer({
326
345
  );
327
346
  }
328
347
 
329
- // In local mode, we want to copy all referenced modules into
330
- // the output bundle directory before starting up
331
- for (const module of bundle.modules) {
332
- await writeFile(
333
- path.join(path.dirname(bundle.path), module.name),
334
- module.content
335
- );
336
- }
337
-
338
348
  const scriptPath = realpathSync(bundle.path);
339
349
 
340
350
  const upstream =
@@ -357,12 +367,12 @@ export async function startLocalServer({
357
367
  bundle,
358
368
  });
359
369
 
360
- const { forkOptions, miniflareCLIPath } = setupMiniflareOptions({
370
+ const { forkOptions, miniflareCLIPath, options } = setupMiniflareOptions({
361
371
  workerName,
362
- port,
372
+ port: initialPort,
363
373
  scriptPath,
364
374
  localProtocol,
365
- ip,
375
+ ip: initialIp,
366
376
  format,
367
377
  rules,
368
378
  compatibilityDate,
@@ -384,27 +394,38 @@ export async function startLocalServer({
384
394
  dataBlobBindings,
385
395
  crons,
386
396
  upstream,
387
- logPrefix,
388
397
  workerDefinitions,
389
398
  enablePagesAssetsServiceBinding,
390
399
  });
391
400
 
392
401
  if (experimentalLocal) {
393
- // TODO: refactor setupMiniflareOptions so we don't need to parse here
394
- const mf2Options: MiniflareOptions = JSON.parse(forkOptions[0]);
395
- const mf = await setupExperimentalLocal(mf2Options, format, bundle);
402
+ const log = await buildMiniflare3Logger();
403
+ const mf3Options = await transformMf2OptionsToMf3Options({
404
+ miniflare2Options: options,
405
+ format,
406
+ bundle,
407
+ log,
408
+ kvNamespaces: bindings?.kv_namespaces,
409
+ r2Buckets: bindings?.r2_buckets,
410
+ d1Databases: bindings?.d1_databases,
411
+ authenticatedAccountId: accountId,
412
+ kvRemote: experimentalLocalRemoteKv,
413
+ inspectorPort,
414
+ });
415
+ const { Miniflare } = await getMiniflare3();
416
+ const mf = new Miniflare(mf3Options);
396
417
  const runtimeURL = await mf.ready;
397
418
  experimentalLocalRef = mf;
398
419
  removeSignalExitListener = onExit((_code, _signal) => {
399
420
  logger.log("⎔ Shutting down experimental local server.");
400
- mf.dispose();
421
+ void mf.dispose();
401
422
  experimentalLocalRef = undefined;
402
423
  });
403
424
  onReady?.(runtimeURL.hostname, parseInt(runtimeURL.port ?? 8787));
404
425
  return;
405
426
  }
406
427
 
407
- const nodeOptions = setupNodeOptions({ inspect, ip, inspectorPort });
428
+ const nodeOptions = setupNodeOptions({ inspect, inspectorPort });
408
429
  logger.log("⎔ Starting a local server...");
409
430
 
410
431
  const child = (local = fork(miniflareCLIPath, forkOptions, {
@@ -421,21 +442,21 @@ export async function startLocalServer({
421
442
  await registerWorker(workerName, {
422
443
  protocol: localProtocol,
423
444
  mode: "local",
424
- port: message.mfPort,
425
- host: ip,
445
+ port: message.port,
446
+ host: initialIp,
426
447
  durableObjects: internalDurableObjects.map((binding) => ({
427
448
  name: binding.name,
428
449
  className: binding.class_name,
429
450
  })),
430
451
  ...(message.durableObjectsPort
431
452
  ? {
432
- durableObjectsHost: ip,
453
+ durableObjectsHost: initialIp,
433
454
  durableObjectsPort: message.durableObjectsPort,
434
455
  }
435
456
  : {}),
436
457
  });
437
458
  }
438
- onReady?.(ip, message.mfPort);
459
+ onReady?.(initialIp, message.port);
439
460
  }
440
461
  });
441
462
 
@@ -4,6 +4,7 @@ import { useApp } from "ink";
4
4
  import { useState, useEffect } from "react";
5
5
  import { bundleWorker, rewriteNodeCompatBuildFailure } from "../bundle";
6
6
  import { logBuildFailure, logger } from "../logger";
7
+ import traverseModuleGraph from "../traverse-module-graph";
7
8
  import type { Config } from "../config";
8
9
  import type { WorkerRegistry } from "../dev-registry";
9
10
  import type { Entry } from "../entry";
@@ -30,7 +31,8 @@ export function useEsbuild({
30
31
  serveAssetsFromWorker,
31
32
  tsconfig,
32
33
  minify,
33
- nodeCompat,
34
+ legacyNodeCompat,
35
+ nodejsCompat,
34
36
  betaD1Shims,
35
37
  define,
36
38
  noBundle,
@@ -41,7 +43,7 @@ export function useEsbuild({
41
43
  local,
42
44
  targetConsumer,
43
45
  testScheduled,
44
- experimentalLocalStubCache,
46
+ experimentalLocal,
45
47
  }: {
46
48
  entry: Entry;
47
49
  destination: string | undefined;
@@ -54,7 +56,8 @@ export function useEsbuild({
54
56
  serveAssetsFromWorker: boolean;
55
57
  tsconfig: string | undefined;
56
58
  minify: boolean | undefined;
57
- nodeCompat: boolean | undefined;
59
+ legacyNodeCompat: boolean | undefined;
60
+ nodejsCompat: boolean | undefined;
58
61
  betaD1Shims?: string[];
59
62
  noBundle: boolean;
60
63
  workerDefinitions: WorkerRegistry;
@@ -63,7 +66,7 @@ export function useEsbuild({
63
66
  local: boolean;
64
67
  targetConsumer: "dev" | "publish";
65
68
  testScheduled: boolean;
66
- experimentalLocalStubCache: boolean | undefined;
69
+ experimentalLocal: boolean | undefined;
67
70
  }): EsbuildBundle | undefined {
68
71
  const [bundle, setBundle] = useState<EsbuildBundle>();
69
72
  const { exit } = useApp();
@@ -85,7 +88,7 @@ export function useEsbuild({
85
88
  const watchMode: WatchMode = {
86
89
  async onRebuild(error) {
87
90
  if (error !== null) {
88
- if (!nodeCompat) rewriteNodeCompatBuildFailure(error);
91
+ if (!legacyNodeCompat) rewriteNodeCompatBuildFailure(error);
89
92
  logBuildFailure(error);
90
93
  logger.error("Watch build failed:", error.message);
91
94
  } else {
@@ -105,14 +108,7 @@ export function useEsbuild({
105
108
  stop,
106
109
  sourceMapPath,
107
110
  }: Awaited<ReturnType<typeof bundleWorker>> = noBundle
108
- ? {
109
- modules: [],
110
- dependencies: {},
111
- resolvedEntryPointPath: entry.file,
112
- bundleType: entry.format === "modules" ? "esm" : "commonjs",
113
- stop: undefined,
114
- sourceMapPath: undefined,
115
- }
111
+ ? await traverseModuleGraph(entry, rules)
116
112
  : await bundleWorker(entry, destination, {
117
113
  serveAssetsFromWorker,
118
114
  jsxFactory,
@@ -121,8 +117,10 @@ export function useEsbuild({
121
117
  watch: watchMode,
122
118
  tsconfig,
123
119
  minify,
124
- nodeCompat,
120
+ legacyNodeCompat,
121
+ nodejsCompat,
125
122
  betaD1Shims,
123
+ doBindings: durableObjects.bindings,
126
124
  define,
127
125
  checkFetch: true,
128
126
  assets: assets && {
@@ -136,7 +134,7 @@ export function useEsbuild({
136
134
  local,
137
135
  targetConsumer,
138
136
  testScheduled,
139
- experimentalLocalStubCache,
137
+ experimentalLocal,
140
138
  });
141
139
 
142
140
  // Capture the `stop()` method to use as the `useEffect()` destructor.
@@ -152,7 +150,7 @@ export function useEsbuild({
152
150
  });
153
151
 
154
152
  stopWatching = () => {
155
- watcher.close();
153
+ void watcher.close();
156
154
  };
157
155
  }
158
156
  setBundle({
@@ -187,7 +185,8 @@ export function useEsbuild({
187
185
  exit,
188
186
  noBundle,
189
187
  minify,
190
- nodeCompat,
188
+ legacyNodeCompat,
189
+ nodejsCompat,
191
190
  define,
192
191
  assets,
193
192
  services,
@@ -198,7 +197,7 @@ export function useEsbuild({
198
197
  local,
199
198
  targetConsumer,
200
199
  testScheduled,
201
- experimentalLocalStubCache,
200
+ experimentalLocal,
202
201
  ]);
203
202
  return bundle;
204
203
  }
@@ -28,4 +28,13 @@ export function validateDevProps(props: DevProps) {
28
28
  "You cannot configure [data_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[rules]` in your wrangler.toml"
29
29
  );
30
30
  }
31
+
32
+ if (
33
+ props.compatibilityFlags?.includes("nodejs_compat") &&
34
+ props.legacyNodeCompat
35
+ ) {
36
+ throw new Error(
37
+ "You cannot use the `nodejs_compat` compatibility flag in conjunction with the legacy `--node-compat` flag. If you want to use the new runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command or your config file."
38
+ );
39
+ }
31
40
  }
@@ -13,7 +13,7 @@ import type { HttpTerminator } from "http-terminator";
13
13
  const DEV_REGISTRY_PORT = "6284";
14
14
  const DEV_REGISTRY_HOST = `http://localhost:${DEV_REGISTRY_PORT}`;
15
15
 
16
- let server: Server;
16
+ let server: Server | null;
17
17
  let terminator: HttpTerminator;
18
18
 
19
19
  export type WorkerRegistry = Record<string, WorkerDefinition>;
@@ -102,6 +102,7 @@ export async function startWorkerRegistry() {
102
102
  */
103
103
  export async function stopWorkerRegistry() {
104
104
  await terminator?.terminate();
105
+ server = null;
105
106
  }
106
107
 
107
108
  /**