node-pluginsmanager-plugin 6.0.2 → 6.0.3
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/cjs/components/Mediator.js +147 -151
- package/lib/cjs/utils/send.d.ts +6 -6
- package/lib/cjs/utils/send.js +84 -54
- package/package.json +2 -2
|
@@ -1,151 +1,147 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const checkExists_1 = require("../checkers/ReferenceError/checkExists");
|
|
7
|
-
const checkObject_1 = require("../checkers/TypeError/checkObject");
|
|
8
|
-
const checkNonEmptyString_1 = require("../checkers/RangeError/checkNonEmptyString");
|
|
9
|
-
const checkNonEmptyObject_1 = require("../checkers/RangeError/checkNonEmptyObject");
|
|
10
|
-
const extractPathMethodByOperationId_1 = __importDefault(require("../utils/descriptor/extractPathMethodByOperationId"));
|
|
11
|
-
const
|
|
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
|
-
case "
|
|
70
|
-
case "
|
|
71
|
-
case "
|
|
72
|
-
case "
|
|
73
|
-
case "
|
|
74
|
-
case "
|
|
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
|
-
mutedRes.
|
|
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
|
-
this.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
exports.default = Mediator;
|
|
151
|
-
;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const checkExists_1 = require("../checkers/ReferenceError/checkExists");
|
|
7
|
+
const checkObject_1 = require("../checkers/TypeError/checkObject");
|
|
8
|
+
const checkNonEmptyString_1 = require("../checkers/RangeError/checkNonEmptyString");
|
|
9
|
+
const checkNonEmptyObject_1 = require("../checkers/RangeError/checkNonEmptyObject");
|
|
10
|
+
const extractPathMethodByOperationId_1 = __importDefault(require("../utils/descriptor/extractPathMethodByOperationId"));
|
|
11
|
+
const DescriptorUser_1 = __importDefault(require("./DescriptorUser"));
|
|
12
|
+
// types & interfaces
|
|
13
|
+
// externals
|
|
14
|
+
const express_openapi_validate_1 = require("express-openapi-validate");
|
|
15
|
+
;
|
|
16
|
+
;
|
|
17
|
+
;
|
|
18
|
+
// module
|
|
19
|
+
class Mediator extends DescriptorUser_1.default {
|
|
20
|
+
// constructor
|
|
21
|
+
constructor(options) {
|
|
22
|
+
super(options);
|
|
23
|
+
this._validator = null;
|
|
24
|
+
}
|
|
25
|
+
// public
|
|
26
|
+
// Check sended parameters by method name (used by the Server)
|
|
27
|
+
checkParameters(operationId, urlParams, bodyParams) {
|
|
28
|
+
// parameters validation
|
|
29
|
+
return this.checkDescriptor().then(() => {
|
|
30
|
+
return (0, checkNonEmptyString_1.checkNonEmptyString)("operationId", operationId);
|
|
31
|
+
}).then(() => {
|
|
32
|
+
return (0, checkNonEmptyObject_1.checkNonEmptyObject)("urlParams", urlParams).then(() => {
|
|
33
|
+
return (0, checkObject_1.checkObject)("urlParams.path", urlParams.path);
|
|
34
|
+
}).then(() => {
|
|
35
|
+
return (0, checkObject_1.checkObject)("urlParams.query", urlParams.query);
|
|
36
|
+
}).then(() => {
|
|
37
|
+
return (0, checkObject_1.checkObject)("urlParams.headers", urlParams.headers);
|
|
38
|
+
}).then(() => {
|
|
39
|
+
return (0, checkObject_1.checkObject)("urlParams.cookies", urlParams.cookies);
|
|
40
|
+
});
|
|
41
|
+
}).then(() => {
|
|
42
|
+
return (0, checkExists_1.checkExists)("bodyParams", bodyParams);
|
|
43
|
+
}).then(() => {
|
|
44
|
+
// search wanted operation
|
|
45
|
+
const foundPathMethod = (0, extractPathMethodByOperationId_1.default)(this._Descriptor.paths, operationId);
|
|
46
|
+
return !foundPathMethod ? Promise.reject(new ReferenceError("Unknown operationId \"" + operationId + "\"")) : new Promise((resolve, reject) => {
|
|
47
|
+
const req = {
|
|
48
|
+
"path": foundPathMethod.path,
|
|
49
|
+
"method": foundPathMethod.method,
|
|
50
|
+
"params": urlParams.path,
|
|
51
|
+
"query": urlParams.query,
|
|
52
|
+
"headers": urlParams.headers,
|
|
53
|
+
"cookies": urlParams.cookies,
|
|
54
|
+
"body": bodyParams
|
|
55
|
+
};
|
|
56
|
+
const validateRequest = this._validator.validate(req.method, req.path); // set to "any" for ts validation
|
|
57
|
+
validateRequest(req, null, (err) => {
|
|
58
|
+
return err ? reject(err) : resolve();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}).catch((err) => {
|
|
62
|
+
return err instanceof express_openapi_validate_1.ValidationError ? Promise.resolve().then(() => {
|
|
63
|
+
switch (err.data[0].keyword) { // extract first Error
|
|
64
|
+
case "required":
|
|
65
|
+
return Promise.reject(new ReferenceError(err.message));
|
|
66
|
+
case "type":
|
|
67
|
+
return Promise.reject(new TypeError(err.message));
|
|
68
|
+
case "minimum":
|
|
69
|
+
case "maximum":
|
|
70
|
+
case "minLength":
|
|
71
|
+
case "maxLength":
|
|
72
|
+
case "minItems":
|
|
73
|
+
case "maxItems":
|
|
74
|
+
case "enum":
|
|
75
|
+
return Promise.reject(new RangeError(err.message));
|
|
76
|
+
default:
|
|
77
|
+
return Promise.reject(new Error(err.message));
|
|
78
|
+
}
|
|
79
|
+
}) : Promise.reject(err);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
// Check sended parameters by method name (used by the Server)
|
|
83
|
+
checkResponse(operationId, res) {
|
|
84
|
+
// parameters validation
|
|
85
|
+
return this.checkDescriptor().then(() => {
|
|
86
|
+
return (0, checkNonEmptyString_1.checkNonEmptyString)("operationId", operationId);
|
|
87
|
+
}).then(() => {
|
|
88
|
+
// search wanted operation
|
|
89
|
+
const foundPathMethod = (0, extractPathMethodByOperationId_1.default)(this._Descriptor.paths, operationId);
|
|
90
|
+
return !foundPathMethod ? Promise.reject(new ReferenceError("Unknown operationId \"" + operationId + "\"")) : new Promise((resolve, reject) => {
|
|
91
|
+
// no content, no validation
|
|
92
|
+
if (204 === res.statusCode) {
|
|
93
|
+
return "undefined" !== typeof res.body ? reject(new ReferenceError("You should not have content data with 204 statusCode")) : resolve();
|
|
94
|
+
}
|
|
95
|
+
// no content, no validation (put requests)
|
|
96
|
+
else if (201 === res.statusCode && "undefined" === typeof res.body) {
|
|
97
|
+
return resolve();
|
|
98
|
+
}
|
|
99
|
+
// validator cannot correctly check pure boolean return
|
|
100
|
+
else if ("undefined" !== typeof res.body && ["true", "false"].includes(res.body)) {
|
|
101
|
+
return resolve();
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
try {
|
|
105
|
+
const mutedRes = Object.assign({}, res);
|
|
106
|
+
mutedRes.headers = res.getHeaders();
|
|
107
|
+
if ("undefined" === typeof mutedRes.body || "" === mutedRes.body) {
|
|
108
|
+
mutedRes.body = {};
|
|
109
|
+
}
|
|
110
|
+
const validateResponse = this._validator.validateResponse(foundPathMethod.method, foundPathMethod.path);
|
|
111
|
+
validateResponse(mutedRes);
|
|
112
|
+
return resolve();
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
return reject(new Error("[" + foundPathMethod.method + "]" +
|
|
116
|
+
foundPathMethod.path +
|
|
117
|
+
" (" + foundPathMethod.operationId + ") => " +
|
|
118
|
+
res.statusCode +
|
|
119
|
+
"\r\n" +
|
|
120
|
+
(e.message ? e.message : e)));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
// init / release
|
|
127
|
+
init(...data) {
|
|
128
|
+
return this._initWorkSpace(...data).then(() => {
|
|
129
|
+
this._validator = new express_openapi_validate_1.OpenApiValidator(this._Descriptor);
|
|
130
|
+
this.initialized = true;
|
|
131
|
+
this.emit("initialized", ...data);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
release(...data) {
|
|
135
|
+
return this._releaseWorkSpace(...data).then(() => {
|
|
136
|
+
// can only be released by Orchestrator
|
|
137
|
+
this._Descriptor = null;
|
|
138
|
+
this._validator = null;
|
|
139
|
+
this.initialized = false;
|
|
140
|
+
this.emit("released", ...data);
|
|
141
|
+
}).then(() => {
|
|
142
|
+
this.removeAllListeners();
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.default = Mediator;
|
|
147
|
+
;
|
package/lib/cjs/utils/send.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { iIncomingMessage, iServerResponse } from "../components/Server";
|
|
2
|
-
export default function send(req: iIncomingMessage, res: iServerResponse, code: number, content:
|
|
3
|
-
"apiVersion": string;
|
|
4
|
-
"cors": boolean;
|
|
5
|
-
"mime": string;
|
|
6
|
-
}): Promise<void>;
|
|
1
|
+
import { iIncomingMessage, iServerResponse } from "../components/Server";
|
|
2
|
+
export default function send(req: iIncomingMessage, res: iServerResponse, code: number, content: any, options: {
|
|
3
|
+
"apiVersion": string;
|
|
4
|
+
"cors": boolean;
|
|
5
|
+
"mime": string;
|
|
6
|
+
}): Promise<void>;
|
package/lib/cjs/utils/send.js
CHANGED
|
@@ -1,54 +1,84 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
// module
|
|
4
|
-
function send(req, res, code, content, options) {
|
|
5
|
-
return
|
|
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
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// module
|
|
4
|
+
function send(req, res, code, content, options) {
|
|
5
|
+
return Promise.resolve().then(() => {
|
|
6
|
+
if (!content) {
|
|
7
|
+
return "";
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
let result = "";
|
|
11
|
+
if (options.mime.includes("application/json")) {
|
|
12
|
+
if ("string" === typeof content) {
|
|
13
|
+
try {
|
|
14
|
+
JSON.parse(content); // is content json & parseable ?
|
|
15
|
+
result = content; // yes => valid JSON string, send it as it is
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
result = JSON.stringify(content); // no => not JSON string, send formatted one
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
const stringified = JSON.stringify(content);
|
|
23
|
+
if (stringified !== content) {
|
|
24
|
+
result = stringified;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
result = content;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
}).then((stringifiedContent) => {
|
|
34
|
+
return new Promise((resolve) => {
|
|
35
|
+
// force data for checking
|
|
36
|
+
res.statusCode = code;
|
|
37
|
+
res.headers = Object.assign({
|
|
38
|
+
"Content-Type": options.mime,
|
|
39
|
+
"Content-Length": stringifiedContent ? Buffer.byteLength(stringifiedContent) : 0,
|
|
40
|
+
"Status-Code-Url-Cat": "https://http.cat/" + code,
|
|
41
|
+
"API-Version": options.apiVersion
|
|
42
|
+
}, options.cors ? {
|
|
43
|
+
"Access-Control-Allow-Origin": "*",
|
|
44
|
+
"Access-Control-Allow-Credentials": true,
|
|
45
|
+
"Access-Control-Allow-Methods": req.headers["access-control-request-method"] ? req.headers["access-control-request-method"] : [
|
|
46
|
+
"GET",
|
|
47
|
+
"POST",
|
|
48
|
+
"PUT",
|
|
49
|
+
"DELETE",
|
|
50
|
+
"PATCH",
|
|
51
|
+
"OPTIONS"
|
|
52
|
+
].join(", "),
|
|
53
|
+
"Access-Control-Allow-Headers": req.headers["access-control-request-headers"] ? req.headers["access-control-request-headers"] : [
|
|
54
|
+
"Origin",
|
|
55
|
+
"Accept",
|
|
56
|
+
"Accept-Version",
|
|
57
|
+
"Content-Length",
|
|
58
|
+
"Content-MD5",
|
|
59
|
+
"Content-Type",
|
|
60
|
+
"Date",
|
|
61
|
+
"X-Api-Version",
|
|
62
|
+
"X-File-Name",
|
|
63
|
+
"X-CSRF-Token",
|
|
64
|
+
"X-Requested-With"
|
|
65
|
+
].join(", ")
|
|
66
|
+
} : {});
|
|
67
|
+
// send data
|
|
68
|
+
res.writeHead(res.statusCode, res.headers);
|
|
69
|
+
if ("" !== stringifiedContent) {
|
|
70
|
+
res.body = content; // for Mediator response validator
|
|
71
|
+
res.end(stringifiedContent, "utf-8", () => {
|
|
72
|
+
resolve();
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
res.end(() => {
|
|
77
|
+
resolve();
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
exports.default = send;
|
|
84
|
+
;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
|
|
3
3
|
"name": "node-pluginsmanager-plugin",
|
|
4
|
-
"version": "6.0.
|
|
4
|
+
"version": "6.0.3",
|
|
5
5
|
"description": "An abstract parent plugin for node-pluginsmanager",
|
|
6
6
|
|
|
7
7
|
"type": "commonjs",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/express": "4.17.21",
|
|
48
|
-
"@types/node": "20.10.
|
|
48
|
+
"@types/node": "20.10.3",
|
|
49
49
|
"@types/socket.io": "3.0.2",
|
|
50
50
|
"@types/uniqid": "5.3.4",
|
|
51
51
|
"@types/ws": "8.5.10",
|