Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream, http, http2: make all stream.end() call return this #18780

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/http.md
Expand Up @@ -513,11 +513,16 @@ See [`request.socket`][]
### request.end([data[, encoding]][, callback])
<!-- YAML
added: v0.1.90
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18780
description: This method now returns a reference to `ClientRequest`.
-->

* `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
Expand Down Expand Up @@ -1010,11 +1015,16 @@ See [`response.socket`][].
### response.end([data][, encoding][, callback])
<!-- YAML
added: v0.1.90
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18780
description: This method now returns a reference to `ServerResponse`.
-->

* `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.
Expand Down
5 changes: 5 additions & 0 deletions doc/api/http2.md
Expand Up @@ -2617,11 +2617,16 @@ See [`response.socket`][].
#### response.end([data][, encoding][, callback])
<!-- YAML
added: v8.4.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18780
description: This method now returns a reference to `ServerResponse`.
-->

* `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.
Expand Down
4 changes: 4 additions & 0 deletions doc/api/stream.md
Expand Up @@ -355,6 +355,9 @@ See also: [`writable.uncork()`][].
<!-- YAML
added: v0.9.4
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18780
description: This method now returns a reference to `writable`.
- version: v8.0.0
pr-url: https://github.com/nodejs/node/pull/11608
description: The `chunk` argument can now be a `Uint8Array` instance.
Expand All @@ -366,6 +369,7 @@ changes:
other than `null`.
* `encoding` {string} The encoding, if `chunk` is a string
* `callback` {Function} Optional callback for when the stream is finished
* Returns: {this}

Calling the `writable.end()` method signals that no more data will be written
to the [Writable][]. The optional `chunk` and `encoding` arguments allow one
Expand Down
9 changes: 4 additions & 5 deletions lib/_http_outgoing.js
Expand Up @@ -736,7 +736,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
}

if (this.finished) {
return false;
return this;
}

var uncork;
Expand Down Expand Up @@ -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)
Expand All @@ -788,7 +787,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
this._finish();
}

return ret;
return this;
};


Expand Down
2 changes: 2 additions & 0 deletions lib/_stream_writable.js
Expand Up @@ -570,6 +570,8 @@ Writable.prototype.end = function(chunk, encoding, cb) {
// ignore unnecessary end() calls.
if (!state.ending)
endWritable(this, state, cb);

return this;
};

Object.defineProperty(Writable.prototype, 'writableLength', {
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/http2/compat.js
Expand Up @@ -596,6 +596,8 @@ class Http2ServerResponse extends Stream {
this[kFinish]();
else
stream.end();

return this;
}

destroy(err) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-request-end-twice.js
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions test/parallel/test-http-request-end.js
Expand Up @@ -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'
Expand All @@ -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);
});
2 changes: 1 addition & 1 deletion test/parallel/test-http2-compat-serverrequest-end.js
Expand Up @@ -26,7 +26,7 @@ server.listen(0, common.mustCall(function() {

server.close();
}));
response.end();
assert.strictEqual(response.end(), response);
}));
}));

Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-stream-writableState-ending.js
Expand Up @@ -24,11 +24,14 @@ writable.on('finish', () => {
testStates(true, true, true);
});

writable.end('testing function end()', () => {
const result = writable.end('testing function end()', () => {
// ending, finished, ended = true.
testStates(true, true, true);
});

// end returns the writable instance
assert.strictEqual(result, writable);

// ending, ended = true.
// finished = false.
testStates(true, false, true);