User cancelling download error handling

This commit is contained in:
Tilman 2025-12-03 00:08:09 +01:00
parent bba3cf6ecb
commit 990ce73ecf

View file

@ -95,19 +95,48 @@ export const handler: Handlers<Data, FreshContextState> = {
// Convert Node.js stream to Web ReadableStream
const readableStream = new ReadableStream({
async start(controller) {
let isCancelled = false;
zipStream.on('data', (chunk: Buffer) => {
controller.enqueue(new Uint8Array(chunk));
try {
if (!isCancelled) {
controller.enqueue(new Uint8Array(chunk));
}
} catch (error) {
// Stream already closed/cancelled, ignore
isCancelled = true;
}
});
zipStream.on('end', () => {
controller.close();
try {
if (!isCancelled) {
controller.close();
}
} catch (error) {
// Stream already closed, ignore
}
});
zipStream.on('error', (error: Error) => {
console.error('Zip stream error:', error);
controller.error(error);
if (!isCancelled) {
console.error('Zip stream error:', error);
try {
controller.error(error);
} catch {
// Stream already closed, ignore
}
}
});
},
cancel() {
// Clean up when client cancels the download
try {
zipStream.destroy();
} catch {
// Ignore cleanup errors
}
},
});
return new Response(readableStream, {