UNPKG

servertime

Version:

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

95 lines (94 loc) 3.64 kB
/// <reference types="node" /> import http from 'http'; import { CLOCKS } from './clocks'; import ServerTiming from './Timer'; export { ServerTiming }; declare module 'http' { interface ServerResponse { serverTiming: ServerTiming; } } export interface ServertimeOptions { /** * If truthy, then only add a 'server-timing' header when NODE_ENV is not * "production". Server timing information can reveal a lot about your * infrastructure to a potential attacker, so be careful with this. Defaults * to `true`. */ devOnly?: boolean; /** * The clock to use. `hr` is the default, high resoltuion timer. `ms` * will use a lower millisecond resolution timer. */ clock?: keyof typeof CLOCKS; } interface Middleware { (req: http.IncomingMessage, res: http.ServerResponse, next: (err?: Error) => void): void; } /** * Returns an express-style middleware that automatically adds `res.serverTiming` to the resposne object. */ export declare function middleware(options?: ServertimeOptions): Middleware; /** * 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. */ export declare function addToResponse(res: http.ServerResponse, options?: ServertimeOptions): void; /** * 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. */ export declare function start(slug: string, label?: string): Middleware; /** * Returns a mini-middleware that calls `res.serverTiming.end(slug, label)`. * * @param slug - The slug to supplied to `start()`. * @return - Middleware function. */ export declare function end(slug: string): Middleware; /** * 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. */ export declare function timeMiddleware(slug: string, middleware: Middleware): Middleware; /** * Wraps a middleware and adds timing data for it to the server-timing header. * * @param slug - The slug to use for timing. * @param label - Label to use in the server-timing header. * @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. */ export declare function timeMiddleware(slug: string, label: string, middleware: Middleware): Middleware; /** * 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. */ export declare function createTimer(options?: { clock?: keyof typeof CLOCKS; }): ServerTiming; declare const _default: { middleware: typeof middleware; addToResponse: typeof addToResponse; start: typeof start; end: typeof end; timeMiddleware: typeof timeMiddleware; createTimer: typeof createTimer; }; export default _default;