Move server getters/setters to prototype

This commit is contained in:
Patrick Mooney 2015-10-31 08:34:51 -05:00
parent 954231d358
commit e8607819e2
2 changed files with 68 additions and 35 deletions

View File

@ -491,38 +491,53 @@ function Server(options) {
this.server.on('error', function (err) {
self.emit('error', err);
});
this.__defineGetter__('maxConnections', function () {
return self.server.maxConnections;
});
this.__defineSetter__('maxConnections', function (val) {
self.server.maxConnections = val;
});
this.__defineGetter__('connections', function () {
return self.server.connections;
});
this.__defineGetter__('name', function () {
return 'LDAPServer';
});
this.__defineGetter__('url', function () {
var str;
if (!this.server.address().family) {
str = 'ldapi://';
str += this.host.replace(new RegExp('/', 'g'), '%2f');
return str;
}
if (this.server instanceof tls.Server) {
str = 'ldaps://';
} else {
str = 'ldap://';
}
str += self.host || 'localhost';
str += ':';
str += self.port || 389;
return str;
});
}
util.inherits(Server, EventEmitter);
Object.defineProperties(Server.prototype, {
maxConnections: {
get: function getMaxConnections() {
return this.server.maxConnections;
},
set: function setMaxConnections(val) {
this.server.maxConnections = val;
},
configurable: false
},
connections: {
get: function getConnections() {
return this.server.connections;
},
configurable: false
},
name: {
get: function getName() {
return 'LDAPServer';
},
configurable: false
},
url: {
get: function getURL() {
var str;
var addr = this.server.address();
if (!addr) {
return null;
}
if (!addr.family) {
str = 'ldapi://';
str += this.host.replace(new RegExp('/', 'g'), '%2f');
return str;
}
if (this.server instanceof tls.Server) {
str = 'ldaps://';
} else {
str = 'ldap://';
}
str += this.host + ':' + this.port;
return str;
},
configurable: false
}
});
module.exports = Server;
@ -705,7 +720,7 @@ Server.prototype.listen = function (port, host, callback) {
}
var self = this;
function _callback() {
function cbListen() {
if (typeof (port) === 'number') {
self.host = self.address().address;
self.port = self.address().port;
@ -719,9 +734,9 @@ Server.prototype.listen = function (port, host, callback) {
}
if (typeof (port) === 'number') {
return this.server.listen(port, host, _callback);
return this.server.listen(port, host, cbListen);
} else {
return this.server.listen(port, _callback);
return this.server.listen(port, cbListen);
}
};
Server.prototype.listenFD = function (fd) {

View File

@ -45,12 +45,30 @@ test('basic create', function (t) {
t.end();
});
test('properties', function (t) {
t.equal(server.name, 'LDAPServer');
// TODO: better test
server.maxConnections = 10;
t.equal(server.maxConnections, 10);
t.equal(server.url, null, 'url empty before bind');
// listen on a random port so we have a url
server.listen(0, 'localhost', function () {
t.ok(server.url);
server.close();
t.end();
});
});
test('listen on unix/named socket', function (t) {
t.plan(1);
t.plan(2);
server = ldap.createServer();
sock = getSock();
server.listen(sock, function () {
t.ok(true);
t.ok(server.url);
t.equal(server.url.split(':')[0], 'ldapi');
server.close();
t.end();
});