wrangler 2.8.0 → 2.9.0

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 (95) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/d1/d1.test.ts +24 -62
  3. package/src/__tests__/d1/migrate.test.ts +48 -0
  4. package/src/__tests__/deployments.test.ts +4 -4
  5. package/src/__tests__/dev.test.tsx +5 -4
  6. package/src/__tests__/helpers/msw/handlers/deployments.ts +10 -18
  7. package/src/__tests__/helpers/msw/handlers/namespaces.ts +18 -41
  8. package/src/__tests__/helpers/msw/handlers/r2.ts +14 -34
  9. package/src/__tests__/helpers/msw/handlers/script.ts +9 -28
  10. package/src/__tests__/helpers/msw/handlers/user.ts +13 -24
  11. package/src/__tests__/helpers/msw/handlers/zones.ts +6 -8
  12. package/src/__tests__/index.test.ts +35 -28
  13. package/src/__tests__/kv.test.ts +55 -44
  14. package/src/__tests__/pages.test.ts +61 -57
  15. package/src/__tests__/parse.test.ts +106 -0
  16. package/src/__tests__/publish.test.ts +126 -0
  17. package/src/__tests__/pubsub.test.ts +15 -12
  18. package/src/__tests__/queues.test.ts +35 -28
  19. package/src/__tests__/r2.test.ts +36 -55
  20. package/src/__tests__/tail.test.ts +6 -18
  21. package/src/__tests__/tsconfig.tsbuildinfo +1 -1
  22. package/src/__tests__/user.test.ts +0 -1
  23. package/src/__tests__/whoami.test.tsx +6 -17
  24. package/src/__tests__/worker-namespace.test.ts +81 -68
  25. package/src/api/dev.ts +80 -11
  26. package/src/api/index.ts +1 -0
  27. package/src/api/pages/index.ts +5 -0
  28. package/src/api/pages/publish.tsx +324 -0
  29. package/src/bundle.ts +63 -11
  30. package/src/cli.ts +2 -2
  31. package/src/config/config.ts +7 -0
  32. package/src/config/environment.ts +12 -10
  33. package/src/config/index.ts +24 -20
  34. package/src/d1/backups.tsx +20 -24
  35. package/src/d1/create.tsx +6 -5
  36. package/src/d1/delete.ts +7 -10
  37. package/src/d1/execute.tsx +82 -84
  38. package/src/d1/index.ts +5 -6
  39. package/src/d1/list.tsx +21 -9
  40. package/src/d1/migrations/apply.tsx +7 -5
  41. package/src/d1/migrations/create.tsx +7 -10
  42. package/src/d1/migrations/list.tsx +7 -5
  43. package/src/d1/migrations/options.ts +2 -2
  44. package/src/d1/options.ts +3 -3
  45. package/src/d1/utils.ts +1 -1
  46. package/src/delete.ts +5 -8
  47. package/src/deployments.ts +16 -6
  48. package/src/deprecated/index.ts +7 -8
  49. package/src/dev/local.tsx +1 -10
  50. package/src/dev/start-server.ts +5 -10
  51. package/src/dev/use-esbuild.ts +1 -0
  52. package/src/dev.tsx +42 -80
  53. package/src/dispatch-namespace.ts +20 -16
  54. package/src/docs/index.ts +7 -8
  55. package/src/entry.ts +1 -2
  56. package/src/generate/index.ts +5 -7
  57. package/src/index.ts +23 -22
  58. package/src/init.ts +5 -7
  59. package/src/kv/index.ts +15 -17
  60. package/src/metrics/send-event.ts +2 -1
  61. package/src/pages/build.ts +9 -127
  62. package/src/pages/buildFunctions.ts +129 -0
  63. package/src/pages/deployment-tails.ts +7 -10
  64. package/src/pages/deployments.tsx +6 -4
  65. package/src/pages/dev.ts +27 -19
  66. package/src/pages/functions/buildPlugin.ts +1 -0
  67. package/src/pages/functions/buildWorker.ts +8 -2
  68. package/src/pages/functions/tsconfig.tsbuildinfo +1 -1
  69. package/src/pages/functions.ts +8 -4
  70. package/src/pages/index.ts +3 -3
  71. package/src/pages/projects.tsx +7 -12
  72. package/src/pages/publish.tsx +15 -239
  73. package/src/pages/types.ts +5 -0
  74. package/src/pages/upload.tsx +6 -4
  75. package/src/parse.ts +23 -1
  76. package/src/publish/index.ts +19 -15
  77. package/src/publish/publish.ts +3 -2
  78. package/src/pubsub/pubsub-commands.ts +18 -19
  79. package/src/queues/cli/commands/consumer/add.ts +18 -24
  80. package/src/queues/cli/commands/consumer/index.ts +3 -6
  81. package/src/queues/cli/commands/consumer/remove.ts +11 -18
  82. package/src/queues/cli/commands/create.ts +8 -8
  83. package/src/queues/cli/commands/delete.ts +8 -8
  84. package/src/queues/cli/commands/index.ts +3 -4
  85. package/src/queues/cli/commands/list.ts +8 -8
  86. package/src/r2/index.ts +28 -28
  87. package/src/secret/index.ts +9 -14
  88. package/src/tail/index.ts +6 -8
  89. package/src/yargs-types.ts +18 -5
  90. package/templates/checked-fetch.js +9 -1
  91. package/templates/d1-beta-facade.js +1 -1
  92. package/templates/middleware/loader-modules.ts +2 -0
  93. package/templates/tsconfig.tsbuildinfo +1 -1
  94. package/wrangler-dist/cli.d.ts +132 -10
  95. package/wrangler-dist/cli.js +2474 -1635
@@ -0,0 +1,324 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { dirname, join, resolve as resolvePath } from "node:path";
4
+ import { cwd } from "node:process";
5
+ import { File, FormData } from "undici";
6
+ import { fetchResult } from "../../cfetch";
7
+ import { FatalError } from "../../errors";
8
+ import { logger } from "../../logger";
9
+ import { buildFunctions } from "../../pages/buildFunctions";
10
+ import {
11
+ FunctionsNoRoutesError,
12
+ getFunctionsNoRoutesWarning,
13
+ } from "../../pages/errors";
14
+ import {
15
+ buildRawWorker,
16
+ checkRawWorker,
17
+ } from "../../pages/functions/buildWorker";
18
+ import { validateRoutes } from "../../pages/functions/routes-validation";
19
+ import { upload } from "../../pages/upload";
20
+ import type { Project, Deployment } from "@cloudflare/types";
21
+
22
+ interface PagesPublishOptions {
23
+ /**
24
+ * Path to static assets to publish to Pages
25
+ */
26
+ directory: string;
27
+ /**
28
+ * The Cloudflare Account ID that owns the project that's
29
+ * being published
30
+ */
31
+ accountId: string;
32
+ /**
33
+ * The name of the project to be published
34
+ */
35
+ projectName: string;
36
+ /**
37
+ * Branch name to use. Defaults to production branch
38
+ */
39
+ branch?: string;
40
+ /**
41
+ * Whether or not to skip local file upload result caching
42
+ */
43
+ skipCaching?: boolean;
44
+ /**
45
+ * Commit message associated to deployment
46
+ */
47
+ commitMessage?: string;
48
+ /**
49
+ * Commit hash associated to deployment
50
+ */
51
+ commitHash?: string;
52
+ /**
53
+ * Whether or not the deployment should be considered to be
54
+ * in a dirty commit state
55
+ */
56
+ commitDirty?: boolean;
57
+ /**
58
+ * Path to the project's functions directory. Default uses
59
+ * the current working directory + /functions since this is
60
+ * typically called in a CLI
61
+ */
62
+ functionsDirectory?: string;
63
+
64
+ /**
65
+ * Whether to run bundling on `_worker.js` before deploying.
66
+ * Default: false
67
+ */
68
+ bundle?: boolean;
69
+
70
+ // TODO: Allow passing in the API key and plumb it through
71
+ // to the API calls so that the publish function does not
72
+ // rely on the `CLOUDFLARE_API_KEY` environment variable
73
+ }
74
+
75
+ /**
76
+ * Publish a directory to an account/project.
77
+ * NOTE: You will need the `CLOUDFLARE_API_KEY` environment
78
+ * variable set
79
+ */
80
+ export async function publish({
81
+ directory,
82
+ accountId,
83
+ projectName,
84
+ branch,
85
+ skipCaching,
86
+ commitMessage,
87
+ commitHash,
88
+ commitDirty,
89
+ functionsDirectory: customFunctionsDirectory,
90
+ bundle,
91
+ }: PagesPublishOptions) {
92
+ let _headers: string | undefined,
93
+ _redirects: string | undefined,
94
+ _routesGenerated: string | undefined,
95
+ _routesCustom: string | undefined,
96
+ _workerJS: string | undefined;
97
+
98
+ const workerScriptPath = resolvePath(directory, "_worker.js");
99
+
100
+ try {
101
+ _headers = readFileSync(join(directory, "_headers"), "utf-8");
102
+ } catch {}
103
+
104
+ try {
105
+ _redirects = readFileSync(join(directory, "_redirects"), "utf-8");
106
+ } catch {}
107
+
108
+ try {
109
+ /**
110
+ * Developers can specify a custom _routes.json file, for projects with Pages
111
+ * Functions or projects in Advanced Mode
112
+ */
113
+ _routesCustom = readFileSync(join(directory, "_routes.json"), "utf-8");
114
+ } catch {}
115
+
116
+ try {
117
+ _workerJS = readFileSync(workerScriptPath, "utf-8");
118
+ } catch {}
119
+
120
+ // Grab the bindings from the API, we need these for shims and other such hacky inserts
121
+ const project = await fetchResult<Project>(
122
+ `/accounts/${accountId}/pages/projects/${projectName}`
123
+ );
124
+ let isProduction = true;
125
+ if (branch) {
126
+ isProduction = project.production_branch === branch;
127
+ }
128
+
129
+ /**
130
+ * Evaluate if this is an Advanced Mode or Pages Functions project. If Advanced Mode, we'll
131
+ * go ahead and upload `_worker.js` as is, but if Pages Functions, we need to attempt to build
132
+ * Functions first and exit if it failed
133
+ */
134
+ let builtFunctions: string | undefined = undefined;
135
+ const functionsDirectory =
136
+ customFunctionsDirectory || join(cwd(), "functions");
137
+ const routesOutputPath = !existsSync(join(directory, "_routes.json"))
138
+ ? join(tmpdir(), `_routes-${Math.random()}.json`)
139
+ : undefined;
140
+
141
+ // Routing configuration displayed in the Functions tab of a deployment in Dash
142
+ let filepathRoutingConfig: string | undefined;
143
+
144
+ const d1Databases = Object.keys(
145
+ project.deployment_configs[isProduction ? "production" : "preview"]
146
+ .d1_databases ?? {}
147
+ );
148
+
149
+ if (!_workerJS && existsSync(functionsDirectory)) {
150
+ const outfile = join(tmpdir(), `./functionsWorker-${Math.random()}.js`);
151
+ const outputConfigPath = join(
152
+ tmpdir(),
153
+ `functions-filepath-routing-config-${Math.random()}.json`
154
+ );
155
+
156
+ try {
157
+ await buildFunctions({
158
+ outfile,
159
+ outputConfigPath,
160
+ functionsDirectory,
161
+ onEnd: () => {},
162
+ buildOutputDirectory: dirname(outfile),
163
+ routesOutputPath,
164
+ local: false,
165
+ d1Databases,
166
+ });
167
+
168
+ builtFunctions = readFileSync(outfile, "utf-8");
169
+ filepathRoutingConfig = readFileSync(outputConfigPath, "utf-8");
170
+ } catch (e) {
171
+ if (e instanceof FunctionsNoRoutesError) {
172
+ logger.warn(
173
+ getFunctionsNoRoutesWarning(functionsDirectory, "skipping")
174
+ );
175
+ } else {
176
+ throw e;
177
+ }
178
+ }
179
+ }
180
+
181
+ const manifest = await upload({
182
+ directory,
183
+ accountId,
184
+ projectName,
185
+ skipCaching: skipCaching ?? false,
186
+ });
187
+
188
+ const formData = new FormData();
189
+
190
+ formData.append("manifest", JSON.stringify(manifest));
191
+
192
+ if (branch) {
193
+ formData.append("branch", branch);
194
+ }
195
+
196
+ if (commitMessage) {
197
+ formData.append("commit_message", commitMessage);
198
+ }
199
+
200
+ if (commitHash) {
201
+ formData.append("commit_hash", commitHash);
202
+ }
203
+
204
+ if (commitDirty !== undefined) {
205
+ formData.append("commit_dirty", commitDirty);
206
+ }
207
+
208
+ if (_headers) {
209
+ formData.append("_headers", new File([_headers], "_headers"));
210
+ logger.log(`✨ Uploading _headers`);
211
+ }
212
+
213
+ if (_redirects) {
214
+ formData.append("_redirects", new File([_redirects], "_redirects"));
215
+ logger.log(`✨ Uploading _redirects`);
216
+ }
217
+
218
+ if (filepathRoutingConfig) {
219
+ formData.append(
220
+ "functions-filepath-routing-config.json",
221
+ new File(
222
+ [filepathRoutingConfig],
223
+ "functions-filepath-routing-config.json"
224
+ )
225
+ );
226
+ }
227
+
228
+ /**
229
+ * Advanced Mode
230
+ * https://developers.cloudflare.com/pages/platform/functions/#advanced-mode
231
+ *
232
+ * When using a _worker.js file, the entire /functions directory is ignored
233
+ * – this includes its routing and middleware characteristics.
234
+ */
235
+ if (_workerJS) {
236
+ let workerFileContents = _workerJS;
237
+ if (bundle) {
238
+ const outfile = join(tmpdir(), `./bundledWorker-${Math.random()}.mjs`);
239
+ await buildRawWorker({
240
+ workerScriptPath,
241
+ outfile,
242
+ directory: directory ?? ".",
243
+ local: false,
244
+ sourcemap: true,
245
+ watch: false,
246
+ onEnd: () => {},
247
+ betaD1Shims: d1Databases,
248
+ });
249
+ workerFileContents = readFileSync(outfile, "utf8");
250
+ } else {
251
+ await checkRawWorker(workerScriptPath, () => {});
252
+ }
253
+
254
+ formData.append("_worker.js", new File([workerFileContents], "_worker.js"));
255
+ logger.log(`✨ Uploading _worker.js`);
256
+
257
+ if (_routesCustom) {
258
+ // user provided a custom _routes.json file
259
+ try {
260
+ const routesCustomJSON = JSON.parse(_routesCustom);
261
+ validateRoutes(routesCustomJSON, join(directory, "_routes.json"));
262
+
263
+ formData.append(
264
+ "_routes.json",
265
+ new File([_routesCustom], "_routes.json")
266
+ );
267
+ logger.log(`✨ Uploading _routes.json`);
268
+ } catch (err) {
269
+ if (err instanceof FatalError) {
270
+ throw err;
271
+ }
272
+ }
273
+ }
274
+ }
275
+
276
+ /**
277
+ * Pages Functions
278
+ * https://developers.cloudflare.com/pages/platform/functions/
279
+ */
280
+ if (builtFunctions && !_workerJS) {
281
+ // if Functions were build successfully, proceed to uploading the build file
282
+ formData.append("_worker.js", new File([builtFunctions], "_worker.js"));
283
+ logger.log(`✨ Uploading Functions`);
284
+
285
+ if (_routesCustom) {
286
+ // user provided a custom _routes.json file
287
+ try {
288
+ const routesCustomJSON = JSON.parse(_routesCustom);
289
+ validateRoutes(routesCustomJSON, join(directory, "_routes.json"));
290
+
291
+ formData.append(
292
+ "_routes.json",
293
+ new File([_routesCustom], "_routes.json")
294
+ );
295
+ logger.log(`✨ Uploading _routes.json`);
296
+ } catch (err) {
297
+ if (err instanceof FatalError) {
298
+ throw err;
299
+ }
300
+ }
301
+ } else if (routesOutputPath) {
302
+ // no custom _routes.json file found, so fallback to the generated one
303
+ try {
304
+ _routesGenerated = readFileSync(routesOutputPath, "utf-8");
305
+
306
+ if (_routesGenerated) {
307
+ formData.append(
308
+ "_routes.json",
309
+ new File([_routesGenerated], "_routes.json")
310
+ );
311
+ }
312
+ } catch {}
313
+ }
314
+ }
315
+
316
+ const deploymentResponse = await fetchResult<Deployment>(
317
+ `/accounts/${accountId}/pages/projects/${projectName}/deployments`,
318
+ {
319
+ method: "POST",
320
+ body: formData,
321
+ }
322
+ );
323
+ return deploymentResponse;
324
+ }
package/src/bundle.ts CHANGED
@@ -9,10 +9,10 @@ import tmp from "tmp-promise";
9
9
  import createModuleCollector from "./module-collection";
10
10
  import { getBasePath, toUrlPath } from "./paths";
11
11
  import type { Config } from "./config";
12
+ import type { DurableObjectBindings } from "./config/environment";
12
13
  import type { WorkerRegistry } from "./dev-registry";
13
14
  import type { Entry } from "./entry";
14
15
  import type { CfModule } from "./worker";
15
-
16
16
  export type BundleResult = {
17
17
  modules: CfModule[];
18
18
  dependencies: esbuild.Metafile["outputs"][string]["inputs"];
@@ -95,6 +95,7 @@ export async function bundleWorker(
95
95
  serveAssetsFromWorker: boolean;
96
96
  assets?: StaticAssetsConfig;
97
97
  betaD1Shims?: string[];
98
+ doBindings: DurableObjectBindings;
98
99
  jsxFactory?: string;
99
100
  jsxFragment?: string;
100
101
  rules: Config["rules"];
@@ -123,6 +124,7 @@ export async function bundleWorker(
123
124
  const {
124
125
  serveAssetsFromWorker,
125
126
  betaD1Shims,
127
+ doBindings,
126
128
  jsxFactory,
127
129
  jsxFragment,
128
130
  rules,
@@ -271,7 +273,13 @@ export async function bundleWorker(
271
273
  Array.isArray(betaD1Shims) &&
272
274
  betaD1Shims.length > 0 &&
273
275
  ((currentEntry: Entry) => {
274
- return applyD1BetaFacade(currentEntry, tmpDir.path, betaD1Shims, local);
276
+ return applyD1BetaFacade(
277
+ currentEntry,
278
+ tmpDir.path,
279
+ betaD1Shims,
280
+ local,
281
+ doBindings
282
+ );
275
283
  }),
276
284
 
277
285
  // Middleware loader: to add middleware, we add the path to the middleware
@@ -332,7 +340,7 @@ export async function bundleWorker(
332
340
  sourceRoot: destination,
333
341
  minify,
334
342
  metafile: true,
335
- conditions: ["worker", "browser"],
343
+ conditions: ["workerd", "worker", "browser"],
336
344
  ...(process.env.NODE_ENV && {
337
345
  define: {
338
346
  // use process.env["NODE_ENV" + ""] so that esbuild doesn't replace it
@@ -395,13 +403,23 @@ export async function bundleWorker(
395
403
  _path.includes(".map")
396
404
  )[0];
397
405
 
406
+ const resolvedEntryPointPath = path.resolve(
407
+ entry.directory,
408
+ entryPointOutputs[0][0]
409
+ );
410
+
411
+ // copy all referenced modules into the output bundle directory
412
+ for (const module of moduleCollector.modules) {
413
+ fs.writeFileSync(
414
+ path.join(path.dirname(resolvedEntryPointPath), module.name),
415
+ module.content
416
+ );
417
+ }
418
+
398
419
  return {
399
420
  modules: moduleCollector.modules,
400
421
  dependencies,
401
- resolvedEntryPointPath: path.resolve(
402
- entry.directory,
403
- entryPointOutputs[0][0]
404
- ),
422
+ resolvedEntryPointPath,
405
423
  bundleType,
406
424
  stop: result.stop,
407
425
  sourceMapPath,
@@ -779,17 +797,51 @@ async function applyFirstPartyWorkerDevFacade(
779
797
  * This code be removed from here when the API is in Workers core,
780
798
  * but moved inside Miniflare for simulating D1.
781
799
  */
782
-
783
800
  async function applyD1BetaFacade(
784
801
  entry: Entry,
785
802
  tmpDirPath: string,
786
803
  betaD1Shims: string[],
787
- local: boolean
804
+ local: boolean,
805
+ doBindings: DurableObjectBindings
788
806
  ): Promise<Entry> {
807
+ let entrypointPath = path.resolve(
808
+ getBasePath(),
809
+ "templates/d1-beta-facade.js"
810
+ );
811
+ if (Array.isArray(doBindings) && doBindings.length > 0) {
812
+ //we have DO bindings, so we need to shim them
813
+ const maskedDoBindings = doBindings
814
+ // Don't shim anything not local to this worker
815
+ .filter((b) => !b.script_name)
816
+ // Reexport the DO classnames
817
+ .map(
818
+ (b) =>
819
+ `export const ${b.class_name} = maskDurableObjectDefinition(OTHER_EXPORTS.${b.class_name});`
820
+ )
821
+ .join("\n");
822
+ const baseFile = fs.readFileSync(
823
+ path.resolve(getBasePath(), "templates/d1-beta-facade.js"),
824
+ "utf8"
825
+ );
826
+ //getMaskedEnv is already used to shim regular Workers
827
+ const contents = `
828
+ ${baseFile}
829
+
830
+ var maskDurableObjectDefinition = (cls) =>
831
+ class extends cls {
832
+ constructor(state, env) {
833
+ super(state, getMaskedEnv(env));
834
+ }
835
+ };
836
+ ${maskedDoBindings}`;
837
+ const doD1FacadePath = path.join(tmpDirPath, "d1-do-facade.js");
838
+ //write our shim so we can build it
839
+ fs.writeFileSync(doD1FacadePath, contents);
840
+ entrypointPath = doD1FacadePath;
841
+ }
789
842
  const targetPath = path.join(tmpDirPath, "d1-beta-facade.entry.js");
790
-
791
843
  await esbuild.build({
792
- entryPoints: [path.resolve(getBasePath(), "templates/d1-beta-facade.js")],
844
+ entryPoints: [entrypointPath],
793
845
  bundle: true,
794
846
  format: "esm",
795
847
  sourcemap: true,
package/src/cli.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import process from "process";
2
2
  import { hideBin } from "yargs/helpers";
3
- import { unstable_dev } from "./api";
3
+ import { unstable_dev, unstable_pages } from "./api";
4
4
  import { FatalError } from "./errors";
5
5
  import { main } from ".";
6
6
 
@@ -24,5 +24,5 @@ if (typeof jest === "undefined" && require.main === module) {
24
24
  * It makes it possible to import wrangler from 'wrangler',
25
25
  * and call wrangler.unstable_dev().
26
26
  */
27
- export { unstable_dev };
27
+ export { unstable_dev, unstable_pages };
28
28
  export type { UnstableDevWorker, UnstableDevOptions };
@@ -1,4 +1,5 @@
1
1
  import type { Environment, RawEnvironment } from "./environment";
2
+ import type { CamelCaseKey } from "yargs";
2
3
 
3
4
  /**
4
5
  * This is the static type definition for the configuration object.
@@ -273,3 +274,9 @@ interface EnvironmentMap {
273
274
  [envName: string]: RawEnvironment;
274
275
  };
275
276
  }
277
+
278
+ // API dev only passes in camel-cased versions of keys, so ensure
279
+ // only camel-cased keys are used
280
+ export type OnlyCamelCase<T = Record<string, never>> = {
281
+ [key in keyof T as CamelCaseKey<key>]: T[key];
282
+ };
@@ -261,6 +261,17 @@ interface EnvironmentInheritable {
261
261
  logpush: boolean | undefined;
262
262
  }
263
263
 
264
+ export type DurableObjectBindings = {
265
+ /** The name of the binding used to refer to the Durable Object */
266
+ name: string;
267
+ /** The exported class name of the Durable Object */
268
+ class_name: string;
269
+ /** The script where the Durable Object is defined (if it's external to this worker) */
270
+ script_name?: string;
271
+ /** The service environment of the script_name to bind to */
272
+ environment?: string;
273
+ }[];
274
+
264
275
  /**
265
276
  * The `EnvironmentNonInheritable` interface declares all the configuration fields for an environment
266
277
  * that cannot be inherited from the top-level environment, and must be defined specifically.
@@ -303,16 +314,7 @@ interface EnvironmentNonInheritable {
303
314
  * @nonInheritable
304
315
  */
305
316
  durable_objects: {
306
- bindings: {
307
- /** The name of the binding used to refer to the Durable Object */
308
- name: string;
309
- /** The exported class name of the Durable Object */
310
- class_name: string;
311
- /** The script where the Durable Object is defined (if it's external to this worker) */
312
- script_name?: string;
313
- /** The service environment of the script_name to bind to */
314
- environment?: string;
315
- }[];
317
+ bindings: DurableObjectBindings;
316
318
  };
317
319
 
318
320
  /**
@@ -2,12 +2,12 @@ import fs from "node:fs";
2
2
  import dotenv from "dotenv";
3
3
  import { findUpSync } from "find-up";
4
4
  import { logger } from "../logger";
5
- import { parseTOML, readFileSync } from "../parse";
5
+ import { parseJSONC, parseTOML, readFileSync } from "../parse";
6
6
  import { removeD1BetaPrefix } from "../worker";
7
7
  import { normalizeAndValidateConfig } from "./validation";
8
8
  import type { CfWorkerInit } from "../worker";
9
- import type { Config, RawConfig } from "./config";
10
- import type { CamelCaseKey } from "yargs";
9
+ import type { CommonYargsOptions } from "../yargs-types";
10
+ import type { Config, OnlyCamelCase, RawConfig } from "./config";
11
11
 
12
12
  export type {
13
13
  Config,
@@ -25,18 +25,21 @@ export type {
25
25
  /**
26
26
  * Get the Wrangler configuration; read it from the give `configPath` if available.
27
27
  */
28
- export function readConfig(
28
+
29
+ export function readConfig<CommandArgs>(
29
30
  configPath: string | undefined,
30
- args: unknown
31
+ // Include command specific args as well as the wrangler global flags
32
+ args: CommandArgs & OnlyCamelCase<CommonYargsOptions>
31
33
  ): Config {
32
34
  let rawConfig: RawConfig = {};
33
35
  if (!configPath) {
34
- configPath = findWranglerToml();
36
+ configPath = findWranglerToml(process.cwd(), args.experimentalJsonConfig);
35
37
  }
36
-
37
38
  // Load the configuration from disk if available
38
- if (configPath) {
39
+ if (configPath?.endsWith("toml")) {
39
40
  rawConfig = parseTOML(readFileSync(configPath), configPath);
41
+ } else if (configPath?.endsWith("json")) {
42
+ rawConfig = parseJSONC(readFileSync(configPath), configPath);
40
43
  }
41
44
 
42
45
  // Process the top-level configuration.
@@ -61,10 +64,16 @@ export function readConfig(
61
64
  * from the current working directory.
62
65
  */
63
66
  export function findWranglerToml(
64
- referencePath: string = process.cwd()
67
+ referencePath: string = process.cwd(),
68
+ preferJson = false
65
69
  ): string | undefined {
66
- const configPath = findUpSync("wrangler.toml", { cwd: referencePath });
67
- return configPath;
70
+ if (preferJson) {
71
+ return (
72
+ findUpSync(`wrangler.json`, { cwd: referencePath }) ??
73
+ findUpSync(`wrangler.toml`, { cwd: referencePath })
74
+ );
75
+ }
76
+ return findUpSync(`wrangler.toml`, { cwd: referencePath });
68
77
  }
69
78
 
70
79
  /**
@@ -316,18 +325,13 @@ export function printBindings(bindings: CfWorkerInit["bindings"]) {
316
325
  logger.log(message);
317
326
  }
318
327
 
319
- type CamelCase<T> = {
320
- [key in keyof T as key | CamelCaseKey<key>]: T[key];
321
- };
322
-
323
- export function withConfig<T extends { config?: string }>(
328
+ export function withConfig<T>(
324
329
  handler: (
325
- t: Omit<CamelCase<T>, "config"> & { config: Config }
330
+ t: OnlyCamelCase<T & CommonYargsOptions> & { config: Config }
326
331
  ) => Promise<void>
327
332
  ) {
328
- return (t: CamelCase<T>) => {
329
- const { config: configPath, ...rest } = t;
330
- return handler({ ...rest, config: readConfig(configPath, rest) });
333
+ return (t: OnlyCamelCase<T & CommonYargsOptions>) => {
334
+ return handler({ ...t, config: readConfig(t.config, t) });
331
335
  };
332
336
  }
333
337
 
@@ -11,17 +11,18 @@ import { requireAuth } from "../user";
11
11
  import { formatBytes, formatTimeAgo } from "./formatTimeAgo";
12
12
  import { Name } from "./options";
13
13
  import { d1BetaWarning, getDatabaseByNameOrBinding } from "./utils";
14
+ import type {
15
+ CommonYargsArgv,
16
+ StrictYargsOptionsToInterface,
17
+ } from "../yargs-types";
14
18
  import type { Backup, Database } from "./types";
15
19
  import type { Response } from "undici";
16
- import type { Argv } from "yargs";
17
20
 
18
- type BackupListArgs = { config?: string; name: string };
19
-
20
- export function ListOptions(yargs: Argv): Argv<BackupListArgs> {
21
+ export function ListOptions(yargs: CommonYargsArgv) {
21
22
  return Name(yargs);
22
23
  }
23
-
24
- export const ListHandler = withConfig<BackupListArgs>(
24
+ type ListHandlerOptions = StrictYargsOptionsToInterface<typeof ListOptions>;
25
+ export const ListHandler = withConfig<ListHandlerOptions>(
25
26
  async ({ config, name }): Promise<void> => {
26
27
  const accountId = await requireAuth({});
27
28
  logger.log(d1BetaWarning);
@@ -77,13 +78,12 @@ export const listBackups = async (
77
78
  return Object.values(results);
78
79
  };
79
80
 
80
- type BackupCreateArgs = BackupListArgs;
81
-
82
- export function CreateOptions(yargs: Argv): Argv<BackupCreateArgs> {
81
+ export function CreateOptions(yargs: CommonYargsArgv) {
83
82
  return ListOptions(yargs);
84
83
  }
84
+ type CreateHandlerOptions = StrictYargsOptionsToInterface<typeof CreateOptions>;
85
85
 
86
- export const CreateHandler = withConfig<BackupCreateArgs>(
86
+ export const CreateHandler = withConfig<CreateHandlerOptions>(
87
87
  async ({ config, name }): Promise<void> => {
88
88
  const accountId = await requireAuth({});
89
89
  logger.log(d1BetaWarning);
@@ -119,19 +119,17 @@ export const createBackup = async (
119
119
  };
120
120
  };
121
121
 
122
- type BackupRestoreArgs = BackupListArgs & {
123
- "backup-id": string;
124
- };
125
-
126
- export function RestoreOptions(yargs: Argv): Argv<BackupRestoreArgs> {
122
+ export function RestoreOptions(yargs: CommonYargsArgv) {
127
123
  return ListOptions(yargs).positional("backup-id", {
128
124
  describe: "The Backup ID to restore",
129
125
  type: "string",
130
126
  demandOption: true,
131
127
  });
132
128
  }
133
-
134
- export const RestoreHandler = withConfig<BackupRestoreArgs>(
129
+ type RestoreHandlerOptions = StrictYargsOptionsToInterface<
130
+ typeof RestoreOptions
131
+ >;
132
+ export const RestoreHandler = withConfig<RestoreHandlerOptions>(
135
133
  async ({ config, name, backupId }): Promise<void> => {
136
134
  const accountId = await requireAuth({});
137
135
  logger.log(d1BetaWarning);
@@ -163,11 +161,7 @@ export const restoreBackup = async (
163
161
  );
164
162
  };
165
163
 
166
- type BackupDownloadArgs = BackupRestoreArgs & {
167
- output?: string;
168
- };
169
-
170
- export function DownloadOptions(yargs: Argv): Argv<BackupDownloadArgs> {
164
+ export function DownloadOptions(yargs: CommonYargsArgv) {
171
165
  return ListOptions(yargs)
172
166
  .positional("backup-id", {
173
167
  describe: "The Backup ID to download",
@@ -180,8 +174,10 @@ export function DownloadOptions(yargs: Argv): Argv<BackupDownloadArgs> {
180
174
  type: "string",
181
175
  });
182
176
  }
183
-
184
- export const DownloadHandler = withConfig<BackupDownloadArgs>(
177
+ type DownloadHandlerOptions = StrictYargsOptionsToInterface<
178
+ typeof DownloadOptions
179
+ >;
180
+ export const DownloadHandler = withConfig<DownloadHandlerOptions>(
185
181
  async ({ name, backupId, output, config }): Promise<void> => {
186
182
  const accountId = await requireAuth({});
187
183
  logger.log(d1BetaWarning);