UNPKG

@thi.ng/server

Version:

Minimal HTTP server with declarative routing, static file serving and freely extensible via pre/post interceptors

27 lines (26 loc) 569 B
import { methodForLevel } from "@thi.ng/logger"; import { now, timeDiff } from "@thi.ng/timestamp"; const measure = (level = "DEBUG") => { const requests = /* @__PURE__ */ new WeakMap(); const method = methodForLevel(level); return { pre: (ctx) => { requests.set(ctx, now()); return true; }, post: (ctx) => { const t0 = requests.get(ctx); if (t0) { requests.delete(ctx); ctx.logger[method]( `request processed in: ${timeDiff(t0).toFixed(3)}ms` ); } } }; }; export { measure };