Skip to content

browser: add BrowserBacktraceStorage implementation [INT-355] #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: feature/INT-355-storage-overhaul--sdk-core
Choose a base branch
from
Open
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
66 changes: 51 additions & 15 deletions examples/sdk/browser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/sdk/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const client = BacktraceClient.builder({
prop2: 123,
},
},
database: {
enable: true,
},
})
.useModule(
new BacktraceSessionReplayModule({
Expand Down
5 changes: 5 additions & 0 deletions packages/browser/src/BacktraceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BacktraceConfiguration } from './BacktraceConfiguration.js';
import { BacktraceClientBuilder } from './builder/BacktraceClientBuilder.js';
import { BacktraceClientSetup } from './builder/BacktraceClientSetup.js';
import { getStackTraceConverter } from './converters/getStackTraceConverter.js';
import { BrowserBacktraceStorage } from './storage/BrowserBacktraceStorage.js';

export class BacktraceClient<O extends BacktraceConfiguration = BacktraceConfiguration> extends BacktraceCoreClient<O> {
private readonly _disposeController: AbortController = new AbortController();
Expand All @@ -23,6 +24,10 @@ export class BacktraceClient<O extends BacktraceConfiguration = BacktraceConfigu
debugIdMapProvider: new VariableDebugIdMapProvider(window as DebugIdContainer),
sessionProvider: new BacktraceBrowserSessionProvider(),
...clientSetup,
database: {
storage: new BrowserBacktraceStorage(),
...clientSetup.database,
},
});

this.captureUnhandledErrors(
Expand Down
63 changes: 63 additions & 0 deletions packages/browser/src/storage/BrowserBacktraceStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { BacktraceStorageModule } from '@backtrace/sdk-core';

export class BrowserBacktraceStorage implements BacktraceStorageModule {
constructor(private readonly _storage = window.localStorage) {}

public async set(key: string, value: string): Promise<boolean> {
return this.setSync(key, value);
}

public async remove(key: string): Promise<boolean> {
return this.removeSync(key);
}

public async get(key: string): Promise<string | undefined> {
return this.getSync(key);
}

public async has(key: string): Promise<boolean> {
return this.hasSync(key);
}

public setSync(key: string, value: string): boolean {
try {
this._storage.setItem(key, value);
return true;
} catch {
return false;
}
}

public removeSync(key: string): boolean {
try {
this._storage.removeItem(key);
return true;
} catch {
return false;
}
}

public getSync(key: string): string | undefined {
try {
return this._storage.getItem(key) ?? undefined;
} catch {
return undefined;
}
}

public hasSync(key: string): boolean {
return key in this._storage;
}

public async *keys(): AsyncGenerator<string> {
for (const key of Object.keys(this._storage)) {
yield key;
}
}

public *keysSync(): Generator<string> {
for (const key of Object.keys(this._storage)) {
yield key;
}
}
}
Loading