From c354c80b1f68e5af3b46a42cbc92bb1e784f095f Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 26 Jun 2024 14:50:18 +0530 Subject: [PATCH] [desktop] Fix stream.end not always being emitted Ref: https://github.com/nodejs/node/issues/10871#issuecomment-650150155 --- desktop/src/main/stream.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/desktop/src/main/stream.ts b/desktop/src/main/stream.ts index beddab8488..749c94f491 100644 --- a/desktop/src/main/stream.ts +++ b/desktop/src/main/stream.ts @@ -115,12 +115,15 @@ const handleRead = async (path: string) => { const handleReadZip = async (zipPath: string, entryName: string) => { const zip = openZip(zipPath); const entry = await zip.entry(entryName); - if (!entry) return new Response("", { status: 404 }); + if (!entry) { + markClosableZip(zipPath); + return new Response("", { status: 404 }); + } // This returns an "old style" NodeJS.ReadableStream. const stream = await zip.stream(entry); // Convert it into a new style NodeJS.Readable. - const nodeReadable = new Readable().wrap(stream); + const nodeReadable = new Readable({ emitClose: true }).wrap(stream); // Then convert it into a Web stream. const webReadableStreamAny = Readable.toWeb(nodeReadable); // However, we get a ReadableStream now. This doesn't go into the @@ -130,7 +133,7 @@ const handleReadZip = async (zipPath: string, entryName: string) => { webReadableStreamAny as ReadableStream; // Let go of the zip handle when the underlying stream closes. - stream.on("end", () => markClosableZip(zipPath)); + nodeReadable.on("close", () => markClosableZip(zipPath)); // While it is documented that entry.time is the modification time, // the units are not mentioned. By seeing the source code, we can