fix
This commit is contained in:
210
src/api.js
210
src/api.js
@@ -21,7 +21,7 @@ const s3 = new S3Client({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
/** Helpers **/
|
/* ---------------- helpers ---------------- */
|
||||||
|
|
||||||
const toListed = (folderKey) => (o) => {
|
const toListed = (folderKey) => (o) => {
|
||||||
const d = o.LastModified ?? new Date(0);
|
const d = o.LastModified ?? new Date(0);
|
||||||
@@ -49,34 +49,40 @@ const byNewest = (a, b) => b.sort - a.sort;
|
|||||||
const sidecarsFor = (key) => [key + ".sha256", key + ".sig"];
|
const sidecarsFor = (key) => [key + ".sha256", key + ".sig"];
|
||||||
|
|
||||||
const parseName = (fname) => {
|
const parseName = (fname) => {
|
||||||
// Expected pattern: parts[3] = platform (windows|darwin|linux), parts[4] = arch
|
// Expected convention: parts[3] = platform (windows|darwin|linux), parts[4] = arch
|
||||||
const parts = fname.split("_");
|
const parts = fname.split("_");
|
||||||
const platform = parts[3] ?? "";
|
const platform = (parts[3] ?? "").toLowerCase();
|
||||||
const arch = parts[4] ?? "";
|
const arch = parts[4] ?? "";
|
||||||
const groupId = `${parts[0] ?? ""}_${platform}_${arch}`;
|
const groupId = `${parts[0] ?? ""}_${platform}_${arch}`;
|
||||||
return { parts, platform, arch, groupId };
|
return { parts, platform, arch, groupId };
|
||||||
};
|
};
|
||||||
|
|
||||||
const windowsCompanionFromTar = (tarKey) => {
|
// Remove sidecar suffixes first, then packaging suffix to compute a common base across variants
|
||||||
// .../name.tar.gz -> .../name_setup.exe
|
const stripSidecar = (name) => {
|
||||||
const base = tarKey.slice(0, -TAR_EXT.length);
|
if (name.endsWith(".sha256")) return name.slice(0, -".sha256".length);
|
||||||
return `${base}_setup.exe`;
|
if (name.endsWith(".sig")) return name.slice(0, -".sig".length);
|
||||||
|
return name;
|
||||||
};
|
};
|
||||||
|
|
||||||
const darwinCompanionFromTar = (tarKey) => {
|
const stripPackaging = (name) => {
|
||||||
// .../name.tar.gz -> .../name.dmg
|
if (name.endsWith(TAR_EXT)) return name.slice(0, -TAR_EXT.length); // correct length
|
||||||
const base = tarKey.slice(0, -TAR_EXT.length);
|
if (name.endsWith("_setup.exe")) return name.slice(0, -"_setup.exe".length);
|
||||||
return `${base}.dmg`;
|
if (name.endsWith(".dmg")) return name.slice(0, -".dmg".length);
|
||||||
|
return name;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Public API **/
|
const baseStem = (name) => stripPackaging(stripSidecar(name));
|
||||||
|
|
||||||
|
const isTar = (name) => name.endsWith(TAR_EXT);
|
||||||
|
const isDmg = (name) => name.endsWith(".dmg");
|
||||||
|
const isSetup = (name) => name.endsWith("_setup.exe");
|
||||||
|
|
||||||
|
/* ---------------- public API ---------------- */
|
||||||
|
|
||||||
const cleanOldItems = async () => {
|
const cleanOldItems = async () => {
|
||||||
const keys = Array.from(oldKeys);
|
const keys = Array.from(oldKeys);
|
||||||
console.log(`cleaning|count|${keys.length}`);
|
console.log(`cleaning|count|${keys.length}`);
|
||||||
if (keys.length === 0) {
|
if (keys.length === 0) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletes = keys.map((Key) =>
|
const deletes = keys.map((Key) =>
|
||||||
s3
|
s3
|
||||||
@@ -84,7 +90,6 @@ const cleanOldItems = async () => {
|
|||||||
.then(() => console.log(`cleaning|key|${Key}`))
|
.then(() => console.log(`cleaning|key|${Key}`))
|
||||||
.catch((err) => console.error(`cleaning|error|${Key}`, err)),
|
.catch((err) => console.error(`cleaning|error|${Key}`, err)),
|
||||||
);
|
);
|
||||||
|
|
||||||
await Promise.allSettled(deletes);
|
await Promise.allSettled(deletes);
|
||||||
oldKeys.clear();
|
oldKeys.clear();
|
||||||
};
|
};
|
||||||
@@ -115,86 +120,117 @@ const getBucketFiles = async (folderName) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const contents = (data.Contents ?? []).filter((it) => it.Key !== folderKey);
|
const contents = (data.Contents ?? []).filter((it) => it.Key !== folderKey);
|
||||||
|
|
||||||
// Normalize listing
|
|
||||||
const listed = contents.map(toListed(folderKey)).sort(byNewest);
|
const listed = contents.map(toListed(folderKey)).sort(byNewest);
|
||||||
|
|
||||||
// Consider only .tar.gz for retention policy
|
// Build indexes
|
||||||
const tarItems = listed.filter((i) => i.name.endsWith(TAR_EXT));
|
const byKey = Object.fromEntries(listed.map((i) => [i.key, i]));
|
||||||
|
const byBase = new Map(); // base -> { tar, dmg, setup, sidecars:Set<string> }
|
||||||
// Nightly policy: keep top 3 per (name_platform_arch); mark others + companions for deletion
|
for (const it of listed) {
|
||||||
const keepSet = new Set();
|
const base = baseStem(it.name);
|
||||||
if (folder === "nightly") {
|
let g = byBase.get(base);
|
||||||
const groupCounts = {};
|
if (!g) {
|
||||||
for (const t of tarItems) {
|
g = { tar: null, dmg: null, setup: null, sidecars: new Set() };
|
||||||
const { groupId, platform } = parseName(t.name);
|
byBase.set(base, g);
|
||||||
groupCounts[groupId] = (groupCounts[groupId] ?? 0) + 1;
|
}
|
||||||
const count = groupCounts[groupId];
|
if (isTar(it.name)) {
|
||||||
|
g.tar = it;
|
||||||
if (count <= 3) {
|
} else if (isDmg(it.name)) {
|
||||||
keepSet.add(t.key);
|
g.dmg = it;
|
||||||
} else {
|
} else if (isSetup(it.name)) {
|
||||||
[t.key, ...sidecarsFor(t.key)].forEach((k) => oldKeys.add(k));
|
g.setup = it;
|
||||||
|
} else if (it.name.endsWith(".sha256") || it.name.endsWith(".sig")) {
|
||||||
if (platform === "windows") {
|
g.sidecars.add(it.key);
|
||||||
const setupKey = windowsCompanionFromTar(t.key);
|
|
||||||
[setupKey, ...sidecarsFor(setupKey)].forEach((k) => oldKeys.add(k));
|
|
||||||
} else if (platform === "darwin") {
|
|
||||||
const dmgKey = darwinCompanionFromTar(t.key);
|
|
||||||
[dmgKey, ...sidecarsFor(dmgKey)].forEach((k) => oldKeys.add(k));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
tarItems.forEach((t) => keepSet.add(t.key));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build final output list (tar + sidecars + optional platform companion + its sidecars)
|
const groups = Array.from(byBase.values());
|
||||||
const byKey = Object.fromEntries(listed.map((i) => [i.key, i]));
|
|
||||||
|
// Nightly retention: keep newest 3 per (name_platform_arch).
|
||||||
|
const keepBaseSet = new Set();
|
||||||
|
if (folder === "nightly") {
|
||||||
|
const buckets = new Map(); // groupId -> array of group objects
|
||||||
|
for (const g of groups) {
|
||||||
|
const rep = g.tar ?? g.dmg ?? g.setup;
|
||||||
|
if (!rep) continue;
|
||||||
|
const { groupId } = parseName(rep.name);
|
||||||
|
if (!buckets.has(groupId)) buckets.set(groupId, []);
|
||||||
|
buckets.get(groupId).push(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [, arr] of buckets) {
|
||||||
|
arr.sort((a, b) => {
|
||||||
|
const ta = (a.tar ?? a.dmg ?? a.setup)?.sort ?? 0;
|
||||||
|
const tb = (b.tar ?? b.dmg ?? b.setup)?.sort ?? 0;
|
||||||
|
return tb - ta;
|
||||||
|
});
|
||||||
|
|
||||||
|
arr.forEach((g, idx) => {
|
||||||
|
const rep = g.tar ?? g.dmg ?? g.setup;
|
||||||
|
if (!rep) return;
|
||||||
|
const base = baseStem(rep.name);
|
||||||
|
if (idx < 3) {
|
||||||
|
keepBaseSet.add(base);
|
||||||
|
} else {
|
||||||
|
// mark all artifacts in the group as old
|
||||||
|
if (g.tar)
|
||||||
|
[g.tar.key, ...sidecarsFor(g.tar.key)].forEach((k) =>
|
||||||
|
oldKeys.add(k),
|
||||||
|
);
|
||||||
|
if (g.dmg)
|
||||||
|
[g.dmg.key, ...sidecarsFor(g.dmg.key)].forEach((k) =>
|
||||||
|
oldKeys.add(k),
|
||||||
|
);
|
||||||
|
if (g.setup)
|
||||||
|
[g.setup.key, ...sidecarsFor(g.setup.key)].forEach((k) =>
|
||||||
|
oldKeys.add(k),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// keep all groups in non-nightly
|
||||||
|
for (const g of groups) {
|
||||||
|
const rep = g.tar ?? g.dmg ?? g.setup;
|
||||||
|
if (rep) keepBaseSet.add(baseStem(rep.name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit kept groups in newest-first order, using representative timestamp
|
||||||
|
const kept = groups
|
||||||
|
.filter((g) => {
|
||||||
|
const rep = g.tar ?? g.dmg ?? g.setup;
|
||||||
|
return rep && keepBaseSet.has(baseStem(rep.name));
|
||||||
|
})
|
||||||
|
.sort((a, b) => {
|
||||||
|
const ta = (a.tar ?? a.dmg ?? a.setup)?.sort ?? 0;
|
||||||
|
const tb = (b.tar ?? b.dmg ?? b.setup)?.sort ?? 0;
|
||||||
|
return tb - ta;
|
||||||
|
});
|
||||||
|
|
||||||
const out = [];
|
const out = [];
|
||||||
|
for (const g of kept) {
|
||||||
for (const t of tarItems) {
|
// tar + sidecars
|
||||||
if (!keepSet.has(t.key)) {
|
if (g.tar) {
|
||||||
continue;
|
out.push(g.tar);
|
||||||
}
|
for (const sk of sidecarsFor(g.tar.key)) {
|
||||||
|
const it = byKey[sk];
|
||||||
// tar.gz
|
if (it) out.push(it);
|
||||||
out.push(t);
|
|
||||||
|
|
||||||
// tar sidecars
|
|
||||||
for (const sk of sidecarsFor(t.key)) {
|
|
||||||
const it = byKey[sk];
|
|
||||||
if (it) {
|
|
||||||
out.push(it);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// dmg + sidecars
|
||||||
// platform companions
|
if (g.dmg) {
|
||||||
const { platform } = parseName(t.name);
|
out.push(g.dmg);
|
||||||
|
for (const sk of sidecarsFor(g.dmg.key)) {
|
||||||
if (platform === "windows") {
|
const it = byKey[sk];
|
||||||
const setupKey = windowsCompanionFromTar(t.key);
|
if (it) out.push(it);
|
||||||
const setupItem = byKey[setupKey];
|
|
||||||
if (setupItem) {
|
|
||||||
out.push(setupItem);
|
|
||||||
for (const sk of sidecarsFor(setupKey)) {
|
|
||||||
const it = byKey[sk];
|
|
||||||
if (it) {
|
|
||||||
out.push(it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (platform === "darwin") {
|
}
|
||||||
const dmgKey = darwinCompanionFromTar(t.key);
|
// setup + sidecars
|
||||||
const dmgItem = byKey[dmgKey];
|
if (g.setup) {
|
||||||
if (dmgItem) {
|
out.push(g.setup);
|
||||||
out.push(dmgItem);
|
for (const sk of sidecarsFor(g.setup.key)) {
|
||||||
for (const sk of sidecarsFor(dmgKey)) {
|
const it = byKey[sk];
|
||||||
const it = byKey[sk];
|
if (it) out.push(it);
|
||||||
if (it) {
|
|
||||||
out.push(it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user