Skip to content

Commit

Permalink
http: make client .aborted boolean
Browse files Browse the repository at this point in the history
PR-URL: #20230
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ronag authored and vsemozhetbyt committed May 14, 2018
1 parent 50b334b commit 4b00c4f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
10 changes: 8 additions & 2 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,16 @@ in the response to be dropped and the socket to be destroyed.
### request.aborted
<!-- YAML
added: v0.11.14
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/20230
description: The `aborted` property is no longer a timestamp number.
-->

If a request has been aborted, this value is the time when the request was
aborted, in milliseconds since 1 January 1970 00:00:00 UTC.
* {boolean}

The `request.aborted` property will be `true` if the request has
been aborted.

### request.connection
<!-- YAML
Expand Down
8 changes: 2 additions & 6 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function ClientRequest(options, cb) {

this._ended = false;
this.res = null;
this.aborted = undefined;
this.aborted = false;
this.timeoutCb = null;
this.upgradeOrConnect = false;
this.parser = null;
Expand Down Expand Up @@ -291,11 +291,7 @@ ClientRequest.prototype.abort = function abort() {
if (!this.aborted) {
process.nextTick(emitAbortNT.bind(this));
}

// Mark as aborting so we can avoid sending queued request data
// This is used as a truthy flag elsewhere. The use of Date.now is for
// debugging purposes only.
this.aborted = Date.now();
this.aborted = true;

// If we're aborting, we don't care about any more response data.
if (this.res) {
Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-http-abort-stream-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,20 @@ const server = http.createServer(common.mustCall((req, res) => {
res.end();
}));

let aborted = false;
server.listen(0, () => {

const res = common.mustCall((res) => {
res.on('data', (chunk) => {
size += chunk.length;
assert(!aborted, 'got data after abort');
assert(!req.aborted, 'got data after abort');
if (size > maxSize) {
aborted = true;
req.abort();
assert.strictEqual(req.aborted, true);
size = maxSize;
}
});

req.on('abort', common.mustCall(() => assert.strictEqual(size, maxSize)));
assert.strictEqual(req.aborted, false);
});

const req = http.get(`http://localhost:${server.address().port}`, res);
Expand Down

0 comments on commit 4b00c4f

Please sign in to comment.