wrangler 0.0.2 → 0.0.6

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 (69) hide show
  1. package/README.md +51 -55
  2. package/bin/wrangler.js +36 -0
  3. package/import_meta_url.js +3 -0
  4. package/miniflare-config-stubs/.env.empty +0 -0
  5. package/miniflare-config-stubs/package.empty.json +1 -0
  6. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  7. package/package.json +111 -9
  8. package/src/__tests__/clipboardy-mock.js +4 -0
  9. package/src/__tests__/index.test.ts +391 -0
  10. package/src/__tests__/jest.setup.ts +17 -0
  11. package/src/__tests__/mock-cfetch.js +42 -0
  12. package/src/__tests__/mock-dialogs.ts +65 -0
  13. package/src/api/form_data.ts +141 -0
  14. package/src/api/inspect.ts +430 -0
  15. package/src/api/preview.ts +128 -0
  16. package/src/api/worker.ts +161 -0
  17. package/src/cfetch.ts +72 -0
  18. package/src/cli.ts +10 -0
  19. package/src/config.ts +122 -0
  20. package/src/dev.tsx +867 -0
  21. package/src/dialogs.tsx +77 -0
  22. package/src/index.tsx +1875 -0
  23. package/src/kv.tsx +211 -0
  24. package/src/module-collection.ts +64 -0
  25. package/src/pages.tsx +818 -0
  26. package/src/proxy.ts +104 -0
  27. package/src/publish.ts +358 -0
  28. package/src/sites.tsx +115 -0
  29. package/src/tail.tsx +71 -0
  30. package/src/user.tsx +1029 -0
  31. package/static-asset-facade.js +47 -0
  32. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +332 -0
  33. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +176 -0
  34. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +25 -0
  35. package/vendor/@cloudflare/kv-asset-handler/README.md +245 -0
  36. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +32 -0
  37. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +354 -0
  38. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +13 -0
  39. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +148 -0
  40. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +1 -0
  41. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +436 -0
  42. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +1 -0
  43. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +40 -0
  44. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +1 -0
  45. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +42 -0
  46. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +26 -0
  47. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +31 -0
  48. package/vendor/@cloudflare/kv-asset-handler/package.json +52 -0
  49. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +296 -0
  50. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +136 -0
  51. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +464 -0
  52. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +33 -0
  53. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +42 -0
  54. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +39 -0
  55. package/vendor/wrangler-mime/CHANGELOG.md +289 -0
  56. package/vendor/wrangler-mime/LICENSE +21 -0
  57. package/vendor/wrangler-mime/Mime.js +97 -0
  58. package/vendor/wrangler-mime/README.md +187 -0
  59. package/vendor/wrangler-mime/cli.js +46 -0
  60. package/vendor/wrangler-mime/index.js +4 -0
  61. package/vendor/wrangler-mime/lite.js +4 -0
  62. package/vendor/wrangler-mime/package.json +52 -0
  63. package/vendor/wrangler-mime/types/other.js +1 -0
  64. package/vendor/wrangler-mime/types/standard.js +1 -0
  65. package/wrangler-dist/cli.js +125758 -0
  66. package/wrangler-dist/cli.js.map +7 -0
  67. package/.npmignore +0 -15
  68. package/index.js +0 -250
  69. package/tests/is.spec.js +0 -1155
