wrangler 2.0.23 → 2.0.26

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 (80) hide show
  1. package/README.md +20 -2
  2. package/bin/wrangler.js +1 -1
  3. package/miniflare-dist/index.mjs +235 -47
  4. package/package.json +11 -6
  5. package/src/__tests__/configuration.test.ts +89 -17
  6. package/src/__tests__/dev.test.tsx +29 -4
  7. package/src/__tests__/generate.test.ts +93 -0
  8. package/src/__tests__/helpers/mock-cfetch.ts +87 -2
  9. package/src/__tests__/index.test.ts +10 -27
  10. package/src/__tests__/init.test.ts +537 -359
  11. package/src/__tests__/jest.setup.ts +34 -1
  12. package/src/__tests__/kv.test.ts +2 -2
  13. package/src/__tests__/metrics.test.ts +5 -0
  14. package/src/__tests__/pages.test.ts +14 -0
  15. package/src/__tests__/publish.test.ts +497 -254
  16. package/src/__tests__/r2.test.ts +173 -71
  17. package/src/__tests__/tail.test.ts +112 -42
  18. package/src/__tests__/user.test.ts +1 -0
  19. package/src/__tests__/validate-dev-props.test.ts +56 -0
  20. package/src/__tests__/whoami.test.tsx +60 -1
  21. package/src/api/dev.ts +7 -0
  22. package/src/bundle.ts +279 -44
  23. package/src/cfetch/internal.ts +73 -2
  24. package/src/config/config.ts +8 -3
  25. package/src/config/environment.ts +40 -8
  26. package/src/config/index.ts +13 -0
  27. package/src/config/validation.ts +102 -8
  28. package/src/create-worker-upload-form.ts +25 -0
  29. package/src/dev/dev.tsx +121 -28
  30. package/src/dev/local.tsx +88 -14
  31. package/src/dev/remote.tsx +39 -8
  32. package/src/dev/use-esbuild.ts +28 -0
  33. package/src/dev/validate-dev-props.ts +31 -0
  34. package/src/dev-registry.tsx +160 -0
  35. package/src/dev.tsx +107 -80
  36. package/src/generate.ts +112 -14
  37. package/src/index.tsx +212 -4
  38. package/src/init.ts +111 -38
  39. package/src/inspect.ts +90 -5
  40. package/src/metrics/index.ts +1 -0
  41. package/src/metrics/metrics-dispatcher.ts +1 -0
  42. package/src/metrics/metrics-usage-headers.ts +24 -0
  43. package/src/metrics/send-event.ts +2 -2
  44. package/src/miniflare-cli/assets.ts +27 -16
  45. package/src/miniflare-cli/index.ts +124 -2
  46. package/src/module-collection.ts +3 -3
  47. package/src/pages/build.tsx +75 -41
  48. package/src/pages/constants.ts +5 -0
  49. package/src/pages/deployments.tsx +10 -10
  50. package/src/pages/dev.tsx +177 -52
  51. package/src/pages/errors.ts +22 -0
  52. package/src/pages/functions/buildPlugin.ts +4 -0
  53. package/src/pages/functions/buildWorker.ts +4 -0
  54. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  55. package/src/pages/functions/routes-consolidation.ts +73 -0
  56. package/src/pages/functions/routes-transformation.test.ts +271 -0
  57. package/src/pages/functions/routes-transformation.ts +122 -0
  58. package/src/pages/functions.tsx +96 -0
  59. package/src/pages/index.tsx +65 -55
  60. package/src/pages/projects.tsx +9 -3
  61. package/src/pages/publish.tsx +76 -23
  62. package/src/pages/types.ts +9 -0
  63. package/src/pages/upload.tsx +38 -21
  64. package/src/publish.ts +126 -112
  65. package/src/r2.ts +81 -0
  66. package/src/tail/filters.ts +3 -1
  67. package/src/tail/index.ts +15 -2
  68. package/src/tail/printing.ts +43 -3
  69. package/src/user/user.tsx +20 -2
  70. package/src/whoami.tsx +79 -1
  71. package/src/worker.ts +12 -0
  72. package/templates/first-party-worker-module-facade.ts +18 -0
  73. package/templates/format-dev-errors.ts +32 -0
  74. package/templates/pages-template-plugin.ts +16 -4
  75. package/templates/pages-template-worker.ts +16 -5
  76. package/templates/{static-asset-facade.js → serve-static-assets.ts} +21 -7
  77. package/templates/service-bindings-module-facade.js +54 -0
  78. package/templates/service-bindings-sw-facade.js +42 -0
  79. package/wrangler-dist/cli.d.ts +7 -0
  80. package/wrangler-dist/cli.js +40851 -15332
