@@ -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-
273272void PipeWrap::PeerCloseAlloc (uv_handle_t * handle,
274273 size_t suggested_size,
275274 uv_buf_t * buf) {
0 commit comments