wrangler 0.0.0-e6e1daba → 0.0.0-e72a5794

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 (299) hide show
  1. package/README.md +10 -12
  2. package/bin/wrangler.js +10 -76
  3. package/package.json +19 -18
  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/gitignore +1 -0
  8. package/templates/middleware/common.ts +9 -4
  9. package/templates/middleware/loader-modules.ts +64 -11
  10. package/templates/middleware/loader-sw.ts +2 -7
  11. package/templates/middleware/middleware-d1-beta.d.ts +3 -0
  12. package/templates/{d1-beta-facade.js → middleware/middleware-d1-beta.ts} +87 -84
  13. package/templates/middleware/middleware-miniflare3-json-error.ts +17 -5
  14. package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
  15. package/templates/middleware/middleware-multiworker-dev.ts +59 -0
  16. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  17. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  18. package/templates/new-worker-scheduled.ts +1 -1
  19. package/templates/new-worker.ts +1 -1
  20. package/templates/pages-dev-util.ts +4 -1
  21. package/templates/pages-shim.ts +0 -3
  22. package/templates/pages-template-plugin.ts +12 -2
  23. package/templates/pages-template-worker.ts +11 -2
  24. package/templates/tsconfig.init.json +1 -2
  25. package/templates/tsconfig.json +1 -1
  26. package/wrangler-dist/cli.d.ts +192 -87
  27. package/wrangler-dist/cli.js +76158 -78596
  28. package/import_meta_url.js +0 -3
  29. package/miniflare-config-stubs/.env.empty +0 -0
  30. package/miniflare-config-stubs/package.empty.json +0 -1
  31. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  32. package/miniflare-dist/index.mjs +0 -6368
  33. package/src/__tests__/access.test.ts +0 -25
  34. package/src/__tests__/api-dev.test.ts +0 -238
  35. package/src/__tests__/api-devregistry.test.js +0 -64
  36. package/src/__tests__/api.test.ts +0 -80
  37. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  38. package/src/__tests__/config-cache.test.ts +0 -42
  39. package/src/__tests__/configuration.test.ts +0 -4233
  40. package/src/__tests__/d1/d1.test.ts +0 -82
  41. package/src/__tests__/d1/execute.test.ts +0 -64
  42. package/src/__tests__/d1/migrate.test.ts +0 -155
  43. package/src/__tests__/d1/splitter.test.ts +0 -255
  44. package/src/__tests__/delete.test.ts +0 -266
  45. package/src/__tests__/deployments.test.ts +0 -129
  46. package/src/__tests__/dev.test.tsx +0 -1587
  47. package/src/__tests__/generate.test.ts +0 -238
  48. package/src/__tests__/get-host-from-url.test.ts +0 -16
  49. package/src/__tests__/guess-worker-format.test.ts +0 -120
  50. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  51. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  52. package/src/__tests__/helpers/end-event-loop.ts +0 -6
  53. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  54. package/src/__tests__/helpers/mock-auth-domain.ts +0 -20
  55. package/src/__tests__/helpers/mock-bin.ts +0 -36
  56. package/src/__tests__/helpers/mock-console.ts +0 -106
  57. package/src/__tests__/helpers/mock-dialogs.ts +0 -139
  58. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +0 -25
  59. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -11
  60. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  61. package/src/__tests__/helpers/mock-istty.ts +0 -74
  62. package/src/__tests__/helpers/mock-known-routes.ts +0 -12
  63. package/src/__tests__/helpers/mock-kv.ts +0 -46
  64. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -263
  65. package/src/__tests__/helpers/mock-process.ts +0 -34
  66. package/src/__tests__/helpers/mock-set-timeout.ts +0 -16
  67. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  68. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  69. package/src/__tests__/helpers/msw/blob-worker.cjs +0 -19
  70. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  71. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -93
  72. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -81
  73. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -31
  74. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -60
  75. package/src/__tests__/helpers/msw/handlers/script.ts +0 -56
  76. package/src/__tests__/helpers/msw/handlers/user.ts +0 -52
  77. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -20
  78. package/src/__tests__/helpers/msw/index.ts +0 -50
  79. package/src/__tests__/helpers/msw/read-file-sync.js +0 -61
  80. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  81. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  82. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +0 -28
  83. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  84. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  85. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  86. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
  87. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  88. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  89. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  90. package/src/__tests__/https-options.test.ts +0 -163
  91. package/src/__tests__/index.test.ts +0 -274
  92. package/src/__tests__/init.test.ts +0 -3164
  93. package/src/__tests__/jest.setup.ts +0 -175
  94. package/src/__tests__/kv.test.ts +0 -1799
  95. package/src/__tests__/logger.test.ts +0 -141
  96. package/src/__tests__/logout.test.ts +0 -47
  97. package/src/__tests__/metrics.test.ts +0 -493
  98. package/src/__tests__/middleware.scheduled.test.ts +0 -145
  99. package/src/__tests__/middleware.test.ts +0 -815
  100. package/src/__tests__/mtls-certificates.test.ts +0 -585
  101. package/src/__tests__/package-manager.test.ts +0 -353
  102. package/src/__tests__/pages/deployment-list.test.ts +0 -78
  103. package/src/__tests__/pages/functions-build.test.ts +0 -402
  104. package/src/__tests__/pages/pages.test.ts +0 -81
  105. package/src/__tests__/pages/project-create.test.ts +0 -63
  106. package/src/__tests__/pages/project-list.test.ts +0 -108
  107. package/src/__tests__/pages/project-upload.test.ts +0 -481
  108. package/src/__tests__/pages/publish.test.ts +0 -2745
  109. package/src/__tests__/pages-deployment-tail.test.ts +0 -955
  110. package/src/__tests__/parse.test.ts +0 -436
  111. package/src/__tests__/paths.test.ts +0 -39
  112. package/src/__tests__/publish.test.ts +0 -8167
  113. package/src/__tests__/pubsub.test.ts +0 -493
  114. package/src/__tests__/queues.test.ts +0 -529
  115. package/src/__tests__/r2.test.ts +0 -374
  116. package/src/__tests__/route.test.ts +0 -45
  117. package/src/__tests__/secret.test.ts +0 -693
  118. package/src/__tests__/tail.test.ts +0 -958
  119. package/src/__tests__/test-old-node-version.js +0 -31
  120. package/src/__tests__/tsconfig-sanity.ts +0 -12
  121. package/src/__tests__/tsconfig.json +0 -8
  122. package/src/__tests__/tsconfig.tsbuildinfo +0 -1
  123. package/src/__tests__/type-generation.test.ts +0 -233
  124. package/src/__tests__/user.test.ts +0 -118
  125. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  126. package/src/__tests__/validate-dev-props.test.ts +0 -56
  127. package/src/__tests__/version.test.ts +0 -35
  128. package/src/__tests__/whoami.test.tsx +0 -172
  129. package/src/__tests__/worker-namespace.test.ts +0 -339
  130. package/src/abort.d.ts +0 -3
  131. package/src/api/dev.ts +0 -328
  132. package/src/api/index.ts +0 -11
  133. package/src/api/mtls-certificate.ts +0 -148
  134. package/src/api/pages/create-worker-bundle-contents.ts +0 -75
  135. package/src/api/pages/index.ts +0 -5
  136. package/src/api/pages/publish.tsx +0 -368
  137. package/src/bundle-reporter.ts +0 -68
  138. package/src/bundle.ts +0 -877
  139. package/src/cfetch/index.ts +0 -158
  140. package/src/cfetch/internal.ts +0 -258
  141. package/src/cli.ts +0 -28
  142. package/src/config/README.md +0 -107
  143. package/src/config/config.ts +0 -282
  144. package/src/config/diagnostics.ts +0 -80
  145. package/src/config/environment.ts +0 -586
  146. package/src/config/index.ts +0 -375
  147. package/src/config/validation-helpers.ts +0 -597
  148. package/src/config/validation.ts +0 -2198
  149. package/src/config-cache.ts +0 -85
  150. package/src/create-worker-preview.ts +0 -293
  151. package/src/create-worker-upload-form.ts +0 -334
  152. package/src/d1/backups.tsx +0 -215
  153. package/src/d1/constants.ts +0 -2
  154. package/src/d1/create.tsx +0 -64
  155. package/src/d1/delete.ts +0 -53
  156. package/src/d1/execute.tsx +0 -354
  157. package/src/d1/formatTimeAgo.ts +0 -14
  158. package/src/d1/index.ts +0 -100
  159. package/src/d1/list.tsx +0 -60
  160. package/src/d1/migrations/apply.tsx +0 -206
  161. package/src/d1/migrations/create.tsx +0 -76
  162. package/src/d1/migrations/helpers.ts +0 -172
  163. package/src/d1/migrations/index.ts +0 -3
  164. package/src/d1/migrations/list.tsx +0 -92
  165. package/src/d1/migrations/options.ts +0 -23
  166. package/src/d1/options.ts +0 -22
  167. package/src/d1/splitter.ts +0 -161
  168. package/src/d1/types.ts +0 -25
  169. package/src/d1/utils.ts +0 -49
  170. package/src/delete.ts +0 -100
  171. package/src/deployments.ts +0 -116
  172. package/src/deprecated/index.ts +0 -144
  173. package/src/dev/dev-vars.ts +0 -39
  174. package/src/dev/dev.tsx +0 -600
  175. package/src/dev/get-local-persistence-path.ts +0 -31
  176. package/src/dev/local.tsx +0 -922
  177. package/src/dev/remote.tsx +0 -632
  178. package/src/dev/start-server.ts +0 -538
  179. package/src/dev/use-esbuild.ts +0 -205
  180. package/src/dev/validate-dev-props.ts +0 -31
  181. package/src/dev-registry.ts +0 -201
  182. package/src/dev.tsx +0 -914
  183. package/src/dialogs.ts +0 -136
  184. package/src/dispatch-namespace.ts +0 -211
  185. package/src/docs/index.ts +0 -97
  186. package/src/durable.ts +0 -102
  187. package/src/entry.ts +0 -327
  188. package/src/environment-variables/factory.ts +0 -88
  189. package/src/environment-variables/misc-variables.ts +0 -30
  190. package/src/errors.ts +0 -11
  191. package/src/generate/index.ts +0 -298
  192. package/src/git-client.ts +0 -135
  193. package/src/global-wrangler-config-path.ts +0 -26
  194. package/src/https-options.ts +0 -127
  195. package/src/index.ts +0 -705
  196. package/src/init.ts +0 -1022
  197. package/src/inspect.ts +0 -782
  198. package/src/intl-polyfill.d.ts +0 -139
  199. package/src/is-ci.ts +0 -14
  200. package/src/is-interactive.ts +0 -16
  201. package/src/jest.d.ts +0 -4
  202. package/src/kv/helpers.ts +0 -430
  203. package/src/kv/index.ts +0 -594
  204. package/src/logger.ts +0 -104
  205. package/src/metrics/index.ts +0 -5
  206. package/src/metrics/metrics-config.ts +0 -239
  207. package/src/metrics/metrics-dispatcher.ts +0 -96
  208. package/src/metrics/metrics-usage-headers.ts +0 -24
  209. package/src/metrics/send-event.ts +0 -98
  210. package/src/miniflare-cli/README.md +0 -30
  211. package/src/miniflare-cli/assets.ts +0 -251
  212. package/src/miniflare-cli/index.ts +0 -210
  213. package/src/miniflare-cli/request-context.ts +0 -40
  214. package/src/miniflare-cli/tsconfig.json +0 -9
  215. package/src/miniflare-cli/tsconfig.tsbuildinfo +0 -1
  216. package/src/miniflare-cli/types.ts +0 -11
  217. package/src/module-collection.ts +0 -267
  218. package/src/mtls-certificate/cli.ts +0 -155
  219. package/src/open-in-browser.ts +0 -17
  220. package/src/package-manager.ts +0 -219
  221. package/src/pages/build.ts +0 -232
  222. package/src/pages/buildFunctions.ts +0 -130
  223. package/src/pages/constants.ts +0 -18
  224. package/src/pages/deployment-tails.ts +0 -281
  225. package/src/pages/deployments.tsx +0 -86
  226. package/src/pages/dev.ts +0 -716
  227. package/src/pages/errors.ts +0 -66
  228. package/src/pages/functions/buildPlugin.ts +0 -90
  229. package/src/pages/functions/buildWorker.ts +0 -288
  230. package/src/pages/functions/filepath-routing.test.ts +0 -234
  231. package/src/pages/functions/filepath-routing.ts +0 -189
  232. package/src/pages/functions/identifiers.ts +0 -78
  233. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  234. package/src/pages/functions/routes-consolidation.ts +0 -73
  235. package/src/pages/functions/routes-transformation.test.ts +0 -282
  236. package/src/pages/functions/routes-transformation.ts +0 -115
  237. package/src/pages/functions/routes-validation.test.ts +0 -403
  238. package/src/pages/functions/routes-validation.ts +0 -202
  239. package/src/pages/functions/routes.ts +0 -151
  240. package/src/pages/functions/tsconfig.json +0 -8
  241. package/src/pages/functions/tsconfig.tsbuildinfo +0 -1
  242. package/src/pages/functions.ts +0 -86
  243. package/src/pages/hash.ts +0 -13
  244. package/src/pages/index.ts +0 -102
  245. package/src/pages/projects.tsx +0 -162
  246. package/src/pages/prompt-select-project.tsx +0 -31
  247. package/src/pages/publish.tsx +0 -276
  248. package/src/pages/types.ts +0 -46
  249. package/src/pages/upload.tsx +0 -449
  250. package/src/pages/utils.ts +0 -23
  251. package/src/parse.ts +0 -308
  252. package/src/paths.ts +0 -71
  253. package/src/proxy.ts +0 -694
  254. package/src/publish/index.ts +0 -274
  255. package/src/publish/publish.ts +0 -1065
  256. package/src/pubsub/index.ts +0 -286
  257. package/src/pubsub/pubsub-commands.ts +0 -623
  258. package/src/queues/cli/commands/consumer/add.ts +0 -65
  259. package/src/queues/cli/commands/consumer/index.ts +0 -19
  260. package/src/queues/cli/commands/consumer/remove.ts +0 -31
  261. package/src/queues/cli/commands/create.ts +0 -25
  262. package/src/queues/cli/commands/delete.ts +0 -26
  263. package/src/queues/cli/commands/index.ts +0 -35
  264. package/src/queues/cli/commands/list.ts +0 -25
  265. package/src/queues/client.ts +0 -135
  266. package/src/queues/utils.ts +0 -18
  267. package/src/r2/constants.ts +0 -4
  268. package/src/r2/helpers.ts +0 -132
  269. package/src/r2/index.ts +0 -289
  270. package/src/routes.ts +0 -140
  271. package/src/secret/index.ts +0 -375
  272. package/src/selfsigned.d.ts +0 -29
  273. package/src/sites.ts +0 -372
  274. package/src/tail/createTail.ts +0 -406
  275. package/src/tail/filters.ts +0 -277
  276. package/src/tail/index.ts +0 -211
  277. package/src/tail/printing.ts +0 -122
  278. package/src/tsconfig-sanity.ts +0 -16
  279. package/src/type-generation.ts +0 -181
  280. package/src/update-check.ts +0 -19
  281. package/src/user/access.ts +0 -68
  282. package/src/user/auth-variables.ts +0 -113
  283. package/src/user/choose-account.tsx +0 -39
  284. package/src/user/generate-auth-url.ts +0 -33
  285. package/src/user/generate-random-state.ts +0 -16
  286. package/src/user/index.ts +0 -2
  287. package/src/user/user.ts +0 -1233
  288. package/src/utils/collectKeyValues.ts +0 -14
  289. package/src/whoami.ts +0 -135
  290. package/src/worker.ts +0 -257
  291. package/src/yargs-types.ts +0 -37
  292. package/src/zones.ts +0 -191
  293. package/templates/__tests__/tsconfig.tsbuildinfo +0 -1
  294. package/templates/first-party-worker-module-facade.ts +0 -19
  295. package/templates/format-dev-errors.ts +0 -34
  296. package/templates/serve-static-assets.ts +0 -66
  297. package/templates/service-bindings-module-facade.js +0 -55
  298. package/templates/service-bindings-sw-facade.js +0 -42
  299. 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
