Skip to content

Commit cfa1f9b

Browse files
committed
Fix request abort check on Node.js 13
1 parent ef5c83c commit cfa1f9b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
"nyc": {
6060
"extension": [
6161
".ts"
62+
],
63+
"exclude": [
64+
"**/tests/**"
6265
]
6366
},
6467
"ava": {

source/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export interface IncomingMessageWithTimings extends IncomingMessage {
3434
timings?: Timings;
3535
}
3636

37+
const nodejsMajorVersion = Number(process.versions.node.split('.')[0]);
38+
3739
const timer = (request: ClientRequestWithTimings): Timings => {
3840
const timings: Timings = {
3941
start: Date.now(),
@@ -81,8 +83,9 @@ const timer = (request: ClientRequestWithTimings): Timings => {
8183
request.prependOnceListener('abort', (): void => {
8284
timings.abort = Date.now();
8385

84-
// Let the `end` response event be responsible for setting the total phase
85-
if (!timings.response) {
86+
// Let the `end` response event be responsible for setting the total phase,
87+
// unless the Node.js major version is >= 13.
88+
if (!timings.response || nodejsMajorVersion >= 13) {
8689
timings.phases.total = Date.now() - timings.start;
8790
}
8891
});

tests/test.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ test.after('cleanup', async () => {
3232
await server.closeAsync();
3333
});
3434

35+
const nodejsMajorVersion = Number(process.versions.node.split('.')[0]);
36+
3537
const error = 'Simple error';
3638

3739
const makeRequest = (url = 'https://httpbin.org/anything', options: http.RequestOptions = {agent: false}): {request: ClientRequest; timings: Timings} => {
@@ -205,13 +207,23 @@ test.cb('sets `total` on abort - after `response` event', t => {
205207
request.once('response', response => {
206208
request.abort();
207209

208-
response.once('end', () => {
209-
t.is(typeof timings.abort, 'number');
210-
t.is(timings.phases.total, timings.end! - timings.start);
211-
t.truthy((request as any).res);
212-
213-
t.end();
214-
});
210+
if (nodejsMajorVersion >= 13) {
211+
process.nextTick(() => {
212+
t.is(typeof timings.abort, 'number');
213+
t.is(timings.phases.total, timings.abort! - timings.start);
214+
t.truthy((request as any).res);
215+
216+
t.end();
217+
});
218+
} else {
219+
response.once('end', () => {
220+
t.is(typeof timings.abort, 'number');
221+
t.is(timings.phases.total, timings.end! - timings.start);
222+
t.truthy((request as any).res);
223+
224+
t.end();
225+
});
226+
}
215227
});
216228
});
217229

0 commit comments

Comments
 (0)