From 81cf3d8f48187bc0115031c236990629f5eada47 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 10 Jan 2021 11:03:35 +0100 Subject: [PATCH 1/2] http: cleanup ClientRequest oncreate --- lib/_http_client.js | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index b81ffa1cefc573..647c60c60e51ce 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -44,6 +44,7 @@ const { const net = require('net'); const url = require('url'); const assert = require('internal/assert'); +const { once } = require('internal/util'); const { _checkIsHttpToken: checkIsHttpToken, debug, @@ -240,8 +241,6 @@ function ClientRequest(input, options, cb) { this.host = host; this.protocol = protocol; - let called = false; - if (this.agent) { // If there is an agent we should default to Connection:keep-alive, // but only if the Agent will actually reuse the connection! @@ -305,18 +304,6 @@ function ClientRequest(input, options, cb) { options.headers); } - const oncreate = (err, socket) => { - if (called) - return; - called = true; - if (err) { - process.nextTick(() => this.emit('error', err)); - return; - } - this.onSocket(socket); - this._deferToConnect(null, null, () => this._flush()); - }; - // initiate connection if (this.agent) { this.agent.addRequest(this, options); @@ -325,12 +312,24 @@ function ClientRequest(input, options, cb) { this._last = true; this.shouldKeepAlive = false; if (typeof options.createConnection === 'function') { - const newSocket = options.createConnection(options, oncreate); - if (newSocket && !called) { - called = true; - this.onSocket(newSocket); - } else { - return; + const oncreate = once((err, socket) => { + if (err) { + process.nextTick(() => this.emit('error', err)); + } else { + this.onSocket(socket); + this._deferToConnect(null, null, () => this._flush()); + } + }); + + try { + const newSocket = options.createConnection(options, oncreate); + if (newSocket) { + oncreate(null, newSocket); + } else { + return; + } + } catch (err) { + oncreate(err); } } else { debug('CLIENT use net.createConnection', options); From 57f81ed4b40613886e03b4c1bc5c993873f5c34d Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 11 Jan 2021 10:15:36 +0100 Subject: [PATCH 2/2] fixup --- lib/_http_client.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 647c60c60e51ce..5048794f274397 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -317,7 +317,6 @@ function ClientRequest(input, options, cb) { process.nextTick(() => this.emit('error', err)); } else { this.onSocket(socket); - this._deferToConnect(null, null, () => this._flush()); } }); @@ -325,8 +324,6 @@ function ClientRequest(input, options, cb) { const newSocket = options.createConnection(options, oncreate); if (newSocket) { oncreate(null, newSocket); - } else { - return; } } catch (err) { oncreate(err); @@ -336,8 +333,6 @@ function ClientRequest(input, options, cb) { this.onSocket(net.createConnection(options)); } } - - this._deferToConnect(null, null, () => this._flush()); } ObjectSetPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype); ObjectSetPrototypeOf(ClientRequest, OutgoingMessage); @@ -825,6 +820,7 @@ function onSocketNT(req, socket, err) { _destroy(req, null, err); } else { tickOnSocket(req, socket); + req._flush(); } }