Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
net: make net.BlockList cloneable
Signed-off-by: James M Snell <jasnell@gmail.com>
  • Loading branch information
jasnell committed Mar 30, 2021
commit bc6cfdf4164c8f50893ff3424f5ffb1641a8bfdc
4 changes: 4 additions & 0 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ are part of the channel.
<!-- YAML
added: v10.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/37917
description: Add 'BlockList' to the list of cloneable types.
- version: v15.9.0
pr-url: https://github.com/nodejs/node/pull/37155
description: Add 'Histogram' types to the list of cloneable types.
Expand Down Expand Up @@ -569,6 +572,7 @@ In particular, the significant differences to `JSON` are:
* {Histogram}s,
* {KeyObject}s,
* {MessagePort}s,
* {net.BlockList}s,
* {X509Certificate}s.

```js
Expand Down
51 changes: 41 additions & 10 deletions lib/internal/blocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
Boolean,
ObjectSetPrototypeOf,
Symbol
} = primordials;

Expand All @@ -14,26 +15,28 @@ const {
const {
customInspectSymbol: kInspect,
} = require('internal/util');

const {
JSTransferable,
kClone,
kDeserialize,
} = require('internal/worker/js_transferable');

const { inspect } = require('internal/util/inspect');

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

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
} = require('internal/errors').codes;

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

class BlockList {
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;
class BlockList extends JSTransferable {
constructor() {
super();
this[kHandle] = new BlockListHandle();
this[kHandle][owner_symbol] = this;
}

Expand Down Expand Up @@ -107,6 +110,34 @@ class BlockList {
get rules() {
return this[kHandle].getRules();
}

[kClone]() {
const handle = this[kHandle];
return {
data: { handle },
deserializeInfo: 'internal/blocklist:InternalBlockList',
};
}

[kDeserialize]({ handle }) {
this[kHandle] = handle;
this[kHandle][owner_symbol] = this;
}
}

class InternalBlockList extends JSTransferable {
constructor(handle) {
super();
this[kHandle] = handle;
if (handle !== undefined)
handle[owner_symbol] = this;
}
}

module.exports = BlockList;
InternalBlockList.prototype.constructor = BlockList.prototype.constructor;
ObjectSetPrototypeOf(InternalBlockList.prototype, BlockList.prototype);

module.exports = {
BlockList,
InternalBlockList,
};
3 changes: 1 addition & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1760,8 +1760,7 @@ module.exports = {
_normalizeArgs: normalizeArgs,
_setSimultaneousAccepts,
get BlockList() {
if (BlockList === undefined)
BlockList = require('internal/blocklist');
BlockList ??= require('internal/blocklist').BlockList;
return BlockList;
},
connect,
Expand Down
11 changes: 7 additions & 4 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1026,15 +1026,18 @@ inline void Environment::SetInstanceMethod(v8::Local<v8::FunctionTemplate> that,
inline void Environment::SetConstructorFunction(
v8::Local<v8::Object> that,
const char* name,
v8::Local<v8::FunctionTemplate> tmpl) {
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl);
v8::Local<v8::FunctionTemplate> tmpl,
SetConstructorFunctionFlag flag) {
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl, flag);
}

inline void Environment::SetConstructorFunction(
v8::Local<v8::Object> that,
v8::Local<v8::String> name,
v8::Local<v8::FunctionTemplate> tmpl) {
tmpl->SetClassName(name);
v8::Local<v8::FunctionTemplate> tmpl,
SetConstructorFunctionFlag flag) {
if (LIKELY(flag == SetConstructorFunctionFlag::SET_CLASS_NAME))
tmpl->SetClassName(name);
that->Set(
context(),
name,
Expand Down
15 changes: 12 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ constexpr size_t kFsStatsBufferLength =
V(base_object_ctor_template, v8::FunctionTemplate) \
V(binding_data_ctor_template, v8::FunctionTemplate) \
V(blob_constructor_template, v8::FunctionTemplate) \
V(blocklist_instance_template, v8::ObjectTemplate) \
V(blocklist_constructor_template, v8::FunctionTemplate) \
V(compiled_fn_entry_template, v8::ObjectTemplate) \
V(dir_instance_template, v8::ObjectTemplate) \
V(fd_constructor_template, v8::ObjectTemplate) \
Expand Down Expand Up @@ -1237,13 +1237,22 @@ class Environment : public MemoryRetainer {
const char* name,
v8::FunctionCallback callback);

enum class SetConstructorFunctionFlag {
NONE,
SET_CLASS_NAME,
};

inline void SetConstructorFunction(v8::Local<v8::Object> that,
const char* name,
v8::Local<v8::FunctionTemplate> tmpl);
v8::Local<v8::FunctionTemplate> tmpl,
SetConstructorFunctionFlag flag =
SetConstructorFunctionFlag::SET_CLASS_NAME);

inline void SetConstructorFunction(v8::Local<v8::Object> that,
v8::Local<v8::String> name,
v8::Local<v8::FunctionTemplate> tmpl);
v8::Local<v8::FunctionTemplate> tmpl,
SetConstructorFunctionFlag flag =
SetConstructorFunctionFlag::SET_CLASS_NAME);

void AtExit(void (*cb)(void* arg), void* arg);
void RunAtExitCallbacks();
Expand Down
Loading