@@ -28,15 +28,21 @@ import {
28
28
uuid4 ,
29
29
} from '@sentry/utils' ;
30
30
31
+ import { initAPIDetails } from './api' ;
31
32
import { Backend , BackendClass } from './basebackend' ;
32
33
import { IS_DEBUG_BUILD } from './flags' ;
33
34
import { IntegrationIndex , setupIntegrations } from './integration' ;
35
+ import { createEventEnvelope , createSessionEnvelope } from './request' ;
36
+ import { NewTransport } from './transports/base' ;
37
+ import { NoopTransport } from './transports/noop' ;
34
38
35
39
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured." ;
36
40
37
41
/**
38
42
* Base implementation for all JavaScript SDK clients.
39
43
*
44
+ * TODO: refactor doc w.r.t. Backend
45
+ *
40
46
* Call the constructor with the corresponding backend constructor and options
41
47
* specific to the client subclass. To access these options later, use
42
48
* {@link Client.getOptions}. Also, the Backend instance is available via
@@ -71,6 +77,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
71
77
* The backend used to physically interact in the environment. Usually, this
72
78
* will correspond to the client. When composing SDKs, however, the Backend
73
79
* from the root SDK will be used.
80
+ * TODO: DELETE
74
81
*/
75
82
protected readonly _backend : B ;
76
83
@@ -86,19 +93,30 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
86
93
/** Number of calls being processed */
87
94
protected _numProcessing : number = 0 ;
88
95
96
+ /** Cached transport used internally. */
97
+ protected _transport : Transport ;
98
+
99
+ /** New v7 Transport that is initialized alongside the old one */
100
+ protected _newTransport ?: NewTransport ;
101
+
89
102
/**
90
103
* Initializes this client instance.
91
104
*
92
105
* @param backendClass A constructor function to create the backend.
93
106
* @param options Options for the client.
94
107
*/
95
108
protected constructor ( backendClass : BackendClass < B , O > , options : O ) {
109
+ // TODO: Delete
96
110
this . _backend = new backendClass ( options ) ;
97
111
this . _options = options ;
98
112
99
113
if ( options . dsn ) {
100
114
this . _dsn = makeDsn ( options . dsn ) ;
115
+ } else {
116
+ IS_DEBUG_BUILD && logger . warn ( 'No DSN provided, client will not do anything.' ) ;
101
117
}
118
+
119
+ this . _transport = this . _setupTransport ( ) ;
102
120
}
103
121
104
122
/**
@@ -115,8 +133,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
115
133
let eventId : string | undefined = hint && hint . event_id ;
116
134
117
135
this . _process (
118
- this . _getBackend ( )
119
- . eventFromException ( exception , hint )
136
+ this . eventFromException ( exception , hint )
120
137
. then ( event => this . _captureEvent ( event , hint , scope ) )
121
138
. then ( result => {
122
139
eventId = result ;
@@ -132,9 +149,10 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
132
149
public captureMessage ( message : string , level ?: Severity , hint ?: EventHint , scope ?: Scope ) : string | undefined {
133
150
let eventId : string | undefined = hint && hint . event_id ;
134
151
152
+ // TODO: refactor
135
153
const promisedEvent = isPrimitive ( message )
136
- ? this . _getBackend ( ) . eventFromMessage ( String ( message ) , level , hint )
137
- : this . _getBackend ( ) . eventFromException ( message , hint ) ;
154
+ ? this . eventFromMessage ( String ( message ) , level , hint )
155
+ : this . eventFromException ( message , hint ) ;
138
156
139
157
this . _process (
140
158
promisedEvent
@@ -204,7 +222,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
204
222
* @inheritDoc
205
223
*/
206
224
public getTransport ( ) : Transport {
207
- return this . _getBackend ( ) . getTransport ( ) ;
225
+ return this . _transport ;
208
226
}
209
227
210
228
/**
@@ -249,6 +267,72 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
249
267
}
250
268
}
251
269
270
+ /**
271
+ * @inheritDoc
272
+ */
273
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
274
+ public eventFromException ( _exception : any , _hint ?: EventHint ) : PromiseLike < Event > {
275
+ throw new SentryError ( 'Client has to implement `eventFromException` method' ) ;
276
+ }
277
+
278
+ /**
279
+ * @inheritDoc
280
+ */
281
+ public eventFromMessage ( _message : string , _level ?: Severity , _hint ?: EventHint ) : PromiseLike < Event > {
282
+ throw new SentryError ( 'Client has to implement `eventFromMessage` method' ) ;
283
+ }
284
+
285
+ /**
286
+ * @inheritDoc
287
+ */
288
+ public sendEvent ( event : Event ) : void {
289
+ // TODO(v7): Remove the if-else
290
+ if (
291
+ this . _newTransport &&
292
+ this . _options . dsn &&
293
+ this . _options . _experiments &&
294
+ this . _options . _experiments . newTransport
295
+ ) {
296
+ const api = initAPIDetails ( this . _options . dsn , this . _options . _metadata , this . _options . tunnel ) ;
297
+ const env = createEventEnvelope ( event , api ) ;
298
+ void this . _newTransport . send ( env ) . then ( null , reason => {
299
+ IS_DEBUG_BUILD && logger . error ( 'Error while sending event:' , reason ) ;
300
+ } ) ;
301
+ } else {
302
+ void this . _transport . sendEvent ( event ) . then ( null , reason => {
303
+ IS_DEBUG_BUILD && logger . error ( 'Error while sending event:' , reason ) ;
304
+ } ) ;
305
+ }
306
+ }
307
+
308
+ /**
309
+ * @inheritDoc
310
+ */
311
+ public sendSession ( session : Session ) : void {
312
+ if ( ! this . _transport . sendSession ) {
313
+ IS_DEBUG_BUILD && logger . warn ( "Dropping session because custom transport doesn't implement sendSession" ) ;
314
+ return ;
315
+ }
316
+
317
+ // TODO(v7): Remove the if-else
318
+ if (
319
+ this . _newTransport &&
320
+ this . _options . dsn &&
321
+ this . _options . _experiments &&
322
+ this . _options . _experiments . newTransport
323
+ ) {
324
+ const api = initAPIDetails ( this . _options . dsn , this . _options . _metadata , this . _options . tunnel ) ;
325
+ const [ env ] = createSessionEnvelope ( session , api ) ;
326
+ void this . _newTransport . send ( env ) . then ( null , reason => {
327
+ IS_DEBUG_BUILD && logger . error ( 'Error while sending session:' , reason ) ;
328
+ } ) ;
329
+ } else {
330
+ void this . _transport . sendSession ( session ) . then ( null , reason => {
331
+ IS_DEBUG_BUILD && logger . error ( 'Error while sending session:' , reason ) ;
332
+ } ) ;
333
+ }
334
+ }
335
+
252
336
/** Updates existing session based on the provided event */
253
337
protected _updateSessionFromEvent ( session : Session , event : Event ) : void {
254
338
let crashed = false ;
@@ -284,7 +368,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
284
368
285
369
/** Deliver captured session to Sentry */
286
370
protected _sendSession ( session : Session ) : void {
287
- this . _getBackend ( ) . sendSession ( session ) ;
371
+ this . sendSession ( session ) ;
288
372
}
289
373
290
374
/**
@@ -317,7 +401,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
317
401
} ) ;
318
402
}
319
403
320
- /** Returns the current backend. */
404
+ /** Returns the current backend.
405
+ * TODO: DELETE
406
+ */
321
407
protected _getBackend ( ) : B {
322
408
return this . _backend ;
323
409
}
@@ -490,8 +576,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
490
576
* Tells the backend to send this event
491
577
* @param event The Sentry event to send
492
578
*/
579
+ // TODO: refactor: get rid of method?
493
580
protected _sendEvent ( event : Event ) : void {
494
- this . _getBackend ( ) . sendEvent ( event ) ;
581
+ this . sendEvent ( event ) ;
495
582
}
496
583
497
584
/**
@@ -582,7 +669,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
582
669
this . _updateSessionFromEvent ( session , processedEvent ) ;
583
670
}
584
671
585
- this . _sendEvent ( processedEvent ) ;
672
+ this . sendEvent ( processedEvent ) ;
586
673
return processedEvent ;
587
674
} )
588
675
. then ( null , reason => {
@@ -618,6 +705,13 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
618
705
} ,
619
706
) ;
620
707
}
708
+
709
+ /**
710
+ * Sets up the transport so it can be used later to send requests.
711
+ */
712
+ protected _setupTransport ( ) : Transport {
713
+ return new NoopTransport ( ) ;
714
+ }
621
715
}
622
716
623
717
/**
0 commit comments