package/src/index.tsx ADDED
@@ -0,0 +1,1875 @@
1
+ import React from "react";
2
+ import { render } from "ink";
3
+ import Dev from "./dev";
4
+ import { readFile } from "node:fs/promises";
5
+ import makeCLI from "yargs";
6
+ import { hideBin } from "yargs/helpers";
7
+ import type yargs from "yargs";
8
+ import { findUp } from "find-up";
9
+ import TOML from "@iarna/toml";
10
+ import type { Config } from "./config";
11
+ import { confirm, prompt } from "./dialogs";
12
+ import { version as wranglerVersion } from "../package.json";
13
+ import {
14
+ login,
15
+ logout,
16
+ listScopes,
17
+ initialise as initialiseUserConfig,
18
+ loginOrRefreshIfRequired,
19
+ getAccountId,
20
+ } from "./user";
21
+ import {
22
+ getNamespaceId,
23
+ listNamespaces,
24
+ listNamespaceKeys,
25
+ putKeyValue,
26
+ putBulkKeyValue,
27
+ deleteBulkKeyValue,
28
+ } from "./kv";
29
+
30
+ import { pages } from "./pages";
31
+
32
+ import cfetch from "./cfetch";
33
+
34
+ import publish from "./publish";
35
+ import path from "path/posix";
36
+ import { writeFile } from "node:fs/promises";
37
+ import { toFormData } from "./api/form_data";
38
+
39
+ import { createTail } from "./tail";
40
+ import onExit from "signal-exit";
41
+ import { setTimeout } from "node:timers/promises";
42
+ import * as fs from "node:fs";
43
+ import { execa } from "execa";
44
+
45
+ async function readConfig(path?: string): Promise<Config> {
46
+ const config: Config = {};
47
+ if (!path) {
48
+ path = await findUp("wrangler.toml");
49
+ // TODO - terminate this early instead of going all the way to the root
50
+ }
51
+
52
+ if (path) {
53
+ const tml: string = await readFile(path, "utf-8");
54
+ const parsed = TOML.parse(tml) as Config;
55
+ Object.assign(config, parsed);
56
+ }
57
+
58
+ const inheritedFields = [
59
+ "name",
60
+ "account_id",
61
+ "workers_dev",
62
+ "compatibility_date",
63
+ "compatibility_flags",
64
+ "zone_id",
65
+ "routes",
66
+ "route",
67
+ "jsx_factory",
68
+ "jsx_fragment",
69
+ "site",
70
+ "triggers",
71
+ "usage_model",
72
+ ];
73
+
74
+ Object.keys(config.env || {}).forEach((env) => {
75
+ inheritedFields.forEach((field) => {
76
+ if (config[field] !== undefined && config.env[env][field] === undefined) {
77
+ config.env[env][field] = config[field]; // TODO: - shallow copy?
78
+ }
79
+ });
80
+ });
81
+
82
+ const mirroredFields = ["vars", "kv_namespaces", "durable_objects"];
83
+ Object.keys(config.env || {}).forEach((env) => {
84
+ mirroredFields.forEach((field) => {
85
+ // if it exists on top level, it should exist on env defns
86
+ Object.keys(config[field] || {}).forEach((fieldKey) => {
87
+ if (!(fieldKey in config.env[env][field])) {
88
+ console.error(
89
+ `In your configuration, "${field}.${fieldKey}" exists at a top level, but not on "env.${env}". This is not what you probably want, since the field "${field}" is not inherited by environments. Please add "${field}.${fieldKey}" to "env.${env}".`
90
+ );
91
+ }
92
+ });
93
+ });
94
+ });
95
+
96
+ // todo: validate, add defaults
97
+ // let's just do some basics for now
98
+
99
+ // @ts-expect-error we're being sneaky here for now
100
+ config.__path__ = path;
101
+
102
+ return config;
103
+ }
104
+
105
+ // a helper to demand one of a set of options
106
+ // via https://github.com/yargs/yargs/issues/1093#issuecomment-491299261
107
+ function demandOneOfOption(...options: string[]) {
108
+ return function (argv: yargs.Arguments) {
109
+ const count = options.filter((option) => argv[option]).length;
110
+ const lastOption = options.pop();
111
+
112
+ if (count === 0) {
113
+ throw new Error(
114
+ `Exactly one of the arguments ${options.join(
115
+ ", "
116
+ )} and ${lastOption} is required`
117
+ );
118
+ } else if (count > 1) {
119
+ throw new Error(
120
+ `Arguments ${options.join(
121
+ ", "
122
+ )} and ${lastOption} are mutually exclusive`
123
+ );
124
+ }
125
+
126
+ return true;
127
+ };
128
+ }
129
+
130
+ export async function main(argv: string[]): Promise<void> {
131
+ const yargs = makeCLI(hideBin(process.argv))
132
+ .command(
133
+ // the default is to simply print the help menu
134
+ ["*"],
135
+ false,
136
+ () => {},
137
+ (args) => {
138
+ yargs.showHelp("log");
139
+ if (args._.length > 0) {
140
+ console.error(`\nUnknown command: ${args._}.`);
141
+ }
142
+ }
143
+ )
144
+ .scriptName("wrangler")
145
+ .wrap(null);
146
+
147
+ // you will note that we use the form for all commands where we use the builder function
148
+ // to define options and subcommands. Further we return the result of this builder even
149
+ // tho it's not completely necessary. The reason is that it's required for type inference
150
+ // of the args in the handle function.I wish we could enforce this pattern, but this
151
+ // comment will have to do for now.
152
+
153
+ // also annoying that choices[] doesn't get inferred as an enum. bleh.
154
+
155
+ // [DEPRECATED] generate
156
+ yargs.command(
157
+ // we definitely want to move away from us cloning github templates
158
+ // we can do something better here, let's see
159
+ "generate [name] [template]",
160
+ false,
161
+ (yargs) => {
162
+ return yargs
163
+ .positional("name", {
164
+ describe: "Name of the Workers project",
165
+ default: "worker",
166
+ })
167
+ .positional("template", {
168
+ describe: "a link to a GitHub template",
169
+ default: "https://github.com/cloudflare/worker-template",
170
+ });
171
+ },
172
+ () => {
173
+ // "👯 [DEPRECATED]. Scaffold a Cloudflare Workers project from a public GitHub repository.",
174
+ console.error(
175
+ "`wrangler generate` has been deprecated, please refer to TODO://some/path for alternatives"
176
+ );
177
+ }
178
+ );
179
+
180
+ // init
181
+ yargs.command(
182
+ "init [name]",
183
+ "📥 Create a wrangler.toml configuration file",
184
+ (yargs) => {
185
+ return yargs.positional("name", {
186
+ describe: "The name of your worker.",
187
+ type: "string",
188
+ });
189
+ },
190
+ async (args) => {
191
+ if ("type" in args) {
192
+ let message = "The --type option is no longer supported.";
193
+ if (args.type === "webpack") {
194
+ message +=
195
+ "\nIf you wish to use webpack then you will need to create a custom build.";
196
+ // TODO: Add a link to docs
197
+ }
198
+ console.error(message);
199
+ return;
200
+ }
201
+
202
+ const destination = path.join(process.cwd(), "wrangler.toml");
203
+ if (fs.existsSync(destination)) {
204
+ console.error(`${destination} file already exists!`);
205
+ const result = await confirm(
206
+ "Do you want to continue initializing this project?"
207
+ );
208
+ if (!result) {
209
+ return;
210
+ }
211
+ }
212
+
213
+ const compatibilityDate = new Date().toISOString().substring(0, 10);
214
+ try {
215
+ await writeFile(
216
+ destination,
217
+ `compatibility_date = "${compatibilityDate}"` + "\n"
218
+ );
219
+ console.log(`✨ Successfully created wrangler.toml`);
220
+ // TODO: suggest next steps?
221
+ } catch (err) {
222
+ console.error(`Failed to create wrangler.toml`);
223
+ console.error(err);
224
+ throw err;
225
+ }
226
+
227
+ // if no package.json, ask, and if yes, create one
228
+ let pathToPackageJson = await findUp("package.json");
229
+
230
+ if (!pathToPackageJson) {
231
+ if (
232
+ await confirm("No package.json found. Would you like to create one?")
233
+ ) {
234
+ await writeFile(
235
+ path.join(process.cwd(), "package.json"),
236
+ JSON.stringify(
237
+ {
238
+ name: "worker",
239
+ version: "0.0.1",
240
+ },
241
+ null,
242
+ " "
243
+ ) + "\n"
244
+ );
245
+ console.log(`✨ Created package.json`);
246
+ pathToPackageJson = path.join(process.cwd(), "package.json");
247
+ } else {
248
+ return;
249
+ }
250
+ }
251
+
252
+ // if workers-types doesn't exist as a dependency
253
+ // offer to install it
254
+ // and make a tsconfig?
255
+ let pathToTSConfig = await findUp("tsconfig.json");
256
+ if (!pathToTSConfig) {
257
+ if (await confirm("Would you like to use typescript?")) {
258
+ await writeFile(
259
+ path.join(process.cwd(), "tsconfig.json"),
260
+ JSON.stringify(
261
+ {
262
+ compilerOptions: {
263
+ target: "esnext",
264
+ module: "esnext",
265
+ moduleResolution: "node",
266
+ esModuleInterop: true,
267
+ allowJs: true,
268
+ allowSyntheticDefaultImports: true,
269
+ isolatedModules: true,
270
+ noEmit: true,
271
+ lib: ["esnext"],
272
+ jsx: "react",
273
+ resolveJsonModule: true,
274
+ types: ["@cloudflare/workers-types"],
275
+ },
276
+ },
277
+ null,
278
+ " "
279
+ ) + "\n"
280
+ );
281
+ await execa("npm", [
282
+ "install",
283
+ "@cloudflare/workers-types",
284
+ "--save-dev",
285
+ ]);
286
+ console.log(
287
+ `✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies`
288
+ );
289
+ pathToTSConfig = path.join(process.cwd(), "tsconfig.json");
290
+ }
291
+ }
292
+ }
293
+ );
294
+
295
+ // build
296
+ yargs.command(
297
+ "build",
298
+ false,
299
+ (yargs) => {
300
+ return yargs.option("env", {
301
+ describe: "Perform on a specific environment",
302
+ });
303
+ },
304
+ () => {
305
+ // "[DEPRECATED] 🦀 Build your project (if applicable)",
306
+ console.error(
307
+ "`wrangler build` has been deprecated, please refer to TODO://some/path for alternatives"
308
+ );
309
+ }
310
+ );
311
+
312
+ // login
313
+ yargs.command(
314
+ // this needs scopes as an option?
315
+ "login",
316
+ false, // we don't need to show this in the menu
317
+ // "🔓 Login to Cloudflare",
318
+ (yargs) => {
319
+ // TODO: This needs some copy editing
320
+ // I mean, this entire app does, but this too.
321
+ return yargs
322
+ .option("scopes-list", {
323
+ describe: "list all the available OAuth scopes with descriptions.",
324
+ })
325
+ .option("scopes", {
326
+ describe: "allows to choose your set of OAuth scopes.",
327
+ array: true,
328
+ type: "string",
329
+ });
330
+
331
+ // TODO: scopes
332
+ },
333
+ async (args) => {
334
+ if (args["scopes-list"]) {
335
+ listScopes();
336
+ return;
337
+ }
338
+ if (args.scopes) {
339
+ if (args.scopes.length === 0) {
340
+ // don't allow no scopes to be passed, that would be weird
341
+ listScopes();
342
+ return;
343
+ }
344
+ await login({ scopes: args.scopes });
345
+ return;
346
+ }
347
+ await login();
348
+
349
+ // TODO: would be nice if it optionally saved login
350
+ // creds inside node_modules/.cache or something
351
+ // this way you could have multiple users on a single machine
352
+ }
353
+ );
354
+
355
+ // logout
356
+ yargs.command(
357
+ // this needs scopes as an option?
358
+ "logout",
359
+ false, // we don't need to show this in the menu
360
+ // "🚪 Logout from Cloudflare",
361
+ () => {},
362
+ async () => {
363
+ await logout();
364
+ }
365
+ );
366
+
367
+ // whoami
368
+ yargs.command(
369
+ "whoami",
370
+ false, // we don't need to show this the menu
371
+ // "🕵️ Retrieve your user info and test your auth config",
372
+ () => {},
373
+ (args) => {
374
+ console.log(":whoami", args);
375
+ }
376
+ );
377
+
378
+ // config
379
+ yargs.command(
380
+ "config",
381
+ false,
382
+ () => {},
383
+ () => {
384
+ // "🕵️ Authenticate Wrangler with a Cloudflare API Token",
385
+ console.error(
386
+ "`wrangler config` has been deprecated, please refer to TODO://some/path for alternatives"
387
+ );
388
+ }
389
+ );
390
+
391
+ // dev
392
+ yargs.command(
393
+ "dev <filename>",
394
+ "👂 Start a local server for developing your worker",
395
+ (yargs) => {
396
+ return yargs
397
+ .positional("filename", { describe: "entry point", type: "string" })
398
+ .option("name", {
399
+ describe: "name of the script",
400
+ type: "string",
401
+ })
402
+ .option("format", {
403
+ choices: ["modules", "service-worker"] as const,
404
+ describe: "Choose an entry type",
405
+ })
406
+ .option("env", {
407
+ describe: "Perform on a specific environment",
408
+ type: "string",
409
+ // TODO: get choices for the toml file?
410
+ })
411
+ .option("ip", {
412
+ describe: "IP address to listen on",
413
+ type: "string",
414
+ default: "127.0.0.1",
415
+ })
416
+ .option("port", {
417
+ describe: "Port to listen on, defaults to 8787",
418
+ type: "number",
419
+ default: 8787,
420
+ })
421
+ .option("host", {
422
+ type: "string",
423
+ describe:
424
+ "Host to forward requests to, defaults to the zone of project",
425
+ })
426
+ .option("local-protocol", {
427
+ default: "http",
428
+ describe: "Protocol to listen to requests on, defaults to http.",
429
+ choices: ["http", "https"],
430
+ })
431
+ .option("public", {
432
+ describe: "Static assets to be served",
433
+ type: "string",
434
+ })
435
+ .option("site", {
436
+ describe: "Root folder of static assets for Workers Sites",
437
+ type: "string",
438
+ })
439
+ .option("upstream-protocol", {
440
+ default: "https",
441
+ describe:
442
+ "Protocol to forward requests to host on, defaults to https.",
443
+ choices: ["http", "https"],
444
+ })
445
+ .option("jsx-factory", {
446
+ describe: "The function that is called for each JSX element",
447
+ type: "string",
448
+ })
449
+ .option("jsx-fragment", {
450
+ describe: "The function that is called for each JSX fragment",
451
+ type: "string",
452
+ });
453
+ },
454
+ async (args) => {
455
+ const { filename, format } = args;
456
+ const config = args.config as Config;
457
+
458
+ // -- snip, extract --
459
+
460
+ if (!args.local) {
461
+ const loggedIn = await loginOrRefreshIfRequired();
462
+ if (!loggedIn) {
463
+ // didn't login, let's just quit
464
+ console.log("Did not login, quitting...");
465
+ return;
466
+ }
467
+ if (!config.account_id) {
468
+ config.account_id = await getAccountId();
469
+ if (!config.account_id) {
470
+ console.error("No account id found, quitting...");
471
+ return;
472
+ }
473
+ }
474
+ }
475
+
476
+ // -- snip, end --
477
+
478
+ const envRootObj = args.env ? config.env[args.env] || {} : config;
479
+
480
+ // TODO: this error shouldn't actually happen,
481
+ // but we haven't fixed it internally yet
482
+ if ("durable_objects" in envRootObj) {
483
+ if (!(args.name || config.name)) {
484
+ console.warn(
485
+ 'A worker with durable objects need to be named, or it may not work as expected. Add a "name" into wrangler.toml, or pass it in the command line with --name.'
486
+ );
487
+ }
488
+ // TODO: if not already published, publish a draft worker
489
+ }
490
+
491
+ render(
492
+ <Dev
493
+ name={args.name || config.name}
494
+ entry={filename}
495
+ buildCommand={config.build || {}}
496
+ format={format}
497
+ initialMode={args.local ? "local" : "remote"}
498
+ jsxFactory={args["jsx-factory"] || envRootObj?.jsx_factory}
499
+ jsxFragment={args["jsx-fragment"] || envRootObj?.jsx_fragment}
500
+ accountId={config.account_id}
501
+ site={args.site || config.site?.bucket}
502
+ port={args.port || config.dev?.port}
503
+ public={args.public}
504
+ compatibilityDate={config.compatibility_date}
505
+ compatibilityFlags={config.compatibility_flags}
506
+ usageModel={config.usage_model}
507
+ variables={{
508
+ ...(envRootObj?.vars || {}),
509
+ ...(envRootObj?.kv_namespaces || []).reduce(
510
+ (obj, { binding, preview_id }) => {
511
+ if (!preview_id) {
512
+ // TODO: This error has to be a _lot_ better, ideally just asking
513
+ // to create a preview namespace for the user automatically
514
+ throw new Error(
515
+ "kv namespaces need a preview id during dev mode"
516
+ );
517
+ }
518
+ return { ...obj, [binding]: { namespaceId: preview_id } };
519
+ },
520
+ {}
521
+ ),
522
+ ...(envRootObj?.durable_objects?.bindings || []).reduce(
523
+ (obj, { name, class_name, script_name }) => {
524
+ return { ...obj, [name]: { class_name, script_name } };
525
+ },
526
+ {}
527
+ ),
528
+ }}
529
+ />
530
+ );
531
+ }
532
+ );
533
+
534
+ // publish
535
+ yargs.command(
536
+ "publish [script]",
537
+ "🆙 Publish your Worker to Cloudflare.",
538
+ (yargs) => {
539
+ return yargs
540
+ .option("env", {
541
+ type: "string",
542
+ describe: "Perform on a specific environment",
543
+ })
544
+ .positional("script", {
545
+ describe: "script to upload",
546
+ type: "string",
547
+ })
548
+ .option("name", {
549
+ describe: "name to use when uploading",
550
+ type: "string",
551
+ })
552
+ .option("public", {
553
+ describe: "Static assets to be served",
554
+ type: "string",
555
+ })
556
+ .option("site", {
557
+ describe: "Root folder of static assets for Workers Sites",
558
+ type: "string",
559
+ })
560
+ .option("triggers", {
561
+ describe: "cron schedules to attach",
562
+ alias: ["schedule", "schedules"],
563
+ type: "array",
564
+ })
565
+ .option("routes", {
566
+ describe: "routes to upload",
567
+ alias: "route",
568
+ type: "array",
569
+ })
570
+ .option("services", {
571
+ describe: "experimental support for services",
572
+ type: "boolean",
573
+ default: "false",
574
+ hidden: true,
575
+ })
576
+ .option("jsx-factory", {
577
+ describe: "The function that is called for each JSX element",
578
+ type: "string",
579
+ })
580
+ .option("jsx-fragment", {
581
+ describe: "The function that is called for each JSX fragment",
582
+ type: "string",
583
+ });
584
+ },
585
+ async (args) => {
586
+ if (args.local) {
587
+ console.error("🚫 Local publishing is not yet supported");
588
+ return;
589
+ }
590
+ const config = args.config as Config;
591
+
592
+ // -- snip, extract --
593
+ if (!args.local) {
594
+ const loggedIn = await loginOrRefreshIfRequired();
595
+ if (!loggedIn) {
596
+ // didn't login, let's just quit
597
+ console.log("Did not login, quitting...");
598
+ return;
599
+ }
600
+ if (!config.account_id) {
601
+ config.account_id = await getAccountId();
602
+ if (!config.account_id) {
603
+ console.error("No account id found, quitting...");
604
+ return;
605
+ }
606
+ }
607
+ }
608
+
609
+ // -- snip, end --
610
+
611
+ await publish({
612
+ config: args.config as Config,
613
+ name: args.name,
614
+ script: args.script,
615
+ env: args.env,
616
+ triggers: args.triggers,
617
+ jsxFactory: args["jsx-factory"],
618
+ jsxFragment: args["jsx-fragment"],
619
+ routes: args.routes,
620
+ public: args.public,
621
+ site: args.site,
622
+ });
623
+ }
624
+ );
625
+
626
+ // tail
627
+ yargs.command(
628
+ "tail [name]",
629
+ "🦚 Starts a log tailing session for a deployed Worker.",
630
+ (yargs) => {
631
+ return (
632
+ yargs
633
+ .positional("name", {
634
+ describe: "name of the worker",
635
+ type: "string",
636
+ })
637
+ // TODO: auto-detect if this should be json or pretty based on atty
638
+ .option("format", {
639
+ default: "json",
640
+ choices: ["json", "pretty"],
641
+ describe: "The format of log entries",
642
+ })
643
+ .option("status", {
644
+ choices: ["ok", "error", "canceled"],
645
+ describe: "Filter by invocation status",
646
+ })
647
+ .option("header", {
648
+ type: "string",
649
+ describe: "Filter by HTTP header",
650
+ })
651
+ .option("method", {
652
+ type: "string",
653
+ describe: "Filter by HTTP method",
654
+ })
655
+ .option("sampling-rate", {
656
+ type: "number",
657
+ describe: "Adds a percentage of requests to log sampling rate",
658
+ })
659
+ .option("search", {
660
+ type: "string",
661
+ describe: "Filter by a text match in console.log messages",
662
+ })
663
+ .option("env", {
664
+ type: "string",
665
+ describe: "Perform on a specific environment",
666
+ })
667
+ );
668
+ // TODO: filter by client ip, which can be 'self' or an ip address
669
+ },
670
+ async (args) => {
671
+ const config = args.config as Config;
672
+
673
+ if (!(args.name || config.name)) {
674
+ console.error("Missing script name");
675
+ return;
676
+ }
677
+ const scriptName = `${args.name || config.name}${
678
+ args.env ? `-${args.env}` : ""
679
+ }`;
680
+
681
+ // -- snip, extract --
682
+
683
+ if (!args.local) {
684
+ const loggedIn = await loginOrRefreshIfRequired();
685
+ if (!loggedIn) {
686
+ // didn't login, let's just quit
687
+ console.log("Did not login, quitting...");
688
+ return;
689
+ }
690
+ if (!config.account_id) {
691
+ config.account_id = await getAccountId();
692
+ if (!config.account_id) {
693
+ console.error("No account id found, quitting...");
694
+ return;
695
+ }
696
+ }
697
+ }
698
+
699
+ // -- snip, end --
700
+
701
+ const accountId = config.account_id;
702
+
703
+ const filters = {
704
+ status: args.status as "ok" | "error" | "canceled",
705
+ header: args.header,
706
+ method: args.method,
707
+ "sampling-rate": args["sampling-rate"],
708
+ search: args.search,
709
+ };
710
+
711
+ const { tail, expiration, /* sendHeartbeat, */ deleteTail } =
712
+ await createTail(accountId, scriptName, filters);
713
+
714
+ console.log(
715
+ `successfully created tail, expires at ${expiration.toLocaleString()}`
716
+ );
717
+
718
+ onExit(async () => {
719
+ tail.terminate();
720
+ await deleteTail();
721
+ });
722
+
723
+ tail.on("message", (data) => {
724
+ console.log(JSON.stringify(JSON.parse(data.toString()), null, " "));
725
+ });
726
+
727
+ while (tail.readyState !== tail.OPEN) {
728
+ switch (tail.readyState) {
729
+ case tail.CONNECTING:
730
+ await setTimeout(1000);
731
+ break;
732
+ case tail.CLOSING:
733
+ await setTimeout(1000);
734
+ break;
735
+ case tail.CLOSED:
736
+ process.exit(1);
737
+ }
738
+ }
739
+
740
+ console.log(`Connected to ${scriptName}, waiting for logs...`);
741
+ }
742
+ );
743
+
744
+ // preview
745
+ yargs.command(
746
+ "preview [method] [body]",
747
+ false,
748
+ (yargs) => {
749
+ return yargs
750
+ .positional("method", {
751
+ describe: "Type of request to preview your worker",
752
+ choices: ["GET", "POST"],
753
+ default: ["GET"],
754
+ })
755
+ .positional("body", {
756
+ type: "string",
757
+ describe: "Body string to post to your preview worker request.",
758
+ default: "Null",
759
+ })
760
+ .option("env", {
761
+ type: "string",
762
+ describe: "Perform on a specific environment",
763
+ })
764
+ .option("watch", {
765
+ default: true,
766
+ describe: "Enable live preview",
767
+ type: "boolean",
768
+ });
769
+ },
770
+ () => {
771
+ // "🔬 [DEPRECATED] Preview your code temporarily on cloudflareworkers.com"
772
+ console.error(
773
+ "`wrangler preview` has been deprecated, please refer to TODO://some/path for alternatives"
774
+ );
775
+ }
776
+ );
777
+
778
+ // route
779
+ yargs.command(
780
+ "route",
781
+ false, // I think we want to hide this command
782
+ // "➡️ List or delete worker routes",
783
+ (yargs) => {
784
+ return yargs
785
+ .command(
786
+ "list",
787
+ "List a route associated with a zone",
788
+ (yargs) => {
789
+ return yargs
790
+ .option("env", {
791
+ type: "string",
792
+ describe: "Perform on a specific environment",
793
+ })
794
+ .option("zone", {
795
+ type: "string",
796
+ describe: "zone id",
797
+ })
798
+ .positional("zone", {
799
+ describe: "zone id",
800
+ type: "string",
801
+ });
802
+ },
803
+ async (args) => {
804
+ console.log(":route list", args);
805
+ // TODO: use environment (current wrangler doesn't do so?)
806
+ const zone = args.zone || (args.config as Config).zone_id;
807
+ if (!zone) {
808
+ console.error("missing zone id");
809
+ return;
810
+ }
811
+
812
+ console.log(await cfetch(`/zones/${zone}/workers/routes`));
813
+ }
814
+ )
815
+ .command(
816
+ "delete <id>",
817
+ "Delete a route associated with a zone",
818
+ (yargs) => {
819
+ return yargs
820
+ .positional("id", {
821
+ describe: "The hash of the route ID to delete.",
822
+ type: "string",
823
+ })
824
+ .option("zone", {
825
+ type: "string",
826
+ describe: "zone id",
827
+ })
828
+ .option("env", {
829
+ type: "string",
830
+ describe: "Perform on a specific environment",
831
+ });
832
+ },
833
+ async (args) => {
834
+ console.log(":route delete", args);
835
+ // TODO: use environment (current wrangler doesn't do so?)
836
+ const zone = args.zone || (args.config as Config).zone_id;
837
+ if (!zone) {
838
+ throw new Error("missing zone id");
839
+ }
840
+
841
+ console.log(
842
+ await cfetch(`/zones/${zone}/workers/routes/${args.id}`, {
843
+ method: "DELETE",
844
+ })
845
+ );
846
+ }
847
+ );
848
+ }
849
+ );
850
+
851
+ // subdomain
852
+ yargs.command(
853
+ "subdomain [name]",
854
+ false,
855
+ // "👷 Create or change your workers.dev subdomain.",
856
+ (yargs) => {
857
+ return yargs.positional("name", { type: "string" });
858
+ },
859
+ () => {
860
+ console.error(
861
+ "`wrangler subdomain` has been deprecated, please refer to TODO://some/path for alternatives"
862
+ );
863
+ }
864
+ );
865
+
866
+ // secret
867
+ yargs.command(
868
+ "secret",
869
+ "🤫 Generate a secret that can be referenced in the worker script",
870
+ (yargs) => {
871
+ return yargs
872
+ .command(
873
+ "put <key>",
874
+ "Create or update a secret variable for a script",
875
+ (yargs) => {
876
+ return yargs
877
+ .positional("key", {
878
+ describe: "The variable name to be accessible in the script.",
879
+ type: "string",
880
+ })
881
+ .option("name", {
882
+ describe: "name of the script",
883
+ type: "string",
884
+ })
885
+ .option("env", {
886
+ type: "string",
887
+ describe:
888
+ "Binds the secret to the script of the specific environment.",
889
+ });
890
+ },
891
+ async (args) => {
892
+ if (args.local) {
893
+ console.error("--local not implemented for this command yet");
894
+ return;
895
+ }
896
+ const config = args.config as Config;
897
+
898
+ // TODO: use environment (how does current wrangler do it?)
899
+ const scriptName = args.name || config.name;
900
+ if (!scriptName) {
901
+ console.error("Missing script name");
902
+ return;
903
+ }
904
+
905
+ // -- snip, extract --
906
+
907
+ if (!args.local) {
908
+ const loggedIn = await loginOrRefreshIfRequired();
909
+ if (!loggedIn) {
910
+ // didn't login, let's just quit
911
+ console.log("Did not login, quitting...");
912
+ return;
913
+ }
914
+ if (!config.account_id) {
915
+ config.account_id = await getAccountId();
916
+ if (!config.account_id) {
917
+ console.error("No account id found, quitting...");
918
+ return;
919
+ }
920
+ }
921
+ }
922
+
923
+ // -- snip, end --
924
+
925
+ const secretValue = await prompt(
926
+ "Enter a secret value:",
927
+ "password"
928
+ );
929
+ async function submitSecret() {
930
+ return await cfetch(
931
+ `/accounts/${config.account_id}/workers/scripts/${scriptName}/secrets/`,
932
+ {
933
+ method: "PUT",
934
+ headers: { "Content-Type": "application/json" },
935
+ body: JSON.stringify({
936
+ name: args.key,
937
+ text: secretValue,
938
+ type: "secret_text",
939
+ }),
940
+ }
941
+ );
942
+ }
943
+
944
+ try {
945
+ console.log(await submitSecret());
946
+ } catch (e) {
947
+ if (e.code === 10007) {
948
+ // upload a draft worker
949
+ await cfetch(
950
+ `/accounts/${config.account_id}/workers/scripts/${scriptName}`,
951
+ {
952
+ method: "PUT",
953
+ // @ts-expect-error TODO: fix this error!
954
+ body: toFormData({
955
+ main: {
956
+ name: scriptName,
957
+ content: `export default { fetch() {} }`,
958
+ type: "esm",
959
+ },
960
+ variables: {},
961
+ modules: [],
962
+ }),
963
+ }
964
+ );
965
+
966
+ // and then try again
967
+ console.log(await submitSecret());
968
+ // TODO: delete the draft worker if this failed too?
969
+ }
970
+ }
971
+ }
972
+ )
973
+ .command(
974
+ "delete <key>",
975
+ "Delete a secret variable from a script",
976
+ (yargs) => {
977
+ return yargs
978
+ .positional("key", {
979
+ describe: "The variable name to be accessible in the script.",
980
+ type: "string",
981
+ })
982
+ .option("name", {
983
+ describe: "name of the script",
984
+ type: "string",
985
+ })
986
+ .option("env", {
987
+ type: "string",
988
+ describe:
989
+ "Binds the secret to the script of the specific environment.",
990
+ });
991
+ },
992
+ async (args) => {
993
+ if (args.local) {
994
+ console.error("--local not implemented for this command yet");
995
+ return;
996
+ }
997
+ const config = args.config as Config;
998
+
999
+ // TODO: use environment (how does current wrangler do it?)
1000
+ const scriptName = args.name || config.name;
1001
+ if (!scriptName) {
1002
+ throw new Error("Missing script name");
1003
+ }
1004
+
1005
+ // -- snip, extract --
1006
+
1007
+ if (!args.local) {
1008
+ const loggedIn = await loginOrRefreshIfRequired();
1009
+ if (!loggedIn) {
1010
+ // didn't login, let's just quit
1011
+ console.log("Did not login, quitting...");
1012
+ return;
1013
+ }
1014
+ if (!config.account_id) {
1015
+ config.account_id = await getAccountId();
1016
+ if (!config.account_id) {
1017
+ console.error("No account id found, quitting...");
1018
+ return;
1019
+ }
1020
+ }
1021
+ }
1022
+
1023
+ // -- snip, end --
1024
+
1025
+ if (await confirm("Are you sure you want to delete this secret?")) {
1026
+ console.log(
1027
+ `Deleting the secret ${args.key} on script ${scriptName}.`
1028
+ );
1029
+
1030
+ console.log(
1031
+ await cfetch(
1032
+ `/accounts/${config.account_id}/workers/scripts/${scriptName}/secrets/${args.key}`,
1033
+ { method: "DELETE" }
1034
+ )
1035
+ );
1036
+ }
1037
+ }
1038
+ )
1039
+ .command(
1040
+ "list",
1041
+ "List all secrets for a script",
1042
+ (yargs) => {
1043
+ return yargs
1044
+ .option("name", {
1045
+ describe: "name of the script",
1046
+ type: "string",
1047
+ })
1048
+ .option("env", {
1049
+ type: "string",
1050
+ describe:
1051
+ "Binds the secret to the script of the specific environment.",
1052
+ });
1053
+ },
1054
+ async (args) => {
1055
+ if (args.local) {
1056
+ console.error("--local not implemented for this command yet");
1057
+ return;
1058
+ }
1059
+ const config = args.config as Config;
1060
+
1061
+ // TODO: use environment (how does current wrangler do it?)
1062
+ const scriptName = args.name || config.name;
1063
+ if (!scriptName) {
1064
+ console.error("Missing script name");
1065
+ return;
1066
+ }
1067
+
1068
+ // -- snip, extract --
1069
+
1070
+ if (!args.local) {
1071
+ const loggedIn = await loginOrRefreshIfRequired();
1072
+ if (!loggedIn) {
1073
+ // didn't login, let's just quit
1074
+ console.log("Did not login, quitting...");
1075
+ return;
1076
+ }
1077
+ if (!config.account_id) {
1078
+ config.account_id = await getAccountId();
1079
+ if (!config.account_id) {
1080
+ console.error("No account id found, quitting...");
1081
+ return;
1082
+ }
1083
+ }
1084
+ }
1085
+
1086
+ // -- snip, end --
1087
+
1088
+ console.log(
1089
+ await cfetch(
1090
+ `/accounts/${config.account_id}/workers/scripts/${scriptName}/secrets`
1091
+ )
1092
+ );
1093
+ }
1094
+ );
1095
+ }
1096
+ );
1097
+
1098
+ // kv
1099
+ // :namespace
1100
+ yargs.command(
1101
+ "kv:namespace",
1102
+ "🗂️ Interact with your Workers KV Namespaces",
1103
+ (yargs) => {
1104
+ return yargs
1105
+ .command(
1106
+ "create <namespace>",
1107
+ "Create a new namespace",
1108
+ (yargs) => {
1109
+ return yargs
1110
+ .positional("namespace", {
1111
+ describe: "The name of the new namespace",
1112
+ type: "string",
1113
+ })
1114
+ .option("env", {
1115
+ type: "string",
1116
+ describe: "Perform on a specific environment",
1117
+ })
1118
+ .option("preview", {
1119
+ type: "boolean",
1120
+ describe: "Interact with a preview namespace",
1121
+ });
1122
+ },
1123
+ async (args) => {
1124
+ if (args._.length !== 2) {
1125
+ throw new Error(
1126
+ `Did you forget to add quotes around "${
1127
+ args.namespace
1128
+ } ${args._.slice(2).join(" ")}"?`
1129
+ );
1130
+ }
1131
+ const config = args.config as Config;
1132
+ if (!config.name) {
1133
+ console.warn(
1134
+ "No configured name present, using `worker` as a prefix for the title"
1135
+ );
1136
+ }
1137
+
1138
+ const title = `${config.name || "worker"}${
1139
+ args.env ? `-${args.env}` : ""
1140
+ }-${args.namespace}${args.preview ? "_preview" : ""}`;
1141
+
1142
+ if (/[\W]+/.test(args.namespace)) {
1143
+ throw new Error("invalid binding name, needs to be js friendly");
1144
+ }
1145
+
1146
+ if (args.local) {
1147
+ const { Miniflare } = await import("miniflare");
1148
+ const mf = new Miniflare({
1149
+ kvPersist: (args.kvPersist as string) || true,
1150
+ // TODO: these options shouldn't be required
1151
+ script: ` `, // has to be a string with at least one char
1152
+ });
1153
+ await mf.getKVNamespace(title); // this should "create" the namespace
1154
+ console.log(`✨ Success! Created KV namespace ${title}`);
1155
+ return;
1156
+ }
1157
+
1158
+ // -- snip, extract --
1159
+
1160
+ if (!args.local) {
1161
+ const loggedIn = await loginOrRefreshIfRequired();
1162
+ if (!loggedIn) {
1163
+ // didn't login, let's just quit
1164
+ console.log("Did not login, quitting...");
1165
+ return;
1166
+ }
1167
+ if (!config.account_id) {
1168
+ config.account_id = await getAccountId();
1169
+ if (!config.account_id) {
1170
+ console.error("No account id found, quitting...");
1171
+ return;
1172
+ }
1173
+ }
1174
+ }
1175
+
1176
+ // -- snip, end --
1177
+
1178
+ // TODO: generate a binding name stripping non alphanumeric chars
1179
+
1180
+ console.log(`🌀 Creating namespace with title "${title}"`);
1181
+
1182
+ const response = await cfetch<{ id: string }>(
1183
+ `/accounts/${config.account_id}/storage/kv/namespaces`,
1184
+ {
1185
+ method: "POST",
1186
+ headers: {
1187
+ "Content-Type": "application/json",
1188
+ },
1189
+ body: JSON.stringify({
1190
+ title,
1191
+ }),
1192
+ }
1193
+ );
1194
+
1195
+ console.log("✨ Success!");
1196
+ console.log(
1197
+ `Add the following to your configuration file in your kv_namespaces array${
1198
+ args.env ? ` under [env.${args.env}]` : ""
1199
+ }:`
1200
+ );
1201
+ console.log(
1202
+ `{ binding = "${args.namespace}", ${
1203
+ args.preview ? "preview_" : ""
1204
+ }id = "${response.id}" }`
1205
+ );
1206
+ }
1207
+ )
1208
+ .command(
1209
+ "list",
1210
+ "Outputs a list of all KV namespaces associated with your account id.",
1211
+ {},
1212
+ async (args) => {
1213
+ if (args.local) {
1214
+ console.error(`local mode is not yet supported for this command`);
1215
+ return;
1216
+ }
1217
+
1218
+ const config = args.config as Config;
1219
+
1220
+ // -- snip, extract --
1221
+
1222
+ if (!args.local) {
1223
+ const loggedIn = await loginOrRefreshIfRequired();
1224
+ if (!loggedIn) {
1225
+ // didn't login, let's just quit
1226
+ console.log("Did not login, quitting...");
1227
+ return;
1228
+ }
1229
+ if (!config.account_id) {
1230
+ config.account_id = await getAccountId();
1231
+ if (!config.account_id) {
1232
+ console.error("No account id found, quitting...");
1233
+ return;
1234
+ }
1235
+ }
1236
+ }
1237
+
1238
+ // -- snip, end --
1239
+
1240
+ // TODO: we should show bindings if they exist for given ids
1241
+
1242
+ console.log(
1243
+ JSON.stringify(
1244
+ await listNamespaces(config.account_id),
1245
+ null,
1246
+ " "
1247
+ )
1248
+ );
1249
+ }
1250
+ )
1251
+ .command(
1252
+ "delete",
1253
+ "Deletes a given namespace.",
1254
+ (yargs) => {
1255
+ return yargs
1256
+ .option("binding", {
1257
+ type: "string",
1258
+ describe: "The name of the namespace to delete",
1259
+ })
1260
+ .option("namespace-id", {
1261
+ type: "string",
1262
+ describe: "The id of the namespace to delete",
1263
+ })
1264
+ .check(demandOneOfOption("binding", "namespace-id"))
1265
+ .option("env", {
1266
+ type: "string",
1267
+ describe: "Perform on a specific environment",
1268
+ })
1269
+ .option("preview", {
1270
+ type: "boolean",
1271
+ describe: "Interact with a preview namespace",
1272
+ });
1273
+ },
1274
+ async (args) => {
1275
+ if (args.local) {
1276
+ console.error(`local mode is not yet supported for this command`);
1277
+ return;
1278
+ }
1279
+ const config = args.config as Config;
1280
+
1281
+ const id =
1282
+ args["namespace-id"] ||
1283
+ (args.env
1284
+ ? config.env[args.env] || {}
1285
+ : config
1286
+ ).kv_namespaces.find(
1287
+ (namespace) => namespace.binding === args.binding
1288
+ )[args.preview ? "preview_id" : "id"];
1289
+ if (!id) {
1290
+ throw new Error("Are you sure? id not found");
1291
+ }
1292
+
1293
+ // -- snip, extract --
1294
+
1295
+ if (!args.local) {
1296
+ const loggedIn = await loginOrRefreshIfRequired();
1297
+ if (!loggedIn) {
1298
+ // didn't login, let's just quit
1299
+ console.log("Did not login, quitting...");
1300
+ return;
1301
+ }
1302
+
1303
+ if (!config.account_id) {
1304
+ config.account_id = await getAccountId();
1305
+ if (!config.account_id) {
1306
+ console.error("No account id found, quitting...");
1307
+ return;
1308
+ }
1309
+ }
1310
+ }
1311
+
1312
+ // -- snip, end --
1313
+
1314
+ await cfetch<{ id: string }>(
1315
+ `/accounts/${config.account_id}/storage/kv/namespaces/${id}`,
1316
+ { method: "DELETE" }
1317
+ );
1318
+
1319
+ // TODO: recommend they remove it from wrangler.toml
1320
+
1321
+ // test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388
1322
+ // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n]
1323
+ // n
1324
+ // 💁 Not deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388
1325
+ // ➜ test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388
1326
+ // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n]
1327
+ // y
1328
+ // 🌀 Deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388
1329
+ // ✨ Success
1330
+ // ⚠️ Make sure to remove this "kv-namespace" entry from your configuration file!
1331
+ // ➜ test-mf
1332
+
1333
+ // TODO: do it automatically
1334
+
1335
+ // TODO: delete the preview namespace as well?
1336
+ }
1337
+ );
1338
+ }
1339
+ );
1340
+
1341
+ // :key
1342
+ yargs.command(
1343
+ "kv:key",
1344
+ "🔑 Individually manage Workers KV key-value pairs",
1345
+ (yargs) => {
1346
+ return yargs
1347
+ .command(
1348
+ "put <key> [value]",
1349
+ "Writes a single key/value pair to the given namespace.",
1350
+ (yargs) => {
1351
+ return yargs
1352
+ .positional("key", {
1353
+ type: "string",
1354
+ describe: "The key to write to.",
1355
+ })
1356
+ .positional("value", {
1357
+ type: "string",
1358
+ describe: "The value to write.",
1359
+ })
1360
+ .option("binding", {
1361
+ type: "string",
1362
+ describe: "The binding of the namespace to write to.",
1363
+ })
1364
+ .option("namespace-id", {
1365
+ type: "string",
1366
+ describe: "The id of the namespace to write to.",
1367
+ })
1368
+ .check(demandOneOfOption("binding", "namespace-id"))
1369
+ .option("env", {
1370
+ type: "string",
1371
+ describe: "Perform on a specific environment",
1372
+ })
1373
+ .option("preview", {
1374
+ type: "boolean",
1375
+ describe: "Interact with a preview namespace",
1376
+ })
1377
+ .option("ttl", {
1378
+ type: "number",
1379
+ describe: "Time for which the entries should be visible.",
1380
+ })
1381
+ .option("expiration", {
1382
+ type: "number",
1383
+ describe:
1384
+ "Time since the UNIX epoch after which the entry expires",
1385
+ })
1386
+ .option("path", {
1387
+ type: "string",
1388
+ describe: "Read value from the file at a given path.",
1389
+ })
1390
+ .check(demandOneOfOption("value", "path"));
1391
+ },
1392
+ async ({ key, ttl, expiration, ...args }) => {
1393
+ const namespaceId = getNamespaceId(args);
1394
+ const value = args.path
1395
+ ? await readFile(args.path, "utf-8")
1396
+ : args.value;
1397
+ const config = args.config as Config;
1398
+
1399
+ if (args.path) {
1400
+ console.log(
1401
+ `writing the contents of ${args.path} to the key "${key}" on namespace ${namespaceId}`
1402
+ );
1403
+ } else {
1404
+ console.log(
1405
+ `writing the value "${value}" to key "${key}" on namespace ${namespaceId}`
1406
+ );
1407
+ }
1408
+
1409
+ if (args.local) {
1410
+ const { Miniflare } = await import("miniflare");
1411
+ const mf = new Miniflare({
1412
+ kvPersist: (args.kvPersist as string) || true,
1413
+ // TODO: these options shouldn't be required
1414
+ script: ` `, // has to be a string with at least one char
1415
+ });
1416
+ const ns = await mf.getKVNamespace(namespaceId);
1417
+ await ns.put(key, value, { expiration, expirationTtl: ttl });
1418
+ return;
1419
+ }
1420
+ // -- snip, extract --
1421
+
1422
+ if (!args.local) {
1423
+ const loggedIn = await loginOrRefreshIfRequired();
1424
+ if (!loggedIn) {
1425
+ // didn't login, let's just quit
1426
+ console.log("Did not login, quitting...");
1427
+ return;
1428
+ }
1429
+
1430
+ if (!config.account_id) {
1431
+ config.account_id = await getAccountId();
1432
+ if (!config.account_id) {
1433
+ console.error("No account id found, quitting...");
1434
+ return;
1435
+ }
1436
+ }
1437
+ }
1438
+
1439
+ // -- snip, end --
1440
+
1441
+ await putKeyValue(config.account_id, namespaceId, key, value, {
1442
+ expiration,
1443
+ expiration_ttl: ttl,
1444
+ });
1445
+ }
1446
+ )
1447
+ .command(
1448
+ "list",
1449
+ "Outputs a list of all keys in a given namespace.",
1450
+ (yargs) => {
1451
+ return yargs
1452
+ .option("binding", {
1453
+ type: "string",
1454
+ describe: "The name of the namespace to list",
1455
+ })
1456
+ .option("namespace-id", {
1457
+ type: "string",
1458
+ describe: "The id of the namespace to list",
1459
+ })
1460
+ .check(demandOneOfOption("binding", "namespace-id"))
1461
+ .option("env", {
1462
+ type: "string",
1463
+ describe: "Perform on a specific environment",
1464
+ })
1465
+ .option("prefix", {
1466
+ type: "string",
1467
+ describe: "A prefix to filter listed keys",
1468
+ });
1469
+ },
1470
+ async ({ prefix, ...args }) => {
1471
+ // TODO: support for limit+cursor (pagination)
1472
+
1473
+ const namespaceId = getNamespaceId(args);
1474
+ const config = args.config as Config;
1475
+
1476
+ if (args.local) {
1477
+ const { Miniflare } = await import("miniflare");
1478
+ const mf = new Miniflare({
1479
+ kvPersist: (args.kvPersist as string) || true,
1480
+ // TODO: these options shouldn't be required
1481
+ script: ` `, // has to be a string with at least one char
1482
+ });
1483
+ const ns = await mf.getKVNamespace(namespaceId);
1484
+ const listResponse = await ns.list({ prefix });
1485
+ console.log(JSON.stringify(listResponse.keys, null, " ")); // TODO: paginate, collate
1486
+ return;
1487
+ }
1488
+
1489
+ // -- snip, extract --
1490
+
1491
+ if (!args.local) {
1492
+ const loggedIn = await loginOrRefreshIfRequired();
1493
+ if (!loggedIn) {
1494
+ // didn't login, let's just quit
1495
+ console.log("Did not login, quitting...");
1496
+ return;
1497
+ }
1498
+
1499
+ if (!config.account_id) {
1500
+ config.account_id = await getAccountId();
1501
+ if (!config.account_id) {
1502
+ console.error("No account id found, quitting...");
1503
+ return;
1504
+ }
1505
+ }
1506
+ }
1507
+
1508
+ // -- snip, end --
1509
+
1510
+ console.log(
1511
+ await listNamespaceKeys(config.account_id, namespaceId, prefix)
1512
+ );
1513
+ }
1514
+ )
1515
+ .command(
1516
+ "get <key>",
1517
+ "Reads a single value by key from the given namespace.",
1518
+ (yargs) => {
1519
+ return yargs
1520
+ .positional("key", {
1521
+ describe: "The key value to get.",
1522
+ type: "string",
1523
+ })
1524
+ .option("binding", {
1525
+ type: "string",
1526
+ describe: "The name of the namespace to get from",
1527
+ })
1528
+ .option("namespace-id", {
1529
+ type: "string",
1530
+ describe: "The id of the namespace to get from",
1531
+ })
1532
+ .check(demandOneOfOption("binding", "namespace-id"))
1533
+ .option("env", {
1534
+ type: "string",
1535
+ describe: "Perform on a specific environment",
1536
+ })
1537
+ .option("preview", {
1538
+ type: "boolean",
1539
+ describe: "Interact with a preview namespace",
1540
+ });
1541
+ },
1542
+ async ({ key, ...args }) => {
1543
+ const namespaceId = getNamespaceId(args);
1544
+ const config = args.config as Config;
1545
+
1546
+ if (args.local) {
1547
+ const { Miniflare } = await import("miniflare");
1548
+ const mf = new Miniflare({
1549
+ kvPersist: (args.kvPersist as string) || true,
1550
+ // TODO: these options shouldn't be required
1551
+ script: ` `, // has to be a string with at least one char
1552
+ });
1553
+ const ns = await mf.getKVNamespace(namespaceId);
1554
+ console.log(await ns.get(key));
1555
+ return;
1556
+ }
1557
+
1558
+ // -- snip, extract --
1559
+
1560
+ if (!args.local) {
1561
+ const loggedIn = await loginOrRefreshIfRequired();
1562
+ if (!loggedIn) {
1563
+ // didn't login, let's just quit
1564
+ console.log("Did not login, quitting...");
1565
+ return;
1566
+ }
1567
+
1568
+ if (!config.account_id) {
1569
+ config.account_id = await getAccountId();
1570
+ if (!config.account_id) {
1571
+ console.error("No account id found, quitting...");
1572
+ return;
1573
+ }
1574
+ }
1575
+ }
1576
+
1577
+ // -- snip, end --
1578
+
1579
+ // annoyingly, the API for this one doesn't return the
1580
+ // data in the 'standard' format. goddammit.
1581
+ // That's why we have the fallthrough response in cfetch.
1582
+ // Oh well.
1583
+ console.log(
1584
+ await cfetch(
1585
+ `/accounts/${config.account_id}/storage/kv/namespaces/${namespaceId}/values/${key}`
1586
+ )
1587
+ );
1588
+ }
1589
+ )
1590
+ .command(
1591
+ "delete <key>",
1592
+ "Removes a single key value pair from the given namespace.",
1593
+ (yargs) => {
1594
+ return yargs
1595
+ .positional("key", {
1596
+ describe: "The key value to delete",
1597
+ type: "string",
1598
+ })
1599
+ .option("binding", {
1600
+ type: "string",
1601
+ describe: "The name of the namespace to delete from",
1602
+ })
1603
+ .option("namespace-id", {
1604
+ type: "string",
1605
+ describe: "The id of the namespace to delete from",
1606
+ })
1607
+ .check(demandOneOfOption("binding", "namespace-id"))
1608
+ .option("env", {
1609
+ type: "string",
1610
+ describe: "Perform on a specific environment",
1611
+ })
1612
+ .option("preview", {
1613
+ type: "boolean",
1614
+ describe: "Interact with a preview namespace",
1615
+ });
1616
+ },
1617
+ async ({ key, ...args }) => {
1618
+ const namespaceId = getNamespaceId(args);
1619
+
1620
+ console.log(
1621
+ `deleting the key "${key}" on namespace ${namespaceId}`
1622
+ );
1623
+
1624
+ if (args.local) {
1625
+ const { Miniflare } = await import("miniflare");
1626
+ const mf = new Miniflare({
1627
+ kvPersist: (args.kvPersist as string) || true,
1628
+ // TODO: these options shouldn't be required
1629
+ script: ` `, // has to be a string with at least one char
1630
+ });
1631
+ const ns = await mf.getKVNamespace(namespaceId);
1632
+ console.log(await ns.delete(key));
1633
+ return;
1634
+ }
1635
+
1636
+ const config = args.config as Config;
1637
+
1638
+ // -- snip, extract --
1639
+
1640
+ if (!args.local) {
1641
+ const loggedIn = await loginOrRefreshIfRequired();
1642
+ if (!loggedIn) {
1643
+ // didn't login, let's just quit
1644
+ console.log("Did not login, quitting...");
1645
+ return;
1646
+ }
1647
+
1648
+ if (!config.account_id) {
1649
+ config.account_id = await getAccountId();
1650
+ if (!config.account_id) {
1651
+ console.error("No account id found, quitting...");
1652
+ return;
1653
+ }
1654
+ }
1655
+ }
1656
+
1657
+ // -- snip, end --
1658
+
1659
+ await cfetch(
1660
+ `/accounts/${config.account_id}/storage/kv/namespaces/${namespaceId}/values/${key}`,
1661
+ { method: "DELETE" }
1662
+ );
1663
+ }
1664
+ );
1665
+ }
1666
+ );
1667
+
1668
+ // :bulk
1669
+ yargs.command(
1670
+ "kv:bulk",
1671
+ "💪 Interact with multiple Workers KV key-value pairs at once",
1672
+ (yargs) => {
1673
+ return yargs
1674
+ .command(
1675
+ "put <filename>",
1676
+ "Upload multiple key-value pairs to a namespace",
1677
+ (yargs) => {
1678
+ return yargs
1679
+ .positional("filename", {
1680
+ describe: `The JSON file of key-value pairs to upload, in form [{"key":..., "value":...}"...]`,
1681
+ type: "string",
1682
+ })
1683
+ .option("binding", {
1684
+ type: "string",
1685
+ describe: "The name of the namespace to put to",
1686
+ })
1687
+ .option("namespace-id", {
1688
+ type: "string",
1689
+ describe: "The id of the namespace to put to",
1690
+ })
1691
+ .check(demandOneOfOption("binding", "namespace-id"))
1692
+ .option("env", {
1693
+ type: "string",
1694
+ describe: "Perform on a specific environment",
1695
+ })
1696
+ .option("preview", {
1697
+ type: "boolean",
1698
+ describe: "Interact with a preview namespace",
1699
+ });
1700
+ },
1701
+ async ({ filename, ...args }) => {
1702
+ // The simplest implementation I could think of.
1703
+ // This could be made more efficient with a streaming parser/uploader
1704
+ // but we'll do that in the future if needed.
1705
+
1706
+ const namespaceId = getNamespaceId(args);
1707
+ const config = args.config as Config;
1708
+ const content = await readFile(filename, "utf-8");
1709
+ let parsedContent;
1710
+ try {
1711
+ parsedContent = JSON.parse(content);
1712
+ } catch (err) {
1713
+ console.error(`could not parse json from ${filename}`);
1714
+ throw err;
1715
+ }
1716
+
1717
+ if (args.local) {
1718
+ const { Miniflare } = await import("miniflare");
1719
+ const mf = new Miniflare({
1720
+ kvPersist: (args.kvPersist as string) || true,
1721
+ // TODO: these options shouldn't be required
1722
+ script: ` `, // has to be a string with at least one char
1723
+ });
1724
+ const ns = await mf.getKVNamespace(namespaceId);
1725
+ for (const {
1726
+ key,
1727
+ value,
1728
+ expiration,
1729
+ expiration_ttl,
1730
+ } of parsedContent) {
1731
+ await ns.put(key, value, {
1732
+ expiration,
1733
+ expirationTtl: expiration_ttl,
1734
+ });
1735
+ }
1736
+
1737
+ return;
1738
+ }
1739
+
1740
+ // -- snip, extract --
1741
+
1742
+ if (!args.local) {
1743
+ const loggedIn = await loginOrRefreshIfRequired();
1744
+ if (!loggedIn) {
1745
+ // didn't login, let's just quit
1746
+ console.log("Did not login, quitting...");
1747
+ return;
1748
+ }
1749
+
1750
+ if (!config.account_id) {
1751
+ config.account_id = await getAccountId();
1752
+ if (!config.account_id) {
1753
+ console.error("No account id found, quitting...");
1754
+ return;
1755
+ }
1756
+ }
1757
+ }
1758
+
1759
+ // -- snip, end --
1760
+
1761
+ console.log(
1762
+ await putBulkKeyValue(config.account_id, namespaceId, content)
1763
+ );
1764
+ }
1765
+ )
1766
+ .command(
1767
+ "delete <filename>",
1768
+ "Upload multiple key-value pairs to a namespace",
1769
+ (yargs) => {
1770
+ return yargs
1771
+ .positional("filename", {
1772
+ describe: `The JSON file of key-value pairs to upload, in form ["key1", "key2", ...]`,
1773
+ type: "string",
1774
+ })
1775
+ .option("binding", {
1776
+ type: "string",
1777
+ describe: "The name of the namespace to delete from",
1778
+ })
1779
+ .option("namespace-id", {
1780
+ type: "string",
1781
+ describe: "The id of the namespace to delete from",
1782
+ })
1783
+ .check(demandOneOfOption("binding", "namespace-id"))
1784
+ .option("env", {
1785
+ type: "string",
1786
+ describe: "Perform on a specific environment",
1787
+ })
1788
+ .option("preview", {
1789
+ type: "boolean",
1790
+ describe: "Interact with a preview namespace",
1791
+ });
1792
+ },
1793
+ async ({ filename, ...args }) => {
1794
+ const namespaceId = getNamespaceId(args);
1795
+ const config = args.config as Config;
1796
+ const content = await readFile(filename, "utf-8");
1797
+ let parsedContent;
1798
+ try {
1799
+ parsedContent = JSON.parse(content);
1800
+ } catch (err) {
1801
+ console.error(`could not parse json from ${filename}`);
1802
+ throw err;
1803
+ }
1804
+
1805
+ if (args.local) {
1806
+ const { Miniflare } = await import("miniflare");
1807
+ const mf = new Miniflare({
1808
+ kvPersist: (args.kvPersist as string) || true,
1809
+ // TODO: these options shouldn't be required
1810
+ script: ` `, // has to be a string with at least one char
1811
+ });
1812
+ const ns = await mf.getKVNamespace(namespaceId);
1813
+ for (const key of parsedContent) {
1814
+ await ns.delete(key);
1815
+ }
1816
+
1817
+ return;
1818
+ }
1819
+
1820
+ // -- snip, extract --
1821
+
1822
+ if (!args.local) {
1823
+ const loggedIn = await loginOrRefreshIfRequired();
1824
+ if (!loggedIn) {
1825
+ // didn't login, let's just quit
1826
+ console.log("Did not login, quitting...");
1827
+ return;
1828
+ }
1829
+
1830
+ if (!config.account_id) {
1831
+ config.account_id = await getAccountId();
1832
+ if (!config.account_id) {
1833
+ console.error("No account id found, quitting...");
1834
+ return;
1835
+ }
1836
+ }
1837
+ }
1838
+
1839
+ // -- snip, end --
1840
+
1841
+ console.log(
1842
+ await deleteBulkKeyValue(config.account_id, namespaceId, content)
1843
+ );
1844
+ }
1845
+ );
1846
+ }
1847
+ );
1848
+
1849
+ yargs.command("pages", "⚡️ Configure Cloudflare Pages", pages);
1850
+
1851
+ yargs
1852
+ .option("config", {
1853
+ alias: "c",
1854
+ describe: "Path to .toml configuration file",
1855
+ type: "string",
1856
+ async coerce(arg) {
1857
+ return await readConfig(arg);
1858
+ },
1859
+ })
1860
+ .option("local", {
1861
+ alias: "l",
1862
+ describe: "Run on my machine",
1863
+ type: "boolean",
1864
+ default: false, // I bet this will a point of contention. We'll revisit it.
1865
+ });
1866
+
1867
+ yargs.group(["config", "help", "version"], "Flags:");
1868
+ yargs.help().alias("h", "help");
1869
+ yargs.version(wranglerVersion).alias("v", "version");
1870
+ yargs.exitProcess(false);
1871
+
1872
+ await initialiseUserConfig();
1873
+
1874
+ await yargs.parse(argv);
1875
+ }