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

http: return this from IncomingMessage#destroy() #32789

Merged
merged 3 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,11 @@ is finished.
### `request.destroy([error])`
<!-- YAML
added: v0.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/32789
description: The function returns `this` for consistency with other Readable
streams.
-->

* `error` {Error} Optional, an error to emit with `'error'` event.
Expand Down Expand Up @@ -1847,9 +1852,15 @@ const req = http.request({
### `message.destroy([error])`
<!-- YAML
added: v0.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/32789
description: The function returns `this` for consistency with other Readable
streams.
-->

* `error` {Error}
* Returns: {this}

Calls `destroy()` on the socket that received the `IncomingMessage`. If `error`
is provided, an `'error'` event is emitted on the socket and `error` is passed
Expand Down
4 changes: 3 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ ClientRequest.prototype.abort = function abort() {

ClientRequest.prototype.destroy = function destroy(err) {
if (this.destroyed) {
return;
return this;
}
this.destroyed = true;

Expand All @@ -365,6 +365,8 @@ ClientRequest.prototype.destroy = function destroy(err) {
} else if (err) {
this[kError] = err;
}

return this;
};

function _destroy(req, socket, err) {
Expand Down
1 change: 1 addition & 0 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ IncomingMessage.prototype.destroy = function destroy(error) {
this.destroyed = true;
if (this.socket)
this.socket.destroy(error);
return this;
};


Expand Down
4 changes: 3 additions & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
// it, since something else is destroying this connection anyway.
OutgoingMessage.prototype.destroy = function destroy(error) {
if (this.destroyed) {
return;
return this;
}
this.destroyed = true;

Expand All @@ -294,6 +294,8 @@ OutgoingMessage.prototype.destroy = function destroy(error) {
socket.destroy(error);
});
}

return this;
};


Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-client-request-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

// Test that http.ClientRequest,prototype.destroy() returns `this`.
require('../common');

const assert = require('assert');
const http = require('http');
const clientRequest = new http.ClientRequest({ createConnection: () => {} });

assert.strictEqual(clientRequest.destroyed, false);
assert.strictEqual(clientRequest.destroy(), clientRequest);
assert.strictEqual(clientRequest.destroyed, true);
assert.strictEqual(clientRequest.destroy(), clientRequest);
10 changes: 10 additions & 0 deletions test/parallel/test-http-incoming-message-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

// Test that http.IncomingMessage,prototype.destroy() returns `this`.
require('../common');

const assert = require('assert');
const http = require('http');
const incomingMessage = new http.IncomingMessage();

assert.strictEqual(incomingMessage.destroy(), incomingMessage);
13 changes: 13 additions & 0 deletions test/parallel/test-outgoing-message-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

// Test that http.OutgoingMessage,prototype.destroy() returns `this`.
require('../common');

const assert = require('assert');
const http = require('http');
const outgoingMessage = new http.OutgoingMessage();

assert.strictEqual(outgoingMessage.destroyed, false);
assert.strictEqual(outgoingMessage.destroy(), outgoingMessage);
assert.strictEqual(outgoingMessage.destroyed, true);
assert.strictEqual(outgoingMessage.destroy(), outgoingMessage);