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