node-pluginsmanager-plugin 4.7.2 → 4.8.2
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/lib/components/Server.js +54 -30
- package/lib/utils/cleanSendedError.js +47 -0
- package/lib/utils/serverCodes.json +16 -0
- package/package.json +12 -12
package/lib/components/Server.js
CHANGED
|
@@ -17,15 +17,16 @@
|
|
|
17
17
|
// locals
|
|
18
18
|
const MediatorUser = require(join(__dirname, "MediatorUser.js"));
|
|
19
19
|
const { checkInteger, checkNonEmptyObject, checkNonEmptyString } = require(join(__dirname, "..", "checkers", "main.js"));
|
|
20
|
-
const errToString = require(join(__dirname, "..", "utils", "errToString.js"));
|
|
21
20
|
const { extractPattern, extractParams, extractSchemaType } = require(join(__dirname, "..", "utils", "descriptor", "main.js"));
|
|
22
21
|
const { extractBody, extractIp, extractCookies } = require(join(__dirname, "..", "utils", "request", "main.js"));
|
|
23
22
|
const send = require(join(__dirname, "..", "utils", "send.js"));
|
|
24
23
|
const getUniqueID = require(join(__dirname, "..", "utils", "getUniqueID.js"));
|
|
24
|
+
const cleanSendedError = require(join(__dirname, "..", "utils", "cleanSendedError.js"));
|
|
25
25
|
|
|
26
26
|
// consts
|
|
27
27
|
|
|
28
28
|
const WEBSOCKET_STATE_OPEN = 1;
|
|
29
|
+
const SERVER_CODES = require(join(__dirname, "..", "utils", "serverCodes.json"));
|
|
29
30
|
|
|
30
31
|
// module
|
|
31
32
|
|
|
@@ -145,7 +146,7 @@ module.exports = class Server extends MediatorUser {
|
|
|
145
146
|
);
|
|
146
147
|
|
|
147
148
|
// get descriptor
|
|
148
|
-
if ("/" + this._Descriptor.info.title + "/descriptor" === req.pattern && "get" === req.method) {
|
|
149
|
+
if ("/" + this._Descriptor.info.title + "/api/descriptor" === req.pattern && "get" === req.method) {
|
|
149
150
|
|
|
150
151
|
return new Promise((resolve, reject) => {
|
|
151
152
|
|
|
@@ -157,35 +158,56 @@ module.exports = class Server extends MediatorUser {
|
|
|
157
158
|
return err ? reject(err) : resolve(api);
|
|
158
159
|
});
|
|
159
160
|
|
|
161
|
+
// add current server
|
|
162
|
+
}).then((api) => {
|
|
163
|
+
|
|
164
|
+
const protocol = res.socket && Boolean(res.socket.encrypted) ? "https" : "http";
|
|
165
|
+
|
|
166
|
+
let port = res.socket && res.socket.localPort ? res.socket.localPort : 0;
|
|
167
|
+
if (!port) {
|
|
168
|
+
port = "http" === protocol ? 80 : 443;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!api.servers) {
|
|
172
|
+
api.servers = [];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
api.servers.push({
|
|
176
|
+
"url": protocol + "://" + req.validatedIp + ":" + port,
|
|
177
|
+
"description": "Actual current server"
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
return Promise.resolve(api);
|
|
181
|
+
|
|
160
182
|
}).then((api) => {
|
|
161
183
|
|
|
162
184
|
this._log("info", "<= [" + req.validatedIp + "] " + JSON.stringify(api));
|
|
163
|
-
return send(req, res,
|
|
185
|
+
return send(req, res, SERVER_CODES.OK, api, this._Descriptor.info.version, this._cors);
|
|
164
186
|
|
|
165
187
|
}).catch((err) => {
|
|
166
188
|
|
|
167
|
-
this.
|
|
189
|
+
this._log("error", err);
|
|
168
190
|
|
|
169
191
|
const result = {
|
|
170
192
|
"code": "INTERNAL_SERVER_ERROR",
|
|
171
|
-
"message":
|
|
193
|
+
"message": cleanSendedError(err)
|
|
172
194
|
};
|
|
173
195
|
|
|
174
|
-
this._log("error", "<= " + JSON.stringify(result));
|
|
175
|
-
return send(req, res,
|
|
196
|
+
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
197
|
+
return send(req, res, SERVER_CODES.INTERNAL_SERVER_ERROR, result, this._Descriptor.info.version, this._cors);
|
|
176
198
|
|
|
177
199
|
});
|
|
178
200
|
|
|
179
201
|
}
|
|
180
202
|
|
|
181
203
|
// get plugin status
|
|
182
|
-
else if ("/" + this._Descriptor.info.title + "/status" === req.pattern && "get" === req.method) {
|
|
204
|
+
else if ("/" + this._Descriptor.info.title + "/api/status" === req.pattern && "get" === req.method) {
|
|
183
205
|
|
|
184
206
|
const initialized = this.initialized && this._Mediator.initialized;
|
|
185
207
|
const status = initialized ? "INITIALIZED" : "ENABLED";
|
|
186
208
|
|
|
187
209
|
this._log("info", "<= [" + req.validatedIp + "] " + status);
|
|
188
|
-
return send(req, res,
|
|
210
|
+
return send(req, res, SERVER_CODES.OK, status, this._Descriptor.info.version, this._cors);
|
|
189
211
|
|
|
190
212
|
}
|
|
191
213
|
|
|
@@ -198,7 +220,7 @@ module.exports = class Server extends MediatorUser {
|
|
|
198
220
|
};
|
|
199
221
|
|
|
200
222
|
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
201
|
-
return send(req, res,
|
|
223
|
+
return send(req, res, SERVER_CODES.NOT_IMPLEMENTED, result, this._Descriptor.info.version, this._cors);
|
|
202
224
|
|
|
203
225
|
}
|
|
204
226
|
|
|
@@ -211,7 +233,7 @@ module.exports = class Server extends MediatorUser {
|
|
|
211
233
|
};
|
|
212
234
|
|
|
213
235
|
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
214
|
-
return send(req, res,
|
|
236
|
+
return send(req, res, SERVER_CODES.NOT_IMPLEMENTED, result, this._Descriptor.info.version, this._cors);
|
|
215
237
|
|
|
216
238
|
}
|
|
217
239
|
|
|
@@ -226,7 +248,7 @@ module.exports = class Server extends MediatorUser {
|
|
|
226
248
|
};
|
|
227
249
|
|
|
228
250
|
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
229
|
-
return send(req, res,
|
|
251
|
+
return send(req, res, SERVER_CODES.MISSING_HEADER, result, this._Descriptor.info.version, this._cors);
|
|
230
252
|
|
|
231
253
|
}
|
|
232
254
|
|
|
@@ -398,12 +420,12 @@ module.exports = class Server extends MediatorUser {
|
|
|
398
420
|
|
|
399
421
|
if ("undefined" === typeof content || null === content) {
|
|
400
422
|
this._log("success", "<= [" + req.validatedIp + "] no content");
|
|
401
|
-
return send(req, res,
|
|
423
|
+
return send(req, res, SERVER_CODES.OK_PUT, undefined, this._Descriptor.info.version, this._cors);
|
|
402
424
|
}
|
|
403
425
|
|
|
404
426
|
else {
|
|
405
427
|
this._log("success", "<= [" + req.validatedIp + "] " + JSON.stringify(content));
|
|
406
|
-
return send(req, res,
|
|
428
|
+
return send(req, res, SERVER_CODES.OK_PUT, content, this._Descriptor.info.version, this._cors);
|
|
407
429
|
}
|
|
408
430
|
|
|
409
431
|
}
|
|
@@ -411,12 +433,12 @@ module.exports = class Server extends MediatorUser {
|
|
|
411
433
|
// no content
|
|
412
434
|
else if ("undefined" === typeof content || null === content) {
|
|
413
435
|
this._log("warning", "<= [" + req.validatedIp + "] no content");
|
|
414
|
-
return send(req, res,
|
|
436
|
+
return send(req, res, SERVER_CODES.OK_NO_CONTENT, undefined, this._Descriptor.info.version, this._cors);
|
|
415
437
|
}
|
|
416
438
|
|
|
417
439
|
else {
|
|
418
440
|
this._log("success", "<= [" + req.validatedIp + "] " + JSON.stringify(content));
|
|
419
|
-
return send(req, res,
|
|
441
|
+
return send(req, res, SERVER_CODES.OK, content, this._Descriptor.info.version, this._cors);
|
|
420
442
|
}
|
|
421
443
|
|
|
422
444
|
}).catch((err) => {
|
|
@@ -425,55 +447,57 @@ module.exports = class Server extends MediatorUser {
|
|
|
425
447
|
|
|
426
448
|
const result = {
|
|
427
449
|
"code": "MISSING_PARAMETER",
|
|
428
|
-
"message":
|
|
450
|
+
"message": cleanSendedError(err)
|
|
429
451
|
};
|
|
430
452
|
|
|
431
453
|
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
432
|
-
return send(req, res,
|
|
454
|
+
return send(req, res, SERVER_CODES.MISSING_PARAMETER, result, this._Descriptor.info.version, this._cors);
|
|
433
455
|
|
|
434
456
|
}
|
|
435
457
|
else if (err instanceof TypeError) {
|
|
436
458
|
|
|
437
459
|
const result = {
|
|
438
460
|
"code": "WRONG_TYPE_PARAMETER",
|
|
439
|
-
"message":
|
|
461
|
+
"message": cleanSendedError(err)
|
|
440
462
|
};
|
|
441
463
|
|
|
442
464
|
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
443
|
-
return send(req, res,
|
|
465
|
+
return send(req, res, SERVER_CODES.WRONG_TYPE_PARAMETER, result, this._Descriptor.info.version, this._cors);
|
|
444
466
|
|
|
445
467
|
}
|
|
446
468
|
else if (err instanceof RangeError) {
|
|
447
469
|
|
|
448
470
|
const result = {
|
|
449
471
|
"code": "EMPTY_OR_RANGE_OR_ENUM_PARAMETER",
|
|
450
|
-
"message":
|
|
472
|
+
"message": cleanSendedError(err)
|
|
451
473
|
};
|
|
452
474
|
|
|
453
475
|
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
454
|
-
return send(req, res,
|
|
476
|
+
return send(req, res, SERVER_CODES.EMPTY_OR_RANGE_OR_ENUM_PARAMETER, result, this._Descriptor.info.version, this._cors);
|
|
455
477
|
|
|
456
478
|
}
|
|
457
479
|
else if (err instanceof SyntaxError) {
|
|
458
480
|
|
|
459
481
|
const result = {
|
|
460
482
|
"code": "JSON_PARSE",
|
|
461
|
-
"message":
|
|
483
|
+
"message": cleanSendedError(err)
|
|
462
484
|
};
|
|
463
485
|
|
|
464
486
|
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
465
|
-
return send(req, res,
|
|
487
|
+
return send(req, res, SERVER_CODES.JSON_PARSE, result, this._Descriptor.info.version, this._cors);
|
|
466
488
|
|
|
467
489
|
}
|
|
468
490
|
else {
|
|
469
491
|
|
|
492
|
+
this._log("error", err);
|
|
493
|
+
|
|
470
494
|
const result = {
|
|
471
495
|
"code": "INTERNAL_SERVER_ERROR",
|
|
472
|
-
"message":
|
|
496
|
+
"message": cleanSendedError(err)
|
|
473
497
|
};
|
|
474
498
|
|
|
475
499
|
this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
|
|
476
|
-
return send(req, res,
|
|
500
|
+
return send(req, res, SERVER_CODES.INTERNAL_SERVER_ERROR, result, this._Descriptor.info.version, this._cors);
|
|
477
501
|
|
|
478
502
|
}
|
|
479
503
|
|
|
@@ -561,7 +585,7 @@ module.exports = class Server extends MediatorUser {
|
|
|
561
585
|
};
|
|
562
586
|
|
|
563
587
|
if ("undefined" !== typeof data) {
|
|
564
|
-
result.data = data;
|
|
588
|
+
result.data = cleanSendedError(data);
|
|
565
589
|
}
|
|
566
590
|
|
|
567
591
|
return Promise.resolve(JSON.stringify(result));
|
|
@@ -602,7 +626,7 @@ module.exports = class Server extends MediatorUser {
|
|
|
602
626
|
}
|
|
603
627
|
|
|
604
628
|
}).catch((err) => {
|
|
605
|
-
this.
|
|
629
|
+
this._log("error", err);
|
|
606
630
|
});
|
|
607
631
|
|
|
608
632
|
return this;
|
|
@@ -626,7 +650,7 @@ module.exports = class Server extends MediatorUser {
|
|
|
626
650
|
};
|
|
627
651
|
|
|
628
652
|
if ("undefined" !== typeof data) {
|
|
629
|
-
result.data = data;
|
|
653
|
+
result.data = cleanSendedError(data);
|
|
630
654
|
}
|
|
631
655
|
|
|
632
656
|
return Promise.resolve(JSON.stringify(result));
|
|
@@ -683,7 +707,7 @@ module.exports = class Server extends MediatorUser {
|
|
|
683
707
|
|
|
684
708
|
}
|
|
685
709
|
}).catch((err) => {
|
|
686
|
-
this.
|
|
710
|
+
this._log("error", err);
|
|
687
711
|
});
|
|
688
712
|
|
|
689
713
|
return this;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// module
|
|
4
|
+
|
|
5
|
+
module.exports = function cleanSendedError (data) {
|
|
6
|
+
|
|
7
|
+
if ("object" === typeof data) {
|
|
8
|
+
|
|
9
|
+
if (data instanceof Error) {
|
|
10
|
+
return data.message ? data.message : data;
|
|
11
|
+
}
|
|
12
|
+
else if (data.message && data.message instanceof Error) {
|
|
13
|
+
|
|
14
|
+
data.message = data.message.message ? data.message.message : data.message;
|
|
15
|
+
|
|
16
|
+
return data;
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
else if (data.error) { // 1 level recursive for "error" data (avoid too deep recursive analyse)
|
|
20
|
+
|
|
21
|
+
if (data.error instanceof Error) {
|
|
22
|
+
data.error = data.error.message ? data.error.message : data.error;
|
|
23
|
+
}
|
|
24
|
+
else if (data.error.message && data.error.message instanceof Error) {
|
|
25
|
+
data.error.message = data.error.message.message ? data.error.message.message : data.error.message;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return data;
|
|
29
|
+
|
|
30
|
+
}
|
|
31
|
+
else if (data.err && data.err instanceof Error) { // 1 level recursive for "err" data (avoid too deep recursive analyse)
|
|
32
|
+
|
|
33
|
+
data.err = data.err.message ? data.err.message : data.err;
|
|
34
|
+
|
|
35
|
+
return data;
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return data;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return data;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
|
|
3
|
+
"OK": 200,
|
|
4
|
+
"OK_PUT": 201,
|
|
5
|
+
"OK_NO_CONTENT": 204,
|
|
6
|
+
|
|
7
|
+
"MISSING_PARAMETER": 400,
|
|
8
|
+
"WRONG_TYPE_PARAMETER": 400,
|
|
9
|
+
"EMPTY_OR_RANGE_OR_ENUM_PARAMETER": 400,
|
|
10
|
+
"JSON_PARSE": 400,
|
|
11
|
+
"MISSING_HEADER": 411,
|
|
12
|
+
|
|
13
|
+
"INTERNAL_SERVER_ERROR": 500,
|
|
14
|
+
"NOT_IMPLEMENTED": 501
|
|
15
|
+
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-pluginsmanager-plugin",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.8.2",
|
|
4
4
|
"description": "An abstract parent plugin for node-pluginsmanager",
|
|
5
5
|
"main": "lib/main.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -39,21 +39,21 @@
|
|
|
39
39
|
"express-openapi-validate": "0.6.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@types/node": "
|
|
42
|
+
"@types/node": "17.0.25",
|
|
43
43
|
"@types/socket.io": "3.0.2",
|
|
44
|
-
"@types/ws": "
|
|
45
|
-
"check-version-modules": "1.3.
|
|
44
|
+
"@types/ws": "8.5.3",
|
|
45
|
+
"check-version-modules": "1.3.5",
|
|
46
46
|
"coveralls": "3.1.1",
|
|
47
47
|
"colors": "1.4.0",
|
|
48
|
-
"eslint": "
|
|
49
|
-
"express": "4.17.
|
|
50
|
-
"husky": "7.0.
|
|
51
|
-
"mocha": "9.
|
|
48
|
+
"eslint": "8.13.0",
|
|
49
|
+
"express": "4.17.3",
|
|
50
|
+
"husky": "7.0.4",
|
|
51
|
+
"mocha": "9.2.2",
|
|
52
52
|
"nyc": "15.1.0",
|
|
53
|
-
"socket.io": "4.
|
|
54
|
-
"socket.io-client": "4.
|
|
55
|
-
"typescript": "4.
|
|
56
|
-
"ws": "8.
|
|
53
|
+
"socket.io": "4.4.1",
|
|
54
|
+
"socket.io-client": "4.4.1",
|
|
55
|
+
"typescript": "4.6.3",
|
|
56
|
+
"ws": "8.5.0"
|
|
57
57
|
},
|
|
58
58
|
"homepage": "https://github.com/Psychopoulet/node-pluginsmanager-plugin#readme",
|
|
59
59
|
"engines": {
|