From db706da235d80e5dadaab64328bfad9cb313be39 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 14 Jul 2019 22:11:06 +0200 Subject: stream: disallow stream methods on finished stream Invoke callback with ERR_STREAM_ALREADY_FINISHED error if `end()` is called on a finished stream. PR-URL: https://github.com/nodejs/node/pull/28687 Refs: https://github.com/nodejs/node/issues/28667 Reviewed-By: Anna Henningsen Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat Reviewed-By: Rich Trott --- lib/_http_outgoing.js | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/_http_outgoing.js') diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index d2c603bb30..32a51d120b 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -46,6 +46,7 @@ const { ERR_INVALID_CHAR, ERR_METHOD_NOT_IMPLEMENTED, ERR_STREAM_CANNOT_PIPE, + ERR_STREAM_ALREADY_FINISHED, ERR_STREAM_WRITE_AFTER_END }, hideStackFrames @@ -704,6 +705,13 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { } if (this.finished) { + if (typeof callback === 'function') { + if (!this.writableFinished) { + this.on('finish', callback); + } else { + callback(new ERR_STREAM_ALREADY_FINISHED('end')); + } + } return this; } -- cgit v1.2.3