Skip to content

Commit fb9257f

Browse files
committed
net: make net.BlockList cloneable
Signed-off-by: James M Snell <[email protected]> PR-URL: #37917 Reviewed-By: Matteo Collina <[email protected]>
1 parent 1bead01 commit fb9257f

File tree

9 files changed

+247
-55
lines changed

9 files changed

+247
-55
lines changed

doc/api/worker_threads.md

Lines changed: 4 additions & 0 deletions
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

Lines changed: 41 additions & 10 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,15 +1026,18 @@ inline void Environment::SetInstanceMethod(v8::Local<v8::FunctionTemplate> that,
10261026
inline void Environment::SetConstructorFunction(
10271027
v8::Local<v8::Object> that,
10281028
const char* name,
1029-
v8::Local<v8::FunctionTemplate> tmpl) {
1030-
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl);
1029+
v8::Local<v8::FunctionTemplate> tmpl,
1030+
SetConstructorFunctionFlag flag) {
1031+
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl, flag);
10311032
}
10321033

10331034
inline void Environment::SetConstructorFunction(
10341035
v8::Local<v8::Object> that,
10351036
v8::Local<v8::String> name,
1036-
v8::Local<v8::FunctionTemplate> tmpl) {
1037-
tmpl->SetClassName(name);
1037+
v8::Local<v8::FunctionTemplate> tmpl,
1038+
SetConstructorFunctionFlag flag) {
1039+
if (LIKELY(flag == SetConstructorFunctionFlag::SET_CLASS_NAME))
1040+
tmpl->SetClassName(name);
10381041
that->Set(
10391042
context(),
10401043
name,

src/env.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ constexpr size_t kFsStatsBufferLength =
452452
V(base_object_ctor_template, v8::FunctionTemplate) \
453453
V(binding_data_ctor_template, v8::FunctionTemplate) \
454454
V(blob_constructor_template, v8::FunctionTemplate) \
455-
V(blocklist_instance_template, v8::ObjectTemplate) \
455+
V(blocklist_constructor_template, v8::FunctionTemplate) \
456456
V(compiled_fn_entry_template, v8::ObjectTemplate) \
457457
V(dir_instance_template, v8::ObjectTemplate) \
458458
V(fd_constructor_template, v8::ObjectTemplate) \
@@ -1237,13 +1237,22 @@ class Environment : public MemoryRetainer {
12371237
const char* name,
12381238
v8::FunctionCallback callback);
12391239

1240+
enum class SetConstructorFunctionFlag {
1241+
NONE,
1242+
SET_CLASS_NAME,
1243+
};
1244+
12401245
inline void SetConstructorFunction(v8::Local<v8::Object> that,
12411246
const char* name,
1242-
v8::Local<v8::FunctionTemplate> tmpl);
1247+
v8::Local<v8::FunctionTemplate> tmpl,
1248+
SetConstructorFunctionFlag flag =
1249+
SetConstructorFunctionFlag::SET_CLASS_NAME);
12431250

12441251
inline void SetConstructorFunction(v8::Local<v8::Object> that,
12451252
v8::Local<v8::String> name,
1246-
v8::Local<v8::FunctionTemplate> tmpl);
1253+
v8::Local<v8::FunctionTemplate> tmpl,
1254+
SetConstructorFunctionFlag flag =
1255+
SetConstructorFunctionFlag::SET_CLASS_NAME);
12471256

12481257
void AtExit(void (*cb)(void* arg), void* arg);
12491258
void RunAtExitCallbacks();

0 commit comments

Comments
 (0)