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
This commit is contained in:
Patrick Mooney 2014-01-23 10:06:40 -06:00
parent c5cfc48da4
commit d20308265a
1 changed files with 6 additions and 3 deletions

View File

@ -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');