Skip to content

Commit

Permalink
improvements for BackendMicroservice and generic constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmachuca committed Apr 30, 2023
1 parent ceac02e commit c2088ed
Showing 1 changed file with 75 additions and 33 deletions.
108 changes: 75 additions & 33 deletions src/QCObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,21 +1004,24 @@
_methods_(self.__definition).map(function (m) {
self[m.name] = m.bind(self);
});

if (typeof self.__definition === "undefined" || (!Object.hasOwnProperty.call(self.__definition, "body")) || typeof self.__definition.body === "undefined") {
try {
if (isBrowser) {
self["body"] = _DOMCreateElement(self.__definition.__classType);
} else {

if (!!self["body"]){
if (typeof self.__definition === "undefined" || (!Object.hasOwnProperty.call(self.__definition, "body")) || typeof self.__definition.body === "undefined") {
try {
if (isBrowser) {
self["body"] = _DOMCreateElement(self.__definition.__classType);
} else {
self["body"] = {};
}
} catch (e) {
self["body"] = {};
}
} catch (e) {
self["body"] = {};
}
} else if (Object.hasOwnProperty.call(self.__definition, "body")) {
self["body"] = self.__definition.body;
} else if (Object.hasOwnProperty.call(self.__definition, "body")) {
self["body"] = self.__definition.body;
}
}


try {
if (typeof self.__new__ === "function") {
self.__new__.call(self, _o_);
Expand Down Expand Up @@ -3980,17 +3983,19 @@
constructor ({
domain= _domain_,
basePath= _basePath_,
body= null,
body = null,
stream= null,
request= null
}){
super(...arguments);

var o = this;

logger.debug("Executing BackendMicroservice ");
logger.debug("Initializing BackendMicroservice...");
let microservice = this;
microservice.body = null;
if (typeof this.body === "undefined") {
this.body = null;
}
if (typeof body !== "undefined"){
this.body = body;
}
this.cors();
microservice.stream = stream;
stream.on("data", (data) => {
Expand Down Expand Up @@ -4025,6 +4030,7 @@

cors() {
if (this.route.cors) {
logger.debug("Validating CORS...");
let {
allow_origins,
allow_credentials,
Expand All @@ -4035,37 +4041,54 @@
if (typeof microservice.headers !== "object") {
microservice.headers = {};
}
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) {
// 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();
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(",");
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(",");
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");
}
}

Expand All @@ -4075,6 +4098,7 @@
}

get(formData) {
logger.debug(`[BackendMicroservice.get] Data received: ${_DataStringify(formData)}`);
this.done();
}

Expand Down Expand Up @@ -4108,27 +4132,45 @@

finishWithBody(stream) {
try {
stream.write(_DataStringify(this.body));
logger.debug("[BackendMicroservice.finishWithBody] Ending the stream...");
logger.debug(`[BackendMicroservice.finishWithBody] type of body is: ${typeof this.body}`);
if (typeof this.body !== "string"){
this.body = _DataStringify(this.body);
}
logger.debug(`[BackendMicroservice.finishWithBody] \n body: ${this.body} `);
stream.write(this.body);
stream.end();
logger.debug(`[BackendMicroservice.finishWithBody] Stream ended.`);
} catch (e) {
logger.debug("Something wrong writing the response for microservice" + e.toString());
logger.debug(`[BackendMicroservice.finishWithBody] Something went wrong ending the stream: ${e}`);
}
}

done() {
logger.debug(`[BackendMicroservice.done] Finalizing the response...`);
var microservice = this;
var stream = microservice.stream;
try {
stream.respond(microservice.headers);
logger.debug(`[BackendMicroservice.done] Sending response headers...`);
if (microservice.route.responseHeaders){
logger.debug(`[BackendMicroservice.done] Response headers present: ${Object.keys(microservice.route.responseHeaders)}`);
stream.respond(microservice.route.responseHeaders);
} else {
throw Error(`[BackendMicroservice.done] No headers present.`);
}
} catch (e) {
logger.debug(e.toString());
logger.debug(`[BackendMicroservice.done] Something went wrong sending response headers: ${e}`);
}
if (microservice.body !== null) {
try {
logger.debug(`[BackendMicroservice.done] A body of message is present. Finalizing the response...`);
microservice.finishWithBody.call(microservice, stream);
} catch (e) {
logger.debug(e.toString());
logger.debug(`[BackendMicroservice.done] Something went wrong finalizing the response: ${e}`);
}
} else {
logger.debug("[BackendMicroservice.done] No body present. Ending stream...");
stream.end();
}
}

Expand Down

0 comments on commit c2088ed

Please sign in to comment.