Skip to content

Commit

Permalink
Merge pull request #28 from Psychopoulet/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Psychopoulet committed Nov 18, 2019
2 parents 51a5bf2 + d00ac34 commit 3452013
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 37 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: node_js
node_js:
- "8"
- "10"
- "12"
install:
- "npm install -g typescript"
- "npm install"
Expand Down
6 changes: 5 additions & 1 deletion documentation/DescriptorUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ More, the class add features like external ressources directory (to create files
## Interfaces

```typescript
type tLogType = "log" | "info" | "warning" | "error";

interface iDescriptorUserOptions {
"descriptor": object | null;
"externalRessourcesDirectory": string; // used to write local data like sqlite database, json files, pictures, etc...
Expand All @@ -27,9 +29,10 @@ interface iDescriptorUserOptions {

#### protected

* ``` protected _Descriptor: object | null; ``` provided by "descriptor" option, sent by the [Orchestrator](./Orchestrator.md)
* ``` protected _externalRessourcesDirectory: string; ``` provided by "externalRessourcesDirectory" option, sent by the [Orchestrator](./Orchestrator.md)
* ``` protected _descriptorValidated: boolean; ``` if checkDescriptor is already successfuly executed, do not execute it again (for performances)
* ``` protected _Descriptor: object | null; ``` provided by "descriptor" option, sent by the [Orchestrator](./Orchestrator.md)
* ``` protected _Logger: Function | null; ``` provided by "logger" option, sent by the [Orchestrator](./Orchestrator.md)

### Constructor

Expand All @@ -43,6 +46,7 @@ interface iDescriptorUserOptions {
* ``` protected _initWorkSpace(data?: any): Promise<void>; ``` Used to avoid full init logic re-writting.
* ``` protected _releaseWorkSpace(data?: any): Promise<void>; ``` Used to avoid full release logic re-writting.
* ``` protected _log(type: tLogType, message: string, bold?: boolean): this; ``` Use "logger" function if provided, do nothing if not

#### public

Expand Down
21 changes: 19 additions & 2 deletions lib/components/DescriptorUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
checkDescriptorPaths
} = require(join(__dirname, "..", "utils", "checkDescriptor", "main.js"));

// consts

const LOG_TYPES_ALLOWED = [ "log", "info", "warning", "error" ];

// module

module.exports = class DescriptorUser extends Events {
Expand All @@ -25,11 +29,14 @@ module.exports = class DescriptorUser extends Events {

this._descriptorValidated = false;

this._externalRessourcesDirectory = options && "string" === typeof options.externalRessourcesDirectory ?
options.externalRessourcesDirectory : "";

this._Descriptor = options && "undefined" !== typeof options.descriptor ?
options.descriptor : null;

this._externalRessourcesDirectory = options && "string" === typeof options.externalRessourcesDirectory ?
options.externalRessourcesDirectory : "";
this._Logger = options && "undefined" !== typeof options.logger ?
options.logger : null;

}

Expand All @@ -49,6 +56,16 @@ module.exports = class DescriptorUser extends Events {

}

_log (type, message, bold = false) {

if (message && "function" === typeof this._Logger && LOG_TYPES_ALLOWED.includes(type)) {
this._Logger(type, message, bold);
}

return this;

}

// public

checkDescriptor () {
Expand Down
6 changes: 4 additions & 2 deletions lib/components/Orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ module.exports = class Orchestrator extends MediatorUser {

this._Mediator = new PluginMediator({
"descriptor": this._Descriptor,
"externalRessourcesDirectory": this._externalRessourcesDirectory
"externalRessourcesDirectory": this._externalRessourcesDirectory,
"logger": this._Logger
});

return this.checkMediator().then(() => {
Expand All @@ -413,7 +414,8 @@ module.exports = class Orchestrator extends MediatorUser {
this._Server = new PluginServer({
"descriptor": this._Descriptor,
"mediator": this._Mediator,
"externalRessourcesDirectory": this._externalRessourcesDirectory
"externalRessourcesDirectory": this._externalRessourcesDirectory,
"logger": this._Logger
});

return this.checkServer().then(() => {
Expand Down
109 changes: 86 additions & 23 deletions lib/components/Server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
eslint complexity: 0
eslint complexity: 0, max-statements: 0
*/

"use strict";
Expand All @@ -9,6 +9,7 @@
// natives
const { join } = require("path");
const { parse } = require("url");
const { EOL } = require("os");

// locals
const MediatorUser = require(join(__dirname, "MediatorUser.js"));
Expand Down Expand Up @@ -62,6 +63,11 @@ module.exports = class Server extends MediatorUser {
const { pathname, query } = parse(req.url, true);
const method = req.method.toLowerCase();

const ip = ((req.headers["x-forwarded-for"] || "").split(",").pop() ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress).replace("::ffff:", "");

const path = extractPath(this._Descriptor.paths, pathname);

if (!path || !this._Descriptor.paths[path] || !this._Descriptor.paths[path][method]) {
Expand All @@ -70,27 +76,46 @@ module.exports = class Server extends MediatorUser {

const { operationId } = this._Descriptor.paths[path][method];

this._log("info", "" +
"=> [" + ip + "] " + req.url + " (" + method.toUpperCase() + ")" +
(operationId ? EOL + "operationId : " + operationId : "") +
(req.headers["content-type"] ? EOL + "content-type : " + req.headers["content-type"] : "") +
(
"get" !== method && req.headers["content-length"] && 4 < req.headers["content-length"] ?
EOL + "content-length : " + req.headers["content-length"] : ""
)
);

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

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

}

// missing operationId
else if (!operationId) {

send(req, res, {
const result = {
"code": "NOT_IMPLEMENTED",
"message": "Missing \"operationId\" in the Descriptor for this request"
}, 501);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 501);

}

// not implemented operationId
else if ("function" !== typeof this._Mediator[operationId]) {

send(req, res, {
const result = {
"code": "NOT_IMPLEMENTED",
"message": "Unknown Mediator's \"operationId\" method for this request"
}, 501);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 501);

}

Expand All @@ -101,10 +126,13 @@ module.exports = class Server extends MediatorUser {
"undefined" === typeof req.headers["content-type"]
) {

send(req, res, {
const result = {
"code": "MISSING_HEADER",
"message": "No \"Content-Type\" header found"
}, 400);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 400);

}

Expand All @@ -115,10 +143,13 @@ module.exports = class Server extends MediatorUser {
"undefined" === typeof req.headers["content-length"]
)) {

send(req, res, {
const result = {
"code": "MISSING_HEADER",
"message": "No \"Content-Length\" header found"
}, 411);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 411);

}

Expand All @@ -128,10 +159,13 @@ module.exports = class Server extends MediatorUser {
Number.isNaN(parseInt(req.headers["content-length"], 10))
) {

send(req, res, {
const result = {
"code": "MISSING_HEADER",
"message": "\"Content-Length\" header is not a number"
}, 411);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 411);

}

Expand Down Expand Up @@ -180,6 +214,11 @@ module.exports = class Server extends MediatorUser {
else {

try {

if (queryData.length) {
this._log("log", queryData);
}

resolve(JSON.parse(queryData));
}
catch (e) {
Expand Down Expand Up @@ -229,63 +268,82 @@ module.exports = class Server extends MediatorUser {

// created
if ("put" === method) {
this._log("success", "<= [" + ip + "] " + content);
send(req, res, content, 201);
}

// no content get
else if ("get" === method && ("undefined" === typeof content || null === content)) {
this._log("warning", "<= [" + ip + "]");
send(req, res, content, 404);
}

// no content
else if ("undefined" === typeof content) {
this._log("warning", "<= [" + ip + "]");
send(req, res, content, 204);
}

else {
this._log("warning", "<= [" + ip + "] " + JSON.stringify(content));
send(req, res, content, 200);
}

}).catch((err) => {

if (err instanceof ReferenceError) {

send(req, res, {
const result = {
"code": "MISSING_PARAMETER",
"message": errToString(err)
}, 400);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 400);

}
else if (err instanceof TypeError) {

send(req, res, {
const result = {
"code": "WRONG_TYPE_PARAMETER",
"message": errToString(err)
}, 400);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 400);

}
else if (err instanceof RangeError) {

send(req, res, {
const result = {
"code": "RANGE_OR_EMPTY_PARAMETER",
"message": errToString(err)
}, 400);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 400);

}
else if (err instanceof SyntaxError) {

send(req, res, {
const result = {
"code": "JSON_PARSE",
"message": errToString(err)
}, 400);
};

this._log("error", "<= [" + ip + "] " + JSON.stringify(result));
send(req, res, result, 400);

}
else {

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

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

}

Expand All @@ -299,10 +357,13 @@ module.exports = class Server extends MediatorUser {

this.emit("error", err);

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

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

});

Expand All @@ -329,6 +390,8 @@ module.exports = class Server extends MediatorUser {

result = JSON.stringify(result);

this._log("info", "<= [PUSH] " + result, true);

// WebSocket
if (this._socketServer.clients && "function" === typeof this._socketServer.clients.forEach) {

Expand Down
10 changes: 8 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ declare module "node-pluginsmanager-plugin" {

// options

type tLogType = "log" | "info" | "warning" | "error";

interface iDescriptorUserOptions {
"descriptor": object | null;
"externalRessourcesDirectory": string;
Expand Down Expand Up @@ -40,10 +42,13 @@ declare module "node-pluginsmanager-plugin" {

// protected

protected _Descriptor: object | null;
protected _externalRessourcesDirectory: string;
protected _descriptorValidated: boolean;

protected _externalRessourcesDirectory: string;

protected _Descriptor: object | null;
protected _Logger: Function | null;

// constructor

constructor (options: iDescriptorUserOptions);
Expand All @@ -54,6 +59,7 @@ declare module "node-pluginsmanager-plugin" {

protected _initWorkSpace(data?: any): Promise<void>;
protected _releaseWorkSpace(data?: any): Promise<void>;
protected _log(type: tLogType, message: string, bold?: boolean): this;

// public

Expand Down

0 comments on commit 3452013

Please sign in to comment.