wrangler 0.0.0-e151223 → 0.0.0-e1b8ac4

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 (156) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +49 -5
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +4886 -0
  6. package/package.json +57 -28
  7. package/pages/functions/buildPlugin.ts +105 -0
  8. package/pages/functions/buildWorker.ts +93 -4
  9. package/pages/functions/filepath-routing.test.ts +223 -36
  10. package/pages/functions/filepath-routing.ts +78 -120
  11. package/pages/functions/routes.ts +13 -17
  12. package/pages/functions/template-plugin.ts +147 -0
  13. package/pages/functions/template-worker.ts +24 -17
  14. package/src/__tests__/config-cache.test.ts +36 -0
  15. package/src/__tests__/configuration.test.ts +3548 -0
  16. package/src/__tests__/dev.test.tsx +1049 -65
  17. package/src/__tests__/guess-worker-format.test.ts +85 -0
  18. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  19. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  20. package/src/__tests__/helpers/mock-account-id.ts +26 -8
  21. package/src/__tests__/helpers/mock-bin.ts +36 -0
  22. package/src/__tests__/helpers/mock-cfetch.ts +48 -10
  23. package/src/__tests__/helpers/mock-console.ts +39 -8
  24. package/src/__tests__/helpers/mock-dialogs.ts +99 -11
  25. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  26. package/src/__tests__/helpers/mock-istty.ts +27 -0
  27. package/src/__tests__/helpers/mock-kv.ts +9 -16
  28. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  29. package/src/__tests__/helpers/mock-process.ts +24 -0
  30. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  31. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  32. package/src/__tests__/helpers/run-in-tmp.ts +10 -0
  33. package/src/__tests__/helpers/run-wrangler.ts +5 -3
  34. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  35. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  36. package/src/__tests__/https-options.test.ts +130 -0
  37. package/src/__tests__/index.test.ts +160 -309
  38. package/src/__tests__/init.test.ts +1992 -0
  39. package/src/__tests__/jest.setup.ts +79 -6
  40. package/src/__tests__/kv.test.ts +651 -280
  41. package/src/__tests__/logger.test.ts +141 -0
  42. package/src/__tests__/package-manager.test.ts +353 -0
  43. package/src/__tests__/pages.test.ts +754 -0
  44. package/src/__tests__/parse.test.ts +330 -0
  45. package/src/__tests__/publish.test.ts +5671 -959
  46. package/src/__tests__/r2.test.ts +233 -0
  47. package/src/__tests__/route.test.ts +45 -0
  48. package/src/__tests__/secret.test.ts +362 -91
  49. package/src/__tests__/tail.test.ts +690 -0
  50. package/src/__tests__/user.test.ts +132 -0
  51. package/src/__tests__/whoami.test.tsx +60 -7
  52. package/src/abort.d.ts +3 -0
  53. package/src/bundle.ts +204 -0
  54. package/src/cfetch/index.ts +28 -18
  55. package/src/cfetch/internal.ts +99 -29
  56. package/src/cli.ts +6 -2
  57. package/src/config/README.md +107 -0
  58. package/src/config/config.ts +243 -0
  59. package/src/config/diagnostics.ts +80 -0
  60. package/src/config/environment.ts +420 -0
  61. package/src/config/index.ts +225 -0
  62. package/src/config/validation-helpers.ts +584 -0
  63. package/src/config/validation.ts +1580 -0
  64. package/src/config-cache.ts +45 -0
  65. package/src/create-worker-preview.ts +185 -0
  66. package/src/create-worker-upload-form.ts +254 -0
  67. package/src/dev/dev-vars.ts +36 -0
  68. package/src/dev/dev.tsx +399 -0
  69. package/src/dev/local.tsx +321 -0
  70. package/src/dev/remote.tsx +242 -0
  71. package/src/dev/use-esbuild.ts +112 -0
  72. package/src/dialogs.tsx +64 -6
  73. package/src/durable.ts +102 -0
  74. package/src/entry.ts +322 -0
  75. package/src/environment-variables.ts +35 -0
  76. package/src/errors.ts +11 -0
  77. package/src/generate-auth-url.ts +33 -0
  78. package/src/generate-random-state.ts +16 -0
  79. package/src/https-options.ts +123 -0
  80. package/src/index.tsx +1835 -988
  81. package/src/inspect.ts +80 -14
  82. package/src/intl-polyfill.d.ts +139 -0
  83. package/src/{kv.tsx → kv.ts} +199 -67
  84. package/src/logger.ts +73 -0
  85. package/src/miniflare-cli/README.md +30 -0
  86. package/src/miniflare-cli/enum-keys.ts +17 -0
  87. package/src/miniflare-cli/index.ts +41 -0
  88. package/src/miniflare-cli/request-context.ts +40 -0
  89. package/src/module-collection.ts +226 -31
  90. package/src/open-in-browser.ts +17 -0
  91. package/src/package-manager.ts +174 -0
  92. package/src/pages.tsx +1132 -84
  93. package/src/parse.ts +222 -0
  94. package/src/proxy.ts +176 -70
  95. package/src/publish.ts +624 -308
  96. package/src/r2.ts +50 -0
  97. package/src/selfsigned.d.ts +29 -0
  98. package/src/sites.tsx +185 -57
  99. package/src/tail/filters.ts +279 -0
  100. package/src/tail/index.ts +299 -0
  101. package/src/tail/printing.ts +67 -0
  102. package/src/update-check.ts +19 -0
  103. package/src/user.tsx +349 -166
  104. package/src/whoami.tsx +5 -3
  105. package/src/{api/worker.ts → worker.ts} +65 -39
  106. package/src/zones.ts +73 -0
  107. package/templates/gitignore +171 -0
  108. package/templates/new-worker-scheduled.js +17 -0
  109. package/templates/new-worker-scheduled.ts +32 -0
  110. package/templates/new-worker.js +15 -0
  111. package/templates/new-worker.ts +30 -0
  112. package/{static-asset-facade.js → templates/static-asset-facade.js} +3 -7
  113. package/templates/tsconfig.json +105 -0
  114. package/wrangler-dist/cli.js +76649 -75066
  115. package/src/__tests__/helpers/mock-user.ts +0 -27
  116. package/src/__tests__/logout.test.ts +0 -50
  117. package/src/api/form_data.ts +0 -134
  118. package/src/api/preview.ts +0 -128
  119. package/src/config.ts +0 -641
  120. package/src/dev.tsx +0 -834
  121. package/src/npm-installer.ts +0 -20
  122. package/src/tail.tsx +0 -73
  123. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  124. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  125. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  126. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  127. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  128. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  129. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  130. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  131. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  132. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  133. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  134. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  135. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  136. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  137. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  138. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  139. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  140. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  141. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  142. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  143. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  144. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  145. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  146. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  147. package/vendor/wrangler-mime/LICENSE +0 -21
  148. package/vendor/wrangler-mime/Mime.js +0 -97
  149. package/vendor/wrangler-mime/README.md +0 -187
  150. package/vendor/wrangler-mime/cli.js +0 -46
  151. package/vendor/wrangler-mime/index.js +0 -4
  152. package/vendor/wrangler-mime/lite.js +0 -4
  153. package/vendor/wrangler-mime/package.json +0 -52
  154. package/vendor/wrangler-mime/types/other.js +0 -1
  155. package/vendor/wrangler-mime/types/standard.js +0 -1
  156. package/wrangler-dist/cli.js.map +0 -7
@@ -1,24 +1,30 @@
1
1
  import { writeFileSync } from "node:fs";
2
+ import { Headers } from "undici";
2
3
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
3
4
  import {
4
5
  setMockResponse,
5
- setMockRawResponse,
6
6
  unsetAllMocks,
7
+ unsetMockFetchKVGetValues,
8
+ setMockFetchKVGetValue,
7
9
  } from "./helpers/mock-cfetch";
8
10
  import { mockConsoleMethods } from "./helpers/mock-console";
11
+ import { clearConfirmMocks, mockConfirm } from "./helpers/mock-dialogs";
9
12
  import { mockKeyListRequest } from "./helpers/mock-kv";
13
+ import { mockProcess } from "./helpers/mock-process";
10
14
  import { runInTempDir } from "./helpers/run-in-tmp";
11
15
  import { runWrangler } from "./helpers/run-wrangler";
12
- import type { KVNamespaceInfo } from "../kv";
16
+ import type { KeyValue, KVNamespaceInfo, NamespaceKeyInfo } from "../kv";
13
17
 
14
18
  describe("wrangler", () => {
15
19
  mockAccountId();
16
20
  mockApiToken();
17
21
  runInTempDir();
18
22
  const std = mockConsoleMethods();
23
+ const proc = mockProcess();
19
24
 
20
25
  afterEach(() => {
21
26
  unsetAllMocks();
27
+ clearConfirmMocks();
22
28
  });
23
29
 
24
30
  describe("kv:namespace", () => {
@@ -42,9 +48,9 @@ describe("wrangler", () => {
42
48
  ).rejects.toThrowErrorMatchingInlineSnapshot(
43
49
  `"Not enough non-option arguments: got 0, need at least 1"`
44
50
  );
45
- expect(std.out).toMatchInlineSnapshot(`""`);
46
- expect(std.err).toMatchInlineSnapshot(`
47
- "wrangler kv:namespace create <namespace>
51
+ expect(std.out).toMatchInlineSnapshot(`
52
+ "
53
+ wrangler kv:namespace create <namespace>
48
54
 
49
55
  Create a new namespace
50
56
 
@@ -57,11 +63,13 @@ describe("wrangler", () => {
57
63
  -v, --version Show version number [boolean]
58
64
 
59
65
  Options:
60
- -l, --local Run on my machine [boolean] [default: false]
61
- --env Perform on a specific environment [string]
62
- --preview Interact with a preview namespace [boolean]
66
+ -e, --env Perform on a specific environment [string]
67
+ --preview Interact with a preview namespace [boolean]"
68
+ `);
69
+ expect(std.err).toMatchInlineSnapshot(`
70
+ "X [ERROR] Not enough non-option arguments: got 0, need at least 1
63
71
 
64
- Not enough non-option arguments: got 0, need at least 1"
72
+ "
65
73
  `);
66
74
  });
67
75
 
@@ -69,11 +77,11 @@ describe("wrangler", () => {
69
77
  await expect(
70
78
  runWrangler("kv:namespace create abc def ghi")
71
79
  ).rejects.toThrowErrorMatchingInlineSnapshot(
72
- `"Unexpected additional positional arguments \\"def ghi\\"."`
80
+ `"Unknown arguments: def, ghi"`
73
81
  );
74
- expect(std.out).toMatchInlineSnapshot(`""`);
75
- expect(std.err).toMatchInlineSnapshot(`
76
- "wrangler kv:namespace create <namespace>
82
+ expect(std.out).toMatchInlineSnapshot(`
83
+ "
84
+ wrangler kv:namespace create <namespace>
77
85
 
78
86
  Create a new namespace
79
87
 
@@ -86,11 +94,13 @@ describe("wrangler", () => {
86
94
  -v, --version Show version number [boolean]
87
95
 
88
96
  Options:
89
- -l, --local Run on my machine [boolean] [default: false]
90
- --env Perform on a specific environment [string]
91
- --preview Interact with a preview namespace [boolean]
97
+ -e, --env Perform on a specific environment [string]
98
+ --preview Interact with a preview namespace [boolean]"
99
+ `);
100
+ expect(std.err).toMatchInlineSnapshot(`
101
+ "X [ERROR] Unknown arguments: def, ghi
92
102
 
93
- Unexpected additional positional arguments \\"def ghi\\"."
103
+ "
94
104
  `);
95
105
  });
96
106
 
@@ -101,9 +111,9 @@ describe("wrangler", () => {
101
111
  `"The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."`
102
112
  );
103
113
 
104
- expect(std.out).toMatchInlineSnapshot(`""`);
105
- expect(std.err).toMatchInlineSnapshot(`
106
- "wrangler kv:namespace create <namespace>
114
+ expect(std.out).toMatchInlineSnapshot(`
115
+ "
116
+ wrangler kv:namespace create <namespace>
107
117
 
108
118
  Create a new namespace
109
119
 
@@ -116,11 +126,13 @@ describe("wrangler", () => {
116
126
  -v, --version Show version number [boolean]
117
127
 
118
128
  Options:
119
- -l, --local Run on my machine [boolean] [default: false]
120
- --env Perform on a specific environment [string]
121
- --preview Interact with a preview namespace [boolean]
129
+ -e, --env Perform on a specific environment [string]
130
+ --preview Interact with a preview namespace [boolean]"
131
+ `);
132
+ expect(std.err).toMatchInlineSnapshot(`
133
+ "X [ERROR] The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.
122
134
 
123
- The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."
135
+ "
124
136
  `);
125
137
  });
126
138
 
@@ -147,11 +159,11 @@ describe("wrangler", () => {
147
159
  });
148
160
 
149
161
  it("should create a namespace using configured worker name", async () => {
150
- writeFileSync("./wrangler.toml", 'name = "otherWorker"', "utf-8");
151
- mockCreateRequest("otherWorker-UnitTestNamespace");
162
+ writeFileSync("./wrangler.toml", 'name = "other-worker"', "utf-8");
163
+ mockCreateRequest("other-worker-UnitTestNamespace");
152
164
  await runWrangler("kv:namespace create UnitTestNamespace");
153
165
  expect(std.out).toMatchInlineSnapshot(`
154
- "🌀 Creating namespace with title \\"otherWorker-UnitTestNamespace\\"
166
+ "🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
155
167
  ✨ Success!
156
168
  Add the following to your configuration file in your kv_namespaces array:
157
169
  { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
@@ -270,24 +282,11 @@ describe("wrangler", () => {
270
282
  A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
271
283
  `);
272
284
  expect(std.err).toMatchInlineSnapshot(`
273
- "wrangler kv:namespace delete
285
+ "X [ERROR] Not able to delete namespace.
274
286
 
275
- Deletes a given namespace.
287
+ A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
276
288
 
277
- Flags:
278
- -c, --config Path to .toml configuration file [string]
279
- -h, --help Show help [boolean]
280
- -v, --version Show version number [boolean]
281
-
282
- Options:
283
- -l, --local Run on my machine [boolean] [default: false]
284
- --binding The name of the namespace to delete [string]
285
- --namespace-id The id of the namespace to delete [string]
286
- --env Perform on a specific environment [string]
287
- --preview Interact with a preview namespace [boolean]
288
-
289
- Not able to delete namespace.
290
- A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
289
+ "
291
290
  `);
292
291
  });
293
292
 
@@ -318,10 +317,7 @@ describe("wrangler", () => {
318
317
  describe("put", () => {
319
318
  function mockKeyPutRequest(
320
319
  expectedNamespaceId: string,
321
- expectedKey: string,
322
- expectedValue: string,
323
- expiration?: number,
324
- expirationTtl?: number
320
+ expectedKV: KeyValue
325
321
  ) {
326
322
  const requests = { count: 0 };
327
323
  setMockResponse(
@@ -331,15 +327,19 @@ describe("wrangler", () => {
331
327
  requests.count++;
332
328
  expect(accountId).toEqual("some-account-id");
333
329
  expect(namespaceId).toEqual(expectedNamespaceId);
334
- expect(key).toEqual(expectedKey);
335
- expect(body).toEqual(expectedValue);
336
- if (expiration !== undefined) {
337
- expect(query.get("expiration")).toEqual(`${expiration}`);
330
+ expect(key).toEqual(expectedKV.key);
331
+ expect(body).toEqual(expectedKV.value);
332
+ if (expectedKV.expiration !== undefined) {
333
+ expect(query.get("expiration")).toEqual(
334
+ `${expectedKV.expiration}`
335
+ );
338
336
  } else {
339
337
  expect(query.has("expiration")).toBe(false);
340
338
  }
341
- if (expirationTtl) {
342
- expect(query.get("expiration_ttl")).toEqual(`${expirationTtl}`);
339
+ if (expectedKV.expiration_ttl) {
340
+ expect(query.get("expiration_ttl")).toEqual(
341
+ `${expectedKV.expiration_ttl}`
342
+ );
343
343
  } else {
344
344
  expect(query.has("expiration_ttl")).toBe(false);
345
345
  }
@@ -350,11 +350,10 @@ describe("wrangler", () => {
350
350
  }
351
351
 
352
352
  it("should put a key in a given namespace specified by namespace-id", async () => {
353
- const requests = mockKeyPutRequest(
354
- "some-namespace-id",
355
- "my-key",
356
- "my-value"
357
- );
353
+ const requests = mockKeyPutRequest("some-namespace-id", {
354
+ key: "my-key",
355
+ value: "my-value",
356
+ });
358
357
 
359
358
  await runWrangler(
360
359
  "kv:key put my-key my-value --namespace-id some-namespace-id"
@@ -362,20 +361,38 @@ describe("wrangler", () => {
362
361
 
363
362
  expect(requests.count).toEqual(1);
364
363
  expect(std.out).toMatchInlineSnapshot(
365
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
364
+ `"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id."`
365
+ );
366
+ expect(std.err).toMatchInlineSnapshot(`""`);
367
+ });
368
+
369
+ it("should encode the key in the api request to put a value", async () => {
370
+ const requests = mockKeyPutRequest("DS9", {
371
+ key: "%2Fmy-key",
372
+ value: "my-value",
373
+ });
374
+
375
+ await runWrangler("kv:key put /my-key my-value --namespace-id DS9");
376
+
377
+ expect(requests.count).toEqual(1);
378
+ expect(std.out).toMatchInlineSnapshot(
379
+ `"Writing the value \\"my-value\\" to key \\"/my-key\\" on namespace DS9."`
366
380
  );
367
381
  expect(std.err).toMatchInlineSnapshot(`""`);
368
382
  });
369
383
 
370
384
  it("should put a key in a given namespace specified by binding", async () => {
371
385
  writeWranglerConfig();
372
- const requests = mockKeyPutRequest("bound-id", "my-key", "my-value");
386
+ const requests = mockKeyPutRequest("bound-id", {
387
+ key: "my-key",
388
+ value: "my-value",
389
+ });
373
390
  await runWrangler(
374
391
  "kv:key put my-key my-value --binding someBinding --preview false"
375
392
  );
376
393
 
377
394
  expect(std.out).toMatchInlineSnapshot(
378
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace bound-id"`
395
+ `"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace bound-id."`
379
396
  );
380
397
  expect(std.err).toMatchInlineSnapshot(`""`);
381
398
  expect(requests.count).toEqual(1);
@@ -383,70 +400,87 @@ describe("wrangler", () => {
383
400
 
384
401
  it("should put a key in a given preview namespace specified by binding", async () => {
385
402
  writeWranglerConfig();
386
- const requests = mockKeyPutRequest(
387
- "preview-bound-id",
388
- "my-key",
389
- "my-value"
390
- );
403
+ const requests = mockKeyPutRequest("preview-bound-id", {
404
+ key: "my-key",
405
+ value: "my-value",
406
+ });
391
407
 
392
408
  await runWrangler(
393
409
  "kv:key put my-key my-value --binding someBinding --preview"
394
410
  );
395
411
 
396
412
  expect(std.out).toMatchInlineSnapshot(
397
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace preview-bound-id"`
413
+ `"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace preview-bound-id."`
398
414
  );
399
415
  expect(std.err).toMatchInlineSnapshot(`""`);
400
416
  expect(requests.count).toEqual(1);
401
417
  });
402
418
 
403
419
  it("should add expiration and ttl properties when putting a key", async () => {
404
- const requests = mockKeyPutRequest(
405
- "some-namespace-id",
406
- "my-key",
407
- "my-value",
408
- 10,
409
- 20
410
- );
420
+ const requests = mockKeyPutRequest("some-namespace-id", {
421
+ key: "my-key",
422
+ value: "my-value",
423
+ expiration: 10,
424
+ expiration_ttl: 20,
425
+ });
411
426
  await runWrangler(
412
427
  "kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
413
428
  );
414
429
  expect(requests.count).toEqual(1);
415
430
  expect(std.out).toMatchInlineSnapshot(
416
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
431
+ `"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id."`
417
432
  );
418
433
  expect(std.err).toMatchInlineSnapshot(`""`);
419
434
  });
420
435
 
421
436
  it("should put a key to the specified environment in a given namespace", async () => {
422
437
  writeWranglerConfig();
423
- const requests = mockKeyPutRequest(
424
- "env-bound-id",
425
- "my-key",
426
- "my-value"
427
- );
438
+ const requests = mockKeyPutRequest("env-bound-id", {
439
+ key: "my-key",
440
+ value: "my-value",
441
+ });
428
442
  await runWrangler(
429
443
  "kv:key put my-key my-value --binding someBinding --env some-environment --preview false"
430
444
  );
431
445
  expect(std.out).toMatchInlineSnapshot(
432
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace env-bound-id"`
446
+ `"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace env-bound-id."`
433
447
  );
434
448
  expect(std.err).toMatchInlineSnapshot(`""`);
435
449
  expect(requests.count).toEqual(1);
436
450
  });
437
451
 
438
452
  it("should put a key with a value loaded from a given path", async () => {
439
- writeFileSync("foo.txt", "file-contents", "utf-8");
440
- const requests = mockKeyPutRequest(
441
- "some-namespace-id",
442
- "my-key",
443
- "file-contents"
444
- );
453
+ const buf = Buffer.from("file-contents", "utf-8");
454
+ writeFileSync("foo.txt", buf);
455
+ const requests = mockKeyPutRequest("some-namespace-id", {
456
+ key: "my-key",
457
+ value: buf,
458
+ });
445
459
  await runWrangler(
446
460
  "kv:key put my-key --namespace-id some-namespace-id --path foo.txt"
447
461
  );
448
462
  expect(std.out).toMatchInlineSnapshot(
449
- `"writing the contents of foo.txt to the key \\"my-key\\" on namespace some-namespace-id"`
463
+ `"Writing the contents of foo.txt to the key \\"my-key\\" on namespace some-namespace-id."`
464
+ );
465
+ expect(std.err).toMatchInlineSnapshot(`""`);
466
+ expect(requests.count).toEqual(1);
467
+ });
468
+
469
+ it("should put a key with a binary value loaded from a given path", async () => {
470
+ const buf = Buffer.from(
471
+ "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAiSURBVHgB7coxEQAACMPAgH/PgAM6dGwu49fA/deIBXrgAj2cAhIFT4QxAAAAAElFTkSuQmCC",
472
+ "base64"
473
+ );
474
+ writeFileSync("test.png", buf);
475
+ const requests = mockKeyPutRequest("another-namespace-id", {
476
+ key: "my-key",
477
+ value: buf,
478
+ });
479
+ await runWrangler(
480
+ "kv:key put my-key --namespace-id another-namespace-id --path test.png"
481
+ );
482
+ expect(std.out).toMatchInlineSnapshot(
483
+ `"Writing the contents of test.png to the key \\"my-key\\" on namespace another-namespace-id."`
450
484
  );
451
485
  expect(std.err).toMatchInlineSnapshot(`""`);
452
486
  expect(requests.count).toEqual(1);
@@ -459,15 +493,15 @@ describe("wrangler", () => {
459
493
  `"Not enough non-option arguments: got 0, need at least 1"`
460
494
  );
461
495
 
462
- expect(std.out).toMatchInlineSnapshot(`""`);
463
- expect(std.err).toMatchInlineSnapshot(`
464
- "wrangler kv:key put <key> [value]
496
+ expect(std.out).toMatchInlineSnapshot(`
497
+ "
498
+ wrangler kv:key put <key> [value]
465
499
 
466
500
  Writes a single key/value pair to the given namespace.
467
501
 
468
502
  Positionals:
469
- key The key to write to. [string] [required]
470
- value The value to write. [string]
503
+ key The key to write to [string] [required]
504
+ value The value to write [string]
471
505
 
472
506
  Flags:
473
507
  -c, --config Path to .toml configuration file [string]
@@ -475,16 +509,18 @@ describe("wrangler", () => {
475
509
  -v, --version Show version number [boolean]
476
510
 
477
511
  Options:
478
- -l, --local Run on my machine [boolean] [default: false]
479
- --binding The binding of the namespace to write to. [string]
480
- --namespace-id The id of the namespace to write to. [string]
481
- --env Perform on a specific environment [string]
512
+ --binding The binding of the namespace to write to [string]
513
+ --namespace-id The id of the namespace to write to [string]
514
+ -e, --env Perform on a specific environment [string]
482
515
  --preview Interact with a preview namespace [boolean]
483
- --ttl Time for which the entries should be visible. [number]
516
+ --ttl Time for which the entries should be visible [number]
484
517
  --expiration Time since the UNIX epoch after which the entry expires [number]
485
- --path Read value from the file at a given path. [string]
518
+ --path Read value from the file at a given path [string]"
519
+ `);
520
+ expect(std.err).toMatchInlineSnapshot(`
521
+ "X [ERROR] Not enough non-option arguments: got 0, need at least 1
486
522
 
487
- Not enough non-option arguments: got 0, need at least 1"
523
+ "
488
524
  `);
489
525
  });
490
526
 
@@ -495,15 +531,15 @@ describe("wrangler", () => {
495
531
  `"Exactly one of the arguments binding and namespace-id is required"`
496
532
  );
497
533
 
498
- expect(std.out).toMatchInlineSnapshot(`""`);
499
- expect(std.err).toMatchInlineSnapshot(`
500
- "wrangler kv:key put <key> [value]
534
+ expect(std.out).toMatchInlineSnapshot(`
535
+ "
536
+ wrangler kv:key put <key> [value]
501
537
 
502
538
  Writes a single key/value pair to the given namespace.
503
539
 
504
540
  Positionals:
505
- key The key to write to. [string] [required]
506
- value The value to write. [string]
541
+ key The key to write to [string] [required]
542
+ value The value to write [string]
507
543
 
508
544
  Flags:
509
545
  -c, --config Path to .toml configuration file [string]
@@ -511,16 +547,18 @@ describe("wrangler", () => {
511
547
  -v, --version Show version number [boolean]
512
548
 
513
549
  Options:
514
- -l, --local Run on my machine [boolean] [default: false]
515
- --binding The binding of the namespace to write to. [string]
516
- --namespace-id The id of the namespace to write to. [string]
517
- --env Perform on a specific environment [string]
550
+ --binding The binding of the namespace to write to [string]
551
+ --namespace-id The id of the namespace to write to [string]
552
+ -e, --env Perform on a specific environment [string]
518
553
  --preview Interact with a preview namespace [boolean]
519
- --ttl Time for which the entries should be visible. [number]
554
+ --ttl Time for which the entries should be visible [number]
520
555
  --expiration Time since the UNIX epoch after which the entry expires [number]
521
- --path Read value from the file at a given path. [string]
556
+ --path Read value from the file at a given path [string]"
557
+ `);
558
+ expect(std.err).toMatchInlineSnapshot(`
559
+ "X [ERROR] Exactly one of the arguments binding and namespace-id is required
522
560
 
523
- Exactly one of the arguments binding and namespace-id is required"
561
+ "
524
562
  `);
525
563
  });
526
564
 
@@ -531,15 +569,15 @@ describe("wrangler", () => {
531
569
  `"Arguments binding and namespace-id are mutually exclusive"`
532
570
  );
533
571
 
534
- expect(std.out).toMatchInlineSnapshot(`""`);
535
- expect(std.err).toMatchInlineSnapshot(`
536
- "wrangler kv:key put <key> [value]
572
+ expect(std.out).toMatchInlineSnapshot(`
573
+ "
574
+ wrangler kv:key put <key> [value]
537
575
 
538
576
  Writes a single key/value pair to the given namespace.
539
577
 
540
578
  Positionals:
541
- key The key to write to. [string] [required]
542
- value The value to write. [string]
579
+ key The key to write to [string] [required]
580
+ value The value to write [string]
543
581
 
544
582
  Flags:
545
583
  -c, --config Path to .toml configuration file [string]
@@ -547,16 +585,18 @@ describe("wrangler", () => {
547
585
  -v, --version Show version number [boolean]
548
586
 
549
587
  Options:
550
- -l, --local Run on my machine [boolean] [default: false]
551
- --binding The binding of the namespace to write to. [string]
552
- --namespace-id The id of the namespace to write to. [string]
553
- --env Perform on a specific environment [string]
588
+ --binding The binding of the namespace to write to [string]
589
+ --namespace-id The id of the namespace to write to [string]
590
+ -e, --env Perform on a specific environment [string]
554
591
  --preview Interact with a preview namespace [boolean]
555
- --ttl Time for which the entries should be visible. [number]
592
+ --ttl Time for which the entries should be visible [number]
556
593
  --expiration Time since the UNIX epoch after which the entry expires [number]
557
- --path Read value from the file at a given path. [string]
594
+ --path Read value from the file at a given path [string]"
595
+ `);
596
+ expect(std.err).toMatchInlineSnapshot(`
597
+ "X [ERROR] Arguments binding and namespace-id are mutually exclusive
558
598
 
559
- Arguments binding and namespace-id are mutually exclusive"
599
+ "
560
600
  `);
561
601
  });
562
602
 
@@ -567,15 +607,15 @@ describe("wrangler", () => {
567
607
  `"Exactly one of the arguments value and path is required"`
568
608
  );
569
609
 
570
- expect(std.out).toMatchInlineSnapshot(`""`);
571
- expect(std.err).toMatchInlineSnapshot(`
572
- "wrangler kv:key put <key> [value]
610
+ expect(std.out).toMatchInlineSnapshot(`
611
+ "
612
+ wrangler kv:key put <key> [value]
573
613
 
574
614
  Writes a single key/value pair to the given namespace.
575
615
 
576
616
  Positionals:
577
- key The key to write to. [string] [required]
578
- value The value to write. [string]
617
+ key The key to write to [string] [required]
618
+ value The value to write [string]
579
619
 
580
620
  Flags:
581
621
  -c, --config Path to .toml configuration file [string]
@@ -583,16 +623,18 @@ describe("wrangler", () => {
583
623
  -v, --version Show version number [boolean]
584
624
 
585
625
  Options:
586
- -l, --local Run on my machine [boolean] [default: false]
587
- --binding The binding of the namespace to write to. [string]
588
- --namespace-id The id of the namespace to write to. [string]
589
- --env Perform on a specific environment [string]
626
+ --binding The binding of the namespace to write to [string]
627
+ --namespace-id The id of the namespace to write to [string]
628
+ -e, --env Perform on a specific environment [string]
590
629
  --preview Interact with a preview namespace [boolean]
591
- --ttl Time for which the entries should be visible. [number]
630
+ --ttl Time for which the entries should be visible [number]
592
631
  --expiration Time since the UNIX epoch after which the entry expires [number]
593
- --path Read value from the file at a given path. [string]
632
+ --path Read value from the file at a given path [string]"
633
+ `);
634
+ expect(std.err).toMatchInlineSnapshot(`
635
+ "X [ERROR] Exactly one of the arguments value and path is required
594
636
 
595
- Exactly one of the arguments value and path is required"
637
+ "
596
638
  `);
597
639
  });
598
640
 
@@ -603,15 +645,15 @@ describe("wrangler", () => {
603
645
  `"Arguments value and path are mutually exclusive"`
604
646
  );
605
647
 
606
- expect(std.out).toMatchInlineSnapshot(`""`);
607
- expect(std.err).toMatchInlineSnapshot(`
608
- "wrangler kv:key put <key> [value]
648
+ expect(std.out).toMatchInlineSnapshot(`
649
+ "
650
+ wrangler kv:key put <key> [value]
609
651
 
610
652
  Writes a single key/value pair to the given namespace.
611
653
 
612
654
  Positionals:
613
- key The key to write to. [string] [required]
614
- value The value to write. [string]
655
+ key The key to write to [string] [required]
656
+ value The value to write [string]
615
657
 
616
658
  Flags:
617
659
  -c, --config Path to .toml configuration file [string]
@@ -619,16 +661,18 @@ describe("wrangler", () => {
619
661
  -v, --version Show version number [boolean]
620
662
 
621
663
  Options:
622
- -l, --local Run on my machine [boolean] [default: false]
623
- --binding The binding of the namespace to write to. [string]
624
- --namespace-id The id of the namespace to write to. [string]
625
- --env Perform on a specific environment [string]
664
+ --binding The binding of the namespace to write to [string]
665
+ --namespace-id The id of the namespace to write to [string]
666
+ -e, --env Perform on a specific environment [string]
626
667
  --preview Interact with a preview namespace [boolean]
627
- --ttl Time for which the entries should be visible. [number]
668
+ --ttl Time for which the entries should be visible [number]
628
669
  --expiration Time since the UNIX epoch after which the entry expires [number]
629
- --path Read value from the file at a given path. [string]
670
+ --path Read value from the file at a given path [string]"
671
+ `);
672
+ expect(std.err).toMatchInlineSnapshot(`
673
+ "X [ERROR] Arguments value and path are mutually exclusive
630
674
 
631
- Arguments value and path are mutually exclusive"
675
+ "
632
676
  `);
633
677
  });
634
678
 
@@ -640,33 +684,36 @@ describe("wrangler", () => {
640
684
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
641
685
  );
642
686
 
643
- expect(std.out).toMatchInlineSnapshot(`""`);
687
+ expect(std.out).toMatchInlineSnapshot(`
688
+ "
689
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
690
+ `);
644
691
  expect(std.err).toMatchInlineSnapshot(`
645
- "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
692
+ "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
646
693
 
647
- %s
648
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
694
+ "
649
695
  `);
650
696
  });
651
697
 
652
698
  it("should error if a given binding has both preview and non-preview and --preview is not specified", async () => {
653
699
  writeWranglerConfig();
654
- const requests = mockKeyPutRequest(
655
- "preview-bound-id",
656
- "my-key",
657
- "my-value"
658
- );
700
+ const requests = mockKeyPutRequest("preview-bound-id", {
701
+ key: "my-key",
702
+ value: "my-value",
703
+ });
659
704
  await expect(
660
705
  runWrangler("kv:key put my-key my-value --binding someBinding")
661
706
  ).rejects.toThrowErrorMatchingInlineSnapshot(
662
707
  `"someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace."`
663
708
  );
664
- expect(std.out).toMatchInlineSnapshot(`""`);
709
+ expect(std.out).toMatchInlineSnapshot(`
710
+ "
711
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
712
+ `);
665
713
  expect(std.err).toMatchInlineSnapshot(`
666
- "someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.
714
+ "X [ERROR] someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.
667
715
 
668
- %s
669
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
716
+ "
670
717
  `);
671
718
  expect(requests.count).toEqual(0);
672
719
  });
@@ -674,26 +721,26 @@ describe("wrangler", () => {
674
721
 
675
722
  describe("list", () => {
676
723
  it("should list the keys of a namespace specified by namespace-id", async () => {
677
- const keys = ["key-1", "key-2", "key-3"];
724
+ const keys = [
725
+ { name: "key-1" },
726
+ { name: "key-2", expiration: 123456789 },
727
+ { name: "key-3", expiration_ttl: 666 },
728
+ ];
678
729
  mockKeyListRequest("some-namespace-id", keys);
679
730
  await runWrangler("kv:key list --namespace-id some-namespace-id");
680
731
  expect(std.err).toMatchInlineSnapshot(`""`);
681
732
  expect(std.out).toMatchInlineSnapshot(`
682
733
  "[
683
734
  {
684
- \\"name\\": \\"key-1\\",
685
- \\"expiration\\": 123456789,
686
- \\"metadata\\": {}
735
+ \\"name\\": \\"key-1\\"
687
736
  },
688
737
  {
689
738
  \\"name\\": \\"key-2\\",
690
- \\"expiration\\": 123456789,
691
- \\"metadata\\": {}
739
+ \\"expiration\\": 123456789
692
740
  },
693
741
  {
694
742
  \\"name\\": \\"key-3\\",
695
- \\"expiration\\": 123456789,
696
- \\"metadata\\": {}
743
+ \\"expiration_ttl\\": 666
697
744
  }
698
745
  ]"
699
746
  `);
@@ -701,7 +748,7 @@ describe("wrangler", () => {
701
748
 
702
749
  it("should list the keys of a namespace specified by binding", async () => {
703
750
  writeWranglerConfig();
704
- const keys = ["key-1", "key-2", "key-3"];
751
+ const keys = [{ name: "key-1" }, { name: "key-2" }, { name: "key-3" }];
705
752
  mockKeyListRequest("bound-id", keys);
706
753
 
707
754
  await runWrangler("kv:key list --binding someBinding");
@@ -709,19 +756,13 @@ describe("wrangler", () => {
709
756
  expect(std.out).toMatchInlineSnapshot(`
710
757
  "[
711
758
  {
712
- \\"name\\": \\"key-1\\",
713
- \\"expiration\\": 123456789,
714
- \\"metadata\\": {}
759
+ \\"name\\": \\"key-1\\"
715
760
  },
716
761
  {
717
- \\"name\\": \\"key-2\\",
718
- \\"expiration\\": 123456789,
719
- \\"metadata\\": {}
762
+ \\"name\\": \\"key-2\\"
720
763
  },
721
764
  {
722
- \\"name\\": \\"key-3\\",
723
- \\"expiration\\": 123456789,
724
- \\"metadata\\": {}
765
+ \\"name\\": \\"key-3\\"
725
766
  }
726
767
  ]"
727
768
  `);
@@ -729,26 +770,20 @@ describe("wrangler", () => {
729
770
 
730
771
  it("should list the keys of a preview namespace specified by binding", async () => {
731
772
  writeWranglerConfig();
732
- const keys = ["key-1", "key-2", "key-3"];
773
+ const keys = [{ name: "key-1" }, { name: "key-2" }, { name: "key-3" }];
733
774
  mockKeyListRequest("preview-bound-id", keys);
734
775
  await runWrangler("kv:key list --binding someBinding --preview");
735
776
  expect(std.err).toMatchInlineSnapshot(`""`);
736
777
  expect(std.out).toMatchInlineSnapshot(`
737
778
  "[
738
779
  {
739
- \\"name\\": \\"key-1\\",
740
- \\"expiration\\": 123456789,
741
- \\"metadata\\": {}
780
+ \\"name\\": \\"key-1\\"
742
781
  },
743
782
  {
744
- \\"name\\": \\"key-2\\",
745
- \\"expiration\\": 123456789,
746
- \\"metadata\\": {}
783
+ \\"name\\": \\"key-2\\"
747
784
  },
748
785
  {
749
- \\"name\\": \\"key-3\\",
750
- \\"expiration\\": 123456789,
751
- \\"metadata\\": {}
786
+ \\"name\\": \\"key-3\\"
752
787
  }
753
788
  ]"
754
789
  `);
@@ -756,7 +791,7 @@ describe("wrangler", () => {
756
791
 
757
792
  it("should list the keys of a namespace specified by binding, in a given environment", async () => {
758
793
  writeWranglerConfig();
759
- const keys = ["key-1", "key-2", "key-3"];
794
+ const keys = [{ name: "key-1" }, { name: "key-2" }, { name: "key-3" }];
760
795
  mockKeyListRequest("env-bound-id", keys);
761
796
  await runWrangler(
762
797
  "kv:key list --binding someBinding --env some-environment"
@@ -765,19 +800,13 @@ describe("wrangler", () => {
765
800
  expect(std.out).toMatchInlineSnapshot(`
766
801
  "[
767
802
  {
768
- \\"name\\": \\"key-1\\",
769
- \\"expiration\\": 123456789,
770
- \\"metadata\\": {}
803
+ \\"name\\": \\"key-1\\"
771
804
  },
772
805
  {
773
- \\"name\\": \\"key-2\\",
774
- \\"expiration\\": 123456789,
775
- \\"metadata\\": {}
806
+ \\"name\\": \\"key-2\\"
776
807
  },
777
808
  {
778
- \\"name\\": \\"key-3\\",
779
- \\"expiration\\": 123456789,
780
- \\"metadata\\": {}
809
+ \\"name\\": \\"key-3\\"
781
810
  }
782
811
  ]"
783
812
  `);
@@ -785,7 +814,7 @@ describe("wrangler", () => {
785
814
 
786
815
  it("should list the keys of a preview namespace specified by binding, in a given environment", async () => {
787
816
  writeWranglerConfig();
788
- const keys = ["key-1", "key-2", "key-3"];
817
+ const keys = [{ name: "key-1" }, { name: "key-2" }, { name: "key-3" }];
789
818
  mockKeyListRequest("preview-env-bound-id", keys);
790
819
  await runWrangler(
791
820
  "kv:key list --binding someBinding --preview --env some-environment"
@@ -794,19 +823,13 @@ describe("wrangler", () => {
794
823
  expect(std.out).toMatchInlineSnapshot(`
795
824
  "[
796
825
  {
797
- \\"name\\": \\"key-1\\",
798
- \\"expiration\\": 123456789,
799
- \\"metadata\\": {}
826
+ \\"name\\": \\"key-1\\"
800
827
  },
801
828
  {
802
- \\"name\\": \\"key-2\\",
803
- \\"expiration\\": 123456789,
804
- \\"metadata\\": {}
829
+ \\"name\\": \\"key-2\\"
805
830
  },
806
831
  {
807
- \\"name\\": \\"key-3\\",
808
- \\"expiration\\": 123456789,
809
- \\"metadata\\": {}
832
+ \\"name\\": \\"key-3\\"
810
833
  }
811
834
  ]"
812
835
  `);
@@ -823,9 +846,9 @@ describe("wrangler", () => {
823
846
  describe(`cursor - ${blankCursorValue}`, () => {
824
847
  it("should make multiple requests for paginated results", async () => {
825
848
  // Create a lot of mock keys, so that the fetch requests will be paginated
826
- const keys: string[] = [];
849
+ const keys: NamespaceKeyInfo[] = [];
827
850
  for (let i = 0; i < 550; i++) {
828
- keys.push("key-" + i);
851
+ keys.push({ name: "key-" + i });
829
852
  }
830
853
  // Ask for the keys in pages of size 100.
831
854
  const requests = mockKeyListRequest(
@@ -834,11 +857,9 @@ describe("wrangler", () => {
834
857
  100,
835
858
  blankCursorValue
836
859
  );
837
- await runWrangler(
838
- "kv:key list --namespace-id some-namespace-id --limit 100"
839
- );
860
+ await runWrangler("kv:key list --namespace-id some-namespace-id");
840
861
  expect(std.err).toMatchInlineSnapshot(`""`);
841
- expect(JSON.parse(std.out).map((k) => k.name)).toEqual(keys);
862
+ expect(JSON.parse(std.out)).toEqual(keys);
842
863
  expect(requests.count).toEqual(6);
843
864
  });
844
865
  });
@@ -852,84 +873,105 @@ describe("wrangler", () => {
852
873
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
853
874
  );
854
875
  expect(std.err).toMatchInlineSnapshot(`
855
- "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
876
+ "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
856
877
 
857
- %s
858
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
878
+ "
879
+ `);
880
+ expect(std.out).toMatchInlineSnapshot(`
881
+ "
882
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
859
883
  `);
860
- expect(std.out).toMatchInlineSnapshot(`""`);
861
884
  });
862
885
  });
863
886
 
864
887
  describe("get", () => {
865
- function mockKeyGetRequest(
866
- expectedNamespaceId: string,
867
- expectedKey: string,
868
- expectedValue: string
869
- ) {
870
- const requests = { count: 0 };
871
- setMockRawResponse(
872
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
873
- ([_url, accountId, namespaceId, key]) => {
874
- requests.count++;
875
- expect(accountId).toEqual("some-account-id");
876
- expect(namespaceId).toEqual(expectedNamespaceId);
877
- expect(key).toEqual(expectedKey);
878
- return expectedValue;
879
- }
880
- );
881
- return requests;
882
- }
888
+ afterEach(() => {
889
+ unsetMockFetchKVGetValues();
890
+ });
883
891
 
884
892
  it("should get a key in a given namespace specified by namespace-id", async () => {
885
- const requests = mockKeyGetRequest(
893
+ setMockFetchKVGetValue(
894
+ "some-account-id",
886
895
  "some-namespace-id",
887
896
  "my-key",
888
897
  "my-value"
889
898
  );
890
899
  await runWrangler("kv:key get my-key --namespace-id some-namespace-id");
891
- expect(std.out).toMatchInlineSnapshot(`"my-value"`);
900
+ expect(proc.write).toEqual(Buffer.from("my-value"));
901
+ expect(std.err).toMatchInlineSnapshot(`""`);
902
+ });
903
+
904
+ it("should get a binary file by key in a given namespace specified by namespace-id", async () => {
905
+ const buf = Buffer.from(
906
+ "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAiSURBVHgB7coxEQAACMPAgH/PgAM6dGwu49fA/deIBXrgAj2cAhIFT4QxAAAAAElFTkSuQmCC",
907
+ "base64"
908
+ );
909
+ setMockFetchKVGetValue(
910
+ "some-account-id",
911
+ "some-namespace-id",
912
+ "my-key",
913
+ buf
914
+ );
915
+ await runWrangler("kv:key get my-key --namespace-id some-namespace-id");
916
+ expect(proc.write).toEqual(buf);
892
917
  expect(std.err).toMatchInlineSnapshot(`""`);
893
- expect(requests.count).toEqual(1);
894
918
  });
895
919
 
896
920
  it("should get a key in a given namespace specified by binding", async () => {
897
921
  writeWranglerConfig();
898
- const requests = mockKeyGetRequest("bound-id", "my-key", "my-value");
922
+ setMockFetchKVGetValue(
923
+ "some-account-id",
924
+ "bound-id",
925
+ "my-key",
926
+ "my-value"
927
+ );
899
928
  await runWrangler(
900
929
  "kv:key get my-key --binding someBinding --preview false"
901
930
  );
902
- expect(std.out).toMatchInlineSnapshot(`"my-value"`);
931
+ expect(proc.write).toEqual(Buffer.from("my-value"));
903
932
  expect(std.err).toMatchInlineSnapshot(`""`);
904
- expect(requests.count).toEqual(1);
905
933
  });
906
934
 
907
935
  it("should get a key in a given preview namespace specified by binding", async () => {
908
936
  writeWranglerConfig();
909
- const requests = mockKeyGetRequest(
937
+ setMockFetchKVGetValue(
938
+ "some-account-id",
910
939
  "preview-bound-id",
911
940
  "my-key",
912
941
  "my-value"
913
942
  );
914
943
  await runWrangler("kv:key get my-key --binding someBinding --preview");
915
- expect(std.out).toMatchInlineSnapshot(`"my-value"`);
944
+ expect(proc.write).toEqual(Buffer.from("my-value"));
916
945
  expect(std.err).toMatchInlineSnapshot(`""`);
917
- expect(requests.count).toEqual(1);
918
946
  });
919
947
 
920
948
  it("should get a key for the specified environment in a given namespace", async () => {
921
949
  writeWranglerConfig();
922
- const requests = mockKeyGetRequest(
950
+ setMockFetchKVGetValue(
951
+ "some-account-id",
923
952
  "env-bound-id",
924
953
  "my-key",
925
954
  "my-value"
926
955
  );
927
956
  await runWrangler(
928
- "kv:key get my-key my-value --binding someBinding --env some-environment --preview false"
957
+ "kv:key get my-key --binding someBinding --env some-environment --preview false"
929
958
  );
930
- expect(std.out).toMatchInlineSnapshot(`"my-value"`);
959
+ expect(proc.write).toEqual(Buffer.from("my-value"));
960
+ expect(std.err).toMatchInlineSnapshot(`""`);
961
+ });
962
+
963
+ it("should encode the key in the api request to get a value", async () => {
964
+ setMockFetchKVGetValue(
965
+ "some-account-id",
966
+ "some-namespace-id",
967
+ "%2Fmy%2Ckey",
968
+ "my-value"
969
+ );
970
+ await runWrangler(
971
+ "kv:key get /my,key --namespace-id some-namespace-id"
972
+ );
973
+ expect(proc.write).toEqual(Buffer.from("my-value"));
931
974
  expect(std.err).toMatchInlineSnapshot(`""`);
932
- expect(requests.count).toEqual(1);
933
975
  });
934
976
 
935
977
  it("should error if no key is provided", async () => {
@@ -938,9 +980,9 @@ describe("wrangler", () => {
938
980
  ).rejects.toThrowErrorMatchingInlineSnapshot(
939
981
  `"Not enough non-option arguments: got 0, need at least 1"`
940
982
  );
941
- expect(std.out).toMatchInlineSnapshot(`""`);
942
- expect(std.err).toMatchInlineSnapshot(`
943
- "wrangler kv:key get <key>
983
+ expect(std.out).toMatchInlineSnapshot(`
984
+ "
985
+ wrangler kv:key get <key>
944
986
 
945
987
  Reads a single value by key from the given namespace.
946
988
 
@@ -953,13 +995,15 @@ describe("wrangler", () => {
953
995
  -v, --version Show version number [boolean]
954
996
 
955
997
  Options:
956
- -l, --local Run on my machine [boolean] [default: false]
957
998
  --binding The name of the namespace to get from [string]
958
999
  --namespace-id The id of the namespace to get from [string]
959
- --env Perform on a specific environment [string]
960
- --preview Interact with a preview namespace [boolean] [default: false]
1000
+ -e, --env Perform on a specific environment [string]
1001
+ --preview Interact with a preview namespace [boolean] [default: false]"
1002
+ `);
1003
+ expect(std.err).toMatchInlineSnapshot(`
1004
+ "X [ERROR] Not enough non-option arguments: got 0, need at least 1
961
1005
 
962
- Not enough non-option arguments: got 0, need at least 1"
1006
+ "
963
1007
  `);
964
1008
  });
965
1009
 
@@ -969,9 +1013,9 @@ describe("wrangler", () => {
969
1013
  ).rejects.toThrowErrorMatchingInlineSnapshot(
970
1014
  `"Exactly one of the arguments binding and namespace-id is required"`
971
1015
  );
972
- expect(std.out).toMatchInlineSnapshot(`""`);
973
- expect(std.err).toMatchInlineSnapshot(`
974
- "wrangler kv:key get <key>
1016
+ expect(std.out).toMatchInlineSnapshot(`
1017
+ "
1018
+ wrangler kv:key get <key>
975
1019
 
976
1020
  Reads a single value by key from the given namespace.
977
1021
 
@@ -984,13 +1028,15 @@ describe("wrangler", () => {
984
1028
  -v, --version Show version number [boolean]
985
1029
 
986
1030
  Options:
987
- -l, --local Run on my machine [boolean] [default: false]
988
1031
  --binding The name of the namespace to get from [string]
989
1032
  --namespace-id The id of the namespace to get from [string]
990
- --env Perform on a specific environment [string]
991
- --preview Interact with a preview namespace [boolean] [default: false]
1033
+ -e, --env Perform on a specific environment [string]
1034
+ --preview Interact with a preview namespace [boolean] [default: false]"
1035
+ `);
1036
+ expect(std.err).toMatchInlineSnapshot(`
1037
+ "X [ERROR] Exactly one of the arguments binding and namespace-id is required
992
1038
 
993
- Exactly one of the arguments binding and namespace-id is required"
1039
+ "
994
1040
  `);
995
1041
  });
996
1042
 
@@ -1001,9 +1047,9 @@ describe("wrangler", () => {
1001
1047
  `"Arguments binding and namespace-id are mutually exclusive"`
1002
1048
  );
1003
1049
 
1004
- expect(std.out).toMatchInlineSnapshot(`""`);
1005
- expect(std.err).toMatchInlineSnapshot(`
1006
- "wrangler kv:key get <key>
1050
+ expect(std.out).toMatchInlineSnapshot(`
1051
+ "
1052
+ wrangler kv:key get <key>
1007
1053
 
1008
1054
  Reads a single value by key from the given namespace.
1009
1055
 
@@ -1016,13 +1062,15 @@ describe("wrangler", () => {
1016
1062
  -v, --version Show version number [boolean]
1017
1063
 
1018
1064
  Options:
1019
- -l, --local Run on my machine [boolean] [default: false]
1020
1065
  --binding The name of the namespace to get from [string]
1021
1066
  --namespace-id The id of the namespace to get from [string]
1022
- --env Perform on a specific environment [string]
1023
- --preview Interact with a preview namespace [boolean] [default: false]
1067
+ -e, --env Perform on a specific environment [string]
1068
+ --preview Interact with a preview namespace [boolean] [default: false]"
1069
+ `);
1070
+ expect(std.err).toMatchInlineSnapshot(`
1071
+ "X [ERROR] Arguments binding and namespace-id are mutually exclusive
1024
1072
 
1025
- Arguments binding and namespace-id are mutually exclusive"
1073
+ "
1026
1074
  `);
1027
1075
  });
1028
1076
 
@@ -1033,12 +1081,14 @@ describe("wrangler", () => {
1033
1081
  ).rejects.toThrowErrorMatchingInlineSnapshot(
1034
1082
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
1035
1083
  );
1036
- expect(std.out).toMatchInlineSnapshot(`""`);
1084
+ expect(std.out).toMatchInlineSnapshot(`
1085
+ "
1086
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1087
+ `);
1037
1088
  expect(std.err).toMatchInlineSnapshot(`
1038
- "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1089
+ "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1039
1090
 
1040
- %s
1041
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1091
+ "
1042
1092
  `);
1043
1093
  });
1044
1094
  });
@@ -1071,6 +1121,16 @@ describe("wrangler", () => {
1071
1121
  expect(requests.count).toEqual(1);
1072
1122
  });
1073
1123
 
1124
+ it("should encode the key in the api request to delete a value", async () => {
1125
+ const requests = mockDeleteRequest("voyager", "%2FNCC-74656");
1126
+ await runWrangler(`kv:key delete --namespace-id voyager /NCC-74656`);
1127
+ expect(requests.count).toEqual(1);
1128
+ expect(std.out).toMatchInlineSnapshot(
1129
+ `"Deleting the key \\"/NCC-74656\\" on namespace voyager."`
1130
+ );
1131
+ expect(std.err).toMatchInlineSnapshot(`""`);
1132
+ });
1133
+
1074
1134
  it("should delete a key in a namespace specified by binding name", async () => {
1075
1135
  writeWranglerConfig();
1076
1136
  const requests = mockDeleteRequest("bound-id", "someKey");
@@ -1098,10 +1158,9 @@ describe("wrangler", () => {
1098
1158
  );
1099
1159
 
1100
1160
  expect(std.err).toMatchInlineSnapshot(`
1101
- "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1161
+ "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1102
1162
 
1103
- %s
1104
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1163
+ "
1105
1164
  `);
1106
1165
  });
1107
1166
 
@@ -1112,7 +1171,7 @@ describe("wrangler", () => {
1112
1171
  `kv:key delete --binding someBinding --env some-environment --preview false someKey`
1113
1172
  );
1114
1173
  expect(std.out).toMatchInlineSnapshot(
1115
- `"deleting the key \\"someKey\\" on namespace env-bound-id"`
1174
+ `"Deleting the key \\"someKey\\" on namespace env-bound-id."`
1116
1175
  );
1117
1176
  expect(std.err).toMatchInlineSnapshot(`""`);
1118
1177
  expect(requests.count).toEqual(1);
@@ -1128,13 +1187,325 @@ describe("wrangler", () => {
1128
1187
  });
1129
1188
  });
1130
1189
  });
1190
+
1191
+ describe("kv:bulk", () => {
1192
+ describe("put", () => {
1193
+ function mockPutRequest(
1194
+ expectedNamespaceId: string,
1195
+ expectedKeyValues: KeyValue[]
1196
+ ) {
1197
+ const requests = { count: 0 };
1198
+ setMockResponse(
1199
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1200
+ "PUT",
1201
+ ([_url, accountId, namespaceId], { body }) => {
1202
+ requests.count++;
1203
+ expect(accountId).toEqual("some-account-id");
1204
+ expect(namespaceId).toEqual(expectedNamespaceId);
1205
+ expect(JSON.parse(body as string)).toEqual(
1206
+ expectedKeyValues.slice(
1207
+ (requests.count - 1) * 5000,
1208
+ requests.count * 5000
1209
+ )
1210
+ );
1211
+ return null;
1212
+ }
1213
+ );
1214
+ return requests;
1215
+ }
1216
+
1217
+ it("should put the key-values parsed from a file", async () => {
1218
+ const keyValues: KeyValue[] = [
1219
+ { key: "someKey1", value: "someValue1" },
1220
+ { key: "ns:someKey2", value: "123", base64: true },
1221
+ { key: "someKey3", value: "someValue3", expiration: 100 },
1222
+ { key: "someKey4", value: "someValue4", expiration_ttl: 500 },
1223
+ ];
1224
+ writeFileSync("./keys.json", JSON.stringify(keyValues));
1225
+ const requests = mockPutRequest("some-namespace-id", keyValues);
1226
+ await runWrangler(
1227
+ `kv:bulk put --namespace-id some-namespace-id keys.json`
1228
+ );
1229
+ expect(requests.count).toEqual(1);
1230
+ expect(std.out).toMatchInlineSnapshot(`"Success!"`);
1231
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1232
+ expect(std.err).toMatchInlineSnapshot(`""`);
1233
+ });
1234
+
1235
+ it("should put the key-values in batches of 5000 parsed from a file", async () => {
1236
+ const keyValues: KeyValue[] = new Array(12000).fill({
1237
+ key: "someKey1",
1238
+ value: "someValue1",
1239
+ });
1240
+ writeFileSync("./keys.json", JSON.stringify(keyValues));
1241
+ const requests = mockPutRequest("some-namespace-id", keyValues);
1242
+ await runWrangler(
1243
+ `kv:bulk put --namespace-id some-namespace-id keys.json`
1244
+ );
1245
+ expect(requests.count).toEqual(3);
1246
+ expect(std.out).toMatchInlineSnapshot(`
1247
+ "Uploaded 0% (0 out of 12,000)
1248
+ Uploaded 41% (5,000 out of 12,000)
1249
+ Uploaded 83% (10,000 out of 12,000)
1250
+ Uploaded 100% (12,000 out of 12,000)
1251
+ Success!"
1252
+ `);
1253
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1254
+ expect(std.err).toMatchInlineSnapshot(`""`);
1255
+ });
1256
+
1257
+ it("should error if the file is not a JSON array", async () => {
1258
+ const keyValues = { key: "someKey1", value: "someValue1" };
1259
+ writeFileSync("./keys.json", JSON.stringify(keyValues));
1260
+ await expect(
1261
+ runWrangler(`kv:bulk put --namespace-id some-namespace-id keys.json`)
1262
+ ).rejects.toThrowErrorMatchingInlineSnapshot(`
1263
+ "Unexpected JSON input from \\"keys.json\\".
1264
+ Expected an array of key-value objects but got type \\"object\\"."
1265
+ `);
1266
+ expect(std.out).toMatchInlineSnapshot(`
1267
+ "
1268
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1269
+ `);
1270
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1271
+ });
1272
+
1273
+ it("should error if the array contains items that are not key-value objects", async () => {
1274
+ const keyValues = [
1275
+ 123,
1276
+ "a string",
1277
+ { key: "someKey" },
1278
+ { value: "someValue" },
1279
+ // add a valid object here to make sure it's not included
1280
+ { key: "someKey1", value: "someValue1" },
1281
+ // this one will only add a warning
1282
+ { key: "someKey1", value: "someValue1", invalid: true },
1283
+ // back to the invalid ones
1284
+ { key: 123, value: "somevalue" },
1285
+ { key: "somekey", value: 123 },
1286
+ { key: "someKey1", value: "someValue1", expiration: "string" },
1287
+ { key: "someKey1", value: "someValue1", expiration_ttl: "string" },
1288
+ {
1289
+ key: 123,
1290
+ value: {
1291
+ a: {
1292
+ nested: "object",
1293
+ },
1294
+ },
1295
+ },
1296
+ { key: "someKey1", value: "someValue1", metadata: 123 },
1297
+ { key: "someKey1", value: "someValue1", base64: "string" },
1298
+ ];
1299
+ writeFileSync("./keys.json", JSON.stringify(keyValues));
1300
+ await expect(
1301
+ runWrangler(`kv:bulk put --namespace-id some-namespace-id keys.json`)
1302
+ ).rejects.toThrowErrorMatchingInlineSnapshot(`
1303
+ "Unexpected JSON input from \\"keys.json\\".
1304
+ Each item in the array should be an object that matches:
1305
+
1306
+ interface KeyValue {
1307
+ key: string;
1308
+ value: string;
1309
+ expiration?: number;
1310
+ expiration_ttl?: number;
1311
+ metadata?: object;
1312
+ base64?: boolean;
1313
+ }
1314
+
1315
+ The item at index 0 is 123
1316
+ The item at index 1 is \\"a string\\"
1317
+ The item at index 2 is {\\"key\\":\\"someKey\\"}
1318
+ The item at index 3 is {\\"value\\":\\"someValue\\"}
1319
+ The item at index 6 is {\\"key\\":123,\\"value\\":\\"somevalue\\"}
1320
+ The item at index 7 is {\\"key\\":\\"somekey\\",\\"value\\":123}
1321
+ The item at index 8 is {\\"key\\":\\"someKey1\\",\\"value\\":\\"someValue1\\",\\"expiration\\":\\"string\\"}
1322
+ The item at index 9 is {\\"key\\":\\"someKey1\\",\\"value\\":\\"someValue1\\",\\"expiration_ttl\\":\\"string\\"}
1323
+ The item at index 10 is {\\"key\\":123,\\"value\\":{\\"a\\":{\\"nested\\":\\"object\\"}}}
1324
+ The item at index 11 is {\\"key\\":\\"someKey1\\",\\"value\\":\\"someValue1\\",\\"metadata\\":123}
1325
+ The item at index 12 is {\\"key\\":\\"someKey1\\",\\"value\\":\\"someValue1\\",\\"base64\\":\\"string\\"}"
1326
+ `);
1327
+
1328
+ expect(std.out).toMatchInlineSnapshot(`
1329
+ "
1330
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1331
+ `);
1332
+ expect(std.warn).toMatchInlineSnapshot(`
1333
+ "▲ [WARNING] Unexpected key-value properties in \\"keys.json\\".
1334
+
1335
+ The item at index 5 contains unexpected properties: [\\"invalid\\"].
1336
+
1337
+ "
1338
+ `);
1339
+ });
1340
+ });
1341
+
1342
+ describe("delete", () => {
1343
+ function mockDeleteRequest(
1344
+ expectedNamespaceId: string,
1345
+ expectedKeys: string[]
1346
+ ) {
1347
+ const requests = { count: 0 };
1348
+ setMockResponse(
1349
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1350
+ "DELETE",
1351
+ ([_url, accountId, namespaceId], { headers, body }) => {
1352
+ requests.count++;
1353
+ expect(accountId).toEqual("some-account-id");
1354
+ expect(namespaceId).toEqual(expectedNamespaceId);
1355
+ expect(new Headers(headers ?? []).get("Content-Type")).toEqual(
1356
+ "application/json"
1357
+ );
1358
+ expect(JSON.parse(body as string)).toEqual(
1359
+ expectedKeys.slice(
1360
+ (requests.count - 1) * 5000,
1361
+ requests.count * 5000
1362
+ )
1363
+ );
1364
+ return null;
1365
+ }
1366
+ );
1367
+ return requests;
1368
+ }
1369
+
1370
+ it("should delete the keys parsed from a file", async () => {
1371
+ const keys = ["someKey1", "ns:someKey2"];
1372
+ writeFileSync("./keys.json", JSON.stringify(keys));
1373
+ mockConfirm({
1374
+ text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1375
+ result: true,
1376
+ });
1377
+ const requests = mockDeleteRequest("some-namespace-id", keys);
1378
+ await runWrangler(
1379
+ `kv:bulk delete --namespace-id some-namespace-id keys.json`
1380
+ );
1381
+ expect(requests.count).toEqual(1);
1382
+ expect(std.out).toMatchInlineSnapshot(`"Success!"`);
1383
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1384
+ expect(std.err).toMatchInlineSnapshot(`""`);
1385
+ });
1386
+
1387
+ it("should delete the keys in batches of 5000 parsed from a file", async () => {
1388
+ const keys = new Array(12000).fill("some-key");
1389
+ writeFileSync("./keys.json", JSON.stringify(keys));
1390
+ mockConfirm({
1391
+ text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1392
+ result: true,
1393
+ });
1394
+ const requests = mockDeleteRequest("some-namespace-id", keys);
1395
+ await runWrangler(
1396
+ `kv:bulk delete --namespace-id some-namespace-id keys.json`
1397
+ );
1398
+ expect(requests.count).toEqual(3);
1399
+ expect(std.out).toMatchInlineSnapshot(`
1400
+ "Deleted 0% (0 out of 12,000)
1401
+ Deleted 41% (5,000 out of 12,000)
1402
+ Deleted 83% (10,000 out of 12,000)
1403
+ Deleted 100% (12,000 out of 12,000)
1404
+ Success!"
1405
+ `);
1406
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1407
+ expect(std.err).toMatchInlineSnapshot(`""`);
1408
+ });
1409
+
1410
+ it("should not delete the keys if the user confirms no", async () => {
1411
+ const keys = ["someKey1", "ns:someKey2"];
1412
+ writeFileSync("./keys.json", JSON.stringify(keys));
1413
+ mockConfirm({
1414
+ text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1415
+ result: false,
1416
+ });
1417
+ await runWrangler(
1418
+ `kv:bulk delete --namespace-id some-namespace-id keys.json`
1419
+ );
1420
+ expect(std.out).toMatchInlineSnapshot(
1421
+ `"Not deleting keys read from \\"keys.json\\"."`
1422
+ );
1423
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1424
+ expect(std.err).toMatchInlineSnapshot(`""`);
1425
+ });
1426
+
1427
+ it("should delete the keys without asking if --force is provided", async () => {
1428
+ const keys = ["someKey1", "ns:someKey2"];
1429
+ writeFileSync("./keys.json", JSON.stringify(keys));
1430
+ const requests = mockDeleteRequest("some-namespace-id", keys);
1431
+ await runWrangler(
1432
+ `kv:bulk delete --namespace-id some-namespace-id keys.json --force`
1433
+ );
1434
+ expect(requests.count).toEqual(1);
1435
+ expect(std.out).toMatchInlineSnapshot(`"Success!"`);
1436
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1437
+ expect(std.err).toMatchInlineSnapshot(`""`);
1438
+ });
1439
+
1440
+ it("should delete the keys without asking if -f is provided", async () => {
1441
+ const keys = ["someKey1", "ns:someKey2"];
1442
+ writeFileSync("./keys.json", JSON.stringify(keys));
1443
+ const requests = mockDeleteRequest("some-namespace-id", keys);
1444
+ await runWrangler(
1445
+ `kv:bulk delete --namespace-id some-namespace-id keys.json -f`
1446
+ );
1447
+ expect(requests.count).toEqual(1);
1448
+ expect(std.out).toMatchInlineSnapshot(`"Success!"`);
1449
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1450
+ expect(std.err).toMatchInlineSnapshot(`""`);
1451
+ });
1452
+
1453
+ it("should error if the file is not a JSON array", async () => {
1454
+ const keys = 12354;
1455
+ writeFileSync("./keys.json", JSON.stringify(keys));
1456
+ mockConfirm({
1457
+ text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1458
+ result: true,
1459
+ });
1460
+ await expect(
1461
+ runWrangler(
1462
+ `kv:bulk delete --namespace-id some-namespace-id keys.json`
1463
+ )
1464
+ ).rejects.toThrowErrorMatchingInlineSnapshot(`
1465
+ "Unexpected JSON input from \\"keys.json\\".
1466
+ Expected an array of strings but got:
1467
+ 12354"
1468
+ `);
1469
+ expect(std.out).toMatchInlineSnapshot(`
1470
+ "
1471
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1472
+ `);
1473
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1474
+ });
1475
+
1476
+ it("should error if the file contains non-string items", async () => {
1477
+ const keys = ["good", 12354, { key: "someKey" }, null];
1478
+ writeFileSync("./keys.json", JSON.stringify(keys));
1479
+ mockConfirm({
1480
+ text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1481
+ result: true,
1482
+ });
1483
+ await expect(
1484
+ runWrangler(
1485
+ `kv:bulk delete --namespace-id some-namespace-id keys.json`
1486
+ )
1487
+ ).rejects.toThrowErrorMatchingInlineSnapshot(`
1488
+ "Unexpected JSON input from \\"keys.json\\".
1489
+ Expected an array of strings.
1490
+ The item at index 1 is type: \\"number\\" - 12354
1491
+ The item at index 2 is type: \\"object\\" - {\\"key\\":\\"someKey\\"}
1492
+ The item at index 3 is type: \\"object\\" - null"
1493
+ `);
1494
+ expect(std.out).toMatchInlineSnapshot(`
1495
+ "
1496
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1497
+ `);
1498
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1499
+ });
1500
+ });
1501
+ });
1131
1502
  });
1132
1503
 
1133
1504
  function writeWranglerConfig() {
1134
1505
  writeFileSync(
1135
1506
  "./wrangler.toml",
1136
1507
  [
1137
- 'name = "otherWorker"',
1508
+ 'name = "other-worker"',
1138
1509
  "kv_namespaces = [",
1139
1510
  ' { binding = "someBinding", id = "bound-id", preview_id = "preview-bound-id" }',
1140
1511
  "]",