From 9488ffed5d5733e0e33f100120dcabd89440a712 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 12:04:35 +0200 Subject: [PATCH 01/11] Delete suite-dummy.html --- Client/WebServer/suite-dummy.html | 54 ------------------------------- 1 file changed, 54 deletions(-) delete mode 100644 Client/WebServer/suite-dummy.html diff --git a/Client/WebServer/suite-dummy.html b/Client/WebServer/suite-dummy.html deleted file mode 100644 index 78b108803..000000000 --- a/Client/WebServer/suite-dummy.html +++ /dev/null @@ -1,54 +0,0 @@ -

Trading Engine

- -
- - - - - -The trading engine hierarchy is the data structure used by the trading bot to keep runtime information highly accessible and exposed to others. - -
- -

You will use this hierarchy for two main purposes:

- -

The hierarchy exposes—literally—all the information processed by the trading bot, providing a comprehensive feedback loop to trading systems and analysis capabilities to users. The hierarchy exposes—literally—all the information processed by the trading bot, providing a comprehensive feedback loop to trading systems and analysis capabilities to users.

- - - -

The hierarchy exposes—literally—all the information processed by the trading bot, providing a comprehensive feedback loop to trading systems and analysis capabilities to users.

- -
Este callout se usa para resaltar una idea particularmente importante. Algo se sobresale, como una conclusion, o algo por el estilo.
- -

The hierarchy exposes all the information processed by the trading bot, providing a comprehensive feedback loop to trading systems and analysis capabilities to users. The hierarchy exposes all the information processed by the trading bot, providing a comprehensive feedback loop to trading systems and analysis capabilities to users.

- - - - - - - - - -

Adding a Trading Engine Node

- -

Calculo que con este segundo nivel de titulo alcanza.

- -

Este es otro formato tipico de la doc: Trading Engine

