@@ -28,15 +28,21 @@ import {
2828  uuid4 , 
2929}  from  '@sentry/utils' ; 
3030
31+ import  {  initAPIDetails  }  from  './api' ; 
3132import  {  Backend ,  BackendClass  }  from  './basebackend' ; 
3233import  {  IS_DEBUG_BUILD  }  from  './flags' ; 
3334import  {  IntegrationIndex ,  setupIntegrations  }  from  './integration' ; 
35+ import  {  createEventEnvelope ,  createSessionEnvelope  }  from  './request' ; 
36+ import  {  NewTransport  }  from  './transports/base' ; 
37+ import  {  NoopTransport  }  from  './transports/noop' ; 
3438
3539const  ALREADY_SEEN_ERROR  =  "Not capturing exception because it's already been captured." ; 
3640
3741/** 
3842 * Base implementation for all JavaScript SDK clients. 
3943 * 
44+  * TODO(v7): refactor doc w.r.t. Backend 
45+  * 
4046 * Call the constructor with the corresponding backend constructor and options 
4147 * specific to the client subclass. To access these options later, use 
4248 * {@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
7177   * The backend used to physically interact in the environment. Usually, this 
7278   * will correspond to the client. When composing SDKs, however, the Backend 
7379   * from the root SDK will be used. 
80+    * TODO(v7): DELETE 
7481   */ 
7582  protected  readonly  _backend : B ; 
7683
@@ -86,19 +93,30 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
8693  /** Number of calls being processed */ 
8794  protected  _numProcessing : number  =  0 ; 
8895
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+ 
89102  /** 
90103   * Initializes this client instance. 
91104   * 
92105   * @param  backendClass A constructor function to create the backend. 
93106   * @param  options Options for the client. 
94107   */ 
95108  protected  constructor ( backendClass : BackendClass < B ,  O > ,  options : O )  { 
109+     // TODO(v7): Delete 
96110    this . _backend  =  new  backendClass ( options ) ; 
97111    this . _options  =  options ; 
98112
99113    if  ( options . dsn )  { 
100114      this . _dsn  =  makeDsn ( options . dsn ) ; 
115+     }  else  { 
116+       IS_DEBUG_BUILD  &&  logger . warn ( 'No DSN provided, client will not do anything.' ) ; 
101117    } 
118+ 
119+     this . _transport  =  this . _setupTransport ( ) ; 
102120  } 
103121
104122  /** 
@@ -115,8 +133,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
115133    let  eventId : string  |  undefined  =  hint  &&  hint . event_id ; 
116134
117135    this . _process ( 
118-       this . _getBackend ( ) 
119-         . eventFromException ( exception ,  hint ) 
136+       this . eventFromException ( exception ,  hint ) 
120137        . then ( event  =>  this . _captureEvent ( event ,  hint ,  scope ) ) 
121138        . then ( result  =>  { 
122139          eventId  =  result ; 
@@ -133,8 +150,8 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
133150    let  eventId : string  |  undefined  =  hint  &&  hint . event_id ; 
134151
135152    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 ) ; 
138155
139156    this . _process ( 
140157      promisedEvent 
@@ -204,7 +221,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
204221   * @inheritDoc  
205222   */ 
206223  public  getTransport ( ) : Transport  { 
207-     return  this . _getBackend ( ) . getTransport ( ) ; 
224+     return  this . _transport ; 
208225  } 
209226
210227  /** 
@@ -249,6 +266,57 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
249266    } 
250267  } 
251268
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+ 
252320  /** Updates existing session based on the provided event */ 
253321  protected  _updateSessionFromEvent ( session : Session ,  event : Event ) : void { 
254322    let  crashed  =  false ; 
@@ -283,8 +351,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
283351  } 
284352
285353  /** Deliver captured session to Sentry */ 
354+   // TODO(v7): should this be deleted? 
286355  protected  _sendSession ( session : Session ) : void { 
287-     this . _getBackend ( ) . sendSession ( session ) ; 
356+     this . sendSession ( session ) ; 
288357  } 
289358
290359  /** 
@@ -317,7 +386,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
317386    } ) ; 
318387  } 
319388
320-   /** Returns the current backend. */ 
389+   /** Returns the current backend. 
390+    * TODO(v7): DELETE 
391+    */ 
321392  protected  _getBackend ( ) : B  { 
322393    return  this . _backend ; 
323394  } 
@@ -490,8 +561,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
490561   * Tells the backend to send this event 
491562   * @param  event The Sentry event to send 
492563   */ 
564+   // TODO(v7): refactor: get rid of method? 
493565  protected  _sendEvent ( event : Event ) : void { 
494-     this . _getBackend ( ) . sendEvent ( event ) ; 
566+     this . sendEvent ( event ) ; 
495567  } 
496568
497569  /** 
@@ -618,6 +690,24 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
618690      } , 
619691    ) ; 
620692  } 
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 > ; 
621711} 
622712
623713/** 
0 commit comments