wrangler 0.0.0-e3e3243 → 0.0.0-e41c7e41

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 (304) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -14
  3. package/bin/wrangler.js +154 -31
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +6258 -21
  6. package/package.json +191 -141
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +209 -0
  9. package/src/__tests__/api-devregistry.test.js +56 -0
  10. package/src/__tests__/api.test.ts +80 -0
  11. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  12. package/src/__tests__/config-cache.test.ts +42 -0
  13. package/src/__tests__/configuration.test.ts +3971 -2810
  14. package/src/__tests__/d1.test.ts +80 -0
  15. package/src/__tests__/delete.test.ts +230 -0
  16. package/src/__tests__/deployments.test.ts +91 -0
  17. package/src/__tests__/dev.test.tsx +1521 -526
  18. package/src/__tests__/dialogs.test.tsx +40 -0
  19. package/src/__tests__/generate.test.ts +235 -0
  20. package/src/__tests__/get-host-from-url.test.ts +16 -0
  21. package/src/__tests__/guess-worker-format.test.ts +74 -57
  22. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  23. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  24. package/src/__tests__/helpers/mock-bin.ts +20 -20
  25. package/src/__tests__/helpers/mock-cfetch.ts +219 -93
  26. package/src/__tests__/helpers/mock-console.ts +68 -32
  27. package/src/__tests__/helpers/mock-dialogs.ts +121 -33
  28. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  29. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  30. package/src/__tests__/helpers/mock-istty.ts +74 -0
  31. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  32. package/src/__tests__/helpers/mock-kv.ts +27 -27
  33. package/src/__tests__/helpers/mock-oauth-flow.ts +252 -0
  34. package/src/__tests__/helpers/mock-process.ts +39 -0
  35. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  36. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  37. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  38. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  39. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  40. package/src/__tests__/helpers/msw/handlers/oauth.ts +36 -0
  41. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  42. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  43. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  44. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  45. package/src/__tests__/helpers/msw/index.ts +21 -0
  46. package/src/__tests__/helpers/run-in-tmp.ts +28 -22
  47. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  48. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  49. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  50. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  51. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  52. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  53. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  54. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  55. package/src/__tests__/https-options.test.ts +144 -108
  56. package/src/__tests__/index.test.ts +248 -967
  57. package/src/__tests__/init.test.ts +2900 -0
  58. package/src/__tests__/jest.setup.ts +167 -34
  59. package/src/__tests__/kv.test.ts +1661 -1366
  60. package/src/__tests__/logger.test.ts +141 -0
  61. package/src/__tests__/metrics.test.ts +450 -0
  62. package/src/__tests__/middleware.scheduled.test.ts +135 -0
  63. package/src/__tests__/middleware.test.ts +726 -0
  64. package/src/__tests__/package-manager.test.ts +312 -165
  65. package/src/__tests__/pages-deployment-tail.test.ts +820 -0
  66. package/src/__tests__/pages.test.ts +2437 -77
  67. package/src/__tests__/parse.test.ts +253 -245
  68. package/src/__tests__/paths.test.ts +39 -0
  69. package/src/__tests__/publish.test.ts +7569 -3445
  70. package/src/__tests__/pubsub.test.ts +370 -0
  71. package/src/__tests__/queues.test.ts +371 -0
  72. package/src/__tests__/r2.test.ts +388 -193
  73. package/src/__tests__/route.test.ts +22 -22
  74. package/src/__tests__/secret.test.ts +626 -407
  75. package/src/__tests__/tail.test.ts +680 -442
  76. package/src/__tests__/test-old-node-version.js +31 -0
  77. package/src/__tests__/type-generation.test.ts +226 -0
  78. package/src/__tests__/user.test.ts +155 -0
  79. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  80. package/src/__tests__/validate-dev-props.test.ts +56 -0
  81. package/src/__tests__/version.test.ts +35 -0
  82. package/src/__tests__/whoami.test.tsx +266 -88
  83. package/src/__tests__/worker-namespace.test.ts +326 -0
  84. package/src/abort.d.ts +3 -0
  85. package/src/api/dev.ts +210 -0
  86. package/src/api/index.ts +2 -0
  87. package/src/bundle-reporter.tsx +68 -0
  88. package/src/bundle.ts +791 -123
  89. package/src/cfetch/index.ts +123 -67
  90. package/src/cfetch/internal.ts +216 -92
  91. package/src/cli.ts +23 -14
  92. package/src/config/README.md +1 -1
  93. package/src/config/config.ts +236 -179
  94. package/src/config/diagnostics.ts +61 -61
  95. package/src/config/environment.ts +506 -316
  96. package/src/config/index.ts +310 -38
  97. package/src/config/validation-helpers.ts +385 -342
  98. package/src/config/validation.ts +1970 -1099
  99. package/src/config-cache.ts +85 -0
  100. package/src/create-worker-preview.ts +240 -124
  101. package/src/create-worker-upload-form.ts +300 -201
  102. package/src/d1/backups.tsx +218 -0
  103. package/src/d1/constants.ts +2 -0
  104. package/src/d1/create.tsx +63 -0
  105. package/src/d1/delete.tsx +56 -0
  106. package/src/d1/execute.tsx +352 -0
  107. package/src/d1/formatTimeAgo.ts +14 -0
  108. package/src/d1/index.ts +99 -0
  109. package/src/d1/list.tsx +48 -0
  110. package/src/d1/migrations.tsx +446 -0
  111. package/src/d1/options.ts +22 -0
  112. package/src/d1/types.tsx +24 -0
  113. package/src/d1/utils.ts +48 -0
  114. package/src/delete.ts +108 -0
  115. package/src/deployments.ts +77 -0
  116. package/src/deprecated/index.ts +438 -0
  117. package/src/dev/dev-vars.ts +39 -0
  118. package/src/dev/dev.tsx +526 -328
  119. package/src/dev/get-local-persistence-path.tsx +31 -0
  120. package/src/dev/local.tsx +867 -213
  121. package/src/dev/remote.tsx +592 -192
  122. package/src/dev/start-server.ts +538 -0
  123. package/src/dev/use-esbuild.ts +184 -84
  124. package/src/dev/validate-dev-props.ts +31 -0
  125. package/src/dev-registry.tsx +201 -0
  126. package/src/dev.tsx +905 -0
  127. package/src/dialogs.tsx +150 -59
  128. package/src/dispatch-namespace.ts +207 -0
  129. package/src/durable.ts +102 -0
  130. package/src/entry.ts +260 -171
  131. package/src/environment-variables.ts +40 -23
  132. package/src/errors.ts +11 -0
  133. package/src/git-client.ts +124 -0
  134. package/src/global-wrangler-config-path.ts +26 -0
  135. package/src/https-options.ts +85 -80
  136. package/src/index.tsx +634 -2341
  137. package/src/init.ts +970 -0
  138. package/src/inspect.ts +707 -557
  139. package/src/intl-polyfill.d.ts +123 -123
  140. package/src/is-ci.ts +14 -0
  141. package/src/is-interactive.ts +16 -0
  142. package/src/kv/helpers.ts +430 -0
  143. package/src/kv/index.ts +596 -0
  144. package/src/logger.ts +92 -0
  145. package/src/metrics/index.ts +5 -0
  146. package/src/metrics/metrics-config.ts +239 -0
  147. package/src/metrics/metrics-dispatcher.ts +96 -0
  148. package/src/metrics/metrics-usage-headers.ts +24 -0
  149. package/src/metrics/send-event.ts +96 -0
  150. package/src/miniflare-cli/assets.ts +215 -0
  151. package/src/miniflare-cli/index.ts +206 -30
  152. package/src/miniflare-cli/request-context.ts +40 -0
  153. package/src/module-collection.ts +222 -213
  154. package/src/open-in-browser.ts +8 -16
  155. package/src/package-manager.ts +183 -90
  156. package/src/pages/build.tsx +270 -0
  157. package/src/pages/constants.ts +18 -0
  158. package/src/pages/deployment-tails.tsx +284 -0
  159. package/src/pages/deployments.tsx +84 -0
  160. package/src/pages/dev.tsx +708 -0
  161. package/src/pages/errors.ts +66 -0
  162. package/src/pages/functions/buildPlugin.ts +107 -0
  163. package/src/pages/functions/buildWorker.ts +166 -0
  164. package/src/pages/functions/filepath-routing.test.ts +234 -0
  165. package/src/pages/functions/filepath-routing.ts +189 -0
  166. package/src/pages/functions/identifiers.ts +78 -0
  167. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  168. package/src/pages/functions/routes-consolidation.ts +73 -0
  169. package/src/pages/functions/routes-transformation.test.ts +282 -0
  170. package/src/pages/functions/routes-transformation.ts +115 -0
  171. package/src/pages/functions/routes-validation.test.ts +403 -0
  172. package/src/pages/functions/routes-validation.ts +202 -0
  173. package/src/pages/functions/routes.ts +151 -0
  174. package/src/pages/functions.tsx +82 -0
  175. package/src/pages/hash.tsx +13 -0
  176. package/src/pages/index.tsx +102 -0
  177. package/src/pages/projects.tsx +169 -0
  178. package/src/pages/prompt-select-project.tsx +31 -0
  179. package/src/pages/publish.tsx +451 -0
  180. package/src/pages/types.ts +41 -0
  181. package/src/pages/upload.tsx +426 -0
  182. package/src/pages/utils.ts +23 -0
  183. package/src/parse.ts +224 -121
  184. package/src/paths.ts +53 -8
  185. package/src/proxy.ts +624 -375
  186. package/src/publish/index.ts +270 -0
  187. package/src/publish/publish.ts +1035 -0
  188. package/src/pubsub/index.ts +286 -0
  189. package/src/pubsub/pubsub-commands.tsx +624 -0
  190. package/src/queues/cli/commands/consumer/add.ts +71 -0
  191. package/src/queues/cli/commands/consumer/index.ts +22 -0
  192. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  193. package/src/queues/cli/commands/create.ts +25 -0
  194. package/src/queues/cli/commands/delete.ts +26 -0
  195. package/src/queues/cli/commands/index.ts +33 -0
  196. package/src/queues/cli/commands/list.ts +25 -0
  197. package/src/queues/client.ts +135 -0
  198. package/src/r2/constants.ts +4 -0
  199. package/src/r2/helpers.ts +131 -0
  200. package/src/r2/index.ts +289 -0
  201. package/src/routes.ts +140 -0
  202. package/src/secret/index.ts +378 -0
  203. package/src/selfsigned.d.ts +23 -23
  204. package/src/sites.tsx +283 -191
  205. package/src/tail/createTail.ts +379 -0
  206. package/src/tail/filters.ts +108 -110
  207. package/src/tail/index.ts +211 -296
  208. package/src/tail/printing.ts +92 -47
  209. package/src/type-generation.ts +173 -0
  210. package/src/update-check.ts +19 -0
  211. package/src/user/access.ts +69 -0
  212. package/src/user/choose-account.tsx +69 -0
  213. package/src/user/env-vars.ts +46 -0
  214. package/src/user/generate-auth-url.ts +33 -0
  215. package/src/user/generate-random-state.ts +16 -0
  216. package/src/user/index.ts +3 -0
  217. package/src/user/user.tsx +1192 -0
  218. package/src/utils/collectKeyValues.ts +14 -0
  219. package/src/whoami.tsx +142 -42
  220. package/src/worker.ts +182 -100
  221. package/src/yargs-types.ts +24 -0
  222. package/src/zones.ts +191 -0
  223. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  224. package/templates/checked-fetch.js +17 -0
  225. package/templates/d1-beta-facade.js +205 -0
  226. package/templates/first-party-worker-module-facade.ts +18 -0
  227. package/templates/format-dev-errors.ts +32 -0
  228. package/templates/gitignore +171 -0
  229. package/templates/init-tests/test-jest-new-worker.js +25 -0
  230. package/templates/init-tests/test-vitest-new-worker.js +26 -0
  231. package/templates/middleware/common.ts +62 -0
  232. package/templates/middleware/loader-modules.ts +84 -0
  233. package/templates/middleware/loader-sw.ts +227 -0
  234. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  235. package/templates/middleware/middleware-pretty-error.ts +40 -0
  236. package/templates/middleware/middleware-scheduled.ts +15 -0
  237. package/templates/new-worker-scheduled.js +17 -0
  238. package/templates/new-worker-scheduled.ts +32 -0
  239. package/templates/new-worker.js +3 -3
  240. package/templates/new-worker.ts +18 -3
  241. package/templates/no-op-worker.js +10 -0
  242. package/templates/pages-dev-pipeline.ts +28 -0
  243. package/templates/pages-dev-util.ts +52 -0
  244. package/templates/pages-shim.ts +9 -0
  245. package/templates/pages-template-plugin.ts +171 -0
  246. package/templates/pages-template-worker.ts +189 -0
  247. package/templates/serve-static-assets.ts +63 -0
  248. package/templates/service-bindings-module-facade.js +54 -0
  249. package/templates/service-bindings-sw-facade.js +42 -0
  250. package/templates/tsconfig.json +96 -95
  251. package/wrangler-dist/cli.d.ts +893 -0
  252. package/wrangler-dist/cli.js +116238 -94482
  253. package/miniflare-dist/index.mjs.map +0 -7
  254. package/pages/functions/buildWorker.ts +0 -62
  255. package/pages/functions/filepath-routing.test.ts +0 -223
  256. package/pages/functions/filepath-routing.ts +0 -181
  257. package/pages/functions/identifiers.ts +0 -78
  258. package/pages/functions/routes.ts +0 -151
  259. package/pages/functions/template-worker.ts +0 -130
  260. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  261. package/src/__tests__/logout.test.ts +0 -52
  262. package/src/__tests__/sentry.test.ts +0 -146
  263. package/src/kv.ts +0 -319
  264. package/src/miniflare-cli/enum-keys.ts +0 -17
  265. package/src/pages.tsx +0 -1061
  266. package/src/publish.ts +0 -350
  267. package/src/r2.ts +0 -50
  268. package/src/reporting.ts +0 -131
  269. package/src/user.tsx +0 -1127
  270. package/templates/static-asset-facade.js +0 -47
  271. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  272. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  273. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  274. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  275. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  276. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  277. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  278. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  279. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  280. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  281. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  282. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  283. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  284. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  285. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  286. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  287. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  288. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  289. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  290. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  291. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  292. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  293. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  294. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  295. package/vendor/wrangler-mime/LICENSE +0 -21
  296. package/vendor/wrangler-mime/Mime.js +0 -97
  297. package/vendor/wrangler-mime/README.md +0 -187
  298. package/vendor/wrangler-mime/cli.js +0 -46
  299. package/vendor/wrangler-mime/index.js +0 -4
  300. package/vendor/wrangler-mime/lite.js +0 -4
  301. package/vendor/wrangler-mime/package.json +0 -52
  302. package/vendor/wrangler-mime/types/other.js +0 -1
  303. package/vendor/wrangler-mime/types/standard.js +0 -1
  304. package/wrangler-dist/cli.js.map +0 -7
