Repository navigation
stream.compose does not preserve 'readableObjectMode' from final stream to returned stream #46829
Description
Activity
I'm assuming this is just a typo on this line:
node/lib/internal/streams/compose.js
Line 91 in 347215e
readableObjectMode: !!tail?.writableObjectMode, You should use
.on('data')instead of.on('readable').
readableevent and.readconsume all the pending data at once.
So, the output is concatenated string for all0,1,2,3,4.https://nodejs.org/dist/latest-v18.x/docs/api/stream.html#readablereadsize
If the size argument is not specified, all of the data contained in the internal buffer will be returned.
That's absolutely true for non-object-mode streams, but I'm confident that's not expected for an object mode stream. I think the code above just should be
-readableObjectMode: !!tail?.writableObjectMode, +readableObjectMode: !!tail?.readableObjectMode,
I don't understand, the transform stream needs to be both readable and writable in object mode. Otherwise the returned stream (the one the transform is writing into) isn't in object mode (the return stream isn't in writableObjectMode anyway).
The example works if you add
writableObjectMode: trueto the transform stream (or justobjectMode: trueinstead of both).the transform stream needs to be both readable and writable in object mode
Can you clarify what you mean by that? I know my transform stream could be a writable object stream, but I don't think it's required or expected that anything that is readable-object-mode must be in writable-object-mode. In this case I'm expecting the transform to take in a byte stream and produce and object stream, essentially.
I'd expect that compose would return a stream that is:
- writable-object-mode if the first argument is writable-object-mode
- readable-object-mode if the last argument is readable-object-mode
- addedstreamIssues and PRs related to Node.js streams.Issues and PRs related to Node.js streams.
on Feb 25, 2023 I agree with @loganfsmyth and it is expected IMO given that a Transform can get bytes and write object
"For Duplex streams, objectMode can be set exclusively for either the Readable or Writable side using the readableObjectMode and writableObjectMode options respectively."
From the Docs
This logs the data:
let readInByteWriteInObject = new Transform({ // reading FROM you in object mode or not readableObjectMode: true, // writing TO you in object mode or not writableObjectMode: false, transform: function (value, enc, callback) { callback(null, { data: value }); }, }); readInByteWriteInObject.on('data', (data) => { console.log(data); }); readInByteWriteInObject.write('0 1 2 3 4')
but this does not:
let readInByteWriteInObject = new Transform({ // reading FROM you in object mode or not readableObjectMode: true, // writing TO you in object mode or not writableObjectMode: false, transform: function (value, enc, callback) { callback(null, { data: value }); }, }); const s = Readable.from("0 1 2 3 4", {objectMode: false}) .compose( readInByteWriteInObject ); s.on('data', (data) => { console.log(data); });
- added a commit that references this issue
on Apr 5, 2023 - added a commit that references this issue
on May 2, 2023 - added a commit that references this issue
on Jul 6, 2023 - added a commit that references this issue
on Jul 6, 2023 - added 2 commits that reference this issue
on Sep 7, 2024 - added a commit that references this issue
on May 22, 2026
Version
v19.7.0
Platform
No response
Subsystem
No response
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
No response
What is the expected behavior?
Should output
What do you see instead?
Additional information
No response