Skip to content

Commit 8118da7

Browse files
committed
http: OutgoingMessage.end() should return this
PR-URL: #18780 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent f6721c2 commit 8118da7

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

doc/api/http.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,16 @@ See [`request.socket`][]
544544
### request.end([data[, encoding]][, callback])
545545
<!-- YAML
546546
added: v0.1.90
547+
changes:
548+
- version: REPLACEME
549+
pr-url: https://github.com/nodejs/node/pull/18780
550+
description: This method now returns a reference to `ClientRequest`.
547551
-->
548552

549553
* `data` {string|Buffer}
550554
* `encoding` {string}
551555
* `callback` {Function}
556+
* Returns: {this}
552557

553558
Finishes sending the request. If any parts of the body are
554559
unsent, it will flush them to the stream. If the request is
@@ -1041,11 +1046,16 @@ See [`response.socket`][].
10411046
### response.end([data][, encoding][, callback])
10421047
<!-- YAML
10431048
added: v0.1.90
1049+
changes:
1050+
- version: REPLACEME
1051+
pr-url: https://github.com/nodejs/node/pull/18780
1052+
description: This method now returns a reference to `ServerResponse`.
10441053
-->
10451054

10461055
* `data` {string|Buffer}
10471056
* `encoding` {string}
10481057
* `callback` {Function}
1058+
* Returns: {this}
10491059

10501060
This method signals to the server that all of the response headers and body
10511061
have been sent; that server should consider this message complete.

lib/_http_outgoing.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
736736
}
737737

738738
if (this.finished) {
739-
return false;
739+
return this;
740740
}
741741

742742
var uncork;
@@ -766,12 +766,11 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
766766

767767
var finish = onFinish.bind(undefined, this);
768768

769-
var ret;
770769
if (this._hasBody && this.chunkedEncoding) {
771-
ret = this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish);
770+
this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish);
772771
} else {
773772
// Force a flush, HACK.
774-
ret = this._send('', 'latin1', finish);
773+
this._send('', 'latin1', finish);
775774
}
776775

777776
if (uncork)
@@ -788,7 +787,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
788787
this._finish();
789788
}
790789

791-
return ret;
790+
return this;
792791
};
793792

794793

test/parallel/test-http-request-end-twice.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const server = http.Server(function(req, res) {
3131
server.listen(0, function() {
3232
const req = http.get({ port: this.address().port }, function(res) {
3333
res.on('end', function() {
34-
assert.ok(!req.end());
34+
assert.strictEqual(req.end(), req);
3535
server.close();
3636
});
3737
res.resume();

test/parallel/test-http-request-end.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const server = http.Server(function(req, res) {
4444
});
4545

4646
server.listen(0, function() {
47-
http.request({
47+
const req = http.request({
4848
port: this.address().port,
4949
path: '/',
5050
method: 'POST'
@@ -54,5 +54,9 @@ server.listen(0, function() {
5454
}).on('error', function(e) {
5555
console.log(e.message);
5656
process.exit(1);
57-
}).end(expected);
57+
});
58+
59+
const result = req.end(expected);
60+
61+
assert.strictEqual(req, result);
5862
});

0 commit comments

Comments
 (0)