Refactor DTrace handlers to support a backend object

This commit is contained in:
Mark Cavage 2011-09-09 13:40:25 -07:00
parent 5afb6ee05a
commit e365d40c8d
1 changed files with 62 additions and 53 deletions

View File

@ -174,10 +174,14 @@ function noExOpHandler(req, res, next) {
} }
function getDTraceHander(op, cb1, cb2) { function getArgumentsWithDTrace(args, op, cb1, cb2) {
assert.ok(op); assert.ok(op);
return function(req, res, next) { var index = 0;
if (typeof(args[0]) === 'object')
index = 1;
args.splice(index, 0, function(req, res, next) {
dtrace.fire(op, function() { dtrace.fire(op, function() {
var c = req.connection; var c = req.connection;
return [ return [
@ -190,9 +194,10 @@ function getDTraceHander(op, cb1, cb2) {
]; ];
}); });
return next(); return next();
}; });
}
return args;
}
///--- API ///--- API
@ -425,11 +430,11 @@ module.exports = Server;
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.add = function(name) { Server.prototype.add = function(name) {
var args = Array.prototype.slice.call(arguments, 1); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('add', 'add',
function(req, res) { function(req, res) {
return req.attributes.length; return req.attributes.length;
})); });
return this._mount(Protocol.LDAP_REQ_ADD, name, args); return this._mount(Protocol.LDAP_REQ_ADD, name, args);
}; };
@ -446,8 +451,8 @@ 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) {
var args = Array.prototype.slice.call(arguments, 1); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('bind')); 'bind');
return this._mount(Protocol.LDAP_REQ_BIND, name, args); return this._mount(Protocol.LDAP_REQ_BIND, name, args);
}; };
@ -464,14 +469,14 @@ 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) {
var args = Array.prototype.slice.call(arguments, 1); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('compare', 'compare',
function(req, res) { function(req, res) {
return req.attribute; return req.attribute;
}, },
function(req, res) { function(req, res) {
return req.value; return req.value;
})); });
return this._mount(Protocol.LDAP_REQ_COMPARE, name, args); return this._mount(Protocol.LDAP_REQ_COMPARE, name, args);
}; };
@ -488,8 +493,8 @@ 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) {
var args = Array.prototype.slice.call(arguments, 1); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('delete')); 'delete');
return this._mount(Protocol.LDAP_REQ_DELETE, name, args); return this._mount(Protocol.LDAP_REQ_DELETE, name, args);
}; };
@ -506,14 +511,14 @@ 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) {
var args = Array.prototype.slice.call(arguments, 1); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('exop', 'exop',
function(req, res) { function(req, res) {
return req.name; return req.name;
}, },
function(req, res) { function(req, res) {
return req.value; return req.value;
})); });
return this._mount(Protocol.LDAP_REQ_EXTENSION, name, args, true); return this._mount(Protocol.LDAP_REQ_EXTENSION, name, args, true);
}; };
@ -530,11 +535,11 @@ 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) {
var args = Array.prototype.slice.call(arguments, 1); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('modify', 'modify',
function(req, res) { function(req, res) {
return req.changes.length; return req.changes.length;
})); });
return this._mount(Protocol.LDAP_REQ_MODIFY, name, args); return this._mount(Protocol.LDAP_REQ_MODIFY, name, args);
}; };
@ -551,15 +556,15 @@ 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) {
var args = Array.prototype.slice.call(arguments, 1); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('modifyDN', 'modifyDN',
function(req, res) { function(req, res) {
return req.newRdn.toString(); return req.newRdn.toString();
}, },
function(req, res) { function(req, res) {
return (req.newSuperior ? return (req.newSuperior ?
req.newSuperior.toString() : ''); req.newSuperior.toString() : '');
})); });
return this._mount(Protocol.LDAP_REQ_MODRDN, name, args); return this._mount(Protocol.LDAP_REQ_MODRDN, name, args);
}; };
@ -576,14 +581,14 @@ 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) {
var args = Array.prototype.slice.call(arguments, 1); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('search', 'search',
function(req, res) { function(req, res) {
return req.scope; return req.scope;
}, },
function(req, res) { function(req, res) {
return req.filter.toString(); return req.filter.toString();
})); });
return this._mount(Protocol.LDAP_REQ_SEARCH, name, args); return this._mount(Protocol.LDAP_REQ_SEARCH, name, args);
}; };
@ -599,8 +604,8 @@ Server.prototype.search = function(name) {
* @throws {TypeError} on bad input * @throws {TypeError} on bad input
*/ */
Server.prototype.unbind = function() { Server.prototype.unbind = function() {
var args = Array.prototype.slice.call(arguments, 0); var args = getArgumentsWithDTrace(Array.prototype.slice.call(arguments, 1),
args.unshift(getDTraceHander('unbind')); 'unbind');
return this._mount(Protocol.LDAP_REQ_UNBIND, 'unbind', args, true); return this._mount(Protocol.LDAP_REQ_UNBIND, 'unbind', args, true);
}; };
@ -608,6 +613,9 @@ Server.prototype.unbind = function() {
// All these just reexpose the requisite net.Server APIs // All these just reexpose the requisite net.Server APIs
Server.prototype.listen = function(port, host, callback) { Server.prototype.listen = function(port, host, callback) {
if (!port)
throw new TypeError('port (number) required');
if (typeof(host) === 'function') { if (typeof(host) === 'function') {
callback = host; callback = host;
host = '0.0.0.0'; host = '0.0.0.0';
@ -752,6 +760,7 @@ Server.prototype._mount = function(op, name, argv, notDN) {
var backend = this; var backend = this;
var index = 0; var index = 0;
if (typeof(argv[0]) === 'object' && !Array.isArray(argv[0])) { if (typeof(argv[0]) === 'object' && !Array.isArray(argv[0])) {
backend = argv[0]; backend = argv[0];
index = 1; index = 1;