-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappNetwork.js
196 lines (181 loc) · 6.86 KB
/
appNetwork.js
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
const net = require('node:net');
const crypto = require('node:crypto');
const {inspect} = require('node:util');
const hash = d => crypto.createHash('sha1').update(d).digest('hex');
class App {
constructor (addr) {
this.requests = [];
this.events = [];
this.responses = {};
this.client = net.createConnection(addr);
this.buff = '';
this.client.on('data',async rd=>{rd=rd.toString('utf-8');
this.buff += rd;
while (this.buff.includes('\x17')) {
let d = this.buff.slice(0,this.buff.indexOf('\x17'));
this.buff = this.buff.slice(this.buff.indexOf('\x17')+1);
let jd = JSON.parse(d);
if (jd.type == 'connect_response') {
throw Error((jd.message??'')+'\n'+inspect(jd,{colors:true,depth:5}));
}
if (jd.type == 'request_response') {
this.responses[jd.id] = jd.data;
}
if (jd.type == 'event') {
let h = this.events.find(e=>e.name==jd.name);
h.callback(jd.data);
}
}
});
}
/**
* Registers a request type
* @param {string} name the name of the request
* @param {boolean} requiresResponse whether the client should be expecting a response
* @param {(payload:object,rawData:object)=>undefined|object} callback the function to be called when the request has been received
* @returns
*/
addRequest(name,requiresResponse) {
this.requests.push({name,requiresResponse});
return this;
}
/**
* Registers a callback for an event
* @param {string} name the name of the event
* @param {(object)=>void} callback the function to be called when the server fires this event
* @returns
*/
addEvent(name,callback) {
this.events.push({name,callback});
return this;
}
/**
* Sends a request
* @param {string} name the name of the request to be sent
* @param {object} data the payload to be sent alongside with the request
* @param {(object)=>void} callback a function to be called when the server responds (if it has to)
*/
sendRequest(name,data,callback=()=>{}) {
let d = JSON.stringify({
type: 'request',
name,
data
});
let dh = hash(d);
this.client.write(d+'\x17');
if (this.requests.find(r=>r.name==name).requiresResponse) {
return new Promise(r=>{
let i = setInterval(()=>{
if (this.responses[dh]) {
clearInterval(i);
r(this.responses[dh]);
callback(this.responses[dh]);
delete this.responses[dh];
}
});
});
}
}
}
class Server {
constructor (creationCb) {
this.requests = [];
/** @type {net.Socket[]} */
this.clients = [];
this.connectionListener = null;
this.disconnectionListener = null;
this.server = net.createServer(
async s => {
this.clients.push(s);
let r = await (this.connectionListener??(()=>undefined)).call(this,s);
if (!r) {
s.on('data',async rd => { rd = rd.toString('utf-8');
for (let d of rd.split('\x17')) if (d.length) {
let dh = hash(d);
let jd = JSON.parse(d);
if (jd.type == 'request') {
let h = this.requests.find(r=>r.name==jd.name);
if (h) {
let r = await h.callback(jd.data,s);
if (h.requiresResponse) {
s.write(JSON.stringify({
type : 'request_response',
data : r,
id : dh
})+'\x17');
}
}
}
}
});
s.on('error',()=>{
this.clients = this.clients.filter(c=>c!=s);
});
s.on('close',()=>{
this.clients = this.clients.filter(c=>c!=s);
if (typeof this.disconnectionListener == 'function') this.disconnectionListener(s);
});
} else {
s.write(JSON.stringify({
type : 'connect_response',
error : 'Could not connect',
message : r
})+'\x17');
this.clients = this.clients.filter(c=>c!=s);
s.end();
}
}
).listen(()=>{
creationCb(this.server.address());
});
}
/**
* Registers the callback for accepting users
* the function should return undeifned if the user was accepted
* and a string indicating why otherwise
* @param {(s:net.Socket)=>object|undefined} l
* @returns {Server}
*/
onConnection(l) {
this.connectionListener = l;
return this;
}
/**
* Registers a callback for when a user is disconnected
* @param {(s:net.Socekt)=>void} l
* @returns {Server}
*/
onDisconnect(l) {
this.disconnectionListener = l;
return this;
}
/**
* Registers a callback for a request
* @param {string} name the name of the request
* @param {boolean} requiresResponse whether the client should be expecting a response
* @param {(payload:any,emmitter:net.Socket)=>undefined|object} callback the function to be called when the request has been received
* @returns
*/
addRequest(name,requiresResponse,callback) {
this.requests.push({name,requiresResponse,callback});
return this;
}
/**
* Dispatches an event to all clients
* @param {string} name the name of the event to be dispatched
* @param {object} data the payload associated with the event
* @param {net.Socket[]} clients allows overriding the clients to send the event to
*/
dispatchEvent(name,data,clients=this.clients) {
let d = JSON.stringify({
type: 'event',
name, data
})+'\x17';
for (let c of clients) {
c.write(d);
}
}
}
module.exports = {
App, Server
}