@@ -8,8 +8,7 @@ import type {
8
8
StackFrame ,
9
9
StackParser ,
10
10
} from '@sentry/types' ;
11
- import type { Debugger , InspectorNotification , Runtime } from 'inspector' ;
12
- import { Session } from 'inspector' ;
11
+ import type { Debugger , InspectorNotification , Runtime , Session } from 'inspector' ;
13
12
import { LRUMap } from 'lru_map' ;
14
13
15
14
export interface DebugSession {
@@ -28,14 +27,24 @@ export interface DebugSession {
28
27
* https://nodejs.org/docs/latest-v19.x/api/inspector.html#promises-api
29
28
* https://nodejs.org/docs/latest-v14.x/api/inspector.html
30
29
*/
31
- class AsyncSession extends Session implements DebugSession {
30
+ class AsyncSession implements DebugSession {
31
+ private readonly _session : Session ;
32
+
33
+ /** Throws is inspector API is not available */
34
+ public constructor ( ) {
35
+ // Node can be build without inspector support so this can throw
36
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
37
+ const { Session } = require ( 'inspector' ) ;
38
+ this . _session = new Session ( ) ;
39
+ }
40
+
32
41
/** @inheritdoc */
33
42
public configureAndConnect ( onPause : ( message : InspectorNotification < Debugger . PausedEventDataType > ) => void ) : void {
34
- this . connect ( ) ;
35
- this . on ( 'Debugger.paused' , onPause ) ;
36
- this . post ( 'Debugger.enable' ) ;
43
+ this . _session . connect ( ) ;
44
+ this . _session . on ( 'Debugger.paused' , onPause ) ;
45
+ this . _session . post ( 'Debugger.enable' ) ;
37
46
// We only want to pause on uncaught exceptions
38
- this . post ( 'Debugger.setPauseOnExceptions' , { state : 'uncaught' } ) ;
47
+ this . _session . post ( 'Debugger.setPauseOnExceptions' , { state : 'uncaught' } ) ;
39
48
}
40
49
41
50
/** @inheritdoc */
@@ -61,7 +70,7 @@ class AsyncSession extends Session implements DebugSession {
61
70
*/
62
71
private _getProperties ( objectId : string ) : Promise < Runtime . PropertyDescriptor [ ] > {
63
72
return new Promise ( ( resolve , reject ) => {
64
- this . post (
73
+ this . _session . post (
65
74
'Runtime.getProperties' ,
66
75
{
67
76
objectId,
@@ -103,6 +112,18 @@ class AsyncSession extends Session implements DebugSession {
103
112
}
104
113
}
105
114
115
+ /**
116
+ * When using Vercel pkg, the inspector module is not available.
117
+ * https://github.com/getsentry/sentry-javascript/issues/6769
118
+ */
119
+ function tryNewAsyncSession ( ) : AsyncSession | undefined {
120
+ try {
121
+ return new AsyncSession ( ) ;
122
+ } catch ( e ) {
123
+ return undefined ;
124
+ }
125
+ }
126
+
106
127
// Add types for the exception event data
107
128
type PausedExceptionEvent = Debugger . PausedEventDataType & {
108
129
data : {
@@ -162,7 +183,10 @@ export class LocalVariables implements Integration {
162
183
163
184
private readonly _cachedFrames : LRUMap < string , Promise < FrameVariables [ ] > > = new LRUMap ( 20 ) ;
164
185
165
- public constructor ( _options : Options = { } , private readonly _session : DebugSession = new AsyncSession ( ) ) { }
186
+ public constructor (
187
+ _options : Options = { } ,
188
+ private readonly _session : DebugSession | undefined = tryNewAsyncSession ( ) ,
189
+ ) { }
166
190
167
191
/**
168
192
* @inheritDoc
@@ -176,7 +200,7 @@ export class LocalVariables implements Integration {
176
200
addGlobalEventProcessor : ( callback : EventProcessor ) => void ,
177
201
clientOptions : ClientOptions | undefined ,
178
202
) : void {
179
- if ( clientOptions ?. _experiments ?. includeStackLocals ) {
203
+ if ( this . _session && clientOptions ?. _experiments ?. includeStackLocals ) {
180
204
this . _session . configureAndConnect ( ev =>
181
205
this . _handlePaused ( clientOptions . stackParser , ev as InspectorNotification < PausedExceptionEvent > ) ,
182
206
) ;
@@ -212,7 +236,7 @@ export class LocalVariables implements Integration {
212
236
return { function : fn } ;
213
237
}
214
238
215
- const vars = await this . _session . getLocalVariables ( localScope . object . objectId ) ;
239
+ const vars = await this . _session ? .getLocalVariables ( localScope . object . objectId ) ;
216
240
217
241
return { function : fn , vars } ;
218
242
} ) ;
0 commit comments