Skip to content

Commit

Permalink
http: return this from ClientRequest#destroy()
Browse files Browse the repository at this point in the history
This commit updates ClientRequest#destroy() to return `this`
for consistency with other writable streams.

PR-URL: #32789
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
cjihrig authored and codebytere committed Jun 27, 2020
1 parent c795955 commit d87031d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,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
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
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);

0 comments on commit d87031d

Please sign in to comment.