From 21dfe7fd20d4bc599126a9aae6698380716ef084 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 27 Nov 2023 03:08:47 +0100 Subject: [PATCH 1/4] chore: less async await --- lib/agent.js | 8 +++--- lib/api/readable.js | 8 +++--- lib/client.js | 4 +-- lib/fetch/body.js | 2 +- lib/fetch/index.js | 4 +-- lib/pool-base.js | 4 +-- lib/proxy-agent.js | 66 +++++++++++++++++++++------------------------ 7 files changed, 47 insertions(+), 49 deletions(-) diff --git a/lib/agent.js b/lib/agent.js index 0b18f2a91bd..8f2e1750d55 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -118,7 +118,7 @@ class Agent extends DispatcherBase { return dispatcher.dispatch(opts, handler) } - async [kClose] () { + [kClose] () { const closePromises = [] for (const ref of this[kClients].values()) { const client = ref.deref() @@ -128,10 +128,10 @@ class Agent extends DispatcherBase { } } - await Promise.all(closePromises) + return Promise.all(closePromises) } - async [kDestroy] (err) { + [kDestroy] (err) { const destroyPromises = [] for (const ref of this[kClients].values()) { const client = ref.deref() @@ -141,7 +141,7 @@ class Agent extends DispatcherBase { } } - await Promise.all(destroyPromises) + return Promise.all(destroyPromises) } } diff --git a/lib/api/readable.js b/lib/api/readable.js index 89913eaa621..eefcf279f4f 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -209,12 +209,14 @@ function isUnusable (self) { return util.isDisturbed(self) || isLocked(self) } -async function consume (stream, type) { +function consume (stream, type) { if (isUnusable(stream)) { - throw new TypeError('unusable') + return Promise.reject(new TypeError('unusable')) } - assert(!stream[kConsume]) + if (stream[kConsume]) { + return Promise.reject(new assert.AssertionError('null != true')) + } return new Promise((resolve, reject) => { stream[kConsume] = { diff --git a/lib/client.js b/lib/client.js index 22cb39039da..09cc57a1e4b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -380,7 +380,7 @@ class Client extends DispatcherBase { return this[kNeedDrain] < 2 } - async [kClose] () { + [kClose] () { // TODO: for H2 we need to gracefully flush the remaining enqueued // request and close each stream. return new Promise((resolve) => { @@ -392,7 +392,7 @@ class Client extends DispatcherBase { }) } - async [kDestroy] (err) { + [kDestroy] (err) { return new Promise((resolve) => { const requests = this[kQueue].splice(this[kPendingIdx]) for (let i = 0; i < requests.length; i++) { diff --git a/lib/fetch/body.js b/lib/fetch/body.js index fd8481b796d..8f4f7ab5b01 100644 --- a/lib/fetch/body.js +++ b/lib/fetch/body.js @@ -235,7 +235,7 @@ function extractBody (object, keepalive = false) { return controller.desiredSize > 0 }, async cancel (reason) { - await iterator.return() + return iterator.return() }, type: undefined }) diff --git a/lib/fetch/index.js b/lib/fetch/index.js index c109a01bf1f..2c6323e5d3b 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -1808,10 +1808,10 @@ async function httpNetworkFetch ( fetchParams.controller.controller = controller }, async pull (controller) { - await pullAlgorithm(controller) + pullAlgorithm(controller) }, async cancel (reason) { - await cancelAlgorithm(reason) + cancelAlgorithm(reason) } }, { diff --git a/lib/pool-base.js b/lib/pool-base.js index 2a909eee083..3c88562ac95 100644 --- a/lib/pool-base.js +++ b/lib/pool-base.js @@ -111,7 +111,7 @@ class PoolBase extends DispatcherBase { return this[kStats] } - async [kClose] () { + [kClose] () { if (this[kQueue].isEmpty()) { return Promise.all(this[kClients].map(c => c.close())) } else { @@ -121,7 +121,7 @@ class PoolBase extends DispatcherBase { } } - async [kDestroy] (err) { + [kDestroy] (err) { while (true) { const item = this[kQueue].shift() if (!item) { diff --git a/lib/proxy-agent.js b/lib/proxy-agent.js index c710948cc5b..e9e46262306 100644 --- a/lib/proxy-agent.js +++ b/lib/proxy-agent.js @@ -82,40 +82,38 @@ class ProxyAgent extends DispatcherBase { this[kClient] = clientFactory(resolvedUrl, { connect }) this[kAgent] = new Agent({ ...opts, - connect: async (opts, callback) => { + connect: (opts, callback) => { let requestedHost = opts.host if (!opts.port) { requestedHost += `:${defaultProtocolPort(opts.protocol)}` } - try { - const { socket, statusCode } = await this[kClient].connect({ - origin, - port, - path: requestedHost, - signal: opts.signal, - headers: { - ...this[kProxyHeaders], - host - } - }) - if (statusCode !== 200) { - socket.on('error', () => {}).destroy() - callback(new RequestAbortedError('Proxy response !== 200 when HTTP Tunneling')) - } - if (opts.protocol !== 'https:') { - callback(null, socket) - return + this[kClient].connect({ + origin, + port, + path: requestedHost, + signal: opts.signal, + headers: { + ...this[kProxyHeaders], + host } - let servername - if (this[kRequestTls]) { - servername = this[kRequestTls].servername - } else { - servername = opts.servername - } - this[kConnectEndpoint]({ ...opts, servername, httpSocket: socket }, callback) - } catch (err) { - callback(err) - } + }).catch(callback) + .then(({ socket, statusCode }) => { + if (statusCode !== 200) { + socket.on('error', () => { }).destroy() + callback(new RequestAbortedError('Proxy response !== 200 when HTTP Tunneling')) + } + if (opts.protocol !== 'https:') { + callback(null, socket) + return + } + let servername + if (this[kRequestTls]) { + servername = this[kRequestTls].servername + } else { + servername = opts.servername + } + this[kConnectEndpoint]({ ...opts, servername, httpSocket: socket }, callback) + }).catch(callback) } }) } @@ -136,14 +134,12 @@ class ProxyAgent extends DispatcherBase { ) } - async [kClose] () { - await this[kAgent].close() - await this[kClient].close() + [kClose] () { + return Promise.all([this[kAgent].close, this[kClient].close]) } - async [kDestroy] () { - await this[kAgent].destroy() - await this[kClient].destroy() + [kDestroy] () { + return Promise.all([this[kAgent].destroy, this[kClient].destroy]) } } From 2b49bf687af2d4b1e932c422f3376d0a3579f17a Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 27 Nov 2023 03:11:39 +0100 Subject: [PATCH 2/4] this seems also not necessary --- lib/fetch/body.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fetch/body.js b/lib/fetch/body.js index 8f4f7ab5b01..6598ea9ab30 100644 --- a/lib/fetch/body.js +++ b/lib/fetch/body.js @@ -234,7 +234,7 @@ function extractBody (object, keepalive = false) { } return controller.desiredSize > 0 }, - async cancel (reason) { + cancel (reason) { return iterator.return() }, type: undefined From 470ef8ba8a475b5b3932bbb3821b2285e6d350ca Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 27 Nov 2023 12:37:59 +0100 Subject: [PATCH 3/4] apply undisputed changes --- lib/fetch/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fetch/index.js b/lib/fetch/index.js index 2c6323e5d3b..870b4bc1245 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -1807,11 +1807,11 @@ async function httpNetworkFetch ( async start (controller) { fetchParams.controller.controller = controller }, - async pull (controller) { - pullAlgorithm(controller) + pull (controller) { + return pullAlgorithm(controller) }, - async cancel (reason) { - cancelAlgorithm(reason) + cancel (reason) { + return cancelAlgorithm(reason) } }, { From 663e2a7226c439671d2b5553d1f1fc88accd2188 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Mon, 27 Nov 2023 13:23:26 +0100 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Toni Villena --- lib/proxy-agent.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/proxy-agent.js b/lib/proxy-agent.js index e9e46262306..d115e223738 100644 --- a/lib/proxy-agent.js +++ b/lib/proxy-agent.js @@ -135,11 +135,11 @@ class ProxyAgent extends DispatcherBase { } [kClose] () { - return Promise.all([this[kAgent].close, this[kClient].close]) + return Promise.all([this[kAgent].close(), this[kClient].close()]) } [kDestroy] () { - return Promise.all([this[kAgent].destroy, this[kClient].destroy]) + return Promise.all([this[kAgent].destroy(), this[kClient].destroy()]) } }