feat: speed up new project creation by copying files concurrently

This commit is contained in:
James Murdza
2024-09-15 10:29:23 -07:00
parent 0509716f34
commit 2f88ff6d58
3 changed files with 35 additions and 17 deletions

View File

@ -1,7 +1,9 @@
import { z } from "zod"
import pLimit from 'p-limit';
export interface Env {
R2: R2Bucket
Templates: R2Bucket
KEY: string
}
@ -144,17 +146,18 @@ export default {
console.log(`Copying template: ${type}`);
const templateDirectory = `templates/${type}`;
// List all objects under the directory
const { objects } = await env.R2.list({ prefix: templateDirectory });
const { objects } = await env.Templates.list({ prefix: type });
// Copy each object to the new directory
for (const { key } of objects) {
const destinationKey = key.replace(templateDirectory, `projects/${sandboxId}`);
const fileBody = await env.R2.get(key).then(res => res?.body ?? "");
// Copy each object to the new directory with a 5 concurrency limit
const limit = pLimit(5);
await Promise.all(objects.map(({ key }) =>
limit(async () => {
const destinationKey = key.replace(type, `projects/${sandboxId}`);
const fileBody = await env.Templates.get(key).then(res => res?.body ?? "");
await env.R2.put(destinationKey, fileBody);
}
})
));
return success
} else {