UNPKG

core-mvc

Version:

Simple but powerful MVC framework for NodeJS.

73 lines (72 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.instrumented = void 0; const log_1 = require("./util/log"); const isPromise = (value) => { const { then } = value; if (!then) { return false; } return typeof then === 'function'; }; const instrumented = ({ argumentFormat, includeResult, singleArgument, minDuration = -1, maxDuration, } = {}) => (target, propertyName, propertyDescriptor) => { // propertyName === "doSomething" // propertyDescriptor === Object.getOwnPropertyDescriptor(MyClass.prototype, "doSomething") const method = propertyDescriptor.value; const source = target.constructor.name; propertyDescriptor.value = function (...args) { const startTime = Date.now(); const maxTimeout = maxDuration ? setTimeout(() => { const duration = (Date.now() - startTime) / 1000; (0, log_1.log)({ event: propertyName, source, stage: 'timeout', args: argValue, duration, }); }, maxDuration * 1000) : undefined; const result = method.apply(this, args); const argValue = singleArgument ? args[0] : argumentFormat ? argumentFormat(...args) : args; if (isPromise(result)) { return result.then(value => { if (maxTimeout) { clearTimeout(maxTimeout); } const duration = (Date.now() - startTime) / 1000; if (duration > minDuration) { (0, log_1.log)({ event: propertyName, source, args: argValue, result: includeResult ? value : undefined, duration, }); } return value; }); } if (maxTimeout) { clearTimeout(maxTimeout); } const duration = (Date.now() - startTime) / 1000; if (duration > minDuration) { (0, log_1.log)({ event: propertyName, source, args: argValue, result: includeResult ? result : undefined, duration, }); } return result; }; return propertyDescriptor; }; exports.instrumented = instrumented;