11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { 41cb0ef41Sopenharmony_ci ObjectDefineProperty, 51cb0ef41Sopenharmony_ci ReflectApply, 61cb0ef41Sopenharmony_ci ArrayPrototypeMap, 71cb0ef41Sopenharmony_ci Symbol, 81cb0ef41Sopenharmony_ci} = primordials; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst { 111cb0ef41Sopenharmony_ci codes: { 121cb0ef41Sopenharmony_ci ERR_INVALID_ARG_TYPE, 131cb0ef41Sopenharmony_ci ERR_INVALID_ARG_VALUE, 141cb0ef41Sopenharmony_ci }, 151cb0ef41Sopenharmony_ci dnsException, 161cb0ef41Sopenharmony_ci} = require('internal/errors'); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciconst { 191cb0ef41Sopenharmony_ci createResolverClass, 201cb0ef41Sopenharmony_ci} = require('internal/dns/utils'); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst { 231cb0ef41Sopenharmony_ci validateFunction, 241cb0ef41Sopenharmony_ci validateString, 251cb0ef41Sopenharmony_ci} = require('internal/validators'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciconst { 281cb0ef41Sopenharmony_ci QueryReqWrap, 291cb0ef41Sopenharmony_ci} = internalBinding('cares_wrap'); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst { 321cb0ef41Sopenharmony_ci hasObserver, 331cb0ef41Sopenharmony_ci startPerf, 341cb0ef41Sopenharmony_ci stopPerf, 351cb0ef41Sopenharmony_ci} = require('internal/perf/observe'); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciconst kPerfHooksDnsLookupResolveContext = Symbol('kPerfHooksDnsLookupResolveContext'); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cifunction onresolve(err, result, ttls) { 401cb0ef41Sopenharmony_ci if (ttls && this.ttl) 411cb0ef41Sopenharmony_ci result = ArrayPrototypeMap( 421cb0ef41Sopenharmony_ci result, (address, index) => ({ address, ttl: ttls[index] })); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci if (err) 451cb0ef41Sopenharmony_ci this.callback(dnsException(err, this.bindingName, this.hostname)); 461cb0ef41Sopenharmony_ci else { 471cb0ef41Sopenharmony_ci this.callback(null, result); 481cb0ef41Sopenharmony_ci if (this[kPerfHooksDnsLookupResolveContext] && hasObserver('dns')) { 491cb0ef41Sopenharmony_ci stopPerf(this, kPerfHooksDnsLookupResolveContext, { detail: { result } }); 501cb0ef41Sopenharmony_ci } 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_cifunction resolver(bindingName) { 551cb0ef41Sopenharmony_ci function query(name, /* options, */ callback) { 561cb0ef41Sopenharmony_ci let options; 571cb0ef41Sopenharmony_ci if (arguments.length > 2) { 581cb0ef41Sopenharmony_ci options = callback; 591cb0ef41Sopenharmony_ci callback = arguments[2]; 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci validateString(name, 'name'); 631cb0ef41Sopenharmony_ci validateFunction(callback, 'callback'); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci const req = new QueryReqWrap(); 661cb0ef41Sopenharmony_ci req.bindingName = bindingName; 671cb0ef41Sopenharmony_ci req.callback = callback; 681cb0ef41Sopenharmony_ci req.hostname = name; 691cb0ef41Sopenharmony_ci req.oncomplete = onresolve; 701cb0ef41Sopenharmony_ci req.ttl = !!(options && options.ttl); 711cb0ef41Sopenharmony_ci const err = this._handle[bindingName](req, name); 721cb0ef41Sopenharmony_ci if (err) throw dnsException(err, bindingName, name); 731cb0ef41Sopenharmony_ci if (hasObserver('dns')) { 741cb0ef41Sopenharmony_ci startPerf(req, kPerfHooksDnsLookupResolveContext, { 751cb0ef41Sopenharmony_ci type: 'dns', 761cb0ef41Sopenharmony_ci name: bindingName, 771cb0ef41Sopenharmony_ci detail: { 781cb0ef41Sopenharmony_ci host: name, 791cb0ef41Sopenharmony_ci ttl: req.ttl, 801cb0ef41Sopenharmony_ci }, 811cb0ef41Sopenharmony_ci }); 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci return req; 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci ObjectDefineProperty(query, 'name', { __proto__: null, value: bindingName }); 861cb0ef41Sopenharmony_ci return query; 871cb0ef41Sopenharmony_ci} 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci// This is the callback-based resolver. There is another similar 901cb0ef41Sopenharmony_ci// resolver in dns/promises.js with resolve methods that are based 911cb0ef41Sopenharmony_ci// on promises instead. 921cb0ef41Sopenharmony_ciconst { Resolver, resolveMap } = createResolverClass(resolver); 931cb0ef41Sopenharmony_ciResolver.prototype.resolve = resolve; 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_cifunction resolve(hostname, rrtype, callback) { 961cb0ef41Sopenharmony_ci let resolver; 971cb0ef41Sopenharmony_ci if (typeof rrtype === 'string') { 981cb0ef41Sopenharmony_ci resolver = resolveMap[rrtype]; 991cb0ef41Sopenharmony_ci } else if (typeof rrtype === 'function') { 1001cb0ef41Sopenharmony_ci resolver = resolveMap.A; 1011cb0ef41Sopenharmony_ci callback = rrtype; 1021cb0ef41Sopenharmony_ci } else { 1031cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE('rrtype', 'string', rrtype); 1041cb0ef41Sopenharmony_ci } 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci if (typeof resolver === 'function') { 1071cb0ef41Sopenharmony_ci return ReflectApply(resolver, this, [hostname, callback]); 1081cb0ef41Sopenharmony_ci } 1091cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE('rrtype', rrtype); 1101cb0ef41Sopenharmony_ci} 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_cimodule.exports = { 1131cb0ef41Sopenharmony_ci Resolver, 1141cb0ef41Sopenharmony_ci}; 115