wrangler 0.0.0-e87ad068 → 0.0.0-e8a2a1d9

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