-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.ts
244 lines (207 loc) · 5.65 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import net from 'net';
import { Kind, Msg, Parser } from './parser';
import { parseSub, parseUnsubArg } from './utils';
import { Client, ClientOptions } from './client';
import { v4 as uuidv4 } from 'uuid';
import Topic from './topic';
import Subscription from './subscription';
/**
* The required information corresponding to a NATS server.
* Refer to https://docs.nats.io/reference/reference-protocols/nats-protocol#info
*
* @interface ServerInfo
*/
interface ServerInfo {
server_id: string;
server_name: string;
version: string;
go: string;
host: string;
port: number;
headers: boolean;
max_payload: number;
proto: number;
}
export default class NATSServer {
/**
* The net.Server instance on which the NATS server will start listening to
* connections.
*
* @type {net.Server}
*/
server: net.Server;
/**
* A map of clients where the key is created using the remoteAddress and
* remotePort of the net.Socket instance.
*
* @private
* @type {Map<string, Client>}
*/
private clients: Map<string, Client>;
/**
* A map of topics where the key is the subject.
*
* @private
* @type {Map<string, Topic>}
*/
private topics: Map<string, Topic>;
/**
* A map of subscriptions where the key is the sid param.
*
* @private
* @type {Map<number, Subscription>}
*/
private subscriptions: Map<number, Subscription>;
private debug: boolean;
private serverInfo: ServerInfo;
constructor(port: number, host: string = '0.0.0.0', debug: boolean = false) {
this.server = new net.Server();
this.clients = new Map<string, Client>();
this.topics = new Map<string, Topic>();
this.debug = debug;
this.serverInfo = {
server_id: uuidv4().replaceAll('-', ''),
server_name: host,
version: '2.6.1',
go: 'go1.21.1',
host,
port,
headers: false,
max_payload: 1024 * 30,
proto: 1
};
this.subscriptions = new Map<number, Subscription>();
}
startServer(): void {
this.server.listen(this.serverInfo.port, this.serverInfo.host, () => {
if (this.debug) {
console.log(`Started listening on port ${this.serverInfo.port}`);
}
});
this.server.on('error', (err) => {
console.error(err);
});
this.server.on('connection', (socket) => {
const key = `${socket.remoteAddress}:${socket.remotePort}`;
if (this.debug) {
console.log('Client connect %s', key);
}
// Create a new client instance
const client: Client = new Client(key, socket);
const cb = (msg: Msg) => {
this.handleMessage(msg, client);
};
const parser = new Parser(cb);
socket.on('close', () => {
if (this.debug) {
console.log('Client disconnected %s', key);
}
this.clients.delete(key);
});
socket.on('error', (err) => {
console.error(err);
});
socket.on('data', (data) => {
if (this.debug) {
console.log('%s', data);
}
try {
parser.parse(data);
} catch (e) {
console.error(e);
}
});
this.sendInfo(client);
});
this;
}
private async handleMessage(msg: Msg, client: Client) {
switch (msg.kind) {
case Kind.PING:
client.sendPong();
break;
case Kind.CONNECT:
this.handleConnect(msg, client);
break;
case Kind.SUB:
this.handleSub(msg, client);
break;
case Kind.PUB:
await this.handlePub(msg, client);
break;
case Kind.UNSUB:
this.handleUnsub(msg, client);
}
}
private handleConnect(msg: Msg, client: Client) {
const newOptions: ClientOptions = JSON.parse(msg.data!.toString());
client.updateOptions(newOptions);
this.clients.set(client.key, client);
client.sendOk();
}
private handleSub(msg: Msg, client: Client) {
const subArg = parseSub(msg.data!);
const subject = subArg.subject.toString();
const topicKey = subject;
let topic = this.topics.get(topicKey);
const subscription = new Subscription(client, subArg.sid, subject);
// If the topic is already present
if (topic) {
topic.sub(subscription);
}
// otherwise create a new topic
else {
topic = new Topic(subject);
topic.sub(subscription);
this.topics.set(topicKey, topic);
}
this.subscriptions.set(subArg.sid, subscription);
client.sendOk();
}
private handleUnsub(msg: Msg, client: Client) {
const unsubArg = parseUnsubArg(msg.data!);
const subscription = this.subscriptions.get(unsubArg.sid);
if (subscription === undefined) {
// TODO: handle when invalid sid is provided
return;
}
const topic = this.topics.get(subscription.subject);
if (topic === undefined) {
// TODO: handle when no topic present
return;
}
topic.unsub(subscription);
client.sendOk();
}
private async handlePub(msg: Msg, client: Client): Promise<void> {
const pubArg = msg.pubArg!;
const subject = pubArg.subject.toString();
const topic = this.topics.get(subject);
if (topic === undefined) {
// TODO: Handle when unknown topic found
return;
}
client.sendOk();
await topic.publish(pubArg);
}
/**
* Send an INFO command to the client.
*
* @private
* @param {Client} client
*/
private sendInfo(client: Client) {
client.socket.write(`INFO ${JSON.stringify(this.serverInfo)}\r\n`);
}
async stopServer(): Promise<void> {
return new Promise<void>((res, rej) => {
this.server.close((err) => {
if (err) {
rej(err);
return;
}
res();
});
});
}
}