Add Svelte web client and service worker
Mobile-first PWA: groups via key-in-fragment links, encrypted outbox and sync over SSE, all split modes, currencies with ECB rate prefill, photos, comments, history and rewind, stats, recurring expenses, CSV and backup, EN/CS, dark/light theme, Web Push decrypted in the service worker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013FYo6JSV1fDU1fm3LU77iF
This commit is contained in:
parent
f67add3660
commit
8fbbb4933e
53 changed files with 4558 additions and 37 deletions
150
apps/web/src/lib/idb.ts
Normal file
150
apps/web/src/lib/idb.ts
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
// IndexedDB storage, shared by the page and the service worker.
|
||||
// Events are kept as the server's ciphertext and decrypted when loaded.
|
||||
|
||||
export interface LocalGroup {
|
||||
id: string;
|
||||
/** the key from the link; anyone with this has full access */
|
||||
secret: string;
|
||||
name: string;
|
||||
icon: string;
|
||||
baseCurrency: string;
|
||||
/** "who am I" on this device */
|
||||
me: string | null;
|
||||
/** my balance in base minor units, cached for the home screen */
|
||||
myBalance: number | null;
|
||||
lastSeq: number;
|
||||
lastOpened: number;
|
||||
push: boolean;
|
||||
/** highest seq the service worker has notified about */
|
||||
notifiedSeq: number;
|
||||
/** set when the server says the group no longer exists */
|
||||
gone?: boolean;
|
||||
}
|
||||
|
||||
export interface StoredEvent {
|
||||
g: string;
|
||||
seq: number;
|
||||
id: string;
|
||||
deviceId: string;
|
||||
iv: string;
|
||||
ct: string;
|
||||
}
|
||||
|
||||
export interface OutboxItem {
|
||||
n?: number;
|
||||
g: string;
|
||||
id: string;
|
||||
deviceId: string;
|
||||
iv: string;
|
||||
ct: string;
|
||||
}
|
||||
|
||||
export interface StoredBlob {
|
||||
g: string;
|
||||
id: string;
|
||||
/** encrypted bytes */
|
||||
data: ArrayBuffer;
|
||||
uploaded: boolean;
|
||||
}
|
||||
|
||||
let dbp: Promise<IDBDatabase> | null = null;
|
||||
|
||||
export function openDb(): Promise<IDBDatabase> {
|
||||
if (dbp) return dbp;
|
||||
dbp = new Promise((resolve, reject) => {
|
||||
const req = indexedDB.open('oweme', 1);
|
||||
req.onupgradeneeded = () => {
|
||||
const db = req.result;
|
||||
db.createObjectStore('groups', { keyPath: 'id' });
|
||||
db.createObjectStore('events', { keyPath: ['g', 'seq'] });
|
||||
db.createObjectStore('outbox', { keyPath: 'n', autoIncrement: true }).createIndex('g', 'g');
|
||||
db.createObjectStore('blobs', { keyPath: ['g', 'id'] });
|
||||
db.createObjectStore('kv');
|
||||
};
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
return dbp;
|
||||
}
|
||||
|
||||
type StoreName = 'groups' | 'events' | 'outbox' | 'blobs' | 'kv';
|
||||
|
||||
function wrap<T>(req: IDBRequest<T>): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
}
|
||||
|
||||
async function store(name: StoreName, mode: IDBTransactionMode = 'readonly'): Promise<IDBObjectStore> {
|
||||
return (await openDb()).transaction(name, mode).objectStore(name);
|
||||
}
|
||||
|
||||
export async function get<T>(name: StoreName, key: IDBValidKey): Promise<T | undefined> {
|
||||
return wrap((await store(name)).get(key));
|
||||
}
|
||||
export async function put(name: StoreName, value: unknown, key?: IDBValidKey): Promise<void> {
|
||||
await wrap((await store(name, 'readwrite')).put(value, key));
|
||||
}
|
||||
export async function del(name: StoreName, key: IDBValidKey | IDBKeyRange): Promise<void> {
|
||||
await wrap((await store(name, 'readwrite')).delete(key));
|
||||
}
|
||||
export async function getAll<T>(name: StoreName, query?: IDBKeyRange): Promise<T[]> {
|
||||
return wrap((await store(name)).getAll(query)) as Promise<T[]>;
|
||||
}
|
||||
|
||||
export const groupRange = (g: string) => IDBKeyRange.bound([g, -Infinity], [g, Infinity]);
|
||||
export const groupBlobRange = (g: string) => IDBKeyRange.bound([g, ''], [g, '']);
|
||||
|
||||
export async function putEvents(events: StoredEvent[]): Promise<void> {
|
||||
if (!events.length) return;
|
||||
const tx = (await openDb()).transaction('events', 'readwrite');
|
||||
for (const e of events) tx.objectStore('events').put(e);
|
||||
await new Promise<void>((res, rej) => ((tx.oncomplete = () => res()), (tx.onerror = () => rej(tx.error))));
|
||||
}
|
||||
|
||||
export async function outboxFor(g: string): Promise<OutboxItem[]> {
|
||||
const s = await store('outbox');
|
||||
return wrap(s.index('g').getAll(g)) as Promise<OutboxItem[]>;
|
||||
}
|
||||
|
||||
export async function updateGroup(id: string, patch: Partial<LocalGroup>): Promise<LocalGroup | undefined> {
|
||||
const tx = (await openDb()).transaction('groups', 'readwrite');
|
||||
const s = tx.objectStore('groups');
|
||||
const g = (await wrap(s.get(id))) as LocalGroup | undefined;
|
||||
if (!g) return undefined;
|
||||
const next = { ...g, ...patch };
|
||||
s.put(next);
|
||||
await new Promise<void>((res, rej) => ((tx.oncomplete = () => res()), (tx.onerror = () => rej(tx.error))));
|
||||
return next;
|
||||
}
|
||||
|
||||
/** Remove a group and everything cached for it from this device. */
|
||||
export async function forgetGroup(id: string): Promise<void> {
|
||||
const db = await openDb();
|
||||
const tx = db.transaction(['groups', 'events', 'outbox', 'blobs'], 'readwrite');
|
||||
tx.objectStore('groups').delete(id);
|
||||
tx.objectStore('events').delete(groupRange(id));
|
||||
tx.objectStore('blobs').delete(groupBlobRange(id));
|
||||
const ob = tx.objectStore('outbox');
|
||||
const keys = await wrap(ob.index('g').getAllKeys(id));
|
||||
for (const k of keys) ob.delete(k);
|
||||
await new Promise<void>((res, rej) => ((tx.oncomplete = () => res()), (tx.onerror = () => rej(tx.error))));
|
||||
}
|
||||
|
||||
export async function kvGet<T>(k: string): Promise<T | undefined> {
|
||||
return get<T>('kv', k);
|
||||
}
|
||||
export async function kvSet(k: string, v: unknown): Promise<void> {
|
||||
await put('kv', v, k);
|
||||
}
|
||||
|
||||
/** Stable random id for this browser, so it can skip notifying itself. */
|
||||
export async function deviceId(): Promise<string> {
|
||||
let id = await kvGet<string>('deviceId');
|
||||
if (!id) {
|
||||
id = Array.from(crypto.getRandomValues(new Uint8Array(12)), (b) => b.toString(36).padStart(2, '0')).join('');
|
||||
await kvSet('deviceId', id);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue