wrangler 0.0.0-e3e3243 → 0.0.0-e4047cab

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