check for stream truncation in saveToTempFile

This commit is contained in:
Hazelnoot 2025-05-08 16:43:52 -04:00
parent e75e4f11a2
commit 7bfe16cbb0

View file

@ -30,13 +30,20 @@ export function createTempDir(): Promise<[string, () => void]> {
});
}
export async function saveToTempFile(stream: NodeJS.ReadableStream): Promise<[string, () => void]> {
export async function saveToTempFile(stream: NodeJS.ReadableStream & { truncated?: boolean }): Promise<[string, () => void]> {
const [filepath, cleanup] = await createTemp();
try {
await pipeline(stream, fs.createWriteStream(filepath));
return [filepath, cleanup];
} catch (e) {
cleanup();
throw e;
}
if (stream.truncated) {
cleanup();
throw new Error('Read failed: input stream truncated');
}
return [filepath, cleanup];
}