mirror of
https://github.com/oven-sh/setup-bun.git
synced 2025-07-21 14:08:24 +02:00
chore: base
This commit is contained in:
parent
b04d87b14c
commit
e187173d21
425 changed files with 1080881 additions and 5 deletions
111
node_modules/undici/lib/core/connect.js
generated
vendored
Normal file
111
node_modules/undici/lib/core/connect.js
generated
vendored
Normal file
|
@ -0,0 +1,111 @@
|
|||
'use strict'
|
||||
|
||||
const net = require('net')
|
||||
const assert = require('assert')
|
||||
const util = require('./util')
|
||||
const { InvalidArgumentError, ConnectTimeoutError } = require('./errors')
|
||||
let tls // include tls conditionally since it is not always available
|
||||
|
||||
// TODO: session re-use does not wait for the first
|
||||
// connection to resolve the session and might therefore
|
||||
// resolve the same servername multiple times even when
|
||||
// re-use is enabled.
|
||||
|
||||
function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
|
||||
if (maxCachedSessions != null && (!Number.isInteger(maxCachedSessions) || maxCachedSessions < 0)) {
|
||||
throw new InvalidArgumentError('maxCachedSessions must be a positive integer or zero')
|
||||
}
|
||||
|
||||
const options = { path: socketPath, ...opts }
|
||||
const sessionCache = new Map()
|
||||
timeout = timeout == null ? 10e3 : timeout
|
||||
maxCachedSessions = maxCachedSessions == null ? 100 : maxCachedSessions
|
||||
|
||||
return function connect ({ hostname, host, protocol, port, servername, httpSocket }, callback) {
|
||||
let socket
|
||||
if (protocol === 'https:') {
|
||||
if (!tls) {
|
||||
tls = require('tls')
|
||||
}
|
||||
servername = servername || options.servername || util.getServerName(host) || null
|
||||
|
||||
const sessionKey = servername || hostname
|
||||
const session = sessionCache.get(sessionKey) || null
|
||||
|
||||
assert(sessionKey)
|
||||
|
||||
socket = tls.connect({
|
||||
highWaterMark: 16384, // TLS in node can't have bigger HWM anyway...
|
||||
...options,
|
||||
servername,
|
||||
session,
|
||||
socket: httpSocket, // upgrade socket connection
|
||||
port: port || 443,
|
||||
host: hostname
|
||||
})
|
||||
|
||||
socket
|
||||
.on('session', function (session) {
|
||||
// cache is disabled
|
||||
if (maxCachedSessions === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
if (sessionCache.size >= maxCachedSessions) {
|
||||
// remove the oldest session
|
||||
const { value: oldestKey } = sessionCache.keys().next()
|
||||
sessionCache.delete(oldestKey)
|
||||
}
|
||||
|
||||
sessionCache.set(sessionKey, session)
|
||||
})
|
||||
.on('error', function (err) {
|
||||
if (sessionKey && err.code !== 'UND_ERR_INFO') {
|
||||
// TODO (fix): Only delete for session related errors.
|
||||
sessionCache.delete(sessionKey)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
assert(!httpSocket, 'httpSocket can only be sent on TLS update')
|
||||
socket = net.connect({
|
||||
highWaterMark: 64 * 1024, // Same as nodejs fs streams.
|
||||
...options,
|
||||
port: port || 80,
|
||||
host: hostname
|
||||
})
|
||||
}
|
||||
|
||||
const timeoutId = timeout
|
||||
? setTimeout(onConnectTimeout, timeout, socket)
|
||||
: null
|
||||
|
||||
socket
|
||||
.setNoDelay(true)
|
||||
.once(protocol === 'https:' ? 'secureConnect' : 'connect', function () {
|
||||
clearTimeout(timeoutId)
|
||||
|
||||
if (callback) {
|
||||
const cb = callback
|
||||
callback = null
|
||||
cb(null, this)
|
||||
}
|
||||
})
|
||||
.on('error', function (err) {
|
||||
clearTimeout(timeoutId)
|
||||
|
||||
if (callback) {
|
||||
const cb = callback
|
||||
callback = null
|
||||
cb(err)
|
||||
}
|
||||
})
|
||||
|
||||
return socket
|
||||
}
|
||||
}
|
||||
|
||||
function onConnectTimeout (socket) {
|
||||
util.destroy(socket, new ConnectTimeoutError())
|
||||
}
|
||||
|
||||
module.exports = buildConnector
|
Loading…
Add table
Add a link
Reference in a new issue