1- import type { Event , Integration , StackFrame } from '@sentry/types' ;
1+ import { convertIntegrationFnToClass } from '@sentry/core' ;
2+ import type { Event , IntegrationFn , StackFrame } from '@sentry/types' ;
23import { GLOBAL_OBJ , addContextToFrame , stripUrlQueryAndFragment } from '@sentry/utils' ;
34
45const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window ;
56
67const DEFAULT_LINES_OF_CONTEXT = 7 ;
78
9+ const INTEGRATION_NAME = 'ContextLines' ;
10+
811interface ContextLinesOptions {
912 /**
1013 * Sets the number of context lines for each frame when loading a file.
@@ -15,6 +18,17 @@ interface ContextLinesOptions {
1518 frameContextLines ?: number ;
1619}
1720
21+ const contextLinesIntegration : IntegrationFn = ( options : ContextLinesOptions = { } ) => {
22+ const contextLines = options . frameContextLines != null ? options . frameContextLines : DEFAULT_LINES_OF_CONTEXT ;
23+
24+ return {
25+ name : INTEGRATION_NAME ,
26+ processEvent ( event ) {
27+ return addSourceContext ( event , contextLines ) ;
28+ } ,
29+ } ;
30+ } ;
31+
1832/**
1933 * Collects source context lines around the lines of stackframes pointing to JS embedded in
2034 * the current page's HTML.
@@ -26,73 +40,41 @@ interface ContextLinesOptions {
2640 * Use this integration if you have inline JS code in HTML pages that can't be accessed
2741 * by our backend (e.g. due to a login-protected page).
2842 */
29- export class ContextLines implements Integration {
30- /**
31- * @inheritDoc
32- */
33- public static id : string = 'ContextLines' ;
34-
35- /**
36- * @inheritDoc
37- */
38- public name : string ;
43+ // eslint-disable-next-line deprecation/deprecation
44+ export const ContextLines = convertIntegrationFnToClass ( INTEGRATION_NAME , contextLinesIntegration ) ;
3945
40- public constructor ( private readonly _options : ContextLinesOptions = { } ) {
41- this . name = ContextLines . id ;
46+ /**
47+ * Processes an event and adds context lines.
48+ */
49+ function addSourceContext ( event : Event , contextLines : number ) : Event {
50+ const doc = WINDOW . document ;
51+ const htmlFilename = WINDOW . location && stripUrlQueryAndFragment ( WINDOW . location . href ) ;
52+ if ( ! doc || ! htmlFilename ) {
53+ return event ;
4254 }
4355
44- /**
45- * @inheritDoc
46- */
47- public setupOnce ( _addGlobalEventProcessor : unknown , _getCurrentHub : unknown ) : void {
48- // noop
56+ const exceptions = event . exception && event . exception . values ;
57+ if ( ! exceptions || ! exceptions . length ) {
58+ return event ;
4959 }
5060
51- /** @inheritDoc */
52- public processEvent ( event : Event ) : Event {
53- return this . addSourceContext ( event ) ;
61+ const html = doc . documentElement . innerHTML ;
62+ if ( ! html ) {
63+ return event ;
5464 }
5565
56- /**
57- * Processes an event and adds context lines.
58- *
59- * TODO (v8): Make this internal/private
60- */
61- public addSourceContext ( event : Event ) : Event {
62- const doc = WINDOW . document ;
63- const htmlFilename = WINDOW . location && stripUrlQueryAndFragment ( WINDOW . location . href ) ;
64- if ( ! doc || ! htmlFilename ) {
65- return event ;
66- }
67-
68- const exceptions = event . exception && event . exception . values ;
69- if ( ! exceptions || ! exceptions . length ) {
70- return event ;
71- }
66+ const htmlLines = [ '<!DOCTYPE html>' , '<html>' , ...html . split ( '\n' ) , '</html>' ] ;
7267
73- const html = doc . documentElement . innerHTML ;
74- if ( ! html ) {
75- return event ;
68+ exceptions . forEach ( exception => {
69+ const stacktrace = exception . stacktrace ;
70+ if ( stacktrace && stacktrace . frames ) {
71+ stacktrace . frames = stacktrace . frames . map ( frame =>
72+ applySourceContextToFrame ( frame , htmlLines , htmlFilename , contextLines ) ,
73+ ) ;
7674 }
75+ } ) ;
7776
78- const htmlLines = [ '<!DOCTYPE html>' , '<html>' , ...html . split ( '\n' ) , '</html>' ] ;
79-
80- exceptions . forEach ( exception => {
81- const stacktrace = exception . stacktrace ;
82- if ( stacktrace && stacktrace . frames ) {
83- stacktrace . frames = stacktrace . frames . map ( frame =>
84- applySourceContextToFrame (
85- frame ,
86- htmlLines ,
87- htmlFilename ,
88- this . _options . frameContextLines != null ? this . _options . frameContextLines : DEFAULT_LINES_OF_CONTEXT ,
89- ) ,
90- ) ;
91- }
92- } ) ;
93-
94- return event ;
95- }
77+ return event ;
9678}
9779
9880/**
0 commit comments