Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/browser/src/BacktraceBrowserSessionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export class BacktraceBrowserSessionProvider implements BacktraceSessionProvider
* the session is treaten as an old session.
*/
public static readonly PERSISTENCE_INTERVAL = TimeHelper.convertSecondsToMilliseconds(30 * 60);
private readonly SESSION_LAST_ACTIVE = 'backtrace-last-active';
private readonly SESSION_GUID = 'backtrace-guid';
private readonly SESSION_LAST_ACTIVE = 'backtrace-session-last-active';
private readonly SESSION_ID = 'backtrace-session-id';

get lastActive(): number {
return this._lastActive;
Expand All @@ -25,14 +25,19 @@ export class BacktraceBrowserSessionProvider implements BacktraceSessionProvider
}

const lastActive = this.readLastActiveTimestamp();
if (!lastActive || TimeHelper.now() - lastActive > BacktraceBrowserSessionProvider.PERSISTENCE_INTERVAL) {
const sessionId = localStorage.getItem(this.SESSION_ID);
if (
!sessionId ||
!lastActive ||
TimeHelper.now() - lastActive > BacktraceBrowserSessionProvider.PERSISTENCE_INTERVAL
) {
this.updateLastActiveTimestamp();
localStorage.setItem(this.SESSION_GUID, this.sessionId);
localStorage.setItem(this.SESSION_ID, this.sessionId);
return;
}
this._lastActive = lastActive;
this.newSession = false;
this.sessionId = localStorage.getItem(this.SESSION_GUID) as string;
this.sessionId = sessionId;
}

public afterMetricsSubmission(): void {
Expand Down
68 changes: 68 additions & 0 deletions packages/browser/tests/client/guidSessionTests.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { BacktraceRequestHandler } from '@backtrace/sdk-core';
import { BacktraceBrowserSessionProvider } from '../../src/BacktraceBrowserSessionProvider.js';
import { BacktraceClient, BacktraceConfiguration } from '../../src/index.js';

describe('Client guid and session tests', () => {
const requestHandler: BacktraceRequestHandler = {
post: jest.fn().mockResolvedValue(Promise.resolve()),
postError: jest.fn().mockResolvedValue(Promise.resolve()),
};

const defaultClientOptions: BacktraceConfiguration = {
name: 'test',
version: '1.0.0',
url: 'https://submit.backtrace.io/foo/bar/baz',
metrics: {
enable: false,
},
breadcrumbs: {
enable: false,
},
};

let client: BacktraceClient | undefined;

function buildClient(options: BacktraceConfiguration = defaultClientOptions) {
client?.dispose();
client = BacktraceClient.builder(options).useRequestHandler(requestHandler).build();
return client;
}

beforeEach(() => {
localStorage.clear();
});

afterEach(() => {
client?.dispose();
client = undefined;
});

it('Should not use the session id as the guid', () => {
const { attributes } = buildClient();

expect(attributes['guid']).toBeTruthy();
expect(attributes['guid']).not.toEqual(attributes['application.session']);
});

it('Should keep the guid when the session changes', () => {
const first = buildClient().attributes;
const expired = Date.now() - BacktraceBrowserSessionProvider.PERSISTENCE_INTERVAL - 1;
localStorage.setItem('backtrace-session-last-active', expired.toString(10));
const second = buildClient().attributes;

expect(second['application.session']).not.toEqual(first['application.session']);
expect(second['guid']).toEqual(first['guid']);
});

it('Should keep the stored guid and start a new session after upgrade', () => {
const previousId = 'previous-session-and-guid';
localStorage.setItem('backtrace-guid', previousId);
localStorage.setItem('backtrace-last-active', Date.now().toString(10));

const { attributes } = buildClient({ ...defaultClientOptions, database: { enable: true, path: 'test' } });

expect(attributes['guid']).toEqual(previousId);
expect(typeof attributes['application.session']).toBe('string');
expect(attributes['application.session']).not.toEqual(previousId);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { TimeHelper } from '@backtrace/sdk-core';
import { BacktraceBrowserSessionProvider } from '../../src/BacktraceBrowserSessionProvider.js';
describe('Session provider tests', () => {
beforeEach(() => {
localStorage.clear();
});

it('Should generate a new uuid on new session', () => {
const sessionProvider = new BacktraceBrowserSessionProvider();

Expand All @@ -16,8 +20,8 @@ describe('Session provider tests', () => {
it('Should generate a new sessionId if the lastActive timestamp is greater than persistence interval time', () => {
const fakeId = 'test';
const lastSessionActiveDate = new Date(Date.now() - BacktraceBrowserSessionProvider.PERSISTENCE_INTERVAL - 1);
localStorage.setItem('backtrace-last-active', lastSessionActiveDate.getTime().toString(10));
localStorage.setItem('backtrace-guid', fakeId);
localStorage.setItem('backtrace-session-last-active', lastSessionActiveDate.getTime().toString(10));
localStorage.setItem('backtrace-session-id', fakeId);

const sessionProvider = new BacktraceBrowserSessionProvider();
expect(sessionProvider.sessionId).toBeDefined();
Expand All @@ -27,21 +31,48 @@ describe('Session provider tests', () => {
it('Should not generate a new sessionId if the lastActive timestamp is lower than persistence interval time', () => {
const fakeId = 'test';
const lastSessionActiveDate = new Date(Date.now() - BacktraceBrowserSessionProvider.PERSISTENCE_INTERVAL + 10);
localStorage.setItem('backtrace-last-active', lastSessionActiveDate.getTime().toString(10));
localStorage.setItem('backtrace-guid', fakeId);
localStorage.setItem('backtrace-session-last-active', lastSessionActiveDate.getTime().toString(10));
localStorage.setItem('backtrace-session-id', fakeId);

const sessionProvider = new BacktraceBrowserSessionProvider();
expect(sessionProvider.sessionId).toBeDefined();
expect(sessionProvider.sessionId).toEqual(fakeId);
});

it('Should start a new session if the stored session id is missing', () => {
localStorage.setItem('backtrace-session-last-active', Date.now().toString(10));

const sessionProvider = new BacktraceBrowserSessionProvider();
expect(sessionProvider.newSession).toBe(true);
expect(sessionProvider.sessionId).toBeTruthy();
expect(localStorage.getItem('backtrace-session-id')).toEqual(sessionProvider.sessionId);
});

it('Should not write to the guid storage key', () => {
localStorage.setItem('backtrace-guid', 'guid');

new BacktraceBrowserSessionProvider();
expect(localStorage.getItem('backtrace-guid')).toEqual('guid');
});

it('Should ignore the legacy last-active key', () => {
const legacyLastActive = Date.now().toString(10);
localStorage.setItem('backtrace-session-id', 'previous-session');
localStorage.setItem('backtrace-last-active', legacyLastActive);

const sessionProvider = new BacktraceBrowserSessionProvider();
expect(sessionProvider.newSession).toBe(true);
expect(sessionProvider.sessionId).not.toEqual('previous-session');
expect(localStorage.getItem('backtrace-last-active')).toEqual(legacyLastActive);
});

it('Should update timestamp', () => {
const timestamp = Date.now();
jest.spyOn(TimeHelper, 'now').mockImplementation(() => {
return timestamp;
});

localStorage.setItem('backtrace-last-active', new Date(2010, 1, 1, 1, 1, 1, 1).getTime().toString(10));
localStorage.setItem('backtrace-session-last-active', new Date(2010, 1, 1, 1, 1, 1, 1).getTime().toString(10));
const sessionProvider = new BacktraceBrowserSessionProvider();

sessionProvider.afterMetricsSubmission();
Expand Down
Loading