diff --git a/lib/dn.js b/lib/dn.js index 7c8d678..1f3b4a1 100644 --- a/lib/dn.js +++ b/lib/dn.js @@ -234,10 +234,9 @@ DN.prototype.parentOf = function(dn) { return parent; }; - DN.prototype.equals = function(dn) { if (!(dn instanceof DN)) - return false; + dn = parse(dn); if (this.rdns.length !== dn.rdns.length) return false; diff --git a/lib/server.js b/lib/server.js index a56737f..dc74864 100644 --- a/lib/server.js +++ b/lib/server.js @@ -377,22 +377,9 @@ module.exports = Server; * @throws {TypeError} on bad input */ Server.prototype.add = function(name) { - if (!name || typeof(name) !== 'string') - throw new TypeError('name (string) required'); - if (arguments.length < 2) - throw new TypeError('name and at least one handler required'); - - var backend = this; - var index = 1; - if (typeof(arguments[1]) === 'object') { - backend = arguments[1]; - index = 2; - } - var route = this._getRoute(dn.parse(name), backend); - route['0x' + Protocol.LDAP_REQ_ADD.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_ADD, + name, + Array.prototype.slice.call(arguments, 1)); }; @@ -407,22 +394,9 @@ Server.prototype.add = function(name) { * @throws {TypeError} on bad input */ Server.prototype.bind = function(name) { - if (!name || typeof(name) !== 'string') - throw new TypeError('name (string) required'); - if (arguments.length < 2) - throw new TypeError('name and at least one handler required'); - - var backend = this; - var index = 1; - if (typeof(arguments[1]) === 'object') { - backend = arguments[1]; - index = 2; - } - var route = this._getRoute(dn.parse(name), backend); - route['0x' + Protocol.LDAP_REQ_BIND.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_BIND, + name, + Array.prototype.slice.call(arguments, 1)); }; @@ -437,22 +411,9 @@ Server.prototype.bind = function(name) { * @throws {TypeError} on bad input */ Server.prototype.compare = function(name) { - if (!name || typeof(name) !== 'string') - throw new TypeError('name (string) required'); - if (arguments.length < 2) - throw new TypeError('name and at least one handler required'); - - var backend = this; - var index = 1; - if (typeof(arguments[1]) === 'object') { - backend = arguments[1]; - index = 2; - } - var route = this._getRoute(dn.parse(name), backend); - route['0x' + Protocol.LDAP_REQ_COMPARE.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_COMPARE, + name, + Array.prototype.slice.call(arguments, 1)); }; @@ -467,22 +428,9 @@ Server.prototype.compare = function(name) { * @throws {TypeError} on bad input */ Server.prototype.del = function(name) { - if (!name || typeof(name) !== 'string') - throw new TypeError('name (string) required'); - if (arguments.length < 2) - throw new TypeError('name and at least one handler required'); - - var backend = this; - var index = 1; - if (typeof(arguments[1]) === 'object') { - backend = arguments[1]; - index = 2; - } - var route = this._getRoute(dn.parse(name), backend); - route['0x' + Protocol.LDAP_REQ_DELETE.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_DELETE, + name, + Array.prototype.slice.call(arguments, 1)); }; @@ -497,22 +445,10 @@ Server.prototype.del = function(name) { * @throws {TypeError} on bad input. */ Server.prototype.exop = function(name) { - if (!name || typeof(name) !== 'string') - throw new TypeError('name (string) required'); - if (arguments.length < 2) - throw new TypeError('name and at least one handler required'); - - var backend = this; - var index = 1; - if (typeof(arguments[1]) === 'object') { - backend = arguments[1]; - index = 2; - } - var route = this._getRoute(name, backend); - route['0x' + Protocol.LDAP_REQ_EXTENSION.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_EXTENSION, + name, + Array.prototype.slice.call(arguments, 1), + true); }; @@ -527,22 +463,9 @@ Server.prototype.exop = function(name) { * @throws {TypeError} on bad input */ Server.prototype.modify = function(name) { - if (!name || typeof(name) !== 'string') - throw new TypeError('name (string) required'); - if (arguments.length < 2) - throw new TypeError('name and at least one handler required'); - - var backend = this; - var index = 1; - if (typeof(arguments[1]) === 'object') { - backend = arguments[1]; - index = 2; - } - var route = this._getRoute(dn.parse(name), backend); - route['0x' + Protocol.LDAP_REQ_MODIFY.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_MODIFY, + name, + Array.prototype.slice.call(arguments, 1)); }; @@ -557,22 +480,9 @@ Server.prototype.modify = function(name) { * @throws {TypeError} on bad input */ Server.prototype.modifyDN = function(name) { - if (!name || typeof(name) !== 'string') - throw new TypeError('name (string) required'); - if (arguments.length < 2) - throw new TypeError('name and at least one handler required'); - - var backend = this; - var index = 1; - if (typeof(arguments[1]) === 'object') { - backend = arguments[1]; - index = 2; - } - var route = this._getRoute(dn.parse(name), backend); - route['0x' + Protocol.LDAP_REQ_MODRDN.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_MODRDN, + name, + Array.prototype.slice.call(arguments, 1)); }; @@ -587,22 +497,9 @@ Server.prototype.modifyDN = function(name) { * @throws {TypeError} on bad input */ Server.prototype.search = function(name) { - if (!name || typeof(name) !== 'string') - throw new TypeError('name (string) required'); - if (arguments.length < 2) - throw new TypeError('name and at least one handler required'); - - var backend = this; - var index = 1; - if (typeof(arguments[1]) === 'object') { - backend = arguments[1]; - index = 2; - } - var route = this._getRoute(dn.parse(name), backend); - route['0x' + Protocol.LDAP_REQ_SEARCH.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_SEARCH, + name, + Array.prototype.slice.call(arguments, 1)); }; @@ -616,20 +513,10 @@ Server.prototype.search = function(name) { * @throws {TypeError} on bad input */ Server.prototype.unbind = function() { - if (arguments.length < 1) - throw new TypeError('at least one handler required'); - - var backend = this; - var index = 0; - if (typeof(arguments[0]) === 'object') { - backend = arguments[0]; - index = 1; - } - var route = this._getRoute('unbind', backend); - route['0x' + Protocol.LDAP_REQ_UNBIND.toString(16)] = - mergeFunctionArgs(Array.prototype.slice.call(arguments, index)); - - return this; + return this._mount(Protocol.LDAP_REQ_UNBIND, + 'unbind', + Array.prototype.slice.call(arguments, 0), + true); }; @@ -756,3 +643,25 @@ Server.prototype._getHandlerChain = function(req) { }; }; + +Server.prototype._mount = function(op, name, argv, notDN) { + assert.ok(op); + assert.ok(name); + assert.ok(argv); + + if (!name || typeof(name) !== 'string') + throw new TypeError('name (string) required'); + if (argv.length < 1) + throw new Error('at least one handler required'); + + var backend = this; + var index = 0; + if (typeof(argv[0]) === 'object' && !Array.isArray(argv[0])) { + backend = argv[0]; + index = 1; + } + var route = this._getRoute(notDN ? name : dn.parse(name), backend); + route['0x' + op.toString(16)] = mergeFunctionArgs(argv.slice(index)); + + return this; +};