diff options
author | Robert Nagy <ronagy@icloud.com> | 2021-01-10 13:03:35 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2021-05-01 13:24:55 +0300 |
commit | e7bc37909cd76a62e5e9e012fe6d9d837af3c26f (patch) | |
tree | 110ede5072729baf959285a627102b8b394f6220 /lib | |
parent | e1c62dd9774668bc25053f48de25b8f6a87bca74 (diff) |
http: cleanup ClientRequest oncreate
PR-URL: https://github.com/nodejs/node/pull/36862
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Daniele Belardi <dwon.dnl@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/_http_client.js | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/lib/_http_client.js b/lib/_http_client.js index 0ff5ab23727..c8edada6401 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -36,6 +36,7 @@ const { const net = require('net'); const url = require('url'); const assert = require('internal/assert'); +const { once } = require('internal/util'); const { _checkIsHttpToken: checkIsHttpToken, debug, @@ -236,8 +237,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! @@ -301,18 +300,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); @@ -321,20 +308,27 @@ 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); + } + }); + + try { + const newSocket = options.createConnection(options, oncreate); + if (newSocket) { + oncreate(null, newSocket); + } + } catch (err) { + oncreate(err); } } else { debug('CLIENT use net.createConnection', options); this.onSocket(net.createConnection(options)); } } - - this._deferToConnect(null, null, () => this._flush()); } ObjectSetPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype); ObjectSetPrototypeOf(ClientRequest, OutgoingMessage); @@ -827,6 +821,7 @@ function onSocketNT(req, socket, err) { _destroy(req, null, err); } else { tickOnSocket(req, socket); + req._flush(); } } |