- -
- - - - - -The trading engine hierarchy is the data structure used by the trading bot to keep runtime information highly accessible and exposed to others. - -
From 9247044dc0a89bca57ea6cb101b84e0a5cd7f004 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 13:13:19 +0200 Subject: [PATCH 02/11] Cleaning HTTP Interface --- Client/client.js | 4 +- Client/httpInterface.js | 981 ++++---- Desktop/httpInterface.js | 2121 +++++++++++++++++ Environment.js | 2 +- EnvironmentForDebug.js | 3 +- .../Sensor-Bot/Webhooks/CheckWebhook.js | 2 +- 6 files changed, 2588 insertions(+), 525 deletions(-) create mode 100644 Desktop/httpInterface.js diff --git a/Client/client.js b/Client/client.js index 07990149a..74fdb60c2 100644 --- a/Client/client.js +++ b/Client/client.js @@ -6,7 +6,7 @@ global.env = ENVIRONMENT_MODULE process.on('uncaughtException', function (err) { if (err.message && err.message.indexOf("EADDRINUSE") > 0) { - console.log("A Superalgos Client cannot be started. Reason: the port configured migth be being used by another application.") + console.log("A Superalgos Client cannot be started. Reason: the port configured migth be being used by another application, or Superalgos might be already running.") return } console.log('[ERROR] Client -> client-> uncaughtException -> err.message = ' + err.message) @@ -168,7 +168,7 @@ try { HTTP_INTERFACE = HTTP_INTERFACE.newHttpInterface() HTTP_INTERFACE.initialize() HTTP_INTERFACE.run() - console.log('Http Interface .............................................. Listening at port ' + global.env.HTTP_INTERFACE_PORT) + console.log('Http Interface .............................................. Listening at port ' + global.env.CLIENT_HTTP_INTERFACE_PORT) console.log('') console.log("You are running Superalgos Beta 11") diff --git a/Client/httpInterface.js b/Client/httpInterface.js index f18721f62..c2cf65f27 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -13,12 +13,8 @@ exports.newHttpInterface = function newHttpInterface() { run: run } - const open = require('open') - - let port = global.env.HTTP_INTERFACE_PORT - - let http = require('http') - let isHttpServerStarted = false + const OPEN_NODE_MODULE = require('open') + const HTTP_NODE_MODULE = require('http') let webhook = new Map() @@ -33,29 +29,19 @@ exports.newHttpInterface = function newHttpInterface() { } function run() { - startHtttpServer() - } - - function startHtttpServer() { - - try { - if (isHttpServerStarted === false) { - gWebServer = http.createServer(onBrowserRequest).listen(port) - isHttpServerStarted = true - /* Starting the browser now is optional */ - if (process.argv.includes("noBrowser")) { - //Running Client only with no UI. - } else { - open('http://localhost:' + port) - } - - } - } catch (err) { - console.log('[ERROR] httpInterface -> startHtttpServer -> Error = ' + err.stack) + /* + We will create an HTTP Server and leave it running forever. + */ + HTTP_NODE_MODULE.createServer(onHttpRequest).listen(global.env.CLIENT_HTTP_INTERFACE_PORT) + /* Starting the browser now is optional */ + if (process.argv.includes("noBrowser")) { + //Running Client only with no UI. + } else { + OPEN_NODE_MODULE('http://localhost:' + global.env.CLIENT_HTTP_INTERFACE_PORT) } } - function onBrowserRequest(httpRequest, httpResponse) { + function onHttpRequest(httpRequest, httpResponse) { function getBody(callback) { // Gets the de body from a POST httpRequest to the web server try { @@ -75,17 +61,16 @@ exports.newHttpInterface = function newHttpInterface() { }) httpRequest.on('error', function (err) { - console.log('[INFO] httpInterface -> onBrowserRequest -> getBody -> err = ' + err.stack) + console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) }) } catch (err) { - console.log('[INFO] httpInterface -> onBrowserRequest -> getBody -> err = ' + err.stack) + console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } } - let requestParameters = httpRequest.url.split('/') - + let requestParameters requestParameters = httpRequest.url.split('?') // Remove version information requestParameters = requestParameters[0].split('/') @@ -177,9 +162,8 @@ exports.newHttpInterface = function newHttpInterface() { respondWithContent(JSON.stringify(error), httpResponse) } } - break } - + break case 'CCXT': { getBody(processRequest) @@ -255,10 +239,8 @@ exports.newHttpInterface = function newHttpInterface() { respondWithContent(JSON.stringify(error), httpResponse) } } - break } break - case 'Webhook': { switch (requestParameters[2]) { // switch by command @@ -338,10 +320,8 @@ exports.newHttpInterface = function newHttpInterface() { break } } - break } break - case 'Docs': { switch (requestParameters[2]) { // switch by command @@ -659,488 +639,485 @@ exports.newHttpInterface = function newHttpInterface() { } } break + case 'App': + { + switch (requestParameters[2]) { // switch by command + case 'Contribute': { + try { + let commitMessage = unescape(requestParameters[3]) + const username = unescape(requestParameters[4]) + const token = unescape(requestParameters[5]) + const currentBranch = unescape(requestParameters[6]) + const contributionsBranch = unescape(requestParameters[7]) + let error - case 'App': { - switch (requestParameters[2]) { // switch by command - case 'Contribute': { - try { - let commitMessage = unescape(requestParameters[3]) - const username = unescape(requestParameters[4]) - const token = unescape(requestParameters[5]) - const currentBranch = unescape(requestParameters[6]) - const contributionsBranch = unescape(requestParameters[7]) - let error - - /* Unsavping # */ - for (let i = 0; i < 10; i++) { - commitMessage = commitMessage.replace('_SLASH_', '/') - commitMessage = commitMessage.replace('_HASHTAG_', '#') - } - - contribute() - - async function contribute() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - await doGit() - if (error !== undefined) { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Contribution Not Sent', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, error) - return - } - - await doGithub() - if (error !== undefined) { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Contribution Not Sent', - anchor: undefined, - placeholder: {} - } - console.log('respond with docs ') - - respondWithDocsObject(docs, error) - return - } - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + /* Unsavping # */ + for (let i = 0; i < 10; i++) { + commitMessage = commitMessage.replace('_SLASH_', '/') + commitMessage = commitMessage.replace('_HASHTAG_', '#') } - } - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) + contribute() - try { - await git.add('./*') - await git.commit(commitMessage) - await git.push('origin', currentBranch) - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> contributionsBranch = ' + contributionsBranch) - console.log('') - console.log('Torubleshooting Tips:') - console.log('') - console.log('1. Make sure that you have set up your Github Username and Token at the APIs -> Github API node at the workspace.') - console.log('2. Make sure you are running the latest version of Git available for your OS.') - console.log('3. Make sure that you have cloned your Superalgos repository fork, and not the main Superalgos repository.') - console.log('4. If your fork is old, you might need to do an app.update and also a node setup at every branch. If you just reforked all is good.') - error = err - } - } - - async function doGithub() { - - const { Octokit } = require("@octokit/rest") - - const octokit = new Octokit({ - auth: token, - userAgent: 'Superalgos Beta 11' - }) - - const repo = 'Superalgos' - const owner = 'Superalgos' - const head = username + ':' + contributionsBranch - const base = currentBranch - const title = 'Contribution: ' + commitMessage - - try { - await octokit.pulls.create({ - owner, - repo, - title, - head, - base, - }); - } catch (err) { - if (err.stack.indexOf('A pull request already exists') >= 0) { - return + async function contribute() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') } else { - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10)) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch) + await doGit() + if (error !== undefined) { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Contribution Not Sent', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + return + } + + await doGithub() + if (error !== undefined) { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Contribution Not Sent', + anchor: undefined, + placeholder: {} + } + console.log('respond with docs ') + + respondWithDocsObject(docs, error) + return + } + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + + try { + await git.add('./*') + await git.commit(commitMessage) + await git.push('origin', currentBranch) + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> contributionsBranch = ' + contributionsBranch) + console.log('') + console.log('Torubleshooting Tips:') + console.log('') + console.log('1. Make sure that you have set up your Github Username and Token at the APIs -> Github API node at the workspace.') + console.log('2. Make sure you are running the latest version of Git available for your OS.') + console.log('3. Make sure that you have cloned your Superalgos repository fork, and not the main Superalgos repository.') + console.log('4. If your fork is old, you might need to do an app.update and also a node setup at every branch. If you just reforked all is good.') error = err } } - } - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Contribute -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> username = ' + username) - console.log('[ERROR] httpInterface -> App -> Contribute -> token starts with = ' + token.substring(0, 10) + '...') - console.log('[ERROR] httpInterface -> App -> Contribute -> token ends with = ' + '...' + token.substring(token.length - 10)) - console.log('[ERROR] httpInterface -> App -> Contribute -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> contributionsBranch = ' + contributionsBranch) + async function doGithub() { - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - break - } + const { Octokit } = require("@octokit/rest") - case 'Update': { - try { - const currentBranch = unescape(requestParameters[3]) - update() + const octokit = new Octokit({ + auth: token, + userAgent: 'Superalgos Beta 11' + }) - async function update() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - let result = await doGit() + const repo = 'Superalgos' + const owner = 'Superalgos' + const head = username + ':' + contributionsBranch + const base = currentBranch + const title = 'Contribution: ' + commitMessage - if (result.error === undefined) { - let customResponse = { - result: global.CUSTOM_OK_RESPONSE.result, - message: result.message + try { + await octokit.pulls.create({ + owner, + repo, + title, + head, + base, + }); + } catch (err) { + if (err.stack.indexOf('A pull request already exists') >= 0) { + return + } else { + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10)) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch) + error = err } - respondWithContent(JSON.stringify(customResponse), httpResponse) - } else { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Update Failed', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, result.error) - } } - } - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Contribute -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> username = ' + username) + console.log('[ERROR] httpInterface -> App -> Contribute -> token starts with = ' + token.substring(0, 10) + '...') + console.log('[ERROR] httpInterface -> App -> Contribute -> token ends with = ' + '...' + token.substring(token.length - 10)) + console.log('[ERROR] httpInterface -> App -> Contribute -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> contributionsBranch = ' + contributionsBranch) - let message - try { - message = await git.pull('https://github.com/Superalgos/Superalgos', currentBranch) - return { message: message } - } catch (err) { - console.log('[ERROR] Error updating ' + currentBranch) - console.log(err.stack) - return { error: err } + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack } + respondWithContent(JSON.stringify(error), httpResponse) } - - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) + break } - break - } - case 'Checkout': { - try { - const currentBranch = unescape(requestParameters[3]) - let error + case 'Update': { + try { + const currentBranch = unescape(requestParameters[3]) + update() - checkout() - - async function checkout() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - await doGit() - - if (error === undefined) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + async function update() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') } else { - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'Switching Branches - Current Branch Not Changed', - anchor: undefined, - placeholder: {} - } + let result = await doGit() - respondWithDocsObject(docs, error) + if (result.error === undefined) { + let customResponse = { + result: global.CUSTOM_OK_RESPONSE.result, + message: result.message + } + respondWithContent(JSON.stringify(customResponse), httpResponse) + } else { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Update Failed', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, result.error) + + } } } - } - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) - try { - await git.checkout(currentBranch) - } catch (err) { - console.log('[ERROR] Error changing current branch to ' + currentBranch) - console.log(err.stack) - error = err - } - } + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - break - } - - case 'Branch': { - try { - branch() - - async function branch() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - let result = await doGit() - - if (result.error === undefined) { - let customResponse = { - result: global.CUSTOM_OK_RESPONSE.result, - message: result - } - respondWithContent(JSON.stringify(customResponse), httpResponse) - } else { - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Could Not Get Current Branch', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, error) + let message + try { + message = await git.pull('https://github.com/Superalgos/Superalgos', currentBranch) + return { message: message } + } catch (err) { + console.log('[ERROR] Error updating ' + currentBranch) + console.log(err.stack) + return { error: err } } } - } - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack } - const git = simpleGit(options) - try { - return await git.branch() - } catch (err) { - console.log('[ERROR] Error reading current branch.') - console.log(err.stack) + respondWithContent(JSON.stringify(error), httpResponse) + } + break + } + + case 'Checkout': { + try { + const currentBranch = unescape(requestParameters[3]) + let error + + checkout() + + async function checkout() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') + } else { + await doGit() + + if (error === undefined) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'Switching Branches - Current Branch Not Changed', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + } + } } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + try { + await git.checkout(currentBranch) + } catch (err) { + console.log('[ERROR] Error changing current branch to ' + currentBranch) + console.log(err.stack) + error = err + } + } + + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) } + break + } - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + case 'Branch': { + try { + branch() - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack + async function branch() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') + } else { + let result = await doGit() + + if (result.error === undefined) { + let customResponse = { + result: global.CUSTOM_OK_RESPONSE.result, + message: result + } + respondWithContent(JSON.stringify(customResponse), httpResponse) + } else { + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Could Not Get Current Branch', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + } + } + } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + try { + return await git.branch() + } catch (err) { + console.log('[ERROR] Error reading current branch.') + console.log(err.stack) + } + } + + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) } - respondWithContent(JSON.stringify(error), httpResponse) + break } - break } - } - function respondWithDocsObject(docs, error) { + function respondWithDocsObject(docs, error) { - if (error.message !== undefined) { - docs.placeholder.errorMessage = { - style: 'Error', - text: error.message + if (error.message !== undefined) { + docs.placeholder.errorMessage = { + style: 'Error', + text: error.message + } } - } - if (error.stack !== undefined) { - docs.placeholder.errorStack = { - style: 'Javascript', - text: error.stack + if (error.stack !== undefined) { + docs.placeholder.errorStack = { + style: 'Javascript', + text: error.stack + } } - } - if (error.code !== undefined) { - docs.placeholder.errorCode = { + if (error.code !== undefined) { + docs.placeholder.errorCode = { + style: 'Json', + text: error.code + } + } + + docs.placeholder.errorDetails = { style: 'Json', - text: error.code + text: JSON.stringify(error, undefined, 4) } - } - docs.placeholder.errorDetails = { - style: 'Json', - text: JSON.stringify(error, undefined, 4) - } - - let customResponse = { - result: global.CUSTOM_FAIL_RESPONSE.result, - docs: docs - } - - respondWithContent(JSON.stringify(customResponse), httpResponse) - - } - } - break - - case 'GOV': { - /* - This is the Governance endpoint at the Http Interface. All methods - related to the Governance System are implemented here and routed - to the backend Servers that can process them. - */ - getBody(processRequest) - - async function processRequest(body) { - try { - let params = JSON.parse(body) - - switch (params.method) { - case 'getGithubStars': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubStars( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'getGithubWatchers': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubWatchers( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'getGithubForks': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubForks( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'mergePullRequests': { - - let serverResponse = await CL.servers.GITHUB_SERVER.mergePullRequests( - params.commitMessage, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'payContributors': { - - let serverResponse = await CL.servers.WEB3_SERVER.payContributors( - params.contractAddress, - params.contractAbi, - params.paymentsArray, - params.mnemonic - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - default: { - respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) - } + let customResponse = { + result: global.CUSTOM_FAIL_RESPONSE.result, + docs: docs } - } catch (err) { - console.log('[ERROR] httpInterface -> GOV -> Method call produced an error.') - console.log('[ERROR] httpInterface -> GOV -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> GOV -> Params Received = ' + body) - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) + respondWithContent(JSON.stringify(customResponse), httpResponse) + } } break - } + case 'GOV': + { + /* + This is the Governance endpoint at the Http Interface. All methods + related to the Governance System are implemented here and routed + to the backend Servers that can process them. + */ + getBody(processRequest) + async function processRequest(body) { + try { + let params = JSON.parse(body) + + switch (params.method) { + case 'getGithubStars': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubStars( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'getGithubWatchers': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubWatchers( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'getGithubForks': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubForks( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'mergePullRequests': { + + let serverResponse = await CL.servers.GITHUB_SERVER.mergePullRequests( + params.commitMessage, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'payContributors': { + + let serverResponse = await CL.servers.WEB3_SERVER.payContributors( + params.contractAddress, + params.contractAbi, + params.paymentsArray, + params.mnemonic + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + default: { + respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) + } + } + } catch (err) { + console.log('[ERROR] httpInterface -> GOV -> Method call produced an error.') + console.log('[ERROR] httpInterface -> GOV -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> GOV -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + } + break case 'LegacyPlotter.js': { respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse) } break - case 'PlotterPanel.js': { respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', httpResponse) } break - case 'Images': // This means the Images folder. { let path = global.env.PATH_TO_CLIENT + 'WebServer/Images/' + requestParameters[2] @@ -1162,7 +1139,6 @@ exports.newHttpInterface = function newHttpInterface() { respondWithImage(path, httpResponse) } break - case 'Icons': // This means the Icons folder under Projects. { let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Icons' @@ -1184,7 +1160,6 @@ exports.newHttpInterface = function newHttpInterface() { respondWithImage(path, httpResponse) } break - case 'GIFs': // This means the GIFs folder under Projects. { let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/GIFs' @@ -1205,7 +1180,6 @@ exports.newHttpInterface = function newHttpInterface() { respondWithImage(path, httpResponse) } break - case 'PNGs': // This means the PNGs folder under Projects. { let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/PNGs' @@ -1226,25 +1200,21 @@ exports.newHttpInterface = function newHttpInterface() { respondWithImage(path, httpResponse) } break - case 'favicon.ico': // This means the Scripts folder. { respondWithImage(global.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', httpResponse) } break - case 'WebServer': // This means the WebServer folder. { respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestParameters[2], httpResponse) } break - case 'externalScripts': // This means the WebServer folder. { respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestParameters[2], httpResponse) } break - case 'Plotters': // This means the plotter folder, not to be confused with the Plotters script! { let project = requestParameters[2] @@ -1255,31 +1225,26 @@ exports.newHttpInterface = function newHttpInterface() { respondWithFile(filePath, httpResponse) } break - case 'ChartLayers': { respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1] + '/' + requestParameters[2], httpResponse) } break - case 'Files': { respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestParameters[2], httpResponse) } break - case 'Fonts': { respondWithFont(global.env.PATH_TO_FONTS + '/' + requestParameters[2], httpResponse) } break - case 'Schema': { sendSchema(global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Schemas/', requestParameters[3]) } break - case 'DirContent': { let folderPath = unescape(requestParameters[2]) @@ -1308,7 +1273,6 @@ exports.newHttpInterface = function newHttpInterface() { } } break - case 'IconNames': { let projects = getDirectories(global.env.PATH_TO_PROJECTS) @@ -1340,7 +1304,6 @@ exports.newHttpInterface = function newHttpInterface() { } } break - case 'PluginFileNames': { processRequest() @@ -1370,11 +1333,9 @@ exports.newHttpInterface = function newHttpInterface() { respondWithContent(JSON.stringify(error), httpResponse) } } - break } - + break case 'LoadPlugin': - { processRequest() @@ -1412,38 +1373,38 @@ exports.newHttpInterface = function newHttpInterface() { respondWithContent(JSON.stringify(error), httpResponse) } } - break } - + break case 'SavePlugin': - getBody(processRequest) + { + getBody(processRequest) - async function processRequest(body) { - try { - let plugin = JSON.parse(body) - let project = requestParameters[2] - let folder = requestParameters[3] - let fileName = requestParameters[4] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/' + folder - let fileContent = JSON.stringify(plugin, undefined, 4) - const fs = require('fs') - fs.writeFileSync(filePath + '/' + fileName + '.json', fileContent) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } catch (err) { - console.log('[ERROR] httpInterface -> SavePlugin -> Method call produced an error.') - console.log('[ERROR] httpInterface -> SavePlugin -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> SavePlugin -> Params Received = ' + body) + async function processRequest(body) { + try { + let plugin = JSON.parse(body) + let project = requestParameters[2] + let folder = requestParameters[3] + let fileName = requestParameters[4] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/' + folder + let fileContent = JSON.stringify(plugin, undefined, 4) + const fs = require('fs') + fs.writeFileSync(filePath + '/' + fileName + '.json', fileContent) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } catch (err) { + console.log('[ERROR] httpInterface -> SavePlugin -> Method call produced an error.') + console.log('[ERROR] httpInterface -> SavePlugin -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> SavePlugin -> Params Received = ' + body) - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) } - respondWithContent(JSON.stringify(error), httpResponse) } } break - case 'Workspace.js': { let fs = require('fs') @@ -1465,7 +1426,6 @@ exports.newHttpInterface = function newHttpInterface() { } } break - case 'ListWorkspaces': { let allWorkspaces = [] @@ -1542,7 +1502,6 @@ exports.newHttpInterface = function newHttpInterface() { } } break - case 'LoadMyWorkspace': { let fileName = unescape(requestParameters[2]) @@ -1550,7 +1509,6 @@ exports.newHttpInterface = function newHttpInterface() { respondWithFile(filePath, httpResponse) } break - case 'SaveWorkspace': { getBody(processRequest) @@ -1600,20 +1558,17 @@ exports.newHttpInterface = function newHttpInterface() { } } break - case 'ListFunctionLibraries': { returnProjectFolderFileList('Function-Libraries') } break - case 'ProjectsSchema': { let path = global.env.PATH_TO_PROJECTS + '/' + 'ProjectsSchema.json' respondWithFile(path, httpResponse) } break - case 'ListSpaceFiles': { let fs = require('fs') @@ -1672,19 +1627,16 @@ exports.newHttpInterface = function newHttpInterface() { } } break - case 'ListUtilitiesFiles': { returnProjectFolderFileList('Utilities') } break - case 'ListGlobalFiles': { returnProjectFolderFileList('Globals') } break - case 'Projects': { let path = '' @@ -1699,7 +1651,6 @@ exports.newHttpInterface = function newHttpInterface() { respondWithFile(filePath, httpResponse) } break - case 'Storage': { let pathToFile = httpRequest.url.substring(9) @@ -1710,61 +1661,51 @@ exports.newHttpInterface = function newHttpInterface() { respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) } break - case 'main.css': { sendStyleSheet('main.css') } break - case 'tutorial.css': { sendStyleSheet('tutorial.css') } break - case 'docs.css': { sendStyleSheet('docs.css') } break - case 'governance.css': { sendStyleSheet('governance.css') } break - case 'context-menu.css': { sendStyleSheet('context-menu.css') } break - case 'credits.css': { sendStyleSheet('credits.css') } break - case 'docs.css': { sendStyleSheet('docs.css') } break - case 'font-awasome.css': { sendStyleSheet('font-awasome.css') } break - case 'prism.css': { sendStyleSheet('prism.css') } break - case 'ExecuteTerminalCommand': { let command = unescape(requestParameters[2]) @@ -1918,9 +1859,9 @@ exports.newHttpInterface = function newHttpInterface() { try { let fileContent = file.toString() - fileContent = fileContent.replace('HTTP_INTERFACE_PORT', global.env.HTTP_INTERFACE_PORT) - fileContent = fileContent.replace('HTTP_INTERFACE_PORT', global.env.HTTP_INTERFACE_PORT) - fileContent = fileContent.replace('HTTP_INTERFACE_PORT', global.env.HTTP_INTERFACE_PORT) + fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) + fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) + fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) respondWithContent(fileContent, httpResponse, 'text/css') } catch (err) { console.log('[ERROR] httpInterface -> mainCSS -> File Not Found: ' + fileName + ' or Error = ' + err.stack) @@ -1942,7 +1883,7 @@ exports.newHttpInterface = function newHttpInterface() { try { let fileContent = file.toString() - fileContent = fileContent.replace('HTTP_INTERFACE_PORT', global.env.HTTP_INTERFACE_PORT) + fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) respondWithContent(fileContent, httpResponse) } catch (err) { console.log('[ERROR] httpInterface -> homePage -> File Not Found: ' + fileName + ' or Error = ' + err.stack) diff --git a/Desktop/httpInterface.js b/Desktop/httpInterface.js new file mode 100644 index 000000000..20a557a51 --- /dev/null +++ b/Desktop/httpInterface.js @@ -0,0 +1,2121 @@ +exports.newHttpInterface = function newHttpInterface() { + + /* + This module represent the HTTP API of the + Desktop App. All HTTP request are processed + by this module. + */ + let thisObject = { + initialize: initialize, + finalize: finalize, + run: run + } + + const open = require('open') + + let port = global.env.DESKTOP_HTTP_INTERFACE_PORT + + let http = require('http') + let isHttpServerStarted = false + + let webhook = new Map() + + return thisObject + + function finalize() { + + } + + function initialize() { + + } + + function run() { + startHtttpServer() + } + + function startHtttpServer() { + + try { + if (isHttpServerStarted === false) { + gWebServer = http.createServer(onBrowserRequest).listen(port) + isHttpServerStarted = true + /* Starting the browser now is optional */ + if (process.argv.includes("noBrowser")) { + //Running Client only with no UI. + } else { + open('http://localhost:' + port) + } + + } + } catch (err) { + console.log('[ERROR] httpInterface -> startHtttpServer -> Error = ' + err.stack) + } + } + + function onBrowserRequest(httpRequest, httpResponse) { + + function getBody(callback) { // Gets the de body from a POST httpRequest to the web server + try { + + let body = '' + + httpRequest.on('data', function (data) { + body += data + // Too much POST data + //if (body.length > 1e6) { + // httpRequest.connection.destroy() + //} + }) + + httpRequest.on('end', function () { + callback(body) + }) + + httpRequest.on('error', function (err) { + console.log('[INFO] httpInterface -> onBrowserRequest -> getBody -> err = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + }) + } catch (err) { + console.log('[INFO] httpInterface -> onBrowserRequest -> getBody -> err = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + } + + let requestParameters = httpRequest.url.split('/') + + requestParameters = httpRequest.url.split('?') // Remove version information + requestParameters = requestParameters[0].split('/') + + switch (requestParameters[1]) { + + case 'WEB3': + { + getBody(processRequest) + + async function processRequest(body) { + try { + let params = JSON.parse(body) + + switch (params.method) { + case 'getNetworkClientStatus': { + + let serverResponse = await CL.servers.WEB3_SERVER.getNetworkClientStatus( + params.host, + params.port, + params.interface + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'createWalletAccount': { + + let serverResponse = await CL.servers.WEB3_SERVER.createWalletAccount( + params.entropy + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'getWalletBalances': { + + let serverResponse = await CL.servers.WEB3_SERVER.getWalletBalances( + params.host, + params.port, + params.interface, + params.walletDefinition + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'signData': { + + let serverResponse = await CL.servers.WEB3_SERVER.signData( + params.privateKey, + params.data + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'recoverAddress': { + + let serverResponse = await CL.servers.WEB3_SERVER.recoverAddress( + params.signature + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'mnemonicToPrivateKey': { + + let serverResponse = await CL.servers.WEB3_SERVER.mnemonicToPrivateKey( + params.mnemonic + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + default: { + respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) + } + } + } catch (err) { + console.log('[ERROR] httpInterface -> WEB3s -> Method call produced an error.') + console.log('[ERROR] httpInterface -> WEB3s -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> WEB3s -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'CCXT': + { + getBody(processRequest) + + async function processRequest(body) { + try { + let params = JSON.parse(body) + + const ccxt = require('ccxt') + + switch (params.method) { + case 'fetchMarkets': { + + const exchangeClass = ccxt[params.exchangeId] + const exchangeConstructorParams = { + 'timeout': 30000, + 'enableRateLimit': true, + verbose: false + } + + let ccxtExchange = new exchangeClass(exchangeConstructorParams) + let ccxtMarkets = [] + + if (ccxtExchange.has.fetchMarkets === true) { + ccxtMarkets = await ccxtExchange.fetchMarkets() + } + respondWithContent(JSON.stringify(ccxtMarkets), httpResponse) + return + } + case 'listExchanges': { + let exchanges = [] + for (let i = 0; i < ccxt.exchanges.length; i++) { + let exchangeId = ccxt.exchanges[i] + + const exchangeClass = ccxt[exchangeId] + const exchangeConstructorParams = { + 'timeout': 30000, + 'enableRateLimit': true, + verbose: false + } + let ccxtExchange + try { ccxtExchange = new exchangeClass(exchangeConstructorParams) } + catch (err) { } + if (ccxtExchange === undefined) { continue } + + + if (ccxtExchange.has.fetchOHLCV === params.has.fetchOHLCV) { + if (ccxtExchange.has.fetchMarkets === params.has.fetchMarkets) { + if (ccxtExchange.timeframes['1m'] !== undefined) { + let exchange = { + name: ccxtExchange.name, + id: ccxtExchange.id + } + exchanges.push(exchange) + } + } + } + } + respondWithContent(JSON.stringify(exchanges), httpResponse) + return + } + } + + let content = { + err: global.DEFAULT_FAIL_RESPONSE // method not supported + } + } catch (err) { + console.log('[INFO] httpInterface -> CCXT FetchMarkets -> Could not fetch markets.') + let error = { + result: 'Fail Because', + message: err.message + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + break + + case 'Webhook': + { + switch (requestParameters[2]) { // switch by command + case 'Fetch-Messages': { + let exchange = requestParameters[3] + let market = requestParameters[4] + + /* Some validations */ + if (exchange === undefined) { + console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no Exchange received -> messageReceived = ' + messageReceived) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + if (market === undefined) { + console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no market received -> messageReceived = ' + messageReceived) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + + let key = exchange + '-' + market + + let webhookMessages = webhook.get(key) + if (webhookMessages === undefined) { + webhookMessages = [] + } + + console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Exchange-Market = ' + exchange + '-' + market) + console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Messeges Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) + + respondWithContent(JSON.stringify(webhookMessages), httpResponse) + webhookMessages = [] + + webhook.set(key, webhookMessages) + break + } + case 'New-Message': { + getBody(processRequest) + + function processRequest(messageReceived) { + let timestamp = (new Date()).valueOf() + let source = requestParameters[3] + let exchange = requestParameters[4] + let market = requestParameters[5] + + /* Some validations */ + if (source === undefined) { + console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Source received -> messageReceived = ' + messageReceived) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + if (exchange === undefined) { + console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Exchange received -> messageReceived = ' + messageReceived) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + if (market === undefined) { + console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no market received -> messageReceived = ' + messageReceived) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + + let key = exchange + '-' + market + + let webhookMessages = webhook.get(key) + if (webhookMessages === undefined) { + webhookMessages = [] + } + + webhookMessages.push([timestamp, source, messageReceived]) + webhook.set(key, webhookMessages) + + console.log('[INFO] httpInterface -> Webhook -> New-Message -> Exchange-Market = ' + exchange + '-' + market) + console.log('[INFO] httpInterface -> Webhook -> New-Message -> messageReceived = ' + messageReceived) + console.log('[INFO] httpInterface -> Webhook -> New-Message -> Messeges waiting to be Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + break + } + } + break + } + break + + case 'Docs': + { + switch (requestParameters[2]) { // switch by command + case 'Save-Node-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Nodes' + + if (checkAllSchmemaDocuments('Node', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Concept-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Concepts' + + if (checkAllSchmemaDocuments('Concept', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Topic-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Topics' + + if (checkAllSchmemaDocuments('Topic', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Tutorial-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Tutorials' + + if (checkAllSchmemaDocuments('Tutorial', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Review-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Reviews' + + if (checkAllSchmemaDocuments('Review', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Book-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Books' + + if (checkAllSchmemaDocuments('Book', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + } + + function checkAllSchmemaDocuments(category, docsSchema, filePath) { + const fs = require('fs') + let noErrorsDuringSaving = true + + for (let i = 0; i < docsSchema.length; i++) { + let schemaDocument = docsSchema[i] + /* + For some type of schemas we will save the file at an extra + folder derived from the document's type. + */ + let fileName = schemaDocument.type.toLowerCase() + for (let j = 0; j < 10; j++) { + fileName = cleanFileName(fileName) + } + let pageNumber = '00' + schemaDocument.pageNumber + let newFilepath = filePath + switch (category) { + case 'Topic': { + fileName = schemaDocument.topic.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() + fileName = cleanFileName(fileName) + newFilepath = createPrefixDirectories(filePath, schemaDocument.topic) + break + } + case 'Tutorial': { + fileName = schemaDocument.tutorial.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() + fileName = cleanFileName(fileName) + newFilepath = createPrefixDirectories(filePath, schemaDocument.tutorial) + break + } + case 'Review': { + fileName = schemaDocument.review.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() + fileName = cleanFileName(fileName) + newFilepath = createPrefixDirectories(filePath, schemaDocument.review) + break + } + case 'Node': { + newFilepath = createPrefixDirectories(filePath, schemaDocument.type) + break + } + case 'Concept': { + newFilepath = createPrefixDirectories(filePath, schemaDocument.type) + break + } + } + + function createPrefixDirectories(filePath, schemaTextToUse) { + let firstLetter = schemaTextToUse.substring(0, 1) + createNewDir(filePath + '/' + firstLetter) + let extraWord = schemaTextToUse.split(' ')[0] + createNewDir(filePath + '/' + firstLetter + '/' + extraWord) + return filePath + '/' + firstLetter + '/' + extraWord + '/' + cleanFileName(schemaTextToUse) + } + + fileName = fileName + '.json' + + if (schemaDocument.deleted === true) { + try { + fs.unlinkSync(newFilepath + '/' + fileName) + console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' deleted.') + } catch (err) { + noErrorsDuringSaving = false + console.log('[ERROR] httpInterface -> Docs -> Delete -> ' + newFilepath + '/' + fileName + ' could not be deleted.') + console.log('[ERROR] httpInterface -> Docs -> Delete -> Resolve the issue that is preventing the Client to delete this file. Look at the error message below as a guide. At the UI you will need to delete this page again in order for the Client to retry next time you execute the docs.save command.') + console.log('[ERROR] httpInterface -> Docs -> Delete -> err.stack = ' + err.stack) + } + } else { + if (schemaDocument.updated === true || schemaDocument.created === true) { + try { + let created = schemaDocument.created + let updated = schemaDocument.updated + schemaDocument.updated = undefined + schemaDocument.created = undefined + let fileContent = JSON.stringify(schemaDocument, undefined, 4) + createNewDir(newFilepath) + fs.writeFileSync(newFilepath + '/' + fileName, fileContent) + if (created === true) { + console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' created.') + } else { + if (updated === true) { + console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' updated.') + } + } + } catch (err) { + noErrorsDuringSaving = false + console.log('[ERROR] httpInterface -> Docs -> Save -> ' + newFilepath + '/' + fileName + ' could not be created / updated.') + console.log('[ERROR] httpInterface -> Docs -> Save -> err.stack = ' + err.stack) + } + } + } + + function createNewDir(path) { + try { + fs.mkdirSync(path) + } catch (err) { + if (err.message.indexOf('file already exists') < 0) { + throw (err) + } + } + } + } + + return noErrorsDuringSaving + } + + function cleanFileName(fileName) { + for (let i = 0; i < 100; i++) { + fileName = fileName + .replace(' ', '-') + .replace('--', '-') + .replace('?', '') + .replace('#', '') + .replace('$', '') + .replace('%', '') + .replace('^', '') + .replace('&', '') + .replace('*', '') + .replace('(', '') + .replace(')', '') + .replace('!', '') + .replace('..', '.') + .replace(',', '') + .replace('\'', '') + } + return fileName + } + } + break + + case 'App': { + switch (requestParameters[2]) { // switch by command + case 'Contribute': { + try { + let commitMessage = unescape(requestParameters[3]) + const username = unescape(requestParameters[4]) + const token = unescape(requestParameters[5]) + const currentBranch = unescape(requestParameters[6]) + const contributionsBranch = unescape(requestParameters[7]) + let error + + /* Unsavping # */ + for (let i = 0; i < 10; i++) { + commitMessage = commitMessage.replace('_SLASH_', '/') + commitMessage = commitMessage.replace('_HASHTAG_', '#') + } + + contribute() + + async function contribute() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') + } else { + await doGit() + if (error !== undefined) { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Contribution Not Sent', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + return + } + + await doGithub() + if (error !== undefined) { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Contribution Not Sent', + anchor: undefined, + placeholder: {} + } + console.log('respond with docs ') + + respondWithDocsObject(docs, error) + return + } + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + + try { + await git.add('./*') + await git.commit(commitMessage) + await git.push('origin', currentBranch) + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> contributionsBranch = ' + contributionsBranch) + console.log('') + console.log('Torubleshooting Tips:') + console.log('') + console.log('1. Make sure that you have set up your Github Username and Token at the APIs -> Github API node at the workspace.') + console.log('2. Make sure you are running the latest version of Git available for your OS.') + console.log('3. Make sure that you have cloned your Superalgos repository fork, and not the main Superalgos repository.') + console.log('4. If your fork is old, you might need to do an app.update and also a node setup at every branch. If you just reforked all is good.') + error = err + } + } + + async function doGithub() { + + const { Octokit } = require("@octokit/rest") + + const octokit = new Octokit({ + auth: token, + userAgent: 'Superalgos Beta 11' + }) + + const repo = 'Superalgos' + const owner = 'Superalgos' + const head = username + ':' + contributionsBranch + const base = currentBranch + const title = 'Contribution: ' + commitMessage + + try { + await octokit.pulls.create({ + owner, + repo, + title, + head, + base, + }); + } catch (err) { + if (err.stack.indexOf('A pull request already exists') >= 0) { + return + } else { + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10)) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch) + error = err + } + } + } + + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Contribute -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> username = ' + username) + console.log('[ERROR] httpInterface -> App -> Contribute -> token starts with = ' + token.substring(0, 10) + '...') + console.log('[ERROR] httpInterface -> App -> Contribute -> token ends with = ' + '...' + token.substring(token.length - 10)) + console.log('[ERROR] httpInterface -> App -> Contribute -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> contributionsBranch = ' + contributionsBranch) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + break + } + + case 'Update': { + try { + const currentBranch = unescape(requestParameters[3]) + update() + + async function update() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') + } else { + let result = await doGit() + + if (result.error === undefined) { + let customResponse = { + result: global.CUSTOM_OK_RESPONSE.result, + message: result.message + } + respondWithContent(JSON.stringify(customResponse), httpResponse) + } else { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Update Failed', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, result.error) + + } + } + } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + + let message + try { + message = await git.pull('https://github.com/Superalgos/Superalgos', currentBranch) + return { message: message } + } catch (err) { + console.log('[ERROR] Error updating ' + currentBranch) + console.log(err.stack) + return { error: err } + } + } + + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + break + } + + case 'Checkout': { + try { + const currentBranch = unescape(requestParameters[3]) + let error + + checkout() + + async function checkout() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') + } else { + await doGit() + + if (error === undefined) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'Switching Branches - Current Branch Not Changed', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + } + } + } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + try { + await git.checkout(currentBranch) + } catch (err) { + console.log('[ERROR] Error changing current branch to ' + currentBranch) + console.log(err.stack) + error = err + } + } + + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + break + } + + case 'Branch': { + try { + branch() + + async function branch() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') + } else { + let result = await doGit() + + if (result.error === undefined) { + let customResponse = { + result: global.CUSTOM_OK_RESPONSE.result, + message: result + } + respondWithContent(JSON.stringify(customResponse), httpResponse) + } else { + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Could Not Get Current Branch', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + } + } + } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + try { + return await git.branch() + } catch (err) { + console.log('[ERROR] Error reading current branch.') + console.log(err.stack) + } + } + + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + break + } + } + + function respondWithDocsObject(docs, error) { + + if (error.message !== undefined) { + docs.placeholder.errorMessage = { + style: 'Error', + text: error.message + } + } + if (error.stack !== undefined) { + docs.placeholder.errorStack = { + style: 'Javascript', + text: error.stack + } + } + if (error.code !== undefined) { + docs.placeholder.errorCode = { + style: 'Json', + text: error.code + } + } + + docs.placeholder.errorDetails = { + style: 'Json', + text: JSON.stringify(error, undefined, 4) + } + + let customResponse = { + result: global.CUSTOM_FAIL_RESPONSE.result, + docs: docs + } + + respondWithContent(JSON.stringify(customResponse), httpResponse) + + } + } + break + + case 'GOV': { + /* + This is the Governance endpoint at the Http Interface. All methods + related to the Governance System are implemented here and routed + to the backend Servers that can process them. + */ + getBody(processRequest) + + async function processRequest(body) { + try { + let params = JSON.parse(body) + + switch (params.method) { + case 'getGithubStars': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubStars( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'getGithubWatchers': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubWatchers( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'getGithubForks': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubForks( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'mergePullRequests': { + + let serverResponse = await CL.servers.GITHUB_SERVER.mergePullRequests( + params.commitMessage, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'payContributors': { + + let serverResponse = await CL.servers.WEB3_SERVER.payContributors( + params.contractAddress, + params.contractAbi, + params.paymentsArray, + params.mnemonic + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + default: { + respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) + } + } + } catch (err) { + console.log('[ERROR] httpInterface -> GOV -> Method call produced an error.') + console.log('[ERROR] httpInterface -> GOV -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> GOV -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'LegacyPlotter.js': + { + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse) + } + break + + case 'PlotterPanel.js': + { + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', httpResponse) + } + break + + case 'Images': // This means the Images folder. + { + let path = global.env.PATH_TO_CLIENT + 'WebServer/Images/' + requestParameters[2] + + if (requestParameters[3] !== undefined) { + path = path + '/' + requestParameters[3] + } + + if (requestParameters[4] !== undefined) { + path = path + '/' + requestParameters[4] + } + + if (requestParameters[5] !== undefined) { + path = path + '/' + requestParameters[5] + } + + path = unescape(path) + + respondWithImage(path, httpResponse) + } + break + + case 'Icons': // This means the Icons folder under Projects. + { + let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Icons' + + if (requestParameters[3] !== undefined) { + path = path + '/' + requestParameters[3] + } + + if (requestParameters[4] !== undefined) { + path = path + '/' + requestParameters[4] + } + + if (requestParameters[5] !== undefined) { + path = path + '/' + requestParameters[5] + } + + path = unescape(path) + + respondWithImage(path, httpResponse) + } + break + + case 'GIFs': // This means the GIFs folder under Projects. + { + let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/GIFs' + + if (requestParameters[3] !== undefined) { + path = path + '/' + requestParameters[3] + } + + if (requestParameters[4] !== undefined) { + path = path + '/' + requestParameters[4] + } + + if (requestParameters[5] !== undefined) { + path = path + '/' + requestParameters[5] + } + + path = unescape(path) + respondWithImage(path, httpResponse) + } + break + + case 'PNGs': // This means the PNGs folder under Projects. + { + let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/PNGs' + + if (requestParameters[3] !== undefined) { + path = path + '/' + requestParameters[3] + } + + if (requestParameters[4] !== undefined) { + path = path + '/' + requestParameters[4] + } + + if (requestParameters[5] !== undefined) { + path = path + '/' + requestParameters[5] + } + + path = unescape(path) + respondWithImage(path, httpResponse) + } + break + + case 'favicon.ico': // This means the Scripts folder. + { + respondWithImage(global.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', httpResponse) + } + break + + case 'WebServer': // This means the WebServer folder. + { + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestParameters[2], httpResponse) + } + break + + case 'externalScripts': // This means the WebServer folder. + { + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestParameters[2], httpResponse) + } + break + + case 'Plotters': // This means the plotter folder, not to be confused with the Plotters script! + { + let project = requestParameters[2] + let dataMine = requestParameters[3] + let codeName = requestParameters[4] + let moduleName = requestParameters[5] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'Bots-Plotters-Code' + '/' + dataMine + '/plotters/' + codeName + '/' + moduleName + respondWithFile(filePath, httpResponse) + } + break + + case 'ChartLayers': + { + respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1] + '/' + requestParameters[2], httpResponse) + } + break + + case 'Files': + { + respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestParameters[2], httpResponse) + } + break + + case 'Fonts': + { + respondWithFont(global.env.PATH_TO_FONTS + '/' + requestParameters[2], httpResponse) + } + break + + case 'Schema': + { + sendSchema(global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Schemas/', requestParameters[3]) + } + break + + case 'DirContent': + { + let folderPath = unescape(requestParameters[2]) + if (requestParameters[3] !== undefined) { + folderPath = folderPath + '/' + requestParameters[3] + } + + if (requestParameters[4] !== undefined) { + folderPath = folderPath + '/' + requestParameters[4] + } + + if (requestParameters[5] !== undefined) { + folderPath = folderPath + '/' + requestParameters[5] + } + let folder + if (requestParameters[2] === 'Root') { + folder = folderPath.replace('Root', '../Superalgos/') + } else { + folder = global.env.PATH_TO_PROJECTS + '/' + folderPath + } + + getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) + + function onFilesReady(files) { + respondWithContent(JSON.stringify(files), httpResponse) + } + } + break + + case 'IconNames': + { + let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let icons = [] + let totalProjects = projects.length + let projectCounter = 0 + + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + + const folder = global.env.PATH_TO_PROJECTS + '/' + project + '/Icons/' + + getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) + + function onFilesReady(files) { + for (let j = 0; j < files.length; j++) { + let file = files[j] + for (let i = 0; i < 10; i++) { + file = file.replace('/', '\\') + } + icons.push([project, file]) + } + + projectCounter++ + if (projectCounter === totalProjects) { + respondWithContent(JSON.stringify(icons), httpResponse) + } + } + } + } + break + + case 'PluginFileNames': + { + processRequest() + + async function processRequest(body) { + try { + let project = unescape(requestParameters[2]) + let folder = unescape(requestParameters[3]) + + let response = await SA.projects.foundations.utilities.plugins.getPluginFileNames( + project, + folder + ) + + respondWithContent(JSON.stringify(response), httpResponse) + + } catch (err) { + console.log('[ERROR] httpInterface -> PluginFileNames -> Method call produced an error.') + console.log('[ERROR] httpInterface -> PluginFileNames -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> PluginFileNames -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'LoadPlugin': + + { + processRequest() + + async function processRequest(body) { + try { + let project = unescape(requestParameters[2]) + let folder = unescape(requestParameters[3]) + let fileName = unescape(requestParameters[4]) + + let response = await SA.projects.foundations.utilities.plugins.getPluginFileContent( + project, + folder, + fileName + ).catch(err => { + let error = { + result: 'Fail Because', + message: err + } + respondWithContent(JSON.stringify(error), httpResponse) + return + }) + + respondWithContent(response, httpResponse) + + } catch (err) { + console.log('[ERROR] httpInterface -> LoadPlugin -> Method call produced an error.') + console.log('[ERROR] httpInterface -> LoadPlugin -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> LoadPlugin -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'SavePlugin': + getBody(processRequest) + + async function processRequest(body) { + try { + let plugin = JSON.parse(body) + let project = requestParameters[2] + let folder = requestParameters[3] + let fileName = requestParameters[4] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/' + folder + let fileContent = JSON.stringify(plugin, undefined, 4) + const fs = require('fs') + fs.writeFileSync(filePath + '/' + fileName + '.json', fileContent) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } catch (err) { + console.log('[ERROR] httpInterface -> SavePlugin -> Method call produced an error.') + console.log('[ERROR] httpInterface -> SavePlugin -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> SavePlugin -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + + case 'Workspace.js': + { + let fs = require('fs') + + try { + let filePath = global.env.PATH_TO_DEFAULT_WORKSPACE + '/Getting-Started-Tutorials.json' + fs.readFile(filePath, onFileRead) + } catch (e) { + console.log('[ERROR] Error reading the Workspace.', e) + } + + function onFileRead(err, workspace) { + if (err) { + respondWithContent(undefined, httpResponse) + } else { + let responseContent = 'function getWorkspace(){ return ' + workspace + '}' + respondWithContent(responseContent, httpResponse) + } + } + } + break + + case 'ListWorkspaces': + { + let allWorkspaces = [] + let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let projectsCount = 0 + + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + readPluginWorkspaces() + function readPluginWorkspaces() { + let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/Workspaces' + try { + let fs = require('fs') + fs.readdir(dirPath, onDirRead) + + function onDirRead(err, fileList) { + let updatedFileList = [] + + if (err) { + /* + If we have a problem reading this folder we will assume that it is + because this project does not need this folder and that's it. + */ + //console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) + } else { + for (let i = 0; i < fileList.length; i++) { + let name = 'Plugin \u2192 ' + fileList[i] + updatedFileList.push([project, name]) + } + } + allWorkspaces = allWorkspaces.concat(updatedFileList) + projectsCount++ + if (projectsCount === projects.length) { + readMyWorkspaces() + } + } + } catch (err) { + console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) + console.log('[ERROR] err.stack = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + } + } + + function readMyWorkspaces() { + let dirPath = global.env.PATH_TO_MY_WORKSPACES + try { + let fs = require('fs') + fs.readdir(dirPath, onDirRead) + + function onDirRead(err, fileList) { + if (err) { + // This happens the first time you run the software. + respondWithContent(JSON.stringify(allWorkspaces), httpResponse) + return + } else { + let updatedFileList = [] + for (let i = 0; i < fileList.length; i++) { + let name = fileList[i] + updatedFileList.push(['', name]) + } + allWorkspaces = allWorkspaces.concat(updatedFileList) + respondWithContent(JSON.stringify(allWorkspaces), httpResponse) + return + } + } + } catch (err) { + console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) + console.log('[ERROR] err.stack = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + } + } + break + + case 'LoadMyWorkspace': + { + let fileName = unescape(requestParameters[2]) + let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' + respondWithFile(filePath, httpResponse) + } + break + + case 'SaveWorkspace': + { + getBody(processRequest) + + async function processRequest(body) { + + let fileContent = body + let fileName = unescape(requestParameters[2]) + let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' + + try { + let fs = require('fs') + let dir = global.env.PATH_TO_MY_WORKSPACES; + + /* Create Dir if it does not exist */ + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } + + fs.writeFile(filePath, fileContent, onFileWritten) + + function onFileWritten(err) { + if (err) { + console.log('[ERROR] SaveWorkspace -> onFileWritten -> Error writing the Workspace file. fileName = ' + fileName) + console.log('[ERROR] SaveWorkspace -> onFileWritten -> err.stack = ' + err.stack) + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + } + + } catch (err) { + console.log('[ERROR] SaveWorkspace -> Error writing the Workspace file. fileName = ' + fileName) + console.log('[ERROR] SaveWorkspace -> err.stack = ' + err.stack) + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + } + break + + case 'ListFunctionLibraries': + { + returnProjectFolderFileList('Function-Libraries') + } + break + + case 'ProjectsSchema': + { + let path = global.env.PATH_TO_PROJECTS + '/' + 'ProjectsSchema.json' + respondWithFile(path, httpResponse) + } + break + + case 'ListSpaceFiles': + { + let fs = require('fs') + let allFiles = [] + let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let dirCount = 0 + let totalDirs = 0 + + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + + let dirPath = project + '/UI/Spaces' + let spaces = getDirectories(global.env.PATH_TO_PROJECTS + '/' + dirPath) + + for (let j = 0; j < spaces.length; j++) { + let space = spaces[j] + readDirectory(dirPath + '/' + space) + } + + function readDirectory(path) { + try { + + totalDirs++ + fs.readdir(global.env.PATH_TO_PROJECTS + '/' + path, onDirRead) + + let otherDirs = getDirectories(global.env.PATH_TO_PROJECTS + '/' + path) + for (let m = 0; m < otherDirs.length; m++) { + let otherDir = otherDirs[m] + readDirectory(path + '/' + otherDir) + } + + function onDirRead(err, fileList) { + if (err) { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } else { + let updatedFileList = [] + for (let k = 0; k < fileList.length; k++) { + let name = fileList[k] + if (name.indexOf('.js') < 0) { continue } + updatedFileList.push(path + '/' + name) + } + allFiles = allFiles.concat(updatedFileList) + dirCount++ + if (dirCount === totalDirs) { + respondWithContent(JSON.stringify(allFiles), httpResponse) + } + } + } + } catch (err) { + console.log('[ERROR] Error reading a directory content. filePath = ' + path) + console.log('[ERROR] err.stack = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + } + } + } + break + + case 'ListUtilitiesFiles': + { + returnProjectFolderFileList('Utilities') + } + break + + case 'ListGlobalFiles': + { + returnProjectFolderFileList('Globals') + } + break + + case 'Projects': + { + let path = '' + for (let i = 2; i < 10; i++) { + if (requestParameters[i] !== undefined) { + let parameter = unescape(requestParameters[i]) + path = path + '/' + parameter + } + + } + let filePath = global.env.PATH_TO_PROJECTS + path + respondWithFile(filePath, httpResponse) + } + break + + case 'Storage': + { + let pathToFile = httpRequest.url.substring(9) + /* Unsavping # */ + for (let i = 0; i < 10; i++) { + pathToFile = pathToFile.replace('_HASHTAG_', '#') + } + respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) + } + break + + case 'main.css': + { + sendStyleSheet('main.css') + } + break + + case 'tutorial.css': + { + sendStyleSheet('tutorial.css') + } + break + + case 'docs.css': + { + sendStyleSheet('docs.css') + } + break + + case 'governance.css': + { + sendStyleSheet('governance.css') + } + break + + case 'context-menu.css': + { + sendStyleSheet('context-menu.css') + } + break + + case 'credits.css': + { + sendStyleSheet('credits.css') + } + break + + case 'docs.css': + { + sendStyleSheet('docs.css') + } + break + + case 'font-awasome.css': + { + sendStyleSheet('font-awasome.css') + } + break + + case 'prism.css': + { + sendStyleSheet('prism.css') + } + break + + case 'ExecuteTerminalCommand': + { + let command = unescape(requestParameters[2]) + executeTerminalCommand(command) + } + break + default: + { + homePage() + } + } + + function returnProjectFolderFileList(projectFolderName) { + { + let allLibraries = [] + let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let projectsCount = 0 + + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + + let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'UI' + '/' + projectFolderName + try { + let fs = require('fs') + fs.readdir(dirPath, onDirRead) + + function onDirRead(err, fileList) { + let updatedFileList = [] + if (err) { + /* + If we have a problem reading this folder we will assume that it is + because this project does not need this folder and that's it. + */ + // console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) + } else { + for (let i = 0; i < fileList.length; i++) { + let name = fileList[i] + updatedFileList.push([project, name]) + } + } + allLibraries = allLibraries.concat(updatedFileList) + projectsCount++ + if (projectsCount === projects.length) { + respondWithContent(JSON.stringify(allLibraries), httpResponse) + } + } + } catch (err) { + console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) + console.log('[ERROR] err.stack = ' + err.stack) + + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + } + } + } + + function executeTerminalCommand(command) { + const util = require('util'); + const exec = util.promisify(require('child_process').exec); + async function lsWithGrep() { + try { + const { stdout, stderr } = await exec(command); + console.log('stdout:', stdout); + console.log('stderr:', stderr); + } catch (err) { + console.error(err.stack); + }; + }; + lsWithGrep(); + } + + function sendSchema(filePath, schemaType) { + let fs = require('fs') + try { + let folder = '' + switch (schemaType) { + case 'AppSchema': { + folder = 'App-Schema' + break + } + case 'DocsNodeSchema': { + folder = 'Docs-Nodes' + break + } + case 'DocsConceptSchema': { + folder = 'Docs-Concepts' + break + } + case 'DocsTopicSchema': { + folder = 'Docs-Topics' + break + } + case 'DocsTutorialSchema': { + folder = 'Docs-Tutorials' + break + } + case 'DocsReviewSchema': { + folder = 'Docs-Reviews' + break + } + case 'DocsBookSchema': { + folder = 'Docs-Books' + break + } + } + getAllFilesInDirectoryAndSubdirectories(filePath + folder, onFilesReady) + function onFilesReady(files) { + + let schemaArray = [] + for (let k = 0; k < files.length; k++) { + let name = files[k] + let nameSplitted = name.split(folder) + let fileName = nameSplitted[1] + for (let i = 0; i < 10; i++) { + fileName = fileName.replace('\\', '/') + } + let fileToRead = filePath + folder + fileName + + let fileContent = fs.readFileSync(fileToRead) + let schemaDocument + try { + schemaDocument = JSON.parse(fileContent) + } catch (err) { + console.log('[ERROR] httpInterface -> sendSchema -> Error Parsing JSON File: ' + fileToRead + ' .Error = ' + err.stack) + respondWithContent("[]", httpResponse) + return + } + schemaArray.push(schemaDocument) + } + let schema = JSON.stringify(schemaArray) + respondWithContent(schema, httpResponse) + } + } catch (err) { + if (err.message.indexOf('no such file or directory') < 0) { + console.log('Could not send Schema:', filePath, schemaType) + console.log(err.stack) + } + respondWithContent("[]", httpResponse) + } + + } + + function sendStyleSheet(fileName) { + let fs = require('fs') + try { + let filePath = global.env.PATH_TO_CLIENT + 'WebServer/css/' + fileName + fs.readFile(filePath, onFileRead) + + function onFileRead(err, file) { + try { + let fileContent = file.toString() + + fileContent = fileContent.replace('DESKTOP_HTTP_INTERFACE_PORT', global.env.DESKTOP_HTTP_INTERFACE_PORT) + fileContent = fileContent.replace('DESKTOP_HTTP_INTERFACE_PORT', global.env.DESKTOP_HTTP_INTERFACE_PORT) + fileContent = fileContent.replace('DESKTOP_HTTP_INTERFACE_PORT', global.env.DESKTOP_HTTP_INTERFACE_PORT) + respondWithContent(fileContent, httpResponse, 'text/css') + } catch (err) { + console.log('[ERROR] httpInterface -> mainCSS -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + } + } + } catch (err) { + console.log(err) + } + } + + function homePage() { + if (requestParameters[1] === '') { + let fs = require('fs') + try { + let fileName = global.env.PATH_TO_CLIENT + 'WebServer/index.html' + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + try { + let fileContent = file.toString() + + fileContent = fileContent.replace('DESKTOP_HTTP_INTERFACE_PORT', global.env.DESKTOP_HTTP_INTERFACE_PORT) + respondWithContent(fileContent, httpResponse) + } catch (err) { + console.log('[ERROR] httpInterface -> homePage -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + } + } + } catch (err) { + console.log(err) + } + } else { + respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1], httpResponse) + } + } + } + + function respondWithFile(fileName, httpResponse) { + let fs = require('fs') + if (fileName.indexOf('undefined') > 0) { + console.log('[WRN] httpInterface -> respondWithFile -> Received httpRequest for undefined file. ') + respondWithContent(undefined, httpResponse) + } else { + try { + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + if (!err) { + respondWithContent(file.toString(), httpResponse) + } else { + //console.log('File requested not found: ' + fileName) + respondWithContent(undefined, httpResponse) + } + } + } catch (err) { + returnEmptyArray() + } + } + } + + function respondWithContent(content, httpResponse, contentType) { + try { + httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. + httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. + httpResponse.setHeader('Expires', '0') // Proxies. + httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. + + if (content !== undefined) { + if (contentType !== undefined) { + httpResponse.writeHead(200, { 'Content-Type': contentType }) + } else { + httpResponse.writeHead(200, { 'Content-Type': 'text/html' }) + } + httpResponse.write(content) + } else { + httpResponse.writeHead(404, { 'Content-Type': 'text/html' }) + httpResponse.write('The specified key does not exist.') + } + httpResponse.end('\n') + } catch (err) { + returnEmptyArray(httpResponse) + } + } + + function respondWithImage(fileName, httpResponse) { + let fs = require('fs') + try { + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + if (err) { + console.log('[ERROR] httpInterface -> respondWithImage -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + return + } + try { + httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. + httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. + httpResponse.setHeader('Expires', '0') // Proxies. + httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. + + httpResponse.writeHead(200, { 'Content-Type': 'image/png' }) + httpResponse.end(file, 'binary') + } catch (err) { + console.log('[ERROR] httpInterface -> respondWithImage -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + } + } + } catch (err) { + console.log('[ERROR] httpInterface -> respondWithImage -> err = ' + err.stack) + } + } + + function respondWithFont(fileName, httpResponse) { + let fs = require('fs') + try { + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + try { + if (err) { + console.log('[ERROR] httpInterface -> respondWithBinary -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + return + } + httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. + httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. + httpResponse.setHeader('Expires', '0') // Proxies. + httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. + + if (fileName.indexOf('2') < 0) { + httpResponse.writeHead(200, { 'Content-Type': 'font/woff' }) + } else { + httpResponse.writeHead(200, { 'Content-Type': 'font/woff2' }) + } + httpResponse.end(file, 'binary') + } catch (err) { + console.log('[ERROR] httpInterface -> respondWithBinary -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + } + } + } catch (err) { + console.log('[ERROR] httpInterface -> respondWithBinary -> err = ' + err.stack) + } + } + + function returnEmptyArray(httpResponse) { + try { + httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. + httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. + httpResponse.setHeader('Expires', '0') // Proxies. + + httpResponse.writeHead(200, { 'Content-Type': 'text/html' }) + httpResponse.write('[]') + httpResponse.end('\n') + } catch (err) { + console.log('[ERROR] httpInterface -> returnEmptyArray -> err.stack ' + err.stack) + } + } + + function getDirectories(path) { + try { + const fs = require('fs') + return fs.readdirSync(path).filter(function (file) { + return fs.statSync(path + '/' + file).isDirectory(); + }); + } catch (err) { + return [] + } + } + + function getAllFilesInDirectoryAndSubdirectories(dir, callback) { + const { promisify } = require('util'); + const { resolve } = require('path'); + const fs = require('fs'); + const readdir = promisify(fs.readdir); + const stat = promisify(fs.stat); + + getFiles(dir) + .then(files => { + let splittedDir = dir.split('/') + let lastFolder = splittedDir[splittedDir.length - 2] + let pathAndNames = [] + for (let i = 0; i < files.length; i++) { + let file = files[i] + let pathName = file.substring(file.indexOf(lastFolder) + lastFolder.length, file.length) + pathName = pathName.substring(1, pathName.length) + pathAndNames.push(pathName) + } + callback(pathAndNames) + }) + .catch(e => { + callback([]) + }); + + async function getFiles(dir) { + const subdirs = await readdir(dir); + const files = await Promise.all(subdirs.map(async (subdir) => { + const res = resolve(dir, subdir); + return (await stat(res)).isDirectory() ? getFiles(res) : res; + })); + return files.reduce((a, f) => a.concat(f), []); + } + } +} diff --git a/Environment.js b/Environment.js index 76276e22a..a237cf14f 100644 --- a/Environment.js +++ b/Environment.js @@ -3,7 +3,7 @@ exports.newEnvironment = function () { let thisObject = { WEB_SERVER_URL: 'localhost', CLIENT_WEB_SOCKETS_INTERFACE_PORT: 18041, - HTTP_INTERFACE_PORT: 34248, + CLIENT_HTTP_INTERFACE_PORT: 34248, PATH_TO_TASK_SERVER: './TaskServer', PATH_TO_DATA_STORAGE: './Data-Storage', PATH_TO_PROJECTS: './Projects', diff --git a/EnvironmentForDebug.js b/EnvironmentForDebug.js index 5e88fb9da..1387f5699 100644 --- a/EnvironmentForDebug.js +++ b/EnvironmentForDebug.js @@ -4,7 +4,8 @@ exports.newEnvironment = function () { WEB_SERVER_URL: 'localhost', CLIENT_WEB_SOCKETS_INTERFACE_PORT: 18041, NETWORK_WEB_SOCKETS_INTERFACE_PORT: 18042, - HTTP_INTERFACE_PORT: 34248, + CLIENT_HTTP_INTERFACE_PORT: 34248, + DESKTOP_HTTP_INTERFACE_PORT: 34249, PATH_TO_TASK_SERVER: './TaskServer', PATH_TO_DATA_STORAGE: './Data-Storage', PATH_TO_PROJECTS: './Projects', diff --git a/Projects/Foundations/TS/Bot-Modules/Sensor-Bot/Webhooks/CheckWebhook.js b/Projects/Foundations/TS/Bot-Modules/Sensor-Bot/Webhooks/CheckWebhook.js index 84d8cfa01..be0254915 100644 --- a/Projects/Foundations/TS/Bot-Modules/Sensor-Bot/Webhooks/CheckWebhook.js +++ b/Projects/Foundations/TS/Bot-Modules/Sensor-Bot/Webhooks/CheckWebhook.js @@ -90,7 +90,7 @@ exports.newFoundationsBotModulesCheckWebhook = function (processIndex) { try { let http = require('http'); let url = 'http://' + global.env.WEB_SERVER_URL + - ':' + global.env.HTTP_INTERFACE_PORT + + ':' + global.env.CLIENT_HTTP_INTERFACE_PORT + '/Webhook/Fetch-Messages/' + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName + "/" + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + "-" + From c99cbbc7a0d6fab6086ad064815dd09ae0fbebfe Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 13:32:31 +0200 Subject: [PATCH 03/11] Update httpInterface.js --- Client/httpInterface.js | 3245 ++++++++++++++++++++------------------- 1 file changed, 1625 insertions(+), 1620 deletions(-) diff --git a/Client/httpInterface.js b/Client/httpInterface.js index c2cf65f27..46c6c5c8b 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -42,262 +42,191 @@ exports.newHttpInterface = function newHttpInterface() { } function onHttpRequest(httpRequest, httpResponse) { + try { + let requestParameters = httpRequest.url.split('/') - function getBody(callback) { // Gets the de body from a POST httpRequest to the web server - try { + switch (requestParameters[1]) { - let body = '' + case 'WEB3': + { + getBody(processRequest) - httpRequest.on('data', function (data) { - body += data - // Too much POST data - //if (body.length > 1e6) { - // httpRequest.connection.destroy() - //} - }) + async function processRequest(body) { + try { + let params = JSON.parse(body) - httpRequest.on('end', function () { - callback(body) - }) + switch (params.method) { + case 'getNetworkClientStatus': { - httpRequest.on('error', function (err) { - console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - }) - } catch (err) { - console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } - } + let serverResponse = await CL.servers.WEB3_SERVER.getNetworkClientStatus( + params.host, + params.port, + params.interface + ) - let requestParameters - requestParameters = httpRequest.url.split('?') // Remove version information - requestParameters = requestParameters[0].split('/') + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'createWalletAccount': { - switch (requestParameters[1]) { + let serverResponse = await CL.servers.WEB3_SERVER.createWalletAccount( + params.entropy + ) - case 'WEB3': - { - getBody(processRequest) + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'getWalletBalances': { - async function processRequest(body) { - try { - let params = JSON.parse(body) + let serverResponse = await CL.servers.WEB3_SERVER.getWalletBalances( + params.host, + params.port, + params.interface, + params.walletDefinition + ) - switch (params.method) { - case 'getNetworkClientStatus': { + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'signData': { - let serverResponse = await CL.servers.WEB3_SERVER.getNetworkClientStatus( - params.host, - params.port, - params.interface - ) + let serverResponse = await CL.servers.WEB3_SERVER.signData( + params.privateKey, + params.data + ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'recoverAddress': { + + let serverResponse = await CL.servers.WEB3_SERVER.recoverAddress( + params.signature + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'mnemonicToPrivateKey': { + + let serverResponse = await CL.servers.WEB3_SERVER.mnemonicToPrivateKey( + params.mnemonic + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + default: { + respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) + } } - case 'createWalletAccount': { + } catch (err) { + console.log('[ERROR] httpInterface -> WEB3s -> Method call produced an error.') + console.log('[ERROR] httpInterface -> WEB3s -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> WEB3s -> Params Received = ' + body) - let serverResponse = await CL.servers.WEB3_SERVER.createWalletAccount( - params.entropy - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'getWalletBalances': { - - let serverResponse = await CL.servers.WEB3_SERVER.getWalletBalances( - params.host, - params.port, - params.interface, - params.walletDefinition - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'signData': { - - let serverResponse = await CL.servers.WEB3_SERVER.signData( - params.privateKey, - params.data - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'recoverAddress': { - - let serverResponse = await CL.servers.WEB3_SERVER.recoverAddress( - params.signature - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'mnemonicToPrivateKey': { - - let serverResponse = await CL.servers.WEB3_SERVER.mnemonicToPrivateKey( - params.mnemonic - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - default: { - respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack } + respondWithContent(JSON.stringify(error), httpResponse) } - } catch (err) { - console.log('[ERROR] httpInterface -> WEB3s -> Method call produced an error.') - console.log('[ERROR] httpInterface -> WEB3s -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> WEB3s -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) } } - } - break - case 'CCXT': - { - getBody(processRequest) + break + case 'CCXT': + { + getBody(processRequest) - async function processRequest(body) { - try { - let params = JSON.parse(body) + async function processRequest(body) { + try { + let params = JSON.parse(body) - const ccxt = require('ccxt') + const ccxt = require('ccxt') - switch (params.method) { - case 'fetchMarkets': { + switch (params.method) { + case 'fetchMarkets': { - const exchangeClass = ccxt[params.exchangeId] - const exchangeConstructorParams = { - 'timeout': 30000, - 'enableRateLimit': true, - verbose: false - } - - let ccxtExchange = new exchangeClass(exchangeConstructorParams) - let ccxtMarkets = [] - - if (ccxtExchange.has.fetchMarkets === true) { - ccxtMarkets = await ccxtExchange.fetchMarkets() - } - respondWithContent(JSON.stringify(ccxtMarkets), httpResponse) - return - } - case 'listExchanges': { - let exchanges = [] - for (let i = 0; i < ccxt.exchanges.length; i++) { - let exchangeId = ccxt.exchanges[i] - - const exchangeClass = ccxt[exchangeId] + const exchangeClass = ccxt[params.exchangeId] const exchangeConstructorParams = { 'timeout': 30000, 'enableRateLimit': true, verbose: false } - let ccxtExchange - try { ccxtExchange = new exchangeClass(exchangeConstructorParams) } - catch (err) { } - if (ccxtExchange === undefined) { continue } + + let ccxtExchange = new exchangeClass(exchangeConstructorParams) + let ccxtMarkets = [] + + if (ccxtExchange.has.fetchMarkets === true) { + ccxtMarkets = await ccxtExchange.fetchMarkets() + } + respondWithContent(JSON.stringify(ccxtMarkets), httpResponse) + return + } + case 'listExchanges': { + let exchanges = [] + for (let i = 0; i < ccxt.exchanges.length; i++) { + let exchangeId = ccxt.exchanges[i] + + const exchangeClass = ccxt[exchangeId] + const exchangeConstructorParams = { + 'timeout': 30000, + 'enableRateLimit': true, + verbose: false + } + let ccxtExchange + try { ccxtExchange = new exchangeClass(exchangeConstructorParams) } + catch (err) { } + if (ccxtExchange === undefined) { continue } - if (ccxtExchange.has.fetchOHLCV === params.has.fetchOHLCV) { - if (ccxtExchange.has.fetchMarkets === params.has.fetchMarkets) { - if (ccxtExchange.timeframes['1m'] !== undefined) { - let exchange = { - name: ccxtExchange.name, - id: ccxtExchange.id + if (ccxtExchange.has.fetchOHLCV === params.has.fetchOHLCV) { + if (ccxtExchange.has.fetchMarkets === params.has.fetchMarkets) { + if (ccxtExchange.timeframes['1m'] !== undefined) { + let exchange = { + name: ccxtExchange.name, + id: ccxtExchange.id + } + exchanges.push(exchange) } - exchanges.push(exchange) } } } + respondWithContent(JSON.stringify(exchanges), httpResponse) + return } - respondWithContent(JSON.stringify(exchanges), httpResponse) - return } - } - let content = { - err: global.DEFAULT_FAIL_RESPONSE // method not supported + let content = { + err: global.DEFAULT_FAIL_RESPONSE // method not supported + } + } catch (err) { + console.log('[INFO] httpInterface -> CCXT FetchMarkets -> Could not fetch markets.') + let error = { + result: 'Fail Because', + message: err.message + } + respondWithContent(JSON.stringify(error), httpResponse) } - } catch (err) { - console.log('[INFO] httpInterface -> CCXT FetchMarkets -> Could not fetch markets.') - let error = { - result: 'Fail Because', - message: err.message - } - respondWithContent(JSON.stringify(error), httpResponse) } } - } - break - case 'Webhook': - { - switch (requestParameters[2]) { // switch by command - case 'Fetch-Messages': { - let exchange = requestParameters[3] - let market = requestParameters[4] - - /* Some validations */ - if (exchange === undefined) { - console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no Exchange received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - if (market === undefined) { - console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no market received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - - let key = exchange + '-' + market - - let webhookMessages = webhook.get(key) - if (webhookMessages === undefined) { - webhookMessages = [] - } - - console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Exchange-Market = ' + exchange + '-' + market) - console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Messeges Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) - - respondWithContent(JSON.stringify(webhookMessages), httpResponse) - webhookMessages = [] - - webhook.set(key, webhookMessages) - break - } - case 'New-Message': { - getBody(processRequest) - - function processRequest(messageReceived) { - let timestamp = (new Date()).valueOf() - let source = requestParameters[3] - let exchange = requestParameters[4] - let market = requestParameters[5] + break + case 'Webhook': + { + switch (requestParameters[2]) { // switch by command + case 'Fetch-Messages': { + let exchange = requestParameters[3] + let market = requestParameters[4] /* Some validations */ - if (source === undefined) { - console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Source received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } if (exchange === undefined) { - console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Exchange received -> messageReceived = ' + messageReceived) + console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no Exchange received -> messageReceived = ' + messageReceived) respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } if (market === undefined) { - console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no market received -> messageReceived = ' + messageReceived) + console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no market received -> messageReceived = ' + messageReceived) respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } @@ -309,1157 +238,1230 @@ exports.newHttpInterface = function newHttpInterface() { webhookMessages = [] } - webhookMessages.push([timestamp, source, messageReceived]) + console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Exchange-Market = ' + exchange + '-' + market) + console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Messeges Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) + + respondWithContent(JSON.stringify(webhookMessages), httpResponse) + webhookMessages = [] + webhook.set(key, webhookMessages) + break + } + case 'New-Message': { + getBody(processRequest) - console.log('[INFO] httpInterface -> Webhook -> New-Message -> Exchange-Market = ' + exchange + '-' + market) - console.log('[INFO] httpInterface -> Webhook -> New-Message -> messageReceived = ' + messageReceived) - console.log('[INFO] httpInterface -> Webhook -> New-Message -> Messeges waiting to be Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + function processRequest(messageReceived) { + let timestamp = (new Date()).valueOf() + let source = requestParameters[3] + let exchange = requestParameters[4] + let market = requestParameters[5] + + /* Some validations */ + if (source === undefined) { + console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Source received -> messageReceived = ' + messageReceived) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + if (exchange === undefined) { + console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Exchange received -> messageReceived = ' + messageReceived) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + if (market === undefined) { + console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no market received -> messageReceived = ' + messageReceived) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + + let key = exchange + '-' + market + + let webhookMessages = webhook.get(key) + if (webhookMessages === undefined) { + webhookMessages = [] + } + + webhookMessages.push([timestamp, source, messageReceived]) + webhook.set(key, webhookMessages) + + console.log('[INFO] httpInterface -> Webhook -> New-Message -> Exchange-Market = ' + exchange + '-' + market) + console.log('[INFO] httpInterface -> Webhook -> New-Message -> messageReceived = ' + messageReceived) + console.log('[INFO] httpInterface -> Webhook -> New-Message -> Messeges waiting to be Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + break } - break } } - } - break - case 'Docs': - { - switch (requestParameters[2]) { // switch by command - case 'Save-Node-Schema': { - getBody(processRequest) + break + case 'Docs': + { + switch (requestParameters[2]) { // switch by command + case 'Save-Node-Schema': { + getBody(processRequest) - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Nodes' - - if (checkAllSchmemaDocuments('Node', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Concept-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Concepts' - - if (checkAllSchmemaDocuments('Concept', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Topic-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Topics' - - if (checkAllSchmemaDocuments('Topic', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Tutorial-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Tutorials' - - if (checkAllSchmemaDocuments('Tutorial', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Review-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Reviews' - - if (checkAllSchmemaDocuments('Review', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Book-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Books' - - if (checkAllSchmemaDocuments('Book', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - } - - function checkAllSchmemaDocuments(category, docsSchema, filePath) { - const fs = require('fs') - let noErrorsDuringSaving = true - - for (let i = 0; i < docsSchema.length; i++) { - let schemaDocument = docsSchema[i] - /* - For some type of schemas we will save the file at an extra - folder derived from the document's type. - */ - let fileName = schemaDocument.type.toLowerCase() - for (let j = 0; j < 10; j++) { - fileName = cleanFileName(fileName) - } - let pageNumber = '00' + schemaDocument.pageNumber - let newFilepath = filePath - switch (category) { - case 'Topic': { - fileName = schemaDocument.topic.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() - fileName = cleanFileName(fileName) - newFilepath = createPrefixDirectories(filePath, schemaDocument.topic) - break - } - case 'Tutorial': { - fileName = schemaDocument.tutorial.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() - fileName = cleanFileName(fileName) - newFilepath = createPrefixDirectories(filePath, schemaDocument.tutorial) - break - } - case 'Review': { - fileName = schemaDocument.review.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() - fileName = cleanFileName(fileName) - newFilepath = createPrefixDirectories(filePath, schemaDocument.review) - break - } - case 'Node': { - newFilepath = createPrefixDirectories(filePath, schemaDocument.type) - break - } - case 'Concept': { - newFilepath = createPrefixDirectories(filePath, schemaDocument.type) - break - } - } - - function createPrefixDirectories(filePath, schemaTextToUse) { - let firstLetter = schemaTextToUse.substring(0, 1) - createNewDir(filePath + '/' + firstLetter) - let extraWord = schemaTextToUse.split(' ')[0] - createNewDir(filePath + '/' + firstLetter + '/' + extraWord) - return filePath + '/' + firstLetter + '/' + extraWord + '/' + cleanFileName(schemaTextToUse) - } - - fileName = fileName + '.json' - - if (schemaDocument.deleted === true) { - try { - fs.unlinkSync(newFilepath + '/' + fileName) - console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' deleted.') - } catch (err) { - noErrorsDuringSaving = false - console.log('[ERROR] httpInterface -> Docs -> Delete -> ' + newFilepath + '/' + fileName + ' could not be deleted.') - console.log('[ERROR] httpInterface -> Docs -> Delete -> Resolve the issue that is preventing the Client to delete this file. Look at the error message below as a guide. At the UI you will need to delete this page again in order for the Client to retry next time you execute the docs.save command.') - console.log('[ERROR] httpInterface -> Docs -> Delete -> err.stack = ' + err.stack) - } - } else { - if (schemaDocument.updated === true || schemaDocument.created === true) { + async function processRequest(body) { try { - let created = schemaDocument.created - let updated = schemaDocument.updated - schemaDocument.updated = undefined - schemaDocument.created = undefined - let fileContent = JSON.stringify(schemaDocument, undefined, 4) - createNewDir(newFilepath) - fs.writeFileSync(newFilepath + '/' + fileName, fileContent) - if (created === true) { - console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' created.') + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Nodes' + + if (checkAllSchmemaDocuments('Node', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } else { - if (updated === true) { - console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' updated.') - } + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Concept-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Concepts' + + if (checkAllSchmemaDocuments('Concept', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Topic-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Topics' + + if (checkAllSchmemaDocuments('Topic', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Tutorial-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Tutorials' + + if (checkAllSchmemaDocuments('Tutorial', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Review-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Reviews' + + if (checkAllSchmemaDocuments('Review', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + + case 'Save-Book-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Books' + + if (checkAllSchmemaDocuments('Book', docsSchema, filePath) === true) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + } + + function checkAllSchmemaDocuments(category, docsSchema, filePath) { + const fs = require('fs') + let noErrorsDuringSaving = true + + for (let i = 0; i < docsSchema.length; i++) { + let schemaDocument = docsSchema[i] + /* + For some type of schemas we will save the file at an extra + folder derived from the document's type. + */ + let fileName = schemaDocument.type.toLowerCase() + for (let j = 0; j < 10; j++) { + fileName = cleanFileName(fileName) + } + let pageNumber = '00' + schemaDocument.pageNumber + let newFilepath = filePath + switch (category) { + case 'Topic': { + fileName = schemaDocument.topic.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() + fileName = cleanFileName(fileName) + newFilepath = createPrefixDirectories(filePath, schemaDocument.topic) + break + } + case 'Tutorial': { + fileName = schemaDocument.tutorial.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() + fileName = cleanFileName(fileName) + newFilepath = createPrefixDirectories(filePath, schemaDocument.tutorial) + break + } + case 'Review': { + fileName = schemaDocument.review.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() + fileName = cleanFileName(fileName) + newFilepath = createPrefixDirectories(filePath, schemaDocument.review) + break + } + case 'Node': { + newFilepath = createPrefixDirectories(filePath, schemaDocument.type) + break + } + case 'Concept': { + newFilepath = createPrefixDirectories(filePath, schemaDocument.type) + break + } + } + + function createPrefixDirectories(filePath, schemaTextToUse) { + let firstLetter = schemaTextToUse.substring(0, 1) + createNewDir(filePath + '/' + firstLetter) + let extraWord = schemaTextToUse.split(' ')[0] + createNewDir(filePath + '/' + firstLetter + '/' + extraWord) + return filePath + '/' + firstLetter + '/' + extraWord + '/' + cleanFileName(schemaTextToUse) + } + + fileName = fileName + '.json' + + if (schemaDocument.deleted === true) { + try { + fs.unlinkSync(newFilepath + '/' + fileName) + console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' deleted.') } catch (err) { noErrorsDuringSaving = false - console.log('[ERROR] httpInterface -> Docs -> Save -> ' + newFilepath + '/' + fileName + ' could not be created / updated.') - console.log('[ERROR] httpInterface -> Docs -> Save -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> Docs -> Delete -> ' + newFilepath + '/' + fileName + ' could not be deleted.') + console.log('[ERROR] httpInterface -> Docs -> Delete -> Resolve the issue that is preventing the Client to delete this file. Look at the error message below as a guide. At the UI you will need to delete this page again in order for the Client to retry next time you execute the docs.save command.') + console.log('[ERROR] httpInterface -> Docs -> Delete -> err.stack = ' + err.stack) + } + } else { + if (schemaDocument.updated === true || schemaDocument.created === true) { + try { + let created = schemaDocument.created + let updated = schemaDocument.updated + schemaDocument.updated = undefined + schemaDocument.created = undefined + let fileContent = JSON.stringify(schemaDocument, undefined, 4) + createNewDir(newFilepath) + fs.writeFileSync(newFilepath + '/' + fileName, fileContent) + if (created === true) { + console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' created.') + } else { + if (updated === true) { + console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' updated.') + } + } + } catch (err) { + noErrorsDuringSaving = false + console.log('[ERROR] httpInterface -> Docs -> Save -> ' + newFilepath + '/' + fileName + ' could not be created / updated.') + console.log('[ERROR] httpInterface -> Docs -> Save -> err.stack = ' + err.stack) + } + } + } + + function createNewDir(path) { + try { + fs.mkdirSync(path) + } catch (err) { + if (err.message.indexOf('file already exists') < 0) { + throw (err) + } } } } - function createNewDir(path) { + return noErrorsDuringSaving + } + + function cleanFileName(fileName) { + for (let i = 0; i < 100; i++) { + fileName = fileName + .replace(' ', '-') + .replace('--', '-') + .replace('?', '') + .replace('#', '') + .replace('$', '') + .replace('%', '') + .replace('^', '') + .replace('&', '') + .replace('*', '') + .replace('(', '') + .replace(')', '') + .replace('!', '') + .replace('..', '.') + .replace(',', '') + .replace('\'', '') + } + return fileName + } + } + break + case 'App': + { + switch (requestParameters[2]) { // switch by command + case 'Contribute': { try { - fs.mkdirSync(path) - } catch (err) { - if (err.message.indexOf('file already exists') < 0) { - throw (err) + let commitMessage = unescape(requestParameters[3]) + const username = unescape(requestParameters[4]) + const token = unescape(requestParameters[5]) + const currentBranch = unescape(requestParameters[6]) + const contributionsBranch = unescape(requestParameters[7]) + let error + + /* Unsavping # */ + for (let i = 0; i < 10; i++) { + commitMessage = commitMessage.replace('_SLASH_', '/') + commitMessage = commitMessage.replace('_HASHTAG_', '#') } - } - } - } - return noErrorsDuringSaving - } + contribute() - function cleanFileName(fileName) { - for (let i = 0; i < 100; i++) { - fileName = fileName - .replace(' ', '-') - .replace('--', '-') - .replace('?', '') - .replace('#', '') - .replace('$', '') - .replace('%', '') - .replace('^', '') - .replace('&', '') - .replace('*', '') - .replace('(', '') - .replace(')', '') - .replace('!', '') - .replace('..', '.') - .replace(',', '') - .replace('\'', '') - } - return fileName - } - } - break - case 'App': - { - switch (requestParameters[2]) { // switch by command - case 'Contribute': { - try { - let commitMessage = unescape(requestParameters[3]) - const username = unescape(requestParameters[4]) - const token = unescape(requestParameters[5]) - const currentBranch = unescape(requestParameters[6]) - const contributionsBranch = unescape(requestParameters[7]) - let error - - /* Unsavping # */ - for (let i = 0; i < 10; i++) { - commitMessage = commitMessage.replace('_SLASH_', '/') - commitMessage = commitMessage.replace('_HASHTAG_', '#') - } - - contribute() - - async function contribute() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - await doGit() - if (error !== undefined) { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Contribution Not Sent', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, error) - return - } - - await doGithub() - if (error !== undefined) { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Contribution Not Sent', - anchor: undefined, - placeholder: {} - } - console.log('respond with docs ') - - respondWithDocsObject(docs, error) - return - } - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } - } - - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) - - try { - await git.add('./*') - await git.commit(commitMessage) - await git.push('origin', currentBranch) - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> contributionsBranch = ' + contributionsBranch) - console.log('') - console.log('Torubleshooting Tips:') - console.log('') - console.log('1. Make sure that you have set up your Github Username and Token at the APIs -> Github API node at the workspace.') - console.log('2. Make sure you are running the latest version of Git available for your OS.') - console.log('3. Make sure that you have cloned your Superalgos repository fork, and not the main Superalgos repository.') - console.log('4. If your fork is old, you might need to do an app.update and also a node setup at every branch. If you just reforked all is good.') - error = err - } - } - - async function doGithub() { - - const { Octokit } = require("@octokit/rest") - - const octokit = new Octokit({ - auth: token, - userAgent: 'Superalgos Beta 11' - }) - - const repo = 'Superalgos' - const owner = 'Superalgos' - const head = username + ':' + contributionsBranch - const base = currentBranch - const title = 'Contribution: ' + commitMessage - - try { - await octokit.pulls.create({ - owner, - repo, - title, - head, - base, - }); - } catch (err) { - if (err.stack.indexOf('A pull request already exists') >= 0) { - return + async function contribute() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') } else { - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10)) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch) + await doGit() + if (error !== undefined) { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Contribution Not Sent', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + return + } + + await doGithub() + if (error !== undefined) { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Contribution Not Sent', + anchor: undefined, + placeholder: {} + } + console.log('respond with docs ') + + respondWithDocsObject(docs, error) + return + } + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + + try { + await git.add('./*') + await git.commit(commitMessage) + await git.push('origin', currentBranch) + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> contributionsBranch = ' + contributionsBranch) + console.log('') + console.log('Torubleshooting Tips:') + console.log('') + console.log('1. Make sure that you have set up your Github Username and Token at the APIs -> Github API node at the workspace.') + console.log('2. Make sure you are running the latest version of Git available for your OS.') + console.log('3. Make sure that you have cloned your Superalgos repository fork, and not the main Superalgos repository.') + console.log('4. If your fork is old, you might need to do an app.update and also a node setup at every branch. If you just reforked all is good.') error = err } } - } - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Contribute -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> username = ' + username) - console.log('[ERROR] httpInterface -> App -> Contribute -> token starts with = ' + token.substring(0, 10) + '...') - console.log('[ERROR] httpInterface -> App -> Contribute -> token ends with = ' + '...' + token.substring(token.length - 10)) - console.log('[ERROR] httpInterface -> App -> Contribute -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> contributionsBranch = ' + contributionsBranch) + async function doGithub() { - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - break - } + const { Octokit } = require("@octokit/rest") - case 'Update': { - try { - const currentBranch = unescape(requestParameters[3]) - update() + const octokit = new Octokit({ + auth: token, + userAgent: 'Superalgos Beta 11' + }) - async function update() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - let result = await doGit() + const repo = 'Superalgos' + const owner = 'Superalgos' + const head = username + ':' + contributionsBranch + const base = currentBranch + const title = 'Contribution: ' + commitMessage - if (result.error === undefined) { - let customResponse = { - result: global.CUSTOM_OK_RESPONSE.result, - message: result.message + try { + await octokit.pulls.create({ + owner, + repo, + title, + head, + base, + }); + } catch (err) { + if (err.stack.indexOf('A pull request already exists') >= 0) { + return + } else { + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...') + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10)) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch) + error = err } - respondWithContent(JSON.stringify(customResponse), httpResponse) - } else { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Update Failed', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, result.error) - } } - } - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Contribute -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Contribute -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> App -> Contribute -> commitMessage = ' + commitMessage) + console.log('[ERROR] httpInterface -> App -> Contribute -> username = ' + username) + console.log('[ERROR] httpInterface -> App -> Contribute -> token starts with = ' + token.substring(0, 10) + '...') + console.log('[ERROR] httpInterface -> App -> Contribute -> token ends with = ' + '...' + token.substring(token.length - 10)) + console.log('[ERROR] httpInterface -> App -> Contribute -> currentBranch = ' + currentBranch) + console.log('[ERROR] httpInterface -> App -> Contribute -> contributionsBranch = ' + contributionsBranch) - let message - try { - message = await git.pull('https://github.com/Superalgos/Superalgos', currentBranch) - return { message: message } - } catch (err) { - console.log('[ERROR] Error updating ' + currentBranch) - console.log(err.stack) - return { error: err } + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack } + respondWithContent(JSON.stringify(error), httpResponse) } - - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) + break } - break - } - case 'Checkout': { - try { - const currentBranch = unescape(requestParameters[3]) - let error + case 'Update': { + try { + const currentBranch = unescape(requestParameters[3]) + update() - checkout() - - async function checkout() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - await doGit() - - if (error === undefined) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + async function update() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') } else { - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'Switching Branches - Current Branch Not Changed', - anchor: undefined, - placeholder: {} - } + let result = await doGit() - respondWithDocsObject(docs, error) + if (result.error === undefined) { + let customResponse = { + result: global.CUSTOM_OK_RESPONSE.result, + message: result.message + } + respondWithContent(JSON.stringify(customResponse), httpResponse) + } else { + + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Update Failed', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, result.error) + + } } } - } - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) - try { - await git.checkout(currentBranch) - } catch (err) { - console.log('[ERROR] Error changing current branch to ' + currentBranch) - console.log(err.stack) - error = err - } - } + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - break - } - - case 'Branch': { - try { - branch() - - async function branch() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - let result = await doGit() - - if (result.error === undefined) { - let customResponse = { - result: global.CUSTOM_OK_RESPONSE.result, - message: result - } - respondWithContent(JSON.stringify(customResponse), httpResponse) - } else { - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Could Not Get Current Branch', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, error) + let message + try { + message = await git.pull('https://github.com/Superalgos/Superalgos', currentBranch) + return { message: message } + } catch (err) { + console.log('[ERROR] Error updating ' + currentBranch) + console.log(err.stack) + return { error: err } } } - } - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack } - const git = simpleGit(options) - try { - return await git.branch() - } catch (err) { - console.log('[ERROR] Error reading current branch.') - console.log(err.stack) + respondWithContent(JSON.stringify(error), httpResponse) + } + break + } + + case 'Checkout': { + try { + const currentBranch = unescape(requestParameters[3]) + let error + + checkout() + + async function checkout() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') + } else { + await doGit() + + if (error === undefined) { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } else { + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'Switching Branches - Current Branch Not Changed', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + } + } } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + try { + await git.checkout(currentBranch) + } catch (err) { + console.log('[ERROR] Error changing current branch to ' + currentBranch) + console.log(err.stack) + error = err + } + } + + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) } + break + } - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + case 'Branch': { + try { + branch() - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack + async function branch() { + const { lookpath } = require('lookpath'); + const gitpath = await lookpath('git'); + if (gitpath === undefined) { + console.log('[ERROR] `git` not installed.') + } else { + let result = await doGit() + + if (result.error === undefined) { + let customResponse = { + result: global.CUSTOM_OK_RESPONSE.result, + message: result + } + respondWithContent(JSON.stringify(customResponse), httpResponse) + } else { + let docs = { + project: 'Foundations', + category: 'Topic', + type: 'App Error - Could Not Get Current Branch', + anchor: undefined, + placeholder: {} + } + + respondWithDocsObject(docs, error) + } + } + } + + async function doGit() { + const simpleGit = require('simple-git'); + const options = { + baseDir: process.cwd(), + binary: 'git', + maxConcurrentProcesses: 6, + } + const git = simpleGit(options) + try { + return await git.branch() + } catch (err) { + console.log('[ERROR] Error reading current branch.') + console.log(err.stack) + } + } + + } catch (err) { + console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') + console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) } - respondWithContent(JSON.stringify(error), httpResponse) + break } - break } - } - function respondWithDocsObject(docs, error) { + function respondWithDocsObject(docs, error) { - if (error.message !== undefined) { - docs.placeholder.errorMessage = { - style: 'Error', - text: error.message + if (error.message !== undefined) { + docs.placeholder.errorMessage = { + style: 'Error', + text: error.message + } } - } - if (error.stack !== undefined) { - docs.placeholder.errorStack = { - style: 'Javascript', - text: error.stack + if (error.stack !== undefined) { + docs.placeholder.errorStack = { + style: 'Javascript', + text: error.stack + } } - } - if (error.code !== undefined) { - docs.placeholder.errorCode = { + if (error.code !== undefined) { + docs.placeholder.errorCode = { + style: 'Json', + text: error.code + } + } + + docs.placeholder.errorDetails = { style: 'Json', - text: error.code + text: JSON.stringify(error, undefined, 4) + } + + let customResponse = { + result: global.CUSTOM_FAIL_RESPONSE.result, + docs: docs + } + + respondWithContent(JSON.stringify(customResponse), httpResponse) + + } + } + break + case 'GOV': + { + /* + This is the Governance endpoint at the Http Interface. All methods + related to the Governance System are implemented here and routed + to the backend Servers that can process them. + */ + getBody(processRequest) + + async function processRequest(body) { + try { + let params = JSON.parse(body) + + switch (params.method) { + case 'getGithubStars': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubStars( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'getGithubWatchers': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubWatchers( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'getGithubForks': { + + let serverResponse = await CL.servers.GITHUB_SERVER.getGithubForks( + params.repository, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'mergePullRequests': { + + let serverResponse = await CL.servers.GITHUB_SERVER.mergePullRequests( + params.commitMessage, + params.username, + params.token + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + case 'payContributors': { + + let serverResponse = await CL.servers.WEB3_SERVER.payContributors( + params.contractAddress, + params.contractAbi, + params.paymentsArray, + params.mnemonic + ) + + respondWithContent(JSON.stringify(serverResponse), httpResponse) + return + } + default: { + respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) + } + } + } catch (err) { + console.log('[ERROR] httpInterface -> GOV -> Method call produced an error.') + console.log('[ERROR] httpInterface -> GOV -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> GOV -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) } } + } + break + case 'LegacyPlotter.js': + { + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse) + } + break + case 'PlotterPanel.js': + { + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', httpResponse) + } + break + case 'Images': // This means the Images folder. + { + let path = global.env.PATH_TO_CLIENT + 'WebServer/Images/' + requestParameters[2] - docs.placeholder.errorDetails = { - style: 'Json', - text: JSON.stringify(error, undefined, 4) + if (requestParameters[3] !== undefined) { + path = path + '/' + requestParameters[3] } - let customResponse = { - result: global.CUSTOM_FAIL_RESPONSE.result, - docs: docs + if (requestParameters[4] !== undefined) { + path = path + '/' + requestParameters[4] } - respondWithContent(JSON.stringify(customResponse), httpResponse) - - } - } - break - case 'GOV': - { - /* - This is the Governance endpoint at the Http Interface. All methods - related to the Governance System are implemented here and routed - to the backend Servers that can process them. - */ - getBody(processRequest) - - async function processRequest(body) { - try { - let params = JSON.parse(body) - - switch (params.method) { - case 'getGithubStars': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubStars( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'getGithubWatchers': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubWatchers( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'getGithubForks': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubForks( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'mergePullRequests': { - - let serverResponse = await CL.servers.GITHUB_SERVER.mergePullRequests( - params.commitMessage, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'payContributors': { - - let serverResponse = await CL.servers.WEB3_SERVER.payContributors( - params.contractAddress, - params.contractAbi, - params.paymentsArray, - params.mnemonic - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - default: { - respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) - } - } - } catch (err) { - console.log('[ERROR] httpInterface -> GOV -> Method call produced an error.') - console.log('[ERROR] httpInterface -> GOV -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> GOV -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) + if (requestParameters[5] !== undefined) { + path = path + '/' + requestParameters[5] } + + path = unescape(path) + + respondWithImage(path, httpResponse) } - } - break - case 'LegacyPlotter.js': - { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse) - } - break - case 'PlotterPanel.js': - { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', httpResponse) - } - break - case 'Images': // This means the Images folder. - { - let path = global.env.PATH_TO_CLIENT + 'WebServer/Images/' + requestParameters[2] + break + case 'Icons': // This means the Icons folder under Projects. + { + let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Icons' - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] + if (requestParameters[3] !== undefined) { + path = path + '/' + requestParameters[3] + } + + if (requestParameters[4] !== undefined) { + path = path + '/' + requestParameters[4] + } + + if (requestParameters[5] !== undefined) { + path = path + '/' + requestParameters[5] + } + + path = unescape(path) + + respondWithImage(path, httpResponse) } + break + case 'GIFs': // This means the GIFs folder under Projects. + { + let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/GIFs' - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] + if (requestParameters[3] !== undefined) { + path = path + '/' + requestParameters[3] + } + + if (requestParameters[4] !== undefined) { + path = path + '/' + requestParameters[4] + } + + if (requestParameters[5] !== undefined) { + path = path + '/' + requestParameters[5] + } + + path = unescape(path) + respondWithImage(path, httpResponse) } + break + case 'PNGs': // This means the PNGs folder under Projects. + { + let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/PNGs' - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] + if (requestParameters[3] !== undefined) { + path = path + '/' + requestParameters[3] + } + + if (requestParameters[4] !== undefined) { + path = path + '/' + requestParameters[4] + } + + if (requestParameters[5] !== undefined) { + path = path + '/' + requestParameters[5] + } + + path = unescape(path) + respondWithImage(path, httpResponse) } - - path = unescape(path) - - respondWithImage(path, httpResponse) - } - break - case 'Icons': // This means the Icons folder under Projects. - { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Icons' - - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] + break + case 'favicon.ico': // This means the Scripts folder. + { + respondWithImage(global.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', httpResponse) } - - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] + break + case 'WebServer': // This means the WebServer folder. + { + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestParameters[2], httpResponse) } - - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] + break + case 'externalScripts': // This means the WebServer folder. + { + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestParameters[2], httpResponse) } - - path = unescape(path) - - respondWithImage(path, httpResponse) - } - break - case 'GIFs': // This means the GIFs folder under Projects. - { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/GIFs' - - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] + break + case 'Plotters': // This means the plotter folder, not to be confused with the Plotters script! + { + let project = requestParameters[2] + let dataMine = requestParameters[3] + let codeName = requestParameters[4] + let moduleName = requestParameters[5] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'Bots-Plotters-Code' + '/' + dataMine + '/plotters/' + codeName + '/' + moduleName + respondWithFile(filePath, httpResponse) } - - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] + break + case 'ChartLayers': + { + respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1] + '/' + requestParameters[2], httpResponse) } - - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] + break + case 'Files': + { + respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestParameters[2], httpResponse) } - - path = unescape(path) - respondWithImage(path, httpResponse) - } - break - case 'PNGs': // This means the PNGs folder under Projects. - { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/PNGs' - - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] + break + case 'Fonts': + { + respondWithFont(global.env.PATH_TO_FONTS + '/' + requestParameters[2], httpResponse) } - - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] + break + case 'Schema': + { + sendSchema(global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Schemas/', requestParameters[3]) } + break + case 'DirContent': + { + let folderPath = unescape(requestParameters[2]) + if (requestParameters[3] !== undefined) { + folderPath = folderPath + '/' + requestParameters[3] + } - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] - } + if (requestParameters[4] !== undefined) { + folderPath = folderPath + '/' + requestParameters[4] + } - path = unescape(path) - respondWithImage(path, httpResponse) - } - break - case 'favicon.ico': // This means the Scripts folder. - { - respondWithImage(global.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', httpResponse) - } - break - case 'WebServer': // This means the WebServer folder. - { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestParameters[2], httpResponse) - } - break - case 'externalScripts': // This means the WebServer folder. - { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestParameters[2], httpResponse) - } - break - case 'Plotters': // This means the plotter folder, not to be confused with the Plotters script! - { - let project = requestParameters[2] - let dataMine = requestParameters[3] - let codeName = requestParameters[4] - let moduleName = requestParameters[5] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'Bots-Plotters-Code' + '/' + dataMine + '/plotters/' + codeName + '/' + moduleName - respondWithFile(filePath, httpResponse) - } - break - case 'ChartLayers': - { - respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1] + '/' + requestParameters[2], httpResponse) - } - break - case 'Files': - { - respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestParameters[2], httpResponse) - } - break - case 'Fonts': - { - respondWithFont(global.env.PATH_TO_FONTS + '/' + requestParameters[2], httpResponse) - } - break - case 'Schema': - { - sendSchema(global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Schemas/', requestParameters[3]) - } - break - case 'DirContent': - { - let folderPath = unescape(requestParameters[2]) - if (requestParameters[3] !== undefined) { - folderPath = folderPath + '/' + requestParameters[3] - } - - if (requestParameters[4] !== undefined) { - folderPath = folderPath + '/' + requestParameters[4] - } - - if (requestParameters[5] !== undefined) { - folderPath = folderPath + '/' + requestParameters[5] - } - let folder - if (requestParameters[2] === 'Root') { - folder = folderPath.replace('Root', '../Superalgos/') - } else { - folder = global.env.PATH_TO_PROJECTS + '/' + folderPath - } - - getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) - - function onFilesReady(files) { - respondWithContent(JSON.stringify(files), httpResponse) - } - } - break - case 'IconNames': - { - let projects = getDirectories(global.env.PATH_TO_PROJECTS) - let icons = [] - let totalProjects = projects.length - let projectCounter = 0 - - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - - const folder = global.env.PATH_TO_PROJECTS + '/' + project + '/Icons/' + if (requestParameters[5] !== undefined) { + folderPath = folderPath + '/' + requestParameters[5] + } + let folder + if (requestParameters[2] === 'Root') { + folder = folderPath.replace('Root', '../Superalgos/') + } else { + folder = global.env.PATH_TO_PROJECTS + '/' + folderPath + } getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) function onFilesReady(files) { - for (let j = 0; j < files.length; j++) { - let file = files[j] - for (let i = 0; i < 10; i++) { - file = file.replace('/', '\\') + respondWithContent(JSON.stringify(files), httpResponse) + } + } + break + case 'IconNames': + { + let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let icons = [] + let totalProjects = projects.length + let projectCounter = 0 + + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + + const folder = global.env.PATH_TO_PROJECTS + '/' + project + '/Icons/' + + getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) + + function onFilesReady(files) { + for (let j = 0; j < files.length; j++) { + let file = files[j] + for (let i = 0; i < 10; i++) { + file = file.replace('/', '\\') + } + icons.push([project, file]) } - icons.push([project, file]) - } - projectCounter++ - if (projectCounter === totalProjects) { - respondWithContent(JSON.stringify(icons), httpResponse) + projectCounter++ + if (projectCounter === totalProjects) { + respondWithContent(JSON.stringify(icons), httpResponse) + } } } } - } - break - case 'PluginFileNames': - { - processRequest() + break + case 'PluginFileNames': + { + processRequest() - async function processRequest(body) { - try { - let project = unescape(requestParameters[2]) - let folder = unescape(requestParameters[3]) + async function processRequest(body) { + try { + let project = unescape(requestParameters[2]) + let folder = unescape(requestParameters[3]) - let response = await SA.projects.foundations.utilities.plugins.getPluginFileNames( - project, - folder - ) + let response = await SA.projects.foundations.utilities.plugins.getPluginFileNames( + project, + folder + ) - respondWithContent(JSON.stringify(response), httpResponse) + respondWithContent(JSON.stringify(response), httpResponse) - } catch (err) { - console.log('[ERROR] httpInterface -> PluginFileNames -> Method call produced an error.') - console.log('[ERROR] httpInterface -> PluginFileNames -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> PluginFileNames -> Params Received = ' + body) + } catch (err) { + console.log('[ERROR] httpInterface -> PluginFileNames -> Method call produced an error.') + console.log('[ERROR] httpInterface -> PluginFileNames -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> PluginFileNames -> Params Received = ' + body) - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - } - break - case 'LoadPlugin': - { - processRequest() - - async function processRequest(body) { - try { - let project = unescape(requestParameters[2]) - let folder = unescape(requestParameters[3]) - let fileName = unescape(requestParameters[4]) - - let response = await SA.projects.foundations.utilities.plugins.getPluginFileContent( - project, - folder, - fileName - ).catch(err => { let error = { result: 'Fail Because', - message: err + message: err.message, + stack: err.stack } respondWithContent(JSON.stringify(error), httpResponse) - return - }) - - respondWithContent(response, httpResponse) - - } catch (err) { - console.log('[ERROR] httpInterface -> LoadPlugin -> Method call produced an error.') - console.log('[ERROR] httpInterface -> LoadPlugin -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> LoadPlugin -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) } } - } - break - case 'SavePlugin': - { - getBody(processRequest) + break + case 'LoadPlugin': + { + processRequest() + + async function processRequest(body) { + try { + let project = unescape(requestParameters[2]) + let folder = unescape(requestParameters[3]) + let fileName = unescape(requestParameters[4]) + + let response = await SA.projects.foundations.utilities.plugins.getPluginFileContent( + project, + folder, + fileName + ).catch(err => { + let error = { + result: 'Fail Because', + message: err + } + respondWithContent(JSON.stringify(error), httpResponse) + return + }) + + respondWithContent(response, httpResponse) + + } catch (err) { + console.log('[ERROR] httpInterface -> LoadPlugin -> Method call produced an error.') + console.log('[ERROR] httpInterface -> LoadPlugin -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> LoadPlugin -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + } + break + case 'SavePlugin': + { + getBody(processRequest) + + async function processRequest(body) { + try { + let plugin = JSON.parse(body) + let project = requestParameters[2] + let folder = requestParameters[3] + let fileName = requestParameters[4] + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/' + folder + let fileContent = JSON.stringify(plugin, undefined, 4) + const fs = require('fs') + fs.writeFileSync(filePath + '/' + fileName + '.json', fileContent) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } catch (err) { + console.log('[ERROR] httpInterface -> SavePlugin -> Method call produced an error.') + console.log('[ERROR] httpInterface -> SavePlugin -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> SavePlugin -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + } + break + case 'Workspace.js': + { + let fs = require('fs') - async function processRequest(body) { try { - let plugin = JSON.parse(body) - let project = requestParameters[2] - let folder = requestParameters[3] - let fileName = requestParameters[4] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/' + folder - let fileContent = JSON.stringify(plugin, undefined, 4) - const fs = require('fs') - fs.writeFileSync(filePath + '/' + fileName + '.json', fileContent) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } catch (err) { - console.log('[ERROR] httpInterface -> SavePlugin -> Method call produced an error.') - console.log('[ERROR] httpInterface -> SavePlugin -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> SavePlugin -> Params Received = ' + body) + let filePath = global.env.PATH_TO_DEFAULT_WORKSPACE + '/Getting-Started-Tutorials.json' + fs.readFile(filePath, onFileRead) + } catch (e) { + console.log('[ERROR] Error reading the Workspace.', e) + } - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack + function onFileRead(err, workspace) { + if (err) { + respondWithContent(undefined, httpResponse) + } else { + let responseContent = 'function getWorkspace(){ return ' + workspace + '}' + respondWithContent(responseContent, httpResponse) } - respondWithContent(JSON.stringify(error), httpResponse) } } - } - break - case 'Workspace.js': - { - let fs = require('fs') + break + case 'ListWorkspaces': + { + let allWorkspaces = [] + let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let projectsCount = 0 - try { - let filePath = global.env.PATH_TO_DEFAULT_WORKSPACE + '/Getting-Started-Tutorials.json' - fs.readFile(filePath, onFileRead) - } catch (e) { - console.log('[ERROR] Error reading the Workspace.', e) - } + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + readPluginWorkspaces() + function readPluginWorkspaces() { + let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/Workspaces' + try { + let fs = require('fs') + fs.readdir(dirPath, onDirRead) - function onFileRead(err, workspace) { - if (err) { - respondWithContent(undefined, httpResponse) - } else { - let responseContent = 'function getWorkspace(){ return ' + workspace + '}' - respondWithContent(responseContent, httpResponse) + function onDirRead(err, fileList) { + let updatedFileList = [] + + if (err) { + /* + If we have a problem reading this folder we will assume that it is + because this project does not need this folder and that's it. + */ + //console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) + } else { + for (let i = 0; i < fileList.length; i++) { + let name = 'Plugin \u2192 ' + fileList[i] + updatedFileList.push([project, name]) + } + } + allWorkspaces = allWorkspaces.concat(updatedFileList) + projectsCount++ + if (projectsCount === projects.length) { + readMyWorkspaces() + } + } + } catch (err) { + console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) + console.log('[ERROR] err.stack = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + } } - } - } - break - case 'ListWorkspaces': - { - let allWorkspaces = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) - let projectsCount = 0 - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - readPluginWorkspaces() - function readPluginWorkspaces() { - let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/Workspaces' + function readMyWorkspaces() { + let dirPath = global.env.PATH_TO_MY_WORKSPACES try { let fs = require('fs') fs.readdir(dirPath, onDirRead) function onDirRead(err, fileList) { - let updatedFileList = [] - if (err) { - /* - If we have a problem reading this folder we will assume that it is - because this project does not need this folder and that's it. - */ - //console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) + // This happens the first time you run the software. + respondWithContent(JSON.stringify(allWorkspaces), httpResponse) + return } else { + let updatedFileList = [] for (let i = 0; i < fileList.length; i++) { - let name = 'Plugin \u2192 ' + fileList[i] - updatedFileList.push([project, name]) + let name = fileList[i] + updatedFileList.push(['', name]) } - } - allWorkspaces = allWorkspaces.concat(updatedFileList) - projectsCount++ - if (projectsCount === projects.length) { - readMyWorkspaces() + allWorkspaces = allWorkspaces.concat(updatedFileList) + respondWithContent(JSON.stringify(allWorkspaces), httpResponse) + return } } } catch (err) { @@ -1470,431 +1472,434 @@ exports.newHttpInterface = function newHttpInterface() { } } } + break + case 'LoadMyWorkspace': + { + let fileName = unescape(requestParameters[2]) + let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' + respondWithFile(filePath, httpResponse) + } + break + case 'SaveWorkspace': + { + getBody(processRequest) - function readMyWorkspaces() { - let dirPath = global.env.PATH_TO_MY_WORKSPACES + async function processRequest(body) { + + let fileContent = body + let fileName = unescape(requestParameters[2]) + let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' + + try { + let fs = require('fs') + let dir = global.env.PATH_TO_MY_WORKSPACES; + + /* Create Dir if it does not exist */ + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } + + fs.writeFile(filePath, fileContent, onFileWritten) + + function onFileWritten(err) { + if (err) { + console.log('[ERROR] SaveWorkspace -> onFileWritten -> Error writing the Workspace file. fileName = ' + fileName) + console.log('[ERROR] SaveWorkspace -> onFileWritten -> err.stack = ' + err.stack) + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } else { + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + } + + } catch (err) { + console.log('[ERROR] SaveWorkspace -> Error writing the Workspace file. fileName = ' + fileName) + console.log('[ERROR] SaveWorkspace -> err.stack = ' + err.stack) + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + } + break + case 'ListFunctionLibraries': + { + returnProjectFolderFileList('Function-Libraries') + } + break + case 'ProjectsSchema': + { + let path = global.env.PATH_TO_PROJECTS + '/' + 'ProjectsSchema.json' + respondWithFile(path, httpResponse) + } + break + case 'ListSpaceFiles': + { + let fs = require('fs') + let allFiles = [] + let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let dirCount = 0 + let totalDirs = 0 + + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + + let dirPath = project + '/UI/Spaces' + let spaces = getDirectories(global.env.PATH_TO_PROJECTS + '/' + dirPath) + + for (let j = 0; j < spaces.length; j++) { + let space = spaces[j] + readDirectory(dirPath + '/' + space) + } + + function readDirectory(path) { + try { + + totalDirs++ + fs.readdir(global.env.PATH_TO_PROJECTS + '/' + path, onDirRead) + + let otherDirs = getDirectories(global.env.PATH_TO_PROJECTS + '/' + path) + for (let m = 0; m < otherDirs.length; m++) { + let otherDir = otherDirs[m] + readDirectory(path + '/' + otherDir) + } + + function onDirRead(err, fileList) { + if (err) { + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } else { + let updatedFileList = [] + for (let k = 0; k < fileList.length; k++) { + let name = fileList[k] + if (name.indexOf('.js') < 0) { continue } + updatedFileList.push(path + '/' + name) + } + allFiles = allFiles.concat(updatedFileList) + dirCount++ + if (dirCount === totalDirs) { + respondWithContent(JSON.stringify(allFiles), httpResponse) + } + } + } + } catch (err) { + console.log('[ERROR] Error reading a directory content. filePath = ' + path) + console.log('[ERROR] err.stack = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + } + } + } + break + case 'ListUtilitiesFiles': + { + returnProjectFolderFileList('Utilities') + } + break + case 'ListGlobalFiles': + { + returnProjectFolderFileList('Globals') + } + break + case 'Projects': + { + let path = '' + for (let i = 2; i < 10; i++) { + if (requestParameters[i] !== undefined) { + let parameter = unescape(requestParameters[i]) + path = path + '/' + parameter + } + + } + let filePath = global.env.PATH_TO_PROJECTS + path + respondWithFile(filePath, httpResponse) + } + break + case 'Storage': + { + let pathToFile = httpRequest.url.substring(9) + /* Unsavping # */ + for (let i = 0; i < 10; i++) { + pathToFile = pathToFile.replace('_HASHTAG_', '#') + } + respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) + } + break + case 'main.css': + { + sendStyleSheet('main.css') + } + break + case 'tutorial.css': + { + sendStyleSheet('tutorial.css') + } + break + case 'docs.css': + { + sendStyleSheet('docs.css') + } + break + case 'governance.css': + { + sendStyleSheet('governance.css') + } + break + case 'context-menu.css': + { + sendStyleSheet('context-menu.css') + } + break + case 'credits.css': + { + sendStyleSheet('credits.css') + } + break + case 'docs.css': + { + sendStyleSheet('docs.css') + } + break + case 'font-awasome.css': + { + sendStyleSheet('font-awasome.css') + } + break + case 'prism.css': + { + sendStyleSheet('prism.css') + } + break + case 'ExecuteTerminalCommand': + { + let command = unescape(requestParameters[2]) + executeTerminalCommand(command) + } + break + default: + { + defaultEndpoint() + } + } + + function getBody(callback) { // Gets the de body from a POST httpRequest to the web server + try { + + let body = '' + + httpRequest.on('data', function (data) { + body += data + // Too much POST data + //if (body.length > 1e6) { + // httpRequest.connection.destroy() + //} + }) + + httpRequest.on('end', function () { + callback(body) + }) + + httpRequest.on('error', function (err) { + console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + }) + } catch (err) { + console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + } + } + + function returnProjectFolderFileList(projectFolderName) { + { + let allLibraries = [] + let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let projectsCount = 0 + + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + + let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'UI' + '/' + projectFolderName try { let fs = require('fs') fs.readdir(dirPath, onDirRead) function onDirRead(err, fileList) { + let updatedFileList = [] if (err) { - // This happens the first time you run the software. - respondWithContent(JSON.stringify(allWorkspaces), httpResponse) - return + /* + If we have a problem reading this folder we will assume that it is + because this project does not need this folder and that's it. + */ + // console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) } else { - let updatedFileList = [] for (let i = 0; i < fileList.length; i++) { let name = fileList[i] - updatedFileList.push(['', name]) + updatedFileList.push([project, name]) } - allWorkspaces = allWorkspaces.concat(updatedFileList) - respondWithContent(JSON.stringify(allWorkspaces), httpResponse) - return + } + allLibraries = allLibraries.concat(updatedFileList) + projectsCount++ + if (projectsCount === projects.length) { + respondWithContent(JSON.stringify(allLibraries), httpResponse) } } } catch (err) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) console.log('[ERROR] err.stack = ' + err.stack) + respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } } } - break - case 'LoadMyWorkspace': - { - let fileName = unescape(requestParameters[2]) - let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' - respondWithFile(filePath, httpResponse) - } - break - case 'SaveWorkspace': - { - getBody(processRequest) - - async function processRequest(body) { - - let fileContent = body - let fileName = unescape(requestParameters[2]) - let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' - - try { - let fs = require('fs') - let dir = global.env.PATH_TO_MY_WORKSPACES; - - /* Create Dir if it does not exist */ - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); - } - - fs.writeFile(filePath, fileContent, onFileWritten) - - function onFileWritten(err) { - if (err) { - console.log('[ERROR] SaveWorkspace -> onFileWritten -> Error writing the Workspace file. fileName = ' + fileName) - console.log('[ERROR] SaveWorkspace -> onFileWritten -> err.stack = ' + err.stack) - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } - } - - } catch (err) { - console.log('[ERROR] SaveWorkspace -> Error writing the Workspace file. fileName = ' + fileName) - console.log('[ERROR] SaveWorkspace -> err.stack = ' + err.stack) - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - } - break - case 'ListFunctionLibraries': - { - returnProjectFolderFileList('Function-Libraries') - } - break - case 'ProjectsSchema': - { - let path = global.env.PATH_TO_PROJECTS + '/' + 'ProjectsSchema.json' - respondWithFile(path, httpResponse) - } - break - case 'ListSpaceFiles': - { - let fs = require('fs') - let allFiles = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) - let dirCount = 0 - let totalDirs = 0 - - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - - let dirPath = project + '/UI/Spaces' - let spaces = getDirectories(global.env.PATH_TO_PROJECTS + '/' + dirPath) - - for (let j = 0; j < spaces.length; j++) { - let space = spaces[j] - readDirectory(dirPath + '/' + space) - } - - function readDirectory(path) { - try { - - totalDirs++ - fs.readdir(global.env.PATH_TO_PROJECTS + '/' + path, onDirRead) - - let otherDirs = getDirectories(global.env.PATH_TO_PROJECTS + '/' + path) - for (let m = 0; m < otherDirs.length; m++) { - let otherDir = otherDirs[m] - readDirectory(path + '/' + otherDir) - } - - function onDirRead(err, fileList) { - if (err) { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } else { - let updatedFileList = [] - for (let k = 0; k < fileList.length; k++) { - let name = fileList[k] - if (name.indexOf('.js') < 0) { continue } - updatedFileList.push(path + '/' + name) - } - allFiles = allFiles.concat(updatedFileList) - dirCount++ - if (dirCount === totalDirs) { - respondWithContent(JSON.stringify(allFiles), httpResponse) - } - } - } - } catch (err) { - console.log('[ERROR] Error reading a directory content. filePath = ' + path) - console.log('[ERROR] err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - } - } - } - break - case 'ListUtilitiesFiles': - { - returnProjectFolderFileList('Utilities') - } - break - case 'ListGlobalFiles': - { - returnProjectFolderFileList('Globals') - } - break - case 'Projects': - { - let path = '' - for (let i = 2; i < 10; i++) { - if (requestParameters[i] !== undefined) { - let parameter = unescape(requestParameters[i]) - path = path + '/' + parameter - } - - } - let filePath = global.env.PATH_TO_PROJECTS + path - respondWithFile(filePath, httpResponse) - } - break - case 'Storage': - { - let pathToFile = httpRequest.url.substring(9) - /* Unsavping # */ - for (let i = 0; i < 10; i++) { - pathToFile = pathToFile.replace('_HASHTAG_', '#') - } - respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) - } - break - case 'main.css': - { - sendStyleSheet('main.css') - } - break - case 'tutorial.css': - { - sendStyleSheet('tutorial.css') - } - break - case 'docs.css': - { - sendStyleSheet('docs.css') - } - break - case 'governance.css': - { - sendStyleSheet('governance.css') - } - break - case 'context-menu.css': - { - sendStyleSheet('context-menu.css') - } - break - case 'credits.css': - { - sendStyleSheet('credits.css') - } - break - case 'docs.css': - { - sendStyleSheet('docs.css') - } - break - case 'font-awasome.css': - { - sendStyleSheet('font-awasome.css') - } - break - case 'prism.css': - { - sendStyleSheet('prism.css') - } - break - case 'ExecuteTerminalCommand': - { - let command = unescape(requestParameters[2]) - executeTerminalCommand(command) - } - break - default: - { - homePage() - } - } - - function returnProjectFolderFileList(projectFolderName) { - { - let allLibraries = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) - let projectsCount = 0 - - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - - let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'UI' + '/' + projectFolderName - try { - let fs = require('fs') - fs.readdir(dirPath, onDirRead) - - function onDirRead(err, fileList) { - let updatedFileList = [] - if (err) { - /* - If we have a problem reading this folder we will assume that it is - because this project does not need this folder and that's it. - */ - // console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) - } else { - for (let i = 0; i < fileList.length; i++) { - let name = fileList[i] - updatedFileList.push([project, name]) - } - } - allLibraries = allLibraries.concat(updatedFileList) - projectsCount++ - if (projectsCount === projects.length) { - respondWithContent(JSON.stringify(allLibraries), httpResponse) - } - } - } catch (err) { - console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) - console.log('[ERROR] err.stack = ' + err.stack) - - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - } } - } - function executeTerminalCommand(command) { - const util = require('util'); - const exec = util.promisify(require('child_process').exec); - async function lsWithGrep() { - try { - const { stdout, stderr } = await exec(command); - console.log('stdout:', stdout); - console.log('stderr:', stderr); - } catch (err) { - console.error(err.stack); + function executeTerminalCommand(command) { + const util = require('util'); + const exec = util.promisify(require('child_process').exec); + async function lsWithGrep() { + try { + const { stdout, stderr } = await exec(command); + console.log('stdout:', stdout); + console.log('stderr:', stderr); + } catch (err) { + console.error(err.stack); + }; }; - }; - lsWithGrep(); - } - - function sendSchema(filePath, schemaType) { - let fs = require('fs') - try { - let folder = '' - switch (schemaType) { - case 'AppSchema': { - folder = 'App-Schema' - break - } - case 'DocsNodeSchema': { - folder = 'Docs-Nodes' - break - } - case 'DocsConceptSchema': { - folder = 'Docs-Concepts' - break - } - case 'DocsTopicSchema': { - folder = 'Docs-Topics' - break - } - case 'DocsTutorialSchema': { - folder = 'Docs-Tutorials' - break - } - case 'DocsReviewSchema': { - folder = 'Docs-Reviews' - break - } - case 'DocsBookSchema': { - folder = 'Docs-Books' - break - } - } - getAllFilesInDirectoryAndSubdirectories(filePath + folder, onFilesReady) - function onFilesReady(files) { - - let schemaArray = [] - for (let k = 0; k < files.length; k++) { - let name = files[k] - let nameSplitted = name.split(folder) - let fileName = nameSplitted[1] - for (let i = 0; i < 10; i++) { - fileName = fileName.replace('\\', '/') - } - let fileToRead = filePath + folder + fileName - - let fileContent = fs.readFileSync(fileToRead) - let schemaDocument - try { - schemaDocument = JSON.parse(fileContent) - } catch (err) { - console.log('[ERROR] httpInterface -> sendSchema -> Error Parsing JSON File: ' + fileToRead + ' .Error = ' + err.stack) - respondWithContent("[]", httpResponse) - return - } - schemaArray.push(schemaDocument) - } - let schema = JSON.stringify(schemaArray) - respondWithContent(schema, httpResponse) - } - } catch (err) { - if (err.message.indexOf('no such file or directory') < 0) { - console.log('Could not send Schema:', filePath, schemaType) - console.log(err.stack) - } - respondWithContent("[]", httpResponse) + lsWithGrep(); } - } - - function sendStyleSheet(fileName) { - let fs = require('fs') - try { - let filePath = global.env.PATH_TO_CLIENT + 'WebServer/css/' + fileName - fs.readFile(filePath, onFileRead) - - function onFileRead(err, file) { - try { - let fileContent = file.toString() - - fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - respondWithContent(fileContent, httpResponse, 'text/css') - } catch (err) { - console.log('[ERROR] httpInterface -> mainCSS -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - } - } - } catch (err) { - console.log(err) - } - } - - function homePage() { - if (requestParameters[1] === '') { + function sendSchema(filePath, schemaType) { let fs = require('fs') try { - let fileName = global.env.PATH_TO_CLIENT + 'WebServer/index.html' - fs.readFile(fileName, onFileRead) + let folder = '' + switch (schemaType) { + case 'AppSchema': { + folder = 'App-Schema' + break + } + case 'DocsNodeSchema': { + folder = 'Docs-Nodes' + break + } + case 'DocsConceptSchema': { + folder = 'Docs-Concepts' + break + } + case 'DocsTopicSchema': { + folder = 'Docs-Topics' + break + } + case 'DocsTutorialSchema': { + folder = 'Docs-Tutorials' + break + } + case 'DocsReviewSchema': { + folder = 'Docs-Reviews' + break + } + case 'DocsBookSchema': { + folder = 'Docs-Books' + break + } + } + getAllFilesInDirectoryAndSubdirectories(filePath + folder, onFilesReady) + function onFilesReady(files) { + + let schemaArray = [] + for (let k = 0; k < files.length; k++) { + let name = files[k] + let nameSplitted = name.split(folder) + let fileName = nameSplitted[1] + for (let i = 0; i < 10; i++) { + fileName = fileName.replace('\\', '/') + } + let fileToRead = filePath + folder + fileName + + let fileContent = fs.readFileSync(fileToRead) + let schemaDocument + try { + schemaDocument = JSON.parse(fileContent) + } catch (err) { + console.log('[ERROR] httpInterface -> sendSchema -> Error Parsing JSON File: ' + fileToRead + ' .Error = ' + err.stack) + respondWithContent("[]", httpResponse) + return + } + schemaArray.push(schemaDocument) + } + let schema = JSON.stringify(schemaArray) + respondWithContent(schema, httpResponse) + } + } catch (err) { + if (err.message.indexOf('no such file or directory') < 0) { + console.log('Could not send Schema:', filePath, schemaType) + console.log(err.stack) + } + respondWithContent("[]", httpResponse) + } + + } + + function sendStyleSheet(fileName) { + let fs = require('fs') + try { + let filePath = global.env.PATH_TO_CLIENT + 'WebServer/css/' + fileName + fs.readFile(filePath, onFileRead) function onFileRead(err, file) { try { let fileContent = file.toString() fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - respondWithContent(fileContent, httpResponse) + fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) + fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) + respondWithContent(fileContent, httpResponse, 'text/css') } catch (err) { - console.log('[ERROR] httpInterface -> homePage -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + console.log('[ERROR] httpInterface -> mainCSS -> File Not Found: ' + fileName + ' or Error = ' + err.stack) } } } catch (err) { console.log(err) } - } else { - respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1], httpResponse) } + + function defaultEndpoint() { + + if (requestParameters[1] === '') { + /* + When there is no endpoint specidied we will respond with this app's Home Page. + */ + let fs = require('fs') + + let fileName = global.env.PATH_TO_CLIENT + 'WebServer/index.html' + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + + let fileContent = file.toString() + + fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) + respondWithContent(fileContent, httpResponse) + } + + } else { + /* + When there is a parameter but it does not match any of the available endpoints, we + will serve the file with the same name at the UI folder. These are in general js files. + */ + respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1], httpResponse) + } + } + + } catch (err) { + console.log(err.stack) } } From 60657c79fd35f74fc3f77fb624036f3c4fc1c576 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 14:18:05 +0200 Subject: [PATCH 04/11] More Cleaning of HTTP Interface --- .vscode/Superalgos.code-workspace | 10 + Client/httpInterface.js | 234 +++++++----------- .../SA/Utilities/FilesAndDirectories.js | 54 ++++ Projects/ProjectsSchema.json | 6 + 4 files changed, 165 insertions(+), 139 deletions(-) create mode 100644 .vscode/Superalgos.code-workspace create mode 100644 Projects/Foundations/SA/Utilities/FilesAndDirectories.js diff --git a/.vscode/Superalgos.code-workspace b/.vscode/Superalgos.code-workspace new file mode 100644 index 000000000..97e2d4c8f --- /dev/null +++ b/.vscode/Superalgos.code-workspace @@ -0,0 +1,10 @@ +{ + "folders": [ + { + "path": "C:\\Superalgos" + } + ], + "settings": { + "search.maintainFileSearchCache": true + } +} \ No newline at end of file diff --git a/Client/httpInterface.js b/Client/httpInterface.js index 46c6c5c8b..f190b5e3d 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -43,9 +43,10 @@ exports.newHttpInterface = function newHttpInterface() { function onHttpRequest(httpRequest, httpResponse) { try { - let requestParameters = httpRequest.url.split('/') + let requestPathAndParameters = httpRequest.url.split('?') // Remove version information + let requestPath = requestPathAndParameters[0].split('/') - switch (requestParameters[1]) { + switch (requestPath[1]) { case 'WEB3': { @@ -214,10 +215,10 @@ exports.newHttpInterface = function newHttpInterface() { break case 'Webhook': { - switch (requestParameters[2]) { // switch by command + switch (requestPath[2]) { // switch by command case 'Fetch-Messages': { - let exchange = requestParameters[3] - let market = requestParameters[4] + let exchange = requestPath[3] + let market = requestPath[4] /* Some validations */ if (exchange === undefined) { @@ -252,9 +253,9 @@ exports.newHttpInterface = function newHttpInterface() { function processRequest(messageReceived) { let timestamp = (new Date()).valueOf() - let source = requestParameters[3] - let exchange = requestParameters[4] - let market = requestParameters[5] + let source = requestPath[3] + let exchange = requestPath[4] + let market = requestPath[5] /* Some validations */ if (source === undefined) { @@ -295,14 +296,14 @@ exports.newHttpInterface = function newHttpInterface() { break case 'Docs': { - switch (requestParameters[2]) { // switch by command + switch (requestPath[2]) { // switch by command case 'Save-Node-Schema': { getBody(processRequest) async function processRequest(body) { try { let docsSchema = JSON.parse(body) - let project = requestParameters[3] + let project = requestPath[3] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Nodes' if (checkAllSchmemaDocuments('Node', docsSchema, filePath) === true) { @@ -333,7 +334,7 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { try { let docsSchema = JSON.parse(body) - let project = requestParameters[3] + let project = requestPath[3] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Concepts' if (checkAllSchmemaDocuments('Concept', docsSchema, filePath) === true) { @@ -364,7 +365,7 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { try { let docsSchema = JSON.parse(body) - let project = requestParameters[3] + let project = requestPath[3] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Topics' if (checkAllSchmemaDocuments('Topic', docsSchema, filePath) === true) { @@ -395,7 +396,7 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { try { let docsSchema = JSON.parse(body) - let project = requestParameters[3] + let project = requestPath[3] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Tutorials' if (checkAllSchmemaDocuments('Tutorial', docsSchema, filePath) === true) { @@ -426,7 +427,7 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { try { let docsSchema = JSON.parse(body) - let project = requestParameters[3] + let project = requestPath[3] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Reviews' if (checkAllSchmemaDocuments('Review', docsSchema, filePath) === true) { @@ -457,7 +458,7 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { try { let docsSchema = JSON.parse(body) - let project = requestParameters[3] + let project = requestPath[3] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Books' if (checkAllSchmemaDocuments('Book', docsSchema, filePath) === true) { @@ -612,14 +613,14 @@ exports.newHttpInterface = function newHttpInterface() { break case 'App': { - switch (requestParameters[2]) { // switch by command + switch (requestPath[2]) { // switch by command case 'Contribute': { try { - let commitMessage = unescape(requestParameters[3]) - const username = unescape(requestParameters[4]) - const token = unescape(requestParameters[5]) - const currentBranch = unescape(requestParameters[6]) - const contributionsBranch = unescape(requestParameters[7]) + let commitMessage = unescape(requestPath[3]) + const username = unescape(requestPath[4]) + const token = unescape(requestPath[5]) + const currentBranch = unescape(requestPath[6]) + const contributionsBranch = unescape(requestPath[7]) let error /* Unsavping # */ @@ -762,7 +763,7 @@ exports.newHttpInterface = function newHttpInterface() { case 'Update': { try { - const currentBranch = unescape(requestParameters[3]) + const currentBranch = unescape(requestPath[3]) update() async function update() { @@ -831,7 +832,7 @@ exports.newHttpInterface = function newHttpInterface() { case 'Checkout': { try { - const currentBranch = unescape(requestParameters[3]) + const currentBranch = unescape(requestPath[3]) let error checkout() @@ -1091,18 +1092,18 @@ exports.newHttpInterface = function newHttpInterface() { break case 'Images': // This means the Images folder. { - let path = global.env.PATH_TO_CLIENT + 'WebServer/Images/' + requestParameters[2] + let path = global.env.PATH_TO_CLIENT + 'WebServer/Images/' + requestPath[2] - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] + if (requestPath[3] !== undefined) { + path = path + '/' + requestPath[3] } - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] + if (requestPath[4] !== undefined) { + path = path + '/' + requestPath[4] } - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] + if (requestPath[5] !== undefined) { + path = path + '/' + requestPath[5] } path = unescape(path) @@ -1112,18 +1113,18 @@ exports.newHttpInterface = function newHttpInterface() { break case 'Icons': // This means the Icons folder under Projects. { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Icons' + let path = global.env.PATH_TO_PROJECTS + '/' + requestPath[2] + '/Icons' - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] + if (requestPath[3] !== undefined) { + path = path + '/' + requestPath[3] } - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] + if (requestPath[4] !== undefined) { + path = path + '/' + requestPath[4] } - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] + if (requestPath[5] !== undefined) { + path = path + '/' + requestPath[5] } path = unescape(path) @@ -1133,18 +1134,18 @@ exports.newHttpInterface = function newHttpInterface() { break case 'GIFs': // This means the GIFs folder under Projects. { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/GIFs' + let path = global.env.PATH_TO_PROJECTS + '/' + requestPath[2] + '/GIFs' - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] + if (requestPath[3] !== undefined) { + path = path + '/' + requestPath[3] } - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] + if (requestPath[4] !== undefined) { + path = path + '/' + requestPath[4] } - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] + if (requestPath[5] !== undefined) { + path = path + '/' + requestPath[5] } path = unescape(path) @@ -1153,18 +1154,18 @@ exports.newHttpInterface = function newHttpInterface() { break case 'PNGs': // This means the PNGs folder under Projects. { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/PNGs' + let path = global.env.PATH_TO_PROJECTS + '/' + requestPath[2] + '/PNGs' - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] + if (requestPath[3] !== undefined) { + path = path + '/' + requestPath[3] } - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] + if (requestPath[4] !== undefined) { + path = path + '/' + requestPath[4] } - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] + if (requestPath[5] !== undefined) { + path = path + '/' + requestPath[5] } path = unescape(path) @@ -1178,66 +1179,66 @@ exports.newHttpInterface = function newHttpInterface() { break case 'WebServer': // This means the WebServer folder. { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestParameters[2], httpResponse) + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestPath[2], httpResponse) } break case 'externalScripts': // This means the WebServer folder. { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestParameters[2], httpResponse) + respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestPath[2], httpResponse) } break case 'Plotters': // This means the plotter folder, not to be confused with the Plotters script! { - let project = requestParameters[2] - let dataMine = requestParameters[3] - let codeName = requestParameters[4] - let moduleName = requestParameters[5] + let project = requestPath[2] + let dataMine = requestPath[3] + let codeName = requestPath[4] + let moduleName = requestPath[5] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'Bots-Plotters-Code' + '/' + dataMine + '/plotters/' + codeName + '/' + moduleName respondWithFile(filePath, httpResponse) } break case 'ChartLayers': { - respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1] + '/' + requestParameters[2], httpResponse) + respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1] + '/' + requestPath[2], httpResponse) } break case 'Files': { - respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestParameters[2], httpResponse) + respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestPath[2], httpResponse) } break case 'Fonts': { - respondWithFont(global.env.PATH_TO_FONTS + '/' + requestParameters[2], httpResponse) + respondWithFont(global.env.PATH_TO_FONTS + '/' + requestPath[2], httpResponse) } break case 'Schema': { - sendSchema(global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Schemas/', requestParameters[3]) + sendSchema(global.env.PATH_TO_PROJECTS + '/' + requestPath[2] + '/Schemas/', requestPath[3]) } break case 'DirContent': { - let folderPath = unescape(requestParameters[2]) - if (requestParameters[3] !== undefined) { - folderPath = folderPath + '/' + requestParameters[3] + let folderPath = unescape(requestPath[2]) + if (requestPath[3] !== undefined) { + folderPath = folderPath + '/' + requestPath[3] } - if (requestParameters[4] !== undefined) { - folderPath = folderPath + '/' + requestParameters[4] + if (requestPath[4] !== undefined) { + folderPath = folderPath + '/' + requestPath[4] } - if (requestParameters[5] !== undefined) { - folderPath = folderPath + '/' + requestParameters[5] + if (requestPath[5] !== undefined) { + folderPath = folderPath + '/' + requestPath[5] } let folder - if (requestParameters[2] === 'Root') { + if (requestPath[2] === 'Root') { folder = folderPath.replace('Root', '../Superalgos/') } else { folder = global.env.PATH_TO_PROJECTS + '/' + folderPath } - getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) + SA.projects.foundations.utilities.filesAndDirectories.getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) function onFilesReady(files) { respondWithContent(JSON.stringify(files), httpResponse) @@ -1246,7 +1247,7 @@ exports.newHttpInterface = function newHttpInterface() { break case 'IconNames': { - let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let projects = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS) let icons = [] let totalProjects = projects.length let projectCounter = 0 @@ -1256,7 +1257,7 @@ exports.newHttpInterface = function newHttpInterface() { const folder = global.env.PATH_TO_PROJECTS + '/' + project + '/Icons/' - getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) + SA.projects.foundations.utilities.filesAndDirectories.getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) function onFilesReady(files) { for (let j = 0; j < files.length; j++) { @@ -1281,8 +1282,8 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { try { - let project = unescape(requestParameters[2]) - let folder = unescape(requestParameters[3]) + let project = unescape(requestPath[2]) + let folder = unescape(requestPath[3]) let response = await SA.projects.foundations.utilities.plugins.getPluginFileNames( project, @@ -1312,9 +1313,9 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { try { - let project = unescape(requestParameters[2]) - let folder = unescape(requestParameters[3]) - let fileName = unescape(requestParameters[4]) + let project = unescape(requestPath[2]) + let folder = unescape(requestPath[3]) + let fileName = unescape(requestPath[4]) let response = await SA.projects.foundations.utilities.plugins.getPluginFileContent( project, @@ -1353,9 +1354,9 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { try { let plugin = JSON.parse(body) - let project = requestParameters[2] - let folder = requestParameters[3] - let fileName = requestParameters[4] + let project = requestPath[2] + let folder = requestPath[3] + let fileName = requestPath[4] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/' + folder let fileContent = JSON.stringify(plugin, undefined, 4) const fs = require('fs') @@ -1400,7 +1401,7 @@ exports.newHttpInterface = function newHttpInterface() { case 'ListWorkspaces': { let allWorkspaces = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let projects = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS) let projectsCount = 0 for (let i = 0; i < projects.length; i++) { @@ -1475,7 +1476,7 @@ exports.newHttpInterface = function newHttpInterface() { break case 'LoadMyWorkspace': { - let fileName = unescape(requestParameters[2]) + let fileName = unescape(requestPath[2]) let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' respondWithFile(filePath, httpResponse) } @@ -1487,7 +1488,7 @@ exports.newHttpInterface = function newHttpInterface() { async function processRequest(body) { let fileContent = body - let fileName = unescape(requestParameters[2]) + let fileName = unescape(requestPath[2]) let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' try { @@ -1544,7 +1545,7 @@ exports.newHttpInterface = function newHttpInterface() { { let fs = require('fs') let allFiles = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let projects = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS) let dirCount = 0 let totalDirs = 0 @@ -1552,7 +1553,7 @@ exports.newHttpInterface = function newHttpInterface() { let project = projects[i] let dirPath = project + '/UI/Spaces' - let spaces = getDirectories(global.env.PATH_TO_PROJECTS + '/' + dirPath) + let spaces = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS + '/' + dirPath) for (let j = 0; j < spaces.length; j++) { let space = spaces[j] @@ -1565,7 +1566,7 @@ exports.newHttpInterface = function newHttpInterface() { totalDirs++ fs.readdir(global.env.PATH_TO_PROJECTS + '/' + path, onDirRead) - let otherDirs = getDirectories(global.env.PATH_TO_PROJECTS + '/' + path) + let otherDirs = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS + '/' + path) for (let m = 0; m < otherDirs.length; m++) { let otherDir = otherDirs[m] readDirectory(path + '/' + otherDir) @@ -1612,8 +1613,8 @@ exports.newHttpInterface = function newHttpInterface() { { let path = '' for (let i = 2; i < 10; i++) { - if (requestParameters[i] !== undefined) { - let parameter = unescape(requestParameters[i]) + if (requestPath[i] !== undefined) { + let parameter = unescape(requestPath[i]) path = path + '/' + parameter } @@ -1679,7 +1680,7 @@ exports.newHttpInterface = function newHttpInterface() { break case 'ExecuteTerminalCommand': { - let command = unescape(requestParameters[2]) + let command = unescape(requestPath[2]) executeTerminalCommand(command) } break @@ -1719,7 +1720,7 @@ exports.newHttpInterface = function newHttpInterface() { function returnProjectFolderFileList(projectFolderName) { { let allLibraries = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) + let projects = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS) let projectsCount = 0 for (let i = 0; i < projects.length; i++) { @@ -1810,7 +1811,7 @@ exports.newHttpInterface = function newHttpInterface() { break } } - getAllFilesInDirectoryAndSubdirectories(filePath + folder, onFilesReady) + SA.projects.foundations.utilities.filesAndDirectories.getAllFilesInDirectoryAndSubdirectories(filePath + folder, onFilesReady) function onFilesReady(files) { let schemaArray = [] @@ -1872,7 +1873,7 @@ exports.newHttpInterface = function newHttpInterface() { function defaultEndpoint() { - if (requestParameters[1] === '') { + if (requestPath[1] === '') { /* When there is no endpoint specidied we will respond with this app's Home Page. */ @@ -1894,10 +1895,10 @@ exports.newHttpInterface = function newHttpInterface() { When there is a parameter but it does not match any of the available endpoints, we will serve the file with the same name at the UI folder. These are in general js files. */ - respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1], httpResponse) + respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1], httpResponse) } } - + } catch (err) { console.log(err.stack) } @@ -2021,49 +2022,4 @@ exports.newHttpInterface = function newHttpInterface() { console.log('[ERROR] httpInterface -> returnEmptyArray -> err.stack ' + err.stack) } } - - function getDirectories(path) { - try { - const fs = require('fs') - return fs.readdirSync(path).filter(function (file) { - return fs.statSync(path + '/' + file).isDirectory(); - }); - } catch (err) { - return [] - } - } - - function getAllFilesInDirectoryAndSubdirectories(dir, callback) { - const { promisify } = require('util'); - const { resolve } = require('path'); - const fs = require('fs'); - const readdir = promisify(fs.readdir); - const stat = promisify(fs.stat); - - getFiles(dir) - .then(files => { - let splittedDir = dir.split('/') - let lastFolder = splittedDir[splittedDir.length - 2] - let pathAndNames = [] - for (let i = 0; i < files.length; i++) { - let file = files[i] - let pathName = file.substring(file.indexOf(lastFolder) + lastFolder.length, file.length) - pathName = pathName.substring(1, pathName.length) - pathAndNames.push(pathName) - } - callback(pathAndNames) - }) - .catch(e => { - callback([]) - }); - - async function getFiles(dir) { - const subdirs = await readdir(dir); - const files = await Promise.all(subdirs.map(async (subdir) => { - const res = resolve(dir, subdir); - return (await stat(res)).isDirectory() ? getFiles(res) : res; - })); - return files.reduce((a, f) => a.concat(f), []); - } - } } diff --git a/Projects/Foundations/SA/Utilities/FilesAndDirectories.js b/Projects/Foundations/SA/Utilities/FilesAndDirectories.js new file mode 100644 index 000000000..2a21c0e76 --- /dev/null +++ b/Projects/Foundations/SA/Utilities/FilesAndDirectories.js @@ -0,0 +1,54 @@ +exports.newFoundationsUtilitiesFilesAndDirectories = function () { + + let thisObject = { + getDirectories: getDirectories, + getAllFilesInDirectoryAndSubdirectories: getAllFilesInDirectoryAndSubdirectories + } + + return thisObject + + function getDirectories(path) { + try { + const fs = require('fs') + return fs.readdirSync(path).filter(function (file) { + return fs.statSync(path + '/' + file).isDirectory(); + }); + } catch (err) { + return [] + } + } + + function getAllFilesInDirectoryAndSubdirectories(dir, callback) { + const { promisify } = require('util'); + const { resolve } = require('path'); + const fs = require('fs'); + const readdir = promisify(fs.readdir); + const stat = promisify(fs.stat); + + getFiles(dir) + .then(files => { + let splittedDir = dir.split('/') + let lastFolder = splittedDir[splittedDir.length - 2] + let pathAndNames = [] + for (let i = 0; i < files.length; i++) { + let file = files[i] + let pathName = file.substring(file.indexOf(lastFolder) + lastFolder.length, file.length) + pathName = pathName.substring(1, pathName.length) + pathAndNames.push(pathName) + } + callback(pathAndNames) + }) + .catch(e => { + callback([]) + }); + + async function getFiles(dir) { + const subdirs = await readdir(dir); + const files = await Promise.all(subdirs.map(async (subdir) => { + const res = resolve(dir, subdir); + return (await stat(res)).isDirectory() ? getFiles(res) : res; + })); + return files.reduce((a, f) => a.concat(f), []); + } + } +} \ No newline at end of file diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index f60a3f0f5..06d55c831 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -900,6 +900,12 @@ "propertyName": "miscellaneousFunctions", "functionName": "newFoundationsUtilitiesMiscellaneousFunctions", "fileName": "MiscellaneousFunctions.js" + }, + { + "name": "Files And Directories", + "propertyName": "filesAndDirectories", + "functionName": "newFoundationsUtilitiesFilesAndDirectories", + "fileName": "FilesAndDirectories.js" } ] }, From c343900615f62b69bbe057cdff49edad91fb0378 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 14:53:31 +0200 Subject: [PATCH 05/11] HTTP Responses --- Client/httpInterface.js | 341 ++++++------------ .../SA/Utilities/FilesAndDirectories.js | 4 +- .../Foundations/SA/Utilities/HttpResponses.js | 112 ++++++ Projects/ProjectsSchema.json | 6 + 4 files changed, 231 insertions(+), 232 deletions(-) create mode 100644 Projects/Foundations/SA/Utilities/HttpResponses.js diff --git a/Client/httpInterface.js b/Client/httpInterface.js index f190b5e3d..51c46bd34 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -65,7 +65,7 @@ exports.newHttpInterface = function newHttpInterface() { params.interface ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'createWalletAccount': { @@ -74,7 +74,7 @@ exports.newHttpInterface = function newHttpInterface() { params.entropy ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'getWalletBalances': { @@ -86,7 +86,7 @@ exports.newHttpInterface = function newHttpInterface() { params.walletDefinition ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'signData': { @@ -96,7 +96,7 @@ exports.newHttpInterface = function newHttpInterface() { params.data ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'recoverAddress': { @@ -105,7 +105,7 @@ exports.newHttpInterface = function newHttpInterface() { params.signature ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'mnemonicToPrivateKey': { @@ -114,11 +114,11 @@ exports.newHttpInterface = function newHttpInterface() { params.mnemonic ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } default: { - respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) } } } catch (err) { @@ -131,7 +131,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } } @@ -162,7 +162,7 @@ exports.newHttpInterface = function newHttpInterface() { if (ccxtExchange.has.fetchMarkets === true) { ccxtMarkets = await ccxtExchange.fetchMarkets() } - respondWithContent(JSON.stringify(ccxtMarkets), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(ccxtMarkets), httpResponse) return } case 'listExchanges': { @@ -194,7 +194,7 @@ exports.newHttpInterface = function newHttpInterface() { } } } - respondWithContent(JSON.stringify(exchanges), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(exchanges), httpResponse) return } } @@ -208,7 +208,7 @@ exports.newHttpInterface = function newHttpInterface() { result: 'Fail Because', message: err.message } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } } @@ -223,12 +223,12 @@ exports.newHttpInterface = function newHttpInterface() { /* Some validations */ if (exchange === undefined) { console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no Exchange received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } if (market === undefined) { console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no market received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } @@ -242,7 +242,7 @@ exports.newHttpInterface = function newHttpInterface() { console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Exchange-Market = ' + exchange + '-' + market) console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Messeges Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) - respondWithContent(JSON.stringify(webhookMessages), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(webhookMessages), httpResponse) webhookMessages = [] webhook.set(key, webhookMessages) @@ -260,17 +260,17 @@ exports.newHttpInterface = function newHttpInterface() { /* Some validations */ if (source === undefined) { console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Source received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } if (exchange === undefined) { console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Exchange received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } if (market === undefined) { console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no market received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } @@ -287,7 +287,7 @@ exports.newHttpInterface = function newHttpInterface() { console.log('[INFO] httpInterface -> Webhook -> New-Message -> Exchange-Market = ' + exchange + '-' + market) console.log('[INFO] httpInterface -> Webhook -> New-Message -> messageReceived = ' + messageReceived) console.log('[INFO] httpInterface -> Webhook -> New-Message -> Messeges waiting to be Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } break } @@ -307,9 +307,9 @@ exports.newHttpInterface = function newHttpInterface() { let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Nodes' if (checkAllSchmemaDocuments('Node', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) } } catch (err) { @@ -322,7 +322,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } break @@ -338,9 +338,9 @@ exports.newHttpInterface = function newHttpInterface() { let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Concepts' if (checkAllSchmemaDocuments('Concept', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) } } catch (err) { @@ -353,7 +353,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } break @@ -369,9 +369,9 @@ exports.newHttpInterface = function newHttpInterface() { let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Topics' if (checkAllSchmemaDocuments('Topic', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) } } catch (err) { @@ -384,7 +384,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } break @@ -400,9 +400,9 @@ exports.newHttpInterface = function newHttpInterface() { let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Tutorials' if (checkAllSchmemaDocuments('Tutorial', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) } } catch (err) { @@ -415,7 +415,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } break @@ -431,9 +431,9 @@ exports.newHttpInterface = function newHttpInterface() { let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Reviews' if (checkAllSchmemaDocuments('Review', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) } } catch (err) { @@ -446,7 +446,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } break @@ -462,9 +462,9 @@ exports.newHttpInterface = function newHttpInterface() { let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Books' if (checkAllSchmemaDocuments('Book', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) } } catch (err) { @@ -477,7 +477,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } break @@ -485,7 +485,7 @@ exports.newHttpInterface = function newHttpInterface() { } function checkAllSchmemaDocuments(category, docsSchema, filePath) { - const fs = require('fs') + const fs = SA.nodeModules.fs let noErrorsDuringSaving = true for (let i = 0; i < docsSchema.length; i++) { @@ -667,7 +667,7 @@ exports.newHttpInterface = function newHttpInterface() { respondWithDocsObject(docs, error) return } - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } } @@ -756,7 +756,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } break } @@ -779,7 +779,7 @@ exports.newHttpInterface = function newHttpInterface() { result: global.CUSTOM_OK_RESPONSE.result, message: result.message } - respondWithContent(JSON.stringify(customResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(customResponse), httpResponse) } else { let docs = { @@ -825,7 +825,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } break } @@ -846,7 +846,7 @@ exports.newHttpInterface = function newHttpInterface() { await doGit() if (error === undefined) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } else { let docs = { project: 'Foundations', @@ -887,7 +887,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } break } @@ -909,7 +909,7 @@ exports.newHttpInterface = function newHttpInterface() { result: global.CUSTOM_OK_RESPONSE.result, message: result } - respondWithContent(JSON.stringify(customResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(customResponse), httpResponse) } else { let docs = { project: 'Foundations', @@ -949,7 +949,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } break } @@ -986,7 +986,7 @@ exports.newHttpInterface = function newHttpInterface() { docs: docs } - respondWithContent(JSON.stringify(customResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(customResponse), httpResponse) } } @@ -1013,7 +1013,7 @@ exports.newHttpInterface = function newHttpInterface() { params.token ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'getGithubWatchers': { @@ -1024,7 +1024,7 @@ exports.newHttpInterface = function newHttpInterface() { params.token ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'getGithubForks': { @@ -1035,7 +1035,7 @@ exports.newHttpInterface = function newHttpInterface() { params.token ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'mergePullRequests': { @@ -1046,7 +1046,7 @@ exports.newHttpInterface = function newHttpInterface() { params.token ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } case 'payContributors': { @@ -1058,11 +1058,11 @@ exports.newHttpInterface = function newHttpInterface() { params.mnemonic ) - respondWithContent(JSON.stringify(serverResponse), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(serverResponse), httpResponse) return } default: { - respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) } } } catch (err) { @@ -1075,19 +1075,19 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } } break case 'LegacyPlotter.js': { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse) } break case 'PlotterPanel.js': { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', httpResponse) } break case 'Images': // This means the Images folder. @@ -1108,7 +1108,7 @@ exports.newHttpInterface = function newHttpInterface() { path = unescape(path) - respondWithImage(path, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithImage(path, httpResponse) } break case 'Icons': // This means the Icons folder under Projects. @@ -1129,7 +1129,7 @@ exports.newHttpInterface = function newHttpInterface() { path = unescape(path) - respondWithImage(path, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithImage(path, httpResponse) } break case 'GIFs': // This means the GIFs folder under Projects. @@ -1149,7 +1149,7 @@ exports.newHttpInterface = function newHttpInterface() { } path = unescape(path) - respondWithImage(path, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithImage(path, httpResponse) } break case 'PNGs': // This means the PNGs folder under Projects. @@ -1169,22 +1169,22 @@ exports.newHttpInterface = function newHttpInterface() { } path = unescape(path) - respondWithImage(path, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithImage(path, httpResponse) } break case 'favicon.ico': // This means the Scripts folder. { - respondWithImage(global.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithImage(global.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', httpResponse) } break case 'WebServer': // This means the WebServer folder. { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestPath[2], httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestPath[2], httpResponse) } break case 'externalScripts': // This means the WebServer folder. { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestPath[2], httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestPath[2], httpResponse) } break case 'Plotters': // This means the plotter folder, not to be confused with the Plotters script! @@ -1194,22 +1194,22 @@ exports.newHttpInterface = function newHttpInterface() { let codeName = requestPath[4] let moduleName = requestPath[5] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'Bots-Plotters-Code' + '/' + dataMine + '/plotters/' + codeName + '/' + moduleName - respondWithFile(filePath, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(filePath, httpResponse) } break case 'ChartLayers': { - respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1] + '/' + requestPath[2], httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1] + '/' + requestPath[2], httpResponse) } break case 'Files': { - respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestPath[2], httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestPath[2], httpResponse) } break case 'Fonts': { - respondWithFont(global.env.PATH_TO_FONTS + '/' + requestPath[2], httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFont(global.env.PATH_TO_FONTS + '/' + requestPath[2], httpResponse) } break case 'Schema': @@ -1241,7 +1241,7 @@ exports.newHttpInterface = function newHttpInterface() { SA.projects.foundations.utilities.filesAndDirectories.getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) function onFilesReady(files) { - respondWithContent(JSON.stringify(files), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(files), httpResponse) } } break @@ -1270,7 +1270,7 @@ exports.newHttpInterface = function newHttpInterface() { projectCounter++ if (projectCounter === totalProjects) { - respondWithContent(JSON.stringify(icons), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(icons), httpResponse) } } } @@ -1290,7 +1290,7 @@ exports.newHttpInterface = function newHttpInterface() { folder ) - respondWithContent(JSON.stringify(response), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(response), httpResponse) } catch (err) { console.log('[ERROR] httpInterface -> PluginFileNames -> Method call produced an error.') @@ -1302,7 +1302,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } } @@ -1326,11 +1326,11 @@ exports.newHttpInterface = function newHttpInterface() { result: 'Fail Because', message: err } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) return }) - respondWithContent(response, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(response, httpResponse) } catch (err) { console.log('[ERROR] httpInterface -> LoadPlugin -> Method call produced an error.') @@ -1342,7 +1342,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } } @@ -1359,9 +1359,9 @@ exports.newHttpInterface = function newHttpInterface() { let fileName = requestPath[4] let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/' + folder let fileContent = JSON.stringify(plugin, undefined, 4) - const fs = require('fs') + const fs = SA.nodeModules.fs fs.writeFileSync(filePath + '/' + fileName + '.json', fileContent) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } catch (err) { console.log('[ERROR] httpInterface -> SavePlugin -> Method call produced an error.') console.log('[ERROR] httpInterface -> SavePlugin -> err.stack = ' + err.stack) @@ -1372,14 +1372,14 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } } break case 'Workspace.js': { - let fs = require('fs') + let fs = SA.nodeModules.fs try { let filePath = global.env.PATH_TO_DEFAULT_WORKSPACE + '/Getting-Started-Tutorials.json' @@ -1390,10 +1390,10 @@ exports.newHttpInterface = function newHttpInterface() { function onFileRead(err, workspace) { if (err) { - respondWithContent(undefined, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(undefined, httpResponse) } else { let responseContent = 'function getWorkspace(){ return ' + workspace + '}' - respondWithContent(responseContent, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(responseContent, httpResponse) } } } @@ -1410,7 +1410,7 @@ exports.newHttpInterface = function newHttpInterface() { function readPluginWorkspaces() { let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/Workspaces' try { - let fs = require('fs') + let fs = SA.nodeModules.fs fs.readdir(dirPath, onDirRead) function onDirRead(err, fileList) { @@ -1437,7 +1437,7 @@ exports.newHttpInterface = function newHttpInterface() { } catch (err) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) console.log('[ERROR] err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } } @@ -1446,13 +1446,13 @@ exports.newHttpInterface = function newHttpInterface() { function readMyWorkspaces() { let dirPath = global.env.PATH_TO_MY_WORKSPACES try { - let fs = require('fs') + let fs = SA.nodeModules.fs fs.readdir(dirPath, onDirRead) function onDirRead(err, fileList) { if (err) { // This happens the first time you run the software. - respondWithContent(JSON.stringify(allWorkspaces), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(allWorkspaces), httpResponse) return } else { let updatedFileList = [] @@ -1461,14 +1461,14 @@ exports.newHttpInterface = function newHttpInterface() { updatedFileList.push(['', name]) } allWorkspaces = allWorkspaces.concat(updatedFileList) - respondWithContent(JSON.stringify(allWorkspaces), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(allWorkspaces), httpResponse) return } } } catch (err) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) console.log('[ERROR] err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } } @@ -1478,7 +1478,7 @@ exports.newHttpInterface = function newHttpInterface() { { let fileName = unescape(requestPath[2]) let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' - respondWithFile(filePath, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(filePath, httpResponse) } break case 'SaveWorkspace': @@ -1492,7 +1492,7 @@ exports.newHttpInterface = function newHttpInterface() { let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' try { - let fs = require('fs') + let fs = SA.nodeModules.fs let dir = global.env.PATH_TO_MY_WORKSPACES; /* Create Dir if it does not exist */ @@ -1511,9 +1511,9 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } else { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } } @@ -1525,7 +1525,7 @@ exports.newHttpInterface = function newHttpInterface() { message: err.message, stack: err.stack } - respondWithContent(JSON.stringify(error), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse) } } } @@ -1538,12 +1538,12 @@ exports.newHttpInterface = function newHttpInterface() { case 'ProjectsSchema': { let path = global.env.PATH_TO_PROJECTS + '/' + 'ProjectsSchema.json' - respondWithFile(path, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(path, httpResponse) } break case 'ListSpaceFiles': { - let fs = require('fs') + let fs = SA.nodeModules.fs let allFiles = [] let projects = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS) let dirCount = 0 @@ -1574,7 +1574,7 @@ exports.newHttpInterface = function newHttpInterface() { function onDirRead(err, fileList) { if (err) { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) } else { let updatedFileList = [] for (let k = 0; k < fileList.length; k++) { @@ -1585,14 +1585,14 @@ exports.newHttpInterface = function newHttpInterface() { allFiles = allFiles.concat(updatedFileList) dirCount++ if (dirCount === totalDirs) { - respondWithContent(JSON.stringify(allFiles), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(allFiles), httpResponse) } } } } catch (err) { console.log('[ERROR] Error reading a directory content. filePath = ' + path) console.log('[ERROR] err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } } @@ -1620,7 +1620,7 @@ exports.newHttpInterface = function newHttpInterface() { } let filePath = global.env.PATH_TO_PROJECTS + path - respondWithFile(filePath, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(filePath, httpResponse) } break case 'Storage': @@ -1630,7 +1630,7 @@ exports.newHttpInterface = function newHttpInterface() { for (let i = 0; i < 10; i++) { pathToFile = pathToFile.replace('_HASHTAG_', '#') } - respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) } break case 'main.css': @@ -1709,11 +1709,11 @@ exports.newHttpInterface = function newHttpInterface() { httpRequest.on('error', function (err) { console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) }) } catch (err) { console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) } } @@ -1728,7 +1728,7 @@ exports.newHttpInterface = function newHttpInterface() { let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'UI' + '/' + projectFolderName try { - let fs = require('fs') + let fs = SA.nodeModules.fs fs.readdir(dirPath, onDirRead) function onDirRead(err, fileList) { @@ -1748,14 +1748,14 @@ exports.newHttpInterface = function newHttpInterface() { allLibraries = allLibraries.concat(updatedFileList) projectsCount++ if (projectsCount === projects.length) { - respondWithContent(JSON.stringify(allLibraries), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(allLibraries), httpResponse) } } } catch (err) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) console.log('[ERROR] err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) return } } @@ -1778,7 +1778,7 @@ exports.newHttpInterface = function newHttpInterface() { } function sendSchema(filePath, schemaType) { - let fs = require('fs') + let fs = SA.nodeModules.fs try { let folder = '' switch (schemaType) { @@ -1830,26 +1830,26 @@ exports.newHttpInterface = function newHttpInterface() { schemaDocument = JSON.parse(fileContent) } catch (err) { console.log('[ERROR] httpInterface -> sendSchema -> Error Parsing JSON File: ' + fileToRead + ' .Error = ' + err.stack) - respondWithContent("[]", httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent("[]", httpResponse) return } schemaArray.push(schemaDocument) } let schema = JSON.stringify(schemaArray) - respondWithContent(schema, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(schema, httpResponse) } } catch (err) { if (err.message.indexOf('no such file or directory') < 0) { console.log('Could not send Schema:', filePath, schemaType) console.log(err.stack) } - respondWithContent("[]", httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent("[]", httpResponse) } } function sendStyleSheet(fileName) { - let fs = require('fs') + let fs = SA.nodeModules.fs try { let filePath = global.env.PATH_TO_CLIENT + 'WebServer/css/' + fileName fs.readFile(filePath, onFileRead) @@ -1861,7 +1861,7 @@ exports.newHttpInterface = function newHttpInterface() { fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - respondWithContent(fileContent, httpResponse, 'text/css') + SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse, 'text/css') } catch (err) { console.log('[ERROR] httpInterface -> mainCSS -> File Not Found: ' + fileName + ' or Error = ' + err.stack) } @@ -1877,7 +1877,7 @@ exports.newHttpInterface = function newHttpInterface() { /* When there is no endpoint specidied we will respond with this app's Home Page. */ - let fs = require('fs') + let fs = SA.nodeModules.fs let fileName = global.env.PATH_TO_CLIENT + 'WebServer/index.html' fs.readFile(fileName, onFileRead) @@ -1887,7 +1887,7 @@ exports.newHttpInterface = function newHttpInterface() { let fileContent = file.toString() fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - respondWithContent(fileContent, httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse) } } else { @@ -1895,7 +1895,7 @@ exports.newHttpInterface = function newHttpInterface() { When there is a parameter but it does not match any of the available endpoints, we will serve the file with the same name at the UI folder. These are in general js files. */ - respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1], httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1], httpResponse) } } @@ -1903,123 +1903,4 @@ exports.newHttpInterface = function newHttpInterface() { console.log(err.stack) } } - - function respondWithFile(fileName, httpResponse) { - let fs = require('fs') - if (fileName.indexOf('undefined') > 0) { - console.log('[WRN] httpInterface -> respondWithFile -> Received httpRequest for undefined file. ') - respondWithContent(undefined, httpResponse) - } else { - try { - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - if (!err) { - respondWithContent(file.toString(), httpResponse) - } else { - //console.log('File requested not found: ' + fileName) - respondWithContent(undefined, httpResponse) - } - } - } catch (err) { - returnEmptyArray() - } - } - } - - function respondWithContent(content, httpResponse, contentType) { - try { - httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. - httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. - httpResponse.setHeader('Expires', '0') // Proxies. - httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. - - if (content !== undefined) { - if (contentType !== undefined) { - httpResponse.writeHead(200, { 'Content-Type': contentType }) - } else { - httpResponse.writeHead(200, { 'Content-Type': 'text/html' }) - } - httpResponse.write(content) - } else { - httpResponse.writeHead(404, { 'Content-Type': 'text/html' }) - httpResponse.write('The specified key does not exist.') - } - httpResponse.end('\n') - } catch (err) { - returnEmptyArray(httpResponse) - } - } - - function respondWithImage(fileName, httpResponse) { - let fs = require('fs') - try { - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - if (err) { - console.log('[ERROR] httpInterface -> respondWithImage -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - return - } - try { - httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. - httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. - httpResponse.setHeader('Expires', '0') // Proxies. - httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. - - httpResponse.writeHead(200, { 'Content-Type': 'image/png' }) - httpResponse.end(file, 'binary') - } catch (err) { - console.log('[ERROR] httpInterface -> respondWithImage -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - } - } - } catch (err) { - console.log('[ERROR] httpInterface -> respondWithImage -> err = ' + err.stack) - } - } - - function respondWithFont(fileName, httpResponse) { - let fs = require('fs') - try { - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - try { - if (err) { - console.log('[ERROR] httpInterface -> respondWithBinary -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - return - } - httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. - httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. - httpResponse.setHeader('Expires', '0') // Proxies. - httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. - - if (fileName.indexOf('2') < 0) { - httpResponse.writeHead(200, { 'Content-Type': 'font/woff' }) - } else { - httpResponse.writeHead(200, { 'Content-Type': 'font/woff2' }) - } - httpResponse.end(file, 'binary') - } catch (err) { - console.log('[ERROR] httpInterface -> respondWithBinary -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - } - } - } catch (err) { - console.log('[ERROR] httpInterface -> respondWithBinary -> err = ' + err.stack) - } - } - - function returnEmptyArray(httpResponse) { - try { - httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. - httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. - httpResponse.setHeader('Expires', '0') // Proxies. - - httpResponse.writeHead(200, { 'Content-Type': 'text/html' }) - httpResponse.write('[]') - httpResponse.end('\n') - } catch (err) { - console.log('[ERROR] httpInterface -> returnEmptyArray -> err.stack ' + err.stack) - } - } } diff --git a/Projects/Foundations/SA/Utilities/FilesAndDirectories.js b/Projects/Foundations/SA/Utilities/FilesAndDirectories.js index 2a21c0e76..2f7bc485b 100644 --- a/Projects/Foundations/SA/Utilities/FilesAndDirectories.js +++ b/Projects/Foundations/SA/Utilities/FilesAndDirectories.js @@ -9,7 +9,7 @@ exports.newFoundationsUtilitiesFilesAndDirectories = function () { function getDirectories(path) { try { - const fs = require('fs') + const fs = SA.nodeModules.fs return fs.readdirSync(path).filter(function (file) { return fs.statSync(path + '/' + file).isDirectory(); }); @@ -21,7 +21,7 @@ exports.newFoundationsUtilitiesFilesAndDirectories = function () { function getAllFilesInDirectoryAndSubdirectories(dir, callback) { const { promisify } = require('util'); const { resolve } = require('path'); - const fs = require('fs'); + const fs = SA.nodeModules.fs; const readdir = promisify(fs.readdir); const stat = promisify(fs.stat); diff --git a/Projects/Foundations/SA/Utilities/HttpResponses.js b/Projects/Foundations/SA/Utilities/HttpResponses.js new file mode 100644 index 000000000..4500bd296 --- /dev/null +++ b/Projects/Foundations/SA/Utilities/HttpResponses.js @@ -0,0 +1,112 @@ +exports.newFoundationsUtilitiesHttpResponses = function () { + + let thisObject = { + respondWithFile: respondWithFile, + respondWithContent: respondWithContent, + respondWithImage: respondWithImage, + respondWithFont: respondWithFont, + respondWithEmptyArray: respondWithEmptyArray + } + + return thisObject + + function respondWithFile(fileName, httpResponse) { + let fs = SA.nodeModules.fs + if (fileName.indexOf('undefined') > 0) { + console.log('[WRN] respondWithFile -> Received httpRequest for undefined file. ') + respondWithContent(undefined, httpResponse) + } else { + try { + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + if (!err) { + respondWithContent(file.toString(), httpResponse) + } else { + //console.log('File requested not found: ' + fileName) + respondWithContent(undefined, httpResponse) + } + } + } catch (err) { + respondWithEmptyArray() + } + } + } + + function respondWithContent(content, httpResponse, contentType) { + try { + httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. + httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. + httpResponse.setHeader('Expires', '0') // Proxies. + httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. + + if (content !== undefined) { + if (contentType !== undefined) { + httpResponse.writeHead(200, { 'Content-Type': contentType }) + } else { + httpResponse.writeHead(200, { 'Content-Type': 'text/html' }) + } + httpResponse.write(content) + } else { + httpResponse.writeHead(404, { 'Content-Type': 'text/html' }) + httpResponse.write('The specified key does not exist.') + } + httpResponse.end('\n') + } catch (err) { + respondWithEmptyArray(httpResponse) + } + } + + function respondWithImage(fileName, httpResponse) { + let fs = SA.nodeModules.fs + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + if (err) { + console.log('[ERROR] respondWithImage -> Image Not Found: ' + fileName + ' or Error = ' + err.stack) + return + } + + httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. + httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. + httpResponse.setHeader('Expires', '0') // Proxies. + httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. + + httpResponse.writeHead(200, { 'Content-Type': 'image/png' }) + httpResponse.end(file, 'binary') + } + } + + function respondWithFont(fileName, httpResponse) { + let fs = SA.nodeModules.fs + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + if (err) { + console.log('[ERROR] respondWithFont -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + return + } + httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. + httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. + httpResponse.setHeader('Expires', '0') // Proxies. + httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. + + if (fileName.indexOf('2') < 0) { + httpResponse.writeHead(200, { 'Content-Type': 'font/woff' }) + } else { + httpResponse.writeHead(200, { 'Content-Type': 'font/woff2' }) + } + httpResponse.end(file, 'binary') + } + } + + function respondWithEmptyArray(httpResponse) { + httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. + httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. + httpResponse.setHeader('Expires', '0') // Proxies. + + httpResponse.writeHead(200, { 'Content-Type': 'text/html' }) + httpResponse.write('[]') + httpResponse.end('\n') + } +} \ No newline at end of file diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 06d55c831..363b02a3b 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -906,6 +906,12 @@ "propertyName": "filesAndDirectories", "functionName": "newFoundationsUtilitiesFilesAndDirectories", "fileName": "FilesAndDirectories.js" + }, + { + "name": "Http Responses", + "propertyName": "httpResponses", + "functionName": "newFoundationsUtilitiesHttpResponses", + "fileName": "HttpResponses.js" } ] }, From 0489e2e047806ec8630f054ef0c9523a1bf453a8 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 15:40:24 +0200 Subject: [PATCH 06/11] http requests --- Client/httpInterface.js | 51 +++++-------------- .../Foundations/SA/Utilities/HttpRequests.js | 35 +++++++++++++ Projects/ProjectsSchema.json | 6 +++ 3 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 Projects/Foundations/SA/Utilities/HttpRequests.js diff --git a/Client/httpInterface.js b/Client/httpInterface.js index 51c46bd34..dffa7dba9 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -50,7 +50,7 @@ exports.newHttpInterface = function newHttpInterface() { case 'WEB3': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -138,7 +138,7 @@ exports.newHttpInterface = function newHttpInterface() { break case 'CCXT': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -249,7 +249,7 @@ exports.newHttpInterface = function newHttpInterface() { break } case 'New-Message': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) function processRequest(messageReceived) { let timestamp = (new Date()).valueOf() @@ -298,7 +298,7 @@ exports.newHttpInterface = function newHttpInterface() { { switch (requestPath[2]) { // switch by command case 'Save-Node-Schema': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -329,7 +329,7 @@ exports.newHttpInterface = function newHttpInterface() { } case 'Save-Concept-Schema': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -360,7 +360,7 @@ exports.newHttpInterface = function newHttpInterface() { } case 'Save-Topic-Schema': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -391,7 +391,7 @@ exports.newHttpInterface = function newHttpInterface() { } case 'Save-Tutorial-Schema': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -422,7 +422,7 @@ exports.newHttpInterface = function newHttpInterface() { } case 'Save-Review-Schema': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -453,7 +453,7 @@ exports.newHttpInterface = function newHttpInterface() { } case 'Save-Book-Schema': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -998,7 +998,7 @@ exports.newHttpInterface = function newHttpInterface() { related to the Governance System are implemented here and routed to the backend Servers that can process them. */ - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -1349,7 +1349,7 @@ exports.newHttpInterface = function newHttpInterface() { break case 'SavePlugin': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { try { @@ -1483,7 +1483,7 @@ exports.newHttpInterface = function newHttpInterface() { break case 'SaveWorkspace': { - getBody(processRequest) + SA.projects.foundations.utilities.httpRequests.getRequestBody(httpRequest, processRequest) async function processRequest(body) { @@ -1690,33 +1690,6 @@ exports.newHttpInterface = function newHttpInterface() { } } - function getBody(callback) { // Gets the de body from a POST httpRequest to the web server - try { - - let body = '' - - httpRequest.on('data', function (data) { - body += data - // Too much POST data - //if (body.length > 1e6) { - // httpRequest.connection.destroy() - //} - }) - - httpRequest.on('end', function () { - callback(body) - }) - - httpRequest.on('error', function (err) { - console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) - SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - }) - } catch (err) { - console.log('[ERROR] httpInterface -> err.stack = ' + err.stack) - SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } - } - function returnProjectFolderFileList(projectFolderName) { { let allLibraries = [] diff --git a/Projects/Foundations/SA/Utilities/HttpRequests.js b/Projects/Foundations/SA/Utilities/HttpRequests.js new file mode 100644 index 000000000..4397491a6 --- /dev/null +++ b/Projects/Foundations/SA/Utilities/HttpRequests.js @@ -0,0 +1,35 @@ +exports.newFoundationsUtilitiesHttpRequests = function () { + + let thisObject = { + getRequestBody: getRequestBody + } + + return thisObject + + function getRequestBody(httpRequest, callback) { // Gets the de body from a POST httpRequest to the web server + try { + + let body = '' + + httpRequest.on('data', function (data) { + body += data + // Too much POST data + //if (body.length > 1e6) { + // httpRequest.connection.destroy() + //} + }) + + httpRequest.on('end', function () { + callback(body) + }) + + httpRequest.on('error', function (err) { + console.log('[ERROR] getBody -> err.stack = ' + err.stack) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + }) + } catch (err) { + console.log('[ERROR] getBody -> err.stack = ' + err.stack) + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + } + } +} \ No newline at end of file diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 363b02a3b..2b8ea0bb8 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -912,6 +912,12 @@ "propertyName": "httpResponses", "functionName": "newFoundationsUtilitiesHttpResponses", "fileName": "HttpResponses.js" + }, + { + "name": "Http Requests", + "propertyName": "httpRequests", + "functionName": "newFoundationsUtilitiesHttpRequests", + "fileName": "HttpRequests.js" } ] }, From 74f1c7d27bd2ca83ef58cdbfa02915a0b0069747 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 16:18:02 +0200 Subject: [PATCH 07/11] Stylesheets done --- Client/httpInterface.js | 229 +++++++----------- .../Foundations/SA/Utilities/HttpResponses.js | 19 +- 2 files changed, 104 insertions(+), 144 deletions(-) diff --git a/Client/httpInterface.js b/Client/httpInterface.js index dffa7dba9..216ef77a7 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -1215,6 +1215,76 @@ exports.newHttpInterface = function newHttpInterface() { case 'Schema': { sendSchema(global.env.PATH_TO_PROJECTS + '/' + requestPath[2] + '/Schemas/', requestPath[3]) + + function sendSchema(filePath, schemaType) { + let fs = SA.nodeModules.fs + try { + let folder = '' + switch (schemaType) { + case 'AppSchema': { + folder = 'App-Schema' + break + } + case 'DocsNodeSchema': { + folder = 'Docs-Nodes' + break + } + case 'DocsConceptSchema': { + folder = 'Docs-Concepts' + break + } + case 'DocsTopicSchema': { + folder = 'Docs-Topics' + break + } + case 'DocsTutorialSchema': { + folder = 'Docs-Tutorials' + break + } + case 'DocsReviewSchema': { + folder = 'Docs-Reviews' + break + } + case 'DocsBookSchema': { + folder = 'Docs-Books' + break + } + } + SA.projects.foundations.utilities.filesAndDirectories.getAllFilesInDirectoryAndSubdirectories(filePath + folder, onFilesReady) + function onFilesReady(files) { + + let schemaArray = [] + for (let k = 0; k < files.length; k++) { + let name = files[k] + let nameSplitted = name.split(folder) + let fileName = nameSplitted[1] + for (let i = 0; i < 10; i++) { + fileName = fileName.replace('\\', '/') + } + let fileToRead = filePath + folder + fileName + + let fileContent = fs.readFileSync(fileToRead) + let schemaDocument + try { + schemaDocument = JSON.parse(fileContent) + } catch (err) { + console.log('[ERROR] sendSchema -> Error Parsing JSON File: ' + fileToRead + ' .Error = ' + err.stack) + SA.projects.foundations.utilities.httpResponses.respondWithContent("[]", httpResponse) + return + } + schemaArray.push(schemaDocument) + } + let schema = JSON.stringify(schemaArray) + SA.projects.foundations.utilities.httpResponses.respondWithContent(schema, httpResponse) + } + } catch (err) { + if (err.message.indexOf('no such file or directory') < 0) { + console.log('Could not send Schema:', filePath, schemaType) + console.log(err.stack) + } + SA.projects.foundations.utilities.httpResponses.respondWithContent("[]", httpResponse) + } + } } break case 'DirContent': @@ -1633,51 +1703,6 @@ exports.newHttpInterface = function newHttpInterface() { SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) } break - case 'main.css': - { - sendStyleSheet('main.css') - } - break - case 'tutorial.css': - { - sendStyleSheet('tutorial.css') - } - break - case 'docs.css': - { - sendStyleSheet('docs.css') - } - break - case 'governance.css': - { - sendStyleSheet('governance.css') - } - break - case 'context-menu.css': - { - sendStyleSheet('context-menu.css') - } - break - case 'credits.css': - { - sendStyleSheet('credits.css') - } - break - case 'docs.css': - { - sendStyleSheet('docs.css') - } - break - case 'font-awasome.css': - { - sendStyleSheet('font-awasome.css') - } - break - case 'prism.css': - { - sendStyleSheet('prism.css') - } - break case 'ExecuteTerminalCommand': { let command = unescape(requestPath[2]) @@ -1750,100 +1775,6 @@ exports.newHttpInterface = function newHttpInterface() { lsWithGrep(); } - function sendSchema(filePath, schemaType) { - let fs = SA.nodeModules.fs - try { - let folder = '' - switch (schemaType) { - case 'AppSchema': { - folder = 'App-Schema' - break - } - case 'DocsNodeSchema': { - folder = 'Docs-Nodes' - break - } - case 'DocsConceptSchema': { - folder = 'Docs-Concepts' - break - } - case 'DocsTopicSchema': { - folder = 'Docs-Topics' - break - } - case 'DocsTutorialSchema': { - folder = 'Docs-Tutorials' - break - } - case 'DocsReviewSchema': { - folder = 'Docs-Reviews' - break - } - case 'DocsBookSchema': { - folder = 'Docs-Books' - break - } - } - SA.projects.foundations.utilities.filesAndDirectories.getAllFilesInDirectoryAndSubdirectories(filePath + folder, onFilesReady) - function onFilesReady(files) { - - let schemaArray = [] - for (let k = 0; k < files.length; k++) { - let name = files[k] - let nameSplitted = name.split(folder) - let fileName = nameSplitted[1] - for (let i = 0; i < 10; i++) { - fileName = fileName.replace('\\', '/') - } - let fileToRead = filePath + folder + fileName - - let fileContent = fs.readFileSync(fileToRead) - let schemaDocument - try { - schemaDocument = JSON.parse(fileContent) - } catch (err) { - console.log('[ERROR] httpInterface -> sendSchema -> Error Parsing JSON File: ' + fileToRead + ' .Error = ' + err.stack) - SA.projects.foundations.utilities.httpResponses.respondWithContent("[]", httpResponse) - return - } - schemaArray.push(schemaDocument) - } - let schema = JSON.stringify(schemaArray) - SA.projects.foundations.utilities.httpResponses.respondWithContent(schema, httpResponse) - } - } catch (err) { - if (err.message.indexOf('no such file or directory') < 0) { - console.log('Could not send Schema:', filePath, schemaType) - console.log(err.stack) - } - SA.projects.foundations.utilities.httpResponses.respondWithContent("[]", httpResponse) - } - - } - - function sendStyleSheet(fileName) { - let fs = SA.nodeModules.fs - try { - let filePath = global.env.PATH_TO_CLIENT + 'WebServer/css/' + fileName - fs.readFile(filePath, onFileRead) - - function onFileRead(err, file) { - try { - let fileContent = file.toString() - - fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) - SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse, 'text/css') - } catch (err) { - console.log('[ERROR] httpInterface -> mainCSS -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - } - } - } catch (err) { - console.log(err) - } - } - function defaultEndpoint() { if (requestPath[1] === '') { @@ -1859,16 +1790,28 @@ exports.newHttpInterface = function newHttpInterface() { let fileContent = file.toString() - fileContent = fileContent.replace('CLIENT_HTTP_INTERFACE_PORT', global.env.CLIENT_HTTP_INTERFACE_PORT) SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse) } - } else { /* When there is a parameter but it does not match any of the available endpoints, we - will serve the file with the same name at the UI folder. These are in general js files. + will serve the file with the same name at at a location that depends on it's file extention. */ - SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1], httpResponse) + let completeFileName = requestPath[1] + let fileExtension = completeFileName.split('.')[1] + let path + + switch (fileExtension) { + case 'js': { + path = global.env.PATH_TO_UI + '/' + completeFileName + SA.projects.foundations.utilities.httpResponses.respondWithFile(path, httpResponse) + break + } + case 'css': { + SA.projects.foundations.utilities.httpResponses.respondWithStyleSheet(httpResponse, global.env.PATH_TO_CLIENT, completeFileName) + break + } + } } } diff --git a/Projects/Foundations/SA/Utilities/HttpResponses.js b/Projects/Foundations/SA/Utilities/HttpResponses.js index 4500bd296..24ddcc930 100644 --- a/Projects/Foundations/SA/Utilities/HttpResponses.js +++ b/Projects/Foundations/SA/Utilities/HttpResponses.js @@ -5,7 +5,8 @@ exports.newFoundationsUtilitiesHttpResponses = function () { respondWithContent: respondWithContent, respondWithImage: respondWithImage, respondWithFont: respondWithFont, - respondWithEmptyArray: respondWithEmptyArray + respondWithEmptyArray: respondWithEmptyArray, + respondWithStyleSheet: respondWithStyleSheet } return thisObject @@ -109,4 +110,20 @@ exports.newFoundationsUtilitiesHttpResponses = function () { httpResponse.write('[]') httpResponse.end('\n') } + + function respondWithStyleSheet(httpResponse, path, fileName) { + let fs = SA.nodeModules.fs + let filePath = path + 'WebServer/css/' + fileName + fs.readFile(filePath, onFileRead) + + function onFileRead(err, file) { + try { + let fileContent = file.toString() + + SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse, 'text/css') + } catch (err) { + console.log('[ERROR] respondWithStyleSheet -> File Not Found: ' + fileName + ' or Error = ' + err.stack) + } + } + } } \ No newline at end of file From 877ac25e4d61d954658eb6a931fd2da0c00ba1ed Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 16:36:08 +0200 Subject: [PATCH 08/11] More Mantainance work --- Client/httpInterface.js | 88 +++---------------- .../Foundations/SA/Utilities/HttpResponses.js | 47 +++++++++- 2 files changed, 57 insertions(+), 78 deletions(-) diff --git a/Client/httpInterface.js b/Client/httpInterface.js index 216ef77a7..e2a02ca4e 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -1600,11 +1600,6 @@ exports.newHttpInterface = function newHttpInterface() { } } break - case 'ListFunctionLibraries': - { - returnProjectFolderFileList('Function-Libraries') - } - break case 'ProjectsSchema': { let path = global.env.PATH_TO_PROJECTS + '/' + 'ProjectsSchema.json' @@ -1669,14 +1664,19 @@ exports.newHttpInterface = function newHttpInterface() { } } break + case 'ListFunctionLibraries': + { + SA.projects.foundations.utilities.httpResponses.respondWithProjectFolderFileList(httpResponse, 'Function-Libraries', 'UI') + } + break case 'ListUtilitiesFiles': { - returnProjectFolderFileList('Utilities') + SA.projects.foundations.utilities.httpResponses.respondWithProjectFolderFileList(httpResponse, 'Utilities', 'UI') } break case 'ListGlobalFiles': { - returnProjectFolderFileList('Globals') + SA.projects.foundations.utilities.httpResponses.respondWithProjectFolderFileList(httpResponse, 'Globals', 'UI') } break case 'Projects': @@ -1703,79 +1703,13 @@ exports.newHttpInterface = function newHttpInterface() { SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) } break - case 'ExecuteTerminalCommand': - { - let command = unescape(requestPath[2]) - executeTerminalCommand(command) - } - break default: { - defaultEndpoint() + noEndpointReceived() } } - function returnProjectFolderFileList(projectFolderName) { - { - let allLibraries = [] - let projects = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS) - let projectsCount = 0 - - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - - let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'UI' + '/' + projectFolderName - try { - let fs = SA.nodeModules.fs - fs.readdir(dirPath, onDirRead) - - function onDirRead(err, fileList) { - let updatedFileList = [] - if (err) { - /* - If we have a problem reading this folder we will assume that it is - because this project does not need this folder and that's it. - */ - // console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) - } else { - for (let i = 0; i < fileList.length; i++) { - let name = fileList[i] - updatedFileList.push([project, name]) - } - } - allLibraries = allLibraries.concat(updatedFileList) - projectsCount++ - if (projectsCount === projects.length) { - SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(allLibraries), httpResponse) - } - } - } catch (err) { - console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) - console.log('[ERROR] err.stack = ' + err.stack) - - SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - } - } - } - - function executeTerminalCommand(command) { - const util = require('util'); - const exec = util.promisify(require('child_process').exec); - async function lsWithGrep() { - try { - const { stdout, stderr } = await exec(command); - console.log('stdout:', stdout); - console.log('stderr:', stderr); - } catch (err) { - console.error(err.stack); - }; - }; - lsWithGrep(); - } - - function defaultEndpoint() { + function noEndpointReceived() { if (requestPath[1] === '') { /* @@ -1797,9 +1731,9 @@ exports.newHttpInterface = function newHttpInterface() { When there is a parameter but it does not match any of the available endpoints, we will serve the file with the same name at at a location that depends on it's file extention. */ - let completeFileName = requestPath[1] + let completeFileName = requestPath[1] let fileExtension = completeFileName.split('.')[1] - let path + let path switch (fileExtension) { case 'js': { diff --git a/Projects/Foundations/SA/Utilities/HttpResponses.js b/Projects/Foundations/SA/Utilities/HttpResponses.js index 24ddcc930..96cb2a44d 100644 --- a/Projects/Foundations/SA/Utilities/HttpResponses.js +++ b/Projects/Foundations/SA/Utilities/HttpResponses.js @@ -6,7 +6,8 @@ exports.newFoundationsUtilitiesHttpResponses = function () { respondWithImage: respondWithImage, respondWithFont: respondWithFont, respondWithEmptyArray: respondWithEmptyArray, - respondWithStyleSheet: respondWithStyleSheet + respondWithStyleSheet: respondWithStyleSheet, + respondWithProjectFolderFileList: respondWithProjectFolderFileList } return thisObject @@ -126,4 +127,48 @@ exports.newFoundationsUtilitiesHttpResponses = function () { } } } + + function respondWithProjectFolderFileList(httpResponse, projectFolderName, rootObjectName) { + { + let allLibraries = [] + let projects = SA.projects.foundations.utilities.filesAndDirectories.getDirectories(global.env.PATH_TO_PROJECTS) + let projectsCount = 0 + + for (let i = 0; i < projects.length; i++) { + let project = projects[i] + + let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + rootObjectName + '/' + projectFolderName + try { + let fs = SA.nodeModules.fs + fs.readdir(dirPath, onDirRead) + + function onDirRead(err, fileList) { + let updatedFileList = [] + if (err) { + /* + If we have a problem reading this folder we will assume that it is + because this project does not need this folder and that's it. + */ + } else { + for (let i = 0; i < fileList.length; i++) { + let name = fileList[i] + updatedFileList.push([project, name]) + } + } + allLibraries = allLibraries.concat(updatedFileList) + projectsCount++ + if (projectsCount === projects.length) { + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(allLibraries), httpResponse) + } + } + } catch (err) { + console.log('[ERROR] returnProjectFolderFileList -> Error reading a directory content. filePath = ' + dirPath) + console.log('[ERROR] returnProjectFolderFileList -> err.stack = ' + err.stack) + + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) + return + } + } + } + } } \ No newline at end of file From bce399dc6e84bc86f7e107e962092f48e5cee91d Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 16:44:36 +0200 Subject: [PATCH 09/11] more cleaning --- Client/client.js | 5 ++++- Client/httpInterface.js | 17 ++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Client/client.js b/Client/client.js index 74fdb60c2..5adf10a82 100644 --- a/Client/client.js +++ b/Client/client.js @@ -97,7 +97,10 @@ try { */ SA.nodeModules = { fs: require('fs'), - nodeFetch: require('node-fetch') + nodeFetch: require('node-fetch'), + open: require('open'), + http: require('http'), + ccxt: require('ccxt') } /* Setting up servers running inside this Client. diff --git a/Client/httpInterface.js b/Client/httpInterface.js index e2a02ca4e..89b98454e 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -13,9 +13,6 @@ exports.newHttpInterface = function newHttpInterface() { run: run } - const OPEN_NODE_MODULE = require('open') - const HTTP_NODE_MODULE = require('http') - let webhook = new Map() return thisObject @@ -32,12 +29,12 @@ exports.newHttpInterface = function newHttpInterface() { /* We will create an HTTP Server and leave it running forever. */ - HTTP_NODE_MODULE.createServer(onHttpRequest).listen(global.env.CLIENT_HTTP_INTERFACE_PORT) + SA.nodeModules.http.createServer(onHttpRequest).listen(global.env.CLIENT_HTTP_INTERFACE_PORT) /* Starting the browser now is optional */ if (process.argv.includes("noBrowser")) { //Running Client only with no UI. } else { - OPEN_NODE_MODULE('http://localhost:' + global.env.CLIENT_HTTP_INTERFACE_PORT) + SA.nodeModules.open('http://localhost:' + global.env.CLIENT_HTTP_INTERFACE_PORT) } } @@ -144,12 +141,10 @@ exports.newHttpInterface = function newHttpInterface() { try { let params = JSON.parse(body) - const ccxt = require('ccxt') - switch (params.method) { case 'fetchMarkets': { - const exchangeClass = ccxt[params.exchangeId] + const exchangeClass = SA.nodeModules.ccxt[params.exchangeId] const exchangeConstructorParams = { 'timeout': 30000, 'enableRateLimit': true, @@ -167,10 +162,10 @@ exports.newHttpInterface = function newHttpInterface() { } case 'listExchanges': { let exchanges = [] - for (let i = 0; i < ccxt.exchanges.length; i++) { - let exchangeId = ccxt.exchanges[i] + for (let i = 0; i < SA.nodeModules.ccxt.exchanges.length; i++) { + let exchangeId = SA.nodeModules.ccxt.exchanges[i] - const exchangeClass = ccxt[exchangeId] + const exchangeClass = SA.nodeModules.ccxt[exchangeId] const exchangeConstructorParams = { 'timeout': 30000, 'enableRateLimit': true, From ab1b96d9d104a92e802937b9d206c7d8821d621e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 17:03:43 +0200 Subject: [PATCH 10/11] Foundations Project: Client Code Mantainance @ HTTP Interface --- Client/client.js | 2 - Client/httpInterface.js | 71 ++++--------------- Client/webSocketsInterface.js | 11 +-- .../Foundations/SA/Utilities/HttpResponses.js | 42 ++++++++++- 4 files changed, 57 insertions(+), 69 deletions(-) diff --git a/Client/client.js b/Client/client.js index 5adf10a82..c5ab5b73a 100644 --- a/Client/client.js +++ b/Client/client.js @@ -165,12 +165,10 @@ try { WEB_SOCKETS_INTERFACE = WEB_SOCKETS_INTERFACE.newWebSocketsInterface() WEB_SOCKETS_INTERFACE.initialize() - WEB_SOCKETS_INTERFACE.run() console.log('Web Sockets Interface ....................................... Listening at port ' + global.env.CLIENT_WEB_SOCKETS_INTERFACE_PORT) HTTP_INTERFACE = HTTP_INTERFACE.newHttpInterface() HTTP_INTERFACE.initialize() - HTTP_INTERFACE.run() console.log('Http Interface .............................................. Listening at port ' + global.env.CLIENT_HTTP_INTERFACE_PORT) console.log('') diff --git a/Client/httpInterface.js b/Client/httpInterface.js index 89b98454e..4cb4b65f3 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -9,8 +9,7 @@ exports.newHttpInterface = function newHttpInterface() { */ let thisObject = { initialize: initialize, - finalize: finalize, - run: run + finalize: finalize } let webhook = new Map() @@ -18,32 +17,29 @@ exports.newHttpInterface = function newHttpInterface() { return thisObject function finalize() { - + webhook = undefined } function initialize() { - - } - - function run() { /* We will create an HTTP Server and leave it running forever. */ - SA.nodeModules.http.createServer(onHttpRequest).listen(global.env.CLIENT_HTTP_INTERFACE_PORT) - /* Starting the browser now is optional */ - if (process.argv.includes("noBrowser")) { - //Running Client only with no UI. - } else { - SA.nodeModules.open('http://localhost:' + global.env.CLIENT_HTTP_INTERFACE_PORT) - } + SA.nodeModules.http.createServer(onHttpRequest).listen(global.env.CLIENT_HTTP_INTERFACE_PORT) + /* Starting the browser now is optional */ + if (process.argv.includes("noBrowser")) { + //Running Client only with no UI. + } else { + SA.nodeModules.open('http://localhost:' + global.env.CLIENT_HTTP_INTERFACE_PORT) + } } function onHttpRequest(httpRequest, httpResponse) { try { let requestPathAndParameters = httpRequest.url.split('?') // Remove version information let requestPath = requestPathAndParameters[0].split('/') + let endpointOrFile = requestPath[1] - switch (requestPath[1]) { + switch (endpointOrFile) { case 'WEB3': { @@ -1194,7 +1190,7 @@ exports.newHttpInterface = function newHttpInterface() { break case 'ChartLayers': { - SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1] + '/' + requestPath[2], httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_UI + '/' + endpointOrFile + '/' + requestPath[2], httpResponse) } break case 'Files': @@ -1700,50 +1696,9 @@ exports.newHttpInterface = function newHttpInterface() { break default: { - noEndpointReceived() + SA.projects.foundations.utilities.httpResponses.respondWithWebFile(httpResponse, endpointOrFile, global.env.PATH_TO_CLIENT) } } - - function noEndpointReceived() { - - if (requestPath[1] === '') { - /* - When there is no endpoint specidied we will respond with this app's Home Page. - */ - let fs = SA.nodeModules.fs - - let fileName = global.env.PATH_TO_CLIENT + 'WebServer/index.html' - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - - let fileContent = file.toString() - - SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse) - } - } else { - /* - When there is a parameter but it does not match any of the available endpoints, we - will serve the file with the same name at at a location that depends on it's file extention. - */ - let completeFileName = requestPath[1] - let fileExtension = completeFileName.split('.')[1] - let path - - switch (fileExtension) { - case 'js': { - path = global.env.PATH_TO_UI + '/' + completeFileName - SA.projects.foundations.utilities.httpResponses.respondWithFile(path, httpResponse) - break - } - case 'css': { - SA.projects.foundations.utilities.httpResponses.respondWithStyleSheet(httpResponse, global.env.PATH_TO_CLIENT, completeFileName) - break - } - } - } - } - } catch (err) { console.log(err.stack) } diff --git a/Client/webSocketsInterface.js b/Client/webSocketsInterface.js index 36dfd2aef..79bb3a216 100644 --- a/Client/webSocketsInterface.js +++ b/Client/webSocketsInterface.js @@ -4,8 +4,7 @@ let thisObject = { initialize: initialize, - finalize: finalize, - run: run + finalize: finalize } const WEB_SOCKET = require('ws') @@ -15,15 +14,11 @@ return thisObject function finalize() { - + socketServer = undefined } function initialize() { - - } - - function run() { - setUpWebSocketServer() + setUpWebSocketServer() } function setUpWebSocketServer() { diff --git a/Projects/Foundations/SA/Utilities/HttpResponses.js b/Projects/Foundations/SA/Utilities/HttpResponses.js index 96cb2a44d..1b0485964 100644 --- a/Projects/Foundations/SA/Utilities/HttpResponses.js +++ b/Projects/Foundations/SA/Utilities/HttpResponses.js @@ -7,7 +7,8 @@ exports.newFoundationsUtilitiesHttpResponses = function () { respondWithFont: respondWithFont, respondWithEmptyArray: respondWithEmptyArray, respondWithStyleSheet: respondWithStyleSheet, - respondWithProjectFolderFileList: respondWithProjectFolderFileList + respondWithProjectFolderFileList: respondWithProjectFolderFileList, + respondWithWebFile: respondWithWebFile } return thisObject @@ -171,4 +172,43 @@ exports.newFoundationsUtilitiesHttpResponses = function () { } } } + + function respondWithWebFile(httpResponse, fileNameReceived, path) { + + if (fileNameReceived === '') { + /* + When there is no endpoint specidied we will respond with this app's Home Page. + */ + let fs = SA.nodeModules.fs + + let fileName = path + 'WebServer/index.html' + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + + let fileContent = file.toString() + + SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse) + } + } else { + /* + When there is a parameter but it does not match any of the available endpoints, we + will serve the file with the same name at at a location that depends on it's file extention. + */ + let completeFileName = fileNameReceived + let fileExtension = completeFileName.split('.')[1] + + switch (fileExtension) { + case 'js': { + let path = global.env.PATH_TO_UI + '/' + completeFileName + SA.projects.foundations.utilities.httpResponses.respondWithFile(path, httpResponse) + break + } + case 'css': { + SA.projects.foundations.utilities.httpResponses.respondWithStyleSheet(httpResponse, path, completeFileName) + break + } + } + } + } } \ No newline at end of file From a649b5e767428261ad25b8e361894e33ea3a4240 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 17:16:33 +0200 Subject: [PATCH 11/11] DeepScan Bug Fixes --- Client/httpInterface.js | 2 + Desktop/httpInterface.js | 2113 +---------------- .../Foundations/SA/Utilities/HttpResponses.js | 2 +- 3 files changed, 25 insertions(+), 2092 deletions(-) diff --git a/Client/httpInterface.js b/Client/httpInterface.js index 4cb4b65f3..eb11587a5 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -193,6 +193,8 @@ exports.newHttpInterface = function newHttpInterface() { let content = { err: global.DEFAULT_FAIL_RESPONSE // method not supported } + SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(content), httpResponse) + } catch (err) { console.log('[INFO] httpInterface -> CCXT FetchMarkets -> Could not fetch markets.') let error = { diff --git a/Desktop/httpInterface.js b/Desktop/httpInterface.js index 20a557a51..96e647f8e 100644 --- a/Desktop/httpInterface.js +++ b/Desktop/httpInterface.js @@ -7,2115 +7,46 @@ exports.newHttpInterface = function newHttpInterface() { */ let thisObject = { initialize: initialize, - finalize: finalize, - run: run + finalize: finalize } - const open = require('open') - - let port = global.env.DESKTOP_HTTP_INTERFACE_PORT - - let http = require('http') - let isHttpServerStarted = false - - let webhook = new Map() - return thisObject function finalize() { - } function initialize() { - + /* + We will create an HTTP Server and leave it running forever. + */ + SA.nodeModules.http.createServer(onHttpRequest).listen(global.env.CLIENT_HTTP_INTERFACE_PORT) + /* Starting the browser now is optional */ + if (process.argv.includes("noBrowser")) { + //Running Client only with no UI. + } else { + SA.nodeModules.open('http://localhost:' + global.env.CLIENT_HTTP_INTERFACE_PORT) + } } - function run() { - startHtttpServer() - } - - function startHtttpServer() { - + function onHttpRequest(httpRequest, httpResponse) { try { - if (isHttpServerStarted === false) { - gWebServer = http.createServer(onBrowserRequest).listen(port) - isHttpServerStarted = true - /* Starting the browser now is optional */ - if (process.argv.includes("noBrowser")) { - //Running Client only with no UI. - } else { - open('http://localhost:' + port) - } + let requestPathAndParameters = httpRequest.url.split('?') // Remove version information + let requestPath = requestPathAndParameters[0].split('/') + let endpointOrFile = requestPath[1] - } - } catch (err) { - console.log('[ERROR] httpInterface -> startHtttpServer -> Error = ' + err.stack) - } - } + switch (endpointOrFile) { + case 'XXX': + { - function onBrowserRequest(httpRequest, httpResponse) { - - function getBody(callback) { // Gets the de body from a POST httpRequest to the web server - try { - - let body = '' - - httpRequest.on('data', function (data) { - body += data - // Too much POST data - //if (body.length > 1e6) { - // httpRequest.connection.destroy() - //} - }) - - httpRequest.on('end', function () { - callback(body) - }) - - httpRequest.on('error', function (err) { - console.log('[INFO] httpInterface -> onBrowserRequest -> getBody -> err = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - }) - } catch (err) { - console.log('[INFO] httpInterface -> onBrowserRequest -> getBody -> err = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } - } - - let requestParameters = httpRequest.url.split('/') - - requestParameters = httpRequest.url.split('?') // Remove version information - requestParameters = requestParameters[0].split('/') - - switch (requestParameters[1]) { - - case 'WEB3': - { - getBody(processRequest) - - async function processRequest(body) { - try { - let params = JSON.parse(body) - - switch (params.method) { - case 'getNetworkClientStatus': { - - let serverResponse = await CL.servers.WEB3_SERVER.getNetworkClientStatus( - params.host, - params.port, - params.interface - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'createWalletAccount': { - - let serverResponse = await CL.servers.WEB3_SERVER.createWalletAccount( - params.entropy - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'getWalletBalances': { - - let serverResponse = await CL.servers.WEB3_SERVER.getWalletBalances( - params.host, - params.port, - params.interface, - params.walletDefinition - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'signData': { - - let serverResponse = await CL.servers.WEB3_SERVER.signData( - params.privateKey, - params.data - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'recoverAddress': { - - let serverResponse = await CL.servers.WEB3_SERVER.recoverAddress( - params.signature - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'mnemonicToPrivateKey': { - - let serverResponse = await CL.servers.WEB3_SERVER.mnemonicToPrivateKey( - params.mnemonic - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - default: { - respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) - } - } - } catch (err) { - console.log('[ERROR] httpInterface -> WEB3s -> Method call produced an error.') - console.log('[ERROR] httpInterface -> WEB3s -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> WEB3s -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } } break - } - - case 'CCXT': - { - getBody(processRequest) - - async function processRequest(body) { - try { - let params = JSON.parse(body) - - const ccxt = require('ccxt') - - switch (params.method) { - case 'fetchMarkets': { - - const exchangeClass = ccxt[params.exchangeId] - const exchangeConstructorParams = { - 'timeout': 30000, - 'enableRateLimit': true, - verbose: false - } - - let ccxtExchange = new exchangeClass(exchangeConstructorParams) - let ccxtMarkets = [] - - if (ccxtExchange.has.fetchMarkets === true) { - ccxtMarkets = await ccxtExchange.fetchMarkets() - } - respondWithContent(JSON.stringify(ccxtMarkets), httpResponse) - return - } - case 'listExchanges': { - let exchanges = [] - for (let i = 0; i < ccxt.exchanges.length; i++) { - let exchangeId = ccxt.exchanges[i] - - const exchangeClass = ccxt[exchangeId] - const exchangeConstructorParams = { - 'timeout': 30000, - 'enableRateLimit': true, - verbose: false - } - let ccxtExchange - try { ccxtExchange = new exchangeClass(exchangeConstructorParams) } - catch (err) { } - if (ccxtExchange === undefined) { continue } - - - if (ccxtExchange.has.fetchOHLCV === params.has.fetchOHLCV) { - if (ccxtExchange.has.fetchMarkets === params.has.fetchMarkets) { - if (ccxtExchange.timeframes['1m'] !== undefined) { - let exchange = { - name: ccxtExchange.name, - id: ccxtExchange.id - } - exchanges.push(exchange) - } - } - } - } - respondWithContent(JSON.stringify(exchanges), httpResponse) - return - } - } - - let content = { - err: global.DEFAULT_FAIL_RESPONSE // method not supported - } - } catch (err) { - console.log('[INFO] httpInterface -> CCXT FetchMarkets -> Could not fetch markets.') - let error = { - result: 'Fail Because', - message: err.message - } - respondWithContent(JSON.stringify(error), httpResponse) - } + default: + { + SA.projects.foundations.utilities.httpResponses.respondWithWebFile(httpResponse, endpointOrFile, global.env.PATH_TO_CLIENT) } - break - } - break - - case 'Webhook': - { - switch (requestParameters[2]) { // switch by command - case 'Fetch-Messages': { - let exchange = requestParameters[3] - let market = requestParameters[4] - - /* Some validations */ - if (exchange === undefined) { - console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no Exchange received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - if (market === undefined) { - console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no market received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - - let key = exchange + '-' + market - - let webhookMessages = webhook.get(key) - if (webhookMessages === undefined) { - webhookMessages = [] - } - - console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Exchange-Market = ' + exchange + '-' + market) - console.log('[INFO] httpInterface -> Webhook -> Fetch-Messages -> Messeges Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) - - respondWithContent(JSON.stringify(webhookMessages), httpResponse) - webhookMessages = [] - - webhook.set(key, webhookMessages) - break - } - case 'New-Message': { - getBody(processRequest) - - function processRequest(messageReceived) { - let timestamp = (new Date()).valueOf() - let source = requestParameters[3] - let exchange = requestParameters[4] - let market = requestParameters[5] - - /* Some validations */ - if (source === undefined) { - console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Source received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - if (exchange === undefined) { - console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Exchange received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - if (market === undefined) { - console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no market received -> messageReceived = ' + messageReceived) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - - let key = exchange + '-' + market - - let webhookMessages = webhook.get(key) - if (webhookMessages === undefined) { - webhookMessages = [] - } - - webhookMessages.push([timestamp, source, messageReceived]) - webhook.set(key, webhookMessages) - - console.log('[INFO] httpInterface -> Webhook -> New-Message -> Exchange-Market = ' + exchange + '-' + market) - console.log('[INFO] httpInterface -> Webhook -> New-Message -> messageReceived = ' + messageReceived) - console.log('[INFO] httpInterface -> Webhook -> New-Message -> Messeges waiting to be Fetched by Webhooks Sensor Bot = ' + webhookMessages.length) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } - break - } - } - break - } - break - - case 'Docs': - { - switch (requestParameters[2]) { // switch by command - case 'Save-Node-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Nodes' - - if (checkAllSchmemaDocuments('Node', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Concept-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Concepts' - - if (checkAllSchmemaDocuments('Concept', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Topic-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Topics' - - if (checkAllSchmemaDocuments('Topic', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Tutorial-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Tutorials' - - if (checkAllSchmemaDocuments('Tutorial', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Tutorial-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Review-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Reviews' - - if (checkAllSchmemaDocuments('Review', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Review-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'Save-Book-Schema': { - getBody(processRequest) - - async function processRequest(body) { - try { - let docsSchema = JSON.parse(body) - let project = requestParameters[3] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Books' - - if (checkAllSchmemaDocuments('Book', docsSchema, filePath) === true) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } - - } catch (err) { - console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> Method call produced an error.') - console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> Docs -> Save-Book-Schema -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - } - - function checkAllSchmemaDocuments(category, docsSchema, filePath) { - const fs = require('fs') - let noErrorsDuringSaving = true - - for (let i = 0; i < docsSchema.length; i++) { - let schemaDocument = docsSchema[i] - /* - For some type of schemas we will save the file at an extra - folder derived from the document's type. - */ - let fileName = schemaDocument.type.toLowerCase() - for (let j = 0; j < 10; j++) { - fileName = cleanFileName(fileName) - } - let pageNumber = '00' + schemaDocument.pageNumber - let newFilepath = filePath - switch (category) { - case 'Topic': { - fileName = schemaDocument.topic.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() - fileName = cleanFileName(fileName) - newFilepath = createPrefixDirectories(filePath, schemaDocument.topic) - break - } - case 'Tutorial': { - fileName = schemaDocument.tutorial.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() - fileName = cleanFileName(fileName) - newFilepath = createPrefixDirectories(filePath, schemaDocument.tutorial) - break - } - case 'Review': { - fileName = schemaDocument.review.toLowerCase() + '-' + pageNumber.substring(pageNumber.length - 3, pageNumber.length) + '-' + schemaDocument.type.toLowerCase() - fileName = cleanFileName(fileName) - newFilepath = createPrefixDirectories(filePath, schemaDocument.review) - break - } - case 'Node': { - newFilepath = createPrefixDirectories(filePath, schemaDocument.type) - break - } - case 'Concept': { - newFilepath = createPrefixDirectories(filePath, schemaDocument.type) - break - } - } - - function createPrefixDirectories(filePath, schemaTextToUse) { - let firstLetter = schemaTextToUse.substring(0, 1) - createNewDir(filePath + '/' + firstLetter) - let extraWord = schemaTextToUse.split(' ')[0] - createNewDir(filePath + '/' + firstLetter + '/' + extraWord) - return filePath + '/' + firstLetter + '/' + extraWord + '/' + cleanFileName(schemaTextToUse) - } - - fileName = fileName + '.json' - - if (schemaDocument.deleted === true) { - try { - fs.unlinkSync(newFilepath + '/' + fileName) - console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' deleted.') - } catch (err) { - noErrorsDuringSaving = false - console.log('[ERROR] httpInterface -> Docs -> Delete -> ' + newFilepath + '/' + fileName + ' could not be deleted.') - console.log('[ERROR] httpInterface -> Docs -> Delete -> Resolve the issue that is preventing the Client to delete this file. Look at the error message below as a guide. At the UI you will need to delete this page again in order for the Client to retry next time you execute the docs.save command.') - console.log('[ERROR] httpInterface -> Docs -> Delete -> err.stack = ' + err.stack) - } - } else { - if (schemaDocument.updated === true || schemaDocument.created === true) { - try { - let created = schemaDocument.created - let updated = schemaDocument.updated - schemaDocument.updated = undefined - schemaDocument.created = undefined - let fileContent = JSON.stringify(schemaDocument, undefined, 4) - createNewDir(newFilepath) - fs.writeFileSync(newFilepath + '/' + fileName, fileContent) - if (created === true) { - console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' created.') - } else { - if (updated === true) { - console.log('[SUCCESS] ' + newFilepath + '/' + fileName + ' updated.') - } - } - } catch (err) { - noErrorsDuringSaving = false - console.log('[ERROR] httpInterface -> Docs -> Save -> ' + newFilepath + '/' + fileName + ' could not be created / updated.') - console.log('[ERROR] httpInterface -> Docs -> Save -> err.stack = ' + err.stack) - } - } - } - - function createNewDir(path) { - try { - fs.mkdirSync(path) - } catch (err) { - if (err.message.indexOf('file already exists') < 0) { - throw (err) - } - } - } - } - - return noErrorsDuringSaving - } - - function cleanFileName(fileName) { - for (let i = 0; i < 100; i++) { - fileName = fileName - .replace(' ', '-') - .replace('--', '-') - .replace('?', '') - .replace('#', '') - .replace('$', '') - .replace('%', '') - .replace('^', '') - .replace('&', '') - .replace('*', '') - .replace('(', '') - .replace(')', '') - .replace('!', '') - .replace('..', '.') - .replace(',', '') - .replace('\'', '') - } - return fileName - } - } - break - - case 'App': { - switch (requestParameters[2]) { // switch by command - case 'Contribute': { - try { - let commitMessage = unescape(requestParameters[3]) - const username = unescape(requestParameters[4]) - const token = unescape(requestParameters[5]) - const currentBranch = unescape(requestParameters[6]) - const contributionsBranch = unescape(requestParameters[7]) - let error - - /* Unsavping # */ - for (let i = 0; i < 10; i++) { - commitMessage = commitMessage.replace('_SLASH_', '/') - commitMessage = commitMessage.replace('_HASHTAG_', '#') - } - - contribute() - - async function contribute() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - await doGit() - if (error !== undefined) { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Contribution Not Sent', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, error) - return - } - - await doGithub() - if (error !== undefined) { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Contribution Not Sent', - anchor: undefined, - placeholder: {} - } - console.log('respond with docs ') - - respondWithDocsObject(docs, error) - return - } - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } - } - - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) - - try { - await git.add('./*') - await git.commit(commitMessage) - await git.push('origin', currentBranch) - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGit -> contributionsBranch = ' + contributionsBranch) - console.log('') - console.log('Torubleshooting Tips:') - console.log('') - console.log('1. Make sure that you have set up your Github Username and Token at the APIs -> Github API node at the workspace.') - console.log('2. Make sure you are running the latest version of Git available for your OS.') - console.log('3. Make sure that you have cloned your Superalgos repository fork, and not the main Superalgos repository.') - console.log('4. If your fork is old, you might need to do an app.update and also a node setup at every branch. If you just reforked all is good.') - error = err - } - } - - async function doGithub() { - - const { Octokit } = require("@octokit/rest") - - const octokit = new Octokit({ - auth: token, - userAgent: 'Superalgos Beta 11' - }) - - const repo = 'Superalgos' - const owner = 'Superalgos' - const head = username + ':' + contributionsBranch - const base = currentBranch - const title = 'Contribution: ' + commitMessage - - try { - await octokit.pulls.create({ - owner, - repo, - title, - head, - base, - }); - } catch (err) { - if (err.stack.indexOf('A pull request already exists') >= 0) { - return - } else { - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...') - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10)) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch) - error = err - } - } - } - - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Contribute -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Contribute -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> App -> Contribute -> commitMessage = ' + commitMessage) - console.log('[ERROR] httpInterface -> App -> Contribute -> username = ' + username) - console.log('[ERROR] httpInterface -> App -> Contribute -> token starts with = ' + token.substring(0, 10) + '...') - console.log('[ERROR] httpInterface -> App -> Contribute -> token ends with = ' + '...' + token.substring(token.length - 10)) - console.log('[ERROR] httpInterface -> App -> Contribute -> currentBranch = ' + currentBranch) - console.log('[ERROR] httpInterface -> App -> Contribute -> contributionsBranch = ' + contributionsBranch) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - break - } - - case 'Update': { - try { - const currentBranch = unescape(requestParameters[3]) - update() - - async function update() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - let result = await doGit() - - if (result.error === undefined) { - let customResponse = { - result: global.CUSTOM_OK_RESPONSE.result, - message: result.message - } - respondWithContent(JSON.stringify(customResponse), httpResponse) - } else { - - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Update Failed', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, result.error) - - } - } - } - - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) - - let message - try { - message = await git.pull('https://github.com/Superalgos/Superalgos', currentBranch) - return { message: message } - } catch (err) { - console.log('[ERROR] Error updating ' + currentBranch) - console.log(err.stack) - return { error: err } - } - } - - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - break - } - - case 'Checkout': { - try { - const currentBranch = unescape(requestParameters[3]) - let error - - checkout() - - async function checkout() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - await doGit() - - if (error === undefined) { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } else { - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'Switching Branches - Current Branch Not Changed', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, error) - } - } - } - - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) - try { - await git.checkout(currentBranch) - } catch (err) { - console.log('[ERROR] Error changing current branch to ' + currentBranch) - console.log(err.stack) - error = err - } - } - - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - break - } - - case 'Branch': { - try { - branch() - - async function branch() { - const { lookpath } = require('lookpath'); - const gitpath = await lookpath('git'); - if (gitpath === undefined) { - console.log('[ERROR] `git` not installed.') - } else { - let result = await doGit() - - if (result.error === undefined) { - let customResponse = { - result: global.CUSTOM_OK_RESPONSE.result, - message: result - } - respondWithContent(JSON.stringify(customResponse), httpResponse) - } else { - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'App Error - Could Not Get Current Branch', - anchor: undefined, - placeholder: {} - } - - respondWithDocsObject(docs, error) - } - } - } - - async function doGit() { - const simpleGit = require('simple-git'); - const options = { - baseDir: process.cwd(), - binary: 'git', - maxConcurrentProcesses: 6, - } - const git = simpleGit(options) - try { - return await git.branch() - } catch (err) { - console.log('[ERROR] Error reading current branch.') - console.log(err.stack) - } - } - - } catch (err) { - console.log('[ERROR] httpInterface -> App -> Update -> Method call produced an error.') - console.log('[ERROR] httpInterface -> App -> Update -> err.stack = ' + err.stack) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - break - } - } - - function respondWithDocsObject(docs, error) { - - if (error.message !== undefined) { - docs.placeholder.errorMessage = { - style: 'Error', - text: error.message - } - } - if (error.stack !== undefined) { - docs.placeholder.errorStack = { - style: 'Javascript', - text: error.stack - } - } - if (error.code !== undefined) { - docs.placeholder.errorCode = { - style: 'Json', - text: error.code - } - } - - docs.placeholder.errorDetails = { - style: 'Json', - text: JSON.stringify(error, undefined, 4) - } - - let customResponse = { - result: global.CUSTOM_FAIL_RESPONSE.result, - docs: docs - } - - respondWithContent(JSON.stringify(customResponse), httpResponse) - - } - } - break - - case 'GOV': { - /* - This is the Governance endpoint at the Http Interface. All methods - related to the Governance System are implemented here and routed - to the backend Servers that can process them. - */ - getBody(processRequest) - - async function processRequest(body) { - try { - let params = JSON.parse(body) - - switch (params.method) { - case 'getGithubStars': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubStars( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'getGithubWatchers': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubWatchers( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'getGithubForks': { - - let serverResponse = await CL.servers.GITHUB_SERVER.getGithubForks( - params.repository, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'mergePullRequests': { - - let serverResponse = await CL.servers.GITHUB_SERVER.mergePullRequests( - params.commitMessage, - params.username, - params.token - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - case 'payContributors': { - - let serverResponse = await CL.servers.WEB3_SERVER.payContributors( - params.contractAddress, - params.contractAbi, - params.paymentsArray, - params.mnemonic - ) - - respondWithContent(JSON.stringify(serverResponse), httpResponse) - return - } - default: { - respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse) - } - } - } catch (err) { - console.log('[ERROR] httpInterface -> GOV -> Method call produced an error.') - console.log('[ERROR] httpInterface -> GOV -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> GOV -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'LegacyPlotter.js': - { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse) - } - break - - case 'PlotterPanel.js': - { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', httpResponse) - } - break - - case 'Images': // This means the Images folder. - { - let path = global.env.PATH_TO_CLIENT + 'WebServer/Images/' + requestParameters[2] - - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] - } - - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] - } - - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] - } - - path = unescape(path) - - respondWithImage(path, httpResponse) - } - break - - case 'Icons': // This means the Icons folder under Projects. - { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Icons' - - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] - } - - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] - } - - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] - } - - path = unescape(path) - - respondWithImage(path, httpResponse) - } - break - - case 'GIFs': // This means the GIFs folder under Projects. - { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/GIFs' - - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] - } - - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] - } - - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] - } - - path = unescape(path) - respondWithImage(path, httpResponse) - } - break - - case 'PNGs': // This means the PNGs folder under Projects. - { - let path = global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/PNGs' - - if (requestParameters[3] !== undefined) { - path = path + '/' + requestParameters[3] - } - - if (requestParameters[4] !== undefined) { - path = path + '/' + requestParameters[4] - } - - if (requestParameters[5] !== undefined) { - path = path + '/' + requestParameters[5] - } - - path = unescape(path) - respondWithImage(path, httpResponse) - } - break - - case 'favicon.ico': // This means the Scripts folder. - { - respondWithImage(global.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', httpResponse) - } - break - - case 'WebServer': // This means the WebServer folder. - { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/' + requestParameters[2], httpResponse) - } - break - - case 'externalScripts': // This means the WebServer folder. - { - respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestParameters[2], httpResponse) - } - break - - case 'Plotters': // This means the plotter folder, not to be confused with the Plotters script! - { - let project = requestParameters[2] - let dataMine = requestParameters[3] - let codeName = requestParameters[4] - let moduleName = requestParameters[5] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'Bots-Plotters-Code' + '/' + dataMine + '/plotters/' + codeName + '/' + moduleName - respondWithFile(filePath, httpResponse) - } - break - - case 'ChartLayers': - { - respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1] + '/' + requestParameters[2], httpResponse) - } - break - - case 'Files': - { - respondWithFile(global.env.PATH_TO_DATA_FILES + '/' + requestParameters[2], httpResponse) - } - break - - case 'Fonts': - { - respondWithFont(global.env.PATH_TO_FONTS + '/' + requestParameters[2], httpResponse) - } - break - - case 'Schema': - { - sendSchema(global.env.PATH_TO_PROJECTS + '/' + requestParameters[2] + '/Schemas/', requestParameters[3]) - } - break - - case 'DirContent': - { - let folderPath = unescape(requestParameters[2]) - if (requestParameters[3] !== undefined) { - folderPath = folderPath + '/' + requestParameters[3] - } - - if (requestParameters[4] !== undefined) { - folderPath = folderPath + '/' + requestParameters[4] - } - - if (requestParameters[5] !== undefined) { - folderPath = folderPath + '/' + requestParameters[5] - } - let folder - if (requestParameters[2] === 'Root') { - folder = folderPath.replace('Root', '../Superalgos/') - } else { - folder = global.env.PATH_TO_PROJECTS + '/' + folderPath - } - - getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) - - function onFilesReady(files) { - respondWithContent(JSON.stringify(files), httpResponse) - } - } - break - - case 'IconNames': - { - let projects = getDirectories(global.env.PATH_TO_PROJECTS) - let icons = [] - let totalProjects = projects.length - let projectCounter = 0 - - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - - const folder = global.env.PATH_TO_PROJECTS + '/' + project + '/Icons/' - - getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) - - function onFilesReady(files) { - for (let j = 0; j < files.length; j++) { - let file = files[j] - for (let i = 0; i < 10; i++) { - file = file.replace('/', '\\') - } - icons.push([project, file]) - } - - projectCounter++ - if (projectCounter === totalProjects) { - respondWithContent(JSON.stringify(icons), httpResponse) - } - } - } - } - break - - case 'PluginFileNames': - { - processRequest() - - async function processRequest(body) { - try { - let project = unescape(requestParameters[2]) - let folder = unescape(requestParameters[3]) - - let response = await SA.projects.foundations.utilities.plugins.getPluginFileNames( - project, - folder - ) - - respondWithContent(JSON.stringify(response), httpResponse) - - } catch (err) { - console.log('[ERROR] httpInterface -> PluginFileNames -> Method call produced an error.') - console.log('[ERROR] httpInterface -> PluginFileNames -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> PluginFileNames -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'LoadPlugin': - - { - processRequest() - - async function processRequest(body) { - try { - let project = unescape(requestParameters[2]) - let folder = unescape(requestParameters[3]) - let fileName = unescape(requestParameters[4]) - - let response = await SA.projects.foundations.utilities.plugins.getPluginFileContent( - project, - folder, - fileName - ).catch(err => { - let error = { - result: 'Fail Because', - message: err - } - respondWithContent(JSON.stringify(error), httpResponse) - return - }) - - respondWithContent(response, httpResponse) - - } catch (err) { - console.log('[ERROR] httpInterface -> LoadPlugin -> Method call produced an error.') - console.log('[ERROR] httpInterface -> LoadPlugin -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> LoadPlugin -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - } - - case 'SavePlugin': - getBody(processRequest) - - async function processRequest(body) { - try { - let plugin = JSON.parse(body) - let project = requestParameters[2] - let folder = requestParameters[3] - let fileName = requestParameters[4] - let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/' + folder - let fileContent = JSON.stringify(plugin, undefined, 4) - const fs = require('fs') - fs.writeFileSync(filePath + '/' + fileName + '.json', fileContent) - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } catch (err) { - console.log('[ERROR] httpInterface -> SavePlugin -> Method call produced an error.') - console.log('[ERROR] httpInterface -> SavePlugin -> err.stack = ' + err.stack) - console.log('[ERROR] httpInterface -> SavePlugin -> Params Received = ' + body) - - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - break - - case 'Workspace.js': - { - let fs = require('fs') - - try { - let filePath = global.env.PATH_TO_DEFAULT_WORKSPACE + '/Getting-Started-Tutorials.json' - fs.readFile(filePath, onFileRead) - } catch (e) { - console.log('[ERROR] Error reading the Workspace.', e) - } - - function onFileRead(err, workspace) { - if (err) { - respondWithContent(undefined, httpResponse) - } else { - let responseContent = 'function getWorkspace(){ return ' + workspace + '}' - respondWithContent(responseContent, httpResponse) - } - } - } - break - - case 'ListWorkspaces': - { - let allWorkspaces = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) - let projectsCount = 0 - - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - readPluginWorkspaces() - function readPluginWorkspaces() { - let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/Plugins/Workspaces' - try { - let fs = require('fs') - fs.readdir(dirPath, onDirRead) - - function onDirRead(err, fileList) { - let updatedFileList = [] - - if (err) { - /* - If we have a problem reading this folder we will assume that it is - because this project does not need this folder and that's it. - */ - //console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) - } else { - for (let i = 0; i < fileList.length; i++) { - let name = 'Plugin \u2192 ' + fileList[i] - updatedFileList.push([project, name]) - } - } - allWorkspaces = allWorkspaces.concat(updatedFileList) - projectsCount++ - if (projectsCount === projects.length) { - readMyWorkspaces() - } - } - } catch (err) { - console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) - console.log('[ERROR] err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - } - } - - function readMyWorkspaces() { - let dirPath = global.env.PATH_TO_MY_WORKSPACES - try { - let fs = require('fs') - fs.readdir(dirPath, onDirRead) - - function onDirRead(err, fileList) { - if (err) { - // This happens the first time you run the software. - respondWithContent(JSON.stringify(allWorkspaces), httpResponse) - return - } else { - let updatedFileList = [] - for (let i = 0; i < fileList.length; i++) { - let name = fileList[i] - updatedFileList.push(['', name]) - } - allWorkspaces = allWorkspaces.concat(updatedFileList) - respondWithContent(JSON.stringify(allWorkspaces), httpResponse) - return - } - } - } catch (err) { - console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) - console.log('[ERROR] err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - } - } - break - - case 'LoadMyWorkspace': - { - let fileName = unescape(requestParameters[2]) - let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' - respondWithFile(filePath, httpResponse) - } - break - - case 'SaveWorkspace': - { - getBody(processRequest) - - async function processRequest(body) { - - let fileContent = body - let fileName = unescape(requestParameters[2]) - let filePath = global.env.PATH_TO_MY_WORKSPACES + '/' + fileName + '.json' - - try { - let fs = require('fs') - let dir = global.env.PATH_TO_MY_WORKSPACES; - - /* Create Dir if it does not exist */ - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); - } - - fs.writeFile(filePath, fileContent, onFileWritten) - - function onFileWritten(err) { - if (err) { - console.log('[ERROR] SaveWorkspace -> onFileWritten -> Error writing the Workspace file. fileName = ' + fileName) - console.log('[ERROR] SaveWorkspace -> onFileWritten -> err.stack = ' + err.stack) - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } else { - respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) - } - } - - } catch (err) { - console.log('[ERROR] SaveWorkspace -> Error writing the Workspace file. fileName = ' + fileName) - console.log('[ERROR] SaveWorkspace -> err.stack = ' + err.stack) - let error = { - result: 'Fail Because', - message: err.message, - stack: err.stack - } - respondWithContent(JSON.stringify(error), httpResponse) - } - } - } - break - - case 'ListFunctionLibraries': - { - returnProjectFolderFileList('Function-Libraries') - } - break - - case 'ProjectsSchema': - { - let path = global.env.PATH_TO_PROJECTS + '/' + 'ProjectsSchema.json' - respondWithFile(path, httpResponse) - } - break - - case 'ListSpaceFiles': - { - let fs = require('fs') - let allFiles = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) - let dirCount = 0 - let totalDirs = 0 - - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - - let dirPath = project + '/UI/Spaces' - let spaces = getDirectories(global.env.PATH_TO_PROJECTS + '/' + dirPath) - - for (let j = 0; j < spaces.length; j++) { - let space = spaces[j] - readDirectory(dirPath + '/' + space) - } - - function readDirectory(path) { - try { - - totalDirs++ - fs.readdir(global.env.PATH_TO_PROJECTS + '/' + path, onDirRead) - - let otherDirs = getDirectories(global.env.PATH_TO_PROJECTS + '/' + path) - for (let m = 0; m < otherDirs.length; m++) { - let otherDir = otherDirs[m] - readDirectory(path + '/' + otherDir) - } - - function onDirRead(err, fileList) { - if (err) { - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - } else { - let updatedFileList = [] - for (let k = 0; k < fileList.length; k++) { - let name = fileList[k] - if (name.indexOf('.js') < 0) { continue } - updatedFileList.push(path + '/' + name) - } - allFiles = allFiles.concat(updatedFileList) - dirCount++ - if (dirCount === totalDirs) { - respondWithContent(JSON.stringify(allFiles), httpResponse) - } - } - } - } catch (err) { - console.log('[ERROR] Error reading a directory content. filePath = ' + path) - console.log('[ERROR] err.stack = ' + err.stack) - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - } - } - } - break - - case 'ListUtilitiesFiles': - { - returnProjectFolderFileList('Utilities') - } - break - - case 'ListGlobalFiles': - { - returnProjectFolderFileList('Globals') - } - break - - case 'Projects': - { - let path = '' - for (let i = 2; i < 10; i++) { - if (requestParameters[i] !== undefined) { - let parameter = unescape(requestParameters[i]) - path = path + '/' + parameter - } - - } - let filePath = global.env.PATH_TO_PROJECTS + path - respondWithFile(filePath, httpResponse) - } - break - - case 'Storage': - { - let pathToFile = httpRequest.url.substring(9) - /* Unsavping # */ - for (let i = 0; i < 10; i++) { - pathToFile = pathToFile.replace('_HASHTAG_', '#') - } - respondWithFile(global.env.PATH_TO_DATA_STORAGE + '/' + pathToFile, httpResponse) - } - break - - case 'main.css': - { - sendStyleSheet('main.css') - } - break - - case 'tutorial.css': - { - sendStyleSheet('tutorial.css') - } - break - - case 'docs.css': - { - sendStyleSheet('docs.css') - } - break - - case 'governance.css': - { - sendStyleSheet('governance.css') - } - break - - case 'context-menu.css': - { - sendStyleSheet('context-menu.css') - } - break - - case 'credits.css': - { - sendStyleSheet('credits.css') - } - break - - case 'docs.css': - { - sendStyleSheet('docs.css') - } - break - - case 'font-awasome.css': - { - sendStyleSheet('font-awasome.css') - } - break - - case 'prism.css': - { - sendStyleSheet('prism.css') - } - break - - case 'ExecuteTerminalCommand': - { - let command = unescape(requestParameters[2]) - executeTerminalCommand(command) - } - break - default: - { - homePage() - } - } - - function returnProjectFolderFileList(projectFolderName) { - { - let allLibraries = [] - let projects = getDirectories(global.env.PATH_TO_PROJECTS) - let projectsCount = 0 - - for (let i = 0; i < projects.length; i++) { - let project = projects[i] - - let dirPath = global.env.PATH_TO_PROJECTS + '/' + project + '/' + 'UI' + '/' + projectFolderName - try { - let fs = require('fs') - fs.readdir(dirPath, onDirRead) - - function onDirRead(err, fileList) { - let updatedFileList = [] - if (err) { - /* - If we have a problem reading this folder we will assume that it is - because this project does not need this folder and that's it. - */ - // console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) - } else { - for (let i = 0; i < fileList.length; i++) { - let name = fileList[i] - updatedFileList.push([project, name]) - } - } - allLibraries = allLibraries.concat(updatedFileList) - projectsCount++ - if (projectsCount === projects.length) { - respondWithContent(JSON.stringify(allLibraries), httpResponse) - } - } - } catch (err) { - console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) - console.log('[ERROR] err.stack = ' + err.stack) - - respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse) - return - } - } - } - } - - function executeTerminalCommand(command) { - const util = require('util'); - const exec = util.promisify(require('child_process').exec); - async function lsWithGrep() { - try { - const { stdout, stderr } = await exec(command); - console.log('stdout:', stdout); - console.log('stderr:', stderr); - } catch (err) { - console.error(err.stack); - }; - }; - lsWithGrep(); - } - - function sendSchema(filePath, schemaType) { - let fs = require('fs') - try { - let folder = '' - switch (schemaType) { - case 'AppSchema': { - folder = 'App-Schema' - break - } - case 'DocsNodeSchema': { - folder = 'Docs-Nodes' - break - } - case 'DocsConceptSchema': { - folder = 'Docs-Concepts' - break - } - case 'DocsTopicSchema': { - folder = 'Docs-Topics' - break - } - case 'DocsTutorialSchema': { - folder = 'Docs-Tutorials' - break - } - case 'DocsReviewSchema': { - folder = 'Docs-Reviews' - break - } - case 'DocsBookSchema': { - folder = 'Docs-Books' - break - } - } - getAllFilesInDirectoryAndSubdirectories(filePath + folder, onFilesReady) - function onFilesReady(files) { - - let schemaArray = [] - for (let k = 0; k < files.length; k++) { - let name = files[k] - let nameSplitted = name.split(folder) - let fileName = nameSplitted[1] - for (let i = 0; i < 10; i++) { - fileName = fileName.replace('\\', '/') - } - let fileToRead = filePath + folder + fileName - - let fileContent = fs.readFileSync(fileToRead) - let schemaDocument - try { - schemaDocument = JSON.parse(fileContent) - } catch (err) { - console.log('[ERROR] httpInterface -> sendSchema -> Error Parsing JSON File: ' + fileToRead + ' .Error = ' + err.stack) - respondWithContent("[]", httpResponse) - return - } - schemaArray.push(schemaDocument) - } - let schema = JSON.stringify(schemaArray) - respondWithContent(schema, httpResponse) - } - } catch (err) { - if (err.message.indexOf('no such file or directory') < 0) { - console.log('Could not send Schema:', filePath, schemaType) - console.log(err.stack) - } - respondWithContent("[]", httpResponse) - } - - } - - function sendStyleSheet(fileName) { - let fs = require('fs') - try { - let filePath = global.env.PATH_TO_CLIENT + 'WebServer/css/' + fileName - fs.readFile(filePath, onFileRead) - - function onFileRead(err, file) { - try { - let fileContent = file.toString() - - fileContent = fileContent.replace('DESKTOP_HTTP_INTERFACE_PORT', global.env.DESKTOP_HTTP_INTERFACE_PORT) - fileContent = fileContent.replace('DESKTOP_HTTP_INTERFACE_PORT', global.env.DESKTOP_HTTP_INTERFACE_PORT) - fileContent = fileContent.replace('DESKTOP_HTTP_INTERFACE_PORT', global.env.DESKTOP_HTTP_INTERFACE_PORT) - respondWithContent(fileContent, httpResponse, 'text/css') - } catch (err) { - console.log('[ERROR] httpInterface -> mainCSS -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - } - } - } catch (err) { - console.log(err) - } - } - - function homePage() { - if (requestParameters[1] === '') { - let fs = require('fs') - try { - let fileName = global.env.PATH_TO_CLIENT + 'WebServer/index.html' - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - try { - let fileContent = file.toString() - - fileContent = fileContent.replace('DESKTOP_HTTP_INTERFACE_PORT', global.env.DESKTOP_HTTP_INTERFACE_PORT) - respondWithContent(fileContent, httpResponse) - } catch (err) { - console.log('[ERROR] httpInterface -> homePage -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - } - } - } catch (err) { - console.log(err) - } - } else { - respondWithFile(global.env.PATH_TO_UI + '/' + requestParameters[1], httpResponse) - } - } - } - - function respondWithFile(fileName, httpResponse) { - let fs = require('fs') - if (fileName.indexOf('undefined') > 0) { - console.log('[WRN] httpInterface -> respondWithFile -> Received httpRequest for undefined file. ') - respondWithContent(undefined, httpResponse) - } else { - try { - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - if (!err) { - respondWithContent(file.toString(), httpResponse) - } else { - //console.log('File requested not found: ' + fileName) - respondWithContent(undefined, httpResponse) - } - } - } catch (err) { - returnEmptyArray() - } - } - } - - function respondWithContent(content, httpResponse, contentType) { - try { - httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. - httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. - httpResponse.setHeader('Expires', '0') // Proxies. - httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. - - if (content !== undefined) { - if (contentType !== undefined) { - httpResponse.writeHead(200, { 'Content-Type': contentType }) - } else { - httpResponse.writeHead(200, { 'Content-Type': 'text/html' }) - } - httpResponse.write(content) - } else { - httpResponse.writeHead(404, { 'Content-Type': 'text/html' }) - httpResponse.write('The specified key does not exist.') - } - httpResponse.end('\n') - } catch (err) { - returnEmptyArray(httpResponse) - } - } - - function respondWithImage(fileName, httpResponse) { - let fs = require('fs') - try { - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - if (err) { - console.log('[ERROR] httpInterface -> respondWithImage -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - return - } - try { - httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. - httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. - httpResponse.setHeader('Expires', '0') // Proxies. - httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. - - httpResponse.writeHead(200, { 'Content-Type': 'image/png' }) - httpResponse.end(file, 'binary') - } catch (err) { - console.log('[ERROR] httpInterface -> respondWithImage -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - } } } catch (err) { - console.log('[ERROR] httpInterface -> respondWithImage -> err = ' + err.stack) - } - } - - function respondWithFont(fileName, httpResponse) { - let fs = require('fs') - try { - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - try { - if (err) { - console.log('[ERROR] httpInterface -> respondWithBinary -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - return - } - httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. - httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. - httpResponse.setHeader('Expires', '0') // Proxies. - httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. - - if (fileName.indexOf('2') < 0) { - httpResponse.writeHead(200, { 'Content-Type': 'font/woff' }) - } else { - httpResponse.writeHead(200, { 'Content-Type': 'font/woff2' }) - } - httpResponse.end(file, 'binary') - } catch (err) { - console.log('[ERROR] httpInterface -> respondWithBinary -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack) - } - } - } catch (err) { - console.log('[ERROR] httpInterface -> respondWithBinary -> err = ' + err.stack) - } - } - - function returnEmptyArray(httpResponse) { - try { - httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1. - httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0. - httpResponse.setHeader('Expires', '0') // Proxies. - - httpResponse.writeHead(200, { 'Content-Type': 'text/html' }) - httpResponse.write('[]') - httpResponse.end('\n') - } catch (err) { - console.log('[ERROR] httpInterface -> returnEmptyArray -> err.stack ' + err.stack) - } - } - - function getDirectories(path) { - try { - const fs = require('fs') - return fs.readdirSync(path).filter(function (file) { - return fs.statSync(path + '/' + file).isDirectory(); - }); - } catch (err) { - return [] - } - } - - function getAllFilesInDirectoryAndSubdirectories(dir, callback) { - const { promisify } = require('util'); - const { resolve } = require('path'); - const fs = require('fs'); - const readdir = promisify(fs.readdir); - const stat = promisify(fs.stat); - - getFiles(dir) - .then(files => { - let splittedDir = dir.split('/') - let lastFolder = splittedDir[splittedDir.length - 2] - let pathAndNames = [] - for (let i = 0; i < files.length; i++) { - let file = files[i] - let pathName = file.substring(file.indexOf(lastFolder) + lastFolder.length, file.length) - pathName = pathName.substring(1, pathName.length) - pathAndNames.push(pathName) - } - callback(pathAndNames) - }) - .catch(e => { - callback([]) - }); - - async function getFiles(dir) { - const subdirs = await readdir(dir); - const files = await Promise.all(subdirs.map(async (subdir) => { - const res = resolve(dir, subdir); - return (await stat(res)).isDirectory() ? getFiles(res) : res; - })); - return files.reduce((a, f) => a.concat(f), []); + console.log(err.stack) } } } diff --git a/Projects/Foundations/SA/Utilities/HttpResponses.js b/Projects/Foundations/SA/Utilities/HttpResponses.js index 1b0485964..987e4f9c7 100644 --- a/Projects/Foundations/SA/Utilities/HttpResponses.js +++ b/Projects/Foundations/SA/Utilities/HttpResponses.js @@ -31,7 +31,7 @@ exports.newFoundationsUtilitiesHttpResponses = function () { } } } catch (err) { - respondWithEmptyArray() + respondWithEmptyArray(httpResponse) } } }