Skip to content

Commit cf04389

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

9 files changed

+254
-57
lines changed

doc/api/worker_threads.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ are part of the channel.
378378
<!-- YAML
379379
added: v10.5.0
380380
changes:
381+
- version: REPLACEME
382+
pr-url: https://github.com/nodejs/node/pull/37917
383+
description: Add 'BlockList' to the list of cloneable types.
381384
- version: v14.5.0
382385
pr-url: https://github.com/nodejs/node/pull/33360
383386
description: Added `KeyObject` to the list of cloneable types.
@@ -401,8 +404,11 @@ In particular, the significant differences to `JSON` are:
401404
* `value` may contain typed arrays, both using `ArrayBuffer`s
402405
and `SharedArrayBuffer`s.
403406
* `value` may contain [`WebAssembly.Module`][] instances.
404-
* `value` may not contain native (C++-backed) objects other than `MessagePort`s,
405-
[`FileHandle`][]s, and [`KeyObject`][]s.
407+
* `value` may not contain native (C++-backed) objects other than:
408+
* {FileHandle}s,
409+
* {KeyObject}s,
410+
* {MessagePort}s,
411+
* {net.BlockList}s,
406412

407413
```js
408414
const { MessageChannel } = require('worker_threads');
@@ -1032,7 +1038,6 @@ thread spawned will spawn another until the application crashes.
10321038
[`ERR_WORKER_NOT_RUNNING`]: errors.md#ERR_WORKER_NOT_RUNNING
10331039
[`EventTarget`]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
10341040
[`FileHandle`]: fs.md#fs_class_filehandle
1035-
[`KeyObject`]: crypto.md#crypto_class_keyobject
10361041
[`MessagePort`]: #worker_threads_class_messageport
10371042
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
10381043
[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array

lib/internal/blocklist.js

+41-9
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,6 +15,13 @@ 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');
@@ -26,14 +34,10 @@ const {
2634

2735
const { validateInt32 } = require('internal/validators');
2836

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;
37+
class BlockList extends JSTransferable {
38+
constructor() {
39+
super();
40+
this[kHandle] = new BlockListHandle();
3741
this[kHandle][owner_symbol] = this;
3842
}
3943

@@ -116,6 +120,34 @@ class BlockList {
116120
get rules() {
117121
return this[kHandle].getRules();
118122
}
123+
124+
[kClone]() {
125+
const handle = this[kHandle];
126+
return {
127+
data: { handle },
128+
deserializeInfo: 'internal/blocklist:InternalBlockList',
129+
};
130+
}
131+
132+
[kDeserialize]({ handle }) {
133+
this[kHandle] = handle;
134+
this[kHandle][owner_symbol] = this;
135+
}
136+
}
137+
138+
class InternalBlockList extends JSTransferable {
139+
constructor(handle) {
140+
super();
141+
this[kHandle] = handle;
142+
if (handle !== undefined)
143+
handle[owner_symbol] = this;
144+
}
119145
}
120146

121-
module.exports = BlockList;
147+
InternalBlockList.prototype.constructor = BlockList.prototype.constructor;
148+
ObjectSetPrototypeOf(InternalBlockList.prototype, BlockList.prototype);
149+
150+
module.exports = {
151+
BlockList,
152+
InternalBlockList,
153+
};

lib/net.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1757,8 +1757,7 @@ module.exports = {
17571757
_normalizeArgs: normalizeArgs,
17581758
_setSimultaneousAccepts,
17591759
get BlockList() {
1760-
if (BlockList === undefined)
1761-
BlockList = require('internal/blocklist');
1760+
BlockList = BlockList ?? require('internal/blocklist').BlockList;
17621761
return BlockList;
17631762
},
17641763
connect,

src/env-inl.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -1110,15 +1110,18 @@ inline void Environment::SetInstanceMethod(v8::Local<v8::FunctionTemplate> that,
11101110
inline void Environment::SetConstructorFunction(
11111111
v8::Local<v8::Object> that,
11121112
const char* name,
1113-
v8::Local<v8::FunctionTemplate> tmpl) {
1114-
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl);
1113+
v8::Local<v8::FunctionTemplate> tmpl,
1114+
SetConstructorFunctionFlag flag) {
1115+
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl, flag);
11151116
}
11161117

11171118
inline void Environment::SetConstructorFunction(
11181119
v8::Local<v8::Object> that,
11191120
v8::Local<v8::String> name,
1120-
v8::Local<v8::FunctionTemplate> tmpl) {
1121-
tmpl->SetClassName(name);
1121+
v8::Local<v8::FunctionTemplate> tmpl,
1122+
SetConstructorFunctionFlag flag) {
1123+
if (LIKELY(flag == SetConstructorFunctionFlag::SET_CLASS_NAME))
1124+
tmpl->SetClassName(name);
11221125
that->Set(
11231126
context(),
11241127
name,

src/env.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ constexpr size_t kFsStatsBufferLength =
411411
V(async_wrap_object_ctor_template, v8::FunctionTemplate) \
412412
V(base_object_ctor_template, v8::FunctionTemplate) \
413413
V(binding_data_ctor_template, v8::FunctionTemplate) \
414-
V(blocklist_instance_template, v8::ObjectTemplate) \
414+
V(blocklist_constructor_template, v8::FunctionTemplate) \
415415
V(compiled_fn_entry_template, v8::ObjectTemplate) \
416416
V(dir_instance_template, v8::ObjectTemplate) \
417417
V(fd_constructor_template, v8::ObjectTemplate) \
@@ -1111,13 +1111,22 @@ class Environment : public MemoryRetainer {
11111111
const char* name,
11121112
v8::FunctionCallback callback);
11131113

1114+
enum class SetConstructorFunctionFlag {
1115+
NONE,
1116+
SET_CLASS_NAME,
1117+
};
1118+
11141119
inline void SetConstructorFunction(v8::Local<v8::Object> that,
11151120
const char* name,
1116-
v8::Local<v8::FunctionTemplate> tmpl);
1121+
v8::Local<v8::FunctionTemplate> tmpl,
1122+
SetConstructorFunctionFlag flag =
1123+
SetConstructorFunctionFlag::SET_CLASS_NAME);
11171124

11181125
inline void SetConstructorFunction(v8::Local<v8::Object> that,
11191126
v8::Local<v8::String> name,
1120-
v8::Local<v8::FunctionTemplate> tmpl);
1127+
v8::Local<v8::FunctionTemplate> tmpl,
1128+
SetConstructorFunctionFlag flag =
1129+
SetConstructorFunctionFlag::SET_CLASS_NAME);
11211130

11221131
void AtExit(void (*cb)(void* arg), void* arg);
11231132
void RunAtExitCallbacks();

0 commit comments

Comments
 (0)