Skip to content

Commit

Permalink
Fix missing "readable" events (#15629)
Browse files Browse the repository at this point in the history
  • Loading branch information
heimskr authored Dec 7, 2024
1 parent cbbf88f commit cc125b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/js/node/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5371,14 +5371,18 @@ function createNativeStreamReadable(Readable) {
}

if (isClosed) {
nativeReadable.push(null);
ProcessNextTick(() => {
nativeReadable.push(null);
});
}

return remainder.byteLength > 0 ? remainder : undefined;
}

if (isClosed) {
nativeReadable.push(null);
ProcessNextTick(() => {
nativeReadable.push(null);
});
}

return view;
Expand All @@ -5390,7 +5394,9 @@ function createNativeStreamReadable(Readable) {
}

if (isClosed) {
nativeReadable.push(null);
ProcessNextTick(() => {
nativeReadable.push(null);
});
}

return view;
Expand Down
23 changes: 23 additions & 0 deletions test/js/node/stream/node-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,26 @@ it("should emit prefinish on current tick", done => {
done();
});
});

for (const size of [0x10, 0xffff, 0x10000, 0x1f000, 0x20000, 0x20010, 0x7ffff, 0x80000, 0xa0000, 0xa0010]) {
it(`should emit 'readable' with null data and 'close' exactly once each, 0x${size.toString(16)} bytes`, async () => {
const path = `${tmpdir()}/${Date.now()}.readable_and_close.txt`;
writeFileSync(path, new Uint8Array(size));
const stream = createReadStream(path);
const close_resolvers = Promise.withResolvers();
const readable_resolvers = Promise.withResolvers();

stream.on("close", () => {
close_resolvers.resolve();
});

stream.on("readable", () => {
const data = stream.read();
if (data === null) {
readable_resolvers.resolve();
}
});

await Promise.all([close_resolvers.promise, readable_resolvers.promise]);
});
}

0 comments on commit cc125b4

Please sign in to comment.