wrangler 0.0.0-ecc8b172 → 0.0.0-ecded1af1

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 (250) hide show
  1. package/README.md +17 -19
  2. package/bin/wrangler.js +24 -93
  3. package/config-schema.json +3115 -0
  4. package/package.json +197 -190
  5. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  6. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  7. package/templates/__tests__/tsconfig.json +8 -0
  8. package/templates/checked-fetch.js +19 -6
  9. package/templates/facade.d.ts +19 -0
  10. package/templates/gitignore +2 -3
  11. package/templates/init-tests/test-jest-new-worker.js +3 -5
  12. package/templates/init-tests/test-vitest-new-worker.js +4 -6
  13. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  14. package/templates/middleware/common.ts +9 -4
  15. package/templates/middleware/loader-modules.ts +94 -44
  16. package/templates/middleware/loader-sw.ts +48 -32
  17. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  18. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  19. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  21. package/templates/middleware/middleware-scheduled.ts +15 -1
  22. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  23. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  24. package/templates/modules-watch-stub.js +4 -0
  25. package/templates/new-worker-scheduled.ts +1 -1
  26. package/templates/new-worker.ts +4 -1
  27. package/templates/pages-dev-pipeline.ts +10 -13
  28. package/templates/pages-dev-util.ts +55 -0
  29. package/templates/pages-shim.ts +1 -1
  30. package/templates/pages-template-plugin.ts +37 -14
  31. package/templates/pages-template-worker.ts +33 -7
  32. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  33. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  34. package/templates/tsconfig-sanity.ts +11 -0
  35. package/templates/tsconfig.init.json +22 -0
  36. package/templates/tsconfig.json +5 -102
  37. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  38. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  39. package/wrangler-dist/ProxyWorker.js +241 -0
  40. package/wrangler-dist/ProxyWorker.js.map +6 -0
  41. package/wrangler-dist/cli.d.ts +26095 -471
  42. package/wrangler-dist/cli.js +178062 -122008
  43. package/wrangler-dist/wasm-sync.wasm +0 -0
  44. package/Cloudflare_CA.pem +0 -18
  45. package/import_meta_url.js +0 -3
  46. package/miniflare-config-stubs/.env.empty +0 -0
  47. package/miniflare-config-stubs/package.empty.json +0 -1
  48. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  49. package/miniflare-dist/index.mjs +0 -6310
  50. package/src/__tests__/api-dev.test.ts +0 -209
  51. package/src/__tests__/api-devregistry.test.js +0 -56
  52. package/src/__tests__/api.test.ts +0 -80
  53. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  54. package/src/__tests__/config-cache.test.ts +0 -42
  55. package/src/__tests__/configuration.test.ts +0 -3864
  56. package/src/__tests__/d1.test.ts +0 -70
  57. package/src/__tests__/delete.test.ts +0 -116
  58. package/src/__tests__/dev.test.tsx +0 -1357
  59. package/src/__tests__/dialogs.test.tsx +0 -40
  60. package/src/__tests__/generate.test.ts +0 -235
  61. package/src/__tests__/get-host-from-url.test.ts +0 -16
  62. package/src/__tests__/guess-worker-format.test.ts +0 -85
  63. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  64. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  65. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  66. package/src/__tests__/helpers/mock-bin.ts +0 -36
  67. package/src/__tests__/helpers/mock-cfetch.ts +0 -333
  68. package/src/__tests__/helpers/mock-console.ts +0 -106
  69. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  70. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -8
  71. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  72. package/src/__tests__/helpers/mock-istty.ts +0 -74
  73. package/src/__tests__/helpers/mock-known-routes.ts +0 -7
  74. package/src/__tests__/helpers/mock-kv.ts +0 -33
  75. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -252
  76. package/src/__tests__/helpers/mock-process.ts +0 -39
  77. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  78. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  79. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -104
  80. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -36
  81. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -80
  82. package/src/__tests__/helpers/msw/handlers/script.ts +0 -75
  83. package/src/__tests__/helpers/msw/handlers/user.ts +0 -63
  84. package/src/__tests__/helpers/msw/index.ts +0 -15
  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/worker-scripts/child-wrangler.toml +0 -1
  88. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  89. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  90. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
  91. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  92. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  93. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  94. package/src/__tests__/https-options.test.ts +0 -163
  95. package/src/__tests__/index.test.ts +0 -251
  96. package/src/__tests__/init.test.ts +0 -2816
  97. package/src/__tests__/jest.setup.ts +0 -191
  98. package/src/__tests__/kv.test.ts +0 -1675
  99. package/src/__tests__/logger.test.ts +0 -141
  100. package/src/__tests__/metrics.test.ts +0 -450
  101. package/src/__tests__/middleware.scheduled.test.ts +0 -135
  102. package/src/__tests__/middleware.test.ts +0 -726
  103. package/src/__tests__/package-manager.test.ts +0 -353
  104. package/src/__tests__/pages.test.ts +0 -2111
  105. package/src/__tests__/parse.test.ts +0 -330
  106. package/src/__tests__/paths.test.ts +0 -39
  107. package/src/__tests__/publish.test.ts +0 -7119
  108. package/src/__tests__/pubsub.test.ts +0 -367
  109. package/src/__tests__/r2.test.ts +0 -371
  110. package/src/__tests__/route.test.ts +0 -45
  111. package/src/__tests__/secret.test.ts +0 -644
  112. package/src/__tests__/tail.test.ts +0 -800
  113. package/src/__tests__/test-old-node-version.js +0 -31
  114. package/src/__tests__/user.test.ts +0 -155
  115. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  116. package/src/__tests__/validate-dev-props.test.ts +0 -56
  117. package/src/__tests__/version.test.ts +0 -35
  118. package/src/__tests__/whoami.test.tsx +0 -278
  119. package/src/__tests__/worker-namespace.test.ts +0 -321
  120. package/src/abort.d.ts +0 -3
  121. package/src/api/dev.ts +0 -208
  122. package/src/api/index.ts +0 -2
  123. package/src/bundle-reporter.tsx +0 -29
  124. package/src/bundle.ts +0 -756
  125. package/src/cfetch/index.ts +0 -160
  126. package/src/cfetch/internal.ts +0 -263
  127. package/src/cli.ts +0 -29
  128. package/src/config/README.md +0 -107
  129. package/src/config/config.ts +0 -275
  130. package/src/config/diagnostics.ts +0 -80
  131. package/src/config/environment.ts +0 -505
  132. package/src/config/index.ts +0 -293
  133. package/src/config/validation-helpers.ts +0 -597
  134. package/src/config/validation.ts +0 -1930
  135. package/src/config-cache.ts +0 -85
  136. package/src/create-worker-preview.ts +0 -275
  137. package/src/create-worker-upload-form.ts +0 -301
  138. package/src/d1/backups.tsx +0 -212
  139. package/src/d1/create.tsx +0 -54
  140. package/src/d1/delete.tsx +0 -56
  141. package/src/d1/execute.tsx +0 -294
  142. package/src/d1/formatTimeAgo.ts +0 -14
  143. package/src/d1/index.ts +0 -75
  144. package/src/d1/list.tsx +0 -48
  145. package/src/d1/options.ts +0 -12
  146. package/src/d1/types.tsx +0 -14
  147. package/src/d1/utils.ts +0 -39
  148. package/src/delete.ts +0 -86
  149. package/src/deprecated/index.ts +0 -453
  150. package/src/dev/dev-vars.ts +0 -36
  151. package/src/dev/dev.tsx +0 -575
  152. package/src/dev/get-local-persistence-path.tsx +0 -31
  153. package/src/dev/local.tsx +0 -686
  154. package/src/dev/remote.tsx +0 -601
  155. package/src/dev/start-server.ts +0 -506
  156. package/src/dev/use-esbuild.ts +0 -197
  157. package/src/dev/validate-dev-props.ts +0 -31
  158. package/src/dev-registry.tsx +0 -201
  159. package/src/dev.tsx +0 -865
  160. package/src/dialogs.tsx +0 -163
  161. package/src/dispatch-namespace.ts +0 -206
  162. package/src/durable.ts +0 -102
  163. package/src/entry.ts +0 -329
  164. package/src/environment-variables.ts +0 -50
  165. package/src/errors.ts +0 -11
  166. package/src/git-client.ts +0 -124
  167. package/src/global-wrangler-config-path.ts +0 -26
  168. package/src/https-options.ts +0 -127
  169. package/src/index.tsx +0 -582
  170. package/src/init.ts +0 -970
  171. package/src/inspect.ts +0 -747
  172. package/src/intl-polyfill.d.ts +0 -139
  173. package/src/is-ci.ts +0 -14
  174. package/src/is-interactive.ts +0 -12
  175. package/src/kv/helpers.ts +0 -430
  176. package/src/kv/index.ts +0 -645
  177. package/src/logger.ts +0 -80
  178. package/src/metrics/index.ts +0 -5
  179. package/src/metrics/metrics-config.ts +0 -239
  180. package/src/metrics/metrics-dispatcher.ts +0 -96
  181. package/src/metrics/metrics-usage-headers.ts +0 -24
  182. package/src/metrics/send-event.ts +0 -94
  183. package/src/miniflare-cli/README.md +0 -30
  184. package/src/miniflare-cli/assets.ts +0 -215
  185. package/src/miniflare-cli/index.ts +0 -208
  186. package/src/miniflare-cli/request-context.ts +0 -40
  187. package/src/module-collection.ts +0 -267
  188. package/src/open-in-browser.ts +0 -17
  189. package/src/package-manager.ts +0 -219
  190. package/src/pages/build.tsx +0 -238
  191. package/src/pages/constants.ts +0 -16
  192. package/src/pages/deployments.tsx +0 -106
  193. package/src/pages/dev.tsx +0 -689
  194. package/src/pages/errors.ts +0 -66
  195. package/src/pages/functions/buildPlugin.ts +0 -110
  196. package/src/pages/functions/buildWorker.ts +0 -156
  197. package/src/pages/functions/filepath-routing.test.ts +0 -234
  198. package/src/pages/functions/filepath-routing.ts +0 -189
  199. package/src/pages/functions/identifiers.ts +0 -78
  200. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  201. package/src/pages/functions/routes-consolidation.ts +0 -73
  202. package/src/pages/functions/routes-transformation.test.ts +0 -282
  203. package/src/pages/functions/routes-transformation.ts +0 -115
  204. package/src/pages/functions/routes-validation.test.ts +0 -403
  205. package/src/pages/functions/routes-validation.ts +0 -202
  206. package/src/pages/functions/routes.ts +0 -151
  207. package/src/pages/functions.tsx +0 -82
  208. package/src/pages/hash.tsx +0 -13
  209. package/src/pages/index.tsx +0 -94
  210. package/src/pages/projects.tsx +0 -169
  211. package/src/pages/publish.tsx +0 -436
  212. package/src/pages/types.ts +0 -49
  213. package/src/pages/upload.tsx +0 -398
  214. package/src/pages/utils.ts +0 -12
  215. package/src/parse.ts +0 -286
  216. package/src/paths.ts +0 -71
  217. package/src/proxy.ts +0 -657
  218. package/src/publish/index.ts +0 -267
  219. package/src/publish/publish.ts +0 -913
  220. package/src/pubsub/index.ts +0 -286
  221. package/src/pubsub/pubsub-commands.tsx +0 -623
  222. package/src/r2/helpers.ts +0 -131
  223. package/src/r2/index.ts +0 -277
  224. package/src/routes.ts +0 -140
  225. package/src/secret/index.ts +0 -403
  226. package/src/selfsigned.d.ts +0 -29
  227. package/src/sites.tsx +0 -372
  228. package/src/tail/createTail.ts +0 -315
  229. package/src/tail/filters.ts +0 -277
  230. package/src/tail/index.ts +0 -217
  231. package/src/tail/printing.ts +0 -107
  232. package/src/update-check.ts +0 -19
  233. package/src/user/choose-account.tsx +0 -69
  234. package/src/user/env-vars.ts +0 -46
  235. package/src/user/generate-auth-url.ts +0 -33
  236. package/src/user/generate-random-state.ts +0 -16
  237. package/src/user/index.ts +0 -3
  238. package/src/user/user.tsx +0 -1192
  239. package/src/utils/collectKeyValues.ts +0 -14
  240. package/src/whoami.tsx +0 -169
  241. package/src/worker.ts +0 -234
  242. package/src/yargs-types.ts +0 -7
  243. package/src/zones.ts +0 -165
  244. package/templates/d1-beta-facade.js +0 -174
  245. package/templates/experimental-local-cache-stubs.js +0 -27
  246. package/templates/first-party-worker-module-facade.ts +0 -18
  247. package/templates/format-dev-errors.ts +0 -32
  248. package/templates/serve-static-assets.ts +0 -63
  249. package/templates/service-bindings-module-facade.js +0 -54
  250. package/templates/service-bindings-sw-facade.js +0 -42
package/src/kv/index.ts DELETED
@@ -1,645 +0,0 @@
1
- import { StringDecoder } from "node:string_decoder";
2
- import { readConfig } from "../config";
3
- import { confirm } from "../dialogs";
4
- import {
5
- demandOneOfOption,
6
- printWranglerBanner,
7
- CommandLineArgsError,
8
- } from "../index";
9
- import { logger } from "../logger";
10
- import * as metrics from "../metrics";
11
- import { parseJSON, readFileSync, readFileSyncToBuffer } from "../parse";
12
- import { requireAuth } from "../user";
13
- import {
14
- createKVNamespace,
15
- deleteKVNamespace,
16
- getKVNamespaceId,
17
- isValidKVNamespaceBinding,
18
- listKVNamespaces,
19
- deleteKVBulkKeyValue,
20
- deleteKVKeyValue,
21
- getKVKeyValue,
22
- isKVKeyValue,
23
- listKVNamespaceKeys,
24
- putKVBulkKeyValue,
25
- putKVKeyValue,
26
- unexpectedKVKeyValueProps,
27
- } from "./helpers";
28
- import type { ConfigPath } from "../index";
29
- import type { KeyValue } from "./helpers";
30
- import type { BuilderCallback, Argv } from "yargs";
31
-
32
- export const kvNamespace: BuilderCallback<unknown, unknown> = (
33
- kvYargs: Argv
34
- ) => {
35
- return kvYargs
36
- .command(
37
- "create <namespace>",
38
- "Create a new namespace",
39
- (yargs) => {
40
- return yargs
41
- .positional("namespace", {
42
- describe: "The name of the new namespace",
43
- type: "string",
44
- demandOption: true,
45
- })
46
- .option("env", {
47
- type: "string",
48
- requiresArg: true,
49
- describe: "Perform on a specific environment",
50
- alias: "e",
51
- })
52
- .option("preview", {
53
- type: "boolean",
54
- describe: "Interact with a preview namespace",
55
- });
56
- },
57
- async (args) => {
58
- await printWranglerBanner();
59
-
60
- if (!isValidKVNamespaceBinding(args.namespace)) {
61
- throw new CommandLineArgsError(
62
- `The namespace binding name "${args.namespace}" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.`
63
- );
64
- }
65
-
66
- const config = readConfig(args.config as ConfigPath, args);
67
- if (!config.name) {
68
- logger.warn(
69
- "No configured name present, using `worker` as a prefix for the title"
70
- );
71
- }
72
-
73
- const name = config.name || "worker";
74
- const environment = args.env ? `-${args.env}` : "";
75
- const preview = args.preview ? "_preview" : "";
76
- const title = `${name}${environment}-${args.namespace}${preview}`;
77
-
78
- const accountId = await requireAuth(config);
79
-
80
- // TODO: generate a binding name stripping non alphanumeric chars
81
-
82
- logger.log(`🌀 Creating namespace with title "${title}"`);
83
- const namespaceId = await createKVNamespace(accountId, title);
84
- await metrics.sendMetricsEvent("create kv namespace", {
85
- sendMetrics: config.send_metrics,
86
- });
87
-
88
- logger.log("✨ Success!");
89
- const envString = args.env ? ` under [env.${args.env}]` : "";
90
- const previewString = args.preview ? "preview_" : "";
91
- logger.log(
92
- `Add the following to your configuration file in your kv_namespaces array${envString}:`
93
- );
94
- logger.log(
95
- `{ binding = "${args.namespace}", ${previewString}id = "${namespaceId}" }`
96
- );
97
-
98
- // TODO: automatically write this block to the wrangler.toml config file??
99
- }
100
- )
101
- .command(
102
- "list",
103
- "Outputs a list of all KV namespaces associated with your account id.",
104
- {},
105
- async (args) => {
106
- const config = readConfig(args.config as ConfigPath, args);
107
-
108
- const accountId = await requireAuth(config);
109
-
110
- // TODO: we should show bindings if they exist for given ids
111
-
112
- logger.log(
113
- JSON.stringify(await listKVNamespaces(accountId), null, " ")
114
- );
115
- await metrics.sendMetricsEvent("list kv namespaces", {
116
- sendMetrics: config.send_metrics,
117
- });
118
- }
119
- )
120
- .command(
121
- "delete",
122
- "Deletes a given namespace.",
123
- (yargs) => {
124
- return yargs
125
- .option("binding", {
126
- type: "string",
127
- requiresArg: true,
128
- describe: "The name of the namespace to delete",
129
- })
130
- .option("namespace-id", {
131
- type: "string",
132
- requiresArg: true,
133
- describe: "The id of the namespace to delete",
134
- })
135
- .check(demandOneOfOption("binding", "namespace-id"))
136
- .option("env", {
137
- type: "string",
138
- requiresArg: true,
139
- describe: "Perform on a specific environment",
140
- alias: "e",
141
- })
142
- .option("preview", {
143
- type: "boolean",
144
- describe: "Interact with a preview namespace",
145
- });
146
- },
147
- async (args) => {
148
- await printWranglerBanner();
149
- const config = readConfig(args.config as ConfigPath, args);
150
-
151
- let id;
152
- try {
153
- id = getKVNamespaceId(args, config);
154
- } catch (e) {
155
- throw new CommandLineArgsError(
156
- "Not able to delete namespace.\n" + ((e as Error).message ?? e)
157
- );
158
- }
159
-
160
- const accountId = await requireAuth(config);
161
-
162
- logger.log(`Deleting KV namespace ${id}.`);
163
- await deleteKVNamespace(accountId, id);
164
- logger.log(`Deleted KV namespace ${id}.`);
165
- await metrics.sendMetricsEvent("delete kv namespace", {
166
- sendMetrics: config.send_metrics,
167
- });
168
-
169
- // TODO: recommend they remove it from wrangler.toml
170
-
171
- // test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388
172
- // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n]
173
- // n
174
- // 💁 Not deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388
175
- // ➜ test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388
176
- // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n]
177
- // y
178
- // 🌀 Deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388
179
- // ✨ Success
180
- // ⚠️ Make sure to remove this "kv-namespace" entry from your configuration file!
181
- // ➜ test-mf
182
-
183
- // TODO: do it automatically
184
-
185
- // TODO: delete the preview namespace as well?
186
- }
187
- );
188
- };
189
-
190
- export const kvKey: BuilderCallback<unknown, unknown> = (kvYargs: Argv) => {
191
- return kvYargs
192
- .command(
193
- "put <key> [value]",
194
- "Writes a single key/value pair to the given namespace.",
195
- (yargs) => {
196
- return yargs
197
- .positional("key", {
198
- type: "string",
199
- describe: "The key to write to",
200
- demandOption: true,
201
- })
202
- .positional("value", {
203
- type: "string",
204
- describe: "The value to write",
205
- })
206
- .option("binding", {
207
- type: "string",
208
- requiresArg: true,
209
- describe: "The binding of the namespace to write to",
210
- })
211
- .option("namespace-id", {
212
- type: "string",
213
- requiresArg: true,
214
- describe: "The id of the namespace to write to",
215
- })
216
- .check(demandOneOfOption("binding", "namespace-id"))
217
- .option("env", {
218
- type: "string",
219
- requiresArg: true,
220
- describe: "Perform on a specific environment",
221
- alias: "e",
222
- })
223
- .option("preview", {
224
- type: "boolean",
225
- describe: "Interact with a preview namespace",
226
- })
227
- .option("ttl", {
228
- type: "number",
229
- describe: "Time for which the entries should be visible",
230
- })
231
- .option("expiration", {
232
- type: "number",
233
- describe: "Time since the UNIX epoch after which the entry expires",
234
- })
235
- .option("metadata", {
236
- type: "string",
237
- describe: "Arbitrary JSON that is associated with a key",
238
- coerce: (jsonStr: string): KeyValue["metadata"] => {
239
- try {
240
- return JSON.parse(jsonStr);
241
- } catch (_) {}
242
- },
243
- })
244
- .option("path", {
245
- type: "string",
246
- requiresArg: true,
247
- describe: "Read value from the file at a given path",
248
- })
249
- .check(demandOneOfOption("value", "path"));
250
- },
251
- async ({ key, ttl, expiration, metadata, ...args }) => {
252
- await printWranglerBanner();
253
- const config = readConfig(args.config as ConfigPath, args);
254
- const namespaceId = getKVNamespaceId(args, config);
255
- // One of `args.path` and `args.value` must be defined
256
- const value = args.path
257
- ? readFileSyncToBuffer(args.path)
258
- : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
259
- args.value!;
260
-
261
- const metadataLog = metadata
262
- ? ` with metadata "${JSON.stringify(metadata)}"`
263
- : "";
264
-
265
- if (args.path) {
266
- logger.log(
267
- `Writing the contents of ${args.path} to the key "${key}" on namespace ${namespaceId}${metadataLog}.`
268
- );
269
- } else {
270
- logger.log(
271
- `Writing the value "${value}" to key "${key}" on namespace ${namespaceId}${metadataLog}.`
272
- );
273
- }
274
-
275
- const accountId = await requireAuth(config);
276
-
277
- await putKVKeyValue(accountId, namespaceId, {
278
- key,
279
- value,
280
- expiration,
281
- expiration_ttl: ttl,
282
- metadata: metadata as KeyValue["metadata"],
283
- });
284
- await metrics.sendMetricsEvent("write kv key-value", {
285
- sendMetrics: config.send_metrics,
286
- });
287
- }
288
- )
289
- .command(
290
- "list",
291
- "Outputs a list of all keys in a given namespace.",
292
- (yargs) => {
293
- return yargs
294
- .option("binding", {
295
- type: "string",
296
- requiresArg: true,
297
- describe: "The name of the namespace to list",
298
- })
299
- .option("namespace-id", {
300
- type: "string",
301
- requiresArg: true,
302
- describe: "The id of the namespace to list",
303
- })
304
- .check(demandOneOfOption("binding", "namespace-id"))
305
- .option("env", {
306
- type: "string",
307
- requiresArg: true,
308
- describe: "Perform on a specific environment",
309
- alias: "e",
310
- })
311
- .option("preview", {
312
- type: "boolean",
313
- // In the case of listing keys we will default to non-preview mode
314
- default: false,
315
- describe: "Interact with a preview namespace",
316
- })
317
- .option("prefix", {
318
- type: "string",
319
- requiresArg: true,
320
- describe: "A prefix to filter listed keys",
321
- });
322
- },
323
- async ({ prefix, ...args }) => {
324
- // TODO: support for limit+cursor (pagination)
325
- const config = readConfig(args.config as ConfigPath, args);
326
- const namespaceId = getKVNamespaceId(args, config);
327
-
328
- const accountId = await requireAuth(config);
329
-
330
- const results = await listKVNamespaceKeys(
331
- accountId,
332
- namespaceId,
333
- prefix
334
- );
335
- logger.log(JSON.stringify(results, undefined, 2));
336
- await metrics.sendMetricsEvent("list kv keys", {
337
- sendMetrics: config.send_metrics,
338
- });
339
- }
340
- )
341
- .command(
342
- "get <key>",
343
- "Reads a single value by key from the given namespace.",
344
- (yargs) => {
345
- return yargs
346
- .positional("key", {
347
- describe: "The key value to get.",
348
- type: "string",
349
- demandOption: true,
350
- })
351
- .option("binding", {
352
- type: "string",
353
- requiresArg: true,
354
- describe: "The name of the namespace to get from",
355
- })
356
- .option("namespace-id", {
357
- type: "string",
358
- requiresArg: true,
359
- describe: "The id of the namespace to get from",
360
- })
361
- .check(demandOneOfOption("binding", "namespace-id"))
362
- .option("env", {
363
- type: "string",
364
- requiresArg: true,
365
- describe: "Perform on a specific environment",
366
- alias: "e",
367
- })
368
- .option("preview", {
369
- type: "boolean",
370
- describe: "Interact with a preview namespace",
371
- })
372
- .option("preview", {
373
- type: "boolean",
374
- // In the case of getting key values we will default to non-preview mode
375
- default: false,
376
- describe: "Interact with a preview namespace",
377
- })
378
- .option("text", {
379
- type: "boolean",
380
- default: false,
381
- describe: "Decode the returned value as a utf8 string",
382
- });
383
- },
384
- async ({ key, ...args }) => {
385
- const config = readConfig(args.config as ConfigPath, args);
386
- const namespaceId = getKVNamespaceId(args, config);
387
-
388
- const accountId = await requireAuth(config);
389
- const bufferKVValue = Buffer.from(
390
- await getKVKeyValue(accountId, namespaceId, key)
391
- );
392
-
393
- if (args.text) {
394
- const decoder = new StringDecoder("utf8");
395
- logger.log(decoder.write(bufferKVValue));
396
- } else {
397
- process.stdout.write(bufferKVValue);
398
- }
399
- await metrics.sendMetricsEvent("read kv value", {
400
- sendMetrics: config.send_metrics,
401
- });
402
- }
403
- )
404
- .command(
405
- "delete <key>",
406
- "Removes a single key value pair from the given namespace.",
407
- (yargs) => {
408
- return yargs
409
- .positional("key", {
410
- describe: "The key value to delete",
411
- type: "string",
412
- demandOption: true,
413
- })
414
- .option("binding", {
415
- type: "string",
416
- requiresArg: true,
417
- describe: "The name of the namespace to delete from",
418
- })
419
- .option("namespace-id", {
420
- type: "string",
421
- requiresArg: true,
422
- describe: "The id of the namespace to delete from",
423
- })
424
- .check(demandOneOfOption("binding", "namespace-id"))
425
- .option("env", {
426
- type: "string",
427
- requiresArg: true,
428
- describe: "Perform on a specific environment",
429
- alias: "e",
430
- })
431
- .option("preview", {
432
- type: "boolean",
433
- describe: "Interact with a preview namespace",
434
- });
435
- },
436
- async ({ key, ...args }) => {
437
- await printWranglerBanner();
438
- const config = readConfig(args.config as ConfigPath, args);
439
- const namespaceId = getKVNamespaceId(args, config);
440
-
441
- logger.log(`Deleting the key "${key}" on namespace ${namespaceId}.`);
442
-
443
- const accountId = await requireAuth(config);
444
-
445
- await deleteKVKeyValue(accountId, namespaceId, key);
446
- await metrics.sendMetricsEvent("delete kv key-value", {
447
- sendMetrics: config.send_metrics,
448
- });
449
- }
450
- );
451
- };
452
-
453
- export const kvBulk: BuilderCallback<unknown, unknown> = (kvYargs: Argv) => {
454
- return kvYargs
455
- .command(
456
- "put <filename>",
457
- "Upload multiple key-value pairs to a namespace",
458
- (yargs) => {
459
- return yargs
460
- .positional("filename", {
461
- describe: `The JSON file of key-value pairs to upload, in form [{"key":..., "value":...}"...]`,
462
- type: "string",
463
- demandOption: true,
464
- })
465
- .option("binding", {
466
- type: "string",
467
- requiresArg: true,
468
- describe: "The name of the namespace to insert values into",
469
- })
470
- .option("namespace-id", {
471
- type: "string",
472
- requiresArg: true,
473
- describe: "The id of the namespace to insert values into",
474
- })
475
- .check(demandOneOfOption("binding", "namespace-id"))
476
- .option("env", {
477
- type: "string",
478
- requiresArg: true,
479
- describe: "Perform on a specific environment",
480
- alias: "e",
481
- })
482
- .option("preview", {
483
- type: "boolean",
484
- describe: "Interact with a preview namespace",
485
- });
486
- },
487
- async ({ filename, ...args }) => {
488
- await printWranglerBanner();
489
- // The simplest implementation I could think of.
490
- // This could be made more efficient with a streaming parser/uploader
491
- // but we'll do that in the future if needed.
492
-
493
- const config = readConfig(args.config as ConfigPath, args);
494
- const namespaceId = getKVNamespaceId(args, config);
495
- const content = parseJSON(readFileSync(filename), filename);
496
-
497
- if (!Array.isArray(content)) {
498
- throw new Error(
499
- `Unexpected JSON input from "${filename}".\n` +
500
- `Expected an array of key-value objects but got type "${typeof content}".`
501
- );
502
- }
503
-
504
- const errors: string[] = [];
505
- const warnings: string[] = [];
506
- for (let i = 0; i < content.length; i++) {
507
- const keyValue = content[i];
508
- if (!isKVKeyValue(keyValue)) {
509
- errors.push(
510
- `The item at index ${i} is ${JSON.stringify(keyValue)}`
511
- );
512
- } else {
513
- const props = unexpectedKVKeyValueProps(keyValue);
514
- if (props.length > 0) {
515
- warnings.push(
516
- `The item at index ${i} contains unexpected properties: ${JSON.stringify(
517
- props
518
- )}.`
519
- );
520
- }
521
- }
522
- }
523
- if (warnings.length > 0) {
524
- logger.warn(
525
- `Unexpected key-value properties in "${filename}".\n` +
526
- warnings.join("\n")
527
- );
528
- }
529
- if (errors.length > 0) {
530
- throw new Error(
531
- `Unexpected JSON input from "${filename}".\n` +
532
- `Each item in the array should be an object that matches:\n\n` +
533
- `interface KeyValue {\n` +
534
- ` key: string;\n` +
535
- ` value: string;\n` +
536
- ` expiration?: number;\n` +
537
- ` expiration_ttl?: number;\n` +
538
- ` metadata?: object;\n` +
539
- ` base64?: boolean;\n` +
540
- `}\n\n` +
541
- errors.join("\n")
542
- );
543
- }
544
-
545
- const accountId = await requireAuth(config);
546
- await putKVBulkKeyValue(accountId, namespaceId, content);
547
- await metrics.sendMetricsEvent("write kv key-values (bulk)", {
548
- sendMetrics: config.send_metrics,
549
- });
550
-
551
- logger.log("Success!");
552
- }
553
- )
554
- .command(
555
- "delete <filename>",
556
- "Delete multiple key-value pairs from a namespace",
557
- (yargs) => {
558
- return yargs
559
- .positional("filename", {
560
- describe: `The JSON file of keys to delete, in the form ["key1", "key2", ...]`,
561
- type: "string",
562
- demandOption: true,
563
- })
564
- .option("binding", {
565
- type: "string",
566
- requiresArg: true,
567
- describe: "The name of the namespace to delete from",
568
- })
569
- .option("namespace-id", {
570
- type: "string",
571
- requiresArg: true,
572
- describe: "The id of the namespace to delete from",
573
- })
574
- .check(demandOneOfOption("binding", "namespace-id"))
575
- .option("env", {
576
- type: "string",
577
- requiresArg: true,
578
- describe: "Perform on a specific environment",
579
- alias: "e",
580
- })
581
- .option("preview", {
582
- type: "boolean",
583
- describe: "Interact with a preview namespace",
584
- })
585
- .option("force", {
586
- type: "boolean",
587
- alias: "f",
588
- describe: "Do not ask for confirmation before deleting",
589
- });
590
- },
591
- async ({ filename, ...args }) => {
592
- await printWranglerBanner();
593
- const config = readConfig(args.config as ConfigPath, args);
594
- const namespaceId = getKVNamespaceId(args, config);
595
-
596
- if (!args.force) {
597
- const result = await confirm(
598
- `Are you sure you want to delete all the keys read from "${filename}" from kv-namespace with id "${namespaceId}"?`
599
- );
600
- if (!result) {
601
- logger.log(`Not deleting keys read from "${filename}".`);
602
- return;
603
- }
604
- }
605
-
606
- const content = parseJSON(readFileSync(filename), filename) as string[];
607
-
608
- if (!Array.isArray(content)) {
609
- throw new Error(
610
- `Unexpected JSON input from "${filename}".\n` +
611
- `Expected an array of strings but got:\n${content}`
612
- );
613
- }
614
-
615
- const errors: string[] = [];
616
- for (let i = 0; i < content.length; i++) {
617
- const key = content[i];
618
- if (typeof key !== "string") {
619
- errors.push(
620
- `The item at index ${i} is type: "${typeof key}" - ${JSON.stringify(
621
- key
622
- )}`
623
- );
624
- }
625
- }
626
-
627
- if (errors.length > 0) {
628
- throw new Error(
629
- `Unexpected JSON input from "${filename}".\n` +
630
- `Expected an array of strings.\n` +
631
- errors.join("\n")
632
- );
633
- }
634
-
635
- const accountId = await requireAuth(config);
636
-
637
- await deleteKVBulkKeyValue(accountId, namespaceId, content);
638
- await metrics.sendMetricsEvent("delete kv key-values (bulk)", {
639
- sendMetrics: config.send_metrics,
640
- });
641
-
642
- logger.log("Success!");
643
- }
644
- );
645
- };