MCPcopy Index your code
hub / github.com/nodejs/node / lookupAndConnectMultiple

Function lookupAndConnectMultiple

lib/net.js:1658–1771  ·  view source on GitHub ↗
(
  self, async_id_symbol, lookup, host, options, dnsopts, port, localAddress, localPort, timeout,
)

Source from the content-addressed store, hash-verified

1656}
1657
1658function lookupAndConnectMultiple(
1659 self, async_id_symbol, lookup, host, options, dnsopts, port, localAddress, localPort, timeout,
1660) {
1661 defaultTriggerAsyncIdScope(self[async_id_symbol], function emitLookup() {
1662 lookup(host, dnsopts, function emitLookup(err, addresses) {
1663 // It's possible we were destroyed while looking this up.
1664 // XXX it would be great if we could cancel the promise returned by
1665 // the look up.
1666 if (!self.connecting) {
1667 return;
1668 } else if (err) {
1669 self.emit('lookup', err, undefined, undefined, host);
1670
1671 // net.createConnection() creates a net.Socket object and immediately
1672 // calls net.Socket.connect() on it (that's us). There are no event
1673 // listeners registered yet so defer the error event to the next tick.
1674 process.nextTick(connectErrorNT, self, err);
1675 return;
1676 }
1677
1678 // Filter addresses by only keeping the one which are either IPv4 or IPV6.
1679 // The first valid address determines which group has preference on the
1680 // alternate family sorting which happens later.
1681 const validAddresses = [[], []];
1682 const validIps = [[], []];
1683 let destinations;
1684 for (let i = 0, l = addresses.length; i < l; i++) {
1685 const address = addresses[i];
1686 const { address: ip, family: addressType } = address;
1687 self.emit('lookup', err, ip, addressType, host);
1688 // It's possible we were destroyed while looking this up.
1689 if (!self.connecting) {
1690 return;
1691 }
1692 if (isIP(ip) && (addressType === 4 || addressType === 6)) {
1693 destinations ||= addressType === 6 ? { 6: 0, 4: 1 } : { 4: 0, 6: 1 };
1694
1695 const destination = destinations[addressType];
1696
1697 // Only try an address once
1698 if (!ArrayPrototypeIncludes(validIps[destination], ip)) {
1699 ArrayPrototypePush(validAddresses[destination], address);
1700 ArrayPrototypePush(validIps[destination], ip);
1701 }
1702 }
1703 }
1704
1705
1706 // When no AAAA or A records are available, fail on the first one
1707 if (!validAddresses[0].length && !validAddresses[1].length) {
1708 const { address: firstIp, family: firstAddressType } = addresses[0];
1709
1710 if (!isIP(firstIp)) {
1711 err = new ERR_INVALID_IP_ADDRESS(firstIp);
1712 process.nextTick(connectErrorNT, self, err);
1713 } else if (firstAddressType !== 4 && firstAddressType !== 6) {
1714 err = new ERR_INVALID_ADDRESS_FAMILY(firstAddressType,
1715 options.host,

Callers 1

lookupAndConnectFunction · 0.85

Calls 5

isIPFunction · 0.85
lookupFunction · 0.70
debugFunction · 0.50
emitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…