Skip to content

Commit

Permalink
src: replace idna functions with ada::idna
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Lemire <daniel@lemire.me>
PR-URL: #47735
Backport-PR-URL: #48873
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
2 people authored and ruyadorno committed Aug 16, 2023
1 parent f4617a4 commit e426180
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/internal/idna.js
Original file line number Diff line number Diff line change
@@ -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 };
26 changes: 26 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,28 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
.ToLocalChecked());
}

void BindingData::ToASCII(const v8::FunctionCallbackInfo<v8::Value>& 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<v8::Value>& 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;
Expand All @@ -318,6 +340,8 @@ void BindingData::Initialize(Local<Object> target,
realm->AddBindingData<BindingData>(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);
Expand All @@ -328,6 +352,8 @@ void BindingData::Initialize(Local<Object> target,

void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(ToASCII);
registry->Register(ToUnicode);
registry->Register(DomainToASCII);
registry->Register(DomainToUnicode);
registry->Register(CanParse);
Expand Down
3 changes: 3 additions & 0 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class BindingData : public SnapshotableObject {
SET_SELF_SIZE(BindingData)
SET_MEMORY_INFO_NAME(BindingData)

static void ToASCII(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);

static void DomainToASCII(const v8::FunctionCallbackInfo<v8::Value>& args);
static void DomainToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);

Expand Down

0 comments on commit e426180

Please sign in to comment.