Skip to content

Commit

Permalink
Merge pull request #39 from Psychopoulet/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Psychopoulet committed Feb 24, 2020
2 parents d8ad289 + 0157797 commit c1b3aa3
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 82 deletions.
4 changes: 2 additions & 2 deletions lib/checkers/RangeError/checkStringLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}).then(() => {

return length !== data.length ? Promise.reject(new RangeError(
"\"" + dataName + "\" must be equal to " + length
"\"" + dataName + "\" length must be equal to " + length
)) : Promise.resolve();

});
Expand All @@ -52,7 +52,7 @@
if (!err && length !== data.length) {

err = new RangeError(
"\"" + dataName + "\" must be equal to " + length
"\"" + dataName + "\" length must be equal to " + length
);

}
Expand Down
4 changes: 4 additions & 0 deletions lib/components/Orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ module.exports = class Orchestrator extends MediatorUser {
this.emit("error", err);
});

return Promise.resolve();

});

// create Server
Expand All @@ -441,6 +443,8 @@ module.exports = class Orchestrator extends MediatorUser {
this.emit("error", err);
});

return Promise.resolve();

});

// init Server (before Mediator)
Expand Down
35 changes: 32 additions & 3 deletions lib/components/Server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
eslint complexity: 0, max-statements: 0, no-undefined: 0
eslint-disable complexity, max-statements, no-undefined, max-lines
*/

"use strict";
Expand All @@ -11,6 +11,9 @@
const { parse } = require("url");
const { EOL } = require("os");

// externals
let SwaggerParser = null; // hotload

// locals
const MediatorUser = require(join(__dirname, "MediatorUser.js"));
const { checkInteger, checkNonEmptyObject, checkNonEmptyString } = require(join(__dirname, "..", "checkers", "main.js"));
Expand Down Expand Up @@ -104,8 +107,34 @@ module.exports = class Server extends MediatorUser {

if ("/" + this._Descriptor.info.title + "/descriptor" === req.pattern && "get" === req.method) {

this._log("info", "<= [" + req.validatedIp + "] " + JSON.stringify(this._Descriptor));
send(req, res, this._Descriptor, 200);
new Promise((resolve, reject) => {

if (null === SwaggerParser) {
SwaggerParser = require("swagger-parser");
}

SwaggerParser.validate(this._Descriptor, (err, api) => {
return err ? reject(err) : resolve(api);
});

}).then((api) => {

this._log("info", "<= [" + req.validatedIp + "] " + JSON.stringify(api));
send(req, res, api, 200);

}).catch((err) => {

this.emit("error", err);

const result = {
"code": "INTERNAL_SERVER_ERROR",
"message": errToString(err)
};

this._log("error", "<= " + JSON.stringify(result));
send(req, res, result, 500);

});

}

Expand Down
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-pluginsmanager-plugin",
"version": "4.0.2",
"version": "4.1.0",
"description": "An abstract parent plugin for node-pluginsmanager",
"main": "lib/main.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -39,19 +39,19 @@
"express-openapi-validate": "~0.5.0"
},
"devDependencies": {
"@types/node": "13.7.0",
"@types/ws": "7.2.1",
"@types/socket.io": "2.1.4",
"check-version-modules": "1.3.0",
"coveralls": "3.0.9",
"colors": "1.4.0",
"eslint": "6.8.0",
"express": "4.17.1",
"husky": "4.2.1",
"mocha": "7.0.1",
"nyc": "15.0.0",
"socket.io": "2.3.0",
"ws": "7.2.1"
"@types/node": "~13.7.4",
"@types/ws": "~7.2.1",
"@types/socket.io": "~2.1.4",
"check-version-modules": "~1.3.0",
"coveralls": "~3.0.9",
"colors": "~1.4.0",
"eslint": "~6.8.0",
"express": "~4.17.1",
"husky": "~4.2.3",
"mocha": "~7.0.1",
"nyc": "~15.0.0",
"socket.io": "~2.3.0",
"ws": "~7.2.1"
},
"homepage": "https://github.com/Psychopoulet/node-pluginsmanager-plugin#readme",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion test/7_Orchestrator_5_requests_1_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"packageFile": join(__dirname, "..", "package.json"),
"descriptorFile": join(__dirname, "utils", "DescriptorUser", "Descriptor.json"),
"mediatorFile": join(__dirname, "utils", "Mediator", "HeritedMediator.js"),
"serverFile": join(__dirname, "utils", "Server", "HeritedServer.js"),
"serverFile": join(__dirname, "utils", "Server", "ServerWithSockets.js"),
"logger": (type, log) => {

let message = MAX_LENGTH_LOGS < log.length ? log.substr(0, MAX_LENGTH_LOGS) + "..." : log;
Expand Down
2 changes: 1 addition & 1 deletion test/7_Orchestrator_5_requests_2_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"packageFile": join(__dirname, "..", "package.json"),
"descriptorFile": join(__dirname, "utils", "DescriptorUser", "Descriptor.json"),
"mediatorFile": join(__dirname, "utils", "Mediator", "HeritedMediator.js"),
"serverFile": join(__dirname, "utils", "Server", "HeritedServer.js")
"serverFile": join(__dirname, "utils", "Server", "ServerWithSockets.js")
};

// tests
Expand Down
2 changes: 1 addition & 1 deletion test/utils/DescriptorUser/Descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.2",
"info": {
"title": "node-pluginsmanager-plugin",
"version": "4.0.2",
"version": "4.1.0",
"description": "An abstract parent plugin for node-pluginsmanager",
"contact": {
"name": "Sébastien VIDAL",
Expand Down
58 changes: 2 additions & 56 deletions test/utils/Server/HeritedServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,15 @@
// locals

// plugin
const { Server } = require(join(__dirname, "..", "..", "..", "lib", "main.js"));
const { readJSONFile } = require(join(__dirname, "..", "..", "..", "lib", "utils", "file", "main.js"));

// utils
const HeritedMediator = require(join(__dirname, "..", "Mediator", "HeritedMediator.js"));
const ServerWithSockets = require(join(__dirname, "..", "Server", "ServerWithSockets.js"));

// module

module.exports = class HeritedServer extends Server {

constructor (opt) {

super(opt);

this._onConnection = null;

}
module.exports = class HeritedServer extends ServerWithSockets {

init (...data) {

Expand All @@ -45,50 +37,4 @@ module.exports = class HeritedServer extends Server {

}

_releaseWorkSpace () {

return this._socketServer ? Promise.resolve().then(() => {

if ("function" === typeof this._onConnection) {

this._socketServer.removeListener("connection", this._onConnection);
this._onConnection = null;

}

}) : Promise.resolve();

}

socketMiddleware (server) {

super.socketMiddleware(server);

this._onConnection = (socket) => {

socket.on("message", (payload) => {

const req = JSON.parse(payload);

// check plugin
if ("string" === typeof req.plugin && this._Descriptor.info.title === req.plugin) {

// check command (switch ?)
if ("string" === typeof req.command && "ping" === req.command) {

this.emit("ping");
this.push("pong", "test");

}

}

});

};

this._socketServer.on("connection", this._onConnection);

}

};
71 changes: 71 additions & 0 deletions test/utils/Server/ServerWithSockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"use strict";

// deps

// natives
const { join } = require("path");

// locals

// plugin
const { Server } = require(join(__dirname, "..", "..", "..", "lib", "main.js"));

// module

module.exports = class ServerWithSockets extends Server {

constructor (opt) {

super(opt);

this._onConnection = null;

}

_releaseWorkSpace () {

return this._socketServer ? Promise.resolve().then(() => {

if ("function" === typeof this._onConnection) {

this._socketServer.removeListener("connection", this._onConnection);
this._onConnection = null;

}

}) : Promise.resolve();

}

socketMiddleware (server) {

super.socketMiddleware(server);

this._onConnection = (socket) => {

socket.on("message", (payload) => {

const req = JSON.parse(payload);

// check plugin
if ("string" === typeof req.plugin && this._Descriptor.info.title === req.plugin) {

// check command (switch ?)
if ("string" === typeof req.command && "ping" === req.command) {

this.emit("ping");
this.push("pong", "test");

}

}

});

};

this._socketServer.on("connection", this._onConnection);

}

};
4 changes: 2 additions & 2 deletions test/utils/Server/checkUrlValidPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

// module

module.exports = function checkUrlValidPaths (URL_API, server) {
module.exports = function checkUrlValidPaths (URL_API, component) {

describe("check valid paths", () => {

it("should test descriptor request", () => {

return httpRequestTest("/node-pluginsmanager-plugin/descriptor", "get", null, 200, "OK", server._Descriptor);
return httpRequestTest("/node-pluginsmanager-plugin/descriptor", "get", null, 200, "OK", component._Descriptor);

});

Expand Down
4 changes: 2 additions & 2 deletions test/utils/Server/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

// module

module.exports = function test (server) {
module.exports = function test (descriptor) {

describe("check url", () => {

checkUrlWrongPaths(URL_API);
checkUrlValidPaths(URL_API, server);
checkUrlValidPaths(URL_API, descriptor);
checkUrlPathParameters(URL_API);
checkUrlQueryParameters(URL_API);
checkUrlCookieParameters(URL_API);
Expand Down

0 comments on commit c1b3aa3

Please sign in to comment.