Add server: encrypted event log API, blobs, SSE, ECB rates, Web Push
Node 24 standard library only (node:http, node:sqlite, node:crypto). Push uses VAPID + RFC 8291 encryption with an allowlist of push services. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013FYo6JSV1fDU1fm3LU77iF
This commit is contained in:
parent
c2043b86bc
commit
8dd25b93b0
10 changed files with 1005 additions and 1 deletions
52
apps/server/src/rates.ts
Normal file
52
apps/server/src/rates.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
// ECB daily reference rates (EUR-based). Fetched by the server so that
|
||||
// clients never contact a third party (SPEC.md §8.2).
|
||||
import type { Store } from './db.ts';
|
||||
|
||||
/** Parse the ECB eurofxref XML into date → { currency: rate }. EUR = "1". */
|
||||
export function parseEcbXml(xml: string): Map<string, Record<string, string>> {
|
||||
const out = new Map<string, Record<string, string>>();
|
||||
const dayRe = /<Cube time="(\d{4}-\d{2}-\d{2})">([\s\S]*?)<\/Cube>/g;
|
||||
const rateRe = /<Cube currency="([A-Z]{3})" rate="(\d+(?:\.\d+)?)"\s*\/>/g;
|
||||
for (const day of xml.matchAll(dayRe)) {
|
||||
const rates: Record<string, string> = { EUR: '1' };
|
||||
for (const r of day[2].matchAll(rateRe)) rates[r[1]] = r[2];
|
||||
out.set(day[1], rates);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export class RatesService {
|
||||
private timer: NodeJS.Timeout | undefined;
|
||||
private readonly store: Store;
|
||||
private readonly baseUrl: string;
|
||||
|
||||
constructor(store: Store, baseUrl: string) {
|
||||
this.store = store;
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
async refresh(): Promise<void> {
|
||||
// Full history the first time (≈8 MB), the last 90 days afterwards.
|
||||
const file = this.store.latestRatesDate() ? 'eurofxref-hist-90d.xml' : 'eurofxref-hist.xml';
|
||||
const res = await fetch(`${this.baseUrl}/${file}`, { signal: AbortSignal.timeout(60_000) });
|
||||
if (!res.ok) throw new Error(`ECB ${file}: HTTP ${res.status}`);
|
||||
const days = parseEcbXml(await res.text());
|
||||
if (days.size === 0) throw new Error(`ECB ${file}: no rates parsed`);
|
||||
this.store.tx(() => {
|
||||
for (const [date, rates] of days) this.store.saveRates(date, rates);
|
||||
});
|
||||
console.log(`rates: stored ${days.size} days from ${file}, latest ${this.store.latestRatesDate()}`);
|
||||
}
|
||||
|
||||
/** Refresh now and then every 6 hours (ECB publishes once a day ~16:00 CET). */
|
||||
start(): void {
|
||||
const run = () => this.refresh().catch((e) => console.error(`rates: ${e.message}`));
|
||||
void run();
|
||||
this.timer = setInterval(run, 6 * 3600_000);
|
||||
this.timer.unref();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
clearInterval(this.timer);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue