wrangler 0.0.0-e978986 → 0.0.0-e9e9872

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