wrangler 0.0.0-e95e5a0 → 0.0.0-e981d0be5

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