Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add platform client config (#2413)
Leendert de Borst002470a2af77
diff --git a/apps/web/src/platform/ClientServices.ts b/apps/web/src/platform/ClientServices.tsnew file mode 100644index 000000000..050fb4e19--- /dev/null+++ b/apps/web/src/platform/ClientServices.ts@@ -0,0 +1,9 @@+/**+ * Registers the web platform with the client core.+ */++import { setPlatform } from '@aliasvault/client/platform';++import { webPlatform } from '@/platform/WebPlatform';++setPlatform(webPlatform);diff --git a/apps/web/src/platform/WebKeyValueStore.ts b/apps/web/src/platform/WebKeyValueStore.tsnew file mode 100644index 000000000..7dcf63e86--- /dev/null+++ b/apps/web/src/platform/WebKeyValueStore.ts@@ -0,0 +1,191 @@+/**+ * The web app's platform storage:+ * - `local:` keys live in IndexedDB (the encrypted vault can exceed the localStorage quota)+ * - `session:` keys live in memory only so the decrypted vault key is never persisted on disk.+ */++import type { IKeyValueStore, StorageKey } from '@aliasvault/client/platform';++const DB_NAME = 'aliasvault-web';+const DB_VERSION = 1;+const STORE_NAME = 'kv';+const CHANNEL_NAME = 'aliasvault-web-storage';++type Listener = (value: unknown) => void;++/**+ * A change another tab broadcast.+ */+type StorageChangeMessage = {+ key: string;+ value: unknown;+};++/**+ * Wrap an IndexedDB request in a promise.+ * @param request - the request+ */+function requestToPromise<T>(request: IDBRequest<T>): Promise<T> {+ return new Promise((resolve, reject) => {+ request.addEventListener('success', () => resolve(request.result));+ request.addEventListener('error', () => reject(request.error ?? new Error('IndexedDB request failed')));+ });+}++/**+ * Wait for a transaction to complete.+ * @param transaction - the transaction+ */+function transactionDone(transaction: IDBTransaction): Promise<void> {+ return new Promise((resolve, reject) => {+ transaction.addEventListener('complete', () => resolve());+ transaction.addEventListener('error', () => reject(transaction.error ?? new Error('IndexedDB transaction failed')));+ transaction.addEventListener('abort', () => reject(transaction.error ?? new Error('IndexedDB transaction aborted')));+ });+}++/**+ * The platform key-value store.+ */+export class WebKeyValueStore implements IKeyValueStore {+ private dbPromise: Promise<IDBDatabase> | null = null;+ private readonly session = new Map<string, unknown>();+ private readonly watchers = new Map<string, Set<Listener>>();+ private readonly channel: BroadcastChannel | null;++ /**+ * Create the store and start listening for changes other tabs make.+ */+ public constructor() {+ this.channel = typeof BroadcastChannel === 'undefined' ? null : new BroadcastChannel(CHANNEL_NAME);+ this.channel?.addEventListener('message', (event: MessageEvent<StorageChangeMessage>) => {+ this.notify(event.data.key, event.data.value);+ });+ }++ /** @inheritdoc */+ public async get<T = unknown>(key: StorageKey): Promise<T | null> {+ if (key.startsWith('session:')) {+ return (this.session.get(key) as T | undefined) ?? null;+ }+ const db = await this.open();+ const value = await requestToPromise(db.transaction(STORE_NAME, 'readonly').objectStore(STORE_NAME).get(key));+ return (value as T | undefined) ?? null;+ }++ /** @inheritdoc */+ public async set(key: StorageKey, value: unknown): Promise<void> {+ await this.setMany([{ key, value }]);+ }++ /** @inheritdoc */+ public async setMany(entries: { key: StorageKey; value: unknown }[]): Promise<void> {+ const persisted = entries.filter(entry => !entry.key.startsWith('session:'));+ for (const entry of entries) {+ if (entry.key.startsWith('session:')) {+ this.session.set(entry.key, entry.value);+ }+ }+ if (persisted.length > 0) {+ const db = await this.open();+ const transaction = db.transaction(STORE_NAME, 'readwrite');+ const store = transaction.objectStore(STORE_NAME);+ for (const entry of persisted) {+ store.put(entry.value, entry.key);+ }+ await transactionDone(transaction);+ }+ for (const entry of entries) {+ this.notify(entry.key, entry.value);+ if (!entry.key.startsWith('session:')) {+ this.broadcast(entry.key, entry.value);+ }+ }+ }++ /** @inheritdoc */+ public async remove(key: StorageKey): Promise<void> {+ await this.removeMany([key]);+ }++ /** @inheritdoc */+ public async removeMany(keys: StorageKey[]): Promise<void> {+ const persisted = keys.filter(key => !key.startsWith('session:'));+ for (const key of keys) {+ if (key.startsWith('session:')) {+ this.session.delete(key);+ }+ }+ if (persisted.length > 0) {+ const db = await this.open();+ const transaction = db.transaction(STORE_NAME, 'readwrite');+ const store = transaction.objectStore(STORE_NAME);+ for (const key of persisted) {+ store.delete(key);+ }+ await transactionDone(transaction);+ }+ for (const key of keys) {+ this.notify(key, null);+ if (!key.startsWith('session:')) {+ this.broadcast(key, null);+ }+ }+ }++ /** @inheritdoc */+ public watch<T = unknown>(key: StorageKey, callback: (value: T | null) => void): () => void {+ const listeners = this.watchers.get(key) ?? new Set<Listener>();+ /**+ * Forward a stored value to the caller's typed callback.+ */+ const listener: Listener = (value: unknown): void => callback((value as T | null) ?? null);+ listeners.add(listener);+ this.watchers.set(key, listeners);+ return (): void => {+ listeners.delete(listener);+ };+ }++ /**+ * Open (and on first use create) the database.+ */+ private open(): Promise<IDBDatabase> {+ this.dbPromise ??= new Promise((resolve, reject) => {+ const request = indexedDB.open(DB_NAME, DB_VERSION);+ request.addEventListener('upgradeneeded', () => {+ if (!request.result.objectStoreNames.contains(STORE_NAME)) {+ request.result.createObjectStore(STORE_NAME);+ }+ });+ request.addEventListener('success', () => resolve(request.result));+ request.addEventListener('error', () => {+ this.dbPromise = null;+ reject(request.error ?? new Error('Could not open IndexedDB'));+ });+ });+ return this.dbPromise;+ }++ /**+ * Notify this tab's watchers of a key.+ * @param key - the key+ * @param value - the new value, null when removed+ */+ private notify(key: string, value: unknown): void {+ this.watchers.get(key)?.forEach(listener => listener(value));+ }++ /**+ * Tell the other tabs about a change to a persisted key.+ * @param key - the key+ * @param value - the new value, null when removed+ */+ private broadcast(key: string, value: unknown): void {+ try {+ this.channel?.postMessage({ key, value } satisfies StorageChangeMessage);+ } catch {+ // A value that cannot be cloned (should not happen for JSON data) is not worth failing the write for.+ }+ }+}diff --git a/apps/web/src/platform/WebPlatform.ts b/apps/web/src/platform/WebPlatform.tsnew file mode 100644index 000000000..960f2fa8c--- /dev/null+++ b/apps/web/src/platform/WebPlatform.ts@@ -0,0 +1,50 @@+/**+ * The web app's implementation of the client core's platform interfaces.+ */++import { createRustSqliteEngine } from '@aliasvault/client/database/RustSqliteEngine';+import { type IClientPlatform, TranslatableMessage } from '@aliasvault/client/platform';+import { createWasmRustCore } from '@aliasvault/client/rust/WasmRustCore';++import i18n from '@/i18n/i18n';+import { WebKeyValueStore } from '@/platform/WebKeyValueStore';+import { devError, devLog, devWarn } from '@/utils/DevLogger';++/**+ * Which translation key backs each of the core's own messages.+ */+const TRANSLATION_KEYS: Record<TranslatableMessage, string> = {+ [TranslatableMessage.ClientOutdated]: 'sharedResources.ClientVersionUnsupported',+ [TranslatableMessage.VaultUpgradeRequired]: 'content.vaultUpgradeRequired',+};++/**+ * Where the wasm binary is served from (copied there by vite.config.ts).+ * @param file - the file name+ */+function wasmUrl(file: string): string {+ return `${import.meta.env.BASE_URL}wasm/${file}`;+}++/**+ * The Rust core, streamed into WebAssembly.instantiateStreaming; it also hosts the SQLite engine.+ */+const rustCore = createWasmRustCore(async (): Promise<Response> => fetch(wasmUrl('aliasvault_core_bg.wasm')));++/**+ * The platform the web app registers with the client core.+ */+export const webPlatform: IClientPlatform = {+ storage: new WebKeyValueStore(),+ logger: { log: devLog, warn: devWarn, error: devError },+ app: {+ version: __APP_VERSION__,+ clientName: 'web',+ },+ rustCore,+ sqlite: createRustSqliteEngine(rustCore),+ /**+ * Translate one of the core's own messages through the app's i18n.+ */+ translate: async (message: TranslatableMessage): Promise<string> => i18n.t(TRANSLATION_KEYS[message]),+};