2121
2222'use strict' ;
2323
24- const util = require ( 'util' ) ;
25-
2624const cares = process . binding ( 'cares_wrap' ) ;
27- const uv = process . binding ( 'uv' ) ;
2825const { isLegalPort } = require ( 'internal/net' ) ;
2926const { customPromisifyArgs } = require ( 'internal/util' ) ;
27+ const errors = require ( 'internal/errors' ) ;
3028
3129const {
3230 GetAddrInfoReqWrap,
@@ -36,30 +34,6 @@ const {
3634 isIP
3735} = cares ;
3836
39- function errnoException ( err , syscall , hostname ) {
40- // FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass
41- // the true error to the user. ENOTFOUND is not even a proper POSIX error!
42- if ( err === uv . UV_EAI_MEMORY ||
43- err === uv . UV_EAI_NODATA ||
44- err === uv . UV_EAI_NONAME ) {
45- err = 'ENOTFOUND' ;
46- }
47- var ex = null ;
48- if ( typeof err === 'string' ) { // c-ares error code.
49- const errHost = hostname ? ` ${ hostname } ` : '' ;
50- ex = new Error ( `${ syscall } ${ err } ${ errHost } ` ) ;
51- ex . code = err ;
52- ex . errno = err ;
53- ex . syscall = syscall ;
54- } else {
55- ex = util . _errnoException ( err , syscall ) ;
56- }
57- if ( hostname ) {
58- ex . hostname = hostname ;
59- }
60- return ex ;
61- }
62-
6337const IANA_DNS_PORT = 53 ;
6438const digits = [
6539 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 0-15
@@ -86,10 +60,11 @@ function isIPv4(str) {
8660 return ( str . length > 3 && str . charCodeAt ( 3 ) === 46 /*'.'*/ ) ;
8761}
8862
63+ const dnsException = errors . dnsException ;
8964
9065function onlookup ( err , addresses ) {
9166 if ( err ) {
92- return this . callback ( errnoException ( err , 'getaddrinfo' , this . hostname ) ) ;
67+ return this . callback ( dnsException ( err , 'getaddrinfo' , this . hostname ) ) ;
9368 }
9469 if ( this . family ) {
9570 this . callback ( null , addresses [ 0 ] , this . family ) ;
@@ -101,7 +76,7 @@ function onlookup(err, addresses) {
10176
10277function onlookupall ( err , addresses ) {
10378 if ( err ) {
104- return this . callback ( errnoException ( err , 'getaddrinfo' , this . hostname ) ) ;
79+ return this . callback ( dnsException ( err , 'getaddrinfo' , this . hostname ) ) ;
10580 }
10681
10782 var family = this . family ;
@@ -181,7 +156,7 @@ function lookup(hostname, options, callback) {
181156
182157 var err = cares . getaddrinfo ( req , hostname , family , hints , verbatim ) ;
183158 if ( err ) {
184- process . nextTick ( callback , errnoException ( err , 'getaddrinfo' , hostname ) ) ;
159+ process . nextTick ( callback , dnsException ( err , 'getaddrinfo' , hostname ) ) ;
185160 return { } ;
186161 }
187162 return req ;
@@ -193,7 +168,7 @@ Object.defineProperty(lookup, customPromisifyArgs,
193168
194169function onlookupservice ( err , host , service ) {
195170 if ( err )
196- return this . callback ( errnoException ( err , 'getnameinfo' , this . host ) ) ;
171+ return this . callback ( dnsException ( err , 'getnameinfo' , this . host ) ) ;
197172
198173 this . callback ( null , host , service ) ;
199174}
@@ -222,7 +197,7 @@ function lookupService(host, port, callback) {
222197 req . oncomplete = onlookupservice ;
223198
224199 var err = cares . getnameinfo ( req , host , port ) ;
225- if ( err ) throw errnoException ( err , 'getnameinfo' , host ) ;
200+ if ( err ) throw dnsException ( err , 'getnameinfo' , host ) ;
226201 return req ;
227202}
228203
@@ -235,7 +210,7 @@ function onresolve(err, result, ttls) {
235210 result = result . map ( ( address , index ) => ( { address, ttl : ttls [ index ] } ) ) ;
236211
237212 if ( err )
238- this . callback ( errnoException ( err , this . bindingName , this . hostname ) ) ;
213+ this . callback ( dnsException ( err , this . bindingName , this . hostname ) ) ;
239214 else
240215 this . callback ( null , result ) ;
241216}
@@ -272,7 +247,7 @@ function resolver(bindingName) {
272247 req . oncomplete = onresolve ;
273248 req . ttl = ! ! ( options && options . ttl ) ;
274249 var err = this . _handle [ bindingName ] ( req , name ) ;
275- if ( err ) throw errnoException ( err , bindingName ) ;
250+ if ( err ) throw dnsException ( err , bindingName ) ;
276251 return req ;
277252 }
278253 Object . defineProperty ( query , 'name' , { value : bindingName } ) ;
0 commit comments