Skip to content

Commit

Permalink
stream: catch and forward error from dest.write
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecastelli committed Oct 18, 2024
1 parent 7ae193d commit 52d26c2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
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
71 changes: 71 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,71 @@
'use strict';

/**
* source is an object mode stream that only flows strings/buffer and the error is unrelated
* source is a binary stream and dest is a binary stream
*/
const common = require('../common');
const assert = require('node:assert');
const { Readable, Transform, Writable } = require('node:stream');

{
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());
}

{
const stringReadable = Readable.from(['hello', 'world']);

const passThrough = new Transform({

Check failure on line 37 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 4
transform(chunk, _encoding, cb) {

Check failure on line 38 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 6
this.push(chunk);

Check failure on line 39 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 6 spaces but found 8
throw new Error('something went wrong');

Check failure on line 40 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 6 spaces but found 8
},

Check failure on line 41 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 6
});

Check failure on line 42 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 4

passThrough.on('error', common.mustCall((err) => {

Check failure on line 44 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 4
assert.strictEqual(err.message, 'something went wrong');

Check failure on line 45 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 6
}));

Check failure on line 46 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 4

stringReadable.pipe(passThrough);

Check failure on line 48 in test/parallel/test-stream-pipe-objectmode-to-non-objectmode.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 4
}

{
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);
}

0 comments on commit 52d26c2

Please sign in to comment.