- "out": "my-value",
979
- "warn": "",
980
- }
981
- `);
982
- });
983
-
984
- it("should get a binary and decode as utf8 text, resulting in improper decoding", async () => {
985
- const buf = Buffer.from(
986
- "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAiSURBVHgB7coxEQAACMPAgH/PgAM6dGwu49fA/deIBXrgAj2cAhIFT4QxAAAAAElFTkSuQmCC",
987
- "base64"
988
- );
989
- setMockFetchKVGetValue(
990
- "some-account-id",
991
- "some-namespace-id",
992
- "my-key",
993
- buf
994
- );
995
- await runWrangler(
996
- "kv:key get my-key --text --namespace-id some-namespace-id"
997
- );
998
- expect(proc.write).not.toEqual(buf);
999
- expect(JSON.stringify(std)).toMatchInlineSnapshot(
1000
- `"{\\"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\\":\\"\\"}"`
1001
- );
1002
- });
1003
-
1004
- it("should get a binary file by key in a given namespace specified by namespace-id", async () => {
1005
- const buf = Buffer.from(
1006
- "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAiSURBVHgB7coxEQAACMPAgH/PgAM6dGwu49fA/deIBXrgAj2cAhIFT4QxAAAAAElFTkSuQmCC",
1007
- "base64"
1008
- );
1009
- setMockFetchKVGetValue(
1010
- "some-account-id",
1011
- "some-namespace-id",
1012
- "my-key",
1013
- buf
1014
- );
1015
- await runWrangler("kv:key get my-key --namespace-id some-namespace-id");
1016
- expect(proc.write).toEqual(buf);
1017
- expect(std.err).toMatchInlineSnapshot(`""`);
1018
- });
1019
-
1020
- it("should get a key in a given namespace specified by binding", async () => {
1021
- writeWranglerConfig();
1022
- setMockFetchKVGetValue(
1023
- "some-account-id",
1024
- "bound-id",
1025
- "my-key",
1026
- "my-value"
1027
- );
1028
- await runWrangler(
1029
- "kv:key get my-key --binding someBinding --preview false"
1030
- );
1031
- expect(proc.write).toEqual(Buffer.from("my-value"));
1032
- expect(std.err).toMatchInlineSnapshot(`""`);
1033
- });
1034
-
1035
- it("should get a key in a given preview namespace specified by binding", async () => {
1036
- writeWranglerConfig();
1037
- setMockFetchKVGetValue(
1038
- "some-account-id",
1039
- "preview-bound-id",
1040
- "my-key",
1041
- "my-value"
1042
- );
1043
- await runWrangler("kv:key get my-key --binding someBinding --preview");
1044
- expect(proc.write).toEqual(Buffer.from("my-value"));
1045
- expect(std.err).toMatchInlineSnapshot(`""`);
1046
- });
1047
-
1048
- it("should get a key for the specified environment in a given namespace", async () => {
1049
- writeWranglerConfig();
1050
- setMockFetchKVGetValue(
1051
- "some-account-id",
1052
- "env-bound-id",
1053
- "my-key",
1054
- "my-value"
1055
- );
1056
- await runWrangler(
1057
- "kv:key get my-key --binding someBinding --env some-environment --preview false"
1058
- );
1059
- expect(proc.write).toEqual(Buffer.from("my-value"));
1060
- expect(std.err).toMatchInlineSnapshot(`""`);
1061
- });
1062
-
1063
- it("should encode the key in the api request to get a value", async () => {
1064
- setMockFetchKVGetValue(
1065
- "some-account-id",
1066
- "some-namespace-id",
1067
- "%2Fmy%2Ckey", // expect the key /my,key to be encoded
1068
- "my-value"
1069
- );
1070
-
1071
- await runWrangler(
1072
- "kv:key get /my,key --namespace-id some-namespace-id"
1073
- );
1074
- expect(proc.write).toEqual(Buffer.from("my-value"));
1075
- expect(std.err).toMatchInlineSnapshot(`""`);
1076
- });
1077
-
1078
- it("should error if no key is provided", async () => {
1079
- await expect(
1080
- runWrangler("kv:key get")
1081
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1082
- `"Not enough non-option arguments: got 0, need at least 1"`
1083
- );
1084
- expect(std.out).toMatchInlineSnapshot(`
1085
- "
1086
- wrangler kv:key get <key>
1087
-
1088
- Reads a single value by key from the given namespace.
1089
-
1090
- Positionals:
1091
- key The key value to get. [string] [required]
1092
-
1093
- Flags:
1094
- -j, --experimental-json-config Experimental: Support wrangler.json [boolean]
1095
- -c, --config Path to .toml configuration file [string]
1096
- -e, --env Environment to use for operations and .env files [string]
1097
- -h, --help Show help [boolean]
1098
- -v, --version Show version number [boolean]
1099
-
1100
- Options:
1101
- --binding The name of the namespace to get from [string]
1102
- --namespace-id The id of the namespace to get from [string]
1103
- --preview Interact with a preview namespace [boolean] [default: false]
1104
- --text Decode the returned value as a utf8 string [boolean] [default: false]"
1105
- `);
1106
- expect(std.err).toMatchInlineSnapshot(`
1107
- "X [ERROR] Not enough non-option arguments: got 0, need at least 1
1108
-
1109
- "
1110
- `);
1111
- });
1112
-
1113
- it("should error if no binding nor namespace is provided", async () => {
1114
- await expect(
1115
- runWrangler("kv:key get foo")
1116
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1117
- `"Exactly one of the arguments binding and namespace-id is required"`
1118
- );
1119
- expect(std.out).toMatchInlineSnapshot(`
1120
- "
1121
- wrangler kv:key get <key>
1122
-
1123
- Reads a single value by key from the given namespace.
1124
-
1125
- Positionals:
1126
- key The key value to get. [string] [required]
1127
-
1128
- Flags:
1129
- -j, --experimental-json-config Experimental: Support wrangler.json [boolean]
1130
- -c, --config Path to .toml configuration file [string]
1131
- -e, --env Environment to use for operations and .env files [string]
1132
- -h, --help Show help [boolean]
1133
- -v, --version Show version number [boolean]
1134
-
1135
- Options:
1136
- --binding The name of the namespace to get from [string]
1137
- --namespace-id The id of the namespace to get from [string]
1138
- --preview Interact with a preview namespace [boolean] [default: false]
1139
- --text Decode the returned value as a utf8 string [boolean] [default: false]"
1140
- `);
1141
- expect(std.err).toMatchInlineSnapshot(`
1142
- "X [ERROR] Exactly one of the arguments binding and namespace-id is required
1143
-
1144
- "
1145
- `);
1146
- });
1147
-
1148
- it("should error if both binding and namespace is provided", async () => {
1149
- await expect(
1150
- runWrangler("kv:key get foo --binding x --namespace-id y")
1151
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1152
- `"Arguments binding and namespace-id are mutually exclusive"`
1153
- );
1154
-
1155
- expect(std.out).toMatchInlineSnapshot(`
1156
- "
1157
- wrangler kv:key get <key>
1158
-
1159
- Reads a single value by key from the given namespace.
1160
-
1161
- Positionals:
1162
- key The key value to get. [string] [required]
1163
-
1164
- Flags:
1165
- -j, --experimental-json-config Experimental: Support wrangler.json [boolean]
1166
- -c, --config Path to .toml configuration file [string]
1167
- -e, --env Environment to use for operations and .env files [string]
1168
- -h, --help Show help [boolean]
1169
- -v, --version Show version number [boolean]
1170
-
1171
- Options:
1172
- --binding The name of the namespace to get from [string]
1173
- --namespace-id The id of the namespace to get from [string]
1174
- --preview Interact with a preview namespace [boolean] [default: false]
1175
- --text Decode the returned value as a utf8 string [boolean] [default: false]"
1176
- `);
1177
- expect(std.err).toMatchInlineSnapshot(`
1178
- "X [ERROR] Arguments binding and namespace-id are mutually exclusive
1179
-
1180
- "
1181
- `);
1182
- });
1183
-
1184
- it("should error if a given binding name is not in the configured kv namespaces", async () => {
1185
- writeWranglerConfig();
1186
- await expect(
1187
- runWrangler("kv:key get key --binding otherBinding")
1188
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1189
- `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
1190
- );
1191
- expect(std.out).toMatchInlineSnapshot(`
1192
- "
1193
- If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
1194
- `);
1195
- expect(std.err).toMatchInlineSnapshot(`
1196
- "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1197
-
1198
- "
1199
- `);
1200
- });
1201
-
1202
- describe("non-interactive", () => {
1203
- mockAccountId({ accountId: null });
1204
-
1205
- it("should error if there are multiple accounts available but not interactive on stdin", async () => {
1206
- mockGetMemberships([
1207
- { id: "xxx", account: { id: "1", name: "one" } },
1208
- { id: "yyy", account: { id: "2", name: "two" } },
1209
- ]);
1210
- setIsTTY({ stdin: false, stdout: true });
1211
- await expect(runWrangler("kv:key get key --namespace-id=xxxx"))
1212
- .rejects.toThrowErrorMatchingInlineSnapshot(`
1213
- "More than one account available but unable to select one in non-interactive mode.
1214
- Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
1215
- Available accounts are (\`<name>\`: \`<account_id>\`):
1216
- \`one\`: \`1\`
1217
- \`two\`: \`2\`"
1218
- `);
1219
- });
1220
-
1221
- it("should error if there are multiple accounts available but not interactive on stdout", async () => {
1222
- mockGetMemberships([
1223
- { id: "xxx", account: { id: "1", name: "one" } },
1224
- { id: "yyy", account: { id: "2", name: "two" } },
1225
- ]);
1226
- setIsTTY({ stdin: true, stdout: false });
1227
- await expect(runWrangler("kv:key get key --namespace-id=xxxx"))
1228
- .rejects.toThrowErrorMatchingInlineSnapshot(`
1229
- "More than one account available but unable to select one in non-interactive mode.
1230
- Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
1231
- Available accounts are (\`<name>\`: \`<account_id>\`):
1232
- \`one\`: \`1\`
1233
- \`two\`: \`2\`"
1234
- `);
1235
- });
1236
-
1237
- it("should recommend using a configuration if unable to fetch memberships", async () => {
1238
- msw.use(
1239
- rest.get("*/memberships", (req, res, ctx) => {
1240
- return res.once(
1241
- ctx.status(200),
1242
- ctx.json(
1243
- createFetchResult(null, false, [
1244
- {
1245
- code: 9109,
1246
- message: "Uauthorized to access requested resource",
1247
- },
1248
- ])
1249
- )
1250
- );
1251
- })
1252
- );
1253
- await expect(runWrangler("kv:key get key --namespace-id=xxxx"))
1254
- .rejects.toThrowErrorMatchingInlineSnapshot(`
1255
- "Failed to automatically retrieve account IDs for the logged in user.
1256
- 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\\""
1257
- `);
1258
- });
1259
-
1260
- it("should error if there are multiple accounts available but not interactive at all", async () => {
1261
- mockGetMemberships([
1262
- { id: "xxx", account: { id: "1", name: "one" } },
1263
- { id: "yyy", account: { id: "2", name: "two" } },
1264
- ]);
1265
- setIsTTY(false);
1266
- await expect(runWrangler("kv:key get key --namespace-id=xxxx"))
1267
- .rejects.toThrowErrorMatchingInlineSnapshot(`
1268
- "More than one account available but unable to select one in non-interactive mode.
1269
- Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
1270
- Available accounts are (\`<name>\`: \`<account_id>\`):
1271
- \`one\`: \`1\`
1272
- \`two\`: \`2\`"
1273
- `);
1274
- });
1275
- });
1276
- });
1277
-
1278
- describe("delete", () => {
1279
- function mockDeleteRequest(
1280
- expectedNamespaceId: string,
1281
- expectedKey: string
1282
- ) {
1283
- const requests = { count: 0 };
1284
- msw.use(
1285
- rest.delete(
1286
- "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
1287
- (req, res, ctx) => {
1288
- requests.count++;
1289
- expect(req.params.accountId).toEqual("some-account-id");
1290
- expect(req.params.namespaceId).toEqual(expectedNamespaceId);
1291
- expect(req.params.key).toEqual(expectedKey);
1292
- return res.once(
1293
- ctx.status(200),
1294
- ctx.json(createFetchResult(null))
1295
- );
1296
- }
1297
- )
1298
- );
1299
- return requests;
1300
- }
1301
-
1302
- it("should delete a key in a namespace specified by id", async () => {
1303
- const requests = mockDeleteRequest("some-namespace-id", "someKey");
1304
- await runWrangler(
1305
- `kv:key delete --namespace-id some-namespace-id someKey`
1306
- );
1307
- expect(requests.count).toEqual(1);
1308
- });
1309
-
1310
- it("should encode the key in the api request to delete a value", async () => {
1311
- const requests = mockDeleteRequest("voyager", "/NCC-74656");
1312
- await runWrangler(`kv:key delete --namespace-id voyager /NCC-74656`);
1313
-
1314
- expect(requests.count).toEqual(1);
1315
- expect(std.out).toMatchInlineSnapshot(
1316
- `"Deleting the key \\"/NCC-74656\\" on namespace voyager."`
1317
- );
1318
- expect(std.err).toMatchInlineSnapshot(`""`);
1319
- });
1320
-
1321
- it("should delete a key in a namespace specified by binding name", async () => {
1322
- writeWranglerConfig();
1323
- const requests = mockDeleteRequest("bound-id", "someKey");
1324
- await runWrangler(
1325
- `kv:key delete --binding someBinding --preview false someKey`
1326
- );
1327
- expect(requests.count).toEqual(1);
1328
- });
1329
-
1330
- it("should delete a key in a preview namespace specified by binding name", async () => {
1331
- writeWranglerConfig();
1332
- const requests = mockDeleteRequest("preview-bound-id", "someKey");
1333
- await runWrangler(
1334
- `kv:key delete --binding someBinding --preview someKey`
1335
- );
1336
- expect(requests.count).toEqual(1);
1337
- });
1338
-
1339
- it("should error if a given binding name is not in the configured kv namespaces", async () => {
1340
- writeWranglerConfig();
1341
- await expect(
1342
- runWrangler(`kv:key delete --binding otherBinding someKey`)
1343
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1344
- `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
1345
- );
1346
-
1347
- expect(std.err).toMatchInlineSnapshot(`
1348
- "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1349
-
1350
- "
1351
- `);
1352
- });
1353
-
1354
- it("should delete a key in a namespace specified by binding name in a given environment", async () => {
1355
- writeWranglerConfig();
1356
- const requests = mockDeleteRequest("env-bound-id", "someKey");
1357
- await runWrangler(
1358
- `kv:key delete --binding someBinding --env some-environment --preview false someKey`
1359
- );
1360
- expect(std.out).toMatchInlineSnapshot(
1361
- `"Deleting the key \\"someKey\\" on namespace env-bound-id."`
1362
- );
1363
- expect(std.err).toMatchInlineSnapshot(`""`);
1364
- expect(requests.count).toEqual(1);
1365
- });
1366
-
1367
- it("should delete a key in a preview namespace specified by binding name in a given environment", async () => {
1368
- writeWranglerConfig();
1369
- const requests = mockDeleteRequest("preview-env-bound-id", "someKey");
1370
- await runWrangler(
1371
- `kv:key delete --binding someBinding --env some-environment --preview someKey`
1372
- );
1373
- expect(requests.count).toEqual(1);
1374
- });
1375
- });
1376
- });
1377
-
1378
- describe("kv:bulk", () => {
1379
- describe("put", () => {
1380
- function mockPutRequest(
1381
- expectedNamespaceId: string,
1382
- expectedKeyValues: KeyValue[]
1383
- ) {
1384
- const requests = { count: 0 };
1385
- msw.use(
1386
- rest.put(
1387
- "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1388
- async (req, res, ctx) => {
1389
- requests.count++;
1390
- expect(req.params.accountId).toEqual("some-account-id");
1391
- expect(req.params.namespaceId).toEqual(expectedNamespaceId);
1392
- expect(await req.json()).toEqual(
1393
- expectedKeyValues.slice(
1394
- (requests.count - 1) * 5000,
1395
- requests.count * 5000
1396
- )
1397
- );
1398
- return res(ctx.status(200), ctx.json(createFetchResult(null)));
1399
- }
1400
- )
1401
- );
1402
- return requests;
1403
- }
1404
-
1405
- it("should put the key-values parsed from a file", async () => {
1406
- const keyValues: KeyValue[] = [
1407
- { key: "someKey1", value: "someValue1" },
1408
- { key: "ns:someKey2", value: "123", base64: true },
1409
- { key: "someKey3", value: "someValue3", expiration: 100 },
1410
- { key: "someKey4", value: "someValue4", expiration_ttl: 500 },
1411
- ];
1412
- writeFileSync("./keys.json", JSON.stringify(keyValues));
1413
- const requests = mockPutRequest("some-namespace-id", keyValues);
1414
- await runWrangler(
1415
- `kv:bulk put --namespace-id some-namespace-id keys.json`
1416
- );
1417
- expect(requests.count).toEqual(1);
1418
- expect(std.out).toMatchInlineSnapshot(`"Success!"`);
1419
- expect(std.warn).toMatchInlineSnapshot(`""`);
1420
- expect(std.err).toMatchInlineSnapshot(`""`);
1421
- });
1422
-
1423
- it("should put the key-values in batches of 5000 parsed from a file", async () => {
1424
- const keyValues: KeyValue[] = new Array(12000).fill({
1425
- key: "someKey1",
1426
- value: "someValue1",
1427
- });
1428
- writeFileSync("./keys.json", JSON.stringify(keyValues));
1429
- const requests = mockPutRequest("some-namespace-id", keyValues);
1430
- await runWrangler(
1431
- `kv:bulk put --namespace-id some-namespace-id keys.json`
1432
- );
1433
- expect(requests.count).toEqual(3);
1434
- expect(std.out).toMatchInlineSnapshot(`
1435
- "Uploaded 0% (0 out of 12,000)
1436
- Uploaded 41% (5,000 out of 12,000)
1437
- Uploaded 83% (10,000 out of 12,000)
1438
- Uploaded 100% (12,000 out of 12,000)
1439
- Success!"
1440
- `);
1441
- expect(std.warn).toMatchInlineSnapshot(`""`);
1442
- expect(std.err).toMatchInlineSnapshot(`""`);
1443
- });
1444
-
1445
- it("should error if the file is not a JSON array", async () => {
1446
- const keyValues = { key: "someKey1", value: "someValue1" };
1447
- writeFileSync("./keys.json", JSON.stringify(keyValues));
1448
- await expect(
1449
- runWrangler(`kv:bulk put --namespace-id some-namespace-id keys.json`)
1450
- ).rejects.toThrowErrorMatchingInlineSnapshot(`
1451
- "Unexpected JSON input from \\"keys.json\\".
1452
- Expected an array of key-value objects but got type \\"object\\"."
1453
- `);
1454
- expect(std.out).toMatchInlineSnapshot(`
1455
- "
1456
- If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
1457
- `);
1458
- expect(std.warn).toMatchInlineSnapshot(`""`);
1459
- });
1460
-
1461
- it("should error if the array contains items that are not key-value objects", async () => {
1462
- const keyValues = [
1463
- 123,
1464
- "a string",
1465
- { key: "someKey" },
1466
- { value: "someValue" },
1467
- // add a valid object here to make sure it's not included
1468
- { key: "someKey1", value: "someValue1" },
1469
- // this one will only add a warning
1470
- { key: "someKey1", value: "someValue1", invalid: true },
1471
- // back to the invalid ones
1472
- { key: 123, value: "somevalue" },
1473
- { key: "somekey", value: 123 },
1474
- { key: "someKey1", value: "someValue1", expiration: "string" },
1475
- { key: "someKey1", value: "someValue1", expiration_ttl: "string" },
1476
- {
1477
- key: 123,
1478
- value: {
1479
- a: {
1480
- nested: "object",
1481
- },
1482
- },
1483
- },
1484
- { key: "someKey1", value: "someValue1", metadata: 123 },
1485
- { key: "someKey1", value: "someValue1", base64: "string" },
1486
- ];
1487
- writeFileSync("./keys.json", JSON.stringify(keyValues));
1488
- await expect(
1489
- runWrangler(`kv:bulk put --namespace-id some-namespace-id keys.json`)
1490
- ).rejects.toThrowErrorMatchingInlineSnapshot(`
1491
- "Unexpected JSON input from \\"keys.json\\".
1492
- Each item in the array should be an object that matches:
1493
-
1494
- interface KeyValue {
1495
- key: string;
1496
- value: string;
1497
- expiration?: number;
1498
- expiration_ttl?: number;
1499
- metadata?: object;
1500
- base64?: boolean;
1501
- }
1502
-
1503
- The item at index 0 is 123
1504
- The item at index 1 is \\"a string\\"
1505
- The item at index 2 is {\\"key\\":\\"someKey\\"}
1506
- The item at index 3 is {\\"value\\":\\"someValue\\"}
1507
- The item at index 6 is {\\"key\\":123,\\"value\\":\\"somevalue\\"}
1508
- The item at index 7 is {\\"key\\":\\"somekey\\",\\"value\\":123}
1509
- The item at index 8 is {\\"key\\":\\"someKey1\\",\\"value\\":\\"someValue1\\",\\"expiration\\":\\"string\\"}
1510
- The item at index 9 is {\\"key\\":\\"someKey1\\",\\"value\\":\\"someValue1\\",\\"expiration_ttl\\":\\"string\\"}
1511
- The item at index 10 is {\\"key\\":123,\\"value\\":{\\"a\\":{\\"nested\\":\\"object\\"}}}
1512
- The item at index 11 is {\\"key\\":\\"someKey1\\",\\"value\\":\\"someValue1\\",\\"metadata\\":123}
1513
- The item at index 12 is {\\"key\\":\\"someKey1\\",\\"value\\":\\"someValue1\\",\\"base64\\":\\"string\\"}"
1514
- `);
1515
-
1516
- expect(std.out).toMatchInlineSnapshot(`
1517
- "
1518
- If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
1519
- `);
1520
- expect(std.warn).toMatchInlineSnapshot(`
1521
- "▲ [WARNING] Unexpected key-value properties in \\"keys.json\\".
1522
-
1523
- The item at index 5 contains unexpected properties: [\\"invalid\\"].
1524
-
1525
- "
1526
- `);
1527
- });
1528
- });
1529
-
1530
- describe("delete", () => {
1531
- function mockDeleteRequest(
1532
- expectedNamespaceId: string,
1533
- expectedKeys: string[]
1534
- ) {
1535
- const requests = { count: 0 };
1536
- msw.use(
1537
- rest.delete(
1538
- "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1539
- async (req, res, ctx) => {
1540
- requests.count++;
1541
- expect(req.params.accountId).toEqual("some-account-id");
1542
- expect(req.params.namespaceId).toEqual(expectedNamespaceId);
1543
- expect(req.headers.get("Content-Type")).toEqual(
1544
- "application/json"
1545
- );
1546
- expect(await req.json()).toEqual(
1547
- expectedKeys.slice(
1548
- (requests.count - 1) * 5000,
1549
- requests.count * 5000
1550
- )
1551
- );
1552
- return res(ctx.status(200), ctx.json(createFetchResult(null)));
1553
- }
1554
- )
1555
- );
1556
- return requests;
1557
- }
1558
-
1559
- it("should delete the keys parsed from a file", async () => {
1560
- const keys = ["someKey1", "ns:someKey2"];
1561
- writeFileSync("./keys.json", JSON.stringify(keys));
1562
- mockConfirm({
1563
- text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1564
- result: true,
1565
- });
1566
- const requests = mockDeleteRequest("some-namespace-id", keys);
1567
- await runWrangler(
1568
- `kv:bulk delete --namespace-id some-namespace-id keys.json`
1569
- );
1570
- expect(requests.count).toEqual(1);
1571
- expect(std.out).toMatchInlineSnapshot(`"Success!"`);
1572
- expect(std.warn).toMatchInlineSnapshot(`""`);
1573
- expect(std.err).toMatchInlineSnapshot(`""`);
1574
- });
1575
-
1576
- it("should delete the keys in batches of 5000 parsed from a file", async () => {
1577
- const keys = new Array(12000).fill("some-key");
1578
- writeFileSync("./keys.json", JSON.stringify(keys));
1579
- mockConfirm({
1580
- text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1581
- result: true,
1582
- });
1583
- const requests = mockDeleteRequest("some-namespace-id", keys);
1584
- await runWrangler(
1585
- `kv:bulk delete --namespace-id some-namespace-id keys.json`
1586
- );
1587
- expect(requests.count).toEqual(3);
1588
- expect(std.out).toMatchInlineSnapshot(`
1589
- "Deleted 0% (0 out of 12,000)
1590
- Deleted 41% (5,000 out of 12,000)
1591
- Deleted 83% (10,000 out of 12,000)
1592
- Deleted 100% (12,000 out of 12,000)
1593
- Success!"
1594
- `);
1595
- expect(std.warn).toMatchInlineSnapshot(`""`);
1596
- expect(std.err).toMatchInlineSnapshot(`""`);
1597
- });
1598
-
1599
- it("should not delete the keys if the user confirms no", async () => {
1600
- const keys = ["someKey1", "ns:someKey2"];
1601
- writeFileSync("./keys.json", JSON.stringify(keys));
1602
- mockConfirm({
1603
- text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1604
- result: false,
1605
- });
1606
- await runWrangler(
1607
- `kv:bulk delete --namespace-id some-namespace-id keys.json`
1608
- );
1609
- expect(std.out).toMatchInlineSnapshot(
1610
- `"Not deleting keys read from \\"keys.json\\"."`
1611
- );
1612
- expect(std.warn).toMatchInlineSnapshot(`""`);
1613
- expect(std.err).toMatchInlineSnapshot(`""`);
1614
- });
1615
-
1616
- it("should delete the keys without asking if --force is provided", async () => {
1617
- const keys = ["someKey1", "ns:someKey2"];
1618
- writeFileSync("./keys.json", JSON.stringify(keys));
1619
- const requests = mockDeleteRequest("some-namespace-id", keys);
1620
- await runWrangler(
1621
- `kv:bulk delete --namespace-id some-namespace-id keys.json --force`
1622
- );
1623
- expect(requests.count).toEqual(1);
1624
- expect(std.out).toMatchInlineSnapshot(`"Success!"`);
1625
- expect(std.warn).toMatchInlineSnapshot(`""`);
1626
- expect(std.err).toMatchInlineSnapshot(`""`);
1627
- });
1628
-
1629
- it("should delete the keys without asking if -f is provided", async () => {
1630
- const keys = ["someKey1", "ns:someKey2"];
1631
- writeFileSync("./keys.json", JSON.stringify(keys));
1632
- const requests = mockDeleteRequest("some-namespace-id", keys);
1633
- await runWrangler(
1634
- `kv:bulk delete --namespace-id some-namespace-id keys.json -f`
1635
- );
1636
- expect(requests.count).toEqual(1);
1637
- expect(std.out).toMatchInlineSnapshot(`"Success!"`);
1638
- expect(std.warn).toMatchInlineSnapshot(`""`);
1639
- expect(std.err).toMatchInlineSnapshot(`""`);
1640
- });
1641
-
1642
- it("should error if the file is not a JSON array", async () => {
1643
- const keys = 12354;
1644
- writeFileSync("./keys.json", JSON.stringify(keys));
1645
- mockConfirm({
1646
- text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1647
- result: true,
1648
- });
1649
- await expect(
1650
- runWrangler(
1651
- `kv:bulk delete --namespace-id some-namespace-id keys.json`
1652
- )
1653
- ).rejects.toThrowErrorMatchingInlineSnapshot(`
1654
- "Unexpected JSON input from \\"keys.json\\".
1655
- Expected an array of strings but got:
1656
- 12354"
1657
- `);
1658
- expect(std.out).toMatchInlineSnapshot(`
1659
- "
1660
- If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
1661
- `);
1662
- expect(std.warn).toMatchInlineSnapshot(`""`);
1663
- });
1664
-
1665
- it("should error if the file contains non-string items", async () => {
1666
- const keys = ["good", 12354, { key: "someKey" }, null];
1667
- writeFileSync("./keys.json", JSON.stringify(keys));
1668
- mockConfirm({
1669
- text: `Are you sure you want to delete all the keys read from "keys.json" from kv-namespace with id "some-namespace-id"?`,
1670
- result: true,
1671
- });
1672
- await expect(
1673
- runWrangler(
1674
- `kv:bulk delete --namespace-id some-namespace-id keys.json`
1675
- )
1676
- ).rejects.toThrowErrorMatchingInlineSnapshot(`
1677
- "Unexpected JSON input from \\"keys.json\\".
1678
- Expected an array of strings.
1679
- The item at index 1 is type: \\"number\\" - 12354
1680
- The item at index 2 is type: \\"object\\" - {\\"key\\":\\"someKey\\"}
1681
- The item at index 3 is type: \\"object\\" - null"
1682
- `);
1683
- expect(std.out).toMatchInlineSnapshot(`
1684
- "
1685
- If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
1686
- `);
1687
- expect(std.warn).toMatchInlineSnapshot(`""`);
1688
- });
1689
- });
1690
- });
1691
- });
1692
-
1693
- function writeWranglerConfig() {
1694
- writeFileSync(
1695
- "./wrangler.toml",
1696
- [
1697
- 'name = "other-worker"',
1698
- "kv_namespaces = [",
1699
- ' { binding = "someBinding", id = "bound-id", preview_id = "preview-bound-id" }',
1700
- "]",
1701
- "",
1702
- "[env.some-environment]",
1703
- "kv_namespaces = [",
1704
- ' { binding = "someBinding", id = "env-bound-id", preview_id = "preview-env-bound-id" }',
1705
- "]",
1706
- ].join("\n"),
1707
- "utf-8"
1708
- );
1709
- }
1710
-
1711
- function setMockFetchKVGetValue(
1712
- accountId: string,
1713
- namespaceId: string,
1714
- key: string,
1715
- value: string | Buffer
1716
- ) {
1717
- msw.use(
1718
- rest.get(
1719
- "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
1720
- (req, res, ctx) => {
1721
- console.dir(req.params);
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
- }