Add apps/price-server that fetches XEC via ecash-price CoinMarketCap on a
configurable interval (default 5 minutes), exposes GET /price for the latest
snapshot, and pushes the same updates over WebSocket.
Depends on D20727
Differential D20728
[price-server] Add XEC price API and websocket server Authored by bytesofman on Sep 29 2026, 13:22. Project Tags None Referenced Files None Subscribers None
Details
Add apps/price-server that fetches XEC via ecash-price CoinMarketCap on a Depends on D20727 cd apps/price-server && pnpm test
Diff Detail
Event TimelineComment Actions Build Bitcoin ABC Diffs / Diff Testing (ai-review) passed. Diff : committed changes only (\(\ ──────────────────────────────────────────────────────────────────────── major [Stability & Availability] → ]8;;vscode://file//work/apps/price-server/package.json:34apps/price-server/package.json:34-46]8;; Declare the test dependency in price-server. The changed tests import chai, but neither dependency list declares it. With pnpm’s default isolated dependency layout, a dependency of another workspace package does not make chai importable from apps/price-server/src/*.test.ts. Add chai to this package’s devDependencies so pnpm test can load the tests reliably. (pnpm.io) ──────────────────────────────────────────────────────────────────────── major [Stability & Availability] → ]8;;vscode://file//work/apps/price-server/src/priceWs.ts:23apps/price-server/src/priceWs.ts:23]8;; Bound each client’s pending WebSocket data. When a connected client reads more slowly than price updates arrive, every send() queues another snapshot. This broadcast checks readyState but never checks the pending byte count. A slow client can therefore retain increasing amounts of server memory. Since each message replaces the previous price snapshot, skip or close a client once its bufferedAmount exceeds a limit. ws exposes that count for queued, unsent data. (github.com) ──────────────────────────────────────────────────────────────────────── major [Stability & Availability] → ]8;;vscode://file//work/apps/price-server/index.ts:39apps/price-server/index.ts:39-41]8;; Close WebSocket clients during shutdown. If a client remains connected when SIGTERM arrives, server.close() leaves the upgraded connection open. Its callback can then wait indefinitely, so shutdown never reaches process.exit(0). Keep the WebSocketServer returned by initPriceWs, close or terminate its clients, and close it as part of shutdown. The ws documentation confirms that closing its server does not automatically close existing clients. (github.com) ──────────────────────────────────────────────────────────────────────── major [Functional Correctness] → ]8;;vscode://file//work/apps/price-server/src/priceService.ts:91apps/price-server/src/priceService.ts:91-93]8;; Prevent overlapping price refreshes. If a provider call takes longer than the configured interval, this timer starts another refresh() before the first finishes. A later request can finish first; the older request can then replace latest and broadcast an older price with a new lastUpdated value. Serialize refreshes or discard results from superseded requests. ──────────────────────────────────────────────────────────────────────── major [Functional Correctness] → ]8;;vscode://file//work/apps/price-server/src/env.ts:42apps/price-server/src/env.ts:42]8;; Reject partially parsed configuration numbers. Number.parseInt accepts a numeric prefix, so PRICE_INTERVAL_MINUTES=1.5 becomes 1 and PORT=3030junk becomes 3030. The port check also accepts values above 65535. Parse each complete value, require a safe integer, and restrict the port to 1–65535. This prevents the server from silently running with a different interval or port than the operator configured. Also applies to: 50-50 ──────────────────────────────────────────────────────────────────────── critical [Stability & Availability] → ]8;;vscode://file//work/price-server.Dockerfile:25price-server.Dockerfile:25]8;; Update the workspace lockfile for the new package. This stack adds apps/price-server/package.json but does not update pnpm-lock.yaml. The Docker install uses --frozen-lockfile, as does the new TeamCity job. A frozen install fails when the lockfile does not match the workspace manifest, so the image build and test job cannot install this package. Regenerate and commit the workspace lockfile with the new package. (pnpm.io) ──────────────────────────────────────── Critical 1 20 files reviewed:
... and 10 more files Print all AI prompts: coderabbit review --show-prompts Comment Actions Address CodeRabbit: declare chai, serialize refreshes, bound WS buffers, close WS on shutdown, strict env int parsing. Rebased onto updated D20727. Comment Actions Build Bitcoin ABC Diffs / Diff Testing (ai-review) passed. Diff : committed changes only (\(\ ──────────────────────────────────────────────────────────────────────── major [Stability & Availability]
→ ]8;;vscode://file//work/apps/price-server/src/priceWs.ts:44apps/price-server/src/priceWs.ts:44-49]8;;
Add an error listener to each client socket. A malformed frame can crash
the process.
ws emits 'error' on a client WebSocket when that client sends an
invalid frame. Examples are invalid UTF-8, a bad opcode, or a payload
larger than maxPayload. The code attaches no listener. Node therefore
throws the 'error' event as an uncaught exception, and the whole
price-server stops. Any remote client can trigger this.
This server only pushes data. Clients do not need to send large payloads.
Set a small maxPayload as well; the ws default is 100 MiB.
🐛 Proposed fix
- const wss = new WebSocketServer({ server });
+ const wss = new WebSocketServer({ server, maxPayload: 1024 });
@@
wss.on('connection', (socket: WebSocket) => {
+ socket.on('error', err => {
+ console.error('WebSocket client error:', err);
+ socket.terminate();
+ });
const latest = priceService.getLatest();──────────────────────────────────────── Major 1 20 files reviewed:
... and 10 more files Print all AI prompts: coderabbit review --show-prompts Comment Actions we may indeed want this but it is too low priority to deploy a new server for at the moment |