wrangler 2.0.3 → 2.0.5
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.
- package/bin/wrangler.js +2 -2
- package/package.json +1 -1
- package/src/__tests__/kv.test.ts +16 -10
- package/src/cfetch/internal.ts +3 -0
- package/src/index.tsx +32 -40
- package/src/kv.ts +35 -12
- package/src/sites.tsx +10 -10
- package/wrangler-dist/cli.js +43 -37
package/bin/wrangler.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const { spawn } = require("
|
|
3
|
-
const { join } = require("
|
|
2
|
+
const { spawn } = require("child_process");
|
|
3
|
+
const { join } = require("path");
|
|
4
4
|
const semiver = require("semiver");
|
|
5
5
|
|
|
6
6
|
const MIN_NODE_VERSION = "16.7.0";
|
package/package.json
CHANGED
package/src/__tests__/kv.test.ts
CHANGED
|
@@ -257,14 +257,6 @@ describe("wrangler", () => {
|
|
|
257
257
|
expect(requests.count).toEqual(1);
|
|
258
258
|
});
|
|
259
259
|
|
|
260
|
-
it("should encode URI id properly for deleting namespace", async () => {
|
|
261
|
-
const requests = mockDeleteRequest("%2Fvoyager");
|
|
262
|
-
await runWrangler(`kv:namespace delete --namespace-id /voyager`);
|
|
263
|
-
expect(requests.count).toEqual(1);
|
|
264
|
-
expect(std.out).toMatchInlineSnapshot(`""`);
|
|
265
|
-
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
260
|
it("should delete a namespace specified by binding name", async () => {
|
|
269
261
|
writeWranglerConfig();
|
|
270
262
|
const requests = mockDeleteRequest("bound-id");
|
|
@@ -389,7 +381,7 @@ describe("wrangler", () => {
|
|
|
389
381
|
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
390
382
|
});
|
|
391
383
|
|
|
392
|
-
it("should encode
|
|
384
|
+
it("should encode the key in the api request to put a value", async () => {
|
|
393
385
|
const requests = mockKeyPutRequest("DS9", {
|
|
394
386
|
key: "%2Fmy-key",
|
|
395
387
|
value: "my-value",
|
|
@@ -951,6 +943,20 @@ describe("wrangler", () => {
|
|
|
951
943
|
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
952
944
|
});
|
|
953
945
|
|
|
946
|
+
it("should encode the key in the api request to get a value", async () => {
|
|
947
|
+
setMockFetchKVGetValue(
|
|
948
|
+
"some-account-id",
|
|
949
|
+
"some-namespace-id",
|
|
950
|
+
"%2Fmy%2Ckey",
|
|
951
|
+
"my-value"
|
|
952
|
+
);
|
|
953
|
+
await runWrangler(
|
|
954
|
+
"kv:key get /my,key --namespace-id some-namespace-id"
|
|
955
|
+
);
|
|
956
|
+
expect(std.out).toMatchInlineSnapshot(`"my-value"`);
|
|
957
|
+
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
958
|
+
});
|
|
959
|
+
|
|
954
960
|
it("should error if no key is provided", async () => {
|
|
955
961
|
await expect(
|
|
956
962
|
runWrangler("kv:key get")
|
|
@@ -1101,7 +1107,7 @@ describe("wrangler", () => {
|
|
|
1101
1107
|
expect(requests.count).toEqual(1);
|
|
1102
1108
|
});
|
|
1103
1109
|
|
|
1104
|
-
it("should encode the
|
|
1110
|
+
it("should encode the key in the api request to delete a value", async () => {
|
|
1105
1111
|
const requests = mockDeleteRequest("voyager", "%2FNCC-74656");
|
|
1106
1112
|
await runWrangler(`kv:key delete --namespace-id voyager /NCC-74656`);
|
|
1107
1113
|
expect(requests.count).toEqual(1);
|
package/src/cfetch/internal.ts
CHANGED
|
@@ -112,6 +112,9 @@ function addAuthorizationHeader(
|
|
|
112
112
|
* doesn't return json. We inline the implementation and try not to share
|
|
113
113
|
* any code with the other calls. We should push back on any new APIs that
|
|
114
114
|
* try to introduce non-"standard" response structures.
|
|
115
|
+
*
|
|
116
|
+
* Note: any calls to fetchKVGetValue must call encodeURIComponent on key
|
|
117
|
+
* before passing it
|
|
115
118
|
*/
|
|
116
119
|
|
|
117
120
|
export async function fetchKVGetValue(
|
package/src/index.tsx
CHANGED
|
@@ -22,17 +22,19 @@ import { confirm, prompt } from "./dialogs";
|
|
|
22
22
|
import { getEntry } from "./entry";
|
|
23
23
|
import { DeprecationError } from "./errors";
|
|
24
24
|
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
25
|
+
getKVNamespaceId,
|
|
26
|
+
listKVNamespaces,
|
|
27
|
+
listKVNamespaceKeys,
|
|
28
|
+
putKVKeyValue,
|
|
29
|
+
putKVBulkKeyValue,
|
|
30
|
+
deleteKVBulkKeyValue,
|
|
31
|
+
createKVNamespace,
|
|
32
|
+
isValidKVNamespaceBinding,
|
|
33
|
+
getKVKeyValue,
|
|
34
|
+
isKVKeyValue,
|
|
35
|
+
unexpectedKVKeyValueProps,
|
|
36
|
+
deleteKVNamespace,
|
|
37
|
+
deleteKVKeyValue,
|
|
36
38
|
} from "./kv";
|
|
37
39
|
import { logger } from "./logger";
|
|
38
40
|
import { getPackageManager } from "./package-manager";
|
|
@@ -1919,7 +1921,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
1919
1921
|
async (args) => {
|
|
1920
1922
|
await printWranglerBanner();
|
|
1921
1923
|
|
|
1922
|
-
if (!
|
|
1924
|
+
if (!isValidKVNamespaceBinding(args.namespace)) {
|
|
1923
1925
|
throw new CommandLineArgsError(
|
|
1924
1926
|
`The namespace binding name "${args.namespace}" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.`
|
|
1925
1927
|
);
|
|
@@ -1942,7 +1944,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
1942
1944
|
// TODO: generate a binding name stripping non alphanumeric chars
|
|
1943
1945
|
|
|
1944
1946
|
logger.log(`🌀 Creating namespace with title "${title}"`);
|
|
1945
|
-
const namespaceId = await
|
|
1947
|
+
const namespaceId = await createKVNamespace(accountId, title);
|
|
1946
1948
|
|
|
1947
1949
|
logger.log("✨ Success!");
|
|
1948
1950
|
const envString = args.env ? ` under [env.${args.env}]` : "";
|
|
@@ -1969,7 +1971,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
1969
1971
|
// TODO: we should show bindings if they exist for given ids
|
|
1970
1972
|
|
|
1971
1973
|
logger.log(
|
|
1972
|
-
JSON.stringify(await
|
|
1974
|
+
JSON.stringify(await listKVNamespaces(accountId), null, " ")
|
|
1973
1975
|
);
|
|
1974
1976
|
}
|
|
1975
1977
|
)
|
|
@@ -2006,7 +2008,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2006
2008
|
|
|
2007
2009
|
let id;
|
|
2008
2010
|
try {
|
|
2009
|
-
id =
|
|
2011
|
+
id = getKVNamespaceId(args, config);
|
|
2010
2012
|
} catch (e) {
|
|
2011
2013
|
throw new CommandLineArgsError(
|
|
2012
2014
|
"Not able to delete namespace.\n" + ((e as Error).message ?? e)
|
|
@@ -2015,12 +2017,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2015
2017
|
|
|
2016
2018
|
const accountId = await requireAuth(config);
|
|
2017
2019
|
|
|
2018
|
-
await
|
|
2019
|
-
`/accounts/${accountId}/storage/kv/namespaces/${encodeURIComponent(
|
|
2020
|
-
id
|
|
2021
|
-
)}`,
|
|
2022
|
-
{ method: "DELETE" }
|
|
2023
|
-
);
|
|
2020
|
+
await deleteKVNamespace(accountId, id);
|
|
2024
2021
|
|
|
2025
2022
|
// TODO: recommend they remove it from wrangler.toml
|
|
2026
2023
|
|
|
@@ -2105,7 +2102,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2105
2102
|
async ({ key, ttl, expiration, ...args }) => {
|
|
2106
2103
|
await printWranglerBanner();
|
|
2107
2104
|
const config = readConfig(args.config as ConfigPath, args);
|
|
2108
|
-
const namespaceId =
|
|
2105
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
2109
2106
|
// One of `args.path` and `args.value` must be defined
|
|
2110
2107
|
const value = args.path
|
|
2111
2108
|
? readFileSync(args.path)
|
|
@@ -2124,7 +2121,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2124
2121
|
|
|
2125
2122
|
const accountId = await requireAuth(config);
|
|
2126
2123
|
|
|
2127
|
-
await
|
|
2124
|
+
await putKVKeyValue(accountId, namespaceId, {
|
|
2128
2125
|
key,
|
|
2129
2126
|
value,
|
|
2130
2127
|
expiration,
|
|
@@ -2169,11 +2166,11 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2169
2166
|
async ({ prefix, ...args }) => {
|
|
2170
2167
|
// TODO: support for limit+cursor (pagination)
|
|
2171
2168
|
const config = readConfig(args.config as ConfigPath, args);
|
|
2172
|
-
const namespaceId =
|
|
2169
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
2173
2170
|
|
|
2174
2171
|
const accountId = await requireAuth(config);
|
|
2175
2172
|
|
|
2176
|
-
const results = await
|
|
2173
|
+
const results = await listKVNamespaceKeys(
|
|
2177
2174
|
accountId,
|
|
2178
2175
|
namespaceId,
|
|
2179
2176
|
prefix
|
|
@@ -2221,11 +2218,11 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2221
2218
|
},
|
|
2222
2219
|
async ({ key, ...args }) => {
|
|
2223
2220
|
const config = readConfig(args.config as ConfigPath, args);
|
|
2224
|
-
const namespaceId =
|
|
2221
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
2225
2222
|
|
|
2226
2223
|
const accountId = await requireAuth(config);
|
|
2227
2224
|
|
|
2228
|
-
logger.log(await
|
|
2225
|
+
logger.log(await getKVKeyValue(accountId, namespaceId, key));
|
|
2229
2226
|
}
|
|
2230
2227
|
)
|
|
2231
2228
|
.command(
|
|
@@ -2263,7 +2260,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2263
2260
|
async ({ key, ...args }) => {
|
|
2264
2261
|
await printWranglerBanner();
|
|
2265
2262
|
const config = readConfig(args.config as ConfigPath, args);
|
|
2266
|
-
const namespaceId =
|
|
2263
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
2267
2264
|
|
|
2268
2265
|
logger.log(
|
|
2269
2266
|
`Deleting the key "${key}" on namespace ${namespaceId}.`
|
|
@@ -2271,12 +2268,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2271
2268
|
|
|
2272
2269
|
const accountId = await requireAuth(config);
|
|
2273
2270
|
|
|
2274
|
-
await
|
|
2275
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(
|
|
2276
|
-
key
|
|
2277
|
-
)}`,
|
|
2278
|
-
{ method: "DELETE" }
|
|
2279
|
-
);
|
|
2271
|
+
await deleteKVKeyValue(accountId, namespaceId, key);
|
|
2280
2272
|
}
|
|
2281
2273
|
);
|
|
2282
2274
|
}
|
|
@@ -2328,7 +2320,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2328
2320
|
// but we'll do that in the future if needed.
|
|
2329
2321
|
|
|
2330
2322
|
const config = readConfig(args.config as ConfigPath, args);
|
|
2331
|
-
const namespaceId =
|
|
2323
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
2332
2324
|
const content = parseJSON(readFileSync(filename), filename);
|
|
2333
2325
|
|
|
2334
2326
|
if (!Array.isArray(content)) {
|
|
@@ -2348,12 +2340,12 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2348
2340
|
keyValue
|
|
2349
2341
|
)}`
|
|
2350
2342
|
);
|
|
2351
|
-
} else if (!
|
|
2343
|
+
} else if (!isKVKeyValue(keyValue)) {
|
|
2352
2344
|
errors.push(
|
|
2353
2345
|
`The item at index ${i} is ${JSON.stringify(keyValue)}`
|
|
2354
2346
|
);
|
|
2355
2347
|
} else {
|
|
2356
|
-
const props =
|
|
2348
|
+
const props = unexpectedKVKeyValueProps(keyValue);
|
|
2357
2349
|
if (props.length > 0) {
|
|
2358
2350
|
warnings.push(
|
|
2359
2351
|
`The item at index ${i} contains unexpected properties: ${JSON.stringify(
|
|
@@ -2386,7 +2378,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2386
2378
|
}
|
|
2387
2379
|
|
|
2388
2380
|
const accountId = await requireAuth(config);
|
|
2389
|
-
await
|
|
2381
|
+
await putKVBulkKeyValue(
|
|
2390
2382
|
accountId,
|
|
2391
2383
|
namespaceId,
|
|
2392
2384
|
content,
|
|
@@ -2438,7 +2430,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2438
2430
|
async ({ filename, ...args }) => {
|
|
2439
2431
|
await printWranglerBanner();
|
|
2440
2432
|
const config = readConfig(args.config as ConfigPath, args);
|
|
2441
|
-
const namespaceId =
|
|
2433
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
2442
2434
|
|
|
2443
2435
|
if (!args.force) {
|
|
2444
2436
|
const result = await confirm(
|
|
@@ -2483,7 +2475,7 @@ export async function main(argv: string[]): Promise<void> {
|
|
|
2483
2475
|
|
|
2484
2476
|
const accountId = await requireAuth(config);
|
|
2485
2477
|
|
|
2486
|
-
await
|
|
2478
|
+
await deleteKVBulkKeyValue(
|
|
2487
2479
|
accountId,
|
|
2488
2480
|
namespaceId,
|
|
2489
2481
|
content,
|
package/src/kv.ts
CHANGED
|
@@ -19,7 +19,7 @@ type KvArgs = {
|
|
|
19
19
|
*
|
|
20
20
|
* @returns the generated id of the created namespace.
|
|
21
21
|
*/
|
|
22
|
-
export async function
|
|
22
|
+
export async function createKVNamespace(
|
|
23
23
|
accountId: string,
|
|
24
24
|
title: string
|
|
25
25
|
): Promise<string> {
|
|
@@ -51,7 +51,7 @@ export interface KVNamespaceInfo {
|
|
|
51
51
|
/**
|
|
52
52
|
* Fetch a list of all the namespaces under the given `accountId`.
|
|
53
53
|
*/
|
|
54
|
-
export async function
|
|
54
|
+
export async function listKVNamespaces(
|
|
55
55
|
accountId: string
|
|
56
56
|
): Promise<KVNamespaceInfo[]> {
|
|
57
57
|
const pageSize = 100;
|
|
@@ -83,7 +83,7 @@ export interface NamespaceKeyInfo {
|
|
|
83
83
|
metadata?: { [key: string]: unknown };
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
export async function
|
|
86
|
+
export async function listKVNamespaceKeys(
|
|
87
87
|
accountId: string,
|
|
88
88
|
namespaceId: string,
|
|
89
89
|
prefix = ""
|
|
@@ -95,6 +95,16 @@ export async function listNamespaceKeys(
|
|
|
95
95
|
);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
export async function deleteKVNamespace(
|
|
99
|
+
accountId: string,
|
|
100
|
+
namespaceId: string
|
|
101
|
+
) {
|
|
102
|
+
return await fetchResult<{ id: string }>(
|
|
103
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}`,
|
|
104
|
+
{ method: "DELETE" }
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
98
108
|
/**
|
|
99
109
|
* Information about a key-value pair, including its "metadata" fields.
|
|
100
110
|
*/
|
|
@@ -119,7 +129,7 @@ const KeyValueKeys = new Set([
|
|
|
119
129
|
/**
|
|
120
130
|
* Is the given object a valid `KeyValue` type?
|
|
121
131
|
*/
|
|
122
|
-
export function
|
|
132
|
+
export function isKVKeyValue(keyValue: object): keyValue is KeyValue {
|
|
123
133
|
const props = Object.keys(keyValue);
|
|
124
134
|
if (!props.includes("key") || !props.includes("value")) {
|
|
125
135
|
return false;
|
|
@@ -130,12 +140,12 @@ export function isKeyValue(keyValue: object): keyValue is KeyValue {
|
|
|
130
140
|
/**
|
|
131
141
|
* Get all the properties on the `keyValue` that are not expected.
|
|
132
142
|
*/
|
|
133
|
-
export function
|
|
143
|
+
export function unexpectedKVKeyValueProps(keyValue: KeyValue): string[] {
|
|
134
144
|
const props = Object.keys(keyValue);
|
|
135
145
|
return props.filter((prop) => !KeyValueKeys.has(prop));
|
|
136
146
|
}
|
|
137
147
|
|
|
138
|
-
export async function
|
|
148
|
+
export async function putKVKeyValue(
|
|
139
149
|
accountId: string,
|
|
140
150
|
namespaceId: string,
|
|
141
151
|
keyValue: KeyValue
|
|
@@ -159,15 +169,28 @@ export async function putKeyValue(
|
|
|
159
169
|
);
|
|
160
170
|
}
|
|
161
171
|
|
|
162
|
-
export async function
|
|
172
|
+
export async function getKVKeyValue(
|
|
163
173
|
accountId: string,
|
|
164
174
|
namespaceId: string,
|
|
165
175
|
key: string
|
|
166
176
|
): Promise<string> {
|
|
167
|
-
return await fetchKVGetValue(accountId, namespaceId, key);
|
|
177
|
+
return await fetchKVGetValue(accountId, namespaceId, encodeURIComponent(key));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export async function deleteKVKeyValue(
|
|
181
|
+
accountId: string,
|
|
182
|
+
namespaceId: string,
|
|
183
|
+
key: string
|
|
184
|
+
) {
|
|
185
|
+
return await fetchResult(
|
|
186
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(
|
|
187
|
+
key
|
|
188
|
+
)}`,
|
|
189
|
+
{ method: "DELETE" }
|
|
190
|
+
);
|
|
168
191
|
}
|
|
169
192
|
|
|
170
|
-
export async function
|
|
193
|
+
export async function putKVBulkKeyValue(
|
|
171
194
|
accountId: string,
|
|
172
195
|
namespaceId: string,
|
|
173
196
|
keyValues: KeyValue[],
|
|
@@ -192,7 +215,7 @@ export async function putBulkKeyValue(
|
|
|
192
215
|
}
|
|
193
216
|
}
|
|
194
217
|
|
|
195
|
-
export async function
|
|
218
|
+
export async function deleteKVBulkKeyValue(
|
|
196
219
|
accountId: string,
|
|
197
220
|
namespaceId: string,
|
|
198
221
|
keys: string[],
|
|
@@ -217,7 +240,7 @@ export async function deleteBulkKeyValue(
|
|
|
217
240
|
}
|
|
218
241
|
}
|
|
219
242
|
|
|
220
|
-
export function
|
|
243
|
+
export function getKVNamespaceId(
|
|
221
244
|
{ preview, binding, "namespace-id": namespaceId }: KvArgs,
|
|
222
245
|
config: Config
|
|
223
246
|
): string {
|
|
@@ -312,7 +335,7 @@ export function getNamespaceId(
|
|
|
312
335
|
/**
|
|
313
336
|
* KV namespace binding names must be valid JS identifiers.
|
|
314
337
|
*/
|
|
315
|
-
export function
|
|
338
|
+
export function isValidKVNamespaceBinding(
|
|
316
339
|
binding: string | undefined
|
|
317
340
|
): binding is string {
|
|
318
341
|
return (
|
package/src/sites.tsx
CHANGED
|
@@ -3,11 +3,11 @@ import * as path from "node:path";
|
|
|
3
3
|
import ignore from "ignore";
|
|
4
4
|
import xxhash from "xxhash-wasm";
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
createKVNamespace,
|
|
7
|
+
listKVNamespaceKeys,
|
|
8
|
+
listKVNamespaces,
|
|
9
|
+
putKVBulkKeyValue,
|
|
10
|
+
deleteKVBulkKeyValue,
|
|
11
11
|
} from "./kv";
|
|
12
12
|
import { logger } from "./logger";
|
|
13
13
|
import type { Config } from "./config";
|
|
@@ -75,14 +75,14 @@ async function createKVNamespaceIfNotAlreadyExisting(
|
|
|
75
75
|
) {
|
|
76
76
|
// check if it already exists
|
|
77
77
|
// TODO: this is super inefficient, should be made better
|
|
78
|
-
const namespaces = await
|
|
78
|
+
const namespaces = await listKVNamespaces(accountId);
|
|
79
79
|
const found = namespaces.find((x) => x.title === title);
|
|
80
80
|
if (found) {
|
|
81
81
|
return { created: false, id: found.id };
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
// else we make the namespace
|
|
85
|
-
const id = await
|
|
85
|
+
const id = await createKVNamespace(accountId, title);
|
|
86
86
|
logger.log(`🌀 Created namespace for Workers Site "${title}"`);
|
|
87
87
|
|
|
88
88
|
return {
|
|
@@ -131,7 +131,7 @@ export async function syncAssets(
|
|
|
131
131
|
);
|
|
132
132
|
|
|
133
133
|
// let's get all the keys in this namespace
|
|
134
|
-
const namespaceKeysResponse = await
|
|
134
|
+
const namespaceKeysResponse = await listKVNamespaceKeys(accountId, namespace);
|
|
135
135
|
const namespaceKeys = new Set(namespaceKeysResponse.map((x) => x.name));
|
|
136
136
|
|
|
137
137
|
const manifest: Record<string, string> = {};
|
|
@@ -185,9 +185,9 @@ export async function syncAssets(
|
|
|
185
185
|
|
|
186
186
|
await Promise.all([
|
|
187
187
|
// upload all the new assets
|
|
188
|
-
|
|
188
|
+
putKVBulkKeyValue(accountId, namespace, toUpload, () => {}),
|
|
189
189
|
// delete all the unused assets
|
|
190
|
-
|
|
190
|
+
deleteKVBulkKeyValue(
|
|
191
191
|
accountId,
|
|
192
192
|
namespace,
|
|
193
193
|
Array.from(namespaceKeys),
|
package/wrangler-dist/cli.js
CHANGED
|
@@ -104962,7 +104962,7 @@ var yargs_default = Yargs;
|
|
|
104962
104962
|
|
|
104963
104963
|
// package.json
|
|
104964
104964
|
var name = "wrangler";
|
|
104965
|
-
var version = "2.0.
|
|
104965
|
+
var version = "2.0.5";
|
|
104966
104966
|
var author = "wrangler@cloudflare.com";
|
|
104967
104967
|
var description = "Command-line interface for all things Cloudflare Workers";
|
|
104968
104968
|
var bin = {
|
|
@@ -108538,7 +108538,7 @@ init_import_meta_url();
|
|
|
108538
108538
|
var import_node_url10 = require("node:url");
|
|
108539
108539
|
var API_MAX = 1e4;
|
|
108540
108540
|
var BATCH_KEY_MAX = API_MAX / 2;
|
|
108541
|
-
async function
|
|
108541
|
+
async function createKVNamespace(accountId, title) {
|
|
108542
108542
|
const response = await fetchResult(`/accounts/${accountId}/storage/kv/namespaces`, {
|
|
108543
108543
|
method: "POST",
|
|
108544
108544
|
headers: {
|
|
@@ -108550,7 +108550,7 @@ async function createNamespace(accountId, title) {
|
|
|
108550
108550
|
});
|
|
108551
108551
|
return response.id;
|
|
108552
108552
|
}
|
|
108553
|
-
async function
|
|
108553
|
+
async function listKVNamespaces(accountId) {
|
|
108554
108554
|
const pageSize = 100;
|
|
108555
108555
|
let page = 1;
|
|
108556
108556
|
const results = [];
|
|
@@ -108569,9 +108569,12 @@ async function listNamespaces(accountId) {
|
|
|
108569
108569
|
}
|
|
108570
108570
|
return results;
|
|
108571
108571
|
}
|
|
108572
|
-
async function
|
|
108572
|
+
async function listKVNamespaceKeys(accountId, namespaceId, prefix2 = "") {
|
|
108573
108573
|
return await fetchListResult(`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys`, {}, new import_node_url10.URLSearchParams({ prefix: prefix2 }));
|
|
108574
108574
|
}
|
|
108575
|
+
async function deleteKVNamespace(accountId, namespaceId) {
|
|
108576
|
+
return await fetchResult(`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}`, { method: "DELETE" });
|
|
108577
|
+
}
|
|
108575
108578
|
var KeyValueKeys = /* @__PURE__ */ new Set([
|
|
108576
108579
|
"key",
|
|
108577
108580
|
"value",
|
|
@@ -108580,18 +108583,18 @@ var KeyValueKeys = /* @__PURE__ */ new Set([
|
|
|
108580
108583
|
"metadata",
|
|
108581
108584
|
"base64"
|
|
108582
108585
|
]);
|
|
108583
|
-
function
|
|
108586
|
+
function isKVKeyValue(keyValue) {
|
|
108584
108587
|
const props = Object.keys(keyValue);
|
|
108585
108588
|
if (!props.includes("key") || !props.includes("value")) {
|
|
108586
108589
|
return false;
|
|
108587
108590
|
}
|
|
108588
108591
|
return true;
|
|
108589
108592
|
}
|
|
108590
|
-
function
|
|
108593
|
+
function unexpectedKVKeyValueProps(keyValue) {
|
|
108591
108594
|
const props = Object.keys(keyValue);
|
|
108592
108595
|
return props.filter((prop) => !KeyValueKeys.has(prop));
|
|
108593
108596
|
}
|
|
108594
|
-
async function
|
|
108597
|
+
async function putKVKeyValue(accountId, namespaceId, keyValue) {
|
|
108595
108598
|
let searchParams;
|
|
108596
108599
|
if (keyValue.expiration || keyValue.expiration_ttl) {
|
|
108597
108600
|
searchParams = new import_node_url10.URLSearchParams();
|
|
@@ -108604,10 +108607,13 @@ async function putKeyValue(accountId, namespaceId, keyValue) {
|
|
|
108604
108607
|
}
|
|
108605
108608
|
return await fetchResult(`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(keyValue.key)}`, { method: "PUT", body: keyValue.value }, searchParams);
|
|
108606
108609
|
}
|
|
108607
|
-
async function
|
|
108608
|
-
return await fetchKVGetValue(accountId, namespaceId, key2);
|
|
108610
|
+
async function getKVKeyValue(accountId, namespaceId, key2) {
|
|
108611
|
+
return await fetchKVGetValue(accountId, namespaceId, encodeURIComponent(key2));
|
|
108612
|
+
}
|
|
108613
|
+
async function deleteKVKeyValue(accountId, namespaceId, key2) {
|
|
108614
|
+
return await fetchResult(`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(key2)}`, { method: "DELETE" });
|
|
108609
108615
|
}
|
|
108610
|
-
async function
|
|
108616
|
+
async function putKVBulkKeyValue(accountId, namespaceId, keyValues, progressCallback) {
|
|
108611
108617
|
for (let index = 0; index < keyValues.length; index += BATCH_KEY_MAX) {
|
|
108612
108618
|
if (progressCallback && keyValues.length > BATCH_KEY_MAX) {
|
|
108613
108619
|
progressCallback(index, keyValues.length);
|
|
@@ -108622,7 +108628,7 @@ async function putBulkKeyValue(accountId, namespaceId, keyValues, progressCallba
|
|
|
108622
108628
|
progressCallback(keyValues.length, keyValues.length);
|
|
108623
108629
|
}
|
|
108624
108630
|
}
|
|
108625
|
-
async function
|
|
108631
|
+
async function deleteKVBulkKeyValue(accountId, namespaceId, keys, progressCallback) {
|
|
108626
108632
|
for (let index = 0; index < keys.length; index += BATCH_KEY_MAX) {
|
|
108627
108633
|
if (progressCallback && keys.length > BATCH_KEY_MAX) {
|
|
108628
108634
|
progressCallback(index, keys.length);
|
|
@@ -108637,7 +108643,7 @@ async function deleteBulkKeyValue(accountId, namespaceId, keys, progressCallback
|
|
|
108637
108643
|
progressCallback(keys.length, keys.length);
|
|
108638
108644
|
}
|
|
108639
108645
|
}
|
|
108640
|
-
function
|
|
108646
|
+
function getKVNamespaceId({ preview, binding, "namespace-id": namespaceId }, config) {
|
|
108641
108647
|
if (namespaceId) {
|
|
108642
108648
|
return namespaceId;
|
|
108643
108649
|
}
|
|
@@ -108678,7 +108684,7 @@ function getNamespaceId({ preview, binding, "namespace-id": namespaceId }, confi
|
|
|
108678
108684
|
}
|
|
108679
108685
|
return namespaceId;
|
|
108680
108686
|
}
|
|
108681
|
-
function
|
|
108687
|
+
function isValidKVNamespaceBinding(binding) {
|
|
108682
108688
|
return typeof binding === "string" && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(binding);
|
|
108683
108689
|
}
|
|
108684
108690
|
|
|
@@ -108714,12 +108720,12 @@ function hashAsset(hasher, filePath, content) {
|
|
|
108714
108720
|
return urlSafe(path13.join(directory, `${baseName}.${hash2}${extName}`));
|
|
108715
108721
|
}
|
|
108716
108722
|
async function createKVNamespaceIfNotAlreadyExisting(title, accountId) {
|
|
108717
|
-
const namespaces = await
|
|
108723
|
+
const namespaces = await listKVNamespaces(accountId);
|
|
108718
108724
|
const found = namespaces.find((x) => x.title === title);
|
|
108719
108725
|
if (found) {
|
|
108720
108726
|
return { created: false, id: found.id };
|
|
108721
108727
|
}
|
|
108722
|
-
const id = await
|
|
108728
|
+
const id = await createKVNamespace(accountId, title);
|
|
108723
108729
|
logger.log(`\u{1F300} Created namespace for Workers Site "${title}"`);
|
|
108724
108730
|
return {
|
|
108725
108731
|
created: true,
|
|
@@ -108736,7 +108742,7 @@ async function syncAssets(accountId, scriptName, siteAssets, preview, dryRun) {
|
|
|
108736
108742
|
}
|
|
108737
108743
|
const title = `__${scriptName}-workers_sites_assets${preview ? "_preview" : ""}`;
|
|
108738
108744
|
const { id: namespace } = await createKVNamespaceIfNotAlreadyExisting(title, accountId);
|
|
108739
|
-
const namespaceKeysResponse = await
|
|
108745
|
+
const namespaceKeysResponse = await listKVNamespaceKeys(accountId, namespace);
|
|
108740
108746
|
const namespaceKeys = new Set(namespaceKeysResponse.map((x) => x.name));
|
|
108741
108747
|
const manifest = {};
|
|
108742
108748
|
const toUpload = [];
|
|
@@ -108774,9 +108780,9 @@ async function syncAssets(accountId, scriptName, siteAssets, preview, dryRun) {
|
|
|
108774
108780
|
logger.log(`Deleting ${key2} from the asset store...`);
|
|
108775
108781
|
}
|
|
108776
108782
|
await Promise.all([
|
|
108777
|
-
|
|
108783
|
+
putKVBulkKeyValue(accountId, namespace, toUpload, () => {
|
|
108778
108784
|
}),
|
|
108779
|
-
|
|
108785
|
+
deleteKVBulkKeyValue(accountId, namespace, Array.from(namespaceKeys), () => {
|
|
108780
108786
|
})
|
|
108781
108787
|
]);
|
|
108782
108788
|
logger.log("\u2197\uFE0F Done syncing assets");
|
|
@@ -112881,7 +112887,7 @@ ${shouldDo}`);
|
|
|
112881
112887
|
});
|
|
112882
112888
|
}, async (args) => {
|
|
112883
112889
|
await printWranglerBanner();
|
|
112884
|
-
if (!
|
|
112890
|
+
if (!isValidKVNamespaceBinding(args.namespace)) {
|
|
112885
112891
|
throw new CommandLineArgsError(`The namespace binding name "${args.namespace}" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.`);
|
|
112886
112892
|
}
|
|
112887
112893
|
const config = readConfig(args.config, args);
|
|
@@ -112894,7 +112900,7 @@ ${shouldDo}`);
|
|
|
112894
112900
|
const title = `${name2}${environment}-${args.namespace}${preview}`;
|
|
112895
112901
|
const accountId = await requireAuth(config);
|
|
112896
112902
|
logger.log(`\u{1F300} Creating namespace with title "${title}"`);
|
|
112897
|
-
const namespaceId = await
|
|
112903
|
+
const namespaceId = await createKVNamespace(accountId, title);
|
|
112898
112904
|
logger.log("\u2728 Success!");
|
|
112899
112905
|
const envString = args.env ? ` under [env.${args.env}]` : "";
|
|
112900
112906
|
const previewString = args.preview ? "preview_" : "";
|
|
@@ -112903,7 +112909,7 @@ ${shouldDo}`);
|
|
|
112903
112909
|
}).command("list", "Outputs a list of all KV namespaces associated with your account id.", {}, async (args) => {
|
|
112904
112910
|
const config = readConfig(args.config, args);
|
|
112905
112911
|
const accountId = await requireAuth(config);
|
|
112906
|
-
logger.log(JSON.stringify(await
|
|
112912
|
+
logger.log(JSON.stringify(await listKVNamespaces(accountId), null, " "));
|
|
112907
112913
|
}).command("delete", "Deletes a given namespace.", (yargs) => {
|
|
112908
112914
|
return yargs.option("binding", {
|
|
112909
112915
|
type: "string",
|
|
@@ -112927,12 +112933,12 @@ ${shouldDo}`);
|
|
|
112927
112933
|
const config = readConfig(args.config, args);
|
|
112928
112934
|
let id;
|
|
112929
112935
|
try {
|
|
112930
|
-
id =
|
|
112936
|
+
id = getKVNamespaceId(args, config);
|
|
112931
112937
|
} catch (e2) {
|
|
112932
112938
|
throw new CommandLineArgsError("Not able to delete namespace.\n" + (e2.message ?? e2));
|
|
112933
112939
|
}
|
|
112934
112940
|
const accountId = await requireAuth(config);
|
|
112935
|
-
await
|
|
112941
|
+
await deleteKVNamespace(accountId, id);
|
|
112936
112942
|
});
|
|
112937
112943
|
});
|
|
112938
112944
|
wrangler.command("kv:key", "\u{1F511} Individually manage Workers KV key-value pairs", (kvKeyYargs) => {
|
|
@@ -112974,7 +112980,7 @@ ${shouldDo}`);
|
|
|
112974
112980
|
}, async ({ key: key2, ttl, expiration, ...args }) => {
|
|
112975
112981
|
await printWranglerBanner();
|
|
112976
112982
|
const config = readConfig(args.config, args);
|
|
112977
|
-
const namespaceId =
|
|
112983
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
112978
112984
|
const value = args.path ? readFileSync5(args.path) : args.value;
|
|
112979
112985
|
if (args.path) {
|
|
112980
112986
|
logger.log(`Writing the contents of ${args.path} to the key "${key2}" on namespace ${namespaceId}.`);
|
|
@@ -112982,7 +112988,7 @@ ${shouldDo}`);
|
|
|
112982
112988
|
logger.log(`Writing the value "${value}" to key "${key2}" on namespace ${namespaceId}.`);
|
|
112983
112989
|
}
|
|
112984
112990
|
const accountId = await requireAuth(config);
|
|
112985
|
-
await
|
|
112991
|
+
await putKVKeyValue(accountId, namespaceId, {
|
|
112986
112992
|
key: key2,
|
|
112987
112993
|
value,
|
|
112988
112994
|
expiration,
|
|
@@ -113013,9 +113019,9 @@ ${shouldDo}`);
|
|
|
113013
113019
|
});
|
|
113014
113020
|
}, async ({ prefix: prefix2, ...args }) => {
|
|
113015
113021
|
const config = readConfig(args.config, args);
|
|
113016
|
-
const namespaceId =
|
|
113022
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
113017
113023
|
const accountId = await requireAuth(config);
|
|
113018
|
-
const results = await
|
|
113024
|
+
const results = await listKVNamespaceKeys(accountId, namespaceId, prefix2);
|
|
113019
113025
|
logger.log(JSON.stringify(results, void 0, 2));
|
|
113020
113026
|
}).command("get <key>", "Reads a single value by key from the given namespace.", (yargs) => {
|
|
113021
113027
|
return yargs.positional("key", {
|
|
@@ -113045,9 +113051,9 @@ ${shouldDo}`);
|
|
|
113045
113051
|
});
|
|
113046
113052
|
}, async ({ key: key2, ...args }) => {
|
|
113047
113053
|
const config = readConfig(args.config, args);
|
|
113048
|
-
const namespaceId =
|
|
113054
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
113049
113055
|
const accountId = await requireAuth(config);
|
|
113050
|
-
logger.log(await
|
|
113056
|
+
logger.log(await getKVKeyValue(accountId, namespaceId, key2));
|
|
113051
113057
|
}).command("delete <key>", "Removes a single key value pair from the given namespace.", (yargs) => {
|
|
113052
113058
|
return yargs.positional("key", {
|
|
113053
113059
|
describe: "The key value to delete",
|
|
@@ -113073,10 +113079,10 @@ ${shouldDo}`);
|
|
|
113073
113079
|
}, async ({ key: key2, ...args }) => {
|
|
113074
113080
|
await printWranglerBanner();
|
|
113075
113081
|
const config = readConfig(args.config, args);
|
|
113076
|
-
const namespaceId =
|
|
113082
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
113077
113083
|
logger.log(`Deleting the key "${key2}" on namespace ${namespaceId}.`);
|
|
113078
113084
|
const accountId = await requireAuth(config);
|
|
113079
|
-
await
|
|
113085
|
+
await deleteKVKeyValue(accountId, namespaceId, key2);
|
|
113080
113086
|
});
|
|
113081
113087
|
});
|
|
113082
113088
|
wrangler.command("kv:bulk", "\u{1F4AA} Interact with multiple Workers KV key-value pairs at once", (kvBulkYargs) => {
|
|
@@ -113105,7 +113111,7 @@ ${shouldDo}`);
|
|
|
113105
113111
|
}, async ({ filename, ...args }) => {
|
|
113106
113112
|
await printWranglerBanner();
|
|
113107
113113
|
const config = readConfig(args.config, args);
|
|
113108
|
-
const namespaceId =
|
|
113114
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
113109
113115
|
const content = parseJSON(readFileSync5(filename), filename);
|
|
113110
113116
|
if (!Array.isArray(content)) {
|
|
113111
113117
|
throw new Error(`Unexpected JSON input from "${filename}".
|
|
@@ -113117,10 +113123,10 @@ Expected an array of key-value objects but got type "${typeof content}".`);
|
|
|
113117
113123
|
const keyValue = content[i2];
|
|
113118
113124
|
if (typeof keyValue !== "object") {
|
|
113119
113125
|
errors.push(`The item at index ${i2} is type: "${typeof keyValue}" - ${JSON.stringify(keyValue)}`);
|
|
113120
|
-
} else if (!
|
|
113126
|
+
} else if (!isKVKeyValue(keyValue)) {
|
|
113121
113127
|
errors.push(`The item at index ${i2} is ${JSON.stringify(keyValue)}`);
|
|
113122
113128
|
} else {
|
|
113123
|
-
const props =
|
|
113129
|
+
const props = unexpectedKVKeyValueProps(keyValue);
|
|
113124
113130
|
if (props.length > 0) {
|
|
113125
113131
|
warnings.push(`The item at index ${i2} contains unexpected properties: ${JSON.stringify(props)}.`);
|
|
113126
113132
|
}
|
|
@@ -113146,7 +113152,7 @@ interface KeyValue {
|
|
|
113146
113152
|
` + errors.join("\n"));
|
|
113147
113153
|
}
|
|
113148
113154
|
const accountId = await requireAuth(config);
|
|
113149
|
-
await
|
|
113155
|
+
await putKVBulkKeyValue(accountId, namespaceId, content, (index, total) => {
|
|
113150
113156
|
logger.log(`Uploaded ${index} of ${total}.`);
|
|
113151
113157
|
});
|
|
113152
113158
|
logger.log("Success!");
|
|
@@ -113179,7 +113185,7 @@ interface KeyValue {
|
|
|
113179
113185
|
}, async ({ filename, ...args }) => {
|
|
113180
113186
|
await printWranglerBanner();
|
|
113181
113187
|
const config = readConfig(args.config, args);
|
|
113182
|
-
const namespaceId =
|
|
113188
|
+
const namespaceId = getKVNamespaceId(args, config);
|
|
113183
113189
|
if (!args.force) {
|
|
113184
113190
|
const result = await confirm(`Are you sure you want to delete all the keys read from "${filename}" from kv-namespace with id "${namespaceId}"?`);
|
|
113185
113191
|
if (!result) {
|
|
@@ -113206,7 +113212,7 @@ Expected an array of strings.
|
|
|
113206
113212
|
` + errors.join("\n"));
|
|
113207
113213
|
}
|
|
113208
113214
|
const accountId = await requireAuth(config);
|
|
113209
|
-
await
|
|
113215
|
+
await deleteKVBulkKeyValue(accountId, namespaceId, content, (index, total) => {
|
|
113210
113216
|
logger.log(`Deleted ${index} of ${total}.`);
|
|
113211
113217
|
});
|
|
113212
113218
|
logger.log("Success!");
|