From 3d6756da423075234a421327798537ce8a3e241d Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 14 Feb 2018 12:29:17 +0000 Subject: [PATCH 1/3] stream: writable.end should return this. --- doc/api/stream.md | 4 ++++ lib/_stream_writable.js | 2 ++ test/parallel/test-stream-writableState-ending.js | 5 ++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index 023ba4d77ba489..12dc1127118848 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -355,6 +355,9 @@ See also: [`writable.uncork()`][]. * `data` {string|Buffer} * `encoding` {string} * `callback` {Function} +* Returns: {this} Finishes sending the request. If any parts of the body are unsent, it will flush them to the stream. If the request is @@ -1010,11 +1015,16 @@ See [`response.socket`][]. ### response.end([data][, encoding][, callback]) * `data` {string|Buffer} * `encoding` {string} * `callback` {Function} +* Returns: {this} This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 425cfbcc2991b1..2f31e44294721f 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -736,7 +736,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { } if (this.finished) { - return false; + return this; } var uncork; @@ -766,12 +766,11 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { var finish = onFinish.bind(undefined, this); - var ret; if (this._hasBody && this.chunkedEncoding) { - ret = this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish); + this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish); } else { // Force a flush, HACK. - ret = this._send('', 'latin1', finish); + this._send('', 'latin1', finish); } if (uncork) @@ -788,7 +787,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { this._finish(); } - return ret; + return this; }; diff --git a/test/parallel/test-http-request-end-twice.js b/test/parallel/test-http-request-end-twice.js index 525377d2e1ccbf..47f08fd6e422f0 100644 --- a/test/parallel/test-http-request-end-twice.js +++ b/test/parallel/test-http-request-end-twice.js @@ -31,7 +31,7 @@ const server = http.Server(function(req, res) { server.listen(0, function() { const req = http.get({ port: this.address().port }, function(res) { res.on('end', function() { - assert.ok(!req.end()); + assert.strictEqual(req.end(), req); server.close(); }); res.resume(); diff --git a/test/parallel/test-http-request-end.js b/test/parallel/test-http-request-end.js index 6dd5fa4e91b1b2..a0cdcf27dd03a9 100644 --- a/test/parallel/test-http-request-end.js +++ b/test/parallel/test-http-request-end.js @@ -44,7 +44,7 @@ const server = http.Server(function(req, res) { }); server.listen(0, function() { - http.request({ + const req = http.request({ port: this.address().port, path: '/', method: 'POST' @@ -54,5 +54,9 @@ server.listen(0, function() { }).on('error', function(e) { console.log(e.message); process.exit(1); - }).end(expected); + }); + + const result = req.end(expected); + + assert.strictEqual(req, result); }); From 54facaaa28164227e801e015685759b087153b4f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 14 Feb 2018 13:39:05 +0000 Subject: [PATCH 3/3] http2: make response.end() return this --- doc/api/http2.md | 5 +++++ lib/internal/http2/compat.js | 2 ++ test/parallel/test-http2-compat-serverrequest-end.js | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/api/http2.md b/doc/api/http2.md index 8b428484c00d96..a6016725c4e0e8 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -2617,11 +2617,16 @@ See [`response.socket`][]. #### response.end([data][, encoding][, callback]) * `data` {string|Buffer} * `encoding` {string} * `callback` {Function} +* Returns: {this} This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 5e6c51377e94ba..9670843176b77e 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -596,6 +596,8 @@ class Http2ServerResponse extends Stream { this[kFinish](); else stream.end(); + + return this; } destroy(err) { diff --git a/test/parallel/test-http2-compat-serverrequest-end.js b/test/parallel/test-http2-compat-serverrequest-end.js index d34372118582db..45a678d1a950d1 100644 --- a/test/parallel/test-http2-compat-serverrequest-end.js +++ b/test/parallel/test-http2-compat-serverrequest-end.js @@ -26,7 +26,7 @@ server.listen(0, common.mustCall(function() { server.close(); })); - response.end(); + assert.strictEqual(response.end(), response); })); }));