21
21
22
22
'use strict' ;
23
23
24
- const util = require ( 'util' ) ;
25
-
26
24
const cares = process . binding ( 'cares_wrap' ) ;
27
- const uv = process . binding ( 'uv' ) ;
28
25
const { isLegalPort } = require ( 'internal/net' ) ;
29
26
const { customPromisifyArgs } = require ( 'internal/util' ) ;
27
+ const errors = require ( 'internal/errors' ) ;
30
28
31
29
const {
32
30
GetAddrInfoReqWrap,
@@ -36,30 +34,6 @@ const {
36
34
isIP
37
35
} = cares ;
38
36
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
-
63
37
const IANA_DNS_PORT = 53 ;
64
38
const digits = [
65
39
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) {
86
60
return ( str . length > 3 && str . charCodeAt ( 3 ) === 46 /*'.'*/ ) ;
87
61
}
88
62
63
+ const dnsException = errors . dnsException ;
89
64
90
65
function onlookup ( err , addresses ) {
91
66
if ( err ) {
92
- return this . callback ( errnoException ( err , 'getaddrinfo' , this . hostname ) ) ;
67
+ return this . callback ( dnsException ( err , 'getaddrinfo' , this . hostname ) ) ;
93
68
}
94
69
if ( this . family ) {
95
70
this . callback ( null , addresses [ 0 ] , this . family ) ;
@@ -101,7 +76,7 @@ function onlookup(err, addresses) {
101
76
102
77
function onlookupall ( err , addresses ) {
103
78
if ( err ) {
104
- return this . callback ( errnoException ( err , 'getaddrinfo' , this . hostname ) ) ;
79
+ return this . callback ( dnsException ( err , 'getaddrinfo' , this . hostname ) ) ;
105
80
}
106
81
107
82
var family = this . family ;
@@ -181,7 +156,7 @@ function lookup(hostname, options, callback) {
181
156
182
157
var err = cares . getaddrinfo ( req , hostname , family , hints , verbatim ) ;
183
158
if ( err ) {
184
- process . nextTick ( callback , errnoException ( err , 'getaddrinfo' , hostname ) ) ;
159
+ process . nextTick ( callback , dnsException ( err , 'getaddrinfo' , hostname ) ) ;
185
160
return { } ;
186
161
}
187
162
return req ;
@@ -193,7 +168,7 @@ Object.defineProperty(lookup, customPromisifyArgs,
193
168
194
169
function onlookupservice ( err , host , service ) {
195
170
if ( err )
196
- return this . callback ( errnoException ( err , 'getnameinfo' , this . host ) ) ;
171
+ return this . callback ( dnsException ( err , 'getnameinfo' , this . host ) ) ;
197
172
198
173
this . callback ( null , host , service ) ;
199
174
}
@@ -222,7 +197,7 @@ function lookupService(host, port, callback) {
222
197
req . oncomplete = onlookupservice ;
223
198
224
199
var err = cares . getnameinfo ( req , host , port ) ;
225
- if ( err ) throw errnoException ( err , 'getnameinfo' , host ) ;
200
+ if ( err ) throw dnsException ( err , 'getnameinfo' , host ) ;
226
201
return req ;
227
202
}
228
203
@@ -235,7 +210,7 @@ function onresolve(err, result, ttls) {
235
210
result = result . map ( ( address , index ) => ( { address, ttl : ttls [ index ] } ) ) ;
236
211
237
212
if ( err )
238
- this . callback ( errnoException ( err , this . bindingName , this . hostname ) ) ;
213
+ this . callback ( dnsException ( err , this . bindingName , this . hostname ) ) ;
239
214
else
240
215
this . callback ( null , result ) ;
241
216
}
@@ -272,7 +247,7 @@ function resolver(bindingName) {
272
247
req . oncomplete = onresolve ;
273
248
req . ttl = ! ! ( options && options . ttl ) ;
274
249
var err = this . _handle [ bindingName ] ( req , name ) ;
275
- if ( err ) throw errnoException ( err , bindingName ) ;
250
+ if ( err ) throw dnsException ( err , bindingName ) ;
276
251
return req ;
277
252
}
278
253
Object . defineProperty ( query , 'name' , { value : bindingName } ) ;
0 commit comments