Skip to content

Commit

Permalink
Rolled out listing function into its own utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Dec 1, 2023
1 parent 3009939 commit c1660ae
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,26 @@ export async function namedResolve(x) {
return output;
}

export async function quickRecursiveDelete(prefix, list_limit = 1000) {
export async function listApply(prefix, op, list_limit = 1000) {
let bound_bucket = s3.getR2Binding();
let list_options = { prefix: prefix, limit: list_limit };
let truncated = true;
let deletions = [];

while (true) {
let listing = await bound_bucket.list(list_options);
for (const f of listing.objects) {
deletions.push(f.key);
}
listing.objects.forEach(op);
truncated = listing.truncated;
if (truncated) {
list_options.cursor = listing.cursor;
} else {
break;
}
}
}

for (var i = 0; i < deletions.length; i++) {
deletions[i] = bound_bucket.delete(deletions[i]);
}
export async function quickRecursiveDelete(prefix, list_limit = 1000) {
let bound_bucket = s3.getR2Binding();
let deletions = [];
await listApply(prefix, f => { deletions.push(bound_bucket.delete(f.key)); }, /* list_limit = */ list_limit);
await Promise.all(deletions);
}
35 changes: 35 additions & 0 deletions tests/other.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ test("named resolve works as expected", async () => {
expect(eres).toEqual({});
});

test("listApply works as expected", async () => {
await other.quickUploadJson("alpha/alex.txt", [ "Union" ]);
await other.quickUploadJson("alpha/bravo/bar.txt", [ "Flyers!!!" ]);
await other.quickUploadJson("alpha/bravo2/stuff.txt", [ "Glow Map" ]);
await other.quickUploadJson("charlie/whee.txt", [ "Harmony 4 You" ]);

{
let survivors = [];
await other.listApply("alpha/", f => { survivors.push(f.key); });
survivors.sort();
expect(survivors).toEqual(["alpha/alex.txt", "alpha/bravo/bar.txt", "alpha/bravo2/stuff.txt"]);
}

{
let survivors = [];
await other.listApply("alpha/bravo", f => { survivors.push(f.key); });
survivors.sort();
expect(survivors).toEqual(["alpha/bravo/bar.txt", "alpha/bravo2/stuff.txt"]);
}

{
let survivors = [];
await other.listApply("alpha/bravo/", f => { survivors.push(f.key); });
survivors.sort();
expect(survivors).toEqual(["alpha/bravo/bar.txt"]);
}

{
let survivors = [];
await other.listApply("", f => { survivors.push(f.key); }, /* list_limit = */ 1); // forcing iteration.
survivors.sort();
expect(survivors).toEqual(["alpha/alex.txt", "alpha/bravo/bar.txt", "alpha/bravo2/stuff.txt", "charlie/whee.txt"]);
}
})

test("quick recursive delete works as expected", async () => {
var it = 0;
while (true) {
Expand Down

0 comments on commit c1660ae

Please sign in to comment.