Skip to content

Commit e426180

Browse files
anonriglemire
authored andcommitted
src: replace idna functions with ada::idna
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>
1 parent f4617a4 commit e426180

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/internal/idna.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'use strict';
22

3-
const { domainToASCII, domainToUnicode } = require('internal/url');
4-
module.exports = { toASCII: domainToASCII, toUnicode: domainToUnicode };
3+
const { toASCII, toUnicode } = internalBinding('url');
4+
module.exports = { toASCII, toUnicode };

src/node_url.cc

+26
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,28 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
294294
.ToLocalChecked());
295295
}
296296

297+
void BindingData::ToASCII(const v8::FunctionCallbackInfo<v8::Value>& args) {
298+
Environment* env = Environment::GetCurrent(args);
299+
CHECK_GE(args.Length(), 1);
300+
CHECK(args[0]->IsString());
301+
302+
Utf8Value input(env->isolate(), args[0]);
303+
auto out = ada::idna::to_ascii(input.ToStringView());
304+
args.GetReturnValue().Set(
305+
String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
306+
}
307+
308+
void BindingData::ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args) {
309+
Environment* env = Environment::GetCurrent(args);
310+
CHECK_GE(args.Length(), 1);
311+
CHECK(args[0]->IsString());
312+
313+
Utf8Value input(env->isolate(), args[0]);
314+
auto out = ada::idna::to_unicode(input.ToStringView());
315+
args.GetReturnValue().Set(
316+
String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
317+
}
318+
297319
void BindingData::UpdateComponents(const ada::url_components& components,
298320
const ada::scheme::type type) {
299321
url_components_buffer_[0] = components.protocol_end;
@@ -318,6 +340,8 @@ void BindingData::Initialize(Local<Object> target,
318340
realm->AddBindingData<BindingData>(context, target);
319341
if (binding_data == nullptr) return;
320342

343+
SetMethodNoSideEffect(context, target, "toASCII", ToASCII);
344+
SetMethodNoSideEffect(context, target, "toUnicode", ToUnicode);
321345
SetMethodNoSideEffect(context, target, "domainToASCII", DomainToASCII);
322346
SetMethodNoSideEffect(context, target, "domainToUnicode", DomainToUnicode);
323347
SetMethodNoSideEffect(context, target, "canParse", CanParse);
@@ -328,6 +352,8 @@ void BindingData::Initialize(Local<Object> target,
328352

329353
void BindingData::RegisterExternalReferences(
330354
ExternalReferenceRegistry* registry) {
355+
registry->Register(ToASCII);
356+
registry->Register(ToUnicode);
331357
registry->Register(DomainToASCII);
332358
registry->Register(DomainToUnicode);
333359
registry->Register(CanParse);

src/node_url.h

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class BindingData : public SnapshotableObject {
4545
SET_SELF_SIZE(BindingData)
4646
SET_MEMORY_INFO_NAME(BindingData)
4747

48+
static void ToASCII(const v8::FunctionCallbackInfo<v8::Value>& args);
49+
static void ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);
50+
4851
static void DomainToASCII(const v8::FunctionCallbackInfo<v8::Value>& args);
4952
static void DomainToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);
5053

0 commit comments

Comments
 (0)