qcobjects-cli
Advanced tools
Comparing version 2.4.47 to 2.4.49
{ | ||
"name": "qcobjects-cli", | ||
"version": "2.4.47", | ||
"version": "2.4.49", | ||
"description": "qcobjects cli command line tool", | ||
@@ -5,0 +5,0 @@ "main": "src/index.cjs", |
@@ -34,19 +34,21 @@ /** | ||
const path = require("path"); | ||
const absolutePath = path.resolve( __dirname, "./" ); | ||
const absolutePath = path.resolve(__dirname, "./"); | ||
const fs = require("fs"); | ||
const mime = require("mime"); | ||
require(absolutePath+"/org.quickcorp.qcobjects.main.file.js"); | ||
require(absolutePath+ "/org.qcobjects.common.pipelog.js"); | ||
require(absolutePath + "/org.quickcorp.qcobjects.main.file.js"); | ||
require(absolutePath + "/org.qcobjects.common.pipelog.js"); | ||
let ImportMicroservice = function (microservicePackage){ | ||
var standardPath = findPackageNodePath(microservicePackage) || findPackageNodePath(microservicePackage+".js"); | ||
if (standardPath !== null){ | ||
Import (microservicePackage); | ||
let ImportMicroservice = function (microservicePackage) { | ||
var _ret_; | ||
var standardPath = findPackageNodePath(microservicePackage) || findPackageNodePath(microservicePackage + ".js"); | ||
if (standardPath !== null) { | ||
_ret_ = Import(microservicePackage); | ||
} else { | ||
Import (absolutePath+"/backend/"+microservicePackage); | ||
_ret_ = Import(absolutePath + "/backend/" + microservicePackage); | ||
} | ||
return _ret_; | ||
}; | ||
Package("org.quickcorp.qcobjects.main.http.server",[ | ||
@@ -60,3 +62,2 @@ | ||
stream=null, | ||
server=null, | ||
request=null | ||
@@ -66,5 +67,12 @@ }){ | ||
logger.debug("Executing Legacy HTTP BackendMicroservice "); | ||
logger.debug("Initializing Legacy BackendMicroservice..."); | ||
let microservice = this; | ||
if (typeof this.body === "undefined") { | ||
this.body = null; | ||
} | ||
if (typeof body !== "undefined"){ | ||
this.body = body; | ||
} | ||
this.cors(); | ||
microservice.stream = stream; | ||
microservice.req.on("data", (data) => { | ||
@@ -75,3 +83,3 @@ // data from POST, GET | ||
}; | ||
if (supportedMethods.hasOwnProperty.call(supportedMethods,requestMethod)) { | ||
if (Object.hasOwnProperty.call(supportedMethods,requestMethod)) { | ||
supportedMethods[requestMethod].call(microservice,data); | ||
@@ -92,3 +100,3 @@ } | ||
}; | ||
if (supportedMethods.hasOwnProperty.call(supportedMethods,requestMethod)) { | ||
if (Object.hasOwnProperty.call(supportedMethods,requestMethod)) { | ||
supportedMethods[requestMethod].call(microservice); | ||
@@ -100,40 +108,63 @@ } | ||
cors(){ | ||
if (this.route.cors){ | ||
let {allow_origins,allow_credentials,allow_methods,allow_headers} = this.route.cors; | ||
cors() { | ||
if (this.route.cors) { | ||
logger.debug("Validating CORS..."); | ||
let { | ||
allow_origins, | ||
allow_credentials, | ||
allow_methods, | ||
allow_headers | ||
} = this.route.cors; | ||
var microservice = this; | ||
if (typeof microservice.headers !== "object"){ | ||
if (typeof microservice.headers !== "object") { | ||
microservice.headers = {}; | ||
} | ||
if (typeof allow_origins !== "undefined"){ | ||
if (typeof microservice.route.responseHeaders !== "object") { | ||
microservice.route.responseHeaders = {}; | ||
} | ||
if (typeof allow_origins !== "undefined") { | ||
logger.debug("CORS: allow_origins available. Validating origins..."); | ||
// an example of allow_origins is ['https://example.com','http://www.example.com'] | ||
if (allow_origins =="*" || (typeof microservice.request.headers.origin == "undefined") || [...allow_origins].indexOf(microservice.request.headers.origin)!== -1){ | ||
if (allow_origins === "*" || (typeof microservice.request.headers.origin === "undefined") || [...allow_origins].indexOf(microservice.request.headers.origin) !== -1) { | ||
// for compatibility with all browsers allways return a wildcard when the origin is allowed | ||
microservice.headers["Access-Control-Allow-Origin"] = "*"; | ||
logger.debug("CORS: Adding header Access-Control-Allow-Origin=*"); | ||
microservice.route.responseHeaders["Access-Control-Allow-Origin"] = "*"; | ||
} else { | ||
logger.debug("Origin is not allowed: " + microservice.request.headers.origin); | ||
logger.debug("Forcing to finish the response..."); | ||
logger.debug("CORS: Origin is not allowed: " + microservice.request.headers.origin); | ||
logger.debug("CORS: Forcing to finish the response..."); | ||
this.body = {}; | ||
try { | ||
this.done(); | ||
} catch (e){} | ||
} catch (e) { | ||
logger.debug(`It was not possible to finish the call to the microservice: ${e}`); | ||
} | ||
} | ||
} else { | ||
microservice.headers["Access-Control-Allow-Origin"] = "*"; | ||
logger.debug("CORS: no allow_origins available. Allowing all origins..."); | ||
logger.debug("CORS: Adding header Access-Control-Allow-Origin=*"); | ||
microservice.route.responseHeaders["Access-Control-Allow-Origin"] = "*"; | ||
} | ||
if (typeof allow_credentials !== "undefined"){ | ||
microservice.headers["Access-Control-Allow-Credentials"] = allow_credentials.toString(); | ||
if (typeof allow_credentials !== "undefined") { | ||
logger.debug(`CORS: allow_credentials present. Allowing ${allow_credentials}...`); | ||
microservice.route.responseHeaders["Access-Control-Allow-Credentials"] = allow_credentials.toString(); | ||
} else { | ||
microservice.headers["Access-Control-Allow-Credentials"] = "true"; | ||
logger.debug("CORS: No allow_credentials present. Allowing all credentials."); | ||
microservice.route.responseHeaders["Access-Control-Allow-Credentials"] = "true"; | ||
} | ||
if (typeof allow_methods !== "undefined"){ | ||
microservice.headers["Access-Control-Allow-Methods"] = [...allow_methods].join(","); | ||
if (typeof allow_methods !== "undefined") { | ||
logger.debug(`CORS: allow_methods present. Allowing ${allow_methods}...`); | ||
microservice.route.responseHeaders["Access-Control-Allow-Methods"] = [...allow_methods].join(","); | ||
} else { | ||
microservice.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS, POST"; | ||
logger.debug("CORS: No allow_methods present. Allowing only GET, OPTIONS and POST"); | ||
microservice.route.responseHeaders["Access-Control-Allow-Methods"] = "GET, OPTIONS, POST"; | ||
} | ||
if (typeof allow_headers !== "undefined"){ | ||
microservice.headers["Access-Control-Allow-Headers"] = [...allow_headers].join(","); | ||
if (typeof allow_headers !== "undefined") { | ||
logger.debug(`CORS: allow_headers present. Allowing ${allow_headers}...`); | ||
microservice.route.responseHeaders["Access-Control-Allow-Headers"] = [...allow_headers].join(","); | ||
} else { | ||
microservice.headers["Access-Control-Allow-Headers"] = "*"; | ||
logger.debug(`CORS: No allow_headers present. Allowing all headers...`); | ||
microservice.route.responseHeaders["Access-Control-Allow-Headers"] = "*"; | ||
} | ||
} else { | ||
logger.debug("No CORS validation available. You can specify cors in CONFIG.backend.routes[].cors"); | ||
} | ||
@@ -154,3 +185,4 @@ } | ||
} catch (e){ | ||
logger.debug("Something wrong writing the response for microservice"+e.toString()); | ||
logger.debug(`Something wrong writing the response for microservice: ${e}`); | ||
throw Error(e); | ||
} | ||
@@ -164,4 +196,4 @@ } | ||
} catch (e){ | ||
logger.debug("Something went wront while sending headers in http..."); | ||
logger.debug(e.toString()); | ||
logger.debug(`Something went wront while sending headers in http... ${e}`); | ||
throw Error(e); | ||
} | ||
@@ -180,13 +212,13 @@ if (microservice.body != null){ | ||
headers={ | ||
":status": 200, | ||
"status": 200, | ||
"content-type": "text/html" | ||
}, | ||
body="", | ||
request=null, | ||
fileDispatcher=null, | ||
stream=null | ||
}){ | ||
body = "", | ||
request = null, | ||
fileDispatcher = null, | ||
stream = null | ||
}) { | ||
super(...arguments); | ||
var self = this; | ||
self.stream = self.stream; | ||
self.stream = stream; | ||
self._generateResponse(); | ||
@@ -231,2 +263,12 @@ | ||
readStream.on("error", function(err) { | ||
const headers = { | ||
"status": 500, | ||
"content-type": mime.getType(fileName) | ||
}; | ||
stream.setHeader("content-type", headers["content-type"]); | ||
stream.setHeader("status", headers["status"]); | ||
stream.write(`<h1>500 - INTERNAL SERVER ERROR</h1> | ||
<p>${err}</p> | ||
`); | ||
stream.end(err); | ||
@@ -238,8 +280,10 @@ }); | ||
const headers = { | ||
":status": 404, | ||
"content-type": "text/html" | ||
"status": 404, | ||
"content-type": mime.getType(fileName) | ||
}; | ||
stream.setHeader("content-type", headers["content-type"]); | ||
stream.setHeader("status", headers["status"]); | ||
stream.write("<h1>404 - FILE NOT FOUND</h1>"); | ||
stream.on("close", () => { | ||
console.log("closing file", fileName); | ||
logger.debug("closing file " + fileName); | ||
}); | ||
@@ -259,12 +303,10 @@ stream.end(); | ||
if (isTemplate){ | ||
logger.debug("TEMPLATE"); | ||
response.body = body; | ||
// stream.respond(response.headers); | ||
Object.keys(headers).map((header)=>stream.setHeader(header, headers[header])); | ||
stream.write(response.body); | ||
stream.end(); | ||
} else if (headers[":status"]==200){ | ||
response.sendFile(stream,templateURI); | ||
} else if (headers["status"] == 200 || headers[":status"] == 200) { | ||
response.sendFile(stream, templateURI); | ||
} else { | ||
logger.debug("NONE "); | ||
// stream.respond(response.headers); | ||
Object.keys(headers).map((header)=>stream.setHeader(header, headers[header])); | ||
stream.end(); | ||
@@ -280,21 +322,21 @@ } | ||
class HTTPServerRequest extends InheritClass { | ||
constructor ({ | ||
scriptname="", | ||
path="", | ||
method="", | ||
url="", | ||
headers=null, | ||
flags=null, | ||
protocol= null, | ||
slashes= null, | ||
auth= null, | ||
host= null, | ||
port= null, | ||
hostname= null, | ||
hash= null, | ||
search= "", | ||
query= "", | ||
pathname= "", | ||
href= "" | ||
}){ | ||
constructor({ | ||
scriptname = "", | ||
path = "", | ||
method = "", | ||
url = "", | ||
headers = null, | ||
flags = null, | ||
protocol = null, | ||
slashes = null, | ||
auth = null, | ||
host = null, | ||
port = null, | ||
hostname = null, | ||
hash = null, | ||
search = "", | ||
query = "", | ||
pathname = "", | ||
href = "" | ||
}) { | ||
super(...arguments); | ||
@@ -305,12 +347,11 @@ } | ||
class HTTPServer extends InheritClass { | ||
constructor ({ | ||
request=null, | ||
response="", | ||
server=null, | ||
scriptname="", | ||
interceptorInstances=[] | ||
}){ | ||
constructor({ | ||
request = null, | ||
response = "", | ||
server = null, | ||
scriptname = "", | ||
interceptorInstances = [] | ||
}) { | ||
super(...arguments); | ||
let oHTTPServer = this; | ||
const welcometo = "Welcome to \n"; | ||
@@ -327,27 +368,24 @@ const instructions = "QCObjects Legacy HTTPServer \n"; | ||
const http = require("http"); | ||
oHTTPServer.server = http.createServer((req, res) => { | ||
this.server = http.createServer((req, res) => { | ||
logger.debug("Legacy Server Instantiated."); | ||
}); | ||
var server = oHTTPServer.server; | ||
this.server.on("error", (err) => console.error(err)); | ||
server.on("error", (err) => console.error(err)); | ||
if (global.get("backendAvailable")){ | ||
logger.info("Loading backend interceptors..."); | ||
let interceptors = CONFIG.get("backend",{}).interceptors; | ||
if (typeof interceptors !== "undefined"){ | ||
let interceptors = CONFIG.get("backend", {}).interceptors; | ||
if (typeof interceptors !== "undefined") { | ||
logger.info("Backend Interceptors Available"); | ||
interceptors.map(interceptor=>{ | ||
ImportMicroservice (interceptor.microservice); | ||
var interceptorClassFactory = ClassFactory(interceptor.microservice+".Interceptor"); | ||
var interceptorInstance = New(interceptorClassFactory,{ | ||
domain:CONFIG.get("domain"), | ||
basePath:CONFIG.get("basePath"), | ||
projectPath:CONFIG.get("projectPath"), | ||
interceptor:interceptor, | ||
server:server | ||
interceptors.map(interceptor => { | ||
ImportMicroservice(interceptor.microservice); | ||
var interceptorClassFactory = ClassFactory(interceptor.microservice + ".Interceptor"); | ||
var interceptorInstance = New(interceptorClassFactory, { | ||
domain: CONFIG.get("domain"), | ||
basePath: CONFIG.get("basePath"), | ||
projectPath: CONFIG.get("projectPath"), | ||
interceptor: interceptor, | ||
server: this.server | ||
}); | ||
oHTTPServer.interceptorInstances.push(interceptorInstance); | ||
this.interceptorInstances.push(interceptorInstance); | ||
}); | ||
@@ -357,3 +395,3 @@ } | ||
server.on("request", (req, res) => { | ||
this.server.on("request", (req, res) => { | ||
@@ -365,3 +403,3 @@ let request = Object.assign(New(HTTPServerRequest),require("url").parse(req.url)); | ||
this.request.path = req.url; | ||
server.setMaxListeners(9999999999); | ||
this.server.setMaxListeners(9999999999); | ||
CONFIG.set("backendTimeout",CONFIG.get("backendTimeout") || 20000); | ||
@@ -384,11 +422,10 @@ var timeoutHandler = ()=>{ | ||
} | ||
}catch (e){ | ||
logger.debug("An unhandled error occurred during timeout catching..."); | ||
logger.debug(e.message); | ||
} catch (e) { | ||
logger.debug(`An unhandled error occurred during timeout catching: ${e}`); | ||
} | ||
server.removeListener("timeout",timeoutHandler); | ||
this.server.removeListener("timeout",timeoutHandler); | ||
}; | ||
if (!res.destroyed){ | ||
server.setTimeout(CONFIG.get("backendTimeout"), timeoutHandler); | ||
this.server.setTimeout(CONFIG.get("backendTimeout"), timeoutHandler); | ||
} | ||
@@ -405,22 +442,40 @@ | ||
if (global.get("backendAvailable")){ | ||
logger.info("Backend Legacy Microservices Available"); | ||
let routes = CONFIG.get("backend").routes; | ||
let selectedRoute = routes.filter(route=>{let standardRoutePath = route.path.replace(/{(.*?)}/g,"(?<$1>.*)");return (new RegExp(standardRoutePath,"g")).test(request.path);}); | ||
if (selectedRoute.length>0){ | ||
selectedRoute.map(route=>{ | ||
let standardRoutePath = route.path.replace(/{(.*?)}/g,"(?<$1>.*)"); //allowing {param} | ||
let selectedRouteParams = {...[...request.path.matchAll((new RegExp( standardRoutePath ,"g")))][0]["groups"]}; | ||
ImportMicroservice (route.microservice); | ||
var microServiceClassFactory = ClassFactory(route.microservice+".Microservice"); | ||
this.response = New(microServiceClassFactory,{ | ||
domain:CONFIG.get("domain"), | ||
basePath:CONFIG.get("basePath"), | ||
projectPath:CONFIG.get("projectPath"), | ||
route:route, | ||
routeParams:selectedRouteParams, | ||
server:server, | ||
stream:res, | ||
req:req, | ||
request:request | ||
if (global.get("backendAvailable")) { | ||
logger.info("Backend Legacy Microservices Available..."); | ||
logger.info("Loading backend routes..."); | ||
let routes = CONFIG.get("backend", {}).routes; | ||
let selectedRoute = routes.filter(route => { | ||
let standardRoutePath = route.path.replace(/{(.*?)}/g, "(?<$1>.*)"); | ||
return (new RegExp(standardRoutePath, "g")).test(request.path); | ||
}); | ||
if (selectedRoute.length > 0) { | ||
selectedRoute.map(route => { | ||
let standardRoutePath = route.path.replace(/{(.*?)}/g, "(?<$1>.*)"); //allowing {param} | ||
console.log(standardRoutePath); | ||
let selectedRouteParams = { | ||
...[...request.path.matchAll((new RegExp(standardRoutePath, "g")))][0]["groups"] | ||
}; | ||
ImportMicroservice(route.microservice).then (()=>{ | ||
logger.debug(`Trying to execute ${route.microservice + ".Microservice"}...`); | ||
var microServiceClassFactory = ClassFactory(route.microservice + ".Microservice"); | ||
if (typeof microServiceClassFactory !== "undefined"){ | ||
const server = this.server; | ||
this.response = New(microServiceClassFactory, { | ||
domain: CONFIG.get("domain"), | ||
basePath: CONFIG.get("basePath"), | ||
projectPath: CONFIG.get("projectPath"), | ||
route: route, | ||
routeParams: selectedRouteParams, | ||
server: server, | ||
stream: res, | ||
req:req, | ||
request: request | ||
}); | ||
} else { | ||
throw Error (`${route.microservice + ".Microservice"} not defined.`); | ||
} | ||
}).catch (e=> { | ||
throw Error (e); | ||
}); | ||
@@ -430,4 +485,8 @@ }); | ||
this.response = New(HTTPServerResponse,{ | ||
server:server, | ||
domain: CONFIG.get("domain"), | ||
basePath: CONFIG.get("basePath"), | ||
projectPath: CONFIG.get("projectPath"), | ||
server:this.server, | ||
stream:res, | ||
req:req, | ||
request:this.request | ||
@@ -441,4 +500,5 @@ }); | ||
this.response = New(HTTPServerResponse,{ | ||
server:server, | ||
server:this.server, | ||
stream:res, | ||
req:req, | ||
request:this.request | ||
@@ -458,5 +518,5 @@ }); | ||
var ifaces = os.networkInterfaces(); | ||
Object.keys(ifaces).forEach(function (iface){ | ||
ifaces[iface].map(function (ipGroup){ | ||
_ret_ += iface +": " + (new PipeLog()).pipe(ipGroup)+"\n"; | ||
Object.keys(ifaces).forEach(function (iface) { | ||
ifaces[iface].map(function (ipGroup) { | ||
_ret_ += iface + ": " + (new PipeLog()).pipe(ipGroup) + "\n"; | ||
}); | ||
@@ -463,0 +523,0 @@ }); |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
199643
4450