Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
http: return this from ClientRequest#destroy()
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 committed May 9, 2020
commit cad76da198ff59e16d3439cbdb5793750c01f382
5 changes: 5 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
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);