node-pluginsmanager-plugin 6.0.3 → 6.0.4

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,147 +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 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
+ "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
+ ;
@@ -319,7 +319,7 @@ class Server extends MediatorUser_1.default {
319
319
  });
320
320
  }
321
321
  else {
322
- this._log("success", "<= [" + req.validatedIp + "] " + content);
322
+ this._log("success", "<= [" + req.validatedIp + "] " + JSON.stringify(content));
323
323
  return (0, send_1.default)(req, res, serverCodes_1.default.OK_PUT, content, {
324
324
  "apiVersion": apiVersion,
325
325
  "cors": this._cors,
@@ -337,7 +337,7 @@ class Server extends MediatorUser_1.default {
337
337
  });
338
338
  }
339
339
  else {
340
- this._log("success", "<= [" + req.validatedIp + "] " + content);
340
+ this._log("success", "<= [" + req.validatedIp + "] " + JSON.stringify(content));
341
341
  return (0, send_1.default)(req, res, serverCodes_1.default.OK, content, {
342
342
  "apiVersion": apiVersion,
343
343
  "cors": this._cors,
@@ -1,6 +1,6 @@
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
+ 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,84 +1,84 @@
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
- ;
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.3",
4
+ "version": "6.0.4",
5
5
  "description": "An abstract parent plugin for node-pluginsmanager",
6
6
 
7
7
  "type": "commonjs",