Skip to content

Commit

Permalink
perf: optimize template upload 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Dec 24, 2024
1 parent 3d50d14 commit a79b572
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions generation/sync-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from "node:path";

import { createHash } from "node:crypto";

import { HeadObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { HeadObjectCommand, ListObjectsV2Command, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";

import { rootDir } from "./utils";

Expand All @@ -31,17 +31,23 @@ const uploadTemplateSnapshot = (() => {
},
});

let existingSnapshots: string[] | undefined;

return async function upload(key: string, buffer: Buffer) {
// check if the file exists
const c0 = new HeadObjectCommand({
Bucket: R2_BUCKET,
Key: `${R2_PREFIX}/${key}.zip`,
});
const response = await r2.send(c0);
if (response.ContentLength) {
if (!existingSnapshots) {
const c0 = new ListObjectsV2Command({
Bucket: R2_BUCKET,
Prefix: R2_PREFIX,
});
const response = await r2.send(c0);
existingSnapshots = response.Contents?.map((c) => c.Key).filter((k) => k !== undefined) as string[];
}

if (existingSnapshots.includes(key)) {
console.log(`${key}.zip already exists, skipping upload`);
return;
}

const c1 = new PutObjectCommand({
Bucket: R2_BUCKET,
Key: `${R2_PREFIX}/${key}.zip`,
Expand Down

0 comments on commit a79b572

Please sign in to comment.