1- const { intRange, runSyncCallbacks } = require ( '@bugsnag/core' )
2- const SessionTracker = require ( './tracker' )
3- const Backoff = require ( 'backo' )
1+ import { App , Client , Config , Device , Notifier , Plugin , Session , intRange , runSyncCallbacks } from '@bugsnag/core'
2+ import SessionTracker from './tracker'
3+ import Backoff from 'backo'
44
5- module . exports = {
5+ interface PluginConfig extends Config {
6+ sessionSummaryInterval ?: number
7+ }
8+
9+ interface SessionCount {
10+ startedAt : string
11+ sessionsStarted : number
12+ }
13+
14+ interface SessionPayload extends Session {
15+ notifier : Notifier
16+ device : Device
17+ app : App
18+ sessionCounts : SessionCount [ ]
19+ }
20+
21+ const plugin : Plugin < PluginConfig > = {
622 load : ( client ) => {
7- const sessionTracker = new SessionTracker ( client . _config . sessionSummaryInterval )
23+ const sessionTracker = new SessionTracker ( client . _config . sessionSummaryInterval ?? undefined )
824 sessionTracker . on ( 'summary' , sendSessionSummary ( client ) )
925 sessionTracker . start ( )
1026 client . _sessionDelegate = {
@@ -31,8 +47,9 @@ module.exports = {
3147 return client
3248 }
3349
34- // Otherwise start a new session
35- return client . startSession ( )
50+ // Otherwise start a new session and ensure a Client is always returned
51+ const newClient = client . startSession ( )
52+ return newClient || client
3653 }
3754 }
3855 } ,
@@ -45,7 +62,7 @@ module.exports = {
4562 }
4663}
4764
48- const sendSessionSummary = client => sessionCounts => {
65+ const sendSessionSummary = ( client : Client ) => ( sessionCounts : SessionCount [ ] ) : void => {
4966 // exit early if the current releaseStage is not enabled
5067 if ( client . _config . enabledReleaseStages !== null && ! client . _config . enabledReleaseStages . includes ( client . _config . releaseStage ) ) {
5168 client . _logger . warn ( 'Session not sent due to releaseStage/enabledReleaseStages configuration' )
@@ -58,7 +75,7 @@ const sendSessionSummary = client => sessionCounts => {
5875 const maxAttempts = 10
5976 req ( handleRes )
6077
61- function handleRes ( err ) {
78+ function handleRes ( err ?: Error | null ) : void {
6279 if ( ! err ) {
6380 const sessionCount = sessionCounts . reduce ( ( accum , s ) => accum + s . sessionsStarted , 0 )
6481 return client . _logger . debug ( `${ sessionCount } session(s) reported` )
@@ -67,11 +84,11 @@ const sendSessionSummary = client => sessionCounts => {
6784 client . _logger . error ( 'Session delivery failed, max retries exceeded' , err )
6885 return
6986 }
70- client . _logger . debug ( 'Session delivery failed, retry #' + ( backoff . attempts + 1 ) + '/' + maxAttempts , err )
87+ client . _logger . error ( 'Session delivery failed, retry #' + ( backoff . attempts + 1 ) + '/' + maxAttempts , err )
7188 setTimeout ( ( ) => req ( handleRes ) , backoff . duration ( ) )
7289 }
7390
74- function req ( cb ) {
91+ function req ( cb : ( err ?: Error | null ) => void ) {
7592 const payload = {
7693 notifier : client . _notifier ,
7794 device : { } ,
@@ -83,7 +100,7 @@ const sendSessionSummary = client => sessionCounts => {
83100 sessionCounts
84101 }
85102
86- const ignore = runSyncCallbacks ( client . _cbs . sp , payload , 'onSessionPayload' , client . _logger )
103+ const ignore = runSyncCallbacks ( client . _cbs . sp , payload as SessionPayload , 'onSessionPayload' , client . _logger )
87104 if ( ignore ) {
88105 client . _logger . debug ( 'Session not sent due to onSessionPayload callback' )
89106 return cb ( null )
@@ -92,3 +109,5 @@ const sendSessionSummary = client => sessionCounts => {
92109 client . _delivery . sendSession ( payload , cb )
93110 }
94111}
112+
113+ export default plugin
0 commit comments