Skip to content

Commit

Permalink
fix: Adapt http2 connection for Node.js 8.11.2 and Node.js 10
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
prantlf committed May 19, 2018
1 parent eabea7b commit 1663f64
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/nettime.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function nettime (options) {
prolog.push(os.EOL)
data = Buffer.concat([Buffer.from(prolog.join(os.EOL)), data])
}
return new Promise(resolve => {
return new Promise(resolve =>
fs.writeFile(outputFile, data, error => {
if (error) {
if (options.failOnOutputFileError === false) {
Expand All @@ -57,8 +57,7 @@ function nettime (options) {
}
}
resolve()
})
})
}))
}

function listenToSocket (socket) {
Expand All @@ -72,14 +71,7 @@ function nettime (options) {
.on('secureConnect', () => {
timings.tlsHandshake = getTiming()
})
.on('close', () => {
timings.socketClose = getTiming()
if (outputFile && data) {
writeOutputFile().then(returnResult)
} else {
returnResult()
}
})
.on('close', checkSocketClosed)
}

function listenToResponse (response) {
Expand All @@ -103,6 +95,18 @@ function nettime (options) {
}
}

let socketClosed
function checkSocketClosed () {
if (!socketClosed) {
timings.socketClose = getTiming()
if (outputFile && data) {
writeOutputFile().then(returnResult)
} else {
returnResult()
}
}
}

const parameters = getParameters()
const httpVersion = options.httpVersion
const scheme = parameters.protocol
Expand Down Expand Up @@ -148,7 +152,8 @@ function nettime (options) {
}
})
listenToResponse(request)
request.on('end', () => client.destroy())
request.on('end', () =>
(client.close || client.destroy).call(client, checkSocketClosed))
.setEncoding('utf8')
} else {
request = protocol.request(parameters, (localResponse) => {
Expand Down

0 comments on commit 1663f64

Please sign in to comment.