import { styleText } from "node:util"; import stringWidth from "string-width"; import type { HTTPMethod, Route, UseOptions } from "./types"; const colorizeMethod = (method: string): string => { const color = ( { HEAD: "gray", GET: "green", POST: "blue", PATCH: "blue", PUT: "blue", DELETE: "red", } as const )[method]; return color ? styleText(color, method) : method; }; /** * `middleware` and `handler` are absent for a route that is not imported yet: * what it runs is known only once its first request loads it. * */ export const debugRouteEntry = (entry: { name: string; path: string; file: string; method: string; middleware?: Array<{ middleware: Array; options?: UseOptions | undefined; }>; handler?: { kind: "handler"; middleware: Array; method: HTTPMethod; }; }): Route["debug"] => { const { path, file } = entry; const methodLines = [entry.method].flatMap((method) => { const coloredMethod = colorizeMethod(method); return method === "GET" ? [coloredMethod + styleText("gray", "|HEAD")] : [coloredMethod]; }); const pending = styleText("dim", "loaded on first request"); const middlewareLines = entry.middleware ?.map(({ options, middleware }) => { const lines: Array = []; if (options?.slot) { lines.push( `${styleText("dim", "slot:")} ${styleText("blue", options.slot)}`, ); } lines.push( styleText("dim", middleware.map(funcName as never).join("; ")), ); return lines.join(" "); }) .join(`\n${Array(12).fill(" ").join("")}`); const handlerLines = entry.handler?.middleware.map((fn) => { return styleText("yellow", funcName(fn as Function)); }); const headline = `${styleText("bgBlue", styleText("black", ` ${path} `))} ${styleText("gray", `[ ${file} ]`)}`; const methods = `${styleText("dim", " methods:")} ${methodLines.join(" ")}`; const middleware = `${styleText("dim", "middleware:")} ${middlewareLines ?? pending}`; const handlerText = handlerLines?.join(Array(7).fill(" ").join("")); const handler = `${styleText("dim", " handler:")} ${handlerText ?? pending}`; const maxColumns = process.stdout.isTTY ? Number(process.stdout.columns || 80) : 80; const debugEntries = [ ["headline", headline], ["methods", methods], ["middleware", middleware], ["handler", handler], ] as const; const lineMapper = (line: string) => { const freeColumns = maxColumns - stringWidth(line); return freeColumns > 0 ? [ line, styleText( "dim", styleText("gray", Array(freeColumns).fill("ยท").join("")), ), ].join("") : line; }; const debug = debugEntries.reduce( (map, [key, line]) => { map[key] = line.split("\n").map(lineMapper).join("\n"); return map; }, {} as Route["debug"], ); return { ...debug, full: Object.values(debug).join("\n"), }; }; const funcName = (fn: Function) => { return fn.name || fn.toString().split("\n")[0].slice(0, 30); };