-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy.io.js
246 lines (218 loc) · 6.87 KB
/
lazy.io.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
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
245
246
'use strict'
/** @constant {Object} http requiring node's http module */
const http = require('http');
/** @constant {Object} io requiring socket.io module */
const io = require('socket.io')(4000);
/** @constant {Object} LazyTypes requiring Lazy types library */
const {LazyTypes} = require('./lazy.utils');
/** @constant {Object} io_config socket.io configuration */
const IO_DEFAULT = {
PING_INTERVAL: 10000,
PING_TIMEOUT: 5000,
COOKIE: true
}
// /**
// * List of allowed API methods
// * @constant {Array} ALLOWED_IO_METHODS
// * @todo: Make that global ?
// **/
// const ALLOWED_IO_METHODS = [
// "get",
// "list",
// "post",
// "patch",
// "delete"
// ];
// /**
// * @class
// * Describes a Lazy IO endpoint
// */
// class LazyIOEndpoint{
// /**
// *
// * @param {String} room Endpoint broadcast room
// * @param {String} message Endpoint message
// */
// constructor(route, method){
// this.route = route;
// this.method = method;
// }
// set route(route){
// if(!LazyTypes.isString(route)){
// throw new Error(`LazyIOEndpoint "route" argument should be a string. got ${LazyTypes.getFullType(route)}.`);
// }
// this._route = route;
// }
// set method(method){
// if(!ALLOWED_IO_METHODS.includes(method)){
// throw new Error(`Invalid LazyIOEndpoint "method" parameter. Valid .`);
// }else if(!LazyTypes.isString(method)){
// throw new Error(`LazyIOEndpoint "method" argument should be a string. Valid values: ${ALLOWED_IO_METHODS.map(method).join(',')}.`);
// }
// this._method = method;
// }
// /**
// * Endpoint route
// * @type {String}
// */
// get route(){
// return this._route;
// }
// /**
// * Endpoint method
// * @see ALLOWED_IO_METHODS
// * @type {String}
// */
// get method(){
// return this._method;
// }
// /**
// * Brodcasts an endpoint message to the specified room
// * @param {any} data
// */
// broadcast(room, data){
// LazyIOInstance.broadcast(room, `${this.method}-${this.route}`, data);
// }
// }
/**
* Describes IO wrapper
* TODO: maybe remove this ? is it useseful ? Maybe leave it as static class ?
* @class
* @requires io
* @requires LazyTypes
* @todo This class is still case specific for roomz.
* Standard message listening and sending should be implemented in next iteration
*/
class LazyIO{
/**
* IO wrapper contructor
* @constructs IO
*/
constructor(){
if(!LazyIOInstance){
LazyIOInstance = this;
}
return LazyIOInstance;
}
set pingInterval(pingInterval = IO_DEFAULT.PING_INTERVAL){
if(!LazyTypes.isNumber(pingInterval)){
throw new Error(`Lazy's IO "pingInterval" parameter should be a number. got ${LazyTypes.getFullType(pingInterval)}.`);
}
this._pingInterval = pingInterval;
}
set pingTimeout(pingTimeout = IO_DEFAULT.PING_TIMEOUT){
if(!LazyTypes.isNumber(pingTimeout)){
throw new Error(`Lazy's IO "pingTimeout" parameter should be a number. got ${LazyTypes.getFullType(pingTimeout)}.`);
}
this._pingTimeout = pingTimeout;
}
set useCookie(useCookie = IO_DEFAULT.COOKIE){
if(!LazyTypes.isBoolean(useCookie)){
throw new Error(`Lazy's IO "useCookie" parameter should be a number. got ${LazyTypes.getFullType(useCookie)}.`);
}
this._useCookie = useCookie;
}
set server(server){
if(this._server){
throw new Error(`Cannot reset server at runtime`);
}else if(!(server instanceof http.Server)){
throw new Error(`server should be an instabnce of Server. got ${server.constructor.name}`);
}
this._server = server;
}
set authFunction(authFunction = () => {}){
if(!LazyTypes.isFunction(authFunction)){
throw new Error(`Lazy's IO "authFunction" parameter should be a function. got ${LazyTypes.getFullType(authFunction)}.`);
}
this._authFunction = authFunction;
}
set onConnect(onConnect){
if(!LazyTypes.isFunction(onConnect)){
throw new Error(`Lazy's IO "onConnect" parameter should be a function. got ${LazyTypes.getFullType(onConnect)}.`);
}
this._onConnect = onConnect;
}
/**
* HTTP server instance
* @type {Server}
*/
get server(){
return this._server || null;
}
/**
* IO middleware authentication function
* @type {Function}
*/
get authFunction(){
return this._authFunction;
}
/**
* IO middleware connection function
* @type {Function}
*/
get onConnect(){
return this._onConnect;
}
get isInit(){
return this._isInit || false;
}
/**
* IO configuration object
* @type {Object}
*/
get config(){
return {
pingInterval: this._pingInterval,
pingTimeout: this._pingTimeout,
// cookie: this._useCookie
}
}
/**
* Initialises IO wrapper
* @method init
* @public
* @param {Server} server Handle to server instance
* @param {Function} auth Authentication middleware function
* @param {Function} onConnect Socket connection middleware function
*/
init(server, authFunction, onConnect, pingInterval, pingTimeout, useCookie) {
if(!this.server){
this.server = server;
this.authFunction = authFunction;
this.pingInterval = pingInterval;
this.pingTimeout = pingTimeout;
this.useCookie = useCookie;
this.onConnect = onConnect;
io.attach(this.server, this.config);
io.on('connection', this.onConnect);
io.use(this.authFunction);
this._isInit = true;
}else{
throw new Error("Cannot re-initialise LazyIO singleton instance at runtime.");
}
}
/**
* Brodcast message to specific room
* @method broadcast
* @public
* @param {String} room io room identifier
* @param {String} message brodcast message
* @param {Object} data brodcast message data
*/
broadcast(room, message, data){
if(!this.isInit){
throw new Error("LazyIO is uninitialised.")
}else if(!LazyTypes.isString(room)){
throw new Error(`"room" argument should be a string. got ${LazyTypes.getFullType(room)}.`);
}else if(!LazyTypes.isString(message)){
throw new Error(`"message" argument should be a string. got ${LazyTypes.getFullType(room)}.`);
}
io.to(room).emit(message, data);
}
}
var LazyIOInstance = new LazyIO();
module.exports = LazyIOInstance;
// module.exports = {
// LazyIO: LazyIOInstance,
// LazyIOEndpoint: LazyIOEndpoint
// };