package/src/dev/local.tsx CHANGED
@@ -1,232 +1,886 @@
1
- import { spawn } from "node:child_process";
1
+ import assert from "node:assert";
2
+ import { fork } from "node:child_process";
2
3
  import { realpathSync } from "node:fs";
3
- import { writeFile } from "node:fs/promises";
4
+ import { readFile, writeFile } from "node:fs/promises";
4
5
  import path from "node:path";
6
+ import chalk from "chalk";
7
+ import { npxImport } from "npx-import";
5
8
  import { useState, useEffect, useRef } from "react";
6
9
  import onExit from "signal-exit";
10
+ import { performApiFetch } from "../cfetch/internal";
11
+ import { registerWorker } from "../dev-registry";
7
12
  import useInspector from "../inspect";
8
- import { DEFAULT_MODULE_RULES } from "../module-collection";
13
+ import { logger } from "../logger";
14
+ import {
15
+ DEFAULT_MODULE_RULES,
16
+ ModuleTypeToRuleType,
17
+ } from "../module-collection";
18
+ import { getBasePath } from "../paths";
9
19
  import { waitForPortToBeAvailable } from "../proxy";
20
+ import { requireAuth } from "../user";
10
21
  import type { Config } from "../config";
22
+ import type { WorkerRegistry } from "../dev-registry";
23
+ import type { LoggerLevel } from "../logger";
24
+ import type { EnablePagesAssetsServiceBindingOptions } from "../miniflare-cli";
11
25
  import type { AssetPaths } from "../sites";
12
- import type { CfWorkerInit, CfScriptFormat } from "../worker";
26
+ import type {
27
+ CfWorkerInit,
28
+ CfScriptFormat,
29
+ CfWasmModuleBindings,
30
+ CfTextBlobBindings,
31
+ CfDataBlobBindings,
32
+ CfDurableObject,
33
+ CfKvNamespace,
34
+ CfR2Bucket,
35
+ CfVars,
36
+ CfQueue,
37
+ CfD1Database,
38
+ } from "../worker";
13
39
  import type { EsbuildBundle } from "./use-esbuild";
40
+ import type {
41
+ Miniflare as Miniflare3Type,
42
+ MiniflareOptions as Miniflare3Options,
43
+ Log as Miniflare3LogType,
44
+ CloudflareFetch,
45
+ } from "@miniflare/tre";
14
46
  import type { MiniflareOptions } from "miniflare";
47
+ import type { ChildProcess } from "node:child_process";
48
+ import type { RequestInit } from "undici";
15
49
 
16
- interface LocalProps {
17
- name: undefined | string;
18
- bundle: EsbuildBundle | undefined;
19
- format: CfScriptFormat | undefined;
20
- compatibilityDate: string | undefined;
21
- compatibilityFlags: undefined | string[];
22
- bindings: CfWorkerInit["bindings"];
23
- assetPaths: undefined | AssetPaths;
24
- public: undefined | string;
25
- port: number;
26
- ip: string;
27
- rules: Config["rules"];
28
- inspectorPort: number;
29
- enableLocalPersistence: boolean;
50
+ export interface LocalProps {
51
+ name: string | undefined;
52
+ bundle: EsbuildBundle | undefined;
53
+ format: CfScriptFormat | undefined;
54
+ compatibilityDate: string;
55
+ compatibilityFlags: string[] | undefined;
56
+ usageModel: "bundled" | "unbound" | undefined;
57
+ bindings: CfWorkerInit["bindings"];
58
+ workerDefinitions: WorkerRegistry | undefined;
59
+ assetPaths: AssetPaths | undefined;
60
+ port: number;
61
+ ip: string;
62
+ rules: Config["rules"];
63
+ inspectorPort: number;
64
+ localPersistencePath: string | null;
65
+ liveReload: boolean;
66
+ crons: Config["triggers"]["crons"];
67
+ queueConsumers: Config["queues"]["consumers"];
68
+ localProtocol: "http" | "https";
69
+ localUpstream: string | undefined;
70
+ inspect: boolean;
71
+ onReady: ((ip: string, port: number) => void) | undefined;
72
+ logPrefix?: string;
73
+ enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
74
+ testScheduled?: boolean;
75
+ experimentalLocal: boolean | undefined;
76
+ accountId: string | undefined; // Account ID? In local mode??? :exploding_head:
77
+ experimentalLocalRemoteKv: boolean | undefined;
30
78
  }
31
79
 
80
+ type InspectorJSON = {
81
+ id: string;
82
+ title: string;
83
+ type: "node";
84
+ description: string;
85
+ webSocketDebuggerUrl: string;
86
+ devtoolsFrontendUrl: string;
87
+ devtoolsFrontendUrlCompat: string;
88
+ faviconUrl: string;
89
+ url: string;
90
+ }[];
91
+
32
92
  export function Local(props: LocalProps) {
33
- const { inspectorUrl } = useLocalWorker(props);
34
- useInspector({
35
- inspectorUrl,
36
- port: props.inspectorPort,
37
- logToTerminal: false,
38
- });
39
- return null;
93
+ const { inspectorUrl } = useLocalWorker(props);
94
+ useInspector({
95
+ inspectorUrl,
96
+ port: props.inspectorPort,
97
+ logToTerminal: props.experimentalLocal ?? false,
98
+ });
99
+ return null;
40
100
  }
41
101
 
42
102
  function useLocalWorker({
43
- name: workerName,
44
- bundle,
45
- format,
46
- compatibilityDate,
47
- compatibilityFlags,
48
- bindings,
49
- assetPaths,
50
- public: publicDirectory,
51
- port,
52
- rules,
53
- enableLocalPersistence,
54
- ip,
103
+ name: workerName,
104
+ bundle,
105
+ format,
106
+ compatibilityDate,
107
+ compatibilityFlags,
108
+ usageModel,
109
+ bindings,
110
+ workerDefinitions,
111
+ assetPaths,
112
+ port,
113
+ inspectorPort,
114
+ rules,
115
+ localPersistencePath,
116
+ liveReload,
117
+ ip,
118
+ crons,
119
+ queueConsumers,
120
+ localProtocol,
121
+ localUpstream,
122
+ inspect,
123
+ onReady,
124
+ logPrefix,
125
+ enablePagesAssetsServiceBinding,
126
+ experimentalLocal,
127
+ accountId,
128
+ experimentalLocalRemoteKv,
55
129
  }: LocalProps) {
56
- // TODO: pass vars via command line
57
- const local = useRef<ReturnType<typeof spawn>>();
58
- const removeSignalExitListener = useRef<() => void>();
59
- const [inspectorUrl, setInspectorUrl] = useState<string | undefined>();
60
- useEffect(() => {
61
- async function startLocalWorker() {
62
- if (!bundle || !format) return;
63
-
64
- // port for the worker
65
- await waitForPortToBeAvailable(port, { retryPeriod: 200, timeout: 2000 });
66
-
67
- if (publicDirectory) {
68
- throw new Error(
69
- '⎔ A "public" folder is not yet supported in local mode.'
70
- );
71
- }
72
-
73
- // In local mode, we want to copy all referenced modules into
74
- // the output bundle directory before starting up
75
- for (const module of bundle.modules) {
76
- await writeFile(
77
- path.join(path.dirname(bundle.path), module.name),
78
- module.content
79
- );
80
- }
81
-
82
- const scriptPath = realpathSync(bundle.path);
83
-
84
- const wasmBindings = { ...bindings.wasm_modules };
85
- if (format === "service-worker") {
86
- for (const { type, name } of bundle.modules) {
87
- if (type === "compiled-wasm") {
88
- // In service-worker format, .wasm modules are referenced by global identifiers,
89
- // so we convert it here.
90
- // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
91
- // characters with an underscore.
92
- const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
93
- wasmBindings[identifier] = name;
94
- }
95
- }
96
- }
97
-
98
- const options: MiniflareOptions = {
99
- name: workerName,
100
- port,
101
- scriptPath,
102
- host: ip,
103
- modules: format === "modules",
104
- modulesRules: (rules || [])
105
- .concat(DEFAULT_MODULE_RULES)
106
- .map(({ type, globs: include, fallthrough }) => ({
107
- type,
108
- include,
109
- fallthrough,
110
- })),
111
- compatibilityDate,
112
- compatibilityFlags,
113
- kvNamespaces: bindings.kv_namespaces?.map((kv) => kv.binding),
114
- kvPersist: enableLocalPersistence,
115
- durableObjects: Object.fromEntries(
116
- (bindings.durable_objects?.bindings ?? []).map<[string, string]>(
117
- (value) => [value.name, value.class_name]
118
- )
119
- ),
120
- durableObjectsPersist: enableLocalPersistence,
121
- cachePersist: enableLocalPersistence,
122
- sitePath: assetPaths?.assetDirectory
123
- ? path.join(assetPaths.baseDirectory, assetPaths.assetDirectory)
124
- : undefined,
125
- siteInclude: assetPaths?.includePatterns.length
126
- ? assetPaths?.includePatterns
127
- : undefined,
128
- siteExclude: assetPaths?.excludePatterns.length
129
- ? assetPaths.excludePatterns
130
- : undefined,
131
- bindings: bindings.vars,
132
- wasmBindings,
133
- sourceMap: true,
134
- logUnhandledRejections: true,
135
- };
136
-
137
- // The path to the Miniflare CLI assumes that this file is being run from
138
- // `wrangler-dist` and that the CLI is found in `miniflare-dist`.
139
- // If either of those paths change this line needs updating.
140
- const miniflareCLIPath = path.resolve(
141
- __dirname,
142
- "../miniflare-dist/index.mjs"
143
- );
144
- const optionsArg = JSON.stringify(options, null);
145
-
146
- console.log("⎔ Starting a local server...");
147
- local.current = spawn(
148
- "node",
149
- [
150
- "--experimental-vm-modules", // ensures that Miniflare can run ESM Workers
151
- "--no-warnings", // hide annoying Node warnings
152
- "--inspect", // start Miniflare listening for a debugger to attach
153
- miniflareCLIPath,
154
- optionsArg,
155
- // "--log=VERBOSE", // uncomment this to Miniflare to log "everything"!
156
- ],
157
- {
158
- cwd: path.dirname(scriptPath),
159
- }
160
- );
161
-
162
- local.current.on("close", (code) => {
163
- if (code) {
164
- console.log(`Miniflare process exited with code ${code}`);
165
- }
166
- });
167
-
168
- local.current.stdout?.on("data", (data: Buffer) => {
169
- console.log(`${data.toString()}`);
170
- });
171
-
172
- local.current.stderr?.on("data", (data: Buffer) => {
173
- console.error(`${data.toString()}`);
174
- const matches =
175
- /Debugger listening on (ws:\/\/127\.0\.0\.1:\d+\/[A-Za-z0-9-]+)/.exec(
176
- data.toString()
177
- );
178
- if (matches) {
179
- setInspectorUrl(matches[1]);
180
- }
181
- });
182
-
183
- local.current.on("exit", (code) => {
184
- if (code) {
185
- console.error(`Miniflare process exited with code ${code}`);
186
- }
187
- });
188
-
189
- local.current.on("error", (error: Error) => {
190
- console.error(`Miniflare process failed to spawn`);
191
- console.error(error);
192
- });
193
-
194
- removeSignalExitListener.current = onExit((_code, _signal) => {
195
- console.log("⎔ Shutting down local server.");
196
- local.current?.kill();
197
- local.current = undefined;
198
- });
199
- }
200
-
201
- startLocalWorker().catch((err) => {
202
- console.error("local worker:", err);
203
- });
204
-
205
- return () => {
206
- if (local.current) {
207
- console.log("⎔ Shutting down local server.");
208
- local.current?.kill();
209
- local.current = undefined;
210
- removeSignalExitListener.current && removeSignalExitListener.current();
211
- removeSignalExitListener.current = undefined;
212
- }
213
- };
214
- }, [
215
- bundle,
216
- workerName,
217
- format,
218
- port,
219
- ip,
220
- bindings.durable_objects?.bindings,
221
- bindings.kv_namespaces,
222
- bindings.vars,
223
- compatibilityDate,
224
- compatibilityFlags,
225
- enableLocalPersistence,
226
- assetPaths,
227
- publicDirectory,
228
- rules,
229
- bindings.wasm_modules,
230
- ]);
231
- return { inspectorUrl };
130
+ // TODO: pass vars via command line
131
+ const local = useRef<ChildProcess>();
132
+ const experimentalLocalRef = useRef<Miniflare3Type>();
133
+ const removeSignalExitListener = useRef<() => void>();
134
+ const removeExperimentalLocalSignalExitListener = useRef<() => void>();
135
+ const [inspectorUrl, setInspectorUrl] = useState<string | undefined>();
136
+
137
+ useEffect(() => {
138
+ if (bindings.services && bindings.services.length > 0) {
139
+ logger.warn(
140
+ "⎔ Support for service bindings in local mode is experimental and may change."
141
+ );
142
+ }
143
+ }, [bindings.services]);
144
+
145
+ useEffect(() => {
146
+ const externalDurableObjects = (
147
+ bindings.durable_objects?.bindings || []
148
+ ).filter((binding) => binding.script_name);
149
+
150
+ if (externalDurableObjects.length > 0) {
151
+ logger.warn(
152
+ "⎔ Support for external Durable Objects in local mode is experimental and may change."
153
+ );
154
+ }
155
+ }, [bindings.durable_objects?.bindings]);
156
+
157
+ useEffect(() => {
158
+ const abortController = new AbortController();
159
+ async function startLocalWorker() {
160
+ if (!bundle || !format) return;
161
+
162
+ // port for the worker
163
+ await waitForPortToBeAvailable(port, {
164
+ retryPeriod: 200,
165
+ timeout: 2000,
166
+ abortSignal: abortController.signal,
167
+ });
168
+
169
+ // In local mode, we want to copy all referenced modules into
170
+ // the output bundle directory before starting up
171
+ for (const module of bundle.modules) {
172
+ await writeFile(
173
+ path.join(path.dirname(bundle.path), module.name),
174
+ module.content
175
+ );
176
+ }
177
+
178
+ const scriptPath = realpathSync(bundle.path);
179
+
180
+ const upstream =
181
+ typeof localUpstream === "string"
182
+ ? `${localProtocol}://${localUpstream}`
183
+ : undefined;
184
+
185
+ const {
186
+ externalDurableObjects,
187
+ internalDurableObjects,
188
+ wasmBindings,
189
+ textBlobBindings,
190
+ dataBlobBindings,
191
+ } = setupBindings({
192
+ wasm_modules: bindings.wasm_modules,
193
+ text_blobs: bindings.text_blobs,
194
+ data_blobs: bindings.data_blobs,
195
+ durable_objects: bindings.durable_objects,
196
+ format,
197
+ bundle,
198
+ });
199
+
200
+ const { forkOptions, miniflareCLIPath, options } = setupMiniflareOptions({
201
+ workerName,
202
+ port,
203
+ scriptPath,
204
+ localProtocol,
205
+ ip,
206
+ format,
207
+ rules,
208
+ compatibilityDate,
209
+ compatibilityFlags,
210
+ usageModel,
211
+ kv_namespaces: bindings?.kv_namespaces,
212
+ r2_buckets: bindings?.r2_buckets,
213
+ queueBindings: bindings?.queues,
214
+ queueConsumers: queueConsumers,
215
+ d1_databases: bindings?.d1_databases,
216
+ internalDurableObjects,
217
+ externalDurableObjects,
218
+ localPersistencePath,
219
+ liveReload,
220
+ assetPaths,
221
+ vars: bindings?.vars,
222
+ wasmBindings,
223
+ textBlobBindings,
224
+ dataBlobBindings,
225
+ crons,
226
+ upstream,
227
+ logPrefix,
228
+ workerDefinitions,
229
+ enablePagesAssetsServiceBinding,
230
+ });
231
+
232
+ if (experimentalLocal) {
233
+ const log = await buildMiniflare3Logger(logPrefix);
234
+ const mf3Options = await transformMf2OptionsToMf3Options({
235
+ miniflare2Options: options,
236
+ format,
237
+ bundle,
238
+ log,
239
+ kvNamespaces: bindings?.kv_namespaces,
240
+ r2Buckets: bindings?.r2_buckets,
241
+ authenticatedAccountId: accountId,
242
+ kvRemote: experimentalLocalRemoteKv,
243
+ inspectorPort,
244
+ });
245
+
246
+ const current = experimentalLocalRef.current;
247
+
248
+ if (current === undefined) {
249
+ // If we don't have an active Miniflare instance, create a new one
250
+ const { Miniflare } = await getMiniflare3();
251
+ if (abortController.signal.aborted) return;
252
+ const mf = new Miniflare(mf3Options);
253
+ experimentalLocalRef.current = mf;
254
+ removeExperimentalLocalSignalExitListener.current = onExit(() => {
255
+ logger.log("⎔ Shutting down experimental local server.");
256
+ mf.dispose();
257
+ experimentalLocalRef.current = undefined;
258
+ });
259
+ await mf.ready;
260
+ } else {
261
+ // Otherwise, reuse the existing instance with its loopback server
262
+ // and just update the options
263
+ if (abortController.signal.aborted) return;
264
+ logger.log("⎔ Reloading experimental local server.");
265
+ await current.setOptions(mf3Options);
266
+ }
267
+
268
+ try {
269
+ // fetch the inspector JSON response from the DevTools Inspector protocol
270
+ const inspectorJSONArr = (await (
271
+ await fetch(`http://127.0.0.1:${inspectorPort}/json`)
272
+ ).json()) as InspectorJSON;
273
+
274
+ const foundInspectorURL = inspectorJSONArr?.find((inspectorJSON) =>
275
+ inspectorJSON.id.startsWith("core:user")
276
+ )?.webSocketDebuggerUrl;
277
+ if (foundInspectorURL === undefined) {
278
+ setInspectorUrl(undefined);
279
+ } else {
280
+ const url = new URL(foundInspectorURL);
281
+ // Force inspector URL to be different on each reload so `useEffect`
282
+ // in `useInspector` is re-run to connect to newly restarted
283
+ // `workerd` server when updating options. Can't use a query param
284
+ // here as that seems to cause an infinite connection loop, can't
285
+ // use a hash as those are forbidden by `ws`, so username it is.
286
+ url.username = `${Date.now()}-${Math.floor(
287
+ Math.random() * Number.MAX_SAFE_INTEGER
288
+ )}`;
289
+ setInspectorUrl(url.toString());
290
+ }
291
+ } catch (error: unknown) {
292
+ logger.error("Error attempting to retrieve Debugger URL:", error);
293
+ }
294
+
295
+ return;
296
+ }
297
+
298
+ const nodeOptions = setupNodeOptions({ inspect, ip, inspectorPort });
299
+ logger.log("⎔ Starting a local server...");
300
+
301
+ const child = (local.current = fork(miniflareCLIPath, forkOptions, {
302
+ cwd: path.dirname(scriptPath),
303
+ execArgv: nodeOptions,
304
+ stdio: "pipe",
305
+ env: {
306
+ ...process.env,
307
+ FORCE_COLOR: chalk.supportsColor.hasBasic ? "1" : undefined,
308
+ },
309
+ }));
310
+
311
+ child.on("message", async (messageString) => {
312
+ const message = JSON.parse(messageString as string);
313
+ if (message.ready) {
314
+ // Let's register our presence in the dev registry
315
+ if (workerName) {
316
+ await registerWorker(workerName, {
317
+ protocol: localProtocol,
318
+ mode: "local",
319
+ port,
320
+ host: ip,
321
+ durableObjects: internalDurableObjects.map((binding) => ({
322
+ name: binding.name,
323
+ className: binding.class_name,
324
+ })),
325
+ ...(message.durableObjectsPort
326
+ ? {
327
+ durableObjectsHost: ip,
328
+ durableObjectsPort: message.durableObjectsPort,
329
+ }
330
+ : {}),
331
+ });
332
+ }
333
+ onReady?.(ip, message.mfPort);
334
+ }
335
+ });
336
+
337
+ child.on("close", (code) => {
338
+ if (code) {
339
+ logger.log(`Miniflare process exited with code ${code}`);
340
+ }
341
+ });
342
+
343
+ child.stdout?.on("data", (data: Buffer) => {
344
+ process.stdout.write(data);
345
+ });
346
+
347
+ // parse the node inspector url (which may be received in chunks) from stderr
348
+ let stderrData = "";
349
+ let inspectorUrlFound = false;
350
+ child.stderr?.on("data", (data: Buffer) => {
351
+ if (!inspectorUrlFound) {
352
+ stderrData += data.toString();
353
+ const matches =
354
+ /Debugger listening on (ws:\/\/127\.0\.0\.1:\d+\/[A-Za-z0-9-]+)[\r|\n]/.exec(
355
+ stderrData
356
+ );
357
+ if (matches) {
358
+ inspectorUrlFound = true;
359
+ setInspectorUrl(matches[1]);
360
+ }
361
+ }
362
+
363
+ process.stderr.write(data);
364
+ });
365
+
366
+ child.on("exit", (code) => {
367
+ if (code) {
368
+ logger.error(`Miniflare process exited with code ${code}`);
369
+ }
370
+ });
371
+
372
+ child.on("error", (error: Error) => {
373
+ logger.error(`Miniflare process failed to spawn`);
374
+ logger.error(error);
375
+ });
376
+
377
+ removeSignalExitListener.current = onExit((_code, _signal) => {
378
+ logger.log("⎔ Shutting down local server.");
379
+ child.kill();
380
+ local.current = undefined;
381
+ });
382
+ }
383
+
384
+ startLocalWorker().catch((err) => {
385
+ logger.error("local worker:", err);
386
+ });
387
+
388
+ return () => {
389
+ abortController.abort();
390
+ if (local.current) {
391
+ logger.log("⎔ Shutting down local server.");
392
+ local.current?.kill();
393
+ local.current = undefined;
394
+ }
395
+ removeSignalExitListener.current?.();
396
+ removeSignalExitListener.current = undefined;
397
+ };
398
+ }, [
399
+ bundle,
400
+ workerName,
401
+ format,
402
+ port,
403
+ inspectorPort,
404
+ ip,
405
+ queueConsumers,
406
+ bindings.queues,
407
+ bindings.durable_objects,
408
+ bindings.kv_namespaces,
409
+ bindings.r2_buckets,
410
+ bindings.d1_databases,
411
+ bindings.vars,
412
+ bindings.services,
413
+ workerDefinitions,
414
+ compatibilityDate,
415
+ compatibilityFlags,
416
+ usageModel,
417
+ localPersistencePath,
418
+ liveReload,
419
+ assetPaths,
420
+ rules,
421
+ bindings.wasm_modules,
422
+ bindings.text_blobs,
423
+ bindings.data_blobs,
424
+ crons,
425
+ localProtocol,
426
+ localUpstream,
427
+ inspect,
428
+ logPrefix,
429
+ onReady,
430
+ enablePagesAssetsServiceBinding,
431
+ experimentalLocal,
432
+ accountId,
433
+ experimentalLocalRemoteKv,
434
+ ]);
435
+
436
+ // Rather than disposing the Miniflare instance on every reload, only dispose
437
+ // it if local mode is disabled and the `Local` component is unmounted. This
438
+ // allows us to use the more efficient `Miniflare#setOptions` on reload which
439
+ // retains internal state (e.g. the Miniflare loopback server).
440
+ useEffect(
441
+ () => () => {
442
+ if (experimentalLocalRef.current) {
443
+ logger.log("⎔ Shutting down experimental local server.");
444
+ // Initialisation errors are also thrown asynchronously by dispose().
445
+ // The catch() above should've caught them though.
446
+ experimentalLocalRef.current?.dispose().catch(() => {});
447
+ experimentalLocalRef.current = undefined;
448
+ }
449
+ removeExperimentalLocalSignalExitListener.current?.();
450
+ removeExperimentalLocalSignalExitListener.current = undefined;
451
+ },
452
+ []
453
+ );
454
+
455
+ return { inspectorUrl };
456
+ }
457
+
458
+ interface SetupBindingsProps {
459
+ wasm_modules: CfWasmModuleBindings | undefined;
460
+ text_blobs: CfTextBlobBindings | undefined;
461
+ data_blobs: CfDataBlobBindings | undefined;
462
+ durable_objects: { bindings: CfDurableObject[] } | undefined;
463
+ bundle: EsbuildBundle;
464
+ format: CfScriptFormat;
465
+ }
466
+
467
+ export function setupBindings({
468
+ wasm_modules,
469
+ text_blobs,
470
+ data_blobs,
471
+ durable_objects,
472
+ format,
473
+ bundle,
474
+ }: SetupBindingsProps) {
475
+ // the wasm_modules/text_blobs/data_blobs bindings are
476
+ // relative to process.cwd(), but the actual worker bundle
477
+ // is in the temp output directory; so we rewrite the paths to be absolute,
478
+ // letting miniflare resolve them correctly
479
+
480
+ // wasm
481
+ const wasmBindings: Record<string, string> = {};
482
+ for (const [name, filePath] of Object.entries(wasm_modules || {})) {
483
+ wasmBindings[name] = path.join(process.cwd(), filePath);
484
+ }
485
+
486
+ // text
487
+ const textBlobBindings: Record<string, string> = {};
488
+ for (const [name, filePath] of Object.entries(text_blobs || {})) {
489
+ textBlobBindings[name] = path.join(process.cwd(), filePath);
490
+ }
491
+
492
+ // data
493
+ const dataBlobBindings: Record<string, string> = {};
494
+ for (const [name, filePath] of Object.entries(data_blobs || {})) {
495
+ dataBlobBindings[name] = path.join(process.cwd(), filePath);
496
+ }
497
+
498
+ if (format === "service-worker") {
499
+ for (const { type, name } of bundle.modules) {
500
+ if (type === "compiled-wasm") {
501
+ // In service-worker format, .wasm modules are referenced by global identifiers,
502
+ // so we convert it here.
503
+ // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
504
+ // characters with an underscore.
505
+ const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
506
+ wasmBindings[identifier] = name;
507
+ } else if (type === "text") {
508
+ // In service-worker format, text modules are referenced by global identifiers,
509
+ // so we convert it here.
510
+ // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
511
+ // characters with an underscore.
512
+ const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
513
+ textBlobBindings[identifier] = name;
514
+ } else if (type === "buffer") {
515
+ // In service-worker format, data blobs are referenced by global identifiers,
516
+ // so we convert it here.
517
+ // This identifier has to be a valid JS identifier, so we replace all non alphanumeric
518
+ // characters with an underscore.
519
+ const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
520
+ dataBlobBindings[identifier] = name;
521
+ }
522
+ }
523
+ }
524
+
525
+ const internalDurableObjects = (durable_objects?.bindings || []).filter(
526
+ (binding) => !binding.script_name
527
+ );
528
+ const externalDurableObjects = (durable_objects?.bindings || []).filter(
529
+ (binding) => binding.script_name
530
+ );
531
+ return {
532
+ internalDurableObjects,
533
+ externalDurableObjects,
534
+ wasmBindings,
535
+ textBlobBindings,
536
+ dataBlobBindings,
537
+ };
538
+ }
539
+
540
+ interface SetupMiniflareOptionsProps {
541
+ workerName: string | undefined;
542
+ port: number;
543
+ scriptPath: string;
544
+ localProtocol: "http" | "https";
545
+ ip: string;
546
+ format: CfScriptFormat;
547
+ rules: Config["rules"];
548
+ compatibilityDate: string;
549
+ compatibilityFlags: string[] | undefined;
550
+ usageModel: "bundled" | "unbound" | undefined;
551
+ kv_namespaces: CfKvNamespace[] | undefined;
552
+ queueBindings: CfQueue[] | undefined;
553
+ queueConsumers: Config["queues"]["consumers"];
554
+ r2_buckets: CfR2Bucket[] | undefined;
555
+ d1_databases: CfD1Database[] | undefined;
556
+ internalDurableObjects: CfDurableObject[];
557
+ externalDurableObjects: CfDurableObject[];
558
+ localPersistencePath: string | null;
559
+ liveReload: boolean;
560
+ assetPaths: AssetPaths | undefined;
561
+ vars: CfVars | undefined;
562
+ wasmBindings: Record<string, string>;
563
+ textBlobBindings: Record<string, string>;
564
+ dataBlobBindings: Record<string, string>;
565
+ crons: Config["triggers"]["crons"];
566
+ upstream: string | undefined;
567
+ logPrefix: string | undefined;
568
+ workerDefinitions: WorkerRegistry | undefined;
569
+ enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
570
+ }
571
+
572
+ export function setupMiniflareOptions({
573
+ workerName,
574
+ port,
575
+ scriptPath,
576
+ localProtocol,
577
+ ip,
578
+ format,
579
+ rules,
580
+ compatibilityDate,
581
+ compatibilityFlags,
582
+ usageModel,
583
+ kv_namespaces,
584
+ queueBindings,
585
+ queueConsumers,
586
+ r2_buckets,
587
+ d1_databases,
588
+ internalDurableObjects,
589
+ externalDurableObjects,
590
+ localPersistencePath,
591
+ liveReload,
592
+ assetPaths,
593
+ vars,
594
+ wasmBindings,
595
+ textBlobBindings,
596
+ dataBlobBindings,
597
+ crons,
598
+ upstream,
599
+ logPrefix,
600
+ workerDefinitions,
601
+ enablePagesAssetsServiceBinding,
602
+ }: SetupMiniflareOptionsProps): {
603
+ miniflareCLIPath: string;
604
+ forkOptions: string[];
605
+ options: MiniflareOptions;
606
+ } {
607
+ // It's now getting _really_ messy now with Pages ASSETS binding outside and the external Durable Objects inside.
608
+ const options: MiniflareOptions = {
609
+ name: workerName,
610
+ port,
611
+ scriptPath,
612
+ https: localProtocol === "https",
613
+ host: ip,
614
+ modules: format === "modules",
615
+ modulesRules: (rules || [])
616
+ .concat(DEFAULT_MODULE_RULES)
617
+ .map(({ type, globs: include, fallthrough }) => ({
618
+ type,
619
+ include,
620
+ fallthrough,
621
+ })),
622
+ compatibilityDate,
623
+ compatibilityFlags,
624
+ usageModel,
625
+ kvNamespaces: kv_namespaces?.map((kv) => kv.binding),
626
+ queueBindings: queueBindings?.map((queue) => {
627
+ return { name: queue.binding, queueName: queue.queue_name };
628
+ }),
629
+ queueConsumers: queueConsumers?.map((consumer) => {
630
+ const waitMs = consumer.max_batch_timeout
631
+ ? 1000 * consumer.max_batch_timeout
632
+ : undefined;
633
+ return {
634
+ queueName: consumer.queue,
635
+ maxBatchSize: consumer.max_batch_size,
636
+ maxWaitMs: waitMs,
637
+ maxRetries: consumer.max_retries,
638
+ deadLetterQueue: consumer.dead_letter_queue,
639
+ };
640
+ }),
641
+ r2Buckets: r2_buckets?.map((r2) => r2.binding),
642
+ durableObjects: Object.fromEntries(
643
+ internalDurableObjects.map((binding) => [
644
+ binding.name,
645
+ binding.class_name,
646
+ ])
647
+ ),
648
+ externalDurableObjects: Object.fromEntries(
649
+ externalDurableObjects
650
+ .map((binding) => {
651
+ const service =
652
+ workerDefinitions &&
653
+ workerDefinitions[binding.script_name as string];
654
+ if (!service) return [binding.name, undefined];
655
+
656
+ const name = service.durableObjects.find(
657
+ (durableObject) => durableObject.className === binding.class_name
658
+ )?.name;
659
+ if (!name) return [binding.name, undefined];
660
+
661
+ return [
662
+ binding.name,
663
+ {
664
+ name,
665
+ host: service.durableObjectsHost,
666
+ port: service.durableObjectsPort,
667
+ },
668
+ ];
669
+ })
670
+ .filter(([_, details]) => !!details)
671
+ ),
672
+ d1Databases: d1_databases?.map((db) => db.binding),
673
+ ...(localPersistencePath
674
+ ? {
675
+ cachePersist: path.join(localPersistencePath, "cache"),
676
+ durableObjectsPersist: path.join(localPersistencePath, "do"),
677
+ kvPersist: path.join(localPersistencePath, "kv"),
678
+ r2Persist: path.join(localPersistencePath, "r2"),
679
+ d1Persist: path.join(localPersistencePath, "d1"),
680
+ }
681
+ : {
682
+ // We mark these as true, so that they'll
683
+ // persist in the temp directory.
684
+ // This means they'll persist across a dev session,
685
+ // even if we change source and reload,
686
+ // and be deleted when the dev session ends
687
+ cachePersist: true,
688
+ durableObjectsPersist: true,
689
+ kvPersist: true,
690
+ r2Persist: true,
691
+ }),
692
+
693
+ liveReload,
694
+ sitePath: assetPaths?.assetDirectory
695
+ ? path.join(assetPaths.baseDirectory, assetPaths.assetDirectory)
696
+ : undefined,
697
+ siteInclude: assetPaths?.includePatterns.length
698
+ ? assetPaths?.includePatterns
699
+ : undefined,
700
+ siteExclude: assetPaths?.excludePatterns.length
701
+ ? assetPaths.excludePatterns
702
+ : undefined,
703
+ bindings: vars,
704
+ wasmBindings,
705
+ textBlobBindings,
706
+ dataBlobBindings,
707
+ sourceMap: true,
708
+ logUnhandledRejections: true,
709
+ crons,
710
+ upstream,
711
+ logLevel: logger.loggerLevel,
712
+ logOptions: logPrefix ? { prefix: logPrefix } : undefined,
713
+ enablePagesAssetsServiceBinding,
714
+ };
715
+ // The path to the Miniflare CLI assumes that this file is being run from
716
+ // `wrangler-dist` and that the CLI is found in `miniflare-dist`.
717
+ // If either of those paths change this line needs updating.
718
+ const miniflareCLIPath = path.resolve(
719
+ getBasePath(),
720
+ "miniflare-dist/index.mjs"
721
+ );
722
+ const miniflareOptions = JSON.stringify(options, null);
723
+ const forkOptions = [miniflareOptions];
724
+ if (enablePagesAssetsServiceBinding) {
725
+ forkOptions.push(JSON.stringify(enablePagesAssetsServiceBinding));
726
+ }
727
+ return { miniflareCLIPath, forkOptions, options };
728
+ }
729
+
730
+ export function setupNodeOptions({
731
+ inspect,
732
+ ip,
733
+ inspectorPort,
734
+ }: {
735
+ inspect: boolean;
736
+ ip: string;
737
+ inspectorPort: number;
738
+ }) {
739
+ const nodeOptions = [
740
+ "--experimental-vm-modules", // ensures that Miniflare can run ESM Workers
741
+ "--no-warnings", // hide annoying Node warnings
742
+ // "--log=VERBOSE", // uncomment this to Miniflare to log "everything"!
743
+ ];
744
+ if (inspect) {
745
+ nodeOptions.push("--inspect=" + `${ip}:${inspectorPort}`); // start Miniflare listening for a debugger to attach
746
+ }
747
+ return nodeOptions;
748
+ }
749
+
750
+ export interface SetupMiniflare3Options {
751
+ // Regular Miniflare 2 options to transform
752
+ miniflare2Options: MiniflareOptions;
753
+ // Miniflare 3 requires all modules to be manually specified
754
+ format: CfScriptFormat;
755
+ bundle: EsbuildBundle;
756
+
757
+ // Miniflare's logger
758
+ log: Miniflare3LogType;
759
+
760
+ // Miniflare 3 accepts namespace/bucket names in addition to binding names.
761
+ // This means multiple workers persisting to the same location can have
762
+ // different binding names for the same namespace/bucket. Therefore, we need
763
+ // the full KV/R2 arrays. This is also required for remote KV storage, as
764
+ // we need actual namespace IDs to connect to.
765
+ kvNamespaces: CfKvNamespace[] | undefined;
766
+ r2Buckets: CfR2Bucket[] | undefined;
767
+
768
+ // Account ID to use for authenticated Cloudflare fetch. If true, prompt
769
+ // user for ID if multiple available.
770
+ authenticatedAccountId: string | true | undefined;
771
+ // Whether to read/write from/to real KV namespaces
772
+ kvRemote: boolean | undefined;
773
+
774
+ // Port to start DevTools inspector server on
775
+ inspectorPort: number;
776
+ }
777
+
778
+ export async function buildMiniflare3Logger(
779
+ logPrefix?: string
780
+ ): Promise<Miniflare3LogType> {
781
+ const { Log, NoOpLog, LogLevel } = await getMiniflare3();
782
+
783
+ let level = logger.loggerLevel.toUpperCase() as Uppercase<LoggerLevel>;
784
+ if (level === "LOG") level = "INFO";
785
+ const logLevel = LogLevel[level];
786
+
787
+ return logLevel === LogLevel.NONE
788
+ ? new NoOpLog()
789
+ : new Log(logLevel, { prefix: logPrefix });
790
+ }
791
+
792
+ export async function transformMf2OptionsToMf3Options({
793
+ miniflare2Options,
794
+ format,
795
+ bundle,
796
+ log,
797
+ kvNamespaces,
798
+ r2Buckets,
799
+ authenticatedAccountId,
800
+ kvRemote,
801
+ inspectorPort,
802
+ }: SetupMiniflare3Options): Promise<Miniflare3Options> {
803
+ // Build authenticated Cloudflare API fetch function if required
804
+ let cloudflareFetch: CloudflareFetch | undefined;
805
+ if (kvRemote && authenticatedAccountId !== undefined) {
806
+ const preferredAccountId =
807
+ authenticatedAccountId === true ? undefined : authenticatedAccountId;
808
+ const accountId = await requireAuth({ account_id: preferredAccountId });
809
+ cloudflareFetch = (resource, searchParams, init) => {
810
+ resource = `/accounts/${accountId}/${resource}`;
811
+ // Miniflare and Wrangler's `undici` versions may be slightly different,
812
+ // but their `RequestInit` types *should* be compatible
813
+ return performApiFetch(resource, init as RequestInit, searchParams);
814
+ };
815
+ }
816
+
817
+ const options: Miniflare3Options = {
818
+ ...miniflare2Options,
819
+ // Miniflare 3 distinguishes between binding name and namespace/bucket IDs.
820
+ kvNamespaces: Object.fromEntries(
821
+ kvNamespaces?.map(({ binding, id }) => [binding, id]) ?? []
822
+ ),
823
+ r2Buckets: Object.fromEntries(
824
+ r2Buckets?.map(({ binding, bucket_name }) => [binding, bucket_name]) ?? []
825
+ ),
826
+ inspectorPort,
827
+ verbose: true,
828
+ cloudflareFetch,
829
+ log,
830
+ };
831
+
832
+ if (format === "modules") {
833
+ // Manually specify all modules from the bundle. If we didn't do this,
834
+ // Miniflare 3 would try collect them automatically again itself.
835
+
836
+ // Resolve entrypoint relative to the temporary directory, ensuring
837
+ // path doesn't start with `..`, which causes issues in `workerd`.
838
+ // Also ensures other modules with relative names can be resolved.
839
+ const root = path.dirname(bundle.path);
840
+
841
+ assert.strictEqual(bundle.type, "esm");
842
+ // Required for source mapped paths to resolve correctly
843
+ options.modulesRoot = root;
844
+ options.modules = [
845
+ // Entrypoint
846
+ {
847
+ type: "ESModule",
848
+ path: bundle.path,
849
+ contents: await readFile(bundle.path, "utf-8"),
850
+ },
851
+ // Misc (WebAssembly, etc, ...)
852
+ ...bundle.modules.map((module) => ({
853
+ type: ModuleTypeToRuleType[module.type ?? "esm"],
854
+ path: path.resolve(root, module.name),
855
+ contents: module.content,
856
+ })),
857
+ ];
858
+ }
859
+
860
+ if (kvRemote) {
861
+ // `kvPersist` is always assigned a truthy value in `setupMiniflareOptions`
862
+ assert(options.kvPersist);
863
+ const kvRemoteCache =
864
+ options.kvPersist === true
865
+ ? // If storing in temporary directory, find this path from the bundle
866
+ // output path
867
+ path.join(path.dirname(bundle.path), ".mf", "kv-remote")
868
+ : // Otherwise, `kvPersist` looks like `.../kv`, so rewrite it to
869
+ // `kv-remote` since the expected metadata format for remote storage
870
+ // is different to local
871
+ path.join(path.dirname(options.kvPersist), "kv-remote");
872
+ options.kvPersist = `remote:?cache=${encodeURIComponent(kvRemoteCache)}`;
873
+ }
874
+
875
+ return options;
876
+ }
877
+
878
+ // Caching of the `npx-import`ed `@miniflare/tre` package
879
+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
880
+ let miniflare3Module: typeof import("@miniflare/tre");
881
+ export async function getMiniflare3(): Promise<
882
+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
883
+ typeof import("@miniflare/tre")
884
+ > {
885
+ return (miniflare3Module ??= await npxImport("@miniflare/tre@3.0.0-next.7"));
232
886
  }