UNPKG

@webfaas/webfaas-core

Version:

WebFaaS Framework - Core

140 lines 7.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClientHTTP = void 0; const http = require("http"); const https = require("https"); const urlParse = require("url"); const ClientHTTPConfig_1 = require("./ClientHTTPConfig"); const Log_1 = require("../Log/Log"); const ILog_1 = require("../Log/ILog"); const WebFaasError_1 = require("../WebFaasError/WebFaasError"); class ClientHTTP { constructor(config, log) { this.listHttpAgent = new Map(); this.listHttpsAgent = new Map(); this.log = log || new Log_1.Log(); if (config) { this.config = config; } else { this.config = new ClientHTTPConfig_1.ClientHTTPConfig(); } } /** * destroy all agents */ destroy() { Array.from(this.listHttpAgent.values()).forEach((agent) => { agent.destroy(); }); this.listHttpAgent.clear(); Array.from(this.listHttpsAgent.values()).forEach((agent) => { agent.destroy(); }); this.listHttpsAgent.clear(); } /** * request http/https * @param url remote url * @param method methods http. ex: GET | POST | PUT | DELETE * @param headers headers http */ request(url, method, dataRequestBuffer, headers, invokeContext) { var selfLog = this.log; return new Promise((resolve, reject) => { try { var dataResponse = []; var urlParsedObj = urlParse.parse(url); var clientHTTPResponseObj = {}; var clientRequest; var startTime = new Date().getTime(); var handleResponse = function (responseHTTP) { responseHTTP.on("data", function (chunk) { dataResponse.push(chunk); }); responseHTTP.on("end", function () { try { clientHTTPResponseObj.statusCode = responseHTTP.statusCode || 0; clientHTTPResponseObj.headers = responseHTTP.headers; clientHTTPResponseObj.data = Buffer.concat(dataResponse); selfLog.write(ILog_1.LogLevelEnum.INFO, "request", ILog_1.LogCodeEnum.PROCESS.toString(), "reponse", { url: url, method: method, statusCode: clientHTTPResponseObj.statusCode, size: clientHTTPResponseObj.data.length, responseTime: (new Date().getTime() - startTime) }, __filename, invokeContext); resolve(clientHTTPResponseObj); } catch (errTry) { selfLog.writeError("request", errTry, { method: method, url: url }, __filename, invokeContext); reject(new WebFaasError_1.WebFaasError.ClientHttpError(errTry, url, method)); } }); }; if (method) { method = method.toUpperCase(); } else { method = "GET"; } if (urlParsedObj.protocol === "https:") { let httpsRequestOption = {}; httpsRequestOption.timeout = this.config.timeout; httpsRequestOption.method = method; if (headers) { httpsRequestOption.headers = headers; } httpsRequestOption.rejectUnauthorized = this.config.rejectUnauthorized; httpsRequestOption.key = this.config.key; httpsRequestOption.cert = this.config.cert; httpsRequestOption.pfx = this.config.pfx; httpsRequestOption.ca = this.config.ca; httpsRequestOption.host = urlParsedObj.host; httpsRequestOption.hostname = urlParsedObj.hostname; httpsRequestOption.path = urlParsedObj.path; httpsRequestOption.port = urlParsedObj.port; var httpsAgent = this.listHttpsAgent.get(httpsRequestOption.hostname || ""); if (!httpsAgent) { httpsAgent = new https.Agent({ keepAlive: this.config.keepAlive, maxSockets: this.config.maxSockets }); this.listHttpsAgent.set(httpsRequestOption.hostname || "", httpsAgent); selfLog.write(ILog_1.LogLevelEnum.INFO, "request", ILog_1.LogCodeEnum.PROCESS.toString(), "new agent", { protocol: "https", hostname: httpsRequestOption.hostname, keepAlive: this.config.keepAlive, maxSockets: this.config.maxSockets }, __filename, invokeContext); } httpsRequestOption.agent = httpsAgent; clientRequest = https.request(httpsRequestOption, handleResponse); } else { let httpRequestOption = {}; httpRequestOption.timeout = this.config.timeout; httpRequestOption.method = method; if (headers) { httpRequestOption.headers = headers; } httpRequestOption.host = urlParsedObj.host; httpRequestOption.hostname = urlParsedObj.hostname; httpRequestOption.path = urlParsedObj.path; httpRequestOption.port = urlParsedObj.port; var httpAgent = this.listHttpAgent.get(httpRequestOption.hostname || ""); if (!httpAgent) { httpAgent = new http.Agent({ keepAlive: this.config.keepAlive, maxSockets: this.config.maxSockets }); this.listHttpAgent.set(httpRequestOption.hostname || "", httpAgent); selfLog.write(ILog_1.LogLevelEnum.INFO, "request", ILog_1.LogCodeEnum.PROCESS.toString(), "new agent", { protocol: "http", hostname: httpRequestOption.hostname, keepAlive: this.config.keepAlive, maxSockets: this.config.maxSockets }, __filename, invokeContext); } httpRequestOption.agent = httpAgent; clientRequest = http.request(httpRequestOption, handleResponse); } clientRequest.on("timeout", function () { clientRequest.abort(); }); clientRequest.on("error", function (err) { selfLog.writeError("request", err, { method: method, url: url }, __filename, invokeContext); reject(new WebFaasError_1.WebFaasError.ClientHttpError(err, url, method)); }); if (dataRequestBuffer) { clientRequest.write(dataRequestBuffer); } clientRequest.end(); } catch (errTry) { selfLog.writeError("request", errTry, { method: method, url: url }, __filename, invokeContext); reject(new WebFaasError_1.WebFaasError.ClientHttpError(errTry, url, method)); } }); } } exports.ClientHTTP = ClientHTTP; //# sourceMappingURL=ClientHTTP.js.map