import { reduce, emptyState, type Action, type Item, type JWTPublicKey, type List, type Membership, type User, type Pick, type Boost, type WebPushSubscription } from '@eligo/protocol'; import { readFileSync, writeFileSync } from 'node:fs'; import { Tail } from 'tail'; const readFullData = (filepath: string): Action[] => { try { return readFileSync(filepath) .toString() .split('\n') .filter((l) => l.length > 0) .map((line) => JSON.parse(line)); } catch (err: any) { if (err.code === 'ENOENT') return []; throw err; } }; // reader watches all actions from _filepath_ happened after _from_ const reader = (filepath: string, from: number) => { let callbacks: ((action: Action) => void)[] = []; const tail = new Tail(filepath); let lastSeen = from; tail.on('line', (line: string) => { const action = JSON.parse(line) as Action; if (action.meta?.timestamp > lastSeen) { callbacks.forEach((callback) => callback(action)); lastSeen = action.meta?.timestamp; } }); return { on: (callback: (action: Action) => void) => { callbacks.push(callback); }, off: (callback: (action: Action) => void) => { callbacks = callbacks.filter((cb) => cb !== callback); } }; }; export type Database = { on: (callback: (action: Action) => void) => void; off: (callback: (action: Action) => void) => void; append: (userId: string | undefined, action: Action) => Promise; find: { (key: 'jwtKeys', filter: Partial): Promise; (key: 'users', filter: Partial): Promise; (key: 'lists', filter: Partial): Promise; (key: 'memberships', filter: Partial): Promise; (key: 'items', filter: Partial): Promise; (key: 'picks', filter: Partial): Promise; (key: 'boosts', filter: Partial): Promise; (key: 'webPushSuscriptions', filter: Partial): Promise< WebPushSubscription | undefined >; }; filter: { (key: 'jwtKeys', filter: Partial>): Promise; (key: 'users', filter: Partial>): Promise; (key: 'lists', filter: Partial>): Promise; (key: 'memberships', filter: Partial>): Promise; (key: 'items', filter: Partial>): Promise; (key: 'picks', filter: Partial>): Promise; (key: 'boosts', filter: Partial>): Promise; (key: 'webPushSuscriptions', filter: Partial>): Promise< WebPushSubscription[] >; }; }; export default (filepath: string): Database => { console.log(`using database ${filepath}`); const data = readFullData(filepath); const stream = reader(filepath, Math.max(...data.map((d) => d.meta?.timestamp as number))); let state = data.reduce(reduce, emptyState); stream.on((action: Action) => (state = reduce(state, action))); return { on: stream.on, off: stream.off, append: async (userId: string | undefined, action: Action) => { action.meta ||= { userId, timestamp: new Date().getTime() }; writeFileSync(filepath, JSON.stringify(action) + '\n', { flag: 'a' }); }, find: async ( key: K, filter: Partial ): Promise => { if (Object.keys(filter).length === 1 && filter.id !== undefined) return state[key][filter.id]; return Object.values(state[key]).find((value: any) => Object.entries(filter).every(([k, v]) => value[k] === v) ); }, filter: async ( key: K, filter: Partial> = {} ): Promise => { return Object.values(state[key]).filter((value: any) => Object.entries(filter).every(([k, v]) => value[k] === v) ); } }; };