mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 03:03:43 +00:00
feat: add support for IPC client managed session storage
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export * from "./ipc-message";
|
||||
export * from "./ipc.service";
|
||||
export * from "./ipc-session-repository";
|
||||
|
||||
44
libs/common/src/platform/ipc/ipc-session-repository.ts
Normal file
44
libs/common/src/platform/ipc/ipc-session-repository.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { firstValueFrom, map } from "rxjs";
|
||||
|
||||
import { Endpoint, IpcSessionRepository as SdkIpcSessionRepository } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { GlobalState, IPC_MEMORY, KeyDefinition, StateProvider } from "../state";
|
||||
|
||||
const IPC_SESSIONS = KeyDefinition.record<object, string>(IPC_MEMORY, "ipcSessions", {
|
||||
deserializer: (value: object) => value,
|
||||
});
|
||||
|
||||
export class IpcSessionRepository implements SdkIpcSessionRepository {
|
||||
private states: GlobalState<Record<string, object>>;
|
||||
|
||||
constructor(private stateProvider: StateProvider) {
|
||||
this.states = this.stateProvider.getGlobal(IPC_SESSIONS);
|
||||
}
|
||||
|
||||
get(endpoint: Endpoint): Promise<any | undefined> {
|
||||
return firstValueFrom(this.states.state$.pipe(map((s) => s?.[endpointToString(endpoint)])));
|
||||
}
|
||||
|
||||
async save(endpoint: Endpoint, session: any): Promise<void> {
|
||||
await this.states.update((s) => ({
|
||||
...s,
|
||||
[endpointToString(endpoint)]: session,
|
||||
}));
|
||||
}
|
||||
|
||||
async remove(endpoint: Endpoint): Promise<void> {
|
||||
await this.states.update((s) => {
|
||||
const newState = { ...s };
|
||||
delete newState[endpointToString(endpoint)];
|
||||
return newState;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function endpointToString(endpoint: Endpoint): string {
|
||||
if (typeof endpoint === "object" && "Web" in endpoint) {
|
||||
return `Web(${endpoint.Web.id})`;
|
||||
}
|
||||
|
||||
return endpoint;
|
||||
}
|
||||
Reference in New Issue
Block a user