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

Fix stream error propagation for node v16+ #96

Merged
merged 3 commits into from
Jun 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 21 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internals.parse = async function (req, tap, options, contentType) {
// Tap request

if (tap) {
source = internals.pipe(source, tap);
[source] = internals.pipe(source, tap);
}

// Multipart
Expand Down Expand Up @@ -132,7 +132,9 @@ internals.decoder = function (source, options) {
return orig.call(stream, event, ...args);
};

return internals.pipe(source, stream);
[source] = internals.pipe(source, stream);

return source;
};


Expand All @@ -150,7 +152,7 @@ internals.raw = async function (req, tap, options) {
// Setup source

if (tap) {
source = internals.pipe(source, tap);
[source] = internals.pipe(source, tap);
}

// Output: 'stream'
Expand Down Expand Up @@ -315,8 +317,8 @@ internals.writeFile = function (req, options, stream) {
file.removeListener('error', finalize);

if (err) {
stream.unpipe(counter);
counter.unpipe(file);
unpipeStreamToCounter();
unpipeCounterToFile();

file.destroy();
Fs.unlink(path, (/* fsErr */) => reject(err)); // Ignore unlink errors
Expand Down Expand Up @@ -345,8 +347,8 @@ internals.writeFile = function (req, options, stream) {
const onAbort = () => finalize(Boom.badRequest('Client connection aborted'));
req.once('aborted', onAbort);

internals.pipe(stream, counter);
internals.pipe(counter, file);
const [, unpipeStreamToCounter] = internals.pipe(stream, counter);
const [, unpipeCounterToFile] = internals.pipe(counter, file);
});

promise.catch(Hoek.ignore); // Prevent triggering node's PromiseRejectionHandledWarning
Expand Down Expand Up @@ -392,15 +394,22 @@ internals.part = async function (part, output, set, options) {

internals.pipe = function (from, to) {

from.once('error', (err) => {
const forwardError = (err) => {

from.unpipe(to);
unpipe();
to.emit('error', err);
});
};

return from.pipe(to);
};
const unpipe = () => {

from.removeListener('error', forwardError);
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 line here is really what this fix is all about. Previously we would unpipe but continue to forward along errors to other streams, and now we stop that error propagation when unpiping.

return from.unpipe(to);
};

from.once('error', forwardError);

return [from.pipe(to), unpipe];
};

internals.Counter = class extends Stream.Transform {

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/eslint-plugin": "*",
"@hapi/eslint-plugin": "5.x.x",
"@hapi/lab": "24.x.x",
"form-data": "3.x.x"
},
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ describe('parse()', () => {
req.abort();

const incoming = await receive;
await expect(Subtext.parse(incoming, null, { parse: false, output: 'file', uploads: path })).to.reject();
await expect(Subtext.parse(incoming, null, { parse: false, output: 'file', uploads: path })).to.reject(/Client connection aborted/);
expect(Fs.readdirSync(path).length).to.equal(count);
});

Expand Down