wrangler 0.0.0-dee034b → 0.0.0-df2daf2c

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