/** * Stack frames, and which of them belong to the application. * * A query or an exception is only useful next to the line of the app that * caused it, so frames are split into the app's and everything else's: the * framework, dependencies, the runtime. */ import type { StackFrame } from './types' import { readFile } from 'node:fs/promises' import { relative, sep } from 'node:path' import process from 'node:process' let projectRoot: string | undefined export function setProjectRoot(root: string): void { projectRoot = root.endsWith(sep) ? root.slice(0, -1) : root } function root(): string { return projectRoot ??= process.cwd() } const FRAMEWORK_SEGMENTS = [`${sep}node_modules${sep}`, `${sep}storage${sep}framework${sep}`, `${sep}pantry${sep}`] /** A file inside the project that is not the framework or a dependency. */ export function isAppFile(file: string): boolean { const base = root() if (!file.startsWith(base + sep)) return false const inside = file.slice(base.length) return !FRAMEWORK_SEGMENTS.some(segment => inside.includes(segment)) } export function displayPath(file: string): string { const base = root() return file.startsWith(base + sep) ? relative(base, file) : file } const FRAME = /^\s*at\s+(?:async\s+)?(?:(.+?)\s+\()?((?:\/|[A-Za-z]:\\|file:\/\/)[^)]*?):(\d+)(?::(\d+))?\)?\s*$/ export function parseStack(stack: string | undefined, limit: number = 30): StackFrame[] { if (!stack) return [] const frames: StackFrame[] = [] for (const line of stack.split('\n')) { const match = FRAME.exec(line) if (!match) continue const file = match[2]!.replace(/^file:\/\//, '') frames.push({ function: match[1] && match[1] !== '' ? match[1] : undefined, file: displayPath(file), line: Number(match[3]), column: match[4] ? Number(match[4]) : undefined, app: isAppFile(file), }) if (frames.length >= limit) break } return frames } /** * `app/Actions/CarIndexAction.ts:14` for the first application frame of the * current stack, or undefined when the caller is not application code. */ export function callerSource(): string | undefined { const holder: { stack?: string } = {} const previous = Error.stackTraceLimit // The default of ten frames is usually all query builder. Error.stackTraceLimit = 40 try { Error.captureStackTrace(holder, callerSource) } finally { Error.stackTraceLimit = previous } if (!holder.stack) return undefined for (const line of holder.stack.split('\n')) { const match = FRAME.exec(line) if (!match) continue const file = match[2]!.replace(/^file:\/\//, '') if (isAppFile(file)) return `${displayPath(file)}:${match[3]}` } return undefined } /** The source lines around `line` of a project file, for an exception. */ export async function sourceExcerpt(file: string, line: number, radius: number = 5): Promise<{ file: string, line: number, start: number, lines: string[] } | undefined> { const absolute = file.startsWith(sep) ? file : `${root()}${sep}${file}` // Only ever the project's own files: the path came out of a stack trace. if (!absolute.startsWith(root() + sep) || absolute.includes(`${sep}..${sep}`)) return undefined try { const lines = (await readFile(absolute, 'utf8')).split('\n') if (line < 1 || line > lines.length) return undefined const start = Math.max(1, line - radius) const end = Math.min(lines.length, line + radius) return { file: displayPath(absolute), line, start, lines: lines.slice(start - 1, end).map(text => text.length > 400 ? `${text.slice(0, 400)}...` : text), } } catch { return undefined } }