http: make client .aborted boolean

PR-URL: https://github.com/nodejs/node/pull/20230
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Robert Nagy
2018-04-26 09:53:54 +02:00
committed by Vse Mozhet Byt
parent 50b334befd
commit 4b00c4fafa
3 changed files with 13 additions and 12 deletions

View File

@@ -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

View File

@@ -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;
@@ -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) {

View File

@@ -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);