package/src/dev.tsx CHANGED
@@ -127,9 +127,9 @@ export function devOptions(yargs: Argv): Argv<DevArgs> {
127
127
  default: true,
128
128
  })
129
129
  .option("ip", {
130
- describe: "IP address to listen on, defaults to `localhost`",
130
+ describe: "IP address to listen on",
131
131
  type: "string",
132
- requiresArg: true,
132
+ default: "0.0.0.0",
133
133
  })
134
134
  .option("port", {
135
135
  describe: "Port to listen on",
@@ -260,9 +260,7 @@ export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
260
260
  }
261
261
  }
262
262
 
263
- type StartDevOptions = ArgumentsCamelCase<DevArgs> & {
264
- // These options can be passed in directly when called with the `wrangler.dev()` API.
265
- // They aren't exposed as CLI arguments.
263
+ export type AdditionalDevProps = {
266
264
  vars?: {
267
265
  [key: string]: unknown;
268
266
  };
@@ -277,9 +275,19 @@ type StartDevOptions = ArgumentsCamelCase<DevArgs> & {
277
275
  script_name?: string | undefined;
278
276
  environment?: string | undefined;
279
277
  }[];
280
- forceLocal?: boolean;
281
- enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
278
+ r2?: {
279
+ binding: string;
280
+ bucket_name: string;
281
+ preview_bucket_name?: string;
282
+ }[];
282
283
  };
284
+ type StartDevOptions = ArgumentsCamelCase<DevArgs> &
285
+ // These options can be passed in directly when called with the `wrangler.dev()` API.
286
+ // They aren't exposed as CLI arguments.
287
+ AdditionalDevProps & {
288
+ forceLocal?: boolean;
289
+ enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
290
+ };
283
291
 
284
292
  export async function startDev(args: StartDevOptions) {
285
293
  let watcher: ReturnType<typeof watch> | undefined;
@@ -296,11 +304,6 @@ export async function startDev(args: StartDevOptions) {
296
304
  ((args.script &&
297
305
  findWranglerToml(path.dirname(args.script))) as ConfigPath);
298
306
  let config = readConfig(configPath, args);
299
- await metrics.sendMetricsEvent(
300
- "run dev",
301
- { local: args.local },
302
- { sendMetrics: config.send_metrics, offline: args.local }
303
- );
304
307
 
305
308
  if (config.configPath) {
306
309
  watcher = watch(config.configPath, {
@@ -322,6 +325,15 @@ export async function startDev(args: StartDevOptions) {
322
325
  "dev"
323
326
  );
324
327
 
328
+ await metrics.sendMetricsEvent(
329
+ "run dev",
330
+ {
331
+ local: args.local,
332
+ usesTypeScript: /\.tsx?$/.test(entry.file),
333
+ },
334
+ { sendMetrics: config.send_metrics, offline: args.local }
335
+ );
336
+
325
337
  if (config.services && config.services.length > 0) {
326
338
  logger.warn(
327
339
  `This worker is bound to live services: ${config.services
@@ -412,77 +424,19 @@ export async function startDev(args: StartDevOptions) {
412
424
  );
413
425
  }
414
426
 
415
- // eslint-disable-next-line no-inner-declarations
416
- async function getBindings(
417
- configParam: Config
418
- ): Promise<CfWorkerInit["bindings"]> {
419
- return {
420
- kv_namespaces: [
421
- ...(configParam.kv_namespaces || []).map(
422
- ({ binding, preview_id, id: _id }) => {
423
- // In `dev`, we make folks use a separate kv namespace called
424
- // `preview_id` instead of `id` so that they don't
425
- // break production data. So here we check that a `preview_id`
426
- // has actually been configured.
427
- // This whole block of code will be obsoleted in the future
428
- // when we have copy-on-write for previews on edge workers.
429
- if (!preview_id) {
430
- // TODO: This error has to be a _lot_ better, ideally just asking
431
- // to create a preview namespace for the user automatically
432
- throw new Error(
433
- `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`
434
- ); // Ugh, I really don't like this message very much
435
- }
436
- return {
437
- binding,
438
- id: preview_id,
439
- };
440
- }
441
- ),
442
- ...(args.kv || []),
443
- ],
444
- // Use a copy of combinedVars since we're modifying it later
445
- vars: {
446
- ...getVarsForDev(configParam),
447
- ...args.vars,
448
- },
449
- wasm_modules: configParam.wasm_modules,
450
- text_blobs: configParam.text_blobs,
451
- data_blobs: configParam.data_blobs,
452
- durable_objects: {
453
- bindings: [
454
- ...(configParam.durable_objects || { bindings: [] }).bindings,
455
- ...(args.durableObjects || []),
456
- ],
457
- },
458
- r2_buckets: configParam.r2_buckets?.map(
459
- ({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
460
- // same idea as kv namespace preview id,
461
- // same copy-on-write TODO
462
- if (!preview_bucket_name) {
463
- throw new Error(
464
- `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`
465
- );
466
- }
467
- return {
468
- binding,
469
- bucket_name: preview_bucket_name,
470
- };
471
- }
472
- ),
473
- worker_namespaces: configParam.worker_namespaces,
474
- services: configParam.services,
475
- unsafe: configParam.unsafe?.bindings,
476
- };
477
- }
478
-
479
427
  const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT);
480
428
  const getInspectorPort = memoizeGetPort(DEFAULT_INSPECTOR_PORT);
481
429
 
482
430
  // eslint-disable-next-line no-inner-declarations
483
431
  async function getDevReactElement(configParam: Config) {
484
432
  // now log all available bindings into the terminal
485
- const bindings = await getBindings(configParam);
433
+ const bindings = await getBindings(configParam, {
434
+ kv: args.kv,
435
+ vars: args.vars,
436
+ durableObjects: args.durableObjects,
437
+ r2: args.r2,
438
+ });
439
+
486
440
  // mask anything that was overridden in .dev.vars
487
441
  // so that we don't log potential secrets into the terminal
488
442
  const maskedVars = { ...bindings.vars };
@@ -537,20 +491,21 @@ export async function startDev(args: StartDevOptions) {
537
491
  liveReload={args.liveReload || false}
538
492
  accountId={config.account_id || getAccountFromCache()?.id}
539
493
  assetPaths={assetPaths}
494
+ assetsConfig={config.assets}
540
495
  port={args.port || config.dev.port || (await getLocalPort())}
541
496
  ip={args.ip || config.dev.ip}
542
497
  inspectorPort={
543
- args["inspector-port"] ||
498
+ args.inspectorPort ||
544
499
  config.dev.inspector_port ||
545
500
  (await getInspectorPort())
546
501
  }
547
502
  isWorkersSite={Boolean(args.site || config.site)}
548
503
  compatibilityDate={getDevCompatibilityDate(
549
504
  config,
550
- args["compatibility-date"]
505
+ args.compatibilityDate
551
506
  )}
552
507
  compatibilityFlags={
553
- args["compatibility-flags"] || config.compatibility_flags
508
+ args.compatibilityFlags || config.compatibility_flags
554
509
  }
555
510
  usageModel={config.usage_model}
556
511
  bindings={bindings}
@@ -562,6 +517,8 @@ export async function startDev(args: StartDevOptions) {
562
517
  showInteractiveDevSession={args.showInteractiveDevSession}
563
518
  forceLocal={args.forceLocal}
564
519
  enablePagesAssetsServiceBinding={args.enablePagesAssetsServiceBinding}
520
+ firstPartyWorker={config.first_party_worker}
521
+ sendMetrics={config.send_metrics}
565
522
  />
566
523
  );
567
524
  }
@@ -596,3 +553,73 @@ function memoizeGetPort(defaultPort: number) {
596
553
  return portValue || (portValue = await getPort({ port: defaultPort }));
597
554
  };
598
555
  }
556
+
557
+ async function getBindings(
558
+ configParam: Config,
559
+ args: AdditionalDevProps
560
+ ): Promise<CfWorkerInit["bindings"]> {
561
+ const bindings = {
562
+ kv_namespaces: [
563
+ ...(configParam.kv_namespaces || []).map(
564
+ ({ binding, preview_id, id: _id }) => {
565
+ // In `dev`, we make folks use a separate kv namespace called
566
+ // `preview_id` instead of `id` so that they don't
567
+ // break production data. So here we check that a `preview_id`
568
+ // has actually been configured.
569
+ // This whole block of code will be obsoleted in the future
570
+ // when we have copy-on-write for previews on edge workers.
571
+ if (!preview_id) {
572
+ // TODO: This error has to be a _lot_ better, ideally just asking
573
+ // to create a preview namespace for the user automatically
574
+ throw new Error(
575
+ `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`
576
+ ); // Ugh, I really don't like this message very much
577
+ }
578
+ return {
579
+ binding,
580
+ id: preview_id,
581
+ };
582
+ }
583
+ ),
584
+ ...(args.kv || []),
585
+ ],
586
+ // Use a copy of combinedVars since we're modifying it later
587
+ vars: {
588
+ ...getVarsForDev(configParam),
589
+ ...args.vars,
590
+ },
591
+ wasm_modules: configParam.wasm_modules,
592
+ text_blobs: configParam.text_blobs,
593
+ data_blobs: configParam.data_blobs,
594
+ durable_objects: {
595
+ bindings: [
596
+ ...(configParam.durable_objects || { bindings: [] }).bindings,
597
+ ...(args.durableObjects || []),
598
+ ],
599
+ },
600
+ r2_buckets: [
601
+ ...(configParam.r2_buckets?.map(
602
+ ({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
603
+ // same idea as kv namespace preview id,
604
+ // same copy-on-write TODO
605
+ if (!preview_bucket_name) {
606
+ throw new Error(
607
+ `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`
608
+ );
609
+ }
610
+ return {
611
+ binding,
612
+ bucket_name: preview_bucket_name,
613
+ };
614
+ }
615
+ ) || []),
616
+ ...(args.r2 || []),
617
+ ],
618
+ worker_namespaces: configParam.worker_namespaces,
619
+ services: configParam.services,
620
+ unsafe: configParam.unsafe?.bindings,
621
+ logfwdr: configParam.logfwdr,
622
+ };
623
+
624
+ return bindings;
625
+ }
package/src/generate.ts CHANGED
@@ -1,33 +1,131 @@
1
- import { DeprecationError } from "./errors";
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { setup as createCloudflare } from "create-cloudflare";
4
+ import { initHandler } from "./init";
5
+ import { logger } from "./logger";
6
+ import { CommandLineArgsError, printWranglerBanner } from ".";
2
7
  import type { Argv, ArgumentsCamelCase } from "yargs";
3
8
 
9
+ // https://github.com/cloudflare/wrangler/blob/master/src/cli/mod.rs#L106-L123
4
10
  interface GenerateArgs {
5
- name: string;
6
- template: string;
11
+ name?: string;
12
+ template?: string;
13
+ type?: string;
14
+ site?: boolean;
7
15
  }
8
16
 
9
17
  export function generateOptions(yargs: Argv) {
10
18
  return yargs
11
19
  .positional("name", {
12
20
  describe: "Name of the Workers project",
13
- default: "worker",
21
+ type: "string",
14
22
  })
15
23
  .positional("template", {
24
+ type: "string",
16
25
  describe: "The URL of a GitHub template",
17
- default: "https://github.com/cloudflare/worker-template",
26
+ })
27
+ .option("type", {
28
+ alias: "t",
29
+ type: "string",
30
+ hidden: true,
31
+ deprecated: true,
32
+ })
33
+ .option("site", {
34
+ alias: "s",
35
+ type: "boolean",
36
+ hidden: true,
37
+ deprecated: true,
18
38
  });
19
39
  }
20
40
 
21
- export function generateHandler(
22
- generateArgs: ArgumentsCamelCase<GenerateArgs>
23
- ) {
24
- // "👯 [DEPRECATED]. Scaffold a Cloudflare Workers project from a public GitHub repository.",
25
- throw new DeprecationError(
26
- "`wrangler generate` has been deprecated.\n" +
27
- "Try running `wrangler init` to generate a basic Worker, or cloning the template repository instead:\n\n" +
41
+ export async function generateHandler({
42
+ // somehow, `init` marks name as required but then also runs fine
43
+ // with the name omitted, and then substitutes it at runtime with ""
44
+ name = "",
45
+ template,
46
+ type,
47
+ site,
48
+ ...args
49
+ }: ArgumentsCamelCase<GenerateArgs>) {
50
+ // delegate to `wrangler init` if no template is specified
51
+ if (template === undefined) {
52
+ return initHandler({ name, ...args });
53
+ }
54
+
55
+ // print down here cuz `init` prints it own its own
56
+ printWranglerBanner();
57
+
58
+ if (type) {
59
+ let message = "The --type option is no longer supported.";
60
+ if (args.type === "webpack") {
61
+ message +=
62
+ "\nIf you wish to use webpack then you will need to create a custom build.";
63
+ // TODO: Add a link to docs
64
+ }
65
+ throw new CommandLineArgsError(message);
66
+ }
67
+
68
+ const creationDirectory = generateWorkerDirectoryName(name);
69
+
70
+ if (site) {
71
+ const gitDirectory =
72
+ creationDirectory !== process.cwd()
73
+ ? path.basename(creationDirectory)
74
+ : "my-site";
75
+ const message =
76
+ "The --site option is no longer supported.\n" +
77
+ "If you wish to create a brand new Worker Sites project then clone the `worker-sites-template` starter repository:\n\n" +
28
78
  "```\n" +
29
- `git clone ${generateArgs.template}\n` +
79
+ `git clone --depth=1 --branch=wrangler2 https://github.com/cloudflare/worker-sites-template ${gitDirectory}\n` +
80
+ `cd ${gitDirectory}\n` +
30
81
  "```\n\n" +
31
- "Please refer to https://developers.cloudflare.com/workers/wrangler/deprecations/#generate for more information."
82
+ "Find out more about how to create and maintain Sites projects at https://developers.cloudflare.com/workers/platform/sites.\n" +
83
+ "Have you considered using Cloudflare Pages instead? See https://pages.cloudflare.com/.";
84
+ throw new CommandLineArgsError(message);
85
+ }
86
+
87
+ logger.log(
88
+ `Creating a worker in ${path.basename(creationDirectory)} from ${template}`
32
89
  );
90
+
91
+ await createCloudflare(creationDirectory, template, {
92
+ init: true, // initialize a git repository
93
+ debug: logger.loggerLevel === "debug",
94
+ force: false, // do not overwrite an existing directory
95
+ });
96
+ }
97
+
98
+ /**
99
+ * Creates a path based on the current working directory and a worker name.
100
+ * Automatically increments a counter when searching for an available directory.
101
+ *
102
+ * Running `wrangler generate worker https://some-git-repo` in a directory
103
+ * with the structure:
104
+ * ```
105
+ * - workers
106
+ * |
107
+ * | - worker
108
+ * | | - wrangler.toml
109
+ * | | ...
110
+ * |
111
+ * | - worker-1
112
+ * | | - wrangler.toml
113
+ * | | ...
114
+ * ```
115
+ *
116
+ * will result in a new worker called `worker-2` being generated.
117
+ *
118
+ * @param workerName the name of the generated worker
119
+ * @returns an absolute path to the directory to generate the worker into
120
+ */
121
+ function generateWorkerDirectoryName(workerName: string): string {
122
+ let workerDirectoryPath = path.resolve(process.cwd(), workerName);
123
+ let i = 1;
124
+
125
+ while (fs.existsSync(workerDirectoryPath)) {
126
+ workerDirectoryPath = path.resolve(process.cwd(), `${workerName}-${i}`);
127
+ i++;
128
+ }
129
+
130
+ return workerDirectoryPath;
33
131
  }
package/src/index.tsx CHANGED
@@ -1,4 +1,6 @@
1
+ import * as fs from "node:fs";
1
2
  import path from "node:path";
3
+ import * as stream from "node:stream";
2
4
  import { StringDecoder } from "node:string_decoder";
3
5
  import { setTimeout } from "node:timers/promises";
4
6
  import TOML from "@iarna/toml";
@@ -45,7 +47,15 @@ import {
45
47
  import { previewHandler, previewOptions } from "./preview";
46
48
  import publish from "./publish";
47
49
  import { pubSubCommands } from "./pubsub/pubsub-commands";
48
- import { createR2Bucket, deleteR2Bucket, listR2Buckets } from "./r2";
50
+ import {
51
+ bucketAndKeyFromObjectPath,
52
+ createR2Bucket,
53
+ deleteR2Bucket,
54
+ deleteR2Object,
55
+ getR2Object,
56
+ listR2Buckets,
57
+ putR2Object,
58
+ } from "./r2";
49
59
  import { getAssetPaths, getSiteAssetPaths } from "./sites";
50
60
  import {
51
61
  createTail,
@@ -67,6 +77,7 @@ import { workerNamespaceCommands } from "./worker-namespace";
67
77
  import type { Config } from "./config";
68
78
  import type { KeyValue } from "./kv";
69
79
  import type { TailCLIFilters } from "./tail";
80
+ import type { Readable } from "node:stream";
70
81
  import type { RawData } from "ws";
71
82
  import type { CommandModule } from "yargs";
72
83
  import type Yargs from "yargs";
@@ -513,10 +524,16 @@ function createCLIParser(argv: string[]) {
513
524
  (args.config as ConfigPath) ||
514
525
  (args.script && findWranglerToml(path.dirname(args.script)));
515
526
  const config = readConfig(configPath, args);
516
- await metrics.sendMetricsEvent("deploy worker script", {
517
- sendMetrics: config.send_metrics,
518
- });
519
527
  const entry = await getEntry(args, config, "publish");
528
+ await metrics.sendMetricsEvent(
529
+ "deploy worker script",
530
+ {
531
+ usesTypeScript: /\.tsx?$/.test(entry.file),
532
+ },
533
+ {
534
+ sendMetrics: config.send_metrics,
535
+ }
536
+ );
520
537
 
521
538
  if (args.public) {
522
539
  throw new Error("The --public field has been renamed to --assets");
@@ -950,6 +967,7 @@ function createCLIParser(argv: string[]) {
950
967
  text_blobs: {},
951
968
  data_blobs: {},
952
969
  worker_namespaces: [],
970
+ logfwdr: { schema: undefined, bindings: [] },
953
971
  unsafe: [],
954
972
  },
955
973
  modules: [],
@@ -1714,6 +1732,7 @@ function createCLIParser(argv: string[]) {
1714
1732
  );
1715
1733
  }
1716
1734
  }
1735
+
1717
1736
  if (errors.length > 0) {
1718
1737
  throw new Error(
1719
1738
  `Unexpected JSON input from "${filename}".\n` +
@@ -1746,6 +1765,195 @@ function createCLIParser(argv: string[]) {
1746
1765
  wrangler.command("r2", "📦 Interact with an R2 store", (r2Yargs) => {
1747
1766
  return r2Yargs
1748
1767
  .command(subHelp)
1768
+ .command("object", "Manage R2 objects", (r2ObjectYargs) => {
1769
+ return r2ObjectYargs
1770
+ .command(
1771
+ "get <objectPath>",
1772
+ "Fetch an object from an R2 bucket",
1773
+ (Objectyargs) => {
1774
+ return Objectyargs.positional("objectPath", {
1775
+ describe:
1776
+ "The source object path in the form of {bucket}/{key}",
1777
+ type: "string",
1778
+ })
1779
+ .option("file", {
1780
+ describe: "The destination file to create",
1781
+ alias: "f",
1782
+ conflicts: "pipe",
1783
+ requiresArg: true,
1784
+ type: "string",
1785
+ })
1786
+ .option("pipe", {
1787
+ describe:
1788
+ "Enables the file to be piped to a destination, rather than specified with the --file option",
1789
+ alias: "p",
1790
+ conflicts: "file",
1791
+ type: "boolean",
1792
+ });
1793
+ },
1794
+ async (objectGetYargs) => {
1795
+ const config = readConfig(
1796
+ objectGetYargs.config as ConfigPath,
1797
+ objectGetYargs
1798
+ );
1799
+ const accountId = await requireAuth(config);
1800
+ const { objectPath, pipe } = objectGetYargs;
1801
+ const { bucket, key } = bucketAndKeyFromObjectPath(objectPath);
1802
+
1803
+ let file = objectGetYargs.file;
1804
+ if (!file && !pipe) {
1805
+ file = key;
1806
+ }
1807
+ if (!pipe) {
1808
+ await printWranglerBanner();
1809
+ logger.log(`Downloading "${key}" from "${bucket}".`);
1810
+ }
1811
+ const input = await getR2Object(accountId, bucket, key);
1812
+ const output = file ? fs.createWriteStream(file) : process.stdout;
1813
+ await new Promise<void>((resolve, reject) => {
1814
+ stream.pipeline(input, output, (err: unknown) => {
1815
+ err ? reject(err) : resolve();
1816
+ });
1817
+ });
1818
+ if (!pipe) logger.log("Download complete.");
1819
+ }
1820
+ )
1821
+ .command(
1822
+ "put <objectPath>",
1823
+ "Create an object in an R2 bucket",
1824
+ (Objectyargs) => {
1825
+ return Objectyargs.positional("objectPath", {
1826
+ describe:
1827
+ "The destination object path in the form of {bucket}/{key}",
1828
+ type: "string",
1829
+ })
1830
+ .option("file", {
1831
+ describe: "The path of the file to upload",
1832
+ alias: "f",
1833
+ conflicts: "pipe",
1834
+ requiresArg: true,
1835
+ type: "string",
1836
+ })
1837
+ .option("pipe", {
1838
+ describe:
1839
+ "Enables the file to be piped in, rather than specified with the --file option",
1840
+ alias: "p",
1841
+ conflicts: "file",
1842
+ type: "boolean",
1843
+ })
1844
+ .option("content-type", {
1845
+ describe:
1846
+ "A standard MIME type describing the format of the object data",
1847
+ alias: "ct",
1848
+ requiresArg: true,
1849
+ type: "string",
1850
+ })
1851
+ .option("content-disposition", {
1852
+ describe:
1853
+ "Specifies presentational information for the object",
1854
+ alias: "cd",
1855
+ requiresArg: true,
1856
+ type: "string",
1857
+ })
1858
+ .option("content-encoding", {
1859
+ describe:
1860
+ "Specifies what content encodings have been applied to the object and thus what decoding mechanisms must be applied to obtain the media-type referenced by the Content-Type header field",
1861
+ alias: "ce",
1862
+ requiresArg: true,
1863
+ type: "string",
1864
+ })
1865
+ .option("content-language", {
1866
+ describe: "The language the content is in",
1867
+ alias: "cl",
1868
+ requiresArg: true,
1869
+ type: "string",
1870
+ })
1871
+ .option("cache-control", {
1872
+ describe:
1873
+ "Specifies caching behavior along the request/reply chain",
1874
+ alias: "cc",
1875
+ requiresArg: true,
1876
+ type: "string",
1877
+ })
1878
+ .option("expires", {
1879
+ describe:
1880
+ "The date and time at which the object is no longer cacheable",
1881
+ alias: "e",
1882
+ requiresArg: true,
1883
+ type: "string",
1884
+ });
1885
+ },
1886
+ async (objectPutYargs) => {
1887
+ await printWranglerBanner();
1888
+
1889
+ const config = readConfig(
1890
+ objectPutYargs.config as ConfigPath,
1891
+ objectPutYargs
1892
+ );
1893
+ const accountId = await requireAuth(config);
1894
+ const { objectPath, file, pipe, ...options } = objectPutYargs;
1895
+ const { bucket, key } = bucketAndKeyFromObjectPath(objectPath);
1896
+ if (!file && !pipe) {
1897
+ throw new CommandLineArgsError(
1898
+ "Either the --file or --pipe options are required."
1899
+ );
1900
+ }
1901
+ let object: Readable | Buffer;
1902
+ let objectSize: number;
1903
+ if (file) {
1904
+ object = fs.createReadStream(file);
1905
+ const stats = fs.statSync(file);
1906
+ objectSize = stats.size;
1907
+ } else {
1908
+ object = await new Promise<Buffer>((resolve, reject) => {
1909
+ const stdin = process.stdin;
1910
+ const chunks = Array<Buffer>();
1911
+ stdin.on("data", (chunk) => chunks.push(chunk));
1912
+ stdin.on("end", () => resolve(Buffer.concat(chunks)));
1913
+ stdin.on("error", (err) =>
1914
+ reject(
1915
+ new CommandLineArgsError(
1916
+ `Could not pipe. Reason: "${err.message}"`
1917
+ )
1918
+ )
1919
+ );
1920
+ });
1921
+ objectSize = object.byteLength;
1922
+ }
1923
+
1924
+ logger.log(`Creating object "${key}" in bucket "${bucket}".`);
1925
+ await putR2Object(accountId, bucket, key, object, {
1926
+ ...options,
1927
+ "content-length": `${objectSize}`,
1928
+ });
1929
+ logger.log("Upload complete.");
1930
+ }
1931
+ )
1932
+ .command(
1933
+ "delete <objectPath>",
1934
+ "Delete an object in an R2 bucket",
1935
+ (objectDeleteYargs) => {
1936
+ return objectDeleteYargs.positional("objectPath", {
1937
+ describe:
1938
+ "The destination object path in the form of {bucket}/{key}",
1939
+ type: "string",
1940
+ });
1941
+ },
1942
+ async (args) => {
1943
+ const { objectPath } = args;
1944
+ await printWranglerBanner();
1945
+
1946
+ const config = readConfig(args.config as ConfigPath, args);
1947
+ const accountId = await requireAuth(config);
1948
+ const { bucket, key } = bucketAndKeyFromObjectPath(objectPath);
1949
+ logger.log(`Deleting object "${key}" from bucket "${bucket}".`);
1950
+
1951
+ await deleteR2Object(accountId, bucket, key);
1952
+ logger.log("Delete complete.");
1953
+ }
1954
+ );
1955
+ })
1956
+
1749
1957
  .command("bucket", "Manage R2 buckets", (r2BucketYargs) => {
1750
1958
  r2BucketYargs.command(
1751
1959
  "create <name>",