1
- import { parseSemver } from '@sentry/utils' ;
2
-
3
- const nodeVersion = parseSemver ( process . versions . node ) ;
4
-
5
- if ( ( nodeVersion . major || 0 ) < 14 ) {
6
- throw new Error ( 'The LocalVariables integration requires node.js >= v14' ) ;
7
- }
8
-
9
- import { Event , EventProcessor , Hub , Integration , StackFrame , StackParser } from '@sentry/types' ;
1
+ import { getCurrentHub } from '@sentry/core' ;
2
+ import { Event , EventProcessor , Integration , StackFrame , StackParser } from '@sentry/types' ;
10
3
import { Debugger , InspectorNotification , Runtime , Session } from 'inspector' ;
11
4
import { LRUMap } from 'lru_map' ;
12
5
6
+ import { NodeClient } from '../client' ;
7
+
13
8
/**
14
9
* Promise API is available as `Experimental` and in Node 19 only.
15
10
*
@@ -67,24 +62,6 @@ function hashFrames(frames: StackFrame[] | undefined): string | undefined {
67
62
return frames . reduce ( ( acc , frame ) => `${ acc } ,${ frame . function } ,${ frame . lineno } ,${ frame . colno } ` , '' ) ;
68
63
}
69
64
70
- type HashFromStackFn = ( stack : string | undefined ) => string | undefined ;
71
-
72
- /**
73
- * Creates a function used to hash stack strings
74
- *
75
- * We use the stack parser to create a unique hash from the exception stack trace
76
- * This is used to lookup vars when the exception passes through the event processor
77
- */
78
- function createHashFn ( stackParser : StackParser | undefined ) : HashFromStackFn {
79
- return ( stack : string | undefined ) => {
80
- if ( stackParser === undefined || stack === undefined ) {
81
- return undefined ;
82
- }
83
-
84
- return hashFrames ( stackParser ( stack , 1 ) ) ;
85
- } ;
86
- }
87
-
88
65
interface FrameVariables {
89
66
function : string ;
90
67
vars ?: Record < string , unknown > ;
@@ -100,29 +77,38 @@ export class LocalVariables implements Integration {
100
77
101
78
private readonly _session : AsyncSession = new AsyncSession ( ) ;
102
79
private readonly _cachedFrames : LRUMap < string , Promise < FrameVariables [ ] > > = new LRUMap ( 50 ) ;
103
-
104
- public constructor ( ) {
105
- this . _session . connect ( ) ;
106
- this . _session . on ( 'Debugger.paused' , this . _handlePaused . bind ( this ) ) ;
107
- this . _session . post ( 'Debugger.enable' ) ;
108
- // We only want to pause on uncaught exceptions
109
- this . _session . post ( 'Debugger.setPauseOnExceptions' , { state : 'uncaught' } ) ;
110
- }
80
+ private _stackParser : StackParser | undefined ;
111
81
112
82
/**
113
83
* @inheritDoc
114
84
*/
115
- public setupOnce ( addGlobalEventProcessor : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
116
- this . _stackHasher = createHashFn ( getCurrentHub ( ) . getClient ( ) ?. getOptions ( ) . stackParser ) ;
85
+ public setupOnce ( addGlobalEventProcessor : ( callback : EventProcessor ) => void ) : void {
86
+ const options = getCurrentHub ( ) . getClient < NodeClient > ( ) ?. getOptions ( ) ;
87
+
88
+ if ( options ?. includeStackLocals ) {
89
+ this . _stackParser = options . stackParser ;
117
90
118
- addGlobalEventProcessor ( async event => this . _addLocalVariables ( event ) ) ;
91
+ this . _session . connect ( ) ;
92
+ this . _session . on ( 'Debugger.paused' , this . _handlePaused . bind ( this ) ) ;
93
+ this . _session . post ( 'Debugger.enable' ) ;
94
+ // We only want to pause on uncaught exceptions
95
+ this . _session . post ( 'Debugger.setPauseOnExceptions' , { state : 'uncaught' } ) ;
96
+
97
+ addGlobalEventProcessor ( async event => this . _addLocalVariables ( event ) ) ;
98
+ }
119
99
}
120
100
121
101
/**
122
102
* We use the stack parser to create a unique hash from the exception stack trace
123
- * This is used to lookup vars when
103
+ * This is used to lookup vars when the exception passes through the event processor
124
104
*/
125
- private _stackHasher : HashFromStackFn = _ => undefined ;
105
+ private _hashFromStack ( stack : string | undefined ) : string | undefined {
106
+ if ( this . _stackParser === undefined || stack === undefined ) {
107
+ return undefined ;
108
+ }
109
+
110
+ return hashFrames ( this . _stackParser ( stack , 1 ) ) ;
111
+ }
126
112
127
113
/**
128
114
* Handle the pause event
@@ -135,7 +121,7 @@ export class LocalVariables implements Integration {
135
121
}
136
122
137
123
// data.description contains the original error.stack
138
- const exceptionHash = this . _stackHasher ( data ?. description ) ;
124
+ const exceptionHash = this . _hashFromStack ( data ?. description ) ;
139
125
140
126
if ( exceptionHash == undefined ) {
141
127
return ;
@@ -223,7 +209,7 @@ export class LocalVariables implements Integration {
223
209
const frameCount = event ?. exception ?. values ?. [ 0 ] ?. stacktrace ?. frames ?. length || 0 ;
224
210
225
211
for ( let i = 0 ; i < frameCount ; i ++ ) {
226
- // Sentry frames are already in reverse order
212
+ // Sentry frames are in reverse order
227
213
const frameIndex = frameCount - i - 1 ;
228
214
229
215
// Drop out if we run out of frames to match up
0 commit comments