Skip to content

stream: catch and forward error from dest.write #55270

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

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
13 changes: 9 additions & 4 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,10 +1004,15 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
src.on('data', ondata);
function ondata(chunk) {
debug('ondata');
const ret = dest.write(chunk);
debug('dest.write', ret);
if (ret === false) {
pause();
try {
const ret = dest.write(chunk);
debug('dest.write', ret);

if (ret === false) {
pause();
}
} catch (error) {
dest.destroy(error);
}
}

Expand Down
73 changes: 73 additions & 0 deletions test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

const common = require('../common');
const assert = require('node:assert');
const { Readable, Transform, Writable } = require('node:stream');

// Pipeine objects from object mode to non-object mode should throw an error and
Copy link
Member

@dario-piotrowicz dario-piotrowicz Jan 26, 2025

Choose a reason for hiding this comment

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

@jakecastelli sorry, just out curiosity, is pipeine a typo? should it be pipeline?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is a typo, thanks for spotting it and PR welcome 👍

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 had an other look, my origin intention should be piping, I've created a quick PR here

// catch by the consumer
{
const objectReadable = Readable.from([
{ hello: 'hello' },
{ world: 'world' },
]);

const passThrough = new Transform({
transform(chunk, _encoding, cb) {
this.push(chunk);
cb(null);
},
});

passThrough.on('error', common.mustCall());

objectReadable.pipe(passThrough);

assert.rejects(async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of passThrough);
}, /ERR_INVALID_ARG_TYPE/).then(common.mustCall());
}

// The error should be properly forwarded when the readable stream is in object mode,
// the writable stream is in non-object mode, and the data is string.
{
const stringReadable = Readable.from(['hello', 'world']);

const passThrough = new Transform({
transform(chunk, _encoding, cb) {
this.push(chunk);
throw new Error('something went wrong');
},
});

passThrough.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'something went wrong');
}));

stringReadable.pipe(passThrough);
}

// The error should be properly forwarded when the readable stream is in object mode,
// the writable stream is in non-object mode, and the data is buffer.
{
const binaryData = Buffer.from('binary data');

const binaryReadable = new Readable({
read() {
this.push(binaryData);
this.push(null);
}
});

const binaryWritable = new Writable({
write(chunk, _encoding, cb) {
throw new Error('something went wrong');
}
});

binaryWritable.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'something went wrong');
}));
binaryReadable.pipe(binaryWritable);
}
Loading