Skip to content

Commit

Permalink
[desktop] Fix stream.end not always being emitted (#2296)
Browse files Browse the repository at this point in the history
This caused the reference counts to not be zero when we'd go to clear
the cache in `clearPendingUploads`.

Bug introduced in the nightly build, but didn't have any negative impact
except printing an error in the logs because of the unhandled promise
rejection.

Ref: nodejs/node#10871 (comment)
  • Loading branch information
mnvr authored Jun 26, 2024
2 parents b67af01 + c354c80 commit 3193139
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 3193139

Please sign in to comment.