From 85af15a8b697dc7da7496d77869fedfa73039f43 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 11 Aug 2020 15:45:26 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/34741 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott --- lib/internal/blocklist.js | 9 +++++++-- src/env.h | 2 ++ src/node_sockaddr.cc | 14 ++++++++++++++ src/node_sockaddr.h | 1 + tools/doc/type-parser.mjs | 1 + 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/internal/blocklist.js b/lib/internal/blocklist.js index d4074ab41c2150..0ad58373b746b2 100644 --- a/lib/internal/blocklist.js +++ b/lib/internal/blocklist.js @@ -26,8 +26,13 @@ const { } = require('internal/errors').codes; class BlockList { - constructor() { - this[kHandle] = new BlockListHandle(); + constructor(handle = new BlockListHandle()) { + // The handle argument is an intentionally undocumented + // internal API. User code will not be able to create + // a BlockListHandle object directly. + if (!(handle instanceof BlockListHandle)) + throw new ERR_INVALID_ARG_TYPE('handle', 'BlockListHandle', handle); + this[kHandle] = handle; this[kHandle][owner_symbol] = this; } diff --git a/src/env.h b/src/env.h index 813d785c397864..ebc0002bdafd9d 100644 --- a/src/env.h +++ b/src/env.h @@ -179,6 +179,7 @@ constexpr size_t kFsStatsBufferLength = V(asn1curve_string, "asn1Curve") \ V(async_ids_stack_string, "async_ids_stack") \ V(bits_string, "bits") \ + V(block_list_string, "blockList") \ V(buffer_string, "buffer") \ V(bytes_parsed_string, "bytesParsed") \ V(bytes_read_string, "bytesRead") \ @@ -410,6 +411,7 @@ constexpr size_t kFsStatsBufferLength = V(async_wrap_object_ctor_template, v8::FunctionTemplate) \ V(base_object_ctor_template, v8::FunctionTemplate) \ V(binding_data_ctor_template, v8::FunctionTemplate) \ + V(blocklist_instance_template, v8::ObjectTemplate) \ V(compiled_fn_entry_template, v8::ObjectTemplate) \ V(dir_instance_template, v8::ObjectTemplate) \ V(fd_constructor_template, v8::ObjectTemplate) \ diff --git a/src/node_sockaddr.cc b/src/node_sockaddr.cc index 8ba82ff68535a6..58d03eefa6e6e6 100644 --- a/src/node_sockaddr.cc +++ b/src/node_sockaddr.cc @@ -524,6 +524,19 @@ SocketAddressBlockListWrap::SocketAddressBlockListWrap( MakeWeak(); } +BaseObjectPtr SocketAddressBlockListWrap::New( + Environment* env) { + Local obj; + if (!env->blocklist_instance_template() + ->NewInstance(env->context()).ToLocal(&obj)) { + return {}; + } + BaseObjectPtr wrap = + MakeDetachedBaseObject(env, obj); + CHECK(wrap); + return wrap; +} + void SocketAddressBlockListWrap::New( const FunctionCallbackInfo& args) { CHECK(args.IsConstructCall()); @@ -673,6 +686,7 @@ void SocketAddressBlockListWrap::Initialize( env->SetProtoMethod(t, "check", SocketAddressBlockListWrap::Check); env->SetProtoMethod(t, "getRules", SocketAddressBlockListWrap::GetRules); + env->set_blocklist_instance_template(t->InstanceTemplate()); target->Set(env->context(), name, t->GetFunction(env->context()).ToLocalChecked()).FromJust(); diff --git a/src/node_sockaddr.h b/src/node_sockaddr.h index f539cf6555f798..69a370afa32e81 100644 --- a/src/node_sockaddr.h +++ b/src/node_sockaddr.h @@ -280,6 +280,7 @@ class SocketAddressBlockListWrap : v8::Local context, void* priv); + static BaseObjectPtr New(Environment* env); static void New(const v8::FunctionCallbackInfo& args); static void AddAddress(const v8::FunctionCallbackInfo& args); static void AddRange(const v8::FunctionCallbackInfo& args); diff --git a/tools/doc/type-parser.mjs b/tools/doc/type-parser.mjs index 84d9a8a7df4fad..7f6ccc8ae423cf 100644 --- a/tools/doc/type-parser.mjs +++ b/tools/doc/type-parser.mjs @@ -122,6 +122,7 @@ const customTypesMap = { 'require': 'modules.html#modules_require_id', 'Handle': 'net.html#net_server_listen_handle_backlog_callback', + 'net.BlockList': 'net.html#net_class_net_blocklist', 'net.Server': 'net.html#net_class_net_server', 'net.Socket': 'net.html#net_class_net_socket',