Skip to content

Commit

Permalink
dns: call ada::idna::to_ascii directly from c++
Browse files Browse the repository at this point in the history
PR-URL: #47920
Fixes: nodejs/performance#77
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
anonrig authored and targos committed May 12, 2023
1 parent d81c54e commit 9be9228
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
3 changes: 1 addition & 2 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
} = primordials;

const cares = internalBinding('cares_wrap');
const { toASCII } = require('internal/idna');
const { isIP } = require('internal/net');
const { customPromisifyArgs } = require('internal/util');
const errors = require('internal/errors');
Expand Down Expand Up @@ -220,7 +219,7 @@ function lookup(hostname, options, callback) {
req.oncomplete = all ? onlookupall : onlookup;

const err = cares.getaddrinfo(
req, toASCII(hostname), family, hints, verbatim,
req, hostname, family, hints, verbatim,
);
if (err) {
process.nextTick(callback, dnsException(err, 'getaddrinfo', hostname));
Expand Down
4 changes: 1 addition & 3 deletions lib/internal/dns/callback_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const {
Symbol,
} = primordials;

const { toASCII } = require('internal/idna');

const {
codes: {
ERR_INVALID_ARG_TYPE,
Expand Down Expand Up @@ -70,7 +68,7 @@ function resolver(bindingName) {
req.hostname = name;
req.oncomplete = onresolve;
req.ttl = !!(options && options.ttl);
const err = this._handle[bindingName](req, toASCII(name));
const err = this._handle[bindingName](req, name);
if (err) throw dnsException(err, bindingName, name);
if (hasObserver('dns')) {
startPerf(req, kPerfHooksDnsLookupResolveContext, {
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/dns/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const {
CANCELLED,
} = dnsErrorCodes;
const { codes, dnsException } = require('internal/errors');
const { toASCII } = require('internal/idna');
const { isIP } = require('internal/net');
const {
getaddrinfo,
Expand Down Expand Up @@ -138,7 +137,7 @@ function createLookupPromise(family, hostname, all, hints, verbatim) {
req.resolve = resolve;
req.reject = reject;

const err = getaddrinfo(req, toASCII(hostname), family, hints, verbatim);
const err = getaddrinfo(req, hostname, family, hints, verbatim);

if (err) {
reject(dnsException(err, 'getaddrinfo', hostname));
Expand Down Expand Up @@ -274,7 +273,7 @@ function createResolverPromise(resolver, bindingName, hostname, ttl) {
req.reject = reject;
req.ttl = ttl;

const err = resolver._handle[bindingName](req, toASCII(hostname));
const err = resolver._handle[bindingName](req, hostname);

if (err)
reject(dnsException(err, bindingName, hostname));
Expand Down
25 changes: 14 additions & 11 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "cares_wrap.h"
#include "ada.h"
#include "async_wrap-inl.h"
#include "base64-inl.h"
#include "base_object-inl.h"
Expand Down Expand Up @@ -1558,6 +1559,7 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
CHECK(args[4]->IsBoolean());
Local<Object> req_wrap_obj = args[0].As<Object>();
node::Utf8Value hostname(env->isolate(), args[1]);
std::string ascii_hostname = ada::idna::to_ascii(hostname.ToStringView());

int32_t flags = 0;
if (args[3]->IsInt32()) {
Expand Down Expand Up @@ -1590,17 +1592,18 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = flags;

TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(
TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap.get(),
"hostname", TRACE_STR_COPY(*hostname),
"family",
family == AF_INET ? "ipv4" : family == AF_INET6 ? "ipv6" : "unspec");

int err = req_wrap->Dispatch(uv_getaddrinfo,
AfterGetAddrInfo,
*hostname,
nullptr,
&hints);
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(dns, native),
"lookup",
req_wrap.get(),
"hostname",
TRACE_STR_COPY(ascii_hostname.data()),
"family",
family == AF_INET ? "ipv4"
: family == AF_INET6 ? "ipv6"
: "unspec");

int err = req_wrap->Dispatch(
uv_getaddrinfo, AfterGetAddrInfo, ascii_hostname.data(), nullptr, &hints);
if (err == 0)
// Release ownership of the pointer allowing the ownership to be transferred
USE(req_wrap.release());
Expand Down

0 comments on commit 9be9228

Please sign in to comment.