diff --git a/lib/client/client.js b/lib/client/client.js index a62d842..653d799 100644 --- a/lib/client/client.js +++ b/lib/client/client.js @@ -105,7 +105,10 @@ function setupSocket(socket, opts) { // On close we have to walk the outstanding messages and go invoke their // callback with an error socket.on('close', function onClose(had_err) { + socket.removeAllListeners('connect'); + socket.removeAllListeners('close'); socket.removeAllListeners('data'); + socket.removeAllListeners('drain'); socket.removeAllListeners('end'); socket.removeAllListeners('error'); socket.removeAllListeners('timeout'); @@ -700,7 +703,9 @@ Client.prototype._connect = function _connect() { var socket = null; var timer = false; - function onConnect() { + socket = proto.connect((this.port || this.socketPath), this.host); + + socket.once('connect', function onConnect() { if (timer) clearTimeout(timer); @@ -709,24 +714,21 @@ Client.prototype._connect = function _connect() { socket.ldap.id = nextClientId() + '__' + socket.ldap.id; self.log = self.log.child({ldap_id: socket.ldap.id}, true); - if (log.trace()) - log.trace('connect event'); + log.trace('connect event'); self.socket = socket; self.emit('connect', socket); - } - - socket = proto.connect((this.port || this.socketPath), - (this.host ? this.host : onConnect), - (this.host ? onConnect : undefined)); + }); setupSocket(socket, this); if (this.connectTimeout) { timer = setTimeout(function onConnectTimeout() { - socket.destroy(); + if (!socket || !socket.readable || !socket.writeable) { + socket.destroy(); - self.emit('connectTimeout'); + self.emit('connectTimeout'); + } }, this.connectTimeout); } diff --git a/lib/messages/result.js b/lib/messages/result.js index 0285cf9..54f8d5c 100644 --- a/lib/messages/result.js +++ b/lib/messages/result.js @@ -87,8 +87,8 @@ LDAPResult.prototype.end = function (status) { } } catch (e) { - this.log.warn('%s failure to write message %j: %s', - this.connection.ldap.id, this.json, e.toString()); + this.log.warn(e, '%s failure to write message %j', + this.connection.ldap.id, this.json); } }; diff --git a/lib/messages/search_response.js b/lib/messages/search_response.js index 4e8c276..33d8523 100644 --- a/lib/messages/search_response.js +++ b/lib/messages/search_response.js @@ -108,13 +108,13 @@ SearchResponse.prototype.send = function (entry, nofiltering) { } // Restore attributes - Object.keys(savedAttrs).forEach(function (k) { + Object.keys(savedAttrs || {}).forEach(function (k) { save.attributes[k] = savedAttrs[k]; }); } catch (e) { - this.log.warn('%s failure to write message %j: %s', - this.connection.ldap.id, this.json, e.toString()); + this.log.warn(e, '%s failure to write message %j', + this.connection.ldap.id, this.json); } }; diff --git a/package.json b/package.json index e227bad..d2c09f0 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "name": "ldapjs", "homepage": "http://ldapjs.org", "description": "LDAP client and server APIs", - "version": "0.5.1", + "version": "0.5.2", "repository": { "type": "git", "url": "git://github.com/mcavage/node-ldapjs.git" @@ -21,7 +21,7 @@ "lib": "./lib" }, "engines": { - "node": "~0.6" + "node": ">=0.6" }, "dependencies": { "asn1": "0.1.11", diff --git a/test/client.test.js b/test/client.test.js index 0419495..72dc77c 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -130,15 +130,16 @@ test('setup', function (t) { server.listen(SOCKET, function () { client = ldap.createClient({ - connectTimeout: 100, + connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10), socketPath: SOCKET, - maxConnections: process.env.LDAP_MAX_CONNS || 5, + maxConnections: parseInt(process.env.LDAP_MAX_CONNS || 5, 10), idleTimeoutMillis: 10, log: new Logger({ name: 'ldapjs_unit_test', stream: process.stderr, level: (process.env.LOG_LEVEL || 'info'), - serializers: Logger.stdSerializers + serializers: Logger.stdSerializers, + src: true }) }); t.ok(client); @@ -149,7 +150,6 @@ test('setup', function (t) { test('simple bind failure', function (t) { - try { client.bind(BIND_DN, uuid(), function (err, res) { t.ok(err); t.notOk(res); @@ -162,10 +162,6 @@ test('simple bind failure', function (t) { t.end(); }); - } catch (e) { - console.log(e.stack); - process.exit(1); - } });