From e4261809b0b5336dab2fe42093a9a0071641aada Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 28 Apr 2023 14:48:38 -0400 Subject: [PATCH] src: replace idna functions with ada::idna Co-authored-by: Daniel Lemire PR-URL: https://github.com/nodejs/node/pull/47735 Backport-PR-URL: https://github.com/nodejs/node/pull/48873 Reviewed-By: James M Snell Reviewed-By: Minwoo Jung --- lib/internal/idna.js | 4 ++-- src/node_url.cc | 26 ++++++++++++++++++++++++++ src/node_url.h | 3 +++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/internal/idna.js b/lib/internal/idna.js index 566f8590d8755c..feb7031ecc5896 100644 --- a/lib/internal/idna.js +++ b/lib/internal/idna.js @@ -1,4 +1,4 @@ 'use strict'; -const { domainToASCII, domainToUnicode } = require('internal/url'); -module.exports = { toASCII: domainToASCII, toUnicode: domainToUnicode }; +const { toASCII, toUnicode } = internalBinding('url'); +module.exports = { toASCII, toUnicode }; diff --git a/src/node_url.cc b/src/node_url.cc index 9ee0b43b4a4d05..30a1a6ec9876ba 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -294,6 +294,28 @@ void BindingData::Update(const FunctionCallbackInfo& args) { .ToLocalChecked()); } +void BindingData::ToASCII(const v8::FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + CHECK_GE(args.Length(), 1); + CHECK(args[0]->IsString()); + + Utf8Value input(env->isolate(), args[0]); + auto out = ada::idna::to_ascii(input.ToStringView()); + args.GetReturnValue().Set( + String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked()); +} + +void BindingData::ToUnicode(const v8::FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + CHECK_GE(args.Length(), 1); + CHECK(args[0]->IsString()); + + Utf8Value input(env->isolate(), args[0]); + auto out = ada::idna::to_unicode(input.ToStringView()); + args.GetReturnValue().Set( + String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked()); +} + void BindingData::UpdateComponents(const ada::url_components& components, const ada::scheme::type type) { url_components_buffer_[0] = components.protocol_end; @@ -318,6 +340,8 @@ void BindingData::Initialize(Local target, realm->AddBindingData(context, target); if (binding_data == nullptr) return; + SetMethodNoSideEffect(context, target, "toASCII", ToASCII); + SetMethodNoSideEffect(context, target, "toUnicode", ToUnicode); SetMethodNoSideEffect(context, target, "domainToASCII", DomainToASCII); SetMethodNoSideEffect(context, target, "domainToUnicode", DomainToUnicode); SetMethodNoSideEffect(context, target, "canParse", CanParse); @@ -328,6 +352,8 @@ void BindingData::Initialize(Local target, void BindingData::RegisterExternalReferences( ExternalReferenceRegistry* registry) { + registry->Register(ToASCII); + registry->Register(ToUnicode); registry->Register(DomainToASCII); registry->Register(DomainToUnicode); registry->Register(CanParse); diff --git a/src/node_url.h b/src/node_url.h index 4630c426c29d67..dd543a97c2e9b4 100644 --- a/src/node_url.h +++ b/src/node_url.h @@ -45,6 +45,9 @@ class BindingData : public SnapshotableObject { SET_SELF_SIZE(BindingData) SET_MEMORY_INFO_NAME(BindingData) + static void ToASCII(const v8::FunctionCallbackInfo& args); + static void ToUnicode(const v8::FunctionCallbackInfo& args); + static void DomainToASCII(const v8::FunctionCallbackInfo& args); static void DomainToUnicode(const v8::FunctionCallbackInfo& args);