/
githubmirror
/
node
Обзор
Документация
Войти
/
githubmirror
/
node
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
lib/internal/dns/callback_resolver.js
113 строк
3 KB
Antoine du Hamel
lib: use `assignFunctionName` util where it makes sense
17 июл 2026, 14:00
Не верифицирован
17 июл 2026, 14:00
5520388
Код
Авторство
О чём код?
'use strict'; const { ArrayPrototypeMap, FunctionPrototypeCall, Symbol, } = primordials; const { DNSException, codes: { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, }, } = require('internal/errors'); const { createResolverClass, } = require('internal/dns/utils'); const { validateFunction, validateString, } = require('internal/validators'); const { QueryReqWrap, } = internalBinding('cares_wrap'); const { hasObserver, startPerf, stopPerf, } = require('internal/perf/observe'); const { assignFunctionName } = require('internal/util'); const kPerfHooksDnsLookupResolveContext = Symbol('kPerfHooksDnsLookupResolveContext'); function onresolve(err, result, ttls) { if (ttls && this.ttl) result = ArrayPrototypeMap( result, (address, index) => ({ address, ttl: ttls[index] })); if (err) this.callback(new DNSException(err, this.bindingName, this.hostname)); else { this.callback(null, result); if (this[kPerfHooksDnsLookupResolveContext] && hasObserver('dns')) { stopPerf(this, kPerfHooksDnsLookupResolveContext, { detail: { result } }); } } } function resolver(bindingName) { function query(name, /* options, */ callback) { let options; if (arguments.length > 2) { options = callback; callback = arguments[2]; } validateString(name, 'name'); validateFunction(callback, 'callback'); const req = new QueryReqWrap(); req.bindingName = bindingName; req.callback = callback; req.hostname = name; req.oncomplete = onresolve; req.ttl = !!(options?.ttl); const err = this._handle[bindingName](req, name); if (err) throw new DNSException(err, bindingName, name); if (hasObserver('dns')) { startPerf(req, kPerfHooksDnsLookupResolveContext, { type: 'dns', name: bindingName, detail: { host: name, ttl: req.ttl, }, }); } return req; } return assignFunctionName(bindingName, query); } // This is the callback-based resolver. There is another similar // resolver in dns/promises.js with resolve methods that are based // on promises instead. const { Resolver, resolveMap } = createResolverClass(resolver); Resolver.prototype.resolve = resolve; function resolve(hostname, rrtype, callback) { let resolver; if (typeof rrtype === 'string') { resolver = resolveMap[rrtype]; } else if (typeof rrtype === 'function') { resolver = resolveMap.A; callback = rrtype; } else { throw new ERR_INVALID_ARG_TYPE('rrtype', 'string', rrtype); } if (typeof resolver === 'function') { return FunctionPrototypeCall(resolver, this, hostname, callback); } throw new ERR_INVALID_ARG_VALUE('rrtype', rrtype); } module.exports = { Resolver, };