Skip to content

Commit

Permalink
Merge pull request #2258 from bugsnag/PLAT-13089-plugin-browser-session
Browse files Browse the repository at this point in the history
Refactor @bugsnag/plugin-browser-session
  • Loading branch information
gingerbenw authored Dec 6, 2024
2 parents 7bbbff9 + c438893 commit c97ef7c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 19 deletions.
18 changes: 12 additions & 6 deletions packages/core/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ interface SessionDeliveryPayload {
notifier?: Notifier
device?: Device
app?: App
sessions?: Session[]
sessions?: Array<{
id: string
startedAt: Date
user?: User
}>
}

interface Delivery {
sendEvent(payload: EventDeliveryPayload, cb: (err?: Error | null) => void): void
sendSession(session: SessionDeliveryPayload, cb: (err?: Error | null) => void): void
}

export interface SessionDelegate {
startSession: (client: ClientWithInternals, session: Session) => ClientWithInternals
pauseSession: (client: ClientWithInternals) => void
resumeSession: (client: ClientWithInternals) => ClientWithInternals
}

/**
* Extend the public type definitions with internal declarations.
*
Expand All @@ -58,11 +68,7 @@ export default class ClientWithInternals<T extends Config = Config> extends Clie
_session: Session | null
_pausedSession: Session | null

_sessionDelegate: {
startSession: (client: ClientWithInternals, session: Session) => ClientWithInternals
pauseSession: (client: ClientWithInternals) => void
resumeSession: (client: ClientWithInternals) => ClientWithInternals
}
_sessionDelegate: SessionDelegate

_addOnSessionPayload: (cb: (sessionPayload: Session) => void) => void

Expand Down
17 changes: 15 additions & 2 deletions packages/plugin-browser-session/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@bugsnag/plugin-browser-session",
"version": "8.1.1",
"main": "session.js",
"main": "dist/session.js",
"types": "dist/types/session.d.ts",
"exports": {
".": {
"types": "./dist/types/session.d.ts",
"default": "./dist/session.js",
"import": "./dist/session.mjs"
}
},
"description": "@bugsnag/js plugin to enable session tracking in browsers",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -12,7 +20,7 @@
"access": "public"
},
"files": [
"*.js"
"dist"
],
"author": "Bugsnag",
"license": "MIT",
Expand All @@ -21,5 +29,10 @@
},
"peerDependencies": {
"@bugsnag/core": "^8.0.0"
},
"scripts": {
"build": "npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"clean": "rm -rf dist/*"
}
}
6 changes: 6 additions & 0 deletions packages/plugin-browser-session/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import createRollupConfig from "../../.rollup/index.mjs";

export default createRollupConfig({
input: "src/session.ts",
external: ["@bugsnag/core/lib/es-utils/includes"]
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const includes = require('@bugsnag/core/lib/es-utils/includes')
import { Plugin, Session } from '@bugsnag/core'
import ClientWithInternals, { Notifier } from '@bugsnag/core/client'
import includes from '@bugsnag/core/lib/es-utils/includes'

module.exports = {
load: client => { client._sessionDelegate = sessionDelegate }
interface InternalClient extends ClientWithInternals {
_notifier?: Notifier
}

const plugin: Plugin = {
load: client => {
(client as InternalClient)._sessionDelegate = sessionDelegate
}
}

const sessionDelegate = {
startSession: (client, session) => {
startSession: (client: InternalClient, session: Session) => {
const sessionClient = client
sessionClient._session = session
sessionClient._pausedSession = null

// exit early if the current releaseStage is not enabled
if (sessionClient._config.enabledReleaseStages !== null && !includes(sessionClient._config.enabledReleaseStages, sessionClient._config.releaseStage)) {
if (sessionClient._config.enabledReleaseStages && !includes(sessionClient._config.enabledReleaseStages, sessionClient._config.releaseStage)) {
sessionClient._logger.warn('Session not sent due to releaseStage/enabledReleaseStages configuration')
return sessionClient
}
Expand All @@ -24,13 +32,13 @@ const sessionDelegate = {
{
id: session.id,
startedAt: session.startedAt,
user: session._user
user: session.getUser()
}
]
})
}, () => {})
return sessionClient
},
resumeSession: (client) => {
resumeSession: (client: InternalClient) => {
// Do nothing if there's already an active session
if (client._session) {
return client
Expand All @@ -47,8 +55,10 @@ const sessionDelegate = {
// Otherwise start a new session
return client.startSession()
},
pauseSession: (client) => {
pauseSession: (client: InternalClient) => {
client._pausedSession = client._session
client._session = null
}
}

export default plugin
2 changes: 1 addition & 1 deletion packages/plugin-browser-session/test/session.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import plugin from '../'
import plugin from '../src/session'
import Client, { EventDeliveryPayload } from '@bugsnag/core/client'
import EventWithInternals from '@bugsnag/core/event'

Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-browser-session/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"]
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"packages/plugin-node-device",
"packages/plugin-node-surrounding-code",
"packages/plugin-node-uncaught-exception",
"packages/plugin-browser-session",
"packages/browser"
],
"exclude": [
Expand Down

0 comments on commit c97ef7c

Please sign in to comment.