node-pluginsmanager-plugin 6.0.2 → 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.
@@ -8,7 +8,6 @@ const checkObject_1 = require("../checkers/TypeError/checkObject");
8
8
  const checkNonEmptyString_1 = require("../checkers/RangeError/checkNonEmptyString");
9
9
  const checkNonEmptyObject_1 = require("../checkers/RangeError/checkNonEmptyObject");
10
10
  const extractPathMethodByOperationId_1 = __importDefault(require("../utils/descriptor/extractPathMethodByOperationId"));
11
- const jsonParser_1 = __importDefault(require("../utils/jsonParser"));
12
11
  const DescriptorUser_1 = __importDefault(require("./DescriptorUser"));
13
12
  // types & interfaces
14
13
  // externals
@@ -108,9 +107,6 @@ class Mediator extends DescriptorUser_1.default {
108
107
  if ("undefined" === typeof mutedRes.body || "" === mutedRes.body) {
109
108
  mutedRes.body = {};
110
109
  }
111
- else {
112
- mutedRes.body = (0, jsonParser_1.default)(mutedRes.body);
113
- }
114
110
  const validateResponse = this._validator.validateResponse(foundPathMethod.method, foundPathMethod.path);
115
111
  validateResponse(mutedRes);
116
112
  return resolve();
@@ -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,5 +1,5 @@
1
1
  import { iIncomingMessage, iServerResponse } from "../components/Server";
2
- export default function send(req: iIncomingMessage, res: iServerResponse, code: number, content: string, options: {
2
+ export default function send(req: iIncomingMessage, res: iServerResponse, code: number, content: any, options: {
3
3
  "apiVersion": string;
4
4
  "cors": boolean;
5
5
  "mime": string;
@@ -2,52 +2,82 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  // module
4
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
- });
5
+ return Promise.resolve().then(() => {
6
+ if (!content) {
7
+ return "";
45
8
  }
46
9
  else {
47
- res.end(() => {
48
- resolve();
49
- });
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;
50
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
+ });
51
81
  });
52
82
  }
53
83
  exports.default = send;
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.4",
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",