rittenhop-dev/versions/5.94.2/node_modules/@sentry/node/cjs/integrations/contextlines.js.map

1 line
25 KiB
Plaintext
Raw Normal View History

2024-09-23 19:40:12 -04:00
{"version":3,"file":"contextlines.js","sources":["../../../src/integrations/contextlines.ts"],"sourcesContent":["import { readFile } from 'fs';\nimport { convertIntegrationFnToClass, defineIntegration } from '@sentry/core';\nimport type { Event, Integration, IntegrationClass, IntegrationFn, StackFrame } from '@sentry/types';\nimport { LRUMap, addContextToFrame } from '@sentry/utils';\n\nconst FILE_CONTENT_CACHE = new LRUMap<string, string[] | null>(100);\nconst DEFAULT_LINES_OF_CONTEXT = 7;\nconst INTEGRATION_NAME = 'ContextLines';\n\n// TODO: Replace with promisify when minimum supported node >= v8\nfunction readTextFileAsync(path: string): Promise<string> {\n return new Promise((resolve, reject) => {\n readFile(path, 'utf8', (err, data) => {\n if (err) reject(err);\n else resolve(data);\n });\n });\n}\n\n/**\n * Resets the file cache. Exists for testing purposes.\n * @hidden\n */\nexport function resetFileContentCache(): void {\n FILE_CONTENT_CACHE.clear();\n}\n\ninterface ContextLinesOptions {\n /**\n * Sets the number of context lines for each frame when loading a file.\n * Defaults to 7.\n *\n * Set to 0 to disable loading and inclusion of source files.\n **/\n frameContextLines?: number;\n}\n\nconst _contextLinesIntegration = ((options: ContextLinesOptions = {}) => {\n const contextLines = options.frameContextLines !== undefined ? options.frameContextLines : DEFAULT_LINES_OF_CONTEXT;\n\n return {\n name: INTEGRATION_NAME,\n // TODO v8: Remove this\n setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function\n processEvent(event) {\n return addSourceContext(event, contextLines);\n },\n };\n}) satisfies IntegrationFn;\n\nexport const contextLinesIntegration = defineIntegration(_contextLinesIntegration);\n\n/**\n * Add node modules / packages to the event.\n * @deprecated Use `contextLinesIntegration()` instead.\n */\n// eslint-disable-next-line deprecation/deprecation\nexport const ContextLines = convertIntegrationFnToClass(INTEGRATION_NAME, contextLinesIntegration) as IntegrationClass<\n Integration & { processEvent: (event: Event) => Promise<Event> }\n> & { new (options?: { frameContextLines?: number }): Integration };\n\nasync function addSourceContext(event: Event, contextLines: number): Promise<Event> {\n // keep a lookup map of which files we've already enqueued to read,\n // so we don't enqueue the same file multiple times which would cause multiple i/o reads\n const enqueuedReadSourceFileTasks: Record<string, number> = {};\n const readSourceFileTasks: Promise<string[] | null>[] = [];\n\n if (contextLines > 0 && event.exception?.values) {\n for (const exception of event.exception.values) {\n if (!exception.stacktrace?.frames) {\n continue;\n }\n\n // We want to iterate in reverse order as calling cache.get will bump the file in our LRU cache.\n // This ends up prioritizes source context for frames at the top of the stack instead of the bottom.\n for (let i = exception.stacktrace.frames.length - 1; i >= 0; i--) {\n const frame = exception.stacktrace.frames[i];\n // Call cache.get to bump the file to the top of the cache and ensure we have not already\n // enqueued a read operation for this filename\n if (frame.filename && !enqueuedReadSourceFileTasks[frame.filename] && !FILE_CONTENT_CACHE.get(frame.filename)) {\n readSourceFileTasks.push(_readSourceFile(frame.filename));\n enqueuedReadSourceFileTasks[frame.filename] = 1;\n }\n }\n }\n }\n\n // check if files to read > 0, if so, await all of them to be read before adding source contexts.\n // Normally, Promise.all here could be short circuited if one of the promises rejects, but we\n // are guarding from that by wrapping the i/o read operation in a try/catch.\n if (readSourceFileTasks.length > 0) {\n await Promise.all(readSourceFileTasks);\n }\n\n // Perform the same loop as above, but this time we can assume all files are in the cache\n // and attempt to add source context to fra