wrangler 0.0.0-e41c7e41 → 0.0.0-e44afaf79

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 (275) hide show
  1. package/README.md +35 -45
  2. package/bin/wrangler.js +27 -102
  3. package/config-schema.json +2487 -0
  4. package/package.json +179 -192
  5. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  6. package/templates/__tests__/tsconfig.json +8 -0
  7. package/templates/checked-fetch.js +19 -6
  8. package/templates/facade.d.ts +19 -0
  9. package/templates/gitignore +2 -3
  10. package/templates/init-tests/test-jest-new-worker.js +3 -5
  11. package/templates/init-tests/test-vitest-new-worker.js +4 -6
  12. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  13. package/templates/middleware/common.ts +9 -4
  14. package/templates/middleware/loader-modules.ts +94 -44
  15. package/templates/middleware/loader-sw.ts +6 -4
  16. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  17. package/templates/middleware/middleware-miniflare3-json-error.ts +17 -5
  18. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  19. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  20. package/templates/middleware/middleware-scheduled.ts +15 -1
  21. package/templates/mixedMode/proxyServerWorker/index.ts +40 -0
  22. package/templates/mixedMode/proxyServerWorker/wrangler.jsonc +4 -0
  23. package/templates/modules-watch-stub.js +4 -0
  24. package/templates/new-worker-scheduled.ts +1 -1
  25. package/templates/new-worker.ts +4 -1
  26. package/templates/pages-dev-pipeline.ts +8 -3
  27. package/templates/pages-dev-util.ts +4 -1
  28. package/templates/pages-shim.ts +1 -1
  29. package/templates/pages-template-plugin.ts +33 -14
  30. package/templates/pages-template-worker.ts +12 -3
  31. package/templates/startDevWorker/InspectorProxyWorker.ts +679 -0
  32. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  33. package/templates/strip-cf-connecting-ip-header.js +13 -0
  34. package/templates/tsconfig-sanity.ts +11 -0
  35. package/templates/tsconfig.init.json +22 -0
  36. package/templates/tsconfig.json +12 -103
  37. package/templates/tsconfig.tsbuildinfo +1 -0
  38. package/wrangler-dist/InspectorProxyWorker.js +471 -0
  39. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  40. package/wrangler-dist/ProxyWorker.js +238 -0
  41. package/wrangler-dist/ProxyWorker.js.map +6 -0
  42. package/wrangler-dist/cli.d.ts +2054 -512
  43. package/wrangler-dist/cli.js +172903 -159717
  44. package/wrangler-dist/cli.js.map +1 -0
  45. package/wrangler-dist/metafile-cjs.json +1 -0
  46. package/Cloudflare_CA.pem +0 -18
  47. package/import_meta_url.js +0 -3
  48. package/miniflare-config-stubs/.env.empty +0 -0
  49. package/miniflare-config-stubs/package.empty.json +0 -1
  50. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  51. package/miniflare-dist/index.mjs +0 -6316
  52. package/src/__tests__/access.test.ts +0 -25
  53. package/src/__tests__/api-dev.test.ts +0 -209
  54. package/src/__tests__/api-devregistry.test.js +0 -56
  55. package/src/__tests__/api.test.ts +0 -80
  56. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  57. package/src/__tests__/config-cache.test.ts +0 -42
  58. package/src/__tests__/configuration.test.ts +0 -3981
  59. package/src/__tests__/d1.test.ts +0 -80
  60. package/src/__tests__/delete.test.ts +0 -230
  61. package/src/__tests__/deployments.test.ts +0 -91
  62. package/src/__tests__/dev.test.tsx +0 -1536
  63. package/src/__tests__/dialogs.test.tsx +0 -40
  64. package/src/__tests__/generate.test.ts +0 -235
  65. package/src/__tests__/get-host-from-url.test.ts +0 -16
  66. package/src/__tests__/guess-worker-format.test.ts +0 -85
  67. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  68. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  69. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  70. package/src/__tests__/helpers/mock-bin.ts +0 -36
  71. package/src/__tests__/helpers/mock-cfetch.ts +0 -333
  72. package/src/__tests__/helpers/mock-console.ts +0 -106
  73. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  74. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -8
  75. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  76. package/src/__tests__/helpers/mock-istty.ts +0 -74
  77. package/src/__tests__/helpers/mock-known-routes.ts +0 -7
  78. package/src/__tests__/helpers/mock-kv.ts +0 -33
  79. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -252
  80. package/src/__tests__/helpers/mock-process.ts +0 -39
  81. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  82. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  83. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  84. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -59
  85. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -104
  86. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -36
  87. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -80
  88. package/src/__tests__/helpers/msw/handlers/script.ts +0 -75
  89. package/src/__tests__/helpers/msw/handlers/user.ts +0 -63
  90. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -22
  91. package/src/__tests__/helpers/msw/index.ts +0 -21
  92. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  93. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  94. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  95. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  96. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  97. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
  98. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  99. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  100. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  101. package/src/__tests__/https-options.test.ts +0 -163
  102. package/src/__tests__/index.test.ts +0 -264
  103. package/src/__tests__/init.test.ts +0 -2900
  104. package/src/__tests__/jest.setup.ts +0 -202
  105. package/src/__tests__/kv.test.ts +0 -1675
  106. package/src/__tests__/logger.test.ts +0 -141
  107. package/src/__tests__/metrics.test.ts +0 -450
  108. package/src/__tests__/middleware.scheduled.test.ts +0 -135
  109. package/src/__tests__/middleware.test.ts +0 -726
  110. package/src/__tests__/package-manager.test.ts +0 -353
  111. package/src/__tests__/pages-deployment-tail.test.ts +0 -820
  112. package/src/__tests__/pages.test.ts +0 -2442
  113. package/src/__tests__/parse.test.ts +0 -330
  114. package/src/__tests__/paths.test.ts +0 -39
  115. package/src/__tests__/publish.test.ts +0 -7633
  116. package/src/__tests__/pubsub.test.ts +0 -370
  117. package/src/__tests__/queues.test.ts +0 -371
  118. package/src/__tests__/r2.test.ts +0 -393
  119. package/src/__tests__/route.test.ts +0 -45
  120. package/src/__tests__/secret.test.ts +0 -644
  121. package/src/__tests__/tail.test.ts +0 -800
  122. package/src/__tests__/test-old-node-version.js +0 -31
  123. package/src/__tests__/type-generation.test.ts +0 -226
  124. package/src/__tests__/user.test.ts +0 -155
  125. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  126. package/src/__tests__/validate-dev-props.test.ts +0 -56
  127. package/src/__tests__/version.test.ts +0 -35
  128. package/src/__tests__/whoami.test.tsx +0 -278
  129. package/src/__tests__/worker-namespace.test.ts +0 -326
  130. package/src/abort.d.ts +0 -3
  131. package/src/api/dev.ts +0 -210
  132. package/src/api/index.ts +0 -2
  133. package/src/bundle-reporter.tsx +0 -68
  134. package/src/bundle.ts +0 -823
  135. package/src/cfetch/index.ts +0 -160
  136. package/src/cfetch/internal.ts +0 -263
  137. package/src/cli.ts +0 -28
  138. package/src/config/README.md +0 -107
  139. package/src/config/config.ts +0 -275
  140. package/src/config/diagnostics.ts +0 -80
  141. package/src/config/environment.ts +0 -561
  142. package/src/config/index.ts +0 -334
  143. package/src/config/validation-helpers.ts +0 -597
  144. package/src/config/validation.ts +0 -2103
  145. package/src/config-cache.ts +0 -85
  146. package/src/create-worker-preview.ts +0 -282
  147. package/src/create-worker-upload-form.ts +0 -316
  148. package/src/d1/backups.tsx +0 -218
  149. package/src/d1/constants.ts +0 -2
  150. package/src/d1/create.tsx +0 -63
  151. package/src/d1/delete.tsx +0 -56
  152. package/src/d1/execute.tsx +0 -352
  153. package/src/d1/formatTimeAgo.ts +0 -14
  154. package/src/d1/index.ts +0 -99
  155. package/src/d1/list.tsx +0 -48
  156. package/src/d1/migrations.tsx +0 -446
  157. package/src/d1/options.ts +0 -22
  158. package/src/d1/types.tsx +0 -24
  159. package/src/d1/utils.ts +0 -48
  160. package/src/delete.ts +0 -108
  161. package/src/deployments.ts +0 -77
  162. package/src/deprecated/index.ts +0 -438
  163. package/src/dev/dev-vars.ts +0 -39
  164. package/src/dev/dev.tsx +0 -590
  165. package/src/dev/get-local-persistence-path.tsx +0 -31
  166. package/src/dev/local.tsx +0 -886
  167. package/src/dev/remote.tsx +0 -603
  168. package/src/dev/start-server.ts +0 -538
  169. package/src/dev/use-esbuild.ts +0 -204
  170. package/src/dev/validate-dev-props.ts +0 -31
  171. package/src/dev-registry.tsx +0 -201
  172. package/src/dev.tsx +0 -905
  173. package/src/dialogs.tsx +0 -168
  174. package/src/dispatch-namespace.ts +0 -207
  175. package/src/durable.ts +0 -102
  176. package/src/entry.ts +0 -329
  177. package/src/environment-variables.ts +0 -50
  178. package/src/errors.ts +0 -11
  179. package/src/git-client.ts +0 -124
  180. package/src/global-wrangler-config-path.ts +0 -26
  181. package/src/https-options.ts +0 -127
  182. package/src/index.tsx +0 -682
  183. package/src/init.ts +0 -970
  184. package/src/inspect.ts +0 -767
  185. package/src/intl-polyfill.d.ts +0 -139
  186. package/src/is-ci.ts +0 -14
  187. package/src/is-interactive.ts +0 -16
  188. package/src/kv/helpers.ts +0 -430
  189. package/src/kv/index.ts +0 -596
  190. package/src/logger.ts +0 -92
  191. package/src/metrics/index.ts +0 -5
  192. package/src/metrics/metrics-config.ts +0 -239
  193. package/src/metrics/metrics-dispatcher.ts +0 -96
  194. package/src/metrics/metrics-usage-headers.ts +0 -24
  195. package/src/metrics/send-event.ts +0 -96
  196. package/src/miniflare-cli/README.md +0 -30
  197. package/src/miniflare-cli/assets.ts +0 -215
  198. package/src/miniflare-cli/index.ts +0 -213
  199. package/src/miniflare-cli/request-context.ts +0 -40
  200. package/src/module-collection.ts +0 -267
  201. package/src/open-in-browser.ts +0 -17
  202. package/src/package-manager.ts +0 -219
  203. package/src/pages/build.tsx +0 -270
  204. package/src/pages/constants.ts +0 -18
  205. package/src/pages/deployment-tails.tsx +0 -284
  206. package/src/pages/deployments.tsx +0 -84
  207. package/src/pages/dev.tsx +0 -708
  208. package/src/pages/errors.ts +0 -66
  209. package/src/pages/functions/buildPlugin.ts +0 -107
  210. package/src/pages/functions/buildWorker.ts +0 -166
  211. package/src/pages/functions/filepath-routing.test.ts +0 -234
  212. package/src/pages/functions/filepath-routing.ts +0 -189
  213. package/src/pages/functions/identifiers.ts +0 -78
  214. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  215. package/src/pages/functions/routes-consolidation.ts +0 -73
  216. package/src/pages/functions/routes-transformation.test.ts +0 -282
  217. package/src/pages/functions/routes-transformation.ts +0 -115
  218. package/src/pages/functions/routes-validation.test.ts +0 -403
  219. package/src/pages/functions/routes-validation.ts +0 -202
  220. package/src/pages/functions/routes.ts +0 -151
  221. package/src/pages/functions.tsx +0 -82
  222. package/src/pages/hash.tsx +0 -13
  223. package/src/pages/index.tsx +0 -102
  224. package/src/pages/projects.tsx +0 -169
  225. package/src/pages/prompt-select-project.tsx +0 -31
  226. package/src/pages/publish.tsx +0 -451
  227. package/src/pages/types.ts +0 -41
  228. package/src/pages/upload.tsx +0 -426
  229. package/src/pages/utils.ts +0 -23
  230. package/src/parse.ts +0 -286
  231. package/src/paths.ts +0 -71
  232. package/src/proxy.ts +0 -689
  233. package/src/publish/index.ts +0 -270
  234. package/src/publish/publish.ts +0 -1035
  235. package/src/pubsub/index.ts +0 -286
  236. package/src/pubsub/pubsub-commands.tsx +0 -624
  237. package/src/queues/cli/commands/consumer/add.ts +0 -71
  238. package/src/queues/cli/commands/consumer/index.ts +0 -22
  239. package/src/queues/cli/commands/consumer/remove.ts +0 -38
  240. package/src/queues/cli/commands/create.ts +0 -25
  241. package/src/queues/cli/commands/delete.ts +0 -26
  242. package/src/queues/cli/commands/index.ts +0 -33
  243. package/src/queues/cli/commands/list.ts +0 -25
  244. package/src/queues/client.ts +0 -135
  245. package/src/r2/constants.ts +0 -4
  246. package/src/r2/helpers.ts +0 -131
  247. package/src/r2/index.ts +0 -289
  248. package/src/routes.ts +0 -140
  249. package/src/secret/index.ts +0 -378
  250. package/src/selfsigned.d.ts +0 -29
  251. package/src/sites.tsx +0 -372
  252. package/src/tail/createTail.ts +0 -379
  253. package/src/tail/filters.ts +0 -277
  254. package/src/tail/index.ts +0 -214
  255. package/src/tail/printing.ts +0 -107
  256. package/src/type-generation.ts +0 -173
  257. package/src/update-check.ts +0 -19
  258. package/src/user/access.ts +0 -69
  259. package/src/user/choose-account.tsx +0 -69
  260. package/src/user/env-vars.ts +0 -46
  261. package/src/user/generate-auth-url.ts +0 -33
  262. package/src/user/generate-random-state.ts +0 -16
  263. package/src/user/index.ts +0 -3
  264. package/src/user/user.tsx +0 -1192
  265. package/src/utils/collectKeyValues.ts +0 -14
  266. package/src/whoami.tsx +0 -169
  267. package/src/worker.ts +0 -245
  268. package/src/yargs-types.ts +0 -24
  269. package/src/zones.ts +0 -191
  270. package/templates/d1-beta-facade.js +0 -205
  271. package/templates/first-party-worker-module-facade.ts +0 -18
  272. package/templates/format-dev-errors.ts +0 -32
  273. package/templates/serve-static-assets.ts +0 -63
  274. package/templates/service-bindings-module-facade.js +0 -54
  275. package/templates/service-bindings-sw-facade.js +0 -42
package/src/dev/local.tsx DELETED
@@ -1,886 +0,0 @@
1
- import assert from "node:assert";
2
- import { fork } from "node:child_process";
3
- import { realpathSync } from "node:fs";
4
- import { readFile, writeFile } from "node:fs/promises";
5
- import path from "node:path";
6
- import chalk from "chalk";
7
- import { npxImport } from "npx-import";
8
- import { useState, useEffect, useRef } from "react";
9
- import onExit from "signal-exit";
10
- import { performApiFetch } from "../cfetch/internal";
11
- import { registerWorker } from "../dev-registry";
12
- import useInspector from "../inspect";
13
- import { logger } from "../logger";
14
- import {
15
- DEFAULT_MODULE_RULES,
16
- ModuleTypeToRuleType,
17
- } from "../module-collection";
18
- import { getBasePath } from "../paths";
19
- import { waitForPortToBeAvailable } from "../proxy";
20
- import { requireAuth } from "../user";
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";
25
- import type { AssetPaths } from "../sites";
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";
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";
46
- import type { MiniflareOptions } from "miniflare";
47
- import type { ChildProcess } from "node:child_process";
48
- import type { RequestInit } from "undici";
49
-
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;
78
- }
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
-
92
- export function Local(props: LocalProps) {
93
- const { inspectorUrl } = useLocalWorker(props);
94
- useInspector({
95
- inspectorUrl,
96
- port: props.inspectorPort,
97
- logToTerminal: props.experimentalLocal ?? false,
98
- });
99
- return null;
100
- }
101
-
102
- function useLocalWorker({
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,
129
- }: LocalProps) {
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"));
886
- }