-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Readable.fromWeb doesn't end on empty string #56474
Comments
import { Readable } from "node:stream";
async function *yieldEmpty() {
yield ''
}
const stream = Readable.from(yieldEmpty());
stream.on('data', chunk => {
console.log(`data: ${chunk.toString()}`)
})
stream.on('end', () => {
console.log('ended')
}); output:
Is it possibly something to look into in |
I believe it's an issue with webstreams import { Readable } from "node:stream";
const webstream = new ReadableStream({
start (controller) {
controller.enqueue(new Uint8Array()) // new Uint8Array([0x20]) works
controller.close()
}
})
const stream = Readable.fromWeb(webstream);
stream.on('data', chunk => {
console.log(`data: ${chunk}`)
})
stream.on('end', () => {
console.log('ended')
}); |
Thank you for taking the time, @jakecastelli . Sorry, I must have expressed myself a little clumsily... Yes, I am assuming the const iterable = {
async *[Symbol.asyncIterator]() {
yield "";
},
};
const response = new Response(iterable);
const reader = response.body.getReader();
reader.read().finally(() => {
console.log("done");
}); In node.js the read request is never finishing as soon as the iterator returns an empty string. I think this is actually the cause of the odd @KhafraDev : Hehe, you replied quicker than me. Yes... I haven't used a |
In the first repro you provided it can be replicated without Response, for the newer one there is a bug in undici. There is likely a bug in Readable.fromWeb as well. |
It is actually not an issue with the WebStreams, but rather how the Redeable works. When in byte mode, if the chunk is undefined or the length is equal or less than zero, it does not emit the Seems to be done by design, but cannot really assure that; maybe @ronag has more insights? |
But it also skipped the |
I have put some time to debug this, looks like the code never pass line 150 node/lib/internal/streams/from.js Lines 150 to 155 in 0e7ec5e
|
Ha, interesting; when I ran @KhafraDev example as well as the one from the Issue, I get the |
btw, the function that parses the node/lib/internal/webstreams/adapters.js Lines 515 to 516 in 542006a
or maybe I'm missing something? |
Interesting that you can get import { Readable } from "node:stream";
const response = new Response({
async *[Symbol.asyncIterator]() {
yield '' // works only with non-empty string
}
})
const stream = Readable.fromWeb(response.body);
// const stream = Readable.from('') // works as expected
stream.on('data', chunk => {
console.log(`data: ${chunk}`)
})
stream.on('end', () => {
console.log('ended')
}); if so, which node version did you use? |
Yes, tried in v22 and also the version in |
Version
v23.5.0
Platform
Subsystem
No response
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
Similar to the
Readable.from
stream, theReadable.fromWeb
stream should trigger both 'data' & 'end' events.What do you see instead?
data
&end
events are only triggered, when the response string is not empty.Additional information
No response
The text was updated successfully, but these errors were encountered: