1 | {"version":3,"file":"connector.js","names":["_net","_interopRequireDefault","require","_nodeUrl","_abortError","obj","__esModule","default","connectInParallel","options","lookup","signal","aborted","AbortError","addresses","lookupAllAddresses","host","Promise","resolve","reject","sockets","Array","length","errors","onError","err","push","removeListener","onConnect","destroy","removeEventListener","onAbort","AggregateError","j","socket","i","len","net","connect","address","family","on","addEventListener","once","connectInSequence","Error","name","isIPv6","isIPv4","url","domainToASCII","all"],"sources":["../src/connector.ts"],"sourcesContent":["import net from 'net';\nimport dns, { type LookupAddress } from 'dns';\n\nimport url from 'node:url';\nimport AbortError from './errors/abort-error';\n\ntype LookupFunction = (hostname: string, options: dns.LookupAllOptions, callback: (err: NodeJS.ErrnoException | null, addresses: dns.LookupAddress[]) => void) => void;\n\nexport async function connectInParallel(options: { host: string, port: number, localAddress?: string | undefined }, lookup: LookupFunction, signal: AbortSignal) {\n if (signal.aborted) {\n throw new AbortError();\n }\n\n const addresses = await lookupAllAddresses(options.host, lookup, signal);\n\n return await new Promise<net.Socket>((resolve, reject) => {\n const sockets = new Array(addresses.length);\n\n const errors: Error[] = [];\n\n function onError(this: net.Socket, err: Error) {\n errors.push(err);\n\n this.removeListener('error', onError);\n this.removeListener('connect', onConnect);\n\n this.destroy();\n\n if (errors.length === addresses.length) {\n signal.removeEventListener('abort', onAbort);\n\n reject(new AggregateError(errors, 'Could not connect (parallel)'));\n }\n }\n\n function onConnect(this: net.Socket) {\n signal.removeEventListener('abort', onAbort);\n\n for (let j = 0; j < sockets.length; j++) {\n const socket = sockets[j];\n\n if (this === socket) {\n continue;\n }\n\n socket.removeListener('error', onError);\n socket.removeListener('connect', onConnect);\n socket.destroy();\n }\n\n resolve(this);\n }\n\n const onAbort = () => {\n for (let j = 0; j < sockets.length; j++) {\n const socket = sockets[j];\n\n socket.removeListener('error', onError);\n socket.removeListener('connect', onConnect);\n\n socket.destroy();\n }\n\n reject(new AbortError());\n };\n\n for (let i = 0, len = addresses.length; i < len; i++) {\n const socket = sockets[i] = net.connect({\n ...options,\n host: addresses[i].address,\n family: addresses[i].family\n });\n\n socket.on('error', onError);\n socket.on('connect', onConnect);\n }\n\n signal.addEventListener('abort', onAbort, { once: true });\n });\n}\n\nexport async function connectInSequence(options: { host: string, port: number, localAddress?: string | undefined }, lookup: LookupFunction, signal: AbortSignal) {\n if (signal.aborted) {\n throw new AbortError();\n }\n\n const errors: any[] = [];\n const addresses = await lookupAllAddresses(options.host, lookup, signal);\n\n for (const address of addresses) {\n try {\n return await new Promise<net.Socket>((resolve, reject) => {\n const socket = net.connect({\n ...options,\n host: address.address,\n family: address.family\n });\n\n const onAbort = () => {\n socket.removeListener('error', onError);\n socket.removeListener('connect', onConnect);\n\n socket.destroy();\n\n reject(new AbortError());\n };\n\n const onError = (err: Error) => {\n signal.removeEventListener('abort', onAbort);\n\n socket.removeListener('error', onError);\n socket.removeListener('connect', onConnect);\n\n socket.destroy();\n\n reject(err);\n };\n\n const onConnect = () => {\n signal.removeEventListener('abort', onAbort);\n\n socket.removeListener('error', onError);\n socket.removeListener('connect', onConnect);\n\n resolve(socket);\n };\n\n signal.addEventListener('abort', onAbort, { once: true });\n\n socket.on('error', onError);\n socket.on('connect', onConnect);\n });\n } catch (err) {\n if (err instanceof Error && err.name === 'AbortError') {\n throw err;\n }\n\n errors.push(err);\n\n continue;\n }\n }\n\n throw new AggregateError(errors, 'Could not connect (sequence)');\n}\n\n/**\n * Look up all addresses for the given hostname.\n */\nexport async function lookupAllAddresses(host: string, lookup: LookupFunction, signal: AbortSignal): Promise<dns.LookupAddress[]> {\n if (signal.aborted) {\n throw new AbortError();\n }\n\n if (net.isIPv6(host)) {\n return [{ address: host, family: 6 }];\n } else if (net.isIPv4(host)) {\n return [{ address: host, family: 4 }];\n } else {\n return await new Promise<LookupAddress[]>((resolve, reject) => {\n const onAbort = () => {\n reject(new AbortError());\n };\n\n signal.addEventListener('abort', onAbort);\n\n lookup(url.domainToASCII(host), { all: true }, (err, addresses) => {\n signal.removeEventListener('abort', onAbort);\n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