From f857ff3a56bd445a2d074c9c37af54257e19c1c8 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 Reviewed-By: James M Snell Reviewed-By: Minwoo Jung --- lib/internal/idna.js | 4 ++-- src/encoding_binding.cc | 27 +++++++++++++++++++++++++++ src/encoding_binding.h | 3 +++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/internal/idna.js b/lib/internal/idna.js index 566f8590d8755c..ad37bb48efc605 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('encoding_binding'); +module.exports = { toASCII, toUnicode }; diff --git a/src/encoding_binding.cc b/src/encoding_binding.cc index 25f4abf6ae47ca..d665b38cfc5f1a 100644 --- a/src/encoding_binding.cc +++ b/src/encoding_binding.cc @@ -1,4 +1,5 @@ #include "encoding_binding.h" +#include "ada.h" #include "env-inl.h" #include "node_errors.h" #include "node_external_reference.h" @@ -193,6 +194,28 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(ret); } +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::Initialize(Local target, Local unused, Local context, @@ -205,6 +228,8 @@ void BindingData::Initialize(Local target, SetMethod(context, target, "encodeInto", EncodeInto); SetMethodNoSideEffect(context, target, "encodeUtf8String", EncodeUtf8String); SetMethodNoSideEffect(context, target, "decodeUTF8", DecodeUTF8); + SetMethodNoSideEffect(context, target, "toASCII", ToASCII); + SetMethodNoSideEffect(context, target, "toUnicode", ToUnicode); } void BindingData::RegisterTimerExternalReferences( @@ -212,6 +237,8 @@ void BindingData::RegisterTimerExternalReferences( registry->Register(EncodeInto); registry->Register(EncodeUtf8String); registry->Register(DecodeUTF8); + registry->Register(ToASCII); + registry->Register(ToUnicode); } } // namespace encoding_binding diff --git a/src/encoding_binding.h b/src/encoding_binding.h index 0258b8188b106d..8a009dfce27731 100644 --- a/src/encoding_binding.h +++ b/src/encoding_binding.h @@ -32,6 +32,9 @@ class BindingData : public SnapshotableObject { static void EncodeUtf8String(const v8::FunctionCallbackInfo& args); static void DecodeUTF8(const v8::FunctionCallbackInfo& args); + static void ToASCII(const v8::FunctionCallbackInfo& args); + static void ToUnicode(const v8::FunctionCallbackInfo& args); + static void Initialize(v8::Local target, v8::Local unused, v8::Local context,