UNPKG

@shopify/react-server

Version:
32 lines (25 loc) 1.05 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var compose = require('koa-compose'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var compose__default = /*#__PURE__*/_interopDefaultLegacy(compose); const MILLIS_PER_SECOND = 1000; const NANOS_PER_MILLIS = 1e6; const START_TIME_STATE_KEY = Symbol('startTime'); async function startRequestTiming(ctx, next) { ctx.state[START_TIME_STATE_KEY] = process.hrtime(); await next(); } async function middleware(ctx, next) { try { await next(); } finally { const [seconds, nanoseconds] = process.hrtime(ctx.state[START_TIME_STATE_KEY]); const ms = seconds * MILLIS_PER_SECOND + nanoseconds / NANOS_PER_MILLIS; const requestTime = Math.round(ms); const uiMetrics = `ui;request_time=${requestTime}`; ctx.set('Server-Timing', uiMetrics); } } const metricsMiddleware = compose__default["default"]([startRequestTiming, middleware]); exports.metricsMiddleware = metricsMiddleware;