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: when value is already in buffer don't emit the next one #46813

Closed
wants to merge 6 commits into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function constructNT(stream) {
} else if (err) {
errorOrDestroy(stream, err, true);
} else {
process.nextTick(emitConstructNT, stream);
emitConstructNT(stream);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the issue, @ronag I see you added this code, is there a purpose for the next tick?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still trying to understand why it was fixed... race condition between who?

}
}

Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-stream2-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
const common = require('../common');
const assert = require('assert');
const { PassThrough, Transform } = require('stream');
const { Readable } = require('node:stream');

{
// Verify writable side consumption
Expand Down Expand Up @@ -468,3 +469,57 @@ const { PassThrough, Transform } = require('stream');
assert.strictEqual(ended, true);
}));
}

{
const createInnerTransform = () => new Transform({
objectMode: true,

construct(callback) {
this.push('header from constructor');
callback();
},

transform: (row, encoding, callback) => {
callback(null, 'transform | ' + row);
},
});

const createOuterTransform = () => {
let innerTransform;

return new Transform({
objectMode: true,

transform(row, encoding, callback) {
if (!innerTransform) {
innerTransform = createInnerTransform();
innerTransform.on('data', (data) => {
this.push(data);
});

callback();
} else if (innerTransform.write('outer | ' + row)) {
process.nextTick(callback);
} else {
innerTransform.once('drain', callback);
}
},
});
};

Readable.from([
'create InnerTransform',
'firstLine',
'secondLine',
])
.compose(createOuterTransform())
.toArray().then(
common.mustCall((value) => {
assert.deepStrictEqual(value, [
'header from constructor',
'transform | outer | firstLine',
'transform | outer | secondLine',
]);
}),
);
}