wrangler 0.0.0-e178d6fb → 0.0.0-e19903d7

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