Comparing version 4.2.0 to 4.2.1
@@ -21,3 +21,2 @@ 'use strict' | ||
ClientClosedError, | ||
ConnectTimeoutError, | ||
SocketError, | ||
@@ -48,3 +47,2 @@ InformationalError, | ||
kNoRef, | ||
kConnectTimeoutValue, | ||
kKeepAliveDefaultTimeout, | ||
@@ -156,3 +154,9 @@ kHostHeader, | ||
if (typeof connect !== 'function') { | ||
connect = makeConnect({ ...tls, maxCachedSessions, socketPath, ...connect }) | ||
connect = makeConnect({ | ||
...tls, | ||
maxCachedSessions, | ||
socketPath, | ||
timeout: connectTimeout, | ||
...connect | ||
}) | ||
} | ||
@@ -165,3 +169,2 @@ | ||
this[kMaxHeadersSize] = maxHeaderSize || 16384 | ||
this[kConnectTimeoutValue] = connectTimeout == null ? 10e3 : connectTimeout | ||
this[kKeepAliveDefaultTimeout] = keepAliveTimeout == null ? 4e3 : keepAliveTimeout | ||
@@ -229,3 +232,3 @@ this[kKeepAliveMaxTimeout] = keepAliveMaxTimeout == null ? 600e3 : keepAliveMaxTimeout | ||
get [kConnected] () { | ||
return !!this[kSocket] && !this[kSocket][kConnecting] && !this[kSocket].destroyed | ||
return !!this[kSocket] && !this[kConnecting] && !this[kSocket].destroyed | ||
} | ||
@@ -456,3 +459,2 @@ | ||
const TIMEOUT_IDLE = 3 | ||
const TIMEOUT_CONNECT = 4 | ||
@@ -999,5 +1001,2 @@ class Parser { | ||
util.destroy(socket, new InformationalError('socket idle timeout')) | ||
} else if (timeoutType === TIMEOUT_CONNECT) { | ||
assert(!client[kConnected]) | ||
util.destroy(socket, new ConnectTimeoutError()) | ||
} | ||
@@ -1011,13 +1010,7 @@ } | ||
function onSocketConnect () { | ||
const { [kClient]: client } = this | ||
this[kConnecting] = false | ||
client.emit('connect', client[kUrl], [client]) | ||
resume(client) | ||
} | ||
function onSocketError (err) { | ||
const { [kParser]: parser } = this | ||
assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID') | ||
// On Mac OS, we get an ECONNRESET even if there is a full body to be forwarded | ||
@@ -1031,13 +1024,9 @@ // to the user. | ||
const { [kClient]: client } = this | ||
this[kError] = err | ||
if (err.code === 'ERR_TLS_CERT_ALTNAME_INVALID') { | ||
assert(client[kRunning] === 0) | ||
while (client[kPending] > 0 && client[kQueue][client[kPendingIdx]].servername === client[kServerName]) { | ||
const request = client[kQueue][client[kPendingIdx]++] | ||
errorRequest(client, request, err) | ||
} | ||
} else if ( | ||
onError(this[kClient], err) | ||
} | ||
function onError (client, err) { | ||
if ( | ||
client[kRunning] === 0 && | ||
@@ -1104,8 +1093,3 @@ err.code !== 'UND_ERR_INFO' && | ||
if (this[kConnecting]) { | ||
this[kConnecting] = false | ||
client.emit('connectionError', client[kUrl], [client], err) | ||
} else { | ||
client.emit('disconnect', client[kUrl], [client], err) | ||
} | ||
client.emit('disconnect', client[kUrl], [client], err) | ||
@@ -1116,2 +1100,3 @@ resume(client) | ||
function connect (client) { | ||
assert(!client[kConnecting]) | ||
assert(!client[kSocket]) | ||
@@ -1132,3 +1117,4 @@ | ||
const socket = client[kConnector]({ | ||
client[kConnecting] = true | ||
client[kConnector]({ | ||
host, | ||
@@ -1139,18 +1125,39 @@ hostname, | ||
servername: client[kServerName] | ||
}, onSocketConnect) | ||
}, function (err, socket) { | ||
client[kConnecting] = false | ||
client[kSocket] = socket | ||
if (err) { | ||
if (err.code === 'ERR_TLS_CERT_ALTNAME_INVALID') { | ||
assert(client[kRunning] === 0) | ||
while (client[kPending] > 0 && client[kQueue][client[kPendingIdx]].servername === client[kServerName]) { | ||
const request = client[kQueue][client[kPendingIdx]++] | ||
errorRequest(client, request, err) | ||
} | ||
} else { | ||
onError(client, err) | ||
} | ||
socket[kNoRef] = false | ||
socket[kConnecting] = true | ||
socket[kWriting] = false | ||
socket[kReset] = false | ||
socket[kError] = null | ||
socket[kParser] = new Parser(client, socket) | ||
socket[kClient] = client | ||
socket | ||
.on('error', onSocketError) | ||
.on('data', onSocketData) | ||
.on('end', onSocketEnd) | ||
.on('close', onSocketClose) | ||
client.emit('connectionError', client[kUrl], [client], err) | ||
} else { | ||
assert(socket) | ||
client[kSocket] = socket | ||
socket[kNoRef] = false | ||
socket[kWriting] = false | ||
socket[kReset] = false | ||
socket[kError] = null | ||
socket[kParser] = new Parser(client, socket) | ||
socket[kClient] = client | ||
socket | ||
.on('error', onSocketError) | ||
.on('data', onSocketData) | ||
.on('end', onSocketEnd) | ||
.on('close', onSocketClose) | ||
client.emit('connect', client[kUrl], [client]) | ||
} | ||
resume(client) | ||
}) | ||
} | ||
@@ -1204,7 +1211,3 @@ | ||
if (socket[kConnecting]) { | ||
if (socket[kParser].timeoutType !== TIMEOUT_CONNECT) { | ||
socket[kParser].setTimeout(client[kConnectTimeoutValue], TIMEOUT_CONNECT) | ||
} | ||
} else if (client[kSize] === 0) { | ||
if (client[kSize] === 0) { | ||
if (socket[kParser].timeoutType !== TIMEOUT_IDLE) { | ||
@@ -1259,2 +1262,6 @@ socket[kParser].setTimeout(client[kKeepAliveTimeoutValue], TIMEOUT_IDLE) | ||
if (client[kConnecting]) { | ||
return | ||
} | ||
if (!socket) { | ||
@@ -1265,3 +1272,3 @@ connect(client) | ||
if (socket.destroyed || socket[kConnecting] || socket[kWriting] || socket[kReset]) { | ||
if (socket.destroyed || socket[kWriting] || socket[kReset]) { | ||
return | ||
@@ -1268,0 +1275,0 @@ } |
@@ -7,3 +7,3 @@ 'use strict' | ||
const util = require('./util') | ||
const { InvalidArgumentError } = require('./errors') | ||
const { InvalidArgumentError, ConnectTimeoutError } = require('./errors') | ||
@@ -16,3 +16,3 @@ // TODO: session re-use does not wait for the first | ||
class Connector { | ||
constructor ({ maxCachedSessions, socketPath, ...opts }) { | ||
constructor ({ maxCachedSessions, socketPath, timeout, ...opts }) { | ||
if (maxCachedSessions != null && (!Number.isInteger(maxCachedSessions) || maxCachedSessions < 0)) { | ||
@@ -23,2 +23,3 @@ throw new InvalidArgumentError('maxCachedSessions must be a positive integer or zero') | ||
this.opts = { path: socketPath, ...opts } | ||
this.timeout = timeout == null ? 10e3 : timeout | ||
this.sessionCache = new Map() | ||
@@ -77,6 +78,25 @@ this.maxCachedSessions = maxCachedSessions == null ? 100 : maxCachedSessions | ||
const timeout = this.timeout | ||
? setTimeout(onConnectTimeout, this.timeout, socket) | ||
: null | ||
socket | ||
.setNoDelay(true) | ||
.once(protocol === 'https:' ? 'secureConnect' : 'connect', callback) | ||
.once(protocol === 'https:' ? 'secureConnect' : 'connect', function () { | ||
if (callback) { | ||
clearTimeout(timeout) | ||
const cb = callback | ||
callback = null | ||
cb(null, this) | ||
} | ||
}) | ||
.on('error', function (err) { | ||
if (callback) { | ||
const cb = callback | ||
callback = null | ||
cb(err) | ||
} | ||
}) | ||
return socket | ||
@@ -86,4 +106,8 @@ } | ||
function onConnectTimeout (socket) { | ||
util.destroy(socket, new ConnectTimeoutError()) | ||
} | ||
module.exports = (opts) => { | ||
return Connector.prototype.connect.bind(new Connector(opts)) | ||
} |
@@ -8,3 +8,2 @@ module.exports = { | ||
kConnecting: Symbol('connecting'), | ||
kConnectTimeoutValue: Symbol('connect timeout value'), | ||
kKeepAliveDefaultTimeout: Symbol('default keep alive timeout'), | ||
@@ -11,0 +10,0 @@ kKeepAliveMaxTimeout: Symbol('max keep alive timeout'), |
@@ -36,3 +36,12 @@ 'use strict' | ||
class Pool extends Dispatcher { | ||
constructor (origin, { connections, factory = defaultFactory, connect, tls, maxCachedSessions, socketPath, ...options } = {}) { | ||
constructor (origin, { | ||
connections, | ||
factory = defaultFactory, | ||
connect, | ||
connectTimeout, | ||
tls, | ||
maxCachedSessions, | ||
socketPath, | ||
...options | ||
} = {}) { | ||
super() | ||
@@ -53,3 +62,9 @@ | ||
if (typeof connect !== 'function') { | ||
connect = makeConnect({ ...tls, maxCachedSessions, socketPath, ...connect }) | ||
connect = makeConnect({ | ||
...tls, | ||
maxCachedSessions, | ||
socketPath, | ||
timeout: connectTimeout == null ? 10e3 : connectTimeout, | ||
...connect | ||
}) | ||
} | ||
@@ -56,0 +71,0 @@ |
{ | ||
"name": "undici", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"description": "An HTTP/1.1 client, written from scratch for Node.js", | ||
@@ -5,0 +5,0 @@ "homepage": "https://undici.nodejs.org", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
430829
65
5109