Skip to content

Commit dc21154

Browse files
committed
child_process: expose a single function with a single bool argument
1 parent 0b741f4 commit dc21154

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

lib/internal/child_process.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ function createSocket(pipe, readable, watchPeerClose) {
337337
if (watchPeerClose &&
338338
process.platform !== 'win32' &&
339339
typeof pipe?.watchPeerClose === 'function') {
340-
pipe.watchPeerClose(() => sock.destroy());
341-
sock.once('close', () => pipe.unwatchPeerClose?.());
340+
pipe.watchPeerClose(true, () => sock.destroy());
341+
sock.once('close', () => pipe.watchPeerClose(false));
342342
}
343343
return sock;
344344
}

src/pipe_wrap.cc

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ void PipeWrap::Initialize(Local<Object> target,
8282
SetProtoMethod(isolate, t, "connect", Connect);
8383
SetProtoMethod(isolate, t, "open", Open);
8484
SetProtoMethod(isolate, t, "watchPeerClose", WatchPeerClose);
85-
SetProtoMethod(isolate, t, "unwatchPeerClose", UnwatchPeerClose);
8685

8786
#ifdef _WIN32
8887
SetProtoMethod(isolate, t, "setPendingInstances", SetPendingInstances);
@@ -114,7 +113,6 @@ void PipeWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
114113
registry->Register(Connect);
115114
registry->Register(Open);
116115
registry->Register(WatchPeerClose);
117-
registry->Register(UnwatchPeerClose);
118116
#ifdef _WIN32
119117
registry->Register(SetPendingInstances);
120118
#endif
@@ -227,6 +225,22 @@ void PipeWrap::WatchPeerClose(const FunctionCallbackInfo<Value>& args) {
227225
PipeWrap* wrap;
228226
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
229227

228+
CHECK_GT(args.Length(), 0);
229+
CHECK(args[0]->IsBoolean());
230+
const bool enable = args[0].As<v8::Boolean>()->Value();
231+
232+
// UnwatchPeerClose
233+
if (!enable) {
234+
if (!wrap->peer_close_watching_) {
235+
wrap->peer_close_cb_.Reset();
236+
return args.GetReturnValue().Set(0);
237+
}
238+
239+
wrap->peer_close_watching_ = false;
240+
wrap->peer_close_cb_.Reset();
241+
return args.GetReturnValue().Set(uv_read_stop(wrap->stream()));
242+
}
243+
230244
if (!wrap->IsAlive()) {
231245
return args.GetReturnValue().Set(UV_EBADF);
232246
}
@@ -235,14 +249,14 @@ void PipeWrap::WatchPeerClose(const FunctionCallbackInfo<Value>& args) {
235249
return args.GetReturnValue().Set(0);
236250
}
237251

238-
CHECK_GT(args.Length(), 0);
239-
CHECK(args[0]->IsFunction());
252+
CHECK_GT(args.Length(), 1);
253+
CHECK(args[1]->IsFunction());
240254

241255
Environment* env = wrap->env();
242256
Isolate* isolate = env->isolate();
243257

244258
// Store the JS callback securely so it isn't garbage collected.
245-
wrap->peer_close_cb_.Reset(isolate, args[0].As<Function>());
259+
wrap->peer_close_cb_.Reset(isolate, args[1].As<Function>());
246260
wrap->peer_close_watching_ = true;
247261

248262
// Start reading to detect EOF/ECONNRESET from the peer.
@@ -255,21 +269,6 @@ void PipeWrap::WatchPeerClose(const FunctionCallbackInfo<Value>& args) {
255269
args.GetReturnValue().Set(err);
256270
}
257271

258-
void PipeWrap::UnwatchPeerClose(const FunctionCallbackInfo<Value>& args) {
259-
PipeWrap* wrap;
260-
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
261-
262-
if (!wrap->peer_close_watching_) {
263-
wrap->peer_close_cb_.Reset();
264-
return args.GetReturnValue().Set(0);
265-
}
266-
267-
// Stop listening and release the JS callback to prevent memory leaks.
268-
wrap->peer_close_watching_ = false;
269-
wrap->peer_close_cb_.Reset();
270-
args.GetReturnValue().Set(uv_read_stop(wrap->stream()));
271-
}
272-
273272
void PipeWrap::PeerCloseAlloc(uv_handle_t* handle,
274273
size_t suggested_size,
275274
uv_buf_t* buf) {

src/pipe_wrap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class PipeWrap : public ConnectionWrap<PipeWrap, uv_pipe_t> {
6666
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args);
6767
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
6868
static void WatchPeerClose(const v8::FunctionCallbackInfo<v8::Value>& args);
69-
static void UnwatchPeerClose(const v8::FunctionCallbackInfo<v8::Value>& args);
7069
static void PeerCloseAlloc(uv_handle_t* handle,
7170
size_t suggested_size,
7271
uv_buf_t* buf);

0 commit comments

Comments
 (0)