Skip to content

Commit 1c14810

Browse files
committed
src: allow instances of net.BlockList to be created internally
Initial PR had it so that user code would create BlockList instances. This sets it up so that instances can be created internally by Node.js PR-URL: #34741 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent bfc3535 commit 1c14810

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

lib/internal/blocklist.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ const {
2626
} = require('internal/errors').codes;
2727

2828
class BlockList {
29-
constructor() {
30-
this[kHandle] = new BlockListHandle();
29+
constructor(handle = new BlockListHandle()) {
30+
// The handle argument is an intentionally undocumented
31+
// internal API. User code will not be able to create
32+
// a BlockListHandle object directly.
33+
if (!(handle instanceof BlockListHandle))
34+
throw new ERR_INVALID_ARG_TYPE('handle', 'BlockListHandle', handle);
35+
this[kHandle] = handle;
3136
this[kHandle][owner_symbol] = this;
3237
}
3338

src/env.h

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ constexpr size_t kFsStatsBufferLength =
182182
V(asn1curve_string, "asn1Curve") \
183183
V(async_ids_stack_string, "async_ids_stack") \
184184
V(bits_string, "bits") \
185+
V(block_list_string, "blockList") \
185186
V(buffer_string, "buffer") \
186187
V(bytes_parsed_string, "bytesParsed") \
187188
V(bytes_read_string, "bytesRead") \
@@ -423,6 +424,7 @@ constexpr size_t kFsStatsBufferLength =
423424
V(async_wrap_object_ctor_template, v8::FunctionTemplate) \
424425
V(base_object_ctor_template, v8::FunctionTemplate) \
425426
V(binding_data_ctor_template, v8::FunctionTemplate) \
427+
V(blocklist_instance_template, v8::ObjectTemplate) \
426428
V(compiled_fn_entry_template, v8::ObjectTemplate) \
427429
V(dir_instance_template, v8::ObjectTemplate) \
428430
V(fd_constructor_template, v8::ObjectTemplate) \

src/node_sockaddr.cc

+14
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,19 @@ SocketAddressBlockListWrap::SocketAddressBlockListWrap(
524524
MakeWeak();
525525
}
526526

527+
BaseObjectPtr<SocketAddressBlockListWrap> SocketAddressBlockListWrap::New(
528+
Environment* env) {
529+
Local<Object> obj;
530+
if (!env->blocklist_instance_template()
531+
->NewInstance(env->context()).ToLocal(&obj)) {
532+
return {};
533+
}
534+
BaseObjectPtr<SocketAddressBlockListWrap> wrap =
535+
MakeDetachedBaseObject<SocketAddressBlockListWrap>(env, obj);
536+
CHECK(wrap);
537+
return wrap;
538+
}
539+
527540
void SocketAddressBlockListWrap::New(
528541
const FunctionCallbackInfo<Value>& args) {
529542
CHECK(args.IsConstructCall());
@@ -673,6 +686,7 @@ void SocketAddressBlockListWrap::Initialize(
673686
env->SetProtoMethod(t, "check", SocketAddressBlockListWrap::Check);
674687
env->SetProtoMethod(t, "getRules", SocketAddressBlockListWrap::GetRules);
675688

689+
env->set_blocklist_instance_template(t->InstanceTemplate());
676690
target->Set(env->context(), name,
677691
t->GetFunction(env->context()).ToLocalChecked()).FromJust();
678692

src/node_sockaddr.h

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ class SocketAddressBlockListWrap :
280280
v8::Local<v8::Context> context,
281281
void* priv);
282282

283+
static BaseObjectPtr<SocketAddressBlockListWrap> New(Environment* env);
283284
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
284285
static void AddAddress(const v8::FunctionCallbackInfo<v8::Value>& args);
285286
static void AddRange(const v8::FunctionCallbackInfo<v8::Value>& args);

tools/doc/type-parser.js

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const customTypesMap = {
122122
'require': 'modules.html#modules_require_id',
123123

124124
'Handle': 'net.html#net_server_listen_handle_backlog_callback',
125+
'net.BlockList': 'net.html#net_class_net_blocklist',
125126
'net.Server': 'net.html#net_class_net_server',
126127
'net.Socket': 'net.html#net_class_net_socket',
127128

0 commit comments

Comments
 (0)