deno.land / std@0.166.0 / node / internal / dns / utils.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright Joyent, Inc. and other Node contributors.//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit// persons to whom the Software is furnished to do so, subject to the// following conditions://// The above copyright notice and this permission notice shall be included// in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.
import { getOptionValue } from "../options.ts";import { emitWarning } from "../../process.ts";import { AI_ADDRCONFIG, AI_ALL, AI_V4MAPPED,} from "../../internal_binding/ares.ts";import { ChannelWrap, strerror } from "../../internal_binding/cares_wrap.ts";import { ERR_DNS_SET_SERVERS_FAILED, ERR_INVALID_ARG_VALUE, ERR_INVALID_IP_ADDRESS,} from "../errors.ts";import type { ErrnoException } from "../errors.ts";import { validateArray, validateInt32, validateOneOf, validateString,} from "../validators.mjs";import { isIP } from "../net.ts";
export interface LookupOptions { family?: number | undefined; hints?: number | undefined; all?: boolean | undefined; verbatim?: boolean | undefined;}
export interface LookupOneOptions extends LookupOptions { all?: false | undefined;}
export interface LookupAllOptions extends LookupOptions { all: true;}
export interface LookupAddress { address: string | null; family: number;}
export function isLookupOptions( options: unknown,): options is LookupOptions | undefined { return typeof options === "object" || typeof options === "undefined";}
export function isLookupCallback( options: unknown,): options is (...args: unknown[]) => void { return typeof options === "function";}
export function isFamily(options: unknown): options is number { return typeof options === "number";}
export interface ResolveOptions { ttl?: boolean;}
export interface ResolveWithTtlOptions extends ResolveOptions { ttl: true;}
export interface RecordWithTtl { address: string; ttl: number;}
export interface AnyARecord extends RecordWithTtl { type: "A";}
export interface AnyAaaaRecord extends RecordWithTtl { type: "AAAA";}
export interface CaaRecord { critial: number; issue?: string | undefined; issuewild?: string | undefined; iodef?: string | undefined; contactemail?: string | undefined; contactphone?: string | undefined;}
export interface MxRecord { priority: number; exchange: string;}
export interface AnyMxRecord extends MxRecord { type: "MX";}
export interface NaptrRecord { flags: string; service: string; regexp: string; replacement: string; order: number; preference: number;}
export interface AnyNaptrRecord extends NaptrRecord { type: "NAPTR";}
export interface SoaRecord { nsname: string; hostmaster: string; serial: number; refresh: number; retry: number; expire: number; minttl: number;}
export interface AnySoaRecord extends SoaRecord { type: "SOA";}
export interface SrvRecord { priority: number; weight: number; port: number; name: string;}
export interface AnySrvRecord extends SrvRecord { type: "SRV";}
export interface AnyTxtRecord { type: "TXT"; entries: string[];}
export interface AnyNsRecord { type: "NS"; value: string;}
export interface AnyPtrRecord { type: "PTR"; value: string;}
export interface AnyCnameRecord { type: "CNAME"; value: string;}
export type AnyRecord = | AnyARecord | AnyAaaaRecord | AnyCnameRecord | AnyMxRecord | AnyNaptrRecord | AnyNsRecord | AnyPtrRecord | AnySoaRecord | AnySrvRecord | AnyTxtRecord;
export type Records = | string[] | AnyRecord[] | MxRecord[] | NaptrRecord[] | SoaRecord | SrvRecord[] | string[];
export type ResolveCallback = ( err: ErrnoException | null, addresses: Records,) => void;
export function isResolveCallback( callback: unknown,): callback is ResolveCallback { return typeof callback === "function";}
const IANA_DNS_PORT = 53;const IPv6RE = /^\[([^[\]]*)\]/;const addrSplitRE = /(^.+?)(?::(\d+))?$/;
export function validateTimeout(options?: { timeout?: number }) { const { timeout = -1 } = { ...options }; validateInt32(timeout, "options.timeout", -1, 2 ** 31 - 1); return timeout;}
export function validateTries(options?: { tries?: number }) { const { tries = 4 } = { ...options }; validateInt32(tries, "options.tries", 1, 2 ** 31 - 1); return tries;}
export interface ResolverOptions { timeout?: number | undefined; /** * @default 4 */ tries?: number;}
/** * An independent resolver for DNS requests. * * Creating a new resolver uses the default server settings. Setting * the servers used for a resolver using `resolver.setServers()` does not affect * other resolvers: * * ```js * const { Resolver } = require('dns'); * const resolver = new Resolver(); * resolver.setServers(['4.4.4.4']); * * // This request will use the server at 4.4.4.4, independent of global settings. * resolver.resolve4('example.org', (err, addresses) => { * // ... * }); * ``` * * The following methods from the `dns` module are available: * * - `resolver.getServers()` * - `resolver.resolve()` * - `resolver.resolve4()` * - `resolver.resolve6()` * - `resolver.resolveAny()` * - `resolver.resolveCaa()` * - `resolver.resolveCname()` * - `resolver.resolveMx()` * - `resolver.resolveNaptr()` * - `resolver.resolveNs()` * - `resolver.resolvePtr()` * - `resolver.resolveSoa()` * - `resolver.resolveSrv()` * - `resolver.resolveTxt()` * - `resolver.reverse()` * - `resolver.setServers()` */export class Resolver { _handle!: ChannelWrap;
constructor(options?: ResolverOptions) { const timeout = validateTimeout(options); const tries = validateTries(options); this._handle = new ChannelWrap(timeout, tries); }
cancel() { this._handle.cancel(); }
getServers(): string[] { return this._handle.getServers().map((val: [string, number]) => { if (!val[1] || val[1] === IANA_DNS_PORT) { return val[0]; }
const host = isIP(val[0]) === 6 ? `[${val[0]}]` : val[0]; return `${host}:${val[1]}`; }); }
setServers(servers: ReadonlyArray<string>) { validateArray(servers, "servers");
// Cache the original servers because in the event of an error while // setting the servers, c-ares won't have any servers available for // resolution. const orig = this._handle.getServers(); const newSet: [number, string, number][] = [];
servers.forEach((serv, index) => { validateString(serv, `servers[${index}]`); let ipVersion = isIP(serv);
if (ipVersion !== 0) { return newSet.push([ipVersion, serv, IANA_DNS_PORT]); }
const match = serv.match(IPv6RE);
// Check for an IPv6 in brackets. if (match) { ipVersion = isIP(match[1]);
if (ipVersion !== 0) { const port = Number.parseInt(serv.replace(addrSplitRE, "$2")) || IANA_DNS_PORT;
return newSet.push([ipVersion, match[1], port]); } }
// addr::port const addrSplitMatch = serv.match(addrSplitRE);
if (addrSplitMatch) { const hostIP = addrSplitMatch[1]; const port = addrSplitMatch[2] || `${IANA_DNS_PORT}`;
ipVersion = isIP(hostIP);
if (ipVersion !== 0) { return newSet.push([ipVersion, hostIP, Number.parseInt(port)]); } }
throw new ERR_INVALID_IP_ADDRESS(serv); });
const errorNumber = this._handle.setServers(newSet);
if (errorNumber !== 0) { // Reset the servers to the old servers, because ares probably unset them. this._handle.setServers(orig.join(",")); const err = strerror(errorNumber);
throw new ERR_DNS_SET_SERVERS_FAILED(err, servers.toString()); } }
/** * The resolver instance will send its requests from the specified IP address. * This allows programs to specify outbound interfaces when used on multi-homed * systems. * * If a v4 or v6 address is not specified, it is set to the default, and the * operating system will choose a local address automatically. * * The resolver will use the v4 local address when making requests to IPv4 DNS * servers, and the v6 local address when making requests to IPv6 DNS servers. * The `rrtype` of resolution requests has no impact on the local address used. * * @param [ipv4='0.0.0.0'] A string representation of an IPv4 address. * @param [ipv6='::0'] A string representation of an IPv6 address. */ setLocalAddress(ipv4: string, ipv6?: string) { validateString(ipv4, "ipv4");
if (ipv6 !== undefined) { validateString(ipv6, "ipv6"); }
this._handle.setLocalAddress(ipv4, ipv6); }}
let defaultResolver = new Resolver();
export function getDefaultResolver(): Resolver { return defaultResolver;}
export function setDefaultResolver<T extends Resolver>(resolver: T) { defaultResolver = resolver;}
export function validateHints(hints: number) { if ((hints & ~(AI_ADDRCONFIG | AI_ALL | AI_V4MAPPED)) !== 0) { throw new ERR_INVALID_ARG_VALUE("hints", hints, "is invalid"); }}
let invalidHostnameWarningEmitted = false;
export function emitInvalidHostnameWarning(hostname: string) { if (invalidHostnameWarningEmitted) { return; }
invalidHostnameWarningEmitted = true;
emitWarning( `The provided hostname "${hostname}" is not a valid ` + "hostname, and is supported in the dns module solely for compatibility.", "DeprecationWarning", "DEP0118", );}
let dnsOrder = getOptionValue("--dns-result-order") || "ipv4first";
export function getDefaultVerbatim() { switch (dnsOrder) { case "verbatim": { return true; } case "ipv4first": { return false; } default: { return false; } }}
/** * Set the default value of `verbatim` in `lookup` and `dnsPromises.lookup()`. * The value could be: * * - `ipv4first`: sets default `verbatim` `false`. * - `verbatim`: sets default `verbatim` `true`. * * The default is `ipv4first` and `setDefaultResultOrder` have higher * priority than `--dns-result-order`. When using `worker threads`, * `setDefaultResultOrder` from the main thread won't affect the default * dns orders in workers. * * @param order must be `'ipv4first'` or `'verbatim'`. */export function setDefaultResultOrder(order: "ipv4first" | "verbatim") { validateOneOf(order, "dnsOrder", ["verbatim", "ipv4first"]); dnsOrder = order;}
export function defaultResolverSetServers(servers: string[]) { const resolver = new Resolver();
resolver.setServers(servers); setDefaultResolver(resolver);}
std

Version Info

Tagged at
a year ago