123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- import { _optionalChain } from '@sentry/utils';
- import { readFile } from 'fs';
- import { defineIntegration, convertIntegrationFnToClass } from '@sentry/core';
- import { LRUMap, addContextToFrame } from '@sentry/utils';
- const FILE_CONTENT_CACHE = new LRUMap(100);
- const DEFAULT_LINES_OF_CONTEXT = 7;
- const INTEGRATION_NAME = 'ContextLines';
- function readTextFileAsync(path) {
- return new Promise((resolve, reject) => {
- readFile(path, 'utf8', (err, data) => {
- if (err) reject(err);
- else resolve(data);
- });
- });
- }
- const _contextLinesIntegration = ((options = {}) => {
- const contextLines = options.frameContextLines !== undefined ? options.frameContextLines : DEFAULT_LINES_OF_CONTEXT;
- return {
- name: INTEGRATION_NAME,
-
- setupOnce() {},
- processEvent(event) {
- return addSourceContext(event, contextLines);
- },
- };
- }) ;
- const contextLinesIntegration = defineIntegration(_contextLinesIntegration);
- const ContextLines = convertIntegrationFnToClass(INTEGRATION_NAME, contextLinesIntegration)
- ;
- async function addSourceContext(event, contextLines) {
-
-
- const enqueuedReadSourceFileTasks = {};
- const readSourceFileTasks = [];
- if (contextLines > 0 && _optionalChain([event, 'access', _2 => _2.exception, 'optionalAccess', _3 => _3.values])) {
- for (const exception of event.exception.values) {
- if (!_optionalChain([exception, 'access', _4 => _4.stacktrace, 'optionalAccess', _5 => _5.frames])) {
- continue;
- }
-
-
- for (let i = exception.stacktrace.frames.length - 1; i >= 0; i--) {
- const frame = exception.stacktrace.frames[i];
-
-
- if (frame.filename && !enqueuedReadSourceFileTasks[frame.filename] && !FILE_CONTENT_CACHE.get(frame.filename)) {
- readSourceFileTasks.push(_readSourceFile(frame.filename));
- enqueuedReadSourceFileTasks[frame.filename] = 1;
- }
- }
- }
- }
-
-
-
- if (readSourceFileTasks.length > 0) {
- await Promise.all(readSourceFileTasks);
- }
-
-
- if (contextLines > 0 && _optionalChain([event, 'access', _6 => _6.exception, 'optionalAccess', _7 => _7.values])) {
- for (const exception of event.exception.values) {
- if (exception.stacktrace && exception.stacktrace.frames) {
- await addSourceContextToFrames(exception.stacktrace.frames, contextLines);
- }
- }
- }
- return event;
- }
- function addSourceContextToFrames(frames, contextLines) {
- for (const frame of frames) {
-
- if (frame.filename && frame.context_line === undefined) {
- const sourceFileLines = FILE_CONTENT_CACHE.get(frame.filename);
- if (sourceFileLines) {
- try {
- addContextToFrame(sourceFileLines, frame, contextLines);
- } catch (e) {
-
-
- }
- }
- }
- }
- }
- async function _readSourceFile(filename) {
- const cachedFile = FILE_CONTENT_CACHE.get(filename);
-
- if (cachedFile === null) {
- return null;
- }
-
- if (cachedFile !== undefined) {
- return cachedFile;
- }
-
-
-
-
- let content = null;
- try {
- const rawFileContents = await readTextFileAsync(filename);
- content = rawFileContents.split('\n');
- } catch (_) {
-
- }
- FILE_CONTENT_CACHE.set(filename, content);
- return content;
- }
- export { ContextLines, contextLinesIntegration };
|