Comparing version 1.1.0 to 1.2.0
@@ -28,2 +28,4 @@ 'use strict'; | ||
var debug = (0, _debug2.default)('derf'); | ||
function hrToNano(hr) { | ||
@@ -34,9 +36,9 @@ return hr[0] * 1e9 + hr[1]; | ||
// the default message to display | ||
function defaultPrinter(debug, time, callArgs, retArgs) { | ||
function defaultPrinter(print, time, callArgs, retArgs) { | ||
var displayTime = Math.floor(time / 1e5) / 10 + 'ms'; | ||
if (retArgs[0]) { | ||
debug('failed in %s', displayTime); | ||
print('failed in %s', displayTime); | ||
} else { | ||
debug('finished in %s', displayTime); | ||
print('finished in %s', displayTime); | ||
} | ||
@@ -51,6 +53,6 @@ } | ||
// 'cast' namespace to be a debug function | ||
var debug = typeof namespace === 'function' ? namespace : (0, _debug2.default)(namespace); | ||
var fnDebug = typeof namespace === 'function' ? namespace : (0, _debug2.default)(namespace); | ||
// noop if debug is not enabled | ||
if (!debug.enabled) { | ||
if (!fnDebug.enabled) { | ||
return fn; | ||
@@ -72,3 +74,3 @@ } | ||
var diff = hrToNano(process.hrtime(start)); | ||
printer(debug, diff, args, retArgs); | ||
printer(fnDebug, diff, args, retArgs); | ||
} catch (e) { | ||
@@ -90,2 +92,4 @@ /* noop */ | ||
var sync = exports.sync = wrap(function (fn, print) { | ||
debug('wrapping sync function: %s', fn.name || 'anonymous'); | ||
return function perfWrappedSync() { | ||
@@ -123,2 +127,4 @@ var start = process.hrtime(); | ||
var promise = exports.promise = wrap(function (fn, print) { | ||
debug('wrapping promise function: %s', fn.name || 'anonymous'); | ||
return function perfWrappedPromise() { | ||
@@ -145,2 +151,3 @@ for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
// it wasn't a promise. great job. | ||
debug('no promise returned from wrapped promise function. not logging'); | ||
return ret; | ||
@@ -157,2 +164,4 @@ }; | ||
var callback = exports.callback = wrap(function (fn, print) { | ||
debug('wrapping callback function: %s', fn.name || 'anonymous'); | ||
return function perfWrapped() { | ||
@@ -173,2 +182,3 @@ var _this = this; | ||
var _ret = function () { | ||
debug('wrapping callback at arguments[%s]', index); | ||
var cb = args[index]; | ||
@@ -193,2 +203,3 @@ args[index] = function perfWrappedCb() { | ||
// no callback at all. Wow.. | ||
debug('no callback passed to wrapped callback function. not logging'); | ||
return fn.apply(this, args); // TODO synchronously handle it? warn? | ||
@@ -205,15 +216,33 @@ }; | ||
var middleware = exports.middleware = wrap(function (fn, print) { | ||
// figure out what we're dealing with | ||
var isError = fn.length === 4; | ||
var resIndex = isError ? 2 : 1; | ||
var nextIndex = isError ? 3 : 2; | ||
debug('wrapping express middleware: %s', fn.name || 'anonymous'); | ||
var arity = fn.length; | ||
return function perfWrappedMiddleware() { | ||
for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
args[_key5] = arguments[_key5]; | ||
} | ||
// normal middleware? | ||
if (arity <= 3) { | ||
debug('%s args, is normal middleware', arity); | ||
return function perfWrappedMiddleware(req, res, next) { | ||
var start = process.hrtime(); | ||
var finished = false; | ||
function log() { | ||
if (!finished) { | ||
finished = true; | ||
// dont try to guess the retArgs | ||
print(start, [req, res], [undefined, undefined]); | ||
} | ||
} | ||
(0, _onFinished2.default)(res, log); | ||
return fn.call(this, req, res, function wrappedNext() { | ||
log(); | ||
next.apply(this, arguments); // eslint-disable-line prefer-rest-params | ||
}); | ||
}; | ||
} | ||
// must be error middleware | ||
debug('%s args, is error middleware', arity); | ||
return function perfWrappedMiddleware(err, req, res, next) { | ||
var start = process.hrtime(); | ||
var res = args[resIndex]; | ||
var next = args[nextIndex]; | ||
var finished = false; | ||
@@ -226,27 +255,12 @@ | ||
// dont try to guess the retArgs | ||
print(start, args, [undefined, undefined]); | ||
print(start, [err, req, res], [undefined, undefined]); | ||
} | ||
} | ||
// try to intercept the reponse end | ||
if (res) { | ||
(0, _onFinished2.default)(res, log); | ||
} | ||
// try to intercept calling next() | ||
if (next && typeof next === 'function') { | ||
args[nextIndex] = function perfWrappedNext() { | ||
log(); | ||
for (var _len6 = arguments.length, retArgs = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) { | ||
retArgs[_key6] = arguments[_key6]; | ||
} | ||
return next.apply(this, retArgs); | ||
}; | ||
} | ||
// | ||
return fn.apply(this, args); | ||
(0, _onFinished2.default)(res, log); | ||
return fn.call(this, err, req, res, function wrappedNext() { | ||
log(); | ||
next.apply(this, arguments); // eslint-disable-line prefer-rest-params | ||
}); | ||
}; | ||
}); |
{ | ||
"name": "derf", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
13285
203