@@ -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(v7): 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(v7): 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(v7): 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 ;
@@ -133,8 +150,8 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
133
150
let eventId : string | undefined = hint && hint . event_id ;
134
151
135
152
const promisedEvent = isPrimitive ( message )
136
- ? this . _getBackend ( ) . eventFromMessage ( String ( message ) , level , hint )
137
- : this . _getBackend ( ) . eventFromException ( message , hint ) ;
153
+ ? this . eventFromMessage ( String ( message ) , level , hint )
154
+ : this . eventFromException ( message , hint ) ;
138
155
139
156
this . _process (
140
157
promisedEvent
@@ -204,7 +221,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
204
221
* @inheritDoc
205
222
*/
206
223
public getTransport ( ) : Transport {
207
- return this . _getBackend ( ) . getTransport ( ) ;
224
+ return this . _transport ;
208
225
}
209
226
210
227
/**
@@ -249,6 +266,57 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
249
266
}
250
267
}
251
268
269
+ /**
270
+ * @inheritDoc
271
+ */
272
+ public sendEvent ( event : Event ) : void {
273
+ // TODO(v7): Remove the if-else
274
+ if (
275
+ this . _newTransport &&
276
+ this . _options . dsn &&
277
+ this . _options . _experiments &&
278
+ this . _options . _experiments . newTransport
279
+ ) {
280
+ const api = initAPIDetails ( this . _options . dsn , this . _options . _metadata , this . _options . tunnel ) ;
281
+ const env = createEventEnvelope ( event , api ) ;
282
+ void this . _newTransport . send ( env ) . then ( null , reason => {
283
+ IS_DEBUG_BUILD && logger . error ( 'Error while sending event:' , reason ) ;
284
+ } ) ;
285
+ } else {
286
+ void this . _transport . sendEvent ( event ) . then ( null , reason => {
287
+ IS_DEBUG_BUILD && logger . error ( 'Error while sending event:' , reason ) ;
288
+ } ) ;
289
+ }
290
+ }
291
+
292
+ /**
293
+ * @inheritDoc
294
+ */
295
+ public sendSession ( session : Session ) : void {
296
+ if ( ! this . _transport . sendSession ) {
297
+ IS_DEBUG_BUILD && logger . warn ( "Dropping session because custom transport doesn't implement sendSession" ) ;
298
+ return ;
299
+ }
300
+
301
+ // TODO(v7): Remove the if-else
302
+ if (
303
+ this . _newTransport &&
304
+ this . _options . dsn &&
305
+ this . _options . _experiments &&
306
+ this . _options . _experiments . newTransport
307
+ ) {
308
+ const api = initAPIDetails ( this . _options . dsn , this . _options . _metadata , this . _options . tunnel ) ;
309
+ const [ env ] = createSessionEnvelope ( session , api ) ;
310
+ void this . _newTransport . send ( env ) . then ( null , reason => {
311
+ IS_DEBUG_BUILD && logger . error ( 'Error while sending session:' , reason ) ;
312
+ } ) ;
313
+ } else {
314
+ void this . _transport . sendSession ( session ) . then ( null , reason => {
315
+ IS_DEBUG_BUILD && logger . error ( 'Error while sending session:' , reason ) ;
316
+ } ) ;
317
+ }
318
+ }
319
+
252
320
/** Updates existing session based on the provided event */
253
321
protected _updateSessionFromEvent ( session : Session , event : Event ) : void {
254
322
let crashed = false ;
@@ -283,8 +351,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
283
351
}
284
352
285
353
/** Deliver captured session to Sentry */
354
+ // TODO(v7): should this be deleted?
286
355
protected _sendSession ( session : Session ) : void {
287
- this . _getBackend ( ) . sendSession ( session ) ;
356
+ this . sendSession ( session ) ;
288
357
}
289
358
290
359
/**
@@ -317,7 +386,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
317
386
} ) ;
318
387
}
319
388
320
- /** Returns the current backend. */
389
+ /** Returns the current backend.
390
+ * TODO(v7): DELETE
391
+ */
321
392
protected _getBackend ( ) : B {
322
393
return this . _backend ;
323
394
}
@@ -490,8 +561,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
490
561
* Tells the backend to send this event
491
562
* @param event The Sentry event to send
492
563
*/
564
+ // TODO(v7): refactor: get rid of method?
493
565
protected _sendEvent ( event : Event ) : void {
494
- this . _getBackend ( ) . sendEvent ( event ) ;
566
+ this . sendEvent ( event ) ;
495
567
}
496
568
497
569
/**
@@ -618,6 +690,24 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
618
690
} ,
619
691
) ;
620
692
}
693
+
694
+ /**
695
+ * Sets up the transport so it can be used later to send requests.
696
+ */
697
+ protected _setupTransport ( ) : Transport {
698
+ return new NoopTransport ( ) ;
699
+ }
700
+
701
+ /**
702
+ * @inheritDoc
703
+ */
704
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
705
+ public abstract eventFromException ( _exception : any , _hint ?: EventHint ) : PromiseLike < Event > ;
706
+
707
+ /**
708
+ * @inheritDoc
709
+ */
710
+ public abstract eventFromMessage ( _message : string , _level ?: Severity , _hint ?: EventHint ) : PromiseLike < Event > ;
621
711
}
622
712
623
713
/**
0 commit comments