From 2516488a4c3a62e7b90f919476323c7eddaf11ac Mon Sep 17 00:00:00 2001 From: Roee Kasher Date: Wed, 19 Jul 2017 06:30:07 +0300 Subject: [PATCH 1/2] http: disable OutgoingMessage pipe method OutgoingMessage should be a write-only stream, and it shouldn't be piped. This commit disables the `pipe` method by throwing an exception (if this method is called). --- lib/_http_outgoing.js | 4 ++++ test/parallel/test-outgoing-message-pipe.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/parallel/test-outgoing-message-pipe.js diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 8402bd2b61aaf4..aeb5eca2ed8076 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -889,6 +889,10 @@ OutgoingMessage.prototype.flush = internalUtil.deprecate(function() { this.flushHeaders(); }, 'OutgoingMessage.flush is deprecated. Use flushHeaders instead.', 'DEP0001'); +OutgoingMessage.prototype.pipe = function pipe() { + // OutgoingMessage should be write-only. Piping from it is disabled. + throw new Error('Pipe from a write-only stream'); +}; module.exports = { OutgoingMessage diff --git a/test/parallel/test-outgoing-message-pipe.js b/test/parallel/test-outgoing-message-pipe.js new file mode 100644 index 00000000000000..24e395148e8adf --- /dev/null +++ b/test/parallel/test-outgoing-message-pipe.js @@ -0,0 +1,15 @@ +'use strict'; +const assert = require('assert'); +const common = require('../common'); +const OutgoingMessage = require('_http_outgoing').OutgoingMessage; + +// Verify that an error is thrown upon a call to `OutgoingMessage.pipe`. + +const outgoingMessage = new OutgoingMessage(); +assert.throws( + () => { outgoingMessage.pipe(outgoingMessage); }, + (err) => { + return ((err instanceof Error) && /Pipe from a write-only stream/.test(err)); + }, + "OutgoingMessage.pipe should throw an error" +); From 4aa3f4b4514c804758c6468cd894b6cef9383f55 Mon Sep 17 00:00:00 2001 From: Roee Kasher Date: Thu, 20 Jul 2017 18:13:04 +0300 Subject: [PATCH 2/2] Review fixes. --- lib/_http_outgoing.js | 2 +- test/parallel/test-outgoing-message-pipe.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index aeb5eca2ed8076..86fdfbacb9a094 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -891,7 +891,7 @@ OutgoingMessage.prototype.flush = internalUtil.deprecate(function() { OutgoingMessage.prototype.pipe = function pipe() { // OutgoingMessage should be write-only. Piping from it is disabled. - throw new Error('Pipe from a write-only stream'); + this.emit('error', new Error('Cannot pipe, not readable')); }; module.exports = { diff --git a/test/parallel/test-outgoing-message-pipe.js b/test/parallel/test-outgoing-message-pipe.js index 24e395148e8adf..2030d8f43b09be 100644 --- a/test/parallel/test-outgoing-message-pipe.js +++ b/test/parallel/test-outgoing-message-pipe.js @@ -7,9 +7,9 @@ const OutgoingMessage = require('_http_outgoing').OutgoingMessage; const outgoingMessage = new OutgoingMessage(); assert.throws( - () => { outgoingMessage.pipe(outgoingMessage); }, + common.mustCall(() => { outgoingMessage.pipe(outgoingMessage); }), (err) => { - return ((err instanceof Error) && /Pipe from a write-only stream/.test(err)); + return ((err instanceof Error) && /Cannot pipe, not readable/.test(err)); }, - "OutgoingMessage.pipe should throw an error" + 'OutgoingMessage.pipe should throw an error' );