import type { Event, IntegrationFn, StackFrame } from '@sentry/core'; import { addContextToFrame, defineIntegration, LRUMap, stripUrlQueryAndFragment } from '@sentry/core'; const INTEGRATION_NAME = 'ContextLines' as const; // Frame filenames come from the parsed `error.stack` string, which application // code can overwrite with external input, so only read source files. const SOURCE_FILE_EXTENSION = /\.(?:[cm]?[jt]sx?|vue|svelte|astro|mdx)$/i; const FILE_CONTENT_CACHE = new LRUMap(100); const DEFAULT_LINES_OF_CONTEXT = 5; /** * Resets the file cache. Exists for testing purposes. * @hidden */ export function resetFileContentCache(): void { FILE_CONTENT_CACHE.clear(); } /** * Reads file contents and caches them in a global LRU cache. * * @param filename filepath to read content from. */ async function readSourceFile(filename: string): Promise { const cachedFile = FILE_CONTENT_CACHE.get(filename); // We have a cache hit if (cachedFile !== undefined) { return cachedFile; } let content: string | null = null; try { content = await Deno.readTextFile(filename); } catch { // } FILE_CONTENT_CACHE.set(filename, content); return content; } interface ContextLinesOptions { /** * Sets the number of context lines for each frame when loading a file. * Defaults to `dataCollection.frameContextLines` (5). * * Set to 0 to disable loading and inclusion of source files. * * When set, this option takes precedence over `dataCollection.frameContextLines`. **/ frameContextLines?: number; } const _contextLinesIntegration = ((options: ContextLinesOptions = {}) => { return { name: INTEGRATION_NAME, processEvent(event, _hint, client) { const contextLines = options.frameContextLines ?? client?.getDataCollectionOptions().frameContextLines ?? DEFAULT_LINES_OF_CONTEXT; return addSourceContext(event, contextLines); }, }; }) satisfies IntegrationFn; /** * Adds source context to event stacktraces. * * Enabled by default in the Deno SDK. * * ```js * Sentry.init({ * integrations: [ * Sentry.contextLinesIntegration(), * ], * }) * ``` */ export const contextLinesIntegration = defineIntegration(_contextLinesIntegration); /** Processes an event and adds context lines */ async function addSourceContext(event: Event, contextLines: number): Promise { if (contextLines > 0 && event.exception?.values) { for (const exception of event.exception.values) { if (exception.stacktrace?.frames) { await addSourceContextToFrames(exception.stacktrace.frames, contextLines); } } } return event; } /** Adds context lines to frames */ async function addSourceContextToFrames(frames: StackFrame[], contextLines: number): Promise { for (const frame of frames) { // Only add context if we have a filename and it hasn't already been added if ( frame.filename && frame.in_app && frame.context_line === undefined && SOURCE_FILE_EXTENSION.test(stripUrlQueryAndFragment(frame.filename)) ) { const permission = await Deno.permissions.query({ name: 'read', path: frame.filename, }); if (permission.state == 'granted') { const sourceFile = await readSourceFile(frame.filename); if (sourceFile) { try { const lines = sourceFile.split('\n'); // A trailing newline ends the last line, it does not start another one. if (lines[lines.length - 1] === '') { lines.pop(); } addContextToFrame(lines, frame, contextLines); } catch { // anomaly, being defensive in case // unlikely to ever happen in practice but can definitely happen in theory } } } } } }