diff --git a/src/RootData.ts b/src/RootData.ts index 521cd3c5..9fa5a659 100644 --- a/src/RootData.ts +++ b/src/RootData.ts @@ -5,7 +5,7 @@ | ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ | | █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ | | | -| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. | +| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. | | Licensed under the AGPL-3.0. | | | | According to our dual licensing model, this program can be used either | @@ -21,7 +21,7 @@ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | GNU Affero General Public License for more details. | | | -| "General Bots" is a registered trademark of pragmatismo.cloud. | +| "General Bots" is a registered trademark of pragmatismo.cloud. | | The licensing of the program under the AGPLv3 does not imply a | | trademark license. Therefore any rights, title and interest in | | our trademarks remain entirely with us. | diff --git a/src/app.ts b/src/app.ts index e2d3729d..26ce00fa 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,7 +5,7 @@ | ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ | | █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ | | | -| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. | +| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. | | Licensed under the AGPL-3.0. | | | | According to our dual licensing model, this program can be used either | @@ -21,7 +21,7 @@ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | GNU Affero General Public License for more details. | | | -| "General Bots" is a registered trademark of pragmatismo.cloud. | +| "General Bots" is a registered trademark of pragmatismo.cloud. | | The licensing of the program under the AGPLv3 does not imply a | | trademark license. Therefore any rights, title and interest in | | our trademarks remain entirely with us. | @@ -34,16 +34,19 @@ 'use strict'; -import express from 'express'; +import { Mutex } from 'async-mutex'; +import auth from 'basic-auth'; import bodyParser from 'body-parser'; -import https from 'https'; -import http from 'http'; -import mkdirp from 'mkdirp'; -import Path from 'path'; -import swaggerUI from 'swagger-ui-dist'; -import path from 'path'; +import { GBLog, GBMinInstance, IGBCoreService, IGBInstance } from 'botlib'; +import child_process from 'child_process'; +import express from 'express'; import fs from 'fs'; -import { GBLog, GBMinInstance, IGBCoreService, IGBInstance, IGBPackage } from 'botlib'; +import http from 'http'; +import httpProxy from 'http-proxy'; +import https from 'https'; +import mkdirp from 'mkdirp'; +import { default as Path, default as path } from 'path'; +import swaggerUI from 'swagger-ui-dist'; import { GBAdminService } from '../packages/admin.gbapp/services/GBAdminService.js'; import { AzureDeployerService } from '../packages/azuredeployer.gbapp/services/AzureDeployerService.js'; import { GBConfigService } from '../packages/core.gbapp/services/GBConfigService.js'; @@ -51,14 +54,11 @@ import { GBConversationalService } from '../packages/core.gbapp/services/GBConve import { GBCoreService } from '../packages/core.gbapp/services/GBCoreService.js'; import { GBDeployer } from '../packages/core.gbapp/services/GBDeployer.js'; import { GBImporter } from '../packages/core.gbapp/services/GBImporterService.js'; -import { GBMinService } from '../packages/core.gbapp/services/GBMinService.js'; -import auth from 'basic-auth'; -import child_process from 'child_process'; -import { RootData } from './RootData.js'; -import { GBSSR } from '../packages/core.gbapp/services/GBSSR.js'; -import { Mutex } from 'async-mutex'; -import httpProxy from 'http-proxy'; import { GBLogEx } from '../packages/core.gbapp/services/GBLogEx.js'; +import { GBMinService } from '../packages/core.gbapp/services/GBMinService.js'; +import { GBSSR } from '../packages/core.gbapp/services/GBSSR.js'; +import { RootData } from './RootData.js'; +import { GBUtil } from './util.js'; /** * General Bots open-core entry point. @@ -105,9 +105,8 @@ export class GBServer { server.use(bodyParser.json()); server.use(bodyParser.json({ limit: '1mb' })); server.use(bodyParser.urlencoded({ limit: '1mb', extended: true })); - server.use(function(req, res, next) { - for (const key in req.query) - { + server.use(function (req, res, next) { + for (const key in req.query) { req.query[key.toLowerCase()] = req.query[key]; } next(); @@ -118,13 +117,14 @@ export class GBServer { }); process.on('uncaughtException', (err, p) => { - GBLog.error(`UNCAUGHT_EXCEPTION: ${JSON.stringify(err, Object.getOwnPropertyNames(err))}`); + GBLogEx.error(0, `GBEXCEPTION: ${GBUtil.toYAML(JSON.stringify(err, Object.getOwnPropertyNames(err)))}`); }); process.on('unhandledRejection', (err, p) => { - GBLog.error(`UNHANDLED_REJECTION: ${JSON.stringify(err, Object.getOwnPropertyNames(err))}`); - }); - // Creates working directory. + GBLogEx.error(0,`GBREJECTION: ${GBUtil.toYAML(JSON.stringify(err, Object.getOwnPropertyNames(err)))}`); + }); + + // Creates working directory. process.env.PWD = process.cwd(); const workDir = Path.join(process.env.PWD, 'work'); @@ -134,7 +134,6 @@ export class GBServer { const mainCallback = () => { (async () => { - try { GBLogEx.info(0, `Now accepting connections on ${port}...`); process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; @@ -163,7 +162,6 @@ export class GBServer { GBServer.globals.publicAddress = serverAddress; } - // Creates a boot instance or load it from storage. let runOnce = false; @@ -237,13 +235,11 @@ export class GBServer { GBServer.globals.minService = minService; await minService.buildMin(instances); - server.all('*', async (req, res, next) => { - const host = req.headers.host; if (req.originalUrl.startsWith('/logs')) { - if (process.env.ENABLE_WEBLOG === "true") { + if (process.env.ENABLE_WEBLOG === 'true') { const admins = { admin: { password: process.env.ADMIN_PASS } }; @@ -258,7 +254,6 @@ export class GBServer { await GBSSR.ssrFilter(req, res, next); } } else { - // Setups unsecure http redirect. const proxy = httpProxy.createProxyServer({}); @@ -285,24 +280,22 @@ export class GBServer { })(); }; - if (process.env.CERTIFICATE_PFX) { - const server1 = http.createServer((req, res) => { - const host = req.headers.host.startsWith('www.') ? - req.headers.host.substring(4) : req.headers.host; + const host = req.headers.host.startsWith('www.') ? req.headers.host.substring(4) : req.headers.host; - res.writeHead(301, { - Location: "https://" + host + req.url - - }).end(); + res + .writeHead(301, { + Location: 'https://' + host + req.url + }) + .end(); }); server1.listen(80); const options1 = { passphrase: process.env.CERTIFICATE_PASSPHRASE, pfx: fs.readFileSync(process.env.CERTIFICATE_PFX), - ca: fs.existsSync(process.env.CERTIFICATE_CA)? fs.readFileSync(process.env.CERTIFICATE_CA):null + ca: fs.existsSync(process.env.CERTIFICATE_CA) ? fs.readFileSync(process.env.CERTIFICATE_CA) : null }; const httpsServer = https.createServer(options1, server).listen(port, mainCallback); @@ -323,8 +316,7 @@ export class GBServer { break; } } - } - else { + } else { server.listen(port, mainCallback); } } diff --git a/src/util.ts b/src/util.ts index cca189ea..ac3cb70e 100644 --- a/src/util.ts +++ b/src/util.ts @@ -5,7 +5,7 @@ | ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ | | █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ | | | -| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. | +| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. | | Licensed under the AGPL-3.0. | | | | According to our dual licensing model, this program can be used either | @@ -21,7 +21,7 @@ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | GNU Affero General Public License for more details. | | | -| "General Bots" is a registered trademark of pragmatismo.cloud. | +| "General Bots" is a registered trademark of pragmatismo.cloud. | | The licensing of the program under the AGPLv3 does not imply a | | trademark license. Therefore any rights, title and interest in | | our trademarks remain entirely with us. | @@ -33,66 +33,65 @@ */ 'use strict'; - +import * as YAML from 'yaml'; export class GBUtil { - - public static repeat (chr, count) { - let str = ""; - for (let x = 0; x < count; x++) { str += chr }; + public static repeat(chr, count) { + let str = ''; + for (let x = 0; x < count; x++) { + str += chr; + } return str; } - public static padL (value, width, pad) { - if (!width || width < 1) - return value; + public static padL(value, width, pad) { + if (!width || width < 1) return value; - if (!pad) pad = " "; - const length = width - value.length + if (!pad) pad = ' '; + const length = width - value.length; if (length < 1) return value.substr(0, width); return (GBUtil.repeat(pad, length) + value).substr(0, width); } - public static padR (value, width, pad) { - if (!width || width < 1) - return value; + + public static padR(value, width, pad) { + if (!width || width < 1) return value; - if (!pad) pad = " "; - const length = width - value.length + if (!pad) pad = ' '; + const length = width - value.length; if (length < 1) value.substr(0, width); return (value + GBUtil.repeat(pad, length)).substr(0, width); } + public static toYAML(json) { + const doc = new YAML.Document(); + doc.contents = json; + return doc.toString(); + } public static sleep(ms) { return new Promise(resolve => { setTimeout(resolve, ms); }); - }; + } public static caseInsensitive(listOrRow) { - - if (!listOrRow) { - + if (!listOrRow || typeof listOrRow !== 'object') { return listOrRow; - }; - - const lowercase = (oldKey) => typeof oldKey === 'string' ? oldKey.toLowerCase() : oldKey; - - const createCaseInsensitiveProxy = (obj) => { + } + const lowercase = oldKey => (typeof oldKey === 'string' ? oldKey.toLowerCase() : oldKey); + const createCaseInsensitiveProxy = obj => { const propertiesMap = new Map(Object.keys(obj).map(propKey => [lowercase(propKey), obj[propKey]])); const caseInsensitiveGetHandler = { get: (target, property) => propertiesMap.get(lowercase(property)) }; return new Proxy(obj, caseInsensitiveGetHandler); }; - - if (listOrRow.length) { + if (Array.isArray(listOrRow)) { return listOrRow.map(row => createCaseInsensitiveProxy(row)); } else { return createCaseInsensitiveProxy(listOrRow); } - }; - -} \ No newline at end of file + } +}