From c5f96279828081193f30842c64cea2ec4dd5d9f6 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 13 Jul 2019 14:34:09 +0200 Subject: [PATCH] http: don't error after http client response --- doc/api/errors.md | 5 +++++ lib/_http_client.js | 19 ++++++++++++++++++- lib/internal/errors.js | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 131773143ceca7..e5abe4d474ab0b 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1717,6 +1717,11 @@ instance.setEncoding('utf8'); An attempt was made to call [`stream.write()`][] after `stream.end()` has been called. + +### ERR_REQUEST_WRITE_AFTER_RESPONSE + +An attempt was made to call [`req.write()`][] after `'response'` has been emitted. + ### ERR_STRING_TOO_LONG diff --git a/lib/_http_client.js b/lib/_http_client.js index 4af23fa8c574f0..5e7b0a7b5ec7b5 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -46,7 +46,8 @@ const { ERR_INVALID_ARG_TYPE, ERR_INVALID_HTTP_TOKEN, ERR_INVALID_PROTOCOL, - ERR_UNESCAPED_CHARACTERS + ERR_UNESCAPED_CHARACTERS, + ERR_REQUEST_WRITE_AFTER_RESPONSE } = codes; const { getTimerDuration } = require('internal/timers'); const { @@ -302,6 +303,22 @@ ClientRequest.prototype._finish = function _finish() { OutgoingMessage.prototype._finish.call(this); }; +ClientRequest.prototype.write = function write(chunk, encoding, callback) { + if (this.res) { + this.emit('error', new ERR_REQUEST_WRITE_AFTER_RESPONSE()); + return true; + } + OutgoingMessage.prototype.write.call(this, chunk, encoding, callback); +}; + +ClientRequest.prototype.end = function end(chunk, encoding, callback) { + if (this.res) { + this.emit('error', new ERR_REQUEST_WRITE_AFTER_RESPONSE()); + return this; + } + OutgoingMessage.prototype.end.call(this, chunk, encoding, callback); +}; + ClientRequest.prototype._implicitHeader = function _implicitHeader() { if (this._header) { throw new ERR_HTTP_HEADERS_SENT('render'); diff --git a/lib/internal/errors.js b/lib/internal/errors.js index f4f4ee09be3f97..4ccacd6197ede2 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1052,6 +1052,7 @@ E('ERR_OUT_OF_RANGE', msg += ` It must be ${range}. Received ${received}`; return msg; }, RangeError); +E('ERR_REQUEST_WRITE_AFTER_RESPONSE', 'write after response', Error); E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error); E('ERR_SCRIPT_EXECUTION_INTERRUPTED', 'Script execution was interrupted by `SIGINT`', Error);