Skip to content

Commit 1663f64

Browse files
committed
fix: Adapt http2 connection for Node.js 8.11.2 and Node.js 10
Node.js 8.11.2 and Node.js 10 do not trigger the "close" event on the socket any more. Also, there is new method "close" on the http2 client, which replaces the earlier (still existing) "destroy". The complete end of the end of the http2 communication can be caught only in the callback of the "close" or "destroy" methods and not in the "close" event handler on the socket any more.
1 parent eabea7b commit 1663f64

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

lib/nettime.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function nettime (options) {
4646
prolog.push(os.EOL)
4747
data = Buffer.concat([Buffer.from(prolog.join(os.EOL)), data])
4848
}
49-
return new Promise(resolve => {
49+
return new Promise(resolve =>
5050
fs.writeFile(outputFile, data, error => {
5151
if (error) {
5252
if (options.failOnOutputFileError === false) {
@@ -57,8 +57,7 @@ function nettime (options) {
5757
}
5858
}
5959
resolve()
60-
})
61-
})
60+
}))
6261
}
6362

6463
function listenToSocket (socket) {
@@ -72,14 +71,7 @@ function nettime (options) {
7271
.on('secureConnect', () => {
7372
timings.tlsHandshake = getTiming()
7473
})
75-
.on('close', () => {
76-
timings.socketClose = getTiming()
77-
if (outputFile && data) {
78-
writeOutputFile().then(returnResult)
79-
} else {
80-
returnResult()
81-
}
82-
})
74+
.on('close', checkSocketClosed)
8375
}
8476

8577
function listenToResponse (response) {
@@ -103,6 +95,18 @@ function nettime (options) {
10395
}
10496
}
10597

98+
let socketClosed
99+
function checkSocketClosed () {
100+
if (!socketClosed) {
101+
timings.socketClose = getTiming()
102+
if (outputFile && data) {
103+
writeOutputFile().then(returnResult)
104+
} else {
105+
returnResult()
106+
}
107+
}
108+
}
109+
106110
const parameters = getParameters()
107111
const httpVersion = options.httpVersion
108112
const scheme = parameters.protocol
@@ -148,7 +152,8 @@ function nettime (options) {
148152
}
149153
})
150154
listenToResponse(request)
151-
request.on('end', () => client.destroy())
155+
request.on('end', () =>
156+
(client.close || client.destroy).call(client, checkSocketClosed))
152157
.setEncoding('utf8')
153158
} else {
154159
request = protocol.request(parameters, (localResponse) => {

0 commit comments

Comments
 (0)