wrangler 0.0.0-e9307365 → 0.0.0-e94b46ff7

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 (199) hide show
  1. package/README.md +35 -49
  2. package/bin/wrangler.js +25 -100
  3. package/config-schema.json +2277 -0
  4. package/package.json +182 -172
  5. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  6. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  7. package/templates/__tests__/tsconfig.json +8 -0
  8. package/templates/checked-fetch.js +19 -6
  9. package/templates/facade.d.ts +19 -0
  10. package/templates/gitignore +2 -3
  11. package/templates/init-tests/test-jest-new-worker.js +23 -0
  12. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  13. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  14. package/templates/middleware/common.ts +67 -0
  15. package/templates/middleware/loader-modules.ts +134 -0
  16. package/templates/middleware/loader-sw.ts +229 -0
  17. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  18. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  19. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  21. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  22. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  23. package/templates/middleware/middleware-pretty-error.ts +40 -0
  24. package/templates/middleware/middleware-scheduled.ts +29 -0
  25. package/templates/modules-watch-stub.js +4 -0
  26. package/templates/new-worker-scheduled.ts +1 -1
  27. package/templates/new-worker.js +1 -1
  28. package/templates/new-worker.ts +4 -1
  29. package/templates/pages-dev-pipeline.ts +33 -0
  30. package/templates/pages-dev-util.ts +55 -0
  31. package/templates/pages-shim.ts +1 -1
  32. package/templates/pages-template-plugin.ts +53 -18
  33. package/templates/pages-template-worker.ts +49 -12
  34. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  35. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  36. package/templates/tsconfig-sanity.ts +11 -0
  37. package/templates/tsconfig.init.json +22 -0
  38. package/templates/tsconfig.json +12 -102
  39. package/templates/tsconfig.tsbuildinfo +1 -0
  40. package/wrangler-dist/InspectorProxyWorker.js +460 -0
  41. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  42. package/wrangler-dist/ProxyWorker.js +238 -0
  43. package/wrangler-dist/ProxyWorker.js.map +6 -0
  44. package/wrangler-dist/cli.d.ts +26533 -167
  45. package/wrangler-dist/cli.js +146609 -141820
  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 -5416
  52. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  53. package/src/__tests__/config-cache.test.ts +0 -42
  54. package/src/__tests__/configuration.test.ts +0 -4022
  55. package/src/__tests__/dev.test.tsx +0 -1294
  56. package/src/__tests__/generate.test.ts +0 -93
  57. package/src/__tests__/guess-worker-format.test.ts +0 -85
  58. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  59. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  60. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  61. package/src/__tests__/helpers/mock-bin.ts +0 -36
  62. package/src/__tests__/helpers/mock-cfetch.ts +0 -263
  63. package/src/__tests__/helpers/mock-console.ts +0 -97
  64. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  65. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  66. package/src/__tests__/helpers/mock-istty.ts +0 -74
  67. package/src/__tests__/helpers/mock-kv.ts +0 -33
  68. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -250
  69. package/src/__tests__/helpers/mock-process.ts +0 -39
  70. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  71. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  72. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  73. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  74. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  75. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  76. package/src/__tests__/https-options.test.ts +0 -163
  77. package/src/__tests__/index.test.ts +0 -245
  78. package/src/__tests__/init.test.ts +0 -1992
  79. package/src/__tests__/jest.setup.ts +0 -150
  80. package/src/__tests__/kv.test.ts +0 -1671
  81. package/src/__tests__/logger.test.ts +0 -141
  82. package/src/__tests__/metrics.test.ts +0 -450
  83. package/src/__tests__/package-manager.test.ts +0 -353
  84. package/src/__tests__/pages.test.ts +0 -1261
  85. package/src/__tests__/parse.test.ts +0 -330
  86. package/src/__tests__/publish.test.ts +0 -6914
  87. package/src/__tests__/pubsub.test.ts +0 -367
  88. package/src/__tests__/r2.test.ts +0 -317
  89. package/src/__tests__/route.test.ts +0 -45
  90. package/src/__tests__/secret.test.ts +0 -492
  91. package/src/__tests__/tail.test.ts +0 -698
  92. package/src/__tests__/test-old-node-version.js +0 -31
  93. package/src/__tests__/user.test.ts +0 -144
  94. package/src/__tests__/validate-dev-props.test.ts +0 -56
  95. package/src/__tests__/version.test.ts +0 -35
  96. package/src/__tests__/whoami.test.tsx +0 -289
  97. package/src/__tests__/worker-namespace.test.ts +0 -327
  98. package/src/abort.d.ts +0 -3
  99. package/src/api/dev.ts +0 -86
  100. package/src/api/index.ts +0 -1
  101. package/src/bundle-reporter.tsx +0 -29
  102. package/src/bundle.ts +0 -472
  103. package/src/cfetch/index.ts +0 -121
  104. package/src/cfetch/internal.ts +0 -181
  105. package/src/cli.ts +0 -27
  106. package/src/config/README.md +0 -107
  107. package/src/config/config.ts +0 -263
  108. package/src/config/diagnostics.ts +0 -80
  109. package/src/config/environment.ts +0 -485
  110. package/src/config/index.ts +0 -251
  111. package/src/config/validation-helpers.ts +0 -584
  112. package/src/config/validation.ts +0 -1856
  113. package/src/config-cache.ts +0 -84
  114. package/src/create-worker-preview.ts +0 -257
  115. package/src/create-worker-upload-form.ts +0 -288
  116. package/src/dev/dev-vars.ts +0 -36
  117. package/src/dev/dev.tsx +0 -531
  118. package/src/dev/local.tsx +0 -385
  119. package/src/dev/remote.tsx +0 -413
  120. package/src/dev/use-esbuild.ts +0 -175
  121. package/src/dev/validate-dev-props.ts +0 -31
  122. package/src/dev-registry.tsx +0 -157
  123. package/src/dev.tsx +0 -616
  124. package/src/dialogs.tsx +0 -135
  125. package/src/durable.ts +0 -102
  126. package/src/entry.ts +0 -328
  127. package/src/environment-variables.ts +0 -35
  128. package/src/errors.ts +0 -11
  129. package/src/faye-websocket.d.ts +0 -6
  130. package/src/generate.ts +0 -131
  131. package/src/git-client.ts +0 -42
  132. package/src/global-wrangler-config-path.ts +0 -26
  133. package/src/https-options.ts +0 -127
  134. package/src/index.tsx +0 -2184
  135. package/src/init.ts +0 -549
  136. package/src/inspect.ts +0 -750
  137. package/src/intl-polyfill.d.ts +0 -139
  138. package/src/is-interactive.ts +0 -12
  139. package/src/kv.ts +0 -430
  140. package/src/logger.ts +0 -73
  141. package/src/metrics/index.ts +0 -5
  142. package/src/metrics/is-ci.ts +0 -14
  143. package/src/metrics/metrics-config.ts +0 -239
  144. package/src/metrics/metrics-dispatcher.ts +0 -96
  145. package/src/metrics/metrics-usage-headers.ts +0 -24
  146. package/src/metrics/send-event.ts +0 -92
  147. package/src/miniflare-cli/README.md +0 -30
  148. package/src/miniflare-cli/assets.ts +0 -543
  149. package/src/miniflare-cli/enum-keys.ts +0 -17
  150. package/src/miniflare-cli/index.ts +0 -85
  151. package/src/miniflare-cli/request-context.ts +0 -40
  152. package/src/module-collection.ts +0 -258
  153. package/src/open-in-browser.ts +0 -17
  154. package/src/package-manager.ts +0 -219
  155. package/src/pages/build.tsx +0 -204
  156. package/src/pages/constants.ts +0 -8
  157. package/src/pages/deployments.tsx +0 -106
  158. package/src/pages/dev.tsx +0 -414
  159. package/src/pages/functions/buildPlugin.ts +0 -105
  160. package/src/pages/functions/buildWorker.ts +0 -151
  161. package/src/pages/functions/filepath-routing.test.ts +0 -234
  162. package/src/pages/functions/filepath-routing.ts +0 -189
  163. package/src/pages/functions/identifiers.ts +0 -78
  164. package/src/pages/functions/routes.ts +0 -151
  165. package/src/pages/index.tsx +0 -84
  166. package/src/pages/projects.tsx +0 -163
  167. package/src/pages/publish.tsx +0 -338
  168. package/src/pages/types.ts +0 -40
  169. package/src/pages/upload.tsx +0 -415
  170. package/src/pages/utils.ts +0 -12
  171. package/src/parse.ts +0 -286
  172. package/src/paths.ts +0 -26
  173. package/src/preview.ts +0 -31
  174. package/src/proxy.ts +0 -476
  175. package/src/publish.ts +0 -807
  176. package/src/pubsub/index.ts +0 -286
  177. package/src/pubsub/pubsub-commands.tsx +0 -623
  178. package/src/r2.ts +0 -118
  179. package/src/selfsigned.d.ts +0 -29
  180. package/src/sites.tsx +0 -372
  181. package/src/tail/filters.ts +0 -279
  182. package/src/tail/index.ts +0 -299
  183. package/src/tail/printing.ts +0 -67
  184. package/src/update-check.ts +0 -19
  185. package/src/user/choose-account.tsx +0 -69
  186. package/src/user/env-vars.ts +0 -46
  187. package/src/user/generate-auth-url.ts +0 -33
  188. package/src/user/generate-random-state.ts +0 -16
  189. package/src/user/index.ts +0 -3
  190. package/src/user/user.tsx +0 -1189
  191. package/src/whoami.tsx +0 -169
  192. package/src/worker-namespace.ts +0 -206
  193. package/src/worker.ts +0 -203
  194. package/src/zones.ts +0 -76
  195. package/templates/first-party-worker-module-facade.ts +0 -18
  196. package/templates/format-dev-errors.ts +0 -32
  197. package/templates/serve-static-assets.ts +0 -63
  198. package/templates/service-bindings-module-facade.js +0 -51
  199. package/templates/service-bindings-sw-facade.js +0 -39
package/src/dev.tsx DELETED
@@ -1,616 +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 { fetch } from "undici";
7
- import { findWranglerToml, printBindings, readConfig } from "./config";
8
- import Dev from "./dev/dev";
9
- import { getVarsForDev } from "./dev/dev-vars";
10
-
11
- import { getEntry } from "./entry";
12
- import { logger } from "./logger";
13
- import * as metrics from "./metrics";
14
- import { getAssetPaths, getSiteAssetPaths } from "./sites";
15
- import { getAccountFromCache } from "./user";
16
- import { getZoneIdFromHost, getZoneForRoute, getHostFromRoute } from "./zones";
17
- import {
18
- printWranglerBanner,
19
- DEFAULT_LOCAL_PORT,
20
- type ConfigPath,
21
- getScriptName,
22
- getDevCompatibilityDate,
23
- getRules,
24
- isLegacyEnv,
25
- DEFAULT_INSPECTOR_PORT,
26
- } from "./index";
27
-
28
- import type { Config } from "./config";
29
- import type { Route } from "./config/environment";
30
- import type { EnablePagesAssetsServiceBindingOptions } from "./miniflare-cli";
31
- import type { CfWorkerInit } from "./worker";
32
- import type { RequestInit } from "undici";
33
- import type { Argv, ArgumentsCamelCase } from "yargs";
34
-
35
- interface DevArgs {
36
- config?: string;
37
- script?: string;
38
- name?: string;
39
- bundle?: boolean;
40
- build?: boolean;
41
- format?: string;
42
- env?: string;
43
- "compatibility-date"?: string;
44
- "compatibility-flags"?: string[];
45
- latest?: boolean;
46
- ip?: string;
47
- inspect?: boolean;
48
- port?: number;
49
- "inspector-port"?: number;
50
- routes?: string[];
51
- host?: string;
52
- "local-protocol"?: "http" | "https";
53
- "local-upstream"?: string | undefined;
54
- "experimental-public"?: string;
55
- public?: string;
56
- assets?: string;
57
- site?: string;
58
- "site-include"?: string[];
59
- "site-exclude"?: string[];
60
- "upstream-protocol"?: "http" | "https";
61
- "jsx-factory"?: string;
62
- "jsx-fragment"?: string;
63
- tsconfig?: string;
64
- local?: boolean;
65
- minify?: boolean;
66
- "node-compat"?: boolean;
67
- "experimental-enable-local-persistence"?: boolean;
68
- "live-reload"?: boolean;
69
- onReady?: () => void;
70
- logLevel?: "none" | "error" | "log" | "warn" | "debug";
71
- logPrefix?: string;
72
- showInteractiveDevSession?: boolean;
73
- }
74
-
75
- export function devOptions(yargs: Argv): Argv<DevArgs> {
76
- return (
77
- yargs
78
- .positional("script", {
79
- describe: "The path to an entry point for your worker",
80
- type: "string",
81
- })
82
- .option("name", {
83
- describe: "Name of the worker",
84
- type: "string",
85
- requiresArg: true,
86
- })
87
- // We want to have a --no-bundle flag, but yargs requires that
88
- // we also have a --bundle flag (that it adds the --no to by itself)
89
- // So we make a --bundle flag, but hide it, and then add a --no-bundle flag
90
- // that's visible to the user but doesn't "do" anything.
91
- .option("bundle", {
92
- describe: "Run wrangler's compilation step before publishing",
93
- type: "boolean",
94
- hidden: true,
95
- })
96
- .option("no-bundle", {
97
- describe: "Skip internal build steps and directly publish script",
98
- type: "boolean",
99
- default: false,
100
- })
101
- .option("format", {
102
- choices: ["modules", "service-worker"] as const,
103
- describe: "Choose an entry type",
104
- deprecated: true,
105
- })
106
- .option("env", {
107
- describe: "Perform on a specific environment",
108
- type: "string",
109
- requiresArg: true,
110
- alias: "e",
111
- })
112
- .option("compatibility-date", {
113
- describe: "Date to use for compatibility checks",
114
- type: "string",
115
- requiresArg: true,
116
- })
117
- .option("compatibility-flags", {
118
- describe: "Flags to use for compatibility checks",
119
- alias: "compatibility-flag",
120
- type: "string",
121
- requiresArg: true,
122
- array: true,
123
- })
124
- .option("latest", {
125
- describe: "Use the latest version of the worker runtime",
126
- type: "boolean",
127
- default: true,
128
- })
129
- .option("ip", {
130
- describe: "IP address to listen on, defaults to `localhost`",
131
- type: "string",
132
- requiresArg: true,
133
- })
134
- .option("port", {
135
- describe: "Port to listen on",
136
- type: "number",
137
- })
138
- .option("inspector-port", {
139
- describe: "Port for devtools to connect to",
140
- type: "number",
141
- })
142
- .option("routes", {
143
- describe: "Routes to upload",
144
- alias: "route",
145
- type: "string",
146
- requiresArg: true,
147
- array: true,
148
- })
149
- .option("host", {
150
- type: "string",
151
- requiresArg: true,
152
- describe:
153
- "Host to forward requests to, defaults to the zone of project",
154
- })
155
- .option("local-protocol", {
156
- describe: "Protocol to listen to requests on, defaults to http.",
157
- choices: ["http", "https"] as const,
158
- })
159
- .options("local-upstream", {
160
- type: "string",
161
- describe:
162
- "Host to act as origin in local mode, defaults to dev.host or route",
163
- })
164
- .option("experimental-public", {
165
- describe: "Static assets to be served",
166
- type: "string",
167
- requiresArg: true,
168
- deprecated: true,
169
- hidden: true,
170
- })
171
- .option("assets", {
172
- describe: "Static assets to be served",
173
- type: "string",
174
- requiresArg: true,
175
- })
176
- .option("site", {
177
- describe: "Root folder of static assets for Workers Sites",
178
- type: "string",
179
- requiresArg: true,
180
- })
181
- .option("site-include", {
182
- describe:
183
- "Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
184
- type: "string",
185
- requiresArg: true,
186
- array: true,
187
- })
188
- .option("site-exclude", {
189
- describe:
190
- "Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
191
- type: "string",
192
- requiresArg: true,
193
- array: true,
194
- })
195
- .option("upstream-protocol", {
196
- describe: "Protocol to forward requests to host on, defaults to https.",
197
- choices: ["http", "https"] as const,
198
- })
199
- .option("jsx-factory", {
200
- describe: "The function that is called for each JSX element",
201
- type: "string",
202
- requiresArg: true,
203
- })
204
- .option("jsx-fragment", {
205
- describe: "The function that is called for each JSX fragment",
206
- type: "string",
207
- requiresArg: true,
208
- })
209
- .option("tsconfig", {
210
- describe: "Path to a custom tsconfig.json file",
211
- type: "string",
212
- requiresArg: true,
213
- })
214
- .option("local", {
215
- alias: "l",
216
- describe: "Run on my machine",
217
- type: "boolean",
218
- default: false, // I bet this will a point of contention. We'll revisit it.
219
- })
220
- .option("minify", {
221
- describe: "Minify the script",
222
- type: "boolean",
223
- })
224
- .option("node-compat", {
225
- describe: "Enable node.js compatibility",
226
- type: "boolean",
227
- })
228
- .option("experimental-enable-local-persistence", {
229
- describe: "Enable persistence for this session (only for local mode)",
230
- type: "boolean",
231
- })
232
- .option("live-reload", {
233
- // TODO: Add back in once we have remote `--live-reload`
234
- hidden: true,
235
- // describe: "Auto reload HTML pages when change is detected",
236
- type: "boolean",
237
- })
238
- .option("inspect", {
239
- describe: "Enable dev tools",
240
- type: "boolean",
241
- deprecated: true,
242
- })
243
- .option("legacy-env", {
244
- type: "boolean",
245
- describe: "Use legacy environments",
246
- hidden: true,
247
- })
248
- );
249
- }
250
-
251
- export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
252
- let watcher;
253
- try {
254
- const devInstance = await startDev(args);
255
- watcher = devInstance.watcher;
256
- const { waitUntilExit } = devInstance.devReactElement;
257
- await waitUntilExit();
258
- } finally {
259
- await watcher?.close();
260
- }
261
- }
262
-
263
- export type AdditionalDevProps = {
264
- vars?: {
265
- [key: string]: unknown;
266
- };
267
- kv?: {
268
- binding: string;
269
- id: string;
270
- preview_id?: string;
271
- }[];
272
- durableObjects?: {
273
- name: string;
274
- class_name: string;
275
- script_name?: string | undefined;
276
- environment?: string | undefined;
277
- }[];
278
- };
279
- type StartDevOptions = ArgumentsCamelCase<DevArgs> &
280
- // These options can be passed in directly when called with the `wrangler.dev()` API.
281
- // They aren't exposed as CLI arguments.
282
- AdditionalDevProps & {
283
- forceLocal?: boolean;
284
- enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
285
- };
286
-
287
- export async function startDev(args: StartDevOptions) {
288
- let watcher: ReturnType<typeof watch> | undefined;
289
- let rerender: (node: React.ReactNode) => void | undefined;
290
- try {
291
- if (args.logLevel) {
292
- // we don't define a "none" logLevel, so "error" will do for now.
293
- logger.loggerLevel = args.logLevel === "none" ? "error" : args.logLevel;
294
- }
295
- await printWranglerBanner();
296
-
297
- const configPath =
298
- (args.config as ConfigPath) ||
299
- ((args.script &&
300
- findWranglerToml(path.dirname(args.script))) as ConfigPath);
301
- let config = readConfig(configPath, args);
302
-
303
- if (config.configPath) {
304
- watcher = watch(config.configPath, {
305
- persistent: true,
306
- }).on("change", async (_event) => {
307
- // TODO: Do we need to handle different `_event` types differently?
308
- // e.g. what if the file is deleted, or added?
309
- config = readConfig(configPath, args);
310
- if (config.configPath) {
311
- logger.log(`${path.basename(config.configPath)} changed...`);
312
- rerender(await getDevReactElement(config));
313
- }
314
- });
315
- }
316
-
317
- const entry = await getEntry(
318
- { assets: args.assets, script: args.script },
319
- config,
320
- "dev"
321
- );
322
-
323
- await metrics.sendMetricsEvent(
324
- "run dev",
325
- {
326
- local: args.local,
327
- usesTypeScript: /\.tsx?$/.test(entry.file),
328
- },
329
- { sendMetrics: config.send_metrics, offline: args.local }
330
- );
331
-
332
- if (config.services && config.services.length > 0) {
333
- logger.warn(
334
- `This worker is bound to live services: ${config.services
335
- .map(
336
- (service) =>
337
- `${service.binding} (${service.service}${
338
- service.environment ? `@${service.environment}` : ""
339
- })`
340
- )
341
- .join(", ")}`
342
- );
343
- }
344
-
345
- if (args.inspect) {
346
- //devtools are enabled by default, but we still need to disable them if the caller doesn't want them
347
- logger.warn(
348
- "Passing --inspect is unnecessary, now you can always connect to devtools."
349
- );
350
- }
351
-
352
- if (args["experimental-public"]) {
353
- throw new Error(
354
- "The --experimental-public field has been renamed to --assets"
355
- );
356
- }
357
-
358
- if (args.public) {
359
- throw new Error("The --public field has been renamed to --assets");
360
- }
361
-
362
- if ((args.assets || config.assets) && (args.site || config.site)) {
363
- throw new Error(
364
- "Cannot use Assets and Workers Sites in the same Worker."
365
- );
366
- }
367
-
368
- if (args.assets) {
369
- logger.warn(
370
- "The --assets argument is experimental and may change or break at any time"
371
- );
372
- }
373
-
374
- const upstreamProtocol =
375
- args["upstream-protocol"] || config.dev.upstream_protocol;
376
- if (upstreamProtocol === "http") {
377
- logger.warn(
378
- "Setting upstream-protocol to http is not currently implemented.\n" +
379
- "If this is required in your project, please add your use case to the following issue:\n" +
380
- "https://github.com/cloudflare/wrangler2/issues/583."
381
- );
382
- }
383
-
384
- // TODO: if worker_dev = false and no routes, then error (only for dev)
385
-
386
- // Compute zone info from the `host` and `route` args and config;
387
- let host = args.host || config.dev.host;
388
- let zoneId: string | undefined;
389
- const routes: Route[] | undefined =
390
- args.routes || (config.route && [config.route]) || config.routes;
391
-
392
- if (args.forceLocal) {
393
- args.local = true;
394
- }
395
-
396
- if (!args.local) {
397
- if (host) {
398
- zoneId = await getZoneIdFromHost(host);
399
- }
400
- if (!zoneId && routes) {
401
- const firstRoute = routes[0];
402
- const zone = await getZoneForRoute(firstRoute);
403
- if (zone) {
404
- zoneId = zone.id;
405
- host = zone.host;
406
- }
407
- }
408
- } else if (!host) {
409
- if (routes) {
410
- const firstRoute = routes[0];
411
- host = getHostFromRoute(firstRoute);
412
- }
413
- }
414
-
415
- const nodeCompat = args.nodeCompat ?? config.node_compat;
416
- if (nodeCompat) {
417
- logger.warn(
418
- "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."
419
- );
420
- }
421
-
422
- const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT);
423
- const getInspectorPort = memoizeGetPort(DEFAULT_INSPECTOR_PORT);
424
-
425
- // eslint-disable-next-line no-inner-declarations
426
- async function getDevReactElement(configParam: Config) {
427
- // now log all available bindings into the terminal
428
- const bindings = await getBindings(configParam, {
429
- kv: args.kv,
430
- vars: args.vars,
431
- durableObjects: args.durableObjects,
432
- });
433
-
434
- // mask anything that was overridden in .dev.vars
435
- // so that we don't log potential secrets into the terminal
436
- const maskedVars = { ...bindings.vars };
437
- for (const key of Object.keys(maskedVars)) {
438
- if (maskedVars[key] !== configParam.vars[key]) {
439
- // This means it was overridden in .dev.vars
440
- // so let's mask it
441
- maskedVars[key] = "(hidden)";
442
- }
443
- }
444
-
445
- printBindings({
446
- ...bindings,
447
- vars: maskedVars,
448
- });
449
-
450
- const assetPaths =
451
- args.assets || config.assets
452
- ? getAssetPaths(config, args.assets)
453
- : getSiteAssetPaths(
454
- config,
455
- args.site,
456
- args.siteInclude,
457
- args.siteExclude
458
- );
459
-
460
- return (
461
- <Dev
462
- name={getScriptName({ name: args.name, env: args.env }, config)}
463
- noBundle={!(args.bundle ?? !config.no_bundle)}
464
- entry={entry}
465
- env={args.env}
466
- zone={zoneId}
467
- host={host}
468
- routes={routes}
469
- rules={getRules(config)}
470
- legacyEnv={isLegacyEnv(config)}
471
- minify={args.minify ?? config.minify}
472
- nodeCompat={nodeCompat}
473
- build={config.build || {}}
474
- define={config.define}
475
- initialMode={args.local ? "local" : "remote"}
476
- jsxFactory={args["jsx-factory"] || config.jsx_factory}
477
- jsxFragment={args["jsx-fragment"] || config.jsx_fragment}
478
- tsconfig={args.tsconfig ?? config.tsconfig}
479
- upstreamProtocol={upstreamProtocol}
480
- localProtocol={args.localProtocol || config.dev.local_protocol}
481
- localUpstream={args["local-upstream"] || host}
482
- enableLocalPersistence={
483
- args.experimentalEnableLocalPersistence || false
484
- }
485
- liveReload={args.liveReload || false}
486
- accountId={config.account_id || getAccountFromCache()?.id}
487
- assetPaths={assetPaths}
488
- assetsConfig={config.assets}
489
- port={args.port || config.dev.port || (await getLocalPort())}
490
- ip={args.ip || config.dev.ip}
491
- inspectorPort={
492
- args["inspector-port"] ||
493
- config.dev.inspector_port ||
494
- (await getInspectorPort())
495
- }
496
- isWorkersSite={Boolean(args.site || config.site)}
497
- compatibilityDate={getDevCompatibilityDate(
498
- config,
499
- args["compatibility-date"]
500
- )}
501
- compatibilityFlags={
502
- args["compatibility-flags"] || config.compatibility_flags
503
- }
504
- usageModel={config.usage_model}
505
- bindings={bindings}
506
- crons={config.triggers.crons}
507
- logLevel={args.logLevel}
508
- logPrefix={args.logPrefix}
509
- onReady={args.onReady}
510
- inspect={args.inspect ?? true}
511
- showInteractiveDevSession={args.showInteractiveDevSession}
512
- forceLocal={args.forceLocal}
513
- enablePagesAssetsServiceBinding={args.enablePagesAssetsServiceBinding}
514
- firstPartyWorker={config.first_party_worker}
515
- sendMetrics={config.send_metrics}
516
- />
517
- );
518
- }
519
- const devReactElement = render(await getDevReactElement(config));
520
- rerender = devReactElement.rerender;
521
- return {
522
- devReactElement,
523
- watcher,
524
- stop: async () => {
525
- devReactElement.unmount();
526
- await watcher?.close();
527
- },
528
- fetch: async (init?: RequestInit) => {
529
- //TODO: we are not guaranteed to be assigned this port, we should fix this ASAP
530
- const port = args.port || config.dev.port || (await getLocalPort());
531
- const address = args.ip || config.dev.ip || "localhost";
532
-
533
- return await fetch(`http://${address}:${port}/`, init);
534
- },
535
- };
536
- } finally {
537
- await watcher?.close();
538
- }
539
- }
540
-
541
- /**
542
- * Avoiding calling `getPort()` multiple times by memoizing the first result.
543
- */
544
- function memoizeGetPort(defaultPort: number) {
545
- let portValue: number;
546
- return async () => {
547
- return portValue || (portValue = await getPort({ port: defaultPort }));
548
- };
549
- }
550
-
551
- async function getBindings(
552
- configParam: Config,
553
- args: AdditionalDevProps
554
- ): Promise<CfWorkerInit["bindings"]> {
555
- const bindings = {
556
- kv_namespaces: [
557
- ...(configParam.kv_namespaces || []).map(
558
- ({ binding, preview_id, id: _id }) => {
559
- // In `dev`, we make folks use a separate kv namespace called
560
- // `preview_id` instead of `id` so that they don't
561
- // break production data. So here we check that a `preview_id`
562
- // has actually been configured.
563
- // This whole block of code will be obsoleted in the future
564
- // when we have copy-on-write for previews on edge workers.
565
- if (!preview_id) {
566
- // TODO: This error has to be a _lot_ better, ideally just asking
567
- // to create a preview namespace for the user automatically
568
- throw new Error(
569
- `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`
570
- ); // Ugh, I really don't like this message very much
571
- }
572
- return {
573
- binding,
574
- id: preview_id,
575
- };
576
- }
577
- ),
578
- ...(args.kv || []),
579
- ],
580
- // Use a copy of combinedVars since we're modifying it later
581
- vars: {
582
- ...getVarsForDev(configParam),
583
- ...args.vars,
584
- },
585
- wasm_modules: configParam.wasm_modules,
586
- text_blobs: configParam.text_blobs,
587
- data_blobs: configParam.data_blobs,
588
- durable_objects: {
589
- bindings: [
590
- ...(configParam.durable_objects || { bindings: [] }).bindings,
591
- ...(args.durableObjects || []),
592
- ],
593
- },
594
- r2_buckets: configParam.r2_buckets?.map(
595
- ({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
596
- // same idea as kv namespace preview id,
597
- // same copy-on-write TODO
598
- if (!preview_bucket_name) {
599
- throw new Error(
600
- `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`
601
- );
602
- }
603
- return {
604
- binding,
605
- bucket_name: preview_bucket_name,
606
- };
607
- }
608
- ),
609
- worker_namespaces: configParam.worker_namespaces,
610
- services: configParam.services,
611
- unsafe: configParam.unsafe?.bindings,
612
- logfwdr: configParam.logfwdr,
613
- };
614
-
615
- return bindings;
616
- }