This repository has been archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (172 loc) · 4.22 KB
/
index.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
const EventEmitter = require('events');
const WebSocket = require('ws');
const OPCODES = require('./constants/OPCODES.js');
const uuid = require('./util/uuid.js');
class Worker extends EventEmitter {
constructor(host, secret) {
super();
this.host = host;
this.secret = secret;
this.attempts = 0;
this.range = null;
this.count = null;
this.connected = false;
this.shutdown = false;
}
/**
* Connects to the cluster manager
*/
connect() {
this.ws = new WebSocket(this.host);
this.ws.on('open', this.onConnect.bind(this));
this.ws.on('close', this.onDisconnect.bind(this));
this.ws.on('error', this.onError.bind(this));
}
/**
* Sends data to the websocket
* @param {Object?} obj The data to send
*/
sendWS(obj) {
this.ws.send(JSON.stringify(obj));
}
/**
* Called when an error happens
* @param {Error} error The error
*/
onError(error) {
this.emit('error', error);
}
/**
* Called when the websocket connects
*/
onConnect() {
this.attempts = 1;
this.connected = true;
this.emit('connect');
this.ws.on('message', msg => {
msg = JSON.parse(msg);
this.onMessage(msg);
});
}
/**
* Called when the websocket disconnects
* @param {number} code The disconnect code
* @param {string} message The message sent with the disconnect
*/
onDisconnect(code, message) {
if(this.shutdown) return;
this.emit('error', message);
this.connected = false;
this.attempts++;
this.connect();
}
/**
* Pings the cluster manager
* @param {string} [id=uuid()] An ID to reqresent the request
* @return {number} The time it took to ping in ms
*/
ping(id = uuid()) {
return new Promise((resolve, reject) => {
const startTime = new Date().getTime();
this.sendWS({
op: OPCODES.ping,
id
});
this.once(`pong_${id}`, msg => {
resolve(new Date().getTime() - startTime);
});
});
}
/**
* Makes a request
* @param {string} to Where to send the request to
* @param {Object} data The data to send
* @param {string} id The ID for the request
* @returns {string} id The ID for the request
*/
request(to, data, id = uuid()) {
if(!this.connected) return;
this.sendWS({
op: OPCODES.request,
to,
data,
id
});
return id;
}
/**
* Awaits a request to the gateway
* @param {string} to The ID to get data from
* @param {Object} data The data to send with the request
* @param {string} [id=uuid()] The ID for the request
* @return {Promise<Object>} The result of the request
*/
awaitRequest(to, data, id = uuid()) {
return new Promise((resolve, reject) => {
this.request(to, data, id);
this.once(`resolve_${id}`, resolve);
});
}
/**
* Resolves a request
* @param {string} id The ID of the request
* @param {Object} data The resolved data
*/
resolve(id, data) {
this.sendWS({
op: OPCODES.resolve,
id,
data
});
}
/**
* Called when the websocket recieves a message
* @param {Object} msg The message
*/
onMessage(msg) {
this.emit('message', msg);
switch(msg.op) {
case OPCODES.identify: {
this.sendWS({
op: OPCODES.identify,
secret: this.secret,
range: this.range ? this.range : 'new',
id: this.id
});
return;
}
case OPCODES.ready: {
this.range = msg.range;
this.count = msg.count;
this.id = msg.id;
this.emit('range', msg.range, msg.count);
return;
}
case OPCODES.shutdown: {
this.emit('shutdown');
this.shutdown = true;
this.ws.close();
return;
}
case OPCODES.ping: {
this.sendWS({
op: OPCODES.pong,
id: msg.id
});
return;
}
case OPCODES.pong: {
this.emit(`pong_${msg.id}`, msg);
return;
}
case OPCODES.resolve: {
this.emit(`resolve_${msg.id}`, msg);
return;
}
case OPCODES.request: {
this.emit('request', msg.id, msg.data);
}
}
}
}
module.exports = Worker;