UNPKG

@webfaas/webfaas-plugin-endpoint-http

Version:
114 lines 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EndPointHTTP = void 0; const http = require("http"); const https = require("https"); const webfaas_core_1 = require("@webfaas/webfaas-core"); const EndPointHTTPConfig_1 = require("./EndPointHTTPConfig"); const ILog_1 = require("@webfaas/webfaas-core/lib/Log/ILog"); const SendMessageRest_1 = require("./rest/SendMessageRest"); const SendMessageJsonRpc_1 = require("./rest/SendMessageJsonRpc"); class EndPointHTTP { constructor(core, config) { this.server = null; this.core = core; this.config = config; this.log = core.getLog(); this.onProcessHTTP = this.onProcessHTTP.bind(this); this.sendMessageRest = new SendMessageRest_1.SendMessageRest(this); this.sendMessageJsonRpc = new SendMessageJsonRpc_1.SendMessageJsonRpc(this); } buildHeaders(contentType) { var headers = {}; headers["server"] = "webfaas"; headers["content-type"] = contentType || "text/plain"; return headers; } getConfig() { return this.config; } getLog() { return this.log; } getCore() { return this.core; } writeEnd(response, statusCode, headers, chunk) { response.writeHead(statusCode, headers); response.write(chunk); response.end(); } onProcessHTTP(request, response) { let bodyList = []; let requestContentType = request.headers["content-type"] || ""; try { request.on("data", function (chunk) { bodyList.push(chunk); }).on("end", () => { try { let body = Buffer.concat(bodyList); if (requestContentType === "application/json-rpc") { this.sendMessageJsonRpc.processRequest(request, response, body); } else { this.sendMessageRest.processRequest(request, response, body); } } catch (errTry) { this.writeEnd(response, 500, this.buildHeaders(), "Internal Server Error"); } }); } catch (errTry) { this.log.writeError("onProcessHTTP", errTry, undefined, __filename); this.writeEnd(response, 500, this.buildHeaders(), "Internal Server Error"); } } start() { return new Promise((resolve, reject) => { try { if (this.config.type === EndPointHTTPConfig_1.EndPointHTTPConfigTypeEnum.HTTPS) { if (this.config.httpConfig) { this.server = https.createServer(this.config.httpConfig, this.onProcessHTTP); } else { this.server = https.createServer(this.onProcessHTTP); } } else { if (this.config.httpConfig) { this.server = http.createServer(this.config.httpConfig, this.onProcessHTTP); } else { this.server = http.createServer(this.onProcessHTTP); } } let opt = { port: this.config.port, hostname: this.config.hostname }; this.server.listen(opt, () => { this.log.write(webfaas_core_1.LogLevelEnum.INFO, "start", ILog_1.LogCodeEnum.PROCESS, "Server started", opt, __filename); resolve(); }); } catch (errTry) { this.log.writeError("start", errTry, undefined, __filename); reject(errTry); } }); } stop() { return new Promise((resolve, reject) => { if (this.server) { this.server.close(() => { this.log.write(webfaas_core_1.LogLevelEnum.INFO, "stop", ILog_1.LogCodeEnum.PROCESS, "Server stoped", undefined, __filename); this.server = null; resolve(); }); } else { resolve(); } }); } } exports.EndPointHTTP = EndPointHTTP; //# sourceMappingURL=EndPointHTTP.js.map