* Creates a promise that resolves with the IP address of the given hostname. * @param {0 | 4 | 6} family - The IP address family (4 or 6, or 0 for both). * @param {string} hostname - The hostname to resolve. * @param {boolean} all - Whether to resolve with all IP addresses for the hostname. * @p
(family, hostname, all, hints, dnsOrder)
| 133 | * @property {0 | 4 | 6} family - The IP address type. 4 for IPv4 or 6 for IPv6, or 0 (for both). |
| 134 | */ |
| 135 | function createLookupPromise(family, hostname, all, hints, dnsOrder) { |
| 136 | return new Promise((resolve, reject) => { |
| 137 | if (!hostname) { |
| 138 | reject(new ERR_INVALID_ARG_VALUE('hostname', hostname, |
| 139 | 'must be a non-empty string')); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | const matchedFamily = isIP(hostname); |
| 144 | |
| 145 | if (matchedFamily !== 0) { |
| 146 | const result = { address: hostname, family: matchedFamily }; |
| 147 | resolve(all ? [result] : result); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | const req = new GetAddrInfoReqWrap(); |
| 152 | |
| 153 | req.family = family; |
| 154 | req.hostname = hostname; |
| 155 | req.oncomplete = all ? onlookupall : onlookup; |
| 156 | req.resolve = resolve; |
| 157 | req.reject = reject; |
| 158 | |
| 159 | let order = DNS_ORDER_VERBATIM; |
| 160 | |
| 161 | if (dnsOrder === 'ipv4first') { |
| 162 | order = DNS_ORDER_IPV4_FIRST; |
| 163 | } else if (dnsOrder === 'ipv6first') { |
| 164 | order = DNS_ORDER_IPV6_FIRST; |
| 165 | } |
| 166 | |
| 167 | const err = getaddrinfo(req, hostname, family, hints, order); |
| 168 | |
| 169 | if (err) { |
| 170 | reject(new DNSException(err, 'getaddrinfo', hostname)); |
| 171 | } else if (hasObserver('dns')) { |
| 172 | const detail = { |
| 173 | hostname, |
| 174 | family, |
| 175 | hints, |
| 176 | verbatim: order === DNS_ORDER_VERBATIM, |
| 177 | order: dnsOrder, |
| 178 | }; |
| 179 | startPerf(req, kPerfHooksDnsLookupContext, { type: 'dns', name: 'lookup', detail }); |
| 180 | } |
| 181 | }); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get the IP address for a given hostname. |