Skip to content

Commit ded4369

Browse files
committed
improve
1 parent 6bd0777 commit ded4369

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

lib/core/connect.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const SessionCache = class WeakSessionCache {
4747
}
4848
}
4949

50+
/** @typedef {import('node:net').Socket} Socket */
51+
5052
/**
5153
* @typedef {Object} BuildConnectorOptions
5254
* @property {boolean} [allowH2=false]
@@ -69,40 +71,47 @@ const SessionCache = class WeakSessionCache {
6971
* @property {number|string|null|undefined} [port]
7072
* @property {string|null} [servername]
7173
* @property {string} localAddress
72-
* @property {import('net').Socket} httpSocket
74+
* @property {Socket} httpSocket
7375
*/
7476
/**
75-
* @typedef {(err: Error | null, socket?: import('net').Socket) => void} ConnectCallback
77+
* @typedef {((err: Error | null, socket?: Socket) => void)|null} ConnectCallback
7678
*/
7779

7880
/**
79-
* @typedef {(options: ConnectorOptions, callback: ConnectCallback) => import('net').Socket} Connector
81+
* @typedef {(options: ConnectorOptions, callback: ConnectCallback) => Socket} Connector
8082
*/
8183

8284
/**
8385
* @param {BuildConnectorOptions} param0
8486
* @returns {Connector}
8587
*/
86-
function buildConnector ({ allowH2, maxCachedSessions = 100, socketPath, timeout = 10e3, session: customSession, keepAliveInitialDelay = 60e3, ...opts }) {
87-
if (maxCachedSessions != null && (!Number.isInteger(maxCachedSessions) || maxCachedSessions < 0)) {
88+
function buildConnector ({
89+
allowH2,
90+
maxCachedSessions = 100,
91+
socketPath,
92+
timeout = 10e3,
93+
session: customSession,
94+
...opts
95+
}) {
96+
if (
97+
maxCachedSessions != null &&
98+
(!Number.isInteger(maxCachedSessions) ||
99+
maxCachedSessions < 0)
100+
) {
88101
throw new InvalidArgumentError('maxCachedSessions must be a positive integer or zero')
89102
}
90103

91104
const options = { path: socketPath, ...opts }
92105
const sessionCache = new SessionCache(maxCachedSessions)
93-
const optionsServername = options.servername
94-
const ALPNProtocols = allowH2 != null ? http2Alpn : http1Alpn
106+
const ALPNProtocols = allowH2 ? http2Alpn : http1Alpn
95107

96-
/**
97-
* @type {(options: ConnectorOptions, callback: ((err: Error | null, socket?: net.Socket) => void) | null) => net.Socket}
98-
*/
99-
return function connector ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {
108+
return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {
100109
let socket
101110
if (protocol === 'https:') {
102111
if (!tls) {
103112
tls = require('node:tls')
104113
}
105-
servername = servername || optionsServername || util.getServerName(host) || null
114+
servername = servername || options.servername || util.getServerName(host) || null
106115

107116
const sessionKey = servername || hostname
108117
assert(sessionKey)
@@ -146,6 +155,7 @@ function buildConnector ({ allowH2, maxCachedSessions = 100, socketPath, timeout
146155

147156
// Set TCP keep alive options on the socket here instead of in connect() for the case of assigning the socket
148157
if (options.keepAlive !== false) {
158+
const keepAliveInitialDelay = options.keepAliveInitialDelay === undefined ? 60e3 : options.keepAliveInitialDelay
149159
socket.setKeepAlive(true, keepAliveInitialDelay)
150160
}
151161

lib/core/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ function getHostname (host) {
281281
/**
282282
* IP addresses are not valid server names per RFC6066
283283
* Currently, the only server names supported are DNS hostnames
284-
* @param {string|null} host
284+
* @param {string|null|undefined} host
285285
* @returns {string|null}
286286
*/
287287
function getServerName (host) {

0 commit comments

Comments
 (0)