Skip to content

Commit dc43ce7

Browse files
anonriglemire
authored andcommitted
src: replace idna functions with ada::idna
Co-authored-by: Daniel Lemire <daniel@lemire.me> PR-URL: #47735 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 185d609 commit dc43ce7

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-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('encoding_binding');
4+
module.exports = { toASCII, toUnicode };

src/encoding_binding.cc

+27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "encoding_binding.h"
2+
#include "ada.h"
23
#include "env-inl.h"
34
#include "node_errors.h"
45
#include "node_external_reference.h"
@@ -193,6 +194,28 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
193194
args.GetReturnValue().Set(ret);
194195
}
195196

197+
void BindingData::ToASCII(const v8::FunctionCallbackInfo<v8::Value>& args) {
198+
Environment* env = Environment::GetCurrent(args);
199+
CHECK_GE(args.Length(), 1);
200+
CHECK(args[0]->IsString());
201+
202+
Utf8Value input(env->isolate(), args[0]);
203+
auto out = ada::idna::to_ascii(input.ToStringView());
204+
args.GetReturnValue().Set(
205+
String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
206+
}
207+
208+
void BindingData::ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args) {
209+
Environment* env = Environment::GetCurrent(args);
210+
CHECK_GE(args.Length(), 1);
211+
CHECK(args[0]->IsString());
212+
213+
Utf8Value input(env->isolate(), args[0]);
214+
auto out = ada::idna::to_unicode(input.ToStringView());
215+
args.GetReturnValue().Set(
216+
String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
217+
}
218+
196219
void BindingData::Initialize(Local<Object> target,
197220
Local<Value> unused,
198221
Local<Context> context,
@@ -205,13 +228,17 @@ void BindingData::Initialize(Local<Object> target,
205228
SetMethod(context, target, "encodeInto", EncodeInto);
206229
SetMethodNoSideEffect(context, target, "encodeUtf8String", EncodeUtf8String);
207230
SetMethodNoSideEffect(context, target, "decodeUTF8", DecodeUTF8);
231+
SetMethodNoSideEffect(context, target, "toASCII", ToASCII);
232+
SetMethodNoSideEffect(context, target, "toUnicode", ToUnicode);
208233
}
209234

210235
void BindingData::RegisterTimerExternalReferences(
211236
ExternalReferenceRegistry* registry) {
212237
registry->Register(EncodeInto);
213238
registry->Register(EncodeUtf8String);
214239
registry->Register(DecodeUTF8);
240+
registry->Register(ToASCII);
241+
registry->Register(ToUnicode);
215242
}
216243

217244
} // namespace encoding_binding

src/encoding_binding.h

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class BindingData : public SnapshotableObject {
3232
static void EncodeUtf8String(const v8::FunctionCallbackInfo<v8::Value>& args);
3333
static void DecodeUTF8(const v8::FunctionCallbackInfo<v8::Value>& args);
3434

35+
static void ToASCII(const v8::FunctionCallbackInfo<v8::Value>& args);
36+
static void ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);
37+
3538
static void Initialize(v8::Local<v8::Object> target,
3639
v8::Local<v8::Value> unused,
3740
v8::Local<v8::Context> context,

0 commit comments

Comments
 (0)