-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
153 lines (133 loc) · 4.36 KB
/
server.ts
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
/**
* @file WebSocket server implementation
* @module core/server
*/
import { WebSocket, WebSocketServer } from 'ws';
import { createLogger } from '../utils/logger.js';
import { createRateLimiter } from '../utils/rate-limiter.js';
import { NostrWSServerOptions, NostrWSServerSocket } from '../types/websocket.js';
import { NostrWSMessage } from '../types/messages.js';
import { RateLimiter } from '../utils/rate-limiter.js';
import { v4 as uuidv4 } from 'uuid';
const logger = createLogger('NostrWSServer');
/**
* NostrWSServer class for handling WebSocket connections
*/
export class NostrWSServer {
private wss: WebSocketServer;
private options: NostrWSServerOptions;
private rateLimiter?: RateLimiter;
private pingInterval?: NodeJS.Timeout;
constructor(options: NostrWSServerOptions) {
this.options = {
...options,
port: options.port || 8080,
host: options.host || 'localhost',
maxConnections: options.maxConnections || 1000,
pingInterval: options.pingInterval || 30000,
rateLimits: options.rateLimits
};
this.wss = new WebSocketServer({
port: this.options.port,
host: this.options.host
});
this.setupServer();
this.startPingInterval();
if (this.options.rateLimits) {
this.rateLimiter = createRateLimiter({
EVENT: this.options.rateLimits
}, logger);
}
}
/**
* Set up WebSocket server event handlers
*/
private setupServer(): void {
this.wss.on('connection', async (ws: WebSocket) => {
const socket = ws as NostrWSServerSocket;
socket.clientId = uuidv4();
socket.subscriptions = new Set();
socket.isAlive = true;
logger.info(`Client connected: ${socket.clientId}`);
if (this.wss.clients.size > this.options.maxConnections!) {
logger.warn(`Max connections (${this.options.maxConnections}) reached`);
socket.close(1008, 'Max connections reached');
return;
}
socket.on('message', async (data: Buffer) => {
try {
const rawMessage = data.toString();
await this.handleMessage(socket, rawMessage);
} catch (error) {
logger.error('Error processing message:', error);
this.options.onError?.(error as Error, socket);
}
});
socket.on('error', (error: Error) => {
logger.error(`Client error (${socket.clientId}):`, error);
this.options.onError?.(error, socket);
});
socket.on('close', () => {
logger.info(`Client disconnected: ${socket.clientId}`);
this.options.onClose?.(socket);
});
socket.on('pong', () => {
socket.isAlive = true;
});
await this.options.onConnection?.(socket);
});
}
private async handleMessage(socket: NostrWSServerSocket, rawMessage: string) {
try {
// Parse the message as a NostrWSMessage tuple
const message = JSON.parse(rawMessage) as NostrWSMessage;
// Rate limiting check
if (this.rateLimiter && await this.rateLimiter.shouldLimit(socket.clientId, message)) {
logger.debug(`Rate limit exceeded for client ${socket.clientId}`);
socket.send(JSON.stringify(['NOTICE', 'Rate limit exceeded']));
return;
}
// Process message
try {
await this.options.onMessage?.(message, socket);
} catch (error) {
this.options.onError?.(error as Error, socket);
}
} catch (error) {
this.options.onError?.(error as Error, socket);
}
}
/**
* Start ping interval to check client connections
*/
private startPingInterval(): void {
if (this.options.pingInterval) {
this.pingInterval = setInterval(() => {
this.wss.clients.forEach((socket: WebSocket) => {
const nostrSocket = socket as NostrWSServerSocket;
if (!nostrSocket.isAlive) {
nostrSocket.terminate();
return;
}
nostrSocket.isAlive = false;
nostrSocket.ping();
});
}, this.options.pingInterval);
}
}
/**
* Stop the server and clean up resources
*/
public stop(): void {
if (this.pingInterval) {
clearInterval(this.pingInterval);
}
this.wss.clients.forEach((socket: WebSocket) => {
const nostrSocket = socket as NostrWSServerSocket;
if (nostrSocket.readyState === WebSocket.OPEN) {
nostrSocket.close();
}
});
this.wss.close();
}
}