UNPKG

servertime

Version:

Add server-timing header to your node.js app, with nanosecond precision.

161 lines (136 loc) 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.middleware = middleware; exports.addToResponse = addToResponse; exports.start = start; exports.end = end; exports.timeMiddleware = timeMiddleware; exports.createTimer = createTimer; Object.defineProperty(exports, "ServerTiming", { enumerable: true, get: function () { return _Timer.default; } }); exports.default = void 0; var _onHeaders = _interopRequireDefault(require("on-headers")); var _clocks = require("./clocks"); var _Timer = _interopRequireDefault(require("./Timer")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Returns an express-style middleware that automatically adds `res.serverTiming` to the resposne object. */ function middleware(options = {}) { return function (_req, res, next) { addToResponse(res, options); if (next) { next(); } }; } /** * Adds a `res.serverTiming` to the specified response. * * @param res - The resposne object. `res.serverTiming` will be set to a new `Timer` object. * `res.setHeader()` will automatically be called with the new header. * @param [options={}] - Options. */ function addToResponse(res, options = {}) { const devOnly = 'devOnly' in options ? options.devOnly : true; const clock = _clocks.CLOCKS[options.clock || 'hr']; // Don't add this twice. if (res.serverTiming) { return; } // If we're not in production, then do nothing. Add a `dummy` serverTiming so caller code doesn't // need to change. if (devOnly && process.env.NODE_ENV === 'production') { res.serverTiming = new _Timer.default({ isDummy: true, clock }); return; } res.serverTiming = new _Timer.default({ clock }); (0, _onHeaders.default)(res, function () { const header = res.serverTiming.getHeader(); if (header && !this.getHeader('server-timing')) { this.setHeader('server-timing', header); } }); } /** * Returns a mini-middleware that calls `res.serverTiming.start(slug, label)`. * * @param slug - The slug to use for timing. The same slug must be supplied to `end(slug)` in order * for this timing to show up in the final header. * @param [label] - Label to use in the server-timing header. * @return - Middleware function. */ function start(slug, label) { return (_req, res, next) => { res.serverTiming.start(slug, label); if (next) { next(); } }; } /** * Returns a mini-middleware that calls `res.serverTiming.end(slug, label)`. * * @param slug - The slug to supplied to `start()`. * @return - Middleware function. */ function end(slug) { return (_req, res, next) => { res.serverTiming.end(slug); if (next) { next(); } }; } /** * Wraps a middleware and adds timing data for it to the server-timing header. * * @param slug - The slug to use for timing. * @param middleware - The `fn(req, res, next)` function to time. Note that the function must call * `next()` in order to be timed. * @return - Middleware function. */ function timeMiddleware(slug, l, mw) { const middleware = mw || l; const label = mw ? l : slug; return (req, res, next) => { res.serverTiming.start(slug, label); middleware(req, res, err => { res.serverTiming.end(slug); next(err); }); }; } /** * Create a new Timer object. * @param [options={}] - Options. * @param [options.clock=hr] - The default is 'hr' which uses `process.hrtime()` to get nanosecond accuracy, * but if you're on a platform that doesn't support `process.hrtime()` you can pass in 'ms' to use `Date.now()` * instead. * @return - New Timer object. */ function createTimer(options = {}) { const clock = _clocks.CLOCKS[options.clock || 'hr']; return new _Timer.default({ clock }); } var _default = { middleware, addToResponse, start, end, timeMiddleware, createTimer }; exports.default = _default; //# sourceMappingURL=index.js.map