ioserver 1.5.3 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +244 -287
- package/dist/BaseClasses.d.ts +301 -0
- package/dist/BaseClasses.d.ts.map +1 -0
- package/dist/BaseClasses.js +281 -0
- package/dist/BaseClasses.js.map +1 -0
- package/dist/IOServer.d.ts +260 -0
- package/dist/IOServer.d.ts.map +1 -0
- package/dist/IOServer.js +656 -0
- package/dist/IOServer.js.map +1 -0
- package/dist/IOServerError.d.ts +80 -0
- package/dist/IOServerError.d.ts.map +1 -0
- package/dist/IOServerError.js +85 -0
- package/dist/IOServerError.js.map +1 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -45
- package/build/ioserver.js +0 -580
- package/build/ioserver.js.map +0 -13
package/build/ioserver.js
DELETED
@@ -1,580 +0,0 @@
|
|
1
|
-
(function() {
|
2
|
-
//###################################################
|
3
|
-
// IOServer - v1.5.3 #
|
4
|
-
// #
|
5
|
-
// Damn simple socket.io server #
|
6
|
-
//###################################################
|
7
|
-
// - Copyright 2023 - #
|
8
|
-
// #
|
9
|
-
// License: Apache v 2.0 #
|
10
|
-
// @Author: Ben Mz #
|
11
|
-
// @Email: 0x42en (at) users.noreply.github.com #
|
12
|
-
// #
|
13
|
-
//###################################################
|
14
|
-
|
15
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
-
// you may not use this file except in compliance with the License.
|
17
|
-
// You may obtain a copy of the License at
|
18
|
-
|
19
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
20
|
-
|
21
|
-
// Unless required by applicable law or agreed to in writing, software
|
22
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
-
// See the License for the specific language governing permissions and
|
25
|
-
// limitations under the License.
|
26
|
-
|
27
|
-
// Add required packages
|
28
|
-
var HOST, IOServer, IOServerError, LOG_LEVEL, PORT, RESERVED_NAMES, TRANSPORTS, VERSION, fastify, fs, path,
|
29
|
-
indexOf = [].indexOf;
|
30
|
-
|
31
|
-
fs = require('fs');
|
32
|
-
|
33
|
-
path = require('path');
|
34
|
-
|
35
|
-
fastify = require('fastify');
|
36
|
-
|
37
|
-
// Set global vars
|
38
|
-
VERSION = '1.4.2';
|
39
|
-
|
40
|
-
PORT = 8080;
|
41
|
-
|
42
|
-
HOST = 'localhost';
|
43
|
-
|
44
|
-
LOG_LEVEL = ['EMERGENCY', 'ALERT', 'CRITICAL', 'ERROR', 'WARNING', 'NOTIFICATION', 'INFORMATION', 'DEBUG'];
|
45
|
-
|
46
|
-
TRANSPORTS = ['websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling'];
|
47
|
-
|
48
|
-
RESERVED_NAMES = ['send', 'log', 'verbose'];
|
49
|
-
|
50
|
-
module.exports = IOServer = class IOServer {
|
51
|
-
// Define the variables used by the server
|
52
|
-
constructor(options = {}) {
|
53
|
-
var _cookie, _cors, _mode, cookie, cors, default_routes, err, host, i, m, mode, port, ref, ref1, ref2, routes, verbose;
|
54
|
-
// Allow sending message from external app
|
55
|
-
this.sendTo = this.sendTo.bind(this);
|
56
|
-
// Set default options
|
57
|
-
({verbose, host, port, cookie, mode, cors, routes} = options);
|
58
|
-
this.host = host ? String(host) : HOST;
|
59
|
-
try {
|
60
|
-
this.port = port ? Number(port) : PORT;
|
61
|
-
if (this.port <= 0) {
|
62
|
-
throw new Error('Invalid port');
|
63
|
-
}
|
64
|
-
if (this.port > 65535) {
|
65
|
-
throw new Error('Invalid port');
|
66
|
-
}
|
67
|
-
} catch (error1) {
|
68
|
-
err = error1;
|
69
|
-
throw new Error(err);
|
70
|
-
}
|
71
|
-
_cookie = Boolean(cookie) ? Boolean(cookie) : false;
|
72
|
-
this.verbose = (ref = String(verbose).toUpperCase(), indexOf.call(LOG_LEVEL, ref) >= 0) ? String(verbose).toUpperCase() : 'ERROR';
|
73
|
-
// Does not yell if route directory does not exists... change that ?
|
74
|
-
default_routes = path.join(process.cwd(), 'routes');
|
75
|
-
this._routes = fs.existsSync(routes) ? String(routes) : default_routes;
|
76
|
-
|
77
|
-
// Process transport mode options
|
78
|
-
_mode = [];
|
79
|
-
if (mode) {
|
80
|
-
if (ref1 = String(mode).toLowerCase(), indexOf.call(TRANSPORTS, ref1) >= 0) {
|
81
|
-
_mode.push(String(mode).toLowerCase());
|
82
|
-
} else if (mode.constructor === Array) {
|
83
|
-
for (i in mode) {
|
84
|
-
m = mode[i];
|
85
|
-
if (ref2 = String(m).toLowerCase(), indexOf.call(TRANSPORTS, ref2) >= 0) {
|
86
|
-
_mode.push(m);
|
87
|
-
}
|
88
|
-
}
|
89
|
-
}
|
90
|
-
} else {
|
91
|
-
_mode.push('websocket');
|
92
|
-
_mode.push('polling');
|
93
|
-
}
|
94
|
-
|
95
|
-
// Setup CORS since necessary in socket.io v3
|
96
|
-
_cors = (cors != null) && cors ? cors : {};
|
97
|
-
if (!_cors.methods) {
|
98
|
-
_cors.methods = ['GET', 'POST'];
|
99
|
-
}
|
100
|
-
if (!_cors.origin) {
|
101
|
-
_cors.origin = [`https://${this.host}`, `http://${this.host}`];
|
102
|
-
}
|
103
|
-
|
104
|
-
// Setup internal lists
|
105
|
-
this.service_list = {};
|
106
|
-
this.manager_list = {};
|
107
|
-
this.method_list = {};
|
108
|
-
this.watcher_list = {};
|
109
|
-
this.controller_list = {};
|
110
|
-
this.middleware_list = {};
|
111
|
-
try {
|
112
|
-
// Instanciate server (needed to register controllers)
|
113
|
-
this._webapp = fastify({
|
114
|
-
logger: this.verbose,
|
115
|
-
ignoreTrailingSlash: true,
|
116
|
-
maxParamLength: 200,
|
117
|
-
caseSensitive: true
|
118
|
-
});
|
119
|
-
} catch (error1) {
|
120
|
-
err = error1;
|
121
|
-
throw new Error(`[!] Unable to instanciate server: ${err}`);
|
122
|
-
}
|
123
|
-
try {
|
124
|
-
// Register standard HTTP error shortcuts
|
125
|
-
this._webapp.register(require('@fastify/sensible'), {
|
126
|
-
errorHandler: false
|
127
|
-
});
|
128
|
-
} catch (error1) {
|
129
|
-
err = error1;
|
130
|
-
throw new Error(`[!] Unable to register sensible plugin: ${err}`);
|
131
|
-
}
|
132
|
-
try {
|
133
|
-
// Allow developper to use throw Error directly in methods
|
134
|
-
this._webapp.setErrorHandler(function(error, req, reply) {
|
135
|
-
var code;
|
136
|
-
// Handle IOServerError
|
137
|
-
if (error instanceof IOServerError) {
|
138
|
-
code = error.getCode() < 0 ? 500 : error.getCode();
|
139
|
-
return reply.status(code).send(error);
|
140
|
-
// Handle HTTPErrors
|
141
|
-
} else if ((error.status != null)) {
|
142
|
-
return reply.status(error.status).send({
|
143
|
-
message: error.message
|
144
|
-
});
|
145
|
-
} else {
|
146
|
-
// Handle standard Error
|
147
|
-
return reply.status(500).send(error.message);
|
148
|
-
}
|
149
|
-
});
|
150
|
-
} catch (error1) {
|
151
|
-
err = error1;
|
152
|
-
throw new Error(`[!] Unable to register error handler: ${err}`);
|
153
|
-
}
|
154
|
-
try {
|
155
|
-
// Register standard HTTP error shortcuts
|
156
|
-
this._webapp.register(require('@fastify/cors'), _cors);
|
157
|
-
} catch (error1) {
|
158
|
-
err = error1;
|
159
|
-
throw new Error(`[!] Unable to register CORS plugin: ${err}`);
|
160
|
-
}
|
161
|
-
try {
|
162
|
-
// Register socket.io listener
|
163
|
-
this._webapp.register(require('fastify-socket.io'), {
|
164
|
-
transports: _mode,
|
165
|
-
cookie: _cookie,
|
166
|
-
cors: _cors
|
167
|
-
});
|
168
|
-
} catch (error1) {
|
169
|
-
err = error1;
|
170
|
-
throw new Error(`[!] Unable to register socket.io plugin: ${err}`);
|
171
|
-
}
|
172
|
-
// Register the global app handle
|
173
|
-
// that will be passed to all entities
|
174
|
-
this.appHandle = {
|
175
|
-
send: this.sendTo,
|
176
|
-
log: this._logify,
|
177
|
-
verbose: this.verbose
|
178
|
-
};
|
179
|
-
}
|
180
|
-
|
181
|
-
_logify(level, text) {
|
182
|
-
var current_level;
|
183
|
-
current_level = LOG_LEVEL.indexOf(this.verbose);
|
184
|
-
if (level <= current_level) {
|
185
|
-
if (level <= 4) {
|
186
|
-
return console.error(text);
|
187
|
-
} else {
|
188
|
-
return console.log(text);
|
189
|
-
}
|
190
|
-
}
|
191
|
-
}
|
192
|
-
|
193
|
-
_unique(arr) {
|
194
|
-
var hash, i, l, result;
|
195
|
-
hash = {};
|
196
|
-
result = [];
|
197
|
-
i = 0;
|
198
|
-
l = arr.length;
|
199
|
-
while (i < l) {
|
200
|
-
if (!hash.hasOwnProperty(arr[i])) {
|
201
|
-
hash[arr[i]] = true;
|
202
|
-
result.push(arr[i]);
|
203
|
-
}
|
204
|
-
++i;
|
205
|
-
}
|
206
|
-
return result;
|
207
|
-
}
|
208
|
-
|
209
|
-
_method_exists(klass, name) {
|
210
|
-
return klass[name] != null;
|
211
|
-
}
|
212
|
-
|
213
|
-
_register_internal_class(type, name, klass) {
|
214
|
-
var err;
|
215
|
-
if (!name) {
|
216
|
-
throw new Error("name is mandatory");
|
217
|
-
}
|
218
|
-
if ((type !== 'service') && name.length < 2) {
|
219
|
-
throw new Error("name MUST be longer than 2 characters");
|
220
|
-
}
|
221
|
-
if (indexOf.call(RESERVED_NAMES, name) >= 0) {
|
222
|
-
throw new Error("Sorry this is a reserved name");
|
223
|
-
}
|
224
|
-
if (!(klass && klass.prototype)) {
|
225
|
-
throw new Error("MUST be a function");
|
226
|
-
}
|
227
|
-
if (this[`${type}_list`][name] != null) {
|
228
|
-
throw new Error(`Sorry this ${type} already exists`);
|
229
|
-
}
|
230
|
-
try {
|
231
|
-
// Register klass with handle reference
|
232
|
-
this._logify(7, `[*] Register ${type} ${name}`);
|
233
|
-
return this[`${type}_list`][name] = new klass(this.appHandle);
|
234
|
-
} catch (error1) {
|
235
|
-
err = error1;
|
236
|
-
throw new Error(err);
|
237
|
-
}
|
238
|
-
}
|
239
|
-
|
240
|
-
addWatcher({name, watcher}) {
|
241
|
-
var err;
|
242
|
-
try {
|
243
|
-
return this._register_internal_class('watcher', name, watcher);
|
244
|
-
} catch (error1) {
|
245
|
-
err = error1;
|
246
|
-
throw new Error(`[!] Error while instantiate ${name} watcher -> ${err}`);
|
247
|
-
}
|
248
|
-
}
|
249
|
-
|
250
|
-
addManager({name, manager}) {
|
251
|
-
var err;
|
252
|
-
try {
|
253
|
-
return this._register_internal_class('manager', name, manager);
|
254
|
-
} catch (error1) {
|
255
|
-
err = error1;
|
256
|
-
throw new Error(`[!] Error while instantiate ${name} manager -> ${err}`);
|
257
|
-
}
|
258
|
-
}
|
259
|
-
|
260
|
-
// Allow to register easily a class to this server
|
261
|
-
// this class will be bind to a specific namespace
|
262
|
-
addService({name, service, middlewares}) {
|
263
|
-
var err;
|
264
|
-
// Allow global register for '/'
|
265
|
-
if (!name) {
|
266
|
-
name = '/';
|
267
|
-
}
|
268
|
-
try {
|
269
|
-
this._register_internal_class('service', name, service);
|
270
|
-
} catch (error1) {
|
271
|
-
err = error1;
|
272
|
-
throw new Error(`[!] Error while instantiate ${name} service -> ${err}`);
|
273
|
-
}
|
274
|
-
// list methods of object... it will be the list of io actions
|
275
|
-
this.method_list[name] = this._dumpMethods(service);
|
276
|
-
// Register middlewares if necessary
|
277
|
-
return this.middleware_list[name] = middlewares ? middlewares : [];
|
278
|
-
}
|
279
|
-
|
280
|
-
|
281
|
-
// Allow to register easily controllers for REST API
|
282
|
-
// this method should be called automatically when fastify is started
|
283
|
-
addController({name, controller, middlewares, prefix}) {
|
284
|
-
var controller_routes, entry, err, j, len, len1, len2, mdwr, middleware, n, o, option, ref, results;
|
285
|
-
if (!middlewares) {
|
286
|
-
middlewares = [];
|
287
|
-
}
|
288
|
-
|
289
|
-
// Sanitize prefix
|
290
|
-
if (prefix && !prefix.startsWith('/')) {
|
291
|
-
prefix = `/${prefix}`;
|
292
|
-
}
|
293
|
-
if (prefix && prefix.endsWith('/')) {
|
294
|
-
prefix = prefix.slice(0, -1);
|
295
|
-
}
|
296
|
-
try {
|
297
|
-
this._register_internal_class('controller', name, controller);
|
298
|
-
} catch (error1) {
|
299
|
-
err = error1;
|
300
|
-
throw new Error(`[!] Error while instantiate ${name} controller -> ${err}`);
|
301
|
-
}
|
302
|
-
if (!fs.existsSync(`${this._routes}/${name}.json`)) {
|
303
|
-
throw new Error(`[!] Predicted routes file does not exists: ${this._routes}/${name}.json`);
|
304
|
-
}
|
305
|
-
// Load defined routes
|
306
|
-
controller_routes = require(`${this._routes}/${name}.json`);
|
307
|
-
// Parse all routes found, and register corresponding controller method
|
308
|
-
results = [];
|
309
|
-
for (j = 0, len = controller_routes.length; j < len; j++) {
|
310
|
-
entry = controller_routes[j];
|
311
|
-
ref = ['onRequest', 'preParsing', 'preValidation', 'preHandler', 'preSerialization', 'onSend', 'onResponse', 'handler', 'errorHandler'];
|
312
|
-
// Auto load function or array of function for fastify routes options
|
313
|
-
for (n = 0, len1 = ref.length; n < len1; n++) {
|
314
|
-
option = ref[n];
|
315
|
-
// Avoid override undefined keys
|
316
|
-
if (entry[option] == null) {
|
317
|
-
continue;
|
318
|
-
}
|
319
|
-
// Adapt object using current controller name
|
320
|
-
if (this.controller_list[name][entry[option]] != null) {
|
321
|
-
entry[option] = this.controller_list[name][entry[option]];
|
322
|
-
}
|
323
|
-
}
|
324
|
-
|
325
|
-
// Adapt all urls if prefix is set, otherwise prefix with controller name
|
326
|
-
entry.url = prefix != null ? `${prefix}${entry.url}` : `/${name}${entry.url}`;
|
327
|
-
|
328
|
-
// Always setup preValidation middlewares
|
329
|
-
if (entry.preValidation == null) {
|
330
|
-
entry.preValidation = [];
|
331
|
-
}
|
332
|
-
for (o = 0, len2 = middlewares.length; o < len2; o++) {
|
333
|
-
middleware = middlewares[o];
|
334
|
-
mdwr = new middleware();
|
335
|
-
entry.preValidation.push(mdwr.handle(this.appHandle));
|
336
|
-
}
|
337
|
-
try {
|
338
|
-
this._logify(7, `[*] Register controller route ${entry.method} ${entry.url}`);
|
339
|
-
results.push(this._webapp.route(entry));
|
340
|
-
} catch (error1) {
|
341
|
-
err = error1;
|
342
|
-
results.push(this._logify(3, `[!] Unable to register route entry: ${err}`));
|
343
|
-
}
|
344
|
-
}
|
345
|
-
return results;
|
346
|
-
}
|
347
|
-
|
348
|
-
// Get service running
|
349
|
-
getService(name) {
|
350
|
-
return this.service_list[name];
|
351
|
-
}
|
352
|
-
|
353
|
-
// Launch socket IO and get ready to handle events on connection
|
354
|
-
// Pass web server used for connections
|
355
|
-
async start() {
|
356
|
-
var d, day, err, hours, manager, manager_name, minutes, month, ns, ref, seconds;
|
357
|
-
d = new Date();
|
358
|
-
day = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
|
359
|
-
month = d.getMonth() < 10 ? `0${d.getMonth()}` : d.getMonth();
|
360
|
-
hours = d.getHours() < 10 ? `0${d.getHours()}` : d.getHours();
|
361
|
-
minutes = d.getMinutes() < 10 ? `0${d.getMinutes()}` : d.getMinutes();
|
362
|
-
seconds = d.getSeconds() < 10 ? `0${d.getSeconds()}` : d.getSeconds();
|
363
|
-
this._logify(4, `################### IOServer v${VERSION} ###################`);
|
364
|
-
this._logify(5, `################### ${day}/${month}/${d.getFullYear()} - ${hours}:${minutes}:${seconds} #########################`);
|
365
|
-
ns = {};
|
366
|
-
ref = this.manager_list;
|
367
|
-
// Register all managers
|
368
|
-
for (manager_name in ref) {
|
369
|
-
manager = ref[manager_name];
|
370
|
-
this._logify(6, `[*] Register ${manager_name} manager`);
|
371
|
-
this.appHandle[manager_name] = manager;
|
372
|
-
}
|
373
|
-
|
374
|
-
// Once webapp is ready
|
375
|
-
this._webapp.ready((err) => {
|
376
|
-
var j, len, mdwr, middleware, ref1, results, service_name;
|
377
|
-
// Register each different services by its namespace
|
378
|
-
results = [];
|
379
|
-
for (service_name in this.service_list) {
|
380
|
-
ns[service_name] = service_name === '/' ? this._webapp.io.of('/') : this._webapp.io.of(`/${service_name}`);
|
381
|
-
ref1 = this.middleware_list[service_name];
|
382
|
-
// Register middleware for namespace
|
383
|
-
for (j = 0, len = ref1.length; j < len; j++) {
|
384
|
-
middleware = ref1[j];
|
385
|
-
mdwr = new middleware();
|
386
|
-
ns[service_name].use(mdwr.handle(this.appHandle));
|
387
|
-
}
|
388
|
-
// get ready for connection
|
389
|
-
ns[service_name].on("connection", this._handleEvents(service_name));
|
390
|
-
results.push(this._logify(6, `[*] service ${service_name} registered...`));
|
391
|
-
}
|
392
|
-
return results;
|
393
|
-
});
|
394
|
-
try {
|
395
|
-
// Start all watchers
|
396
|
-
// Do not wait for watchers to finish...
|
397
|
-
Promise.all(Object.values(this.watcher_list).map(async(watcher) => {
|
398
|
-
var err;
|
399
|
-
try {
|
400
|
-
this._logify(6, `[*] Start watcher ${watcher.constructor.name}`);
|
401
|
-
return (await watcher.watch());
|
402
|
-
} catch (error1) {
|
403
|
-
err = error1;
|
404
|
-
throw new Error(`Unable to start ${watcher.constructor.name} watcher: ${err}`);
|
405
|
-
}
|
406
|
-
}));
|
407
|
-
} catch (error1) {
|
408
|
-
err = error1;
|
409
|
-
throw new Error(`[!] Error on watchers start: ${err}`);
|
410
|
-
}
|
411
|
-
try {
|
412
|
-
// Start web server
|
413
|
-
this._logify(5, `[*] Starting server on https://${this.host}:${this.port} ...`);
|
414
|
-
return (await this._webapp.listen({
|
415
|
-
port: this.port,
|
416
|
-
host: this.host
|
417
|
-
}));
|
418
|
-
} catch (error1) {
|
419
|
-
err = error1;
|
420
|
-
return this._logify(3, `[!] Unable to start server: ${err}`);
|
421
|
-
}
|
422
|
-
}
|
423
|
-
|
424
|
-
|
425
|
-
// Force server stop
|
426
|
-
stop() {
|
427
|
-
var err;
|
428
|
-
try {
|
429
|
-
return this._webapp.close(() => {
|
430
|
-
return this._logify(6, "[*] Server stopped");
|
431
|
-
});
|
432
|
-
} catch (error1) {
|
433
|
-
err = error1;
|
434
|
-
throw new Error(`[!] Unable to stop server: ${err}`);
|
435
|
-
}
|
436
|
-
}
|
437
|
-
|
438
|
-
sendTo({namespace, event, data, room = null, sid = null} = {}) {
|
439
|
-
var ns, sockets;
|
440
|
-
if (namespace != null) {
|
441
|
-
// Auto correct namespace
|
442
|
-
if (!namespace.startsWith('/')) {
|
443
|
-
namespace = `/${namespace}`;
|
444
|
-
}
|
445
|
-
|
446
|
-
// Search for namespace object
|
447
|
-
ns = this._webapp.io.of(namespace);
|
448
|
-
} else {
|
449
|
-
ns = this._webapp.io.of('/');
|
450
|
-
}
|
451
|
-
|
452
|
-
// Send event to specific sid if set
|
453
|
-
if (sid != null) {
|
454
|
-
ns.sockets.get(sid).emit(event, data);
|
455
|
-
} else {
|
456
|
-
// Restrict access to clients in room if set
|
457
|
-
sockets = room != null ? ns.in(room) : ns;
|
458
|
-
sockets.emit(event, data);
|
459
|
-
}
|
460
|
-
return true;
|
461
|
-
}
|
462
|
-
|
463
|
-
// Once a client is connected, get ready to handle his events
|
464
|
-
_handleEvents(service_name) {
|
465
|
-
return (socket, next) => {
|
466
|
-
var action, index, ref, results;
|
467
|
-
this._logify(5, `[*] received connection for service ${service_name}`);
|
468
|
-
ref = this.method_list[service_name];
|
469
|
-
|
470
|
-
// The register all user defined functions
|
471
|
-
results = [];
|
472
|
-
for (index in ref) {
|
473
|
-
action = ref[index];
|
474
|
-
// does not listen for private methods
|
475
|
-
if (action.substring(0, 1) === '_') {
|
476
|
-
continue;
|
477
|
-
}
|
478
|
-
// do not listen for constructor method
|
479
|
-
if (action === 'constructor') {
|
480
|
-
continue;
|
481
|
-
}
|
482
|
-
this._logify(6, `[*] method ${action} of ${service_name} listening...`);
|
483
|
-
results.push(socket.on(action, this._handleCallback({
|
484
|
-
service: service_name,
|
485
|
-
method: action,
|
486
|
-
socket: socket
|
487
|
-
})));
|
488
|
-
}
|
489
|
-
return results;
|
490
|
-
};
|
491
|
-
}
|
492
|
-
|
493
|
-
// On a specific event call the appropriate method of object
|
494
|
-
_handleCallback({service, method, socket}) {
|
495
|
-
return (data, callback) => {
|
496
|
-
this._logify(6, `[*] call method ${method} of service ${service}`);
|
497
|
-
return new Promise(async(resolve, reject) => {
|
498
|
-
var err;
|
499
|
-
try {
|
500
|
-
await this.service_list[service][method](socket, data, callback);
|
501
|
-
return resolve();
|
502
|
-
} catch (error1) {
|
503
|
-
err = error1;
|
504
|
-
return reject(err);
|
505
|
-
}
|
506
|
-
}).catch((err) => {
|
507
|
-
var payload;
|
508
|
-
if (typeof err === 'string') {
|
509
|
-
err = new IOServerError(err, -1);
|
510
|
-
}
|
511
|
-
payload = {
|
512
|
-
status: 'error',
|
513
|
-
type: err.constructor.name || 'Error',
|
514
|
-
message: err.message || null,
|
515
|
-
code: err.code || -1
|
516
|
-
};
|
517
|
-
this._logify(5, `Error on ${service}:${method} execution: ${err}`);
|
518
|
-
if (callback) {
|
519
|
-
return callback(payload);
|
520
|
-
} else {
|
521
|
-
return socket.emit('error', payload);
|
522
|
-
}
|
523
|
-
});
|
524
|
-
};
|
525
|
-
}
|
526
|
-
|
527
|
-
|
528
|
-
// Based on Kri-ban solution
|
529
|
-
// http://stackoverflow.com/questions/7445726/how-to-list-methods-of-inherited-classes-in-coffeescript-or-javascript
|
530
|
-
// Thanks ___ ;)
|
531
|
-
_dumpMethods(klass) {
|
532
|
-
var k, names, result;
|
533
|
-
result = [];
|
534
|
-
k = klass.prototype;
|
535
|
-
while (k) {
|
536
|
-
names = Object.getOwnPropertyNames(k);
|
537
|
-
result = result.concat(names);
|
538
|
-
k = Object.getPrototypeOf(k);
|
539
|
-
if (!Object.getPrototypeOf(k)) { // avoid listing Object properties
|
540
|
-
break;
|
541
|
-
}
|
542
|
-
}
|
543
|
-
return this._unique(result).sort();
|
544
|
-
}
|
545
|
-
|
546
|
-
};
|
547
|
-
|
548
|
-
// IO Server error class
|
549
|
-
module.exports.IOServerError = IOServerError = class IOServerError extends Error {
|
550
|
-
constructor(message, code = -1) {
|
551
|
-
super(message);
|
552
|
-
this.type = this.constructor.name;
|
553
|
-
this.code = code;
|
554
|
-
}
|
555
|
-
|
556
|
-
getMessage() {
|
557
|
-
return this.message;
|
558
|
-
}
|
559
|
-
|
560
|
-
getType() {
|
561
|
-
return this.type;
|
562
|
-
}
|
563
|
-
|
564
|
-
getCode() {
|
565
|
-
return this.code;
|
566
|
-
}
|
567
|
-
|
568
|
-
toJson() {
|
569
|
-
return {
|
570
|
-
message: this.message,
|
571
|
-
type: this.type,
|
572
|
-
code: this.code
|
573
|
-
};
|
574
|
-
}
|
575
|
-
|
576
|
-
};
|
577
|
-
|
578
|
-
}).call(this);
|
579
|
-
|
580
|
-
//# sourceMappingURL=ioserver.js.map
|
package/build/ioserver.js.map
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"version": 3,
|
3
|
-
"file": "ioserver.js",
|
4
|
-
"sourceRoot": "../",
|
5
|
-
"sources": [
|
6
|
-
"src/ioserver.coffee"
|
7
|
-
],
|
8
|
-
"names": [],
|
9
|
-
"mappings": "AAyBuB;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,MAAA,IAAA,EAAA,QAAA,EAAA,aAAA,EAAA,SAAA,EAAA,IAAA,EAAA,cAAA,EAAA,UAAA,EAAA,OAAA,EAAA,OAAA,EAAA,EAAA,EAAA,IAAA;IAAA;;EACvB,EAAA,GAAW,OAAA,CAAQ,IAAR;;EACX,IAAA,GAAW,OAAA,CAAQ,MAAR;;EACX,OAAA,GAAW,OAAA,CAAQ,SAAR,EAHY;;;EAMvB,OAAA,GAAa;;EACb,IAAA,GAAa;;EACb,IAAA,GAAa;;EACb,SAAA,GAAa,CAAC,WAAD,EAAa,OAAb,EAAqB,UAArB,EAAgC,OAAhC,EAAwC,SAAxC,EAAkD,cAAlD,EAAiE,aAAjE,EAA+E,OAA/E;;EACb,UAAA,GAAa,CAAC,WAAD,EAAa,UAAb,EAAwB,aAAxB,EAAsC,eAAtC;;EACb,cAAA,GAAiB,CAAC,MAAD,EAAS,KAAT,EAAgB,SAAhB;;EAEjB,M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A,GAAI,MAAM,CAAC,cAAP,CAAsB,CAAtB;QACJ,IAAS,CAAI,MAAM,CAAC,cAAP,CAAsB,CAAtB,CAAb;AAAA,gBAAA;;MAJJ;AAMA,aAAO,IAAC,CAAA,OAAD,CAAS,MAAT,CAAgB,CAAC,IAAjB,CAAA;IATG;;EAvXD,EAbM;;;EAgZvB,MAAM,CAAC,OAAO,CAAC,aAAf,GAAqC,gBAAN,MAAA,cAAA,QAA4B,MAA5B;IAC3B,WAAa,CAAC,OAAD,EAAU,OAAO,CAAC,CAAlB,CAAA;WACT,CAAM,OAAN;MACA,IAAC,CAAA,IAAD,GAAQ,IAAC,CAAA,WAAW,CAAC;MACrB,IAAC,CAAA,IAAD,GAAQ;IAHC;;IAKb,UAAY,CAAA,CAAA;AACR,aAAO,IAAC,CAAA;IADA;;IAGZ,OAAS,CAAA,CAAA;AACL,aAAO,IAAC,CAAA;IADH;;IAGT,OAAS,CAAA,CAAA;AACL,aAAO,IAAC,CAAA;IADH;;IAGT,MAAQ,CAAA,CAAA;AACJ,aAAO;QACH,OAAA,EAAS,IAAC,CAAA,OADP;QAEH,IAAA,EAAM,IAAC,CAAA,IAFJ;QAGH,IAAA,EAAM,IAAC,CAAA;MAHJ;IADH;;EAfmB;AAhZR",
|
10
|
-
"sourcesContent": [
|
11
|
-
"####################################################\n# IOServer - v1.5.3 #\n# #\n# Damn simple socket.io server #\n####################################################\n# - Copyright 2023 - #\n# #\n# License: Apache v 2.0 #\n# @Author: Ben Mz #\n# @Email: 0x42en (at) users.noreply.github.com #\n# #\n####################################################\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n# Add required packages\nfs = require 'fs'\npath = require 'path'\nfastify = require 'fastify'\n\n# Set global vars\nVERSION = '1.4.2'\nPORT = 8080\nHOST = 'localhost'\nLOG_LEVEL = ['EMERGENCY','ALERT','CRITICAL','ERROR','WARNING','NOTIFICATION','INFORMATION','DEBUG']\nTRANSPORTS = ['websocket','htmlfile','xhr-polling','jsonp-polling']\nRESERVED_NAMES = ['send', 'log', 'verbose']\n\nmodule.exports = class IOServer\n # Define the variables used by the server\n constructor: (options = {}) ->\n # Set default options\n { verbose, host, port, cookie, mode, cors, routes } = options\n\n @host = if host then String(host) else HOST\n try\n @port = if port then Number(port) else PORT\n if @port <= 0\n throw new Error 'Invalid port'\n if @port > 65535\n throw new Error 'Invalid port'\n catch err\n throw new Error err\n \n _cookie = if Boolean(cookie) then Boolean(cookie) else false\n @verbose = if String(verbose).toUpperCase() in LOG_LEVEL then String(verbose).toUpperCase() else 'ERROR'\n # Does not yell if route directory does not exists... change that ?\n default_routes = path.join(process.cwd(), 'routes')\n @_routes = if fs.existsSync(routes) then String(routes) else default_routes\n \n # Process transport mode options\n _mode = []\n if mode\n if String(mode).toLowerCase() in TRANSPORTS\n _mode.push String(mode).toLowerCase()\n else if mode.constructor is Array\n for i,m of mode\n if String(m).toLowerCase() in TRANSPORTS\n _mode.push m\n else\n _mode.push 'websocket'\n _mode.push 'polling'\n \n # Setup CORS since necessary in socket.io v3\n _cors = if cors? and cors then cors else {}\n if not _cors.methods\n _cors.methods = ['GET','POST']\n if not _cors.origin\n _cors.origin = [\"https://#{@host}\",\"http://#{@host}\"]\n \n # Setup internal lists\n @service_list = {}\n @manager_list = {}\n @method_list = {}\n @watcher_list = {}\n \n @controller_list = {}\n @middleware_list = {}\n \n try\n # Instanciate server (needed to register controllers)\n @_webapp = fastify({\n logger: @verbose\n ignoreTrailingSlash: true\n maxParamLength: 200\n caseSensitive: true\n })\n catch err\n throw new Error \"[!] Unable to instanciate server: #{err}\"\n \n try\n # Register standard HTTP error shortcuts\n @_webapp.register(require('@fastify/sensible'), { errorHandler: false })\n catch err\n throw new Error \"[!] Unable to register sensible plugin: #{err}\"\n \n try\n # Allow developper to use throw Error directly in methods\n @_webapp.setErrorHandler (error, req, reply) ->\n # Handle IOServerError\n if (error instanceof IOServerError)\n code = if error.getCode() < 0 then 500 else error.getCode()\n reply.status(code).send(error)\n # Handle HTTPErrors\n else if (error.status?)\n reply.status(error.status).send({message: error.message})\n # Handle standard Error\n else\n reply.status(500).send(error.message)\n catch err\n throw new Error \"[!] Unable to register error handler: #{err}\"\n \n try\n # Register standard HTTP error shortcuts\n @_webapp.register(require('@fastify/cors'), _cors)\n catch err\n throw new Error \"[!] Unable to register CORS plugin: #{err}\"\n \n try\n # Register socket.io listener\n @_webapp.register( require('fastify-socket.io'), {\n transports: _mode,\n cookie: _cookie\n cors: _cors\n })\n catch err\n throw new Error \"[!] Unable to register socket.io plugin: #{err}\"\n\n # Register the global app handle\n # that will be passed to all entities\n @appHandle = {\n send: @sendTo\n log: @_logify\n verbose: @verbose\n }\n \n _logify: (level, text) ->\n current_level = LOG_LEVEL.indexOf @verbose\n\n if level <= current_level\n if level <= 4\n console.error text\n else\n console.log text\n \n _unique: (arr) ->\n hash = {}\n result = []\n\n i = 0\n l = arr.length\n while i < l\n unless hash.hasOwnProperty(arr[i])\n hash[arr[i]] = true\n result.push arr[i]\n ++i\n\n return result\n \n _method_exists: (klass, name) ->\n return klass[name]?\n \n _register_internal_class: (type, name, klass) ->\n if not name\n throw new Error \"name is mandatory\"\n if (type isnt 'service') and name.length < 2\n throw new Error \"name MUST be longer than 2 characters\"\n if name in RESERVED_NAMES\n throw new Error \"Sorry this is a reserved name\"\n \n if not (klass and klass.prototype)\n throw new Error \"MUST be a function\"\n \n if @[\"#{type}_list\"][name]?\n throw new Error \"Sorry this #{type} already exists\"\n \n try\n # Register klass with handle reference\n @_logify 7, \"[*] Register #{type} #{name}\"\n @[\"#{type}_list\"][name] = new klass(@appHandle)\n catch err\n throw new Error err\n\n \n addWatcher: ({name, watcher}) ->\n try\n @_register_internal_class 'watcher', name, watcher\n catch err\n throw new Error \"[!] Error while instantiate #{name} watcher -> #{err}\"\n\n addManager: ({name, manager}) ->\n try\n @_register_internal_class 'manager', name, manager\n catch err\n throw new Error \"[!] Error while instantiate #{name} manager -> #{err}\"\n\n # Allow to register easily a class to this server\n # this class will be bind to a specific namespace\n addService: ({name, service, middlewares}) ->\n # Allow global register for '/'\n if not name\n name = '/'\n try\n @_register_internal_class 'service', name, service\n catch err\n throw new Error \"[!] Error while instantiate #{name} service -> #{err}\"\n\n # list methods of object... it will be the list of io actions\n @method_list[name] = @_dumpMethods service\n # Register middlewares if necessary\n @middleware_list[name] = if middlewares then middlewares else []\n \n # Allow to register easily controllers for REST API\n # this method should be called automatically when fastify is started\n addController: ({name, controller, middlewares, prefix}) ->\n if not middlewares\n middlewares = []\n \n # Sanitize prefix\n if prefix and not prefix.startsWith('/')\n prefix = \"/#{prefix}\"\n \n if prefix and prefix.endsWith('/')\n prefix = prefix.slice(0, -1)\n \n try\n @_register_internal_class 'controller', name, controller\n catch err\n throw new Error \"[!] Error while instantiate #{name} controller -> #{err}\"\n \n if not fs.existsSync(\"#{@_routes}/#{name}.json\")\n throw new Error \"[!] Predicted routes file does not exists: #{@_routes}/#{name}.json\"\n\n # Load defined routes\n controller_routes = require \"#{@_routes}/#{name}.json\"\n\n # Parse all routes found, and register corresponding controller method\n for entry in controller_routes\n\n # Auto load function or array of function for fastify routes options\n for option in ['onRequest', 'preParsing', 'preValidation', 'preHandler', 'preSerialization', 'onSend', 'onResponse', 'handler', 'errorHandler']\n # Avoid override undefined keys\n if not entry[option]?\n continue\n # Adapt object using current controller name\n if @controller_list[name][entry[option]]?\n entry[option] = @controller_list[name][entry[option]]\n \n # Adapt all urls if prefix is set, otherwise prefix with controller name\n entry.url = if prefix? then \"#{prefix}#{entry.url}\" else \"/#{name}#{entry.url}\"\n \n # Always setup preValidation middlewares\n if not entry.preValidation?\n entry.preValidation = []\n\n for middleware in middlewares\n mdwr = new middleware()\n entry.preValidation.push mdwr.handle(@appHandle)\n\n try\n @_logify 7, \"[*] Register controller route #{entry.method} #{entry.url}\"\n @_webapp.route entry\n catch err\n @_logify 3, \"[!] Unable to register route entry: #{err}\"\n\n # Get service running\n getService: (name) -> @service_list[name]\n\n # Launch socket IO and get ready to handle events on connection\n # Pass web server used for connections\n start: ->\n d = new Date()\n day = if d.getDate() < 10 then \"0#{d.getDate()}\" else d.getDate()\n month = if d.getMonth() < 10 then \"0#{d.getMonth()}\" else d.getMonth()\n hours = if d.getHours() < 10 then \"0#{d.getHours()}\" else d.getHours()\n minutes = if d.getMinutes() < 10 then \"0#{d.getMinutes()}\" else d.getMinutes()\n seconds = if d.getSeconds() < 10 then \"0#{d.getSeconds()}\" else d.getSeconds()\n @_logify 4, \"################### IOServer v#{VERSION} ###################\"\n @_logify 5, \"################### #{day}/#{month}/#{d.getFullYear()} - #{hours}:#{minutes}:#{seconds} #########################\"\n \n ns = {}\n\n # Register all managers\n for manager_name, manager of @manager_list\n @_logify 6, \"[*] Register #{manager_name} manager\"\n @appHandle[manager_name] = manager\n \n # Once webapp is ready\n @_webapp.ready (err) =>\n # Register each different services by its namespace\n for service_name of @service_list\n ns[service_name] = if service_name is '/' then @_webapp.io.of '/' else @_webapp.io.of \"/#{service_name}\"\n\n # Register middleware for namespace \n for middleware in @middleware_list[service_name]\n mdwr = new middleware()\n ns[service_name].use mdwr.handle(@appHandle)\n\n # get ready for connection\n ns[service_name].on \"connection\", @_handleEvents(service_name)\n @_logify 6, \"[*] service #{service_name} registered...\"\n \n try\n # Start all watchers\n # Do not wait for watchers to finish...\n Promise.all Object.values(this.watcher_list).map (watcher) =>\n try\n @_logify 6, \"[*] Start watcher #{watcher.constructor.name}\"\n await watcher.watch()\n catch err\n throw new Error \"Unable to start #{watcher.constructor.name} watcher: #{err}\"\n catch err\n throw new Error \"[!] Error on watchers start: #{err}\"\n \n try\n # Start web server\n @_logify 5, \"[*] Starting server on https://#{@host}:#{@port} ...\"\n await @_webapp.listen\n port: @port\n host: @host\n catch err\n @_logify 3, \"[!] Unable to start server: #{err}\"\n \n # Force server stop\n stop: ->\n try\n @_webapp.close () =>\n @_logify 6, \"[*] Server stopped\"\n catch err\n throw new Error \"[!] Unable to stop server: #{err}\"\n\n # Allow sending message from external app\n sendTo: ({namespace, event, data, room=null, sid=null}={}) =>\n if namespace?\n # Auto correct namespace\n if not namespace.startsWith('/')\n namespace = \"/#{namespace}\"\n \n # Search for namespace object\n ns = @_webapp.io.of(namespace)\n else\n ns = @_webapp.io.of('/')\n \n # Send event to specific sid if set\n if sid?\n ns.sockets.get(sid).emit event, data\n else\n # Restrict access to clients in room if set\n sockets = if room? then ns.in(room) else ns\n sockets.emit event, data\n\n return true\n\n # Once a client is connected, get ready to handle his events\n _handleEvents: (service_name) ->\n (socket, next) =>\n @_logify 5, \"[*] received connection for service #{service_name}\"\n \n # The register all user defined functions\n for index, action of @method_list[service_name]\n # does not listen for private methods\n if action.substring(0,1) is '_'\n continue\n # do not listen for constructor method\n if action is 'constructor'\n continue\n \n @_logify 6, \"[*] method #{action} of #{service_name} listening...\"\n socket.on action, @_handleCallback\n service: service_name\n method: action\n socket: socket\n\n # On a specific event call the appropriate method of object\n _handleCallback: ({service, method, socket}) ->\n return (data, callback) =>\n @_logify 6, \"[*] call method #{method} of service #{service}\"\n return new Promise (resolve, reject) =>\n try\n await @service_list[service][method](socket, data, callback)\n resolve()\n catch err\n reject(err)\n .catch (err) =>\n if typeof err is 'string'\n err = new IOServerError(err, -1)\n\n payload = { \n status: 'error',\n type: err.constructor.name or 'Error',\n message: err.message or null,\n code: err.code or -1\n }\n\n @_logify 5, \"Error on #{service}:#{method} execution: #{err}\"\n if callback\n callback payload\n else\n socket.emit 'error', payload\n \n # Based on Kri-ban solution\n # http://stackoverflow.com/questions/7445726/how-to-list-methods-of-inherited-classes-in-coffeescript-or-javascript\n # Thanks ___ ;)\n _dumpMethods: (klass) ->\n result = []\n k = klass.prototype\n while k\n names = Object.getOwnPropertyNames(k)\n result = result.concat(names)\n k = Object.getPrototypeOf(k)\n break if not Object.getPrototypeOf(k) # avoid listing Object properties\n\n return @_unique(result).sort()\n\n# IO Server error class\nmodule.exports.IOServerError = class IOServerError extends Error\n constructor: (message, code = -1) ->\n super(message)\n @type = @constructor.name\n @code = code\n \n getMessage: () ->\n return @message\n \n getType: () ->\n return @type\n \n getCode: () ->\n return @code\n\n toJson: () ->\n return {\n message: @message\n type: @type\n code: @code\n }\n"
|
12
|
-
]
|
13
|
-
}
|