UNPKG

@kitmi/jacaranda

Version:

JavaScript application framework

34 lines (33 loc) 1.1 kB
/** * Middleware to generate a unique request id for tracking in log. * @module Middleware_RequestId */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return _default; } }); const _utils = require("@kitmi/utils"); const BASE_LINE = 1733961600000; const MAX_SEQ = 16777216; let counter = 0; const middlewareCreator = ()=>{ return (ctx, next)=>{ const instanceId = (0, _utils.ntob64)(parseInt(process.env.NODE_APP_INSTANCE || '0') + 1); counter = ++counter % MAX_SEQ; const seqId = (0, _utils.ntob64)(counter); // from restart of the server, max 4 chars const timestamp = (0, _utils.ntob64)(Date.now() - BASE_LINE); const reqId = `${timestamp}:${instanceId}:${seqId}`; ctx.state.reqId = ctx.req.id = reqId; if (!ctx.header['x-request-id']) { ctx.set('x-request-id', reqId); } return next(); }; }; const _default = middlewareCreator; //# sourceMappingURL=requestId.js.map