Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
182 lines (159 loc) · 5.32 KB
/
Copy pathserver.ts
File metadata and controls
182 lines (159 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import crypto from 'node:crypto';
import { createServer } from 'node:http';
import next from 'next';
import postgres from 'postgres';
import { WebSocketServer, type WebSocket } from 'ws';
import { applyCommittedMigrations } from './app/lib/db/migrations';
import { markNodeSeen } from './app/lib/nodes/mark-seen';
import { addConnection, removeConnection } from './app/lib/ws/connection-manager';
import { createNodeSocketHandler } from './app/lib/ws/node-socket';
import type { WsEventPayload } from './app/lib/ws/protocol';
type AckResultRow = { id: string };
const dev = process.env.NODE_ENV !== 'production';
const hostname = process.env.HOSTNAME ?? '0.0.0.0';
const port = parseInt(process.env.PORT ?? '3000', 10);
const DEFAULT_LEASE_DURATION_SECONDS = 60;
const AUTH_TIMEOUT_MS = 10_000;
const PING_INTERVAL_MS = 30_000;
const SEEN_THROTTLE_MS = 60_000;
const WS_PATH = '/api/nodes/ws';
function sslMode(): 'require' | false {
const mode = process.env.DATABASE_SSL?.trim().toLowerCase();
if (mode === 'disable' || mode === 'false') return false;
return 'require';
}
function createDb() {
const url = process.env.DATABASE_URL?.trim();
if (!url) throw new Error('DATABASE_URL is not set');
return postgres(url, { ssl: sslMode() });
}
const db = createDb();
function hashNodeToken(token: string): string {
return crypto.createHash('sha256').update(token).digest('hex');
}
function validateAckEventIds(eventIds: unknown): string[] {
if (!Array.isArray(eventIds) || eventIds.length === 0) {
throw new Error('eventIds are required');
}
const seen = new Set<string>();
const result: string[] = [];
for (const id of eventIds) {
if (typeof id !== 'string' || id.trim() === '') {
throw new Error('eventIds must be non-empty strings');
}
const trimmed = id.trim();
if (!seen.has(trimmed)) {
seen.add(trimmed);
result.push(trimmed);
}
}
return result;
}
async function authenticateToken(token: string): Promise<{ id: string } | null> {
const tokenHash = hashNodeToken(token);
const rows = await db<AckResultRow[]>`
SELECT id, status FROM nodes WHERE token_hash = ${tokenHash} LIMIT 1
`;
const node = rows[0] as { id: string; status: string } | undefined;
if (!node || node.status !== 'active') return null;
return { id: node.id };
}
const DEFAULT_MAX_RETRY_ATTEMPTS = 5;
async function pushPendingEventsForNode(ws: WebSocket, nodeId: string): Promise<void> {
const rows = await db<WsEventPayload[]>`
WITH candidates AS (
SELECT e.id
FROM events e
WHERE e.node_id = ${nodeId}
AND e.expires_at > now()
AND e.attempt_count < ${DEFAULT_MAX_RETRY_ATTEMPTS}
AND (
e.status = 'pending'
OR (e.status = 'leased' AND e.lease_expires_at <= now())
)
ORDER BY e.received_at ASC
)
UPDATE events e
SET
status = 'leased',
lease_expires_at = now() + (${DEFAULT_LEASE_DURATION_SECONDS} * interval '1 second'),
attempt_count = e.attempt_count + 1,
updated_at = now()
FROM candidates c
WHERE e.id = c.id
RETURNING
e.id,
e.route_id AS "routeId",
(SELECT r.slug FROM routes r WHERE r.id = e.route_id) AS "routeSlug",
e.headers_json AS headers,
e.body_text AS body,
e.content_type AS "contentType",
e.received_at::text AS "receivedAt",
e.lease_expires_at::text AS "leaseExpiresAt",
e.attempt_count AS "attemptCount"
`;
for (const event of rows) {
ws.send(JSON.stringify({ type: 'event', ...event }));
}
}
async function handleAck(ws: WebSocket, nodeId: string, eventIds: unknown): Promise<void> {
let validated: string[];
try {
validated = validateAckEventIds(eventIds);
} catch (err) {
ws.send(JSON.stringify({ type: 'ack_error', error: (err as Error).message }));
return;
}
const rows = await db`
UPDATE events
SET
status = 'delivered',
acked_at = now(),
updated_at = now()
WHERE node_id = ${nodeId}
AND status = 'leased'
AND id IN ${db(validated)}
RETURNING id
`;
const ackedIds = rows.map((r) => r.id);
ws.send(JSON.stringify({ type: 'ack_ok', acked: ackedIds.length, eventIds: ackedIds }));
}
const handleNodeWebSocket = createNodeSocketHandler({
authenticateToken,
markSeen: (nodeId) => markNodeSeen(db, nodeId),
pushPendingEvents: pushPendingEventsForNode,
handleAck,
addConnection,
removeConnection,
authTimeoutMs: AUTH_TIMEOUT_MS,
pingIntervalMs: PING_INTERVAL_MS,
seenThrottleMs: SEEN_THROTTLE_MS,
});
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
async function main() {
await applyCommittedMigrations(db);
await app.prepare();
const httpServer = createServer((req, res) => {
handle(req, res);
});
const wss = new WebSocketServer({ noServer: true });
wss.on('connection', handleNodeWebSocket);
httpServer.on('upgrade', (req, socket, head) => {
const url = new URL(req.url ?? '', `http://${req.headers.host ?? 'localhost'}`);
if (url.pathname === WS_PATH) {
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
} else {
socket.destroy();
}
});
httpServer.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
}
main().catch((error) => {
console.error('[server] failed to start', error);
process.exit(1);
});