'use strict'; const elysia = require('elysia'); const pc = require('picocolors'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const pc__default = /*#__PURE__*/_interopDefaultCompat(pc); const UNITS = ["\xB5s", "ms", "s"]; const DURATION_FORMATTER = Intl.NumberFormat(void 0, { maximumFractionDigits: 2 }); function duration(duration2) { if (!duration2) { return "-/-"; } let unitIndex = 0; while (duration2 >= 1e3 && unitIndex < UNITS.length - 1) { duration2 /= 1e3; unitIndex++; } return `${DURATION_FORMATTER.format(duration2)}${UNITS[unitIndex]}`; } const METHOD_COLOR_LUT = { GET: pc__default.green, POST: pc__default.blue, PUT: pc__default.yellow, DELETE: pc__default.red, PATCH: pc__default.magenta, OPTIONS: pc__default.cyan, HEAD: pc__default.gray }; function method(method2) { const colorer = METHOD_COLOR_LUT[method2.toUpperCase()]; if (colorer) { return colorer(method2); } else { return method2; } } const STATUS_COLOR_LUT = { 200: pc__default.green, 201: pc__default.blue, 204: pc__default.yellow, 400: pc__default.red, 401: pc__default.magenta, 403: pc__default.cyan, 404: pc__default.gray, 500: pc__default.gray }; function status(status2) { if (status2 === void 0) { return ""; } const colorer = STATUS_COLOR_LUT[+status2]; if (colorer) { return colorer(status2); } else { return status2.toString(); } } const REQUEST_START_TIME_KEY = "@tqman/nice-logger/request-start-time"; const logger = (options = {}) => { const { enabled = process.env.NODE_ENV !== "production", mode = "combined" } = options; const app = new elysia.Elysia({ name: "@tqman/nice-logger", seed: options }); if (!enabled) return app; const getTimestamp = typeof options.withTimestamp === "function" ? options.withTimestamp : () => (/* @__PURE__ */ new Date()).toLocaleString(); app.onStart((ctx) => { if (!options.withBanner) { return; } if (typeof options.withBanner === "function") { options.withBanner(); return; } const ELYSIA_VERSION = undefined("elysia/package.json").version; console.log(`\u{1F98A} ${pc__default.green(`${pc__default.bold("Elysia")} v${ELYSIA_VERSION}`)}`); if (typeof options.withBanner === "object") { Object.entries(options.withBanner).forEach(([key, value]) => { const v = typeof value === "function" ? value(ctx) : value; if (v) { console.log(`${pc__default.green(" \u279C ")} ${pc__default.bold(key)}: ${pc__default.cyan(v)}`); } }); console.log(); return; } console.log( `${pc__default.green(" \u279C ")} ${pc__default.bold("Server")}: ${pc__default.cyan(String(ctx.server?.url))} ` ); }).onRequest((ctx) => { ctx.store = { ...ctx.store, [REQUEST_START_TIME_KEY]: process.hrtime.bigint() }; if (mode === "live") { const url = new URL(ctx.request.url); const components = [ options.withTimestamp ? pc__default.dim(`[${getTimestamp()}]`) : "", pc__default.blue("--->"), pc__default.bold(method(ctx.request.method)), url.pathname ]; console.log(components.join(" ")); } }).onAfterResponse(({ request, set, response, store }) => { if (response instanceof Error) { return; } const url = new URL(request.url); const duration$1 = Number( process.hrtime.bigint() - store[REQUEST_START_TIME_KEY] ) / 1e3; const sign = mode === "combined" ? pc__default.green("\u2713") : pc__default.green("<---"); const components = [ options.withTimestamp ? pc__default.dim(`[${getTimestamp()}]`) : "", sign, pc__default.bold(method(request.method)), url.pathname, status(set.status), pc__default.dim(`[${duration(duration$1)}]`) ]; console.log(components.join(" ")); }).onError(({ request, error, store }) => { const url = new URL(request.url); const duration$1 = store[REQUEST_START_TIME_KEY] ? Number( process.hrtime.bigint() - store[REQUEST_START_TIME_KEY] ) / 1e3 : null; const status$1 = "status" in error ? error.status : 500; const sign = mode === "combined" ? pc__default.red("\u2717") : pc__default.red("<-x-"); const components = [ options.withTimestamp ? pc__default.dim(`[${getTimestamp()}]`) : "", sign, pc__default.bold(method(request.method)), url.pathname, status(status$1), pc__default.dim(`[${duration(duration$1)}]`) ]; console.log(components.join(" ")); }); return app.as("plugin"); }; exports.logger = logger;