summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRobert Nagy <ronagy@icloud.com>2019-07-14 22:11:06 +0200
committerRich Trott <rtrott@gmail.com>2019-08-20 08:47:19 -0700
commitdb706da235d80e5dadaab64328bfad9cb313be39 (patch)
tree58182b1324b6fe3e370bca7a377f99cff73903bf /lib
parent85898e0aca26cee2e68aea9ba384e23cd4457849 (diff)
downloadandroid-node-v8-db706da235d80e5dadaab64328bfad9cb313be39.tar.gz
android-node-v8-db706da235d80e5dadaab64328bfad9cb313be39.tar.bz2
android-node-v8-db706da235d80e5dadaab64328bfad9cb313be39.zip
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 <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/_http_outgoing.js8
-rw-r--r--lib/_stream_writable.js8
-rw-r--r--lib/internal/errors.js3
3 files changed, 19 insertions, 0 deletions
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;
}
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js
index c6d4ec4266..9a01cb1c06 100644
--- a/lib/_stream_writable.js
+++ b/lib/_stream_writable.js
@@ -41,6 +41,7 @@ const {
ERR_MULTIPLE_CALLBACK,
ERR_STREAM_CANNOT_PIPE,
ERR_STREAM_DESTROYED,
+ ERR_STREAM_ALREADY_FINISHED,
ERR_STREAM_NULL_VALUES,
ERR_STREAM_WRITE_AFTER_END,
ERR_UNKNOWN_ENCODING
@@ -591,6 +592,13 @@ Writable.prototype.end = function(chunk, encoding, cb) {
// Ignore unnecessary end() calls.
if (!state.ending)
endWritable(this, state, cb);
+ else if (typeof cb === 'function') {
+ if (!state.finished) {
+ this.once('finish', cb);
+ } else {
+ cb(new ERR_STREAM_ALREADY_FINISHED('end'));
+ }
+ }
return this;
};
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index f8c42ff9b2..5c0d328a1b 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -1128,6 +1128,9 @@ E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error);
E('ERR_SRI_PARSE',
'Subresource Integrity string %s had an unexpected at %d',
SyntaxError);
+E('ERR_STREAM_ALREADY_FINISHED',
+ 'Cannot call %s after a stream was finished',
+ Error);
E('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable', Error);
E('ERR_STREAM_DESTROYED', 'Cannot call %s after a stream was destroyed', Error);
E('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);