wrangler 0.0.0-e978986 → 0.0.0-e9e9872

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