Skip to content

Commit a4169ce

Browse files
jasnellMylesBorins
authored andcommitted
net: make net.BlockList cloneable
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #37917 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent ae70aa3 commit a4169ce

9 files changed

+247
-55
lines changed

doc/api/worker_threads.md

+4
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ are part of the channel.
527527
<!-- YAML
528528
added: v10.5.0
529529
changes:
530+
- version: REPLACEME
531+
pr-url: https://github.com/nodejs/node/pull/37917
532+
description: Add 'BlockList' to the list of cloneable types.
530533
- version: v15.9.0
531534
pr-url: https://github.com/nodejs/node/pull/37155
532535
description: Add 'Histogram' types to the list of cloneable types.
@@ -569,6 +572,7 @@ In particular, the significant differences to `JSON` are:
569572
* {Histogram}s,
570573
* {KeyObject}s,
571574
* {MessagePort}s,
575+
* {net.BlockList}s,
572576
* {X509Certificate}s.
573577

574578
```js

lib/internal/blocklist.js

+41-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
Boolean,
5+
ObjectSetPrototypeOf,
56
Symbol
67
} = primordials;
78

@@ -14,26 +15,28 @@ const {
1415
const {
1516
customInspectSymbol: kInspect,
1617
} = require('internal/util');
18+
19+
const {
20+
JSTransferable,
21+
kClone,
22+
kDeserialize,
23+
} = require('internal/worker/js_transferable');
24+
1725
const { inspect } = require('internal/util/inspect');
1826

1927
const kHandle = Symbol('kHandle');
2028
const { owner_symbol } = internalBinding('symbols');
2129

2230
const {
23-
ERR_INVALID_ARG_TYPE,
2431
ERR_INVALID_ARG_VALUE,
2532
} = require('internal/errors').codes;
2633

2734
const { validateInt32, validateString } = require('internal/validators');
2835

29-
class BlockList {
30-
constructor(handle = new BlockListHandle()) {
31-
// The handle argument is an intentionally undocumented
32-
// internal API. User code will not be able to create
33-
// a BlockListHandle object directly.
34-
if (!(handle instanceof BlockListHandle))
35-
throw new ERR_INVALID_ARG_TYPE('handle', 'BlockListHandle', handle);
36-
this[kHandle] = handle;
36+
class BlockList extends JSTransferable {
37+
constructor() {
38+
super();
39+
this[kHandle] = new BlockListHandle();
3740
this[kHandle][owner_symbol] = this;
3841
}
3942

@@ -107,6 +110,34 @@ class BlockList {
107110
get rules() {
108111
return this[kHandle].getRules();
109112
}
113+
114+
[kClone]() {
115+
const handle = this[kHandle];
116+
return {
117+
data: { handle },
118+
deserializeInfo: 'internal/blocklist:InternalBlockList',
119+
};
120+
}
121+
122+
[kDeserialize]({ handle }) {
123+
this[kHandle] = handle;
124+
this[kHandle][owner_symbol] = this;
125+
}
126+
}
127+
128+
class InternalBlockList extends JSTransferable {
129+
constructor(handle) {
130+
super();
131+
this[kHandle] = handle;
132+
if (handle !== undefined)
133+
handle[owner_symbol] = this;
134+
}
110135
}
111136

112-
module.exports = BlockList;
137+
InternalBlockList.prototype.constructor = BlockList.prototype.constructor;
138+
ObjectSetPrototypeOf(InternalBlockList.prototype, BlockList.prototype);
139+
140+
module.exports = {
141+
BlockList,
142+
InternalBlockList,
143+
};

lib/net.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1748,8 +1748,7 @@ module.exports = {
17481748
_normalizeArgs: normalizeArgs,
17491749
_setSimultaneousAccepts,
17501750
get BlockList() {
1751-
if (BlockList === undefined)
1752-
BlockList = require('internal/blocklist');
1751+
BlockList ??= require('internal/blocklist').BlockList;
17531752
return BlockList;
17541753
},
17551754
connect,

src/env-inl.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -1035,15 +1035,18 @@ inline void Environment::SetInstanceMethod(v8::Local<v8::FunctionTemplate> that,
10351035
inline void Environment::SetConstructorFunction(
10361036
v8::Local<v8::Object> that,
10371037
const char* name,
1038-
v8::Local<v8::FunctionTemplate> tmpl) {
1039-
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl);
1038+
v8::Local<v8::FunctionTemplate> tmpl,
1039+
SetConstructorFunctionFlag flag) {
1040+
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl, flag);
10401041
}
10411042

10421043
inline void Environment::SetConstructorFunction(
10431044
v8::Local<v8::Object> that,
10441045
v8::Local<v8::String> name,
1045-
v8::Local<v8::FunctionTemplate> tmpl) {
1046-
tmpl->SetClassName(name);
1046+
v8::Local<v8::FunctionTemplate> tmpl,
1047+
SetConstructorFunctionFlag flag) {
1048+
if (LIKELY(flag == SetConstructorFunctionFlag::SET_CLASS_NAME))
1049+
tmpl->SetClassName(name);
10471050
that->Set(
10481051
context(),
10491052
name,

src/env.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ constexpr size_t kFsStatsBufferLength =
442442
V(base_object_ctor_template, v8::FunctionTemplate) \
443443
V(binding_data_ctor_template, v8::FunctionTemplate) \
444444
V(blob_constructor_template, v8::FunctionTemplate) \
445-
V(blocklist_instance_template, v8::ObjectTemplate) \
445+
V(blocklist_constructor_template, v8::FunctionTemplate) \
446446
V(compiled_fn_entry_template, v8::ObjectTemplate) \
447447
V(dir_instance_template, v8::ObjectTemplate) \
448448
V(fd_constructor_template, v8::ObjectTemplate) \
@@ -1231,13 +1231,22 @@ class Environment : public MemoryRetainer {
12311231
const char* name,
12321232
v8::FunctionCallback callback);
12331233

1234+
enum class SetConstructorFunctionFlag {
1235+
NONE,
1236+
SET_CLASS_NAME,
1237+
};
1238+
12341239
inline void SetConstructorFunction(v8::Local<v8::Object> that,
12351240
const char* name,
1236-
v8::Local<v8::FunctionTemplate> tmpl);
1241+
v8::Local<v8::FunctionTemplate> tmpl,
1242+
SetConstructorFunctionFlag flag =
1243+
SetConstructorFunctionFlag::SET_CLASS_NAME);
12371244

12381245
inline void SetConstructorFunction(v8::Local<v8::Object> that,
12391246
v8::Local<v8::String> name,
1240-
v8::Local<v8::FunctionTemplate> tmpl);
1247+
v8::Local<v8::FunctionTemplate> tmpl,
1248+
SetConstructorFunctionFlag flag =
1249+
SetConstructorFunctionFlag::SET_CLASS_NAME);
12411250

12421251
void AtExit(void (*cb)(void* arg), void* arg);
12431252
void RunAtExitCallbacks();

0 commit comments

Comments
 (0)