diff --git a/.changeset/twelve-goats-provide.md b/.changeset/twelve-goats-provide.md new file mode 100644 index 000000000000..58882f823a4f --- /dev/null +++ b/.changeset/twelve-goats-provide.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +Improve messaging when bulk deleting or uploading KV Pairs + +Closes #555 diff --git a/packages/wrangler/src/__tests__/kv.test.ts b/packages/wrangler/src/__tests__/kv.test.ts index 46cc5e2f646b..89b73bf3e052 100644 --- a/packages/wrangler/src/__tests__/kv.test.ts +++ b/packages/wrangler/src/__tests__/kv.test.ts @@ -1205,10 +1205,10 @@ describe("wrangler", () => { ); expect(requests.count).toEqual(3); expect(std.out).toMatchInlineSnapshot(` - "Uploaded 0 of 12000. - Uploaded 5000 of 12000. - Uploaded 10000 of 12000. - Uploaded 12000 of 12000. + "Uploaded 0% (0 out of 12,000) + Uploaded 41% (5,000 out of 12,000) + Uploaded 83% (10,000 out of 12,000) + Uploaded 100% (12,000 out of 12,000) Success!" `); expect(std.warn).toMatchInlineSnapshot(`""`); @@ -1358,10 +1358,10 @@ describe("wrangler", () => { ); expect(requests.count).toEqual(3); expect(std.out).toMatchInlineSnapshot(` - "Deleted 0 of 12000. - Deleted 5000 of 12000. - Deleted 10000 of 12000. - Deleted 12000 of 12000. + "Deleted 0% (0 out of 12,000) + Deleted 41% (5,000 out of 12,000) + Deleted 83% (10,000 out of 12,000) + Deleted 100% (12,000 out of 12,000) Success!" `); expect(std.warn).toMatchInlineSnapshot(`""`); diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 9b63e05edd2a..89d9fe5c3696 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -2397,14 +2397,7 @@ function createCLIParser(argv: string[]) { } const accountId = await requireAuth(config); - await putKVBulkKeyValue( - accountId, - namespaceId, - content, - (index, total) => { - logger.log(`Uploaded ${index} of ${total}.`); - } - ); + await putKVBulkKeyValue(accountId, namespaceId, content); logger.log("Success!"); } @@ -2494,14 +2487,7 @@ function createCLIParser(argv: string[]) { const accountId = await requireAuth(config); - await deleteKVBulkKeyValue( - accountId, - namespaceId, - content, - (index, total) => { - logger.log(`Deleted ${index} of ${total}.`); - } - ); + await deleteKVBulkKeyValue(accountId, namespaceId, content); logger.log("Success!"); } diff --git a/packages/wrangler/src/kv.ts b/packages/wrangler/src/kv.ts index 402f6baa5184..5c633b7df3d3 100644 --- a/packages/wrangler/src/kv.ts +++ b/packages/wrangler/src/kv.ts @@ -1,5 +1,6 @@ import { URLSearchParams } from "node:url"; import { fetchListResult, fetchResult, fetchKVGetValue } from "./cfetch"; +import { logger } from "./logger"; import type { Config } from "./config"; /** The largest number of kv items we can pass to the API in a single request. */ @@ -205,15 +206,37 @@ export async function deleteKVKeyValue( ); } +/** + * Formatter for converting e.g. 5328 --> 5,328 + */ +const formatNumber = new Intl.NumberFormat("en-US", { + notation: "standard", +}).format; + +/** + * Helper function for bulk requests, logs ongoing output to console. + */ +function logBulkProgress( + operation: "put" | "delete", + index: number, + total: number +) { + logger.log( + `${operation === "put" ? "Uploaded" : "Deleted"} ${Math.floor( + (100 * index) / total + )}% (${formatNumber(index)} out of ${formatNumber(total)})` + ); +} + export async function putKVBulkKeyValue( accountId: string, namespaceId: string, keyValues: KeyValue[], - progressCallback: (index: number, total: number) => void + quiet = false ) { for (let index = 0; index < keyValues.length; index += BATCH_KEY_MAX) { - if (progressCallback && keyValues.length > BATCH_KEY_MAX) { - progressCallback(index, keyValues.length); + if (!quiet && keyValues.length > BATCH_KEY_MAX) { + logBulkProgress("put", index, keyValues.length); } await fetchResult( @@ -225,8 +248,9 @@ export async function putKVBulkKeyValue( } ); } - if (progressCallback && keyValues.length > BATCH_KEY_MAX) { - progressCallback(keyValues.length, keyValues.length); + + if (!quiet && keyValues.length > BATCH_KEY_MAX) { + logBulkProgress("put", keyValues.length, keyValues.length); } } @@ -234,11 +258,11 @@ export async function deleteKVBulkKeyValue( accountId: string, namespaceId: string, keys: string[], - progressCallback: (index: number, total: number) => void + quiet = false ) { for (let index = 0; index < keys.length; index += BATCH_KEY_MAX) { - if (progressCallback && keys.length > BATCH_KEY_MAX) { - progressCallback(index, keys.length); + if (!quiet && keys.length > BATCH_KEY_MAX) { + logBulkProgress("delete", index, keys.length); } await fetchResult( @@ -250,8 +274,8 @@ export async function deleteKVBulkKeyValue( } ); } - if (progressCallback && keys.length > BATCH_KEY_MAX) { - progressCallback(keys.length, keys.length); + if (!quiet && keys.length > BATCH_KEY_MAX) { + logBulkProgress("delete", keys.length, keys.length); } } diff --git a/packages/wrangler/src/sites.tsx b/packages/wrangler/src/sites.tsx index 457441c3525c..20ad2ab58137 100644 --- a/packages/wrangler/src/sites.tsx +++ b/packages/wrangler/src/sites.tsx @@ -187,14 +187,9 @@ export async function syncAssets( await Promise.all([ // upload all the new assets - putKVBulkKeyValue(accountId, namespace, toUpload, () => {}), + putKVBulkKeyValue(accountId, namespace, toUpload), // delete all the unused assets - deleteKVBulkKeyValue( - accountId, - namespace, - Array.from(namespaceKeys), - () => {} - ), + deleteKVBulkKeyValue(accountId, namespace, Array.from(namespaceKeys)), ]); logger.log("↗️ Done syncing assets");