Rework server mount APIs

This commit is contained in:
Mark Cavage 2011-08-15 09:44:31 -07:00
parent dda17ef190
commit 120e0011a3
2 changed files with 52 additions and 144 deletions

View File

@ -234,10 +234,9 @@ DN.prototype.parentOf = function(dn) {
return parent; return parent;
}; };
DN.prototype.equals = function(dn) { DN.prototype.equals = function(dn) {
if (!(dn instanceof DN)) if (!(dn instanceof DN))
return false; dn = parse(dn);
if (this.rdns.length !== dn.rdns.length) if (this.rdns.length !== dn.rdns.length)
return false; return false;

View File

@ -377,22 +377,9 @@ module.exports = Server;
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.add = function(name) { Server.prototype.add = function(name) {
if (!name || typeof(name) !== 'string') return this._mount(Protocol.LDAP_REQ_ADD,
throw new TypeError('name (string) required'); name,
if (arguments.length < 2) Array.prototype.slice.call(arguments, 1));
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;
}; };
@ -407,22 +394,9 @@ Server.prototype.add = function(name) {
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.bind = function(name) { Server.prototype.bind = function(name) {
if (!name || typeof(name) !== 'string') return this._mount(Protocol.LDAP_REQ_BIND,
throw new TypeError('name (string) required'); name,
if (arguments.length < 2) Array.prototype.slice.call(arguments, 1));
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;
}; };
@ -437,22 +411,9 @@ Server.prototype.bind = function(name) {
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.compare = function(name) { Server.prototype.compare = function(name) {
if (!name || typeof(name) !== 'string') return this._mount(Protocol.LDAP_REQ_COMPARE,
throw new TypeError('name (string) required'); name,
if (arguments.length < 2) Array.prototype.slice.call(arguments, 1));
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;
}; };
@ -467,22 +428,9 @@ Server.prototype.compare = function(name) {
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.del = function(name) { Server.prototype.del = function(name) {
if (!name || typeof(name) !== 'string') return this._mount(Protocol.LDAP_REQ_DELETE,
throw new TypeError('name (string) required'); name,
if (arguments.length < 2) Array.prototype.slice.call(arguments, 1));
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;
}; };
@ -497,22 +445,10 @@ Server.prototype.del = function(name) {
* @throws {TypeError} on bad input. * @throws {TypeError} on bad input.
*/ */
Server.prototype.exop = function(name) { Server.prototype.exop = function(name) {
if (!name || typeof(name) !== 'string') return this._mount(Protocol.LDAP_REQ_EXTENSION,
throw new TypeError('name (string) required'); name,
if (arguments.length < 2) Array.prototype.slice.call(arguments, 1),
throw new TypeError('name and at least one handler required'); true);
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;
}; };
@ -527,22 +463,9 @@ Server.prototype.exop = function(name) {
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.modify = function(name) { Server.prototype.modify = function(name) {
if (!name || typeof(name) !== 'string') return this._mount(Protocol.LDAP_REQ_MODIFY,
throw new TypeError('name (string) required'); name,
if (arguments.length < 2) Array.prototype.slice.call(arguments, 1));
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;
}; };
@ -557,22 +480,9 @@ Server.prototype.modify = function(name) {
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.modifyDN = function(name) { Server.prototype.modifyDN = function(name) {
if (!name || typeof(name) !== 'string') return this._mount(Protocol.LDAP_REQ_MODRDN,
throw new TypeError('name (string) required'); name,
if (arguments.length < 2) Array.prototype.slice.call(arguments, 1));
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;
}; };
@ -587,22 +497,9 @@ Server.prototype.modifyDN = function(name) {
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.search = function(name) { Server.prototype.search = function(name) {
if (!name || typeof(name) !== 'string') return this._mount(Protocol.LDAP_REQ_SEARCH,
throw new TypeError('name (string) required'); name,
if (arguments.length < 2) Array.prototype.slice.call(arguments, 1));
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;
}; };
@ -616,20 +513,10 @@ Server.prototype.search = function(name) {
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.unbind = function() { Server.prototype.unbind = function() {
if (arguments.length < 1) return this._mount(Protocol.LDAP_REQ_UNBIND,
throw new TypeError('at least one handler required'); 'unbind',
Array.prototype.slice.call(arguments, 0),
var backend = this; true);
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;
}; };
@ -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;
};