Skip to content
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

stream: preserve object mode in compose #47413

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/streams/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module.exports = function compose(...streams) {
d = new Duplex({
// TODO (ronag): highWaterMark?
writableObjectMode: !!head?.writableObjectMode,
readableObjectMode: !!tail?.writableObjectMode,
readableObjectMode: !!tail?.readableObjectMode,
writable,
readable,
});
Expand Down
74 changes: 74 additions & 0 deletions test/parallel/test-stream-compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,77 @@ const assert = require('assert');
assert.strictEqual(buf, 'HELLOWORLD');
}));
}

{
// In the new stream than should use the writeable of the first stream and readable of the last stream
// #46829
(async () => {
const newStream = compose(
new PassThrough({
// reading FROM you in object mode or not
readableObjectMode: false,

// writing TO you in object mode or not
writableObjectMode: false,
}),
new Transform({
// reading FROM you in object mode or not
readableObjectMode: true,

// writing TO you in object mode or not
writableObjectMode: false,
transform: (chunk, encoding, callback) => {
callback(null, {
value: chunk.toString()
});
}
})
);

assert.strictEqual(newStream.writableObjectMode, false);
assert.strictEqual(newStream.readableObjectMode, true);

newStream.write('Steve Rogers');
newStream.write('On your left');

newStream.end();

assert.deepStrictEqual(await newStream.toArray(), [{ value: 'Steve Rogers' }, { value: 'On your left' }]);
})().then(common.mustCall());
}

{
// In the new stream than should use the writeable of the first stream and readable of the last stream
// #46829
(async () => {
const newStream = compose(
new PassThrough({
// reading FROM you in object mode or not
readableObjectMode: true,

// writing TO you in object mode or not
writableObjectMode: true,
}),
new Transform({
// reading FROM you in object mode or not
readableObjectMode: false,

// writing TO you in object mode or not
writableObjectMode: true,
transform: (chunk, encoding, callback) => {
callback(null, chunk.value);
}
})
);

assert.strictEqual(newStream.writableObjectMode, true);
assert.strictEqual(newStream.readableObjectMode, false);

newStream.write({ value: 'Steve Rogers' });
newStream.write({ value: 'On your left' });

newStream.end();

assert.deepStrictEqual(await newStream.toArray(), [Buffer.from('Steve RogersOn your left')]);
})().then(common.mustCall());
}