MCPcopy
hub / github.com/nodejs/undici / buildConnector

Function buildConnector

lib/core/connect.js:62–151  ·  view source on GitHub ↗
({ allowH2, preferH2, useH2c, maxCachedSessions, socketPath, timeout, session: customSession, ...opts })

Source from the content-addressed store, hash-verified

60}
61
62function buildConnector ({ allowH2, preferH2, useH2c, maxCachedSessions, socketPath, timeout, session: customSession, ...opts }) {
63 if (maxCachedSessions != null && (!Number.isInteger(maxCachedSessions) || maxCachedSessions < 0)) {
64 throw new InvalidArgumentError('maxCachedSessions must be a positive integer or zero')
65 }
66
67 const options = { path: socketPath, ...opts }
68 const sessionCache = new SessionCache(maxCachedSessions == null ? 100 : maxCachedSessions)
69 timeout = timeout == null ? 10e3 : timeout
70 allowH2 = allowH2 != null ? allowH2 : true
71 return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {
72 let socket
73 if (protocol === 'https:') {
74 if (!tls) {
75 tls = require('node:tls')
76 }
77 servername = servername || options.servername || util.getServerName(host) || null
78
79 const sessionKey = servername || hostname
80 assert(sessionKey)
81
82 const session = customSession || sessionCache.get(sessionKey) || null
83
84 port = port || 443
85
86 socket = tls.connect({
87 highWaterMark: 16384, // TLS in node can't have bigger HWM anyway...
88 ...options,
89 servername,
90 session,
91 localAddress,
92 ALPNProtocols: allowH2 ? (preferH2 ? ['h2', 'http/1.1'] : ['http/1.1', 'h2']) : ['http/1.1'],
93 socket: httpSocket, // upgrade socket connection
94 port,
95 host: hostname
96 })
97
98 socket
99 .on('session', function (session) {
100 // TODO (fix): Can a session become invalid once established? Don't think so?
101 sessionCache.set(sessionKey, session)
102 })
103 } else {
104 assert(!httpSocket, 'httpSocket can only be sent on TLS update')
105
106 port = port || 80
107
108 socket = net.connect({
109 highWaterMark: 64 * 1024, // Same as nodejs fs streams.
110 ...options,
111 localAddress,
112 port,
113 host: hostname
114 })
115 if (useH2c === true) {
116 socket.alpnProtocol = 'h2'
117 }
118 }
119

Callers 10

constructorMethod · 0.85
constructorMethod · 0.85
constructorMethod · 0.85
constructorMethod · 0.85
constructorMethod · 0.85
ca-fingerprint.jsFile · 0.85
index.jsFile · 0.85

Calls 5

getMethod · 0.65
connectMethod · 0.65
onMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…