[desktop] Ensure cached zips are also marked closeable on errors

This commit is contained in:
Manav Rathi
2024-06-26 10:24:04 +05:30
parent 2d3e3c91d2
commit a3bb8fa911
3 changed files with 29 additions and 23 deletions

View File

@@ -9,21 +9,22 @@ import { clearOpenZipCache, markClosableZip, openZip } from "./zip";
export const listZipItems = async (zipPath: string): Promise<ZipItem[]> => {
const zip = openZip(zipPath);
const entries = await zip.entries();
const entryNames: string[] = [];
try {
const entries = await zip.entries();
const entryNames: string[] = [];
for (const entry of Object.values(entries)) {
const basename = path.basename(entry.name);
// Ignore "hidden" files (files whose names begins with a dot).
if (entry.isFile && !basename.startsWith(".")) {
// `entry.name` is the path within the zip.
entryNames.push(entry.name);
for (const entry of Object.values(entries)) {
const basename = path.basename(entry.name);
// Ignore "hidden" files (files whose names begins with a dot).
if (entry.isFile && !basename.startsWith(".")) {
// `entry.name` is the path within the zip.
entryNames.push(entry.name);
}
}
return entryNames.map((entryName) => [zipPath, entryName]);
} finally {
markClosableZip(zipPath);
}
markClosableZip(zipPath);
return entryNames.map((entryName) => [zipPath, entryName]);
};
export const pathOrZipItemSize = async (
@@ -35,14 +36,16 @@ export const pathOrZipItemSize = async (
} else {
const [zipPath, entryName] = pathOrZipItem;
const zip = openZip(zipPath);
const entry = await zip.entry(entryName);
if (!entry)
throw new Error(
`An entry with name ${entryName} does not exist in the zip file at ${zipPath}`,
);
const size = entry.size;
await zip.close();
return size;
try {
const entry = await zip.entry(entryName);
if (!entry)
throw new Error(
`An entry with name ${entryName} does not exist in the zip file at ${zipPath}`,
);
return entry.size;
} finally {
markClosableZip(zipPath);
}
}
};

View File

@@ -129,7 +129,7 @@ const handleReadZip = async (zipPath: string, entryName: string) => {
const webReadableStream =
webReadableStreamAny as ReadableStream<Uint8Array>;
// Close the zip handle when the underlying stream closes.
// Let go of the zip handle when the underlying stream closes.
stream.on("end", () => markClosableZip(zipPath));
// While it is documented that entry.time is the modification time,

View File

@@ -129,8 +129,11 @@ export const makeFileForDataOrPathOrZipItem = async (
writeToTemporaryFile = async () => {
const [zipPath, entryName] = dataOrPathOrZipItem;
const zip = openZip(zipPath);
await zip.extract(entryName, path);
markClosableZip(zipPath);
try {
await zip.extract(entryName, path);
} finally {
markClosableZip(zipPath);
}
};
}
}