Skip to content

Commit

Permalink
sendRequest: clean up error/response event handlers upon failure/succ…
Browse files Browse the repository at this point in the history
…ess 🐛
  • Loading branch information
derhuerst committed Oct 11, 2024
1 parent 0e5efae commit 565dfef
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/send-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const createSendRequest = (cfg, opt = {}) => {
logCtx.clientRequest = req

const res = await new Promise((resolve, reject) => {
req.once('error', (err) => {
const onError = (err) => {
reject(err)
clearTimeout(abortTimer)

Expand All @@ -162,8 +162,16 @@ const createSendRequest = (cfg, opt = {}) => {
err,
}, `failed to send client request: ${err.message}`)
req.destroy(err)
})
req.once('response', resolve)
}

// todo: is this necessary for req/res to be GCed?
const cleanUpAndResolve = (response) => {
req.removeListener('error', onError)
req.removeListener('response', cleanUpAndResolve)
resolve(response)
}
req.on('error', onError)
req.on('response', cleanUpAndResolve)

req.end(reqBody) // send request body
})
Expand Down

0 comments on commit 565dfef

Please sign in to comment.