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