Skip to content

Commit

Permalink
[desktop] Fix stream.end not always being emitted
Browse files Browse the repository at this point in the history
  • Loading branch information
mnvr committed Jun 26, 2024
1 parent b67af01 commit c354c80
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions desktop/src/main/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> now. This doesn't go into the
Expand All @@ -130,7 +133,7 @@ const handleReadZip = async (zipPath: string, entryName: string) => {
webReadableStreamAny as ReadableStream<Uint8Array>;

// 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
Expand Down

0 comments on commit c354c80

Please sign in to comment.