wrangler 2.0.12 → 2.0.16

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 (149) hide show
  1. package/README.md +7 -1
  2. package/bin/wrangler.js +111 -57
  3. package/miniflare-dist/index.mjs +9 -2
  4. package/package.json +156 -154
  5. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  6. package/src/__tests__/config-cache.test.ts +30 -24
  7. package/src/__tests__/configuration.test.ts +3935 -3476
  8. package/src/__tests__/dev.test.tsx +1128 -979
  9. package/src/__tests__/guess-worker-format.test.ts +68 -68
  10. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  11. package/src/__tests__/helpers/faye-websocket.d.ts +4 -4
  12. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  13. package/src/__tests__/helpers/mock-bin.ts +20 -20
  14. package/src/__tests__/helpers/mock-cfetch.ts +92 -92
  15. package/src/__tests__/helpers/mock-console.ts +49 -39
  16. package/src/__tests__/helpers/mock-dialogs.ts +94 -71
  17. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  18. package/src/__tests__/helpers/mock-istty.ts +65 -18
  19. package/src/__tests__/helpers/mock-kv.ts +26 -26
  20. package/src/__tests__/helpers/mock-oauth-flow.ts +223 -228
  21. package/src/__tests__/helpers/mock-process.ts +39 -0
  22. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  23. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  24. package/src/__tests__/helpers/run-in-tmp.ts +27 -27
  25. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  26. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  27. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  28. package/src/__tests__/https-options.test.ts +104 -104
  29. package/src/__tests__/index.test.ts +239 -234
  30. package/src/__tests__/init.test.ts +1605 -1250
  31. package/src/__tests__/jest.setup.ts +63 -33
  32. package/src/__tests__/kv.test.ts +1128 -1011
  33. package/src/__tests__/logger.test.ts +100 -74
  34. package/src/__tests__/package-manager.test.ts +303 -303
  35. package/src/__tests__/pages.test.ts +1152 -652
  36. package/src/__tests__/parse.test.ts +252 -252
  37. package/src/__tests__/publish.test.ts +6371 -5622
  38. package/src/__tests__/pubsub.test.ts +367 -0
  39. package/src/__tests__/r2.test.ts +133 -133
  40. package/src/__tests__/route.test.ts +18 -18
  41. package/src/__tests__/secret.test.ts +382 -377
  42. package/src/__tests__/tail.test.ts +530 -530
  43. package/src/__tests__/user.test.ts +123 -111
  44. package/src/__tests__/whoami.test.tsx +198 -117
  45. package/src/__tests__/worker-namespace.test.ts +327 -0
  46. package/src/abort.d.ts +1 -1
  47. package/src/api/dev.ts +49 -0
  48. package/src/api/index.ts +1 -0
  49. package/src/bundle-reporter.tsx +29 -0
  50. package/src/bundle.ts +157 -149
  51. package/src/cfetch/index.ts +80 -80
  52. package/src/cfetch/internal.ts +90 -83
  53. package/src/cli.ts +21 -7
  54. package/src/config/config.ts +204 -195
  55. package/src/config/diagnostics.ts +61 -61
  56. package/src/config/environment.ts +390 -357
  57. package/src/config/index.ts +206 -193
  58. package/src/config/validation-helpers.ts +366 -366
  59. package/src/config/validation.ts +1573 -1376
  60. package/src/config-cache.ts +79 -41
  61. package/src/create-worker-preview.ts +206 -136
  62. package/src/create-worker-upload-form.ts +247 -238
  63. package/src/dev/dev-vars.ts +13 -13
  64. package/src/dev/dev.tsx +329 -307
  65. package/src/dev/local.tsx +304 -275
  66. package/src/dev/remote.tsx +366 -224
  67. package/src/dev/use-esbuild.ts +126 -91
  68. package/src/dev.tsx +538 -0
  69. package/src/dialogs.tsx +97 -97
  70. package/src/durable.ts +87 -87
  71. package/src/entry.ts +234 -228
  72. package/src/environment-variables.ts +23 -23
  73. package/src/errors.ts +6 -6
  74. package/src/generate.ts +33 -0
  75. package/src/git-client.ts +42 -0
  76. package/src/https-options.ts +79 -79
  77. package/src/index.tsx +1775 -2763
  78. package/src/init.ts +549 -0
  79. package/src/inspect.ts +593 -593
  80. package/src/intl-polyfill.d.ts +123 -123
  81. package/src/is-interactive.ts +12 -0
  82. package/src/kv.ts +277 -277
  83. package/src/logger.ts +46 -39
  84. package/src/miniflare-cli/enum-keys.ts +8 -8
  85. package/src/miniflare-cli/index.ts +42 -31
  86. package/src/miniflare-cli/request-context.ts +18 -18
  87. package/src/module-collection.ts +212 -212
  88. package/src/open-in-browser.ts +4 -6
  89. package/src/package-manager.ts +123 -123
  90. package/src/pages/build.tsx +202 -0
  91. package/src/pages/constants.ts +7 -0
  92. package/src/pages/deployments.tsx +101 -0
  93. package/src/pages/dev.tsx +964 -0
  94. package/src/pages/functions/buildPlugin.ts +105 -0
  95. package/src/pages/functions/buildWorker.ts +151 -0
  96. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  97. package/src/pages/functions/filepath-routing.ts +189 -0
  98. package/src/pages/functions/identifiers.ts +78 -0
  99. package/src/pages/functions/routes.ts +151 -0
  100. package/src/pages/index.tsx +84 -0
  101. package/src/pages/projects.tsx +157 -0
  102. package/src/pages/publish.tsx +335 -0
  103. package/src/pages/types.ts +40 -0
  104. package/src/pages/upload.tsx +384 -0
  105. package/src/pages/utils.ts +12 -0
  106. package/src/parse.ts +202 -138
  107. package/src/paths.ts +6 -6
  108. package/src/preview.ts +31 -0
  109. package/src/proxy.ts +400 -402
  110. package/src/publish.ts +667 -621
  111. package/src/pubsub/index.ts +286 -0
  112. package/src/pubsub/pubsub-commands.tsx +577 -0
  113. package/src/r2.ts +19 -19
  114. package/src/selfsigned.d.ts +23 -23
  115. package/src/sites.tsx +271 -225
  116. package/src/tail/filters.ts +108 -108
  117. package/src/tail/index.ts +217 -217
  118. package/src/tail/printing.ts +45 -45
  119. package/src/update-check.ts +11 -11
  120. package/src/user/choose-account.tsx +60 -0
  121. package/src/user/env-vars.ts +46 -0
  122. package/src/user/generate-auth-url.ts +33 -0
  123. package/src/user/generate-random-state.ts +16 -0
  124. package/src/user/index.ts +3 -0
  125. package/src/user/user.tsx +1161 -0
  126. package/src/whoami.tsx +61 -42
  127. package/src/worker-namespace.ts +190 -0
  128. package/src/worker.ts +110 -100
  129. package/src/zones.ts +39 -36
  130. package/templates/checked-fetch.js +17 -0
  131. package/templates/new-worker-scheduled.js +3 -3
  132. package/templates/new-worker-scheduled.ts +15 -15
  133. package/templates/new-worker.js +3 -3
  134. package/templates/new-worker.ts +15 -15
  135. package/templates/no-op-worker.js +10 -0
  136. package/templates/pages-template-plugin.ts +155 -0
  137. package/templates/pages-template-worker.ts +161 -0
  138. package/templates/static-asset-facade.js +31 -31
  139. package/templates/tsconfig.json +95 -95
  140. package/wrangler-dist/cli.js +55383 -54138
  141. package/pages/functions/buildPlugin.ts +0 -105
  142. package/pages/functions/buildWorker.ts +0 -151
  143. package/pages/functions/filepath-routing.ts +0 -189
  144. package/pages/functions/identifiers.ts +0 -78
  145. package/pages/functions/routes.ts +0 -156
  146. package/pages/functions/template-plugin.ts +0 -147
  147. package/pages/functions/template-worker.ts +0 -143
  148. package/src/pages.tsx +0 -2093
  149. package/src/user.tsx +0 -1214
package/src/dev/local.tsx CHANGED
@@ -15,307 +15,336 @@ import type { EsbuildBundle } from "./use-esbuild";
15
15
  import type { MiniflareOptions } from "miniflare";
16
16
 
17
17
  interface LocalProps {
18
- name: string | undefined;
19
- bundle: EsbuildBundle | undefined;
20
- format: CfScriptFormat | undefined;
21
- compatibilityDate: string;
22
- compatibilityFlags: string[] | undefined;
23
- bindings: CfWorkerInit["bindings"];
24
- localProtocol: "http" | "https";
25
- assetPaths: AssetPaths | undefined;
26
- public: string | undefined;
27
- port: number;
28
- ip: string;
29
- rules: Config["rules"];
30
- inspectorPort: number;
31
- enableLocalPersistence: boolean;
32
- crons: Config["triggers"]["crons"];
18
+ name: string | undefined;
19
+ bundle: EsbuildBundle | undefined;
20
+ format: CfScriptFormat | undefined;
21
+ compatibilityDate: string;
22
+ compatibilityFlags: string[] | undefined;
23
+ bindings: CfWorkerInit["bindings"];
24
+ assetPaths: AssetPaths | undefined;
25
+ isWorkersSite: boolean;
26
+ port: number;
27
+ ip: string;
28
+ rules: Config["rules"];
29
+ inspectorPort: number;
30
+ enableLocalPersistence: boolean;
31
+ crons: Config["triggers"]["crons"];
32
+ localProtocol: "http" | "https";
33
+ localUpstream: string | undefined;
34
+ inspect: boolean | undefined;
35
+ onReady: (() => void) | undefined;
36
+ logLevel: "none" | "error" | "log" | "warn" | "debug" | undefined;
33
37
  }
34
38
 
35
39
  export function Local(props: LocalProps) {
36
- const { inspectorUrl } = useLocalWorker(props);
37
- useInspector({
38
- inspectorUrl,
39
- port: props.inspectorPort,
40
- logToTerminal: false,
41
- });
42
- return null;
40
+ const { inspectorUrl } = useLocalWorker(props);
41
+ useInspector({
42
+ inspectorUrl,
43
+ port: props.inspectorPort,
44
+ logToTerminal: false,
45
+ });
46
+ return null;
43
47
  }
44
48
 
45
49
  function useLocalWorker({
46
- name: workerName,
47
- bundle,
48
- format,
49
- compatibilityDate,
50
- compatibilityFlags,
51
- bindings,
52
- assetPaths,
53
- public: publicDirectory,
54
- port,
55
- rules,
56
- enableLocalPersistence,
57
- ip,
58
- localProtocol,
59
- crons,
50
+ name: workerName,
51
+ bundle,
52
+ format,
53
+ compatibilityDate,
54
+ compatibilityFlags,
55
+ bindings,
56
+ assetPaths,
57
+ isWorkersSite,
58
+ port,
59
+ rules,
60
+ enableLocalPersistence,
61
+ ip,
62
+ crons,
63
+ localProtocol,
64
+ localUpstream,
65
+ inspect,
66
+ onReady,
67
+ logLevel,
60
68
  }: LocalProps) {
61
- // TODO: pass vars via command line
62
- const local = useRef<ReturnType<typeof spawn>>();
63
- const removeSignalExitListener = useRef<() => void>();
64
- const [inspectorUrl, setInspectorUrl] = useState<string | undefined>();
65
- // if we're using local persistence for data, we should use the cwd
66
- // as an explicit path, or else it'll use the temp dir
67
- // which disappears when dev ends
68
- const localPersistencePath = enableLocalPersistence
69
- ? // Maybe we could make the path configurable as well?
70
- path.join(process.cwd(), "wrangler-local-state")
71
- : // We otherwise choose null, but choose true later
72
- // so that it's persisted in the temp dir across a dev session
73
- // even when we change source and reload
74
- null;
75
- useEffect(() => {
76
- const abortController = new AbortController();
77
- async function startLocalWorker() {
78
- if (!bundle || !format) return;
69
+ // TODO: pass vars via command line
70
+ const local = useRef<ReturnType<typeof spawn>>();
71
+ const removeSignalExitListener = useRef<() => void>();
72
+ const [inspectorUrl, setInspectorUrl] = useState<string | undefined>();
73
+ // if we're using local persistence for data, we should use the cwd
74
+ // as an explicit path, or else it'll use the temp dir
75
+ // which disappears when dev ends
76
+ const localPersistencePath = enableLocalPersistence
77
+ ? // Maybe we could make the path configurable as well?
78
+ path.join(process.cwd(), "wrangler-local-state")
79
+ : // We otherwise choose null, but choose true later
80
+ // so that it's persisted in the temp dir across a dev session
81
+ // even when we change source and reload
82
+ null;
83
+ useEffect(() => {
84
+ const abortController = new AbortController();
85
+ async function startLocalWorker() {
86
+ if (!bundle || !format) return;
79
87
 
80
- // port for the worker
81
- await waitForPortToBeAvailable(port, {
82
- retryPeriod: 200,
83
- timeout: 2000,
84
- abortSignal: abortController.signal,
85
- });
88
+ // port for the worker
89
+ await waitForPortToBeAvailable(port, {
90
+ retryPeriod: 200,
91
+ timeout: 2000,
92
+ abortSignal: abortController.signal,
93
+ });
86
94
 
87
- if (bindings.services && bindings.services.length > 0) {
88
- throw new Error(
89
- "⎔ Service bindings are not yet supported in local mode."
90
- );
91
- }
95
+ if (bindings.services && bindings.services.length > 0) {
96
+ throw new Error(
97
+ "⎔ Service bindings are not yet supported in local mode."
98
+ );
99
+ }
92
100
 
93
- // In local mode, we want to copy all referenced modules into
94
- // the output bundle directory before starting up
95
- for (const module of bundle.modules) {
96
- await writeFile(
97
- path.join(path.dirname(bundle.path), module.name),
98
- module.content
99
- );
100
- }
101
+ // In local mode, we want to copy all referenced modules into
102
+ // the output bundle directory before starting up
103
+ for (const module of bundle.modules) {
104
+ await writeFile(
105
+ path.join(path.dirname(bundle.path), module.name),
106
+ module.content
107
+ );
108
+ }
101
109
 
102
- const scriptPath = realpathSync(bundle.path);
110
+ const scriptPath = realpathSync(bundle.path);
103
111
 
104
- // the wasm_modules/text_blobs/data_blobs bindings are
105
- // relative to process.cwd(), but the actual worker bundle
106
- // is in the temp output directory; so we rewrite the paths to be absolute,
107
- // letting miniflare resolve them correctly
112
+ // the wasm_modules/text_blobs/data_blobs bindings are
113
+ // relative to process.cwd(), but the actual worker bundle
114
+ // is in the temp output directory; so we rewrite the paths to be absolute,
115
+ // letting miniflare resolve them correctly
108
116
 
109
- // wasm
110
- const wasmBindings: Record<string, string> = {};
111
- for (const [name, filePath] of Object.entries(
112
- bindings.wasm_modules || {}
113
- )) {
114
- wasmBindings[name] = path.join(process.cwd(), filePath);
115
- }
117
+ // wasm
118
+ const wasmBindings: Record<string, string> = {};
119
+ for (const [name, filePath] of Object.entries(
120
+ bindings.wasm_modules || {}
121
+ )) {
122
+ wasmBindings[name] = path.join(process.cwd(), filePath);
123
+ }
116
124
 
117
- // text
118
- const textBlobBindings: Record<string, string> = {};
119
- for (const [name, filePath] of Object.entries(
120
- bindings.text_blobs || {}
121
- )) {
122
- textBlobBindings[name] = path.join(process.cwd(), filePath);
123
- }
125
+ // text
126
+ const textBlobBindings: Record<string, string> = {};
127
+ for (const [name, filePath] of Object.entries(
128
+ bindings.text_blobs || {}
129
+ )) {
130
+ textBlobBindings[name] = path.join(process.cwd(), filePath);
131
+ }
124
132
 
125
- // data
126
- const dataBlobBindings: Record<string, string> = {};
127
- for (const [name, filePath] of Object.entries(
128
- bindings.data_blobs || {}
129
- )) {
130
- dataBlobBindings[name] = path.join(process.cwd(), filePath);
131
- }
133
+ // data
134
+ const dataBlobBindings: Record<string, string> = {};
135
+ for (const [name, filePath] of Object.entries(
136
+ bindings.data_blobs || {}
137
+ )) {
138
+ dataBlobBindings[name] = path.join(process.cwd(), filePath);
139
+ }
132
140
 
133
- if (format === "service-worker") {
134
- for (const { type, name } of bundle.modules) {
135
- if (type === "compiled-wasm") {
136
- // In service-worker format, .wasm modules are referenced by global identifiers,
137
- // so we convert it here.
138
- // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
139
- // characters with an underscore.
140
- const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
141
- wasmBindings[identifier] = name;
142
- } else if (type === "text") {
143
- // In service-worker format, text modules are referenced by global identifiers,
144
- // so we convert it here.
145
- // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
146
- // characters with an underscore.
147
- const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
148
- textBlobBindings[identifier] = name;
149
- } else if (type === "buffer") {
150
- // In service-worker format, data blobs are referenced by global identifiers,
151
- // so we convert it here.
152
- // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
153
- // characters with an underscore.
154
- const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
155
- dataBlobBindings[identifier] = name;
156
- }
157
- }
158
- }
141
+ if (format === "service-worker") {
142
+ for (const { type, name } of bundle.modules) {
143
+ if (type === "compiled-wasm") {
144
+ // In service-worker format, .wasm modules are referenced by global identifiers,
145
+ // so we convert it here.
146
+ // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
147
+ // characters with an underscore.
148
+ const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
149
+ wasmBindings[identifier] = name;
150
+ } else if (type === "text") {
151
+ // In service-worker format, text modules are referenced by global identifiers,
152
+ // so we convert it here.
153
+ // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
154
+ // characters with an underscore.
155
+ const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
156
+ textBlobBindings[identifier] = name;
157
+ } else if (type === "buffer") {
158
+ // In service-worker format, data blobs are referenced by global identifiers,
159
+ // so we convert it here.
160
+ // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
161
+ // characters with an underscore.
162
+ const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
163
+ dataBlobBindings[identifier] = name;
164
+ }
165
+ }
166
+ }
159
167
 
160
- const options: MiniflareOptions = {
161
- name: workerName,
162
- port,
163
- scriptPath,
164
- https: localProtocol === "https",
165
- host: ip,
166
- modules: format === "modules",
167
- modulesRules: (rules || [])
168
- .concat(DEFAULT_MODULE_RULES)
169
- .map(({ type, globs: include, fallthrough }) => ({
170
- type,
171
- include,
172
- fallthrough,
173
- })),
174
- compatibilityDate,
175
- compatibilityFlags,
176
- kvNamespaces: bindings.kv_namespaces?.map((kv) => kv.binding),
177
- durableObjects: Object.fromEntries(
178
- (bindings.durable_objects?.bindings ?? []).map<[string, string]>(
179
- (value) => [value.name, value.class_name]
180
- )
181
- ),
182
- ...(localPersistencePath
183
- ? {
184
- kvPersist: path.join(localPersistencePath, "kv"),
185
- durableObjectsPersist: path.join(localPersistencePath, "do"),
186
- cachePersist: path.join(localPersistencePath, "cache"),
187
- }
188
- : {
189
- // We mark these as true, so that they'll
190
- // persist in the temp directory.
191
- // This means they'll persist across a dev session,
192
- // even if we change source and reload,
193
- // and be deleted when the dev session ends
194
- durableObjectsPersist: true,
195
- cachePersist: true,
196
- kvPersist: true,
197
- }),
168
+ const upstream =
169
+ typeof localUpstream === "string"
170
+ ? `${localProtocol}://${localUpstream}`
171
+ : undefined;
198
172
 
199
- sitePath: assetPaths?.assetDirectory
200
- ? path.join(assetPaths.baseDirectory, assetPaths.assetDirectory)
201
- : undefined,
202
- siteInclude: assetPaths?.includePatterns.length
203
- ? assetPaths?.includePatterns
204
- : undefined,
205
- siteExclude: assetPaths?.excludePatterns.length
206
- ? assetPaths.excludePatterns
207
- : undefined,
208
- bindings: bindings.vars,
209
- wasmBindings,
210
- textBlobBindings,
211
- dataBlobBindings,
212
- sourceMap: true,
213
- logUnhandledRejections: true,
214
- crons,
215
- };
173
+ const options: MiniflareOptions = {
174
+ name: workerName,
175
+ port,
176
+ scriptPath,
177
+ https: localProtocol === "https",
178
+ host: ip,
179
+ modules: format === "modules",
180
+ modulesRules: (rules || [])
181
+ .concat(DEFAULT_MODULE_RULES)
182
+ .map(({ type, globs: include, fallthrough }) => ({
183
+ type,
184
+ include,
185
+ fallthrough,
186
+ })),
187
+ compatibilityDate,
188
+ compatibilityFlags,
189
+ kvNamespaces: bindings.kv_namespaces?.map((kv) => kv.binding),
190
+ durableObjects: Object.fromEntries(
191
+ (bindings.durable_objects?.bindings ?? []).map<[string, string]>(
192
+ (value) => [value.name, value.class_name]
193
+ )
194
+ ),
195
+ ...(localPersistencePath
196
+ ? {
197
+ kvPersist: path.join(localPersistencePath, "kv"),
198
+ durableObjectsPersist: path.join(localPersistencePath, "do"),
199
+ cachePersist: path.join(localPersistencePath, "cache"),
200
+ }
201
+ : {
202
+ // We mark these as true, so that they'll
203
+ // persist in the temp directory.
204
+ // This means they'll persist across a dev session,
205
+ // even if we change source and reload,
206
+ // and be deleted when the dev session ends
207
+ durableObjectsPersist: true,
208
+ cachePersist: true,
209
+ kvPersist: true,
210
+ }),
216
211
 
217
- // The path to the Miniflare CLI assumes that this file is being run from
218
- // `wrangler-dist` and that the CLI is found in `miniflare-dist`.
219
- // If either of those paths change this line needs updating.
220
- const miniflareCLIPath = path.resolve(
221
- __dirname,
222
- "../miniflare-dist/index.mjs"
223
- );
224
- const optionsArg = JSON.stringify(options, null);
212
+ sitePath: assetPaths?.assetDirectory
213
+ ? path.join(assetPaths.baseDirectory, assetPaths.assetDirectory)
214
+ : undefined,
215
+ siteInclude: assetPaths?.includePatterns.length
216
+ ? assetPaths?.includePatterns
217
+ : undefined,
218
+ siteExclude: assetPaths?.excludePatterns.length
219
+ ? assetPaths.excludePatterns
220
+ : undefined,
221
+ bindings: bindings.vars,
222
+ wasmBindings,
223
+ textBlobBindings,
224
+ dataBlobBindings,
225
+ sourceMap: true,
226
+ logUnhandledRejections: true,
227
+ crons,
228
+ upstream,
229
+ disableLogs: logLevel === "none",
230
+ };
225
231
 
226
- logger.log("⎔ Starting a local server...");
227
- local.current = spawn(
228
- "node",
229
- [
230
- "--experimental-vm-modules", // ensures that Miniflare can run ESM Workers
231
- "--no-warnings", // hide annoying Node warnings
232
- "--inspect", // start Miniflare listening for a debugger to attach
233
- miniflareCLIPath,
234
- optionsArg,
235
- // "--log=VERBOSE", // uncomment this to Miniflare to log "everything"!
236
- ],
237
- {
238
- cwd: path.dirname(scriptPath),
239
- }
240
- );
232
+ // The path to the Miniflare CLI assumes that this file is being run from
233
+ // `wrangler-dist` and that the CLI is found in `miniflare-dist`.
234
+ // If either of those paths change this line needs updating.
235
+ const miniflareCLIPath = path.resolve(
236
+ __dirname,
237
+ "../miniflare-dist/index.mjs"
238
+ );
239
+ const optionsArg = JSON.stringify(options, null);
241
240
 
242
- local.current.on("close", (code) => {
243
- if (code) {
244
- logger.log(`Miniflare process exited with code ${code}`);
245
- }
246
- });
241
+ logger.log(" Starting a local server...");
242
+ const localServerOptions = [
243
+ "--experimental-vm-modules", // ensures that Miniflare can run ESM Workers
244
+ "--no-warnings", // hide annoying Node warnings
245
+ miniflareCLIPath,
246
+ optionsArg,
247
+ // "--log=VERBOSE", // uncomment this to Miniflare to log "everything"!
248
+ ];
249
+ if (inspect) {
250
+ localServerOptions.push("--inspect"); // start Miniflare listening for a debugger to attach
251
+ }
252
+ // spawn isn't technically synchronous here
253
+ local.current = spawn("node", localServerOptions, {
254
+ cwd: path.dirname(scriptPath),
255
+ });
256
+ //TODO: instead of being lucky with spawn's timing, have miniflare-cli notify wrangler that it's ready in packages/wrangler/src/miniflare-cli/index.ts, after the mf.startScheduler promise resolves
257
+ if (onReady) {
258
+ await new Promise((resolve) => setTimeout(resolve, 500));
259
+ onReady();
260
+ }
247
261
 
248
- local.current.stdout?.on("data", (data: Buffer) => {
249
- process.stdout.write(data);
250
- });
262
+ local.current.on("close", (code) => {
263
+ if (code) {
264
+ logger.log(`Miniflare process exited with code ${code}`);
265
+ }
266
+ });
251
267
 
252
- // parse the node inspector url (which may be received in chunks) from stderr
253
- let stderrData = "";
254
- local.current.stderr?.on("data", (data: Buffer) => {
255
- stderrData += data.toString();
256
- const matches =
257
- /Debugger listening on (ws:\/\/127\.0\.0\.1:\d+\/[A-Za-z0-9-]+)[\r|\n]/.exec(
258
- stderrData
259
- );
260
- if (matches) {
261
- local.current?.stderr?.removeAllListeners("data");
262
- setInspectorUrl(matches[1]);
263
- }
264
- });
268
+ local.current.stdout?.on("data", (data: Buffer) => {
269
+ process.stdout.write(data);
270
+ });
265
271
 
266
- local.current.on("exit", (code) => {
267
- if (code) {
268
- logger.error(`Miniflare process exited with code ${code}`);
269
- }
270
- });
272
+ // parse the node inspector url (which may be received in chunks) from stderr
273
+ let stderrData = "";
274
+ let inspectorUrlFound = false;
275
+ local.current.stderr?.on("data", (data: Buffer) => {
276
+ if (!inspectorUrlFound) {
277
+ stderrData += data.toString();
278
+ const matches =
279
+ /Debugger listening on (ws:\/\/127\.0\.0\.1:\d+\/[A-Za-z0-9-]+)[\r|\n]/.exec(
280
+ stderrData
281
+ );
282
+ if (matches) {
283
+ inspectorUrlFound = true;
284
+ setInspectorUrl(matches[1]);
285
+ }
286
+ }
271
287
 
272
- local.current.on("error", (error: Error) => {
273
- logger.error(`Miniflare process failed to spawn`);
274
- logger.error(error);
275
- });
288
+ process.stderr.write(data);
289
+ });
276
290
 
277
- removeSignalExitListener.current = onExit((_code, _signal) => {
278
- logger.log("⎔ Shutting down local server.");
279
- local.current?.kill();
280
- local.current = undefined;
281
- });
282
- }
291
+ local.current.on("exit", (code) => {
292
+ if (code) {
293
+ logger.error(`Miniflare process exited with code ${code}`);
294
+ }
295
+ });
283
296
 
284
- startLocalWorker().catch((err) => {
285
- logger.error("local worker:", err);
286
- });
297
+ local.current.on("error", (error: Error) => {
298
+ logger.error(`Miniflare process failed to spawn`);
299
+ logger.error(error);
300
+ });
287
301
 
288
- return () => {
289
- abortController.abort();
290
- if (local.current) {
291
- logger.log("⎔ Shutting down local server.");
292
- local.current?.kill();
293
- local.current = undefined;
294
- removeSignalExitListener.current && removeSignalExitListener.current();
295
- removeSignalExitListener.current = undefined;
296
- }
297
- };
298
- }, [
299
- bundle,
300
- workerName,
301
- format,
302
- port,
303
- localProtocol,
304
- ip,
305
- bindings.durable_objects?.bindings,
306
- bindings.kv_namespaces,
307
- bindings.vars,
308
- bindings.services,
309
- compatibilityDate,
310
- compatibilityFlags,
311
- localPersistencePath,
312
- assetPaths,
313
- publicDirectory,
314
- rules,
315
- bindings.wasm_modules,
316
- bindings.text_blobs,
317
- bindings.data_blobs,
318
- crons,
319
- ]);
320
- return { inspectorUrl };
302
+ removeSignalExitListener.current = onExit((_code, _signal) => {
303
+ logger.log("⎔ Shutting down local server.");
304
+ local.current?.kill();
305
+ local.current = undefined;
306
+ });
307
+ }
308
+
309
+ startLocalWorker().catch((err) => {
310
+ logger.error("local worker:", err);
311
+ });
312
+
313
+ return () => {
314
+ abortController.abort();
315
+ if (local.current) {
316
+ logger.log("⎔ Shutting down local server.");
317
+ local.current?.kill();
318
+ local.current = undefined;
319
+ removeSignalExitListener.current && removeSignalExitListener.current();
320
+ removeSignalExitListener.current = undefined;
321
+ }
322
+ };
323
+ }, [
324
+ bundle,
325
+ workerName,
326
+ format,
327
+ port,
328
+ ip,
329
+ bindings.durable_objects?.bindings,
330
+ bindings.kv_namespaces,
331
+ bindings.vars,
332
+ bindings.services,
333
+ compatibilityDate,
334
+ compatibilityFlags,
335
+ localPersistencePath,
336
+ assetPaths,
337
+ isWorkersSite,
338
+ rules,
339
+ bindings.wasm_modules,
340
+ bindings.text_blobs,
341
+ bindings.data_blobs,
342
+ crons,
343
+ localProtocol,
344
+ localUpstream,
345
+ inspect,
346
+ logLevel,
347
+ onReady,
348
+ ]);
349
+ return { inspectorUrl };
321
350
  }