wrangler 0.0.0-e7a0cd5b → 0.0.0-e8053554

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 (264) hide show
  1. package/README.md +12 -14
  2. package/bin/wrangler.js +24 -65
  3. package/package.json +213 -191
  4. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  5. package/templates/__tests__/tsconfig.json +8 -0
  6. package/templates/checked-fetch.js +19 -6
  7. package/templates/facade.d.ts +17 -0
  8. package/templates/gitignore +1 -0
  9. package/templates/init-tests/test-jest-new-worker.js +3 -5
  10. package/templates/init-tests/test-vitest-new-worker.js +3 -5
  11. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  12. package/templates/middleware/common.ts +9 -4
  13. package/templates/middleware/loader-modules.ts +64 -11
  14. package/templates/middleware/loader-sw.ts +47 -32
  15. package/templates/middleware/middleware-d1-beta.d.ts +3 -0
  16. package/templates/middleware/middleware-miniflare3-json-error.ts +33 -0
  17. package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
  18. package/templates/middleware/middleware-multiworker-dev.ts +68 -0
  19. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  20. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  21. package/templates/modules-watch-stub.js +4 -0
  22. package/templates/new-worker-scheduled.ts +1 -1
  23. package/templates/new-worker.ts +4 -1
  24. package/templates/pages-dev-pipeline.ts +4 -1
  25. package/templates/pages-dev-util.ts +4 -1
  26. package/templates/pages-shim.ts +1 -1
  27. package/templates/pages-template-plugin.ts +31 -12
  28. package/templates/pages-template-worker.ts +11 -2
  29. package/templates/startDevWorker/InspectorProxyWorker.ts +652 -0
  30. package/templates/startDevWorker/ProxyWorker.ts +252 -0
  31. package/templates/tsconfig-sanity.ts +11 -0
  32. package/templates/tsconfig.init.json +105 -0
  33. package/templates/tsconfig.json +5 -103
  34. package/wrangler-dist/InspectorProxyWorker.js +460 -0
  35. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  36. package/wrangler-dist/ProxyWorker.js +186 -0
  37. package/wrangler-dist/ProxyWorker.js.map +6 -0
  38. package/wrangler-dist/cli.d.ts +23148 -533
  39. package/wrangler-dist/cli.js +135057 -136366
  40. package/wrangler-dist/wasm-sync.wasm +0 -0
  41. package/import_meta_url.js +0 -3
  42. package/miniflare-config-stubs/.env.empty +0 -0
  43. package/miniflare-config-stubs/package.empty.json +0 -1
  44. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  45. package/miniflare-dist/index.mjs +0 -6316
  46. package/src/__tests__/access.test.ts +0 -25
  47. package/src/__tests__/api-dev.test.ts +0 -209
  48. package/src/__tests__/api-devregistry.test.js +0 -57
  49. package/src/__tests__/api.test.ts +0 -80
  50. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  51. package/src/__tests__/config-cache.test.ts +0 -42
  52. package/src/__tests__/configuration.test.ts +0 -4028
  53. package/src/__tests__/d1.test.ts +0 -72
  54. package/src/__tests__/delete.test.ts +0 -230
  55. package/src/__tests__/deployments.test.ts +0 -95
  56. package/src/__tests__/dev.test.tsx +0 -1509
  57. package/src/__tests__/dialogs.test.tsx +0 -40
  58. package/src/__tests__/generate.test.ts +0 -235
  59. package/src/__tests__/get-host-from-url.test.ts +0 -16
  60. package/src/__tests__/guess-worker-format.test.ts +0 -85
  61. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  62. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  63. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  64. package/src/__tests__/helpers/mock-bin.ts +0 -36
  65. package/src/__tests__/helpers/mock-cfetch.ts +0 -333
  66. package/src/__tests__/helpers/mock-console.ts +0 -106
  67. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  68. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -8
  69. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  70. package/src/__tests__/helpers/mock-istty.ts +0 -74
  71. package/src/__tests__/helpers/mock-known-routes.ts +0 -7
  72. package/src/__tests__/helpers/mock-kv.ts +0 -33
  73. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -252
  74. package/src/__tests__/helpers/mock-process.ts +0 -39
  75. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  76. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  77. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  78. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -59
  79. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -104
  80. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -36
  81. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -80
  82. package/src/__tests__/helpers/msw/handlers/script.ts +0 -75
  83. package/src/__tests__/helpers/msw/handlers/user.ts +0 -63
  84. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -22
  85. package/src/__tests__/helpers/msw/index.ts +0 -21
  86. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  87. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  88. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  89. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  90. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  91. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
  92. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  93. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  94. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  95. package/src/__tests__/https-options.test.ts +0 -163
  96. package/src/__tests__/index.test.ts +0 -262
  97. package/src/__tests__/init.test.ts +0 -2900
  98. package/src/__tests__/jest.setup.ts +0 -202
  99. package/src/__tests__/kv.test.ts +0 -1675
  100. package/src/__tests__/logger.test.ts +0 -141
  101. package/src/__tests__/metrics.test.ts +0 -450
  102. package/src/__tests__/middleware.scheduled.test.ts +0 -135
  103. package/src/__tests__/middleware.test.ts +0 -726
  104. package/src/__tests__/package-manager.test.ts +0 -353
  105. package/src/__tests__/pages.test.ts +0 -2223
  106. package/src/__tests__/parse.test.ts +0 -330
  107. package/src/__tests__/paths.test.ts +0 -39
  108. package/src/__tests__/publish.test.ts +0 -7660
  109. package/src/__tests__/pubsub.test.ts +0 -370
  110. package/src/__tests__/queues.test.ts +0 -371
  111. package/src/__tests__/r2.test.ts +0 -376
  112. package/src/__tests__/route.test.ts +0 -45
  113. package/src/__tests__/secret.test.ts +0 -644
  114. package/src/__tests__/tail.test.ts +0 -800
  115. package/src/__tests__/test-old-node-version.js +0 -31
  116. package/src/__tests__/type-generation.test.ts +0 -217
  117. package/src/__tests__/user.test.ts +0 -155
  118. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  119. package/src/__tests__/validate-dev-props.test.ts +0 -56
  120. package/src/__tests__/version.test.ts +0 -35
  121. package/src/__tests__/whoami.test.tsx +0 -278
  122. package/src/__tests__/worker-namespace.test.ts +0 -326
  123. package/src/abort.d.ts +0 -3
  124. package/src/api/dev.ts +0 -208
  125. package/src/api/index.ts +0 -2
  126. package/src/bundle-reporter.tsx +0 -68
  127. package/src/bundle.ts +0 -785
  128. package/src/cfetch/index.ts +0 -160
  129. package/src/cfetch/internal.ts +0 -263
  130. package/src/cli.ts +0 -28
  131. package/src/config/README.md +0 -107
  132. package/src/config/config.ts +0 -275
  133. package/src/config/diagnostics.ts +0 -80
  134. package/src/config/environment.ts +0 -563
  135. package/src/config/index.ts +0 -342
  136. package/src/config/validation-helpers.ts +0 -597
  137. package/src/config/validation.ts +0 -2113
  138. package/src/config-cache.ts +0 -85
  139. package/src/create-worker-preview.ts +0 -282
  140. package/src/create-worker-upload-form.ts +0 -321
  141. package/src/d1/backups.tsx +0 -218
  142. package/src/d1/create.tsx +0 -54
  143. package/src/d1/delete.tsx +0 -56
  144. package/src/d1/execute.tsx +0 -299
  145. package/src/d1/formatTimeAgo.ts +0 -14
  146. package/src/d1/index.ts +0 -76
  147. package/src/d1/list.tsx +0 -48
  148. package/src/d1/options.ts +0 -12
  149. package/src/d1/types.tsx +0 -14
  150. package/src/d1/utils.ts +0 -39
  151. package/src/delete.ts +0 -108
  152. package/src/deployments.ts +0 -74
  153. package/src/deprecated/index.ts +0 -438
  154. package/src/dev/dev-vars.ts +0 -39
  155. package/src/dev/dev.tsx +0 -587
  156. package/src/dev/get-local-persistence-path.tsx +0 -31
  157. package/src/dev/local.tsx +0 -717
  158. package/src/dev/remote.tsx +0 -603
  159. package/src/dev/start-server.ts +0 -513
  160. package/src/dev/use-esbuild.ts +0 -204
  161. package/src/dev/validate-dev-props.ts +0 -31
  162. package/src/dev-registry.tsx +0 -201
  163. package/src/dev.tsx +0 -869
  164. package/src/dialogs.tsx +0 -163
  165. package/src/dispatch-namespace.ts +0 -207
  166. package/src/durable.ts +0 -102
  167. package/src/entry.ts +0 -329
  168. package/src/environment-variables.ts +0 -50
  169. package/src/errors.ts +0 -11
  170. package/src/git-client.ts +0 -124
  171. package/src/global-wrangler-config-path.ts +0 -26
  172. package/src/https-options.ts +0 -127
  173. package/src/index.tsx +0 -684
  174. package/src/init.ts +0 -970
  175. package/src/inspect.ts +0 -767
  176. package/src/intl-polyfill.d.ts +0 -139
  177. package/src/is-ci.ts +0 -14
  178. package/src/is-interactive.ts +0 -16
  179. package/src/kv/helpers.ts +0 -430
  180. package/src/kv/index.ts +0 -596
  181. package/src/logger.ts +0 -92
  182. package/src/metrics/index.ts +0 -5
  183. package/src/metrics/metrics-config.ts +0 -239
  184. package/src/metrics/metrics-dispatcher.ts +0 -96
  185. package/src/metrics/metrics-usage-headers.ts +0 -24
  186. package/src/metrics/send-event.ts +0 -94
  187. package/src/miniflare-cli/README.md +0 -30
  188. package/src/miniflare-cli/assets.ts +0 -215
  189. package/src/miniflare-cli/index.ts +0 -213
  190. package/src/miniflare-cli/request-context.ts +0 -40
  191. package/src/module-collection.ts +0 -267
  192. package/src/open-in-browser.ts +0 -17
  193. package/src/package-manager.ts +0 -219
  194. package/src/pages/build.tsx +0 -238
  195. package/src/pages/constants.ts +0 -18
  196. package/src/pages/deployments.tsx +0 -106
  197. package/src/pages/dev.tsx +0 -693
  198. package/src/pages/errors.ts +0 -66
  199. package/src/pages/functions/buildPlugin.ts +0 -110
  200. package/src/pages/functions/buildWorker.ts +0 -156
  201. package/src/pages/functions/filepath-routing.test.ts +0 -234
  202. package/src/pages/functions/filepath-routing.ts +0 -189
  203. package/src/pages/functions/identifiers.ts +0 -78
  204. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  205. package/src/pages/functions/routes-consolidation.ts +0 -73
  206. package/src/pages/functions/routes-transformation.test.ts +0 -282
  207. package/src/pages/functions/routes-transformation.ts +0 -115
  208. package/src/pages/functions/routes-validation.test.ts +0 -403
  209. package/src/pages/functions/routes-validation.ts +0 -202
  210. package/src/pages/functions/routes.ts +0 -151
  211. package/src/pages/functions.tsx +0 -82
  212. package/src/pages/hash.tsx +0 -13
  213. package/src/pages/index.tsx +0 -94
  214. package/src/pages/projects.tsx +0 -169
  215. package/src/pages/publish.tsx +0 -436
  216. package/src/pages/types.ts +0 -49
  217. package/src/pages/upload.tsx +0 -425
  218. package/src/pages/utils.ts +0 -12
  219. package/src/parse.ts +0 -286
  220. package/src/paths.ts +0 -71
  221. package/src/proxy.ts +0 -689
  222. package/src/publish/index.ts +0 -270
  223. package/src/publish/publish.ts +0 -1035
  224. package/src/pubsub/index.ts +0 -286
  225. package/src/pubsub/pubsub-commands.tsx +0 -624
  226. package/src/queues/cli/commands/consumer/add.ts +0 -71
  227. package/src/queues/cli/commands/consumer/index.ts +0 -22
  228. package/src/queues/cli/commands/consumer/remove.ts +0 -38
  229. package/src/queues/cli/commands/create.ts +0 -25
  230. package/src/queues/cli/commands/delete.ts +0 -26
  231. package/src/queues/cli/commands/index.ts +0 -33
  232. package/src/queues/cli/commands/list.ts +0 -25
  233. package/src/queues/client.ts +0 -135
  234. package/src/r2/helpers.ts +0 -131
  235. package/src/r2/index.ts +0 -277
  236. package/src/routes.ts +0 -140
  237. package/src/secret/index.ts +0 -379
  238. package/src/selfsigned.d.ts +0 -29
  239. package/src/sites.tsx +0 -372
  240. package/src/tail/createTail.ts +0 -315
  241. package/src/tail/filters.ts +0 -277
  242. package/src/tail/index.ts +0 -214
  243. package/src/tail/printing.ts +0 -107
  244. package/src/type-generation.ts +0 -159
  245. package/src/update-check.ts +0 -19
  246. package/src/user/access.ts +0 -69
  247. package/src/user/choose-account.tsx +0 -69
  248. package/src/user/env-vars.ts +0 -46
  249. package/src/user/generate-auth-url.ts +0 -33
  250. package/src/user/generate-random-state.ts +0 -16
  251. package/src/user/index.ts +0 -3
  252. package/src/user/user.tsx +0 -1192
  253. package/src/utils/collectKeyValues.ts +0 -14
  254. package/src/whoami.tsx +0 -169
  255. package/src/worker.ts +0 -242
  256. package/src/yargs-types.ts +0 -20
  257. package/src/zones.ts +0 -191
  258. package/templates/d1-beta-facade.js +0 -174
  259. package/templates/experimental-local-cache-stubs.js +0 -27
  260. package/templates/first-party-worker-module-facade.ts +0 -18
  261. package/templates/format-dev-errors.ts +0 -32
  262. package/templates/serve-static-assets.ts +0 -63
  263. package/templates/service-bindings-module-facade.js +0 -54
  264. package/templates/service-bindings-sw-facade.js +0 -42
package/src/dev.tsx DELETED
@@ -1,869 +0,0 @@
1
- import path from "node:path";
2
- import { watch } from "chokidar";
3
- import getPort from "get-port";
4
- import { render } from "ink";
5
- import React from "react";
6
- import { findWranglerToml, printBindings, readConfig } from "./config";
7
- import Dev from "./dev/dev";
8
- import { getVarsForDev } from "./dev/dev-vars";
9
- import { getLocalPersistencePath } from "./dev/get-local-persistence-path";
10
-
11
- import { startDevServer } from "./dev/start-server";
12
- import { getEntry } from "./entry";
13
- import { logger } from "./logger";
14
- import * as metrics from "./metrics";
15
- import { getAssetPaths, getSiteAssetPaths } from "./sites";
16
- import { getAccountFromCache } from "./user";
17
- import { collectKeyValues } from "./utils/collectKeyValues";
18
- import { identifyD1BindingsAsBeta } from "./worker";
19
- import { getHostFromRoute, getZoneForRoute, getZoneIdFromHost } from "./zones";
20
- import {
21
- type ConfigPath,
22
- DEFAULT_INSPECTOR_PORT,
23
- DEFAULT_LOCAL_PORT,
24
- getDevCompatibilityDate,
25
- getRules,
26
- getScriptName,
27
- isLegacyEnv,
28
- printWranglerBanner,
29
- } from "./index";
30
- import type { Config, Environment } from "./config";
31
- import type { Route } from "./config/environment";
32
- import type { EnablePagesAssetsServiceBindingOptions } from "./miniflare-cli";
33
- import type { CfWorkerInit } from "./worker";
34
- import type { CommonYargsOptions } from "./yargs-types";
35
- import type { Argv, ArgumentsCamelCase } from "yargs";
36
-
37
- interface DevArgs {
38
- config?: string;
39
- script?: string;
40
- name?: string;
41
- bundle?: boolean;
42
- build?: boolean;
43
- format?: string;
44
- env?: string;
45
- "compatibility-date"?: string;
46
- "compatibility-flags"?: string[];
47
- latest?: boolean;
48
- ip?: string;
49
- inspect?: boolean;
50
- port?: number;
51
- "inspector-port"?: number;
52
- routes?: string[];
53
- host?: string;
54
- "local-protocol"?: "http" | "https";
55
- "local-upstream"?: string | undefined;
56
- "experimental-public"?: string;
57
- public?: string;
58
- assets?: string;
59
- site?: string;
60
- "site-include"?: string[];
61
- "site-exclude"?: string[];
62
- "upstream-protocol"?: "http" | "https";
63
- "jsx-factory"?: string;
64
- "jsx-fragment"?: string;
65
- tsconfig?: string;
66
- local?: boolean;
67
- "experimental-local"?: boolean;
68
- minify?: boolean;
69
- var?: string[];
70
- define?: string[];
71
- "node-compat"?: boolean;
72
- "experimental-enable-local-persistence"?: boolean;
73
- persist?: boolean;
74
- "persist-to"?: string;
75
- "live-reload"?: boolean;
76
- onReady?: (ip: string, port: number) => void;
77
- logLevel?: "none" | "info" | "error" | "log" | "warn" | "debug";
78
- logPrefix?: string;
79
- showInteractiveDevSession?: boolean;
80
- "test-scheduled"?: boolean;
81
- }
82
-
83
- export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
84
- return (
85
- yargs
86
- .positional("script", {
87
- describe: "The path to an entry point for your worker",
88
- type: "string",
89
- })
90
- .option("name", {
91
- describe: "Name of the worker",
92
- type: "string",
93
- requiresArg: true,
94
- })
95
- // We want to have a --no-bundle flag, but yargs requires that
96
- // we also have a --bundle flag (that it adds the --no to by itself)
97
- // So we make a --bundle flag, but hide it, and then add a --no-bundle flag
98
- // that's visible to the user but doesn't "do" anything.
99
- .option("bundle", {
100
- describe: "Run wrangler's compilation step before publishing",
101
- type: "boolean",
102
- hidden: true,
103
- })
104
- .option("no-bundle", {
105
- describe: "Skip internal build steps and directly publish script",
106
- type: "boolean",
107
- default: false,
108
- })
109
- .option("format", {
110
- choices: ["modules", "service-worker"] as const,
111
- describe: "Choose an entry type",
112
- hidden: true,
113
- deprecated: true,
114
- })
115
- .option("compatibility-date", {
116
- describe: "Date to use for compatibility checks",
117
- type: "string",
118
- requiresArg: true,
119
- })
120
- .option("compatibility-flags", {
121
- describe: "Flags to use for compatibility checks",
122
- alias: "compatibility-flag",
123
- type: "string",
124
- requiresArg: true,
125
- array: true,
126
- })
127
- .option("latest", {
128
- describe: "Use the latest version of the worker runtime",
129
- type: "boolean",
130
- default: true,
131
- })
132
- .option("ip", {
133
- describe: "IP address to listen on",
134
- type: "string",
135
- })
136
- .option("port", {
137
- describe: "Port to listen on",
138
- type: "number",
139
- })
140
- .option("inspector-port", {
141
- describe: "Port for devtools to connect to",
142
- type: "number",
143
- })
144
- .option("routes", {
145
- describe: "Routes to upload",
146
- alias: "route",
147
- type: "string",
148
- requiresArg: true,
149
- array: true,
150
- })
151
- .option("host", {
152
- type: "string",
153
- requiresArg: true,
154
- describe:
155
- "Host to forward requests to, defaults to the zone of project",
156
- })
157
- .option("local-protocol", {
158
- describe: "Protocol to listen to requests on, defaults to http.",
159
- choices: ["http", "https"] as const,
160
- })
161
- .options("local-upstream", {
162
- type: "string",
163
- describe:
164
- "Host to act as origin in local mode, defaults to dev.host or route",
165
- })
166
- .option("experimental-public", {
167
- describe: "Static assets to be served",
168
- type: "string",
169
- requiresArg: true,
170
- deprecated: true,
171
- hidden: true,
172
- })
173
- .option("assets", {
174
- describe: "Static assets to be served",
175
- type: "string",
176
- requiresArg: true,
177
- })
178
- .option("site", {
179
- describe: "Root folder of static assets for Workers Sites",
180
- type: "string",
181
- requiresArg: true,
182
- })
183
- .option("site-include", {
184
- describe:
185
- "Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
186
- type: "string",
187
- requiresArg: true,
188
- array: true,
189
- })
190
- .option("site-exclude", {
191
- describe:
192
- "Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
193
- type: "string",
194
- requiresArg: true,
195
- array: true,
196
- })
197
- .option("upstream-protocol", {
198
- describe: "Protocol to forward requests to host on, defaults to https.",
199
- choices: ["http", "https"] as const,
200
- })
201
- .option("var", {
202
- describe:
203
- "A key-value pair to be injected into the script as a variable",
204
- type: "string",
205
- requiresArg: true,
206
- array: true,
207
- })
208
- .option("define", {
209
- describe: "A key-value pair to be substituted in the script",
210
- type: "string",
211
- requiresArg: true,
212
- array: true,
213
- })
214
- .option("jsx-factory", {
215
- describe: "The function that is called for each JSX element",
216
- type: "string",
217
- requiresArg: true,
218
- })
219
- .option("jsx-fragment", {
220
- describe: "The function that is called for each JSX fragment",
221
- type: "string",
222
- requiresArg: true,
223
- })
224
- .option("tsconfig", {
225
- describe: "Path to a custom tsconfig.json file",
226
- type: "string",
227
- requiresArg: true,
228
- })
229
- .option("local", {
230
- alias: "l",
231
- describe: "Run on my machine",
232
- type: "boolean",
233
- default: false, // I bet this will a point of contention. We'll revisit it.
234
- })
235
- .option("experimental-local", {
236
- describe: "Run on my machine using the Cloudflare Workers runtime",
237
- type: "boolean",
238
- default: false,
239
- })
240
- .check((argv) => {
241
- if (argv.local && argv["experimental-local"]) {
242
- throw new Error(
243
- "--local and --experimental-local are mutually exclusive. " +
244
- "Please select one or the other."
245
- );
246
- }
247
- return true;
248
- })
249
- .option("minify", {
250
- describe: "Minify the script",
251
- type: "boolean",
252
- })
253
- .option("node-compat", {
254
- describe: "Enable node.js compatibility",
255
- type: "boolean",
256
- })
257
- .option("experimental-enable-local-persistence", {
258
- describe:
259
- "Enable persistence for local mode (deprecated, use --persist)",
260
- type: "boolean",
261
- deprecated: true,
262
- hidden: true,
263
- })
264
- .option("persist", {
265
- describe:
266
- "Enable persistence for local mode, using default path: .wrangler/state",
267
- type: "boolean",
268
- })
269
- .option("persist-to", {
270
- describe:
271
- "Specify directory to use for local persistence (implies --persist)",
272
- type: "string",
273
- requiresArg: true,
274
- })
275
- .option("live-reload", {
276
- // TODO: Add back in once we have remote `--live-reload`
277
- hidden: true,
278
- // describe: "Auto reload HTML pages when change is detected",
279
- type: "boolean",
280
- })
281
- .option("inspect", {
282
- describe: "Enable dev tools",
283
- type: "boolean",
284
- deprecated: true,
285
- hidden: true,
286
- })
287
- .option("legacy-env", {
288
- type: "boolean",
289
- describe: "Use legacy environments",
290
- hidden: true,
291
- })
292
- .option("test-scheduled", {
293
- describe: "Test scheduled events by visiting /__scheduled in browser",
294
- type: "boolean",
295
- default: false,
296
- })
297
- .option("log-level", {
298
- choices: ["debug", "info", "log", "warn", "error", "none"] as const,
299
- describe: "Specify logging level",
300
- default: "log",
301
- })
302
- );
303
- }
304
-
305
- export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
306
- let watcher;
307
- try {
308
- const devInstance = await startDev(args);
309
- watcher = devInstance.watcher;
310
- const { waitUntilExit } = devInstance.devReactElement;
311
- await waitUntilExit();
312
- } finally {
313
- await watcher?.close();
314
- }
315
- }
316
-
317
- export type AdditionalDevProps = {
318
- vars?: {
319
- [key: string]: unknown;
320
- };
321
- kv?: {
322
- binding: string;
323
- id: string;
324
- preview_id?: string;
325
- }[];
326
- durableObjects?: {
327
- name: string;
328
- class_name: string;
329
- script_name?: string | undefined;
330
- environment?: string | undefined;
331
- }[];
332
- r2?: {
333
- binding: string;
334
- bucket_name: string;
335
- preview_bucket_name?: string;
336
- }[];
337
- d1Databases?: Environment["d1_databases"];
338
- };
339
-
340
- type StartDevOptions = ArgumentsCamelCase<DevArgs> &
341
- // These options can be passed in directly when called with the `wrangler.dev()` API.
342
- // They aren't exposed as CLI arguments.
343
- AdditionalDevProps & {
344
- forceLocal?: boolean;
345
- enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
346
- };
347
-
348
- export async function startDev(args: StartDevOptions) {
349
- let watcher: ReturnType<typeof watch> | undefined;
350
- let rerender: (node: React.ReactNode) => void | undefined;
351
- try {
352
- if (args.logLevel) {
353
- logger.loggerLevel = args.logLevel;
354
- }
355
- await printWranglerBanner();
356
-
357
- const configPath =
358
- (args.config as ConfigPath) ||
359
- ((args.script &&
360
- findWranglerToml(path.dirname(args.script))) as ConfigPath);
361
- let config = readConfig(configPath, args);
362
-
363
- if (config.configPath) {
364
- watcher = watch(config.configPath, {
365
- persistent: true,
366
- }).on("change", async (_event) => {
367
- // TODO: Do we need to handle different `_event` types differently?
368
- // e.g. what if the file is deleted, or added?
369
- config = readConfig(configPath, args);
370
- if (config.configPath) {
371
- logger.log(`${path.basename(config.configPath)} changed...`);
372
- rerender(await getDevReactElement(config));
373
- }
374
- });
375
- }
376
-
377
- const {
378
- entry,
379
- nodeCompat,
380
- upstreamProtocol,
381
- zoneId,
382
- host,
383
- routes,
384
- getLocalPort,
385
- getInspectorPort,
386
- cliDefines,
387
- localPersistencePath,
388
- } = await validateDevServerSettings(args, config);
389
-
390
- await metrics.sendMetricsEvent(
391
- "run dev",
392
- {
393
- local: args.local,
394
- usesTypeScript: /\.tsx?$/.test(entry.file),
395
- },
396
- { sendMetrics: config.send_metrics, offline: args.local }
397
- );
398
-
399
- // eslint-disable-next-line no-inner-declarations
400
- async function getDevReactElement(configParam: Config) {
401
- const { assetPaths, bindings } = await getBindingsAndAssetPaths(
402
- args,
403
- configParam
404
- );
405
-
406
- return (
407
- <Dev
408
- name={getScriptName({ name: args.name, env: args.env }, configParam)}
409
- noBundle={!(args.bundle ?? !configParam.no_bundle)}
410
- entry={entry}
411
- env={args.env}
412
- zone={zoneId}
413
- host={host}
414
- routes={routes}
415
- rules={getRules(configParam)}
416
- legacyEnv={isLegacyEnv(configParam)}
417
- minify={args.minify ?? configParam.minify}
418
- nodeCompat={nodeCompat}
419
- build={configParam.build || {}}
420
- define={{ ...configParam.define, ...cliDefines }}
421
- initialMode={
422
- args.local || args.experimentalLocal ? "local" : "remote"
423
- }
424
- jsxFactory={args["jsx-factory"] || configParam.jsx_factory}
425
- jsxFragment={args["jsx-fragment"] || configParam.jsx_fragment}
426
- tsconfig={args.tsconfig ?? configParam.tsconfig}
427
- upstreamProtocol={upstreamProtocol}
428
- localProtocol={args.localProtocol || configParam.dev.local_protocol}
429
- localUpstream={args["local-upstream"] || host}
430
- localPersistencePath={localPersistencePath}
431
- liveReload={args.liveReload || false}
432
- accountId={configParam.account_id || getAccountFromCache()?.id}
433
- assetPaths={assetPaths}
434
- assetsConfig={configParam.assets}
435
- port={args.port || configParam.dev.port || (await getLocalPort())}
436
- ip={args.ip || configParam.dev.ip}
437
- inspectorPort={
438
- args.inspectorPort ||
439
- configParam.dev.inspector_port ||
440
- (await getInspectorPort())
441
- }
442
- isWorkersSite={Boolean(args.site || configParam.site)}
443
- compatibilityDate={getDevCompatibilityDate(
444
- configParam,
445
- args.compatibilityDate
446
- )}
447
- compatibilityFlags={
448
- args.compatibilityFlags || configParam.compatibility_flags
449
- }
450
- usageModel={configParam.usage_model}
451
- bindings={bindings}
452
- crons={configParam.triggers.crons}
453
- queueConsumers={configParam.queues.consumers}
454
- logPrefix={args.logPrefix}
455
- onReady={args.onReady}
456
- inspect={args.inspect ?? true}
457
- showInteractiveDevSession={args.showInteractiveDevSession}
458
- forceLocal={args.forceLocal}
459
- enablePagesAssetsServiceBinding={args.enablePagesAssetsServiceBinding}
460
- firstPartyWorker={configParam.first_party_worker}
461
- sendMetrics={configParam.send_metrics}
462
- testScheduled={args["test-scheduled"]}
463
- experimentalLocal={args.experimentalLocal}
464
- />
465
- );
466
- }
467
- const devReactElement = render(await getDevReactElement(config));
468
- rerender = devReactElement.rerender;
469
- return {
470
- devReactElement,
471
- watcher,
472
- stop: async () => {
473
- devReactElement.unmount();
474
- await watcher?.close();
475
- },
476
- };
477
- } finally {
478
- await watcher?.close();
479
- }
480
- }
481
-
482
- export async function startApiDev(args: StartDevOptions) {
483
- if (args.logLevel) {
484
- logger.loggerLevel = args.logLevel;
485
- }
486
- await printWranglerBanner();
487
-
488
- const configPath =
489
- (args.config as ConfigPath) ||
490
- ((args.script &&
491
- findWranglerToml(path.dirname(args.script))) as ConfigPath);
492
- const config = readConfig(configPath, args);
493
-
494
- const {
495
- entry,
496
- nodeCompat,
497
- upstreamProtocol,
498
- zoneId,
499
- host,
500
- routes,
501
- getLocalPort,
502
- getInspectorPort,
503
- cliDefines,
504
- localPersistencePath,
505
- } = await validateDevServerSettings(args, config);
506
-
507
- await metrics.sendMetricsEvent(
508
- "run dev (api)",
509
- { local: args.local },
510
- { sendMetrics: config.send_metrics, offline: args.local }
511
- );
512
-
513
- // eslint-disable-next-line no-inner-declarations
514
- async function getDevServer(configParam: Config) {
515
- const { assetPaths, bindings } = await getBindingsAndAssetPaths(
516
- args,
517
- configParam
518
- );
519
-
520
- //if args.bundle is on, don't disable bundling
521
- //if there's no args.bundle, and configParam.no_bundle is on, disable bundling
522
- //otherwise, enable bundling
523
- const enableBundling = args.bundle ?? !configParam.no_bundle;
524
- return await startDevServer({
525
- name: getScriptName({ name: args.name, env: args.env }, configParam),
526
- noBundle: !enableBundling,
527
- entry: entry,
528
- env: args.env,
529
- zone: zoneId,
530
- host: host,
531
- routes: routes,
532
- rules: getRules(configParam),
533
- legacyEnv: isLegacyEnv(configParam),
534
- minify: args.minify ?? configParam.minify,
535
- nodeCompat: nodeCompat,
536
- build: configParam.build || {},
537
- define: { ...config.define, ...cliDefines },
538
- initialMode: args.local ? "local" : "remote",
539
- jsxFactory: args["jsx-factory"] || configParam.jsx_factory,
540
- jsxFragment: args["jsx-fragment"] || configParam.jsx_fragment,
541
- tsconfig: args.tsconfig ?? configParam.tsconfig,
542
- upstreamProtocol: upstreamProtocol,
543
- localProtocol: args.localProtocol || configParam.dev.local_protocol,
544
- localUpstream: args["local-upstream"] || host,
545
- localPersistencePath,
546
- liveReload: args.liveReload || false,
547
- accountId: configParam.account_id || getAccountFromCache()?.id,
548
- assetPaths: assetPaths,
549
- assetsConfig: configParam.assets,
550
- //port can be 0, which means to use a random port
551
- port:
552
- args.port === 0
553
- ? args.port
554
- : args.port || configParam.dev.port || (await getLocalPort()),
555
- ip: args.ip || configParam.dev.ip,
556
- inspectorPort:
557
- args["inspector-port"] ||
558
- configParam.dev.inspector_port ||
559
- (await getInspectorPort()),
560
- isWorkersSite: Boolean(args.site || configParam.site),
561
- compatibilityDate: getDevCompatibilityDate(
562
- config,
563
- // Only `compatibilityDate` will be set when using `unstable_dev`
564
- args["compatibility-date"] ?? args.compatibilityDate
565
- ),
566
- compatibilityFlags:
567
- args["compatibility-flags"] ??
568
- args.compatibilityFlags ??
569
- configParam.compatibility_flags,
570
- usageModel: configParam.usage_model,
571
- bindings: bindings,
572
- crons: configParam.triggers.crons,
573
- queueConsumers: configParam.queues.consumers,
574
- logPrefix: args.logPrefix,
575
- onReady: args.onReady,
576
- inspect: args.inspect ?? true,
577
- showInteractiveDevSession: args.showInteractiveDevSession,
578
- forceLocal: args.forceLocal,
579
- enablePagesAssetsServiceBinding: args.enablePagesAssetsServiceBinding,
580
- local: args.local ?? true,
581
- firstPartyWorker: configParam.first_party_worker,
582
- sendMetrics: configParam.send_metrics,
583
- testScheduled: args.testScheduled,
584
- experimentalLocal: args.experimentalLocal,
585
- });
586
- }
587
-
588
- const devServer = await getDevServer(config);
589
- if (!devServer) {
590
- throw logger.error("Failed to start dev server.");
591
- }
592
-
593
- return {
594
- stop: async () => {
595
- await devServer.stop();
596
- },
597
- };
598
- }
599
- /**
600
- * Avoiding calling `getPort()` multiple times by memoizing the first result.
601
- */
602
- function memoizeGetPort(defaultPort: number) {
603
- let portValue: number;
604
- return async () => {
605
- return portValue || (portValue = await getPort({ port: defaultPort }));
606
- };
607
- }
608
- /**
609
- * mask anything that was overridden in .dev.vars
610
- * so that we don't log potential secrets into the terminal
611
- */
612
- function maskVars(bindings: CfWorkerInit["bindings"], configParam: Config) {
613
- const maskedVars = { ...bindings.vars };
614
- for (const key of Object.keys(maskedVars)) {
615
- if (maskedVars[key] !== configParam.vars[key]) {
616
- // This means it was overridden in .dev.vars
617
- // so let's mask it
618
- maskedVars[key] = "(hidden)";
619
- }
620
- }
621
- return maskedVars;
622
- }
623
-
624
- async function getZoneIdHostAndRoutes(args: StartDevOptions, config: Config) {
625
- // TODO: if worker_dev = false and no routes, then error (only for dev)
626
- // Compute zone info from the `host` and `route` args and config;
627
- let host = args.host || config.dev.host;
628
- let zoneId: string | undefined;
629
- const routes: Route[] | undefined =
630
- args.routes || (config.route && [config.route]) || config.routes;
631
-
632
- if (args.forceLocal) {
633
- args.local = true;
634
- }
635
-
636
- if (!args.local) {
637
- if (host) {
638
- zoneId = await getZoneIdFromHost(host);
639
- }
640
- if (!zoneId && routes) {
641
- const firstRoute = routes[0];
642
- const zone = await getZoneForRoute(firstRoute);
643
- if (zone) {
644
- zoneId = zone.id;
645
- host = zone.host;
646
- }
647
- }
648
- } else if (!host) {
649
- if (routes) {
650
- const firstRoute = routes[0];
651
- host = getHostFromRoute(firstRoute);
652
- }
653
- }
654
- return { host, routes, zoneId };
655
- }
656
-
657
- async function validateDevServerSettings(
658
- args: StartDevOptions,
659
- config: Config
660
- ) {
661
- const entry = await getEntry(
662
- { assets: args.assets, script: args.script },
663
- config,
664
- "dev"
665
- );
666
-
667
- const { zoneId, host, routes } = await getZoneIdHostAndRoutes(args, config);
668
- const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT);
669
- const getInspectorPort = memoizeGetPort(DEFAULT_INSPECTOR_PORT);
670
-
671
- if (config.services && config.services.length > 0) {
672
- logger.warn(
673
- `This worker is bound to live services: ${config.services
674
- .map(
675
- (service) =>
676
- `${service.binding} (${service.service}${
677
- service.environment ? `@${service.environment}` : ""
678
- })`
679
- )
680
- .join(", ")}`
681
- );
682
- }
683
-
684
- if (args.inspect) {
685
- //devtools are enabled by default, but we still need to disable them if the caller doesn't want them
686
- logger.warn(
687
- "Passing --inspect is unnecessary, now you can always connect to devtools."
688
- );
689
- }
690
-
691
- if (args["experimental-public"]) {
692
- throw new Error(
693
- "The --experimental-public field has been renamed to --assets"
694
- );
695
- }
696
-
697
- if (args.public) {
698
- throw new Error("The --public field has been renamed to --assets");
699
- }
700
-
701
- if ((args.assets || config.assets) && (args.site || config.site)) {
702
- throw new Error("Cannot use Assets and Workers Sites in the same Worker.");
703
- }
704
-
705
- if (args.assets) {
706
- logger.warn(
707
- "The --assets argument is experimental and may change or break at any time"
708
- );
709
- }
710
- const upstreamProtocol =
711
- args["upstream-protocol"] || config.dev.upstream_protocol;
712
- if (upstreamProtocol === "http") {
713
- logger.warn(
714
- "Setting upstream-protocol to http is not currently implemented.\n" +
715
- "If this is required in your project, please add your use case to the following issue:\n" +
716
- "https://github.com/cloudflare/wrangler2/issues/583."
717
- );
718
- }
719
- const nodeCompat = args.nodeCompat ?? config.node_compat;
720
- if (nodeCompat) {
721
- logger.warn(
722
- "Enabling node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
723
- );
724
- }
725
-
726
- if (args.experimentalEnableLocalPersistence) {
727
- logger.warn(
728
- `--experimental-enable-local-persistence is deprecated.\n` +
729
- `Move any existing data to .wrangler/state and use --persist, or\n` +
730
- `use --persist-to=./wrangler-local-state to keep using the old path.`
731
- );
732
- }
733
-
734
- const localPersistencePath = getLocalPersistencePath(
735
- args.persistTo,
736
- Boolean(args.persist),
737
- config.configPath
738
- );
739
-
740
- const cliDefines = collectKeyValues(args.define);
741
-
742
- return {
743
- entry,
744
- upstreamProtocol,
745
- nodeCompat,
746
- getLocalPort,
747
- getInspectorPort,
748
- zoneId,
749
- host,
750
- routes,
751
- cliDefines,
752
- localPersistencePath,
753
- };
754
- }
755
-
756
- async function getBindingsAndAssetPaths(
757
- args: StartDevOptions,
758
- configParam: Config
759
- ) {
760
- const cliVars = collectKeyValues(args.var);
761
-
762
- // now log all available bindings into the terminal
763
- const bindings = await getBindings(configParam, args.env, {
764
- kv: args.kv,
765
- vars: { ...args.vars, ...cliVars },
766
- durableObjects: args.durableObjects,
767
- r2: args.r2,
768
- d1Databases: args.d1Databases,
769
- });
770
-
771
- const maskedVars = maskVars(bindings, configParam);
772
-
773
- printBindings({
774
- ...bindings,
775
- vars: maskedVars,
776
- });
777
-
778
- const assetPaths =
779
- args.assets || configParam.assets
780
- ? getAssetPaths(configParam, args.assets)
781
- : getSiteAssetPaths(
782
- configParam,
783
- args.site,
784
- args.siteInclude,
785
- args.siteExclude
786
- );
787
- return { assetPaths, bindings };
788
- }
789
-
790
- async function getBindings(
791
- configParam: Config,
792
- env: string | undefined,
793
- args: AdditionalDevProps
794
- ): Promise<CfWorkerInit["bindings"]> {
795
- const bindings = {
796
- kv_namespaces: [
797
- ...(configParam.kv_namespaces || []).map(
798
- ({ binding, preview_id, id: _id }) => {
799
- // In `dev`, we make folks use a separate kv namespace called
800
- // `preview_id` instead of `id` so that they don't
801
- // break production data. So here we check that a `preview_id`
802
- // has actually been configured.
803
- // This whole block of code will be obsoleted in the future
804
- // when we have copy-on-write for previews on edge workers.
805
- if (!preview_id) {
806
- // TODO: This error has to be a _lot_ better, ideally just asking
807
- // to create a preview namespace for the user automatically
808
- throw new Error(
809
- `In development, you should use a separate kv namespace than the one you'd use in production. Please create a new kv namespace with "wrangler kv:namespace create <name> --preview" and add its id as preview_id to the kv_namespace "${binding}" in your wrangler.toml`
810
- ); // Ugh, I really don't like this message very much
811
- }
812
- return {
813
- binding,
814
- id: preview_id,
815
- };
816
- }
817
- ),
818
- ...(args.kv || []),
819
- ],
820
- // Use a copy of combinedVars since we're modifying it later
821
- vars: {
822
- ...getVarsForDev(configParam, env),
823
- ...args.vars,
824
- },
825
- metadata_binding: configParam.metadata_binding,
826
- wasm_modules: configParam.wasm_modules,
827
- text_blobs: configParam.text_blobs,
828
- data_blobs: configParam.data_blobs,
829
- durable_objects: {
830
- bindings: [
831
- ...(configParam.durable_objects || { bindings: [] }).bindings,
832
- ...(args.durableObjects || []),
833
- ],
834
- },
835
- queues: [
836
- ...(configParam.queues.producers || []).map((queue) => {
837
- return { binding: queue.binding, queue_name: queue.queue };
838
- }),
839
- ],
840
- r2_buckets: [
841
- ...(configParam.r2_buckets?.map(
842
- ({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
843
- // same idea as kv namespace preview id,
844
- // same copy-on-write TODO
845
- if (!preview_bucket_name) {
846
- throw new Error(
847
- `In development, you should use a separate r2 bucket than the one you'd use in production. Please create a new r2 bucket with "wrangler r2 bucket create <name>" and add its name as preview_bucket_name to the r2_buckets "${binding}" in your wrangler.toml`
848
- );
849
- }
850
- return {
851
- binding,
852
- bucket_name: preview_bucket_name,
853
- };
854
- }
855
- ) || []),
856
- ...(args.r2 || []),
857
- ],
858
- dispatch_namespaces: configParam.dispatch_namespaces,
859
- services: configParam.services,
860
- unsafe: configParam.unsafe?.bindings,
861
- logfwdr: configParam.logfwdr,
862
- d1_databases: identifyD1BindingsAsBeta([
863
- ...configParam.d1_databases,
864
- ...(args.d1Databases || []),
865
- ]),
866
- };
867
-
868
- return bindings;
869
- }