From d20308265ae61820304941a3f87103977977382a Mon Sep 17 00:00:00 2001 From: Patrick Mooney Date: Thu, 23 Jan 2014 10:06:40 -0600 Subject: [PATCH] Circumvent lack of 'close' event for tls sockets It appears that in node.js (at least recent versions), the 'close' event is not emitted by TLS sockets. The CleartextStream class specifies that the 'close' event is optional, so such operation is allowed. In order to compensate, the event can be trapped at the raw net.socket instead. Fix mcavage/node-ldapjs#161 --- lib/client/client.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/client/client.js b/lib/client/client.js index 6dc9cde..1ad463e 100644 --- a/lib/client/client.js +++ b/lib/client/client.js @@ -103,11 +103,14 @@ function setupSocket(socket, opts) { }; } + // Since tls.socket doesn't emit 'close' events, we must register to receive + // them on net.socket instead + var closeSocket = (opts.secure ? socket.socket : socket); // On close we have to walk the outstanding messages and go invoke their - // callback with an error - socket.on('close', function onClose(had_err) { + // callback with an error. + closeSocket.on('close', function onClose(had_err) { socket.removeAllListeners('connect'); - socket.removeAllListeners('close'); + closeSocket.removeAllListeners('close'); socket.removeAllListeners('data'); socket.removeAllListeners('drain'); socket.removeAllListeners('end');