UNPKG

@softchef/cdk-iot-device-management

Version:

IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.

97 lines (96 loc) 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeHttpHandler = void 0; const protocol_http_1 = require("@aws-sdk/protocol-http"); const querystring_builder_1 = require("@aws-sdk/querystring-builder"); const http_1 = require("http"); const https_1 = require("https"); const constants_1 = require("./constants"); const get_transformed_headers_1 = require("./get-transformed-headers"); const set_connection_timeout_1 = require("./set-connection-timeout"); const set_socket_timeout_1 = require("./set-socket-timeout"); const write_request_body_1 = require("./write-request-body"); class NodeHttpHandler { constructor(options) { this.metadata = { handlerProtocol: "http/1.1" }; if (typeof options === "function") { this.configProvider = async () => { return this.resolveDefaultConfig(await options()); }; } else { this.config = this.resolveDefaultConfig(options); } } resolveDefaultConfig(options) { const { connectionTimeout, socketTimeout, httpAgent, httpsAgent } = options || {}; const keepAlive = true; const maxSockets = 50; return { connectionTimeout, socketTimeout, httpAgent: httpAgent || new http_1.Agent({ keepAlive, maxSockets }), httpsAgent: httpsAgent || new https_1.Agent({ keepAlive, maxSockets }), }; } destroy() { var _a, _b, _c, _d; (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.httpAgent) === null || _b === void 0 ? void 0 : _b.destroy(); (_d = (_c = this.config) === null || _c === void 0 ? void 0 : _c.httpsAgent) === null || _d === void 0 ? void 0 : _d.destroy(); } async handle(request, { abortSignal } = {}) { if (!this.config && this.configProvider) { this.config = await this.configProvider(); } return new Promise((resolve, reject) => { if (!this.config) { throw new Error("Node HTTP request handler config is not resolved"); } if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) { const abortError = new Error("Request aborted"); abortError.name = "AbortError"; reject(abortError); return; } const isSSL = request.protocol === "https:"; const queryString = querystring_builder_1.buildQueryString(request.query || {}); const nodeHttpsOptions = { headers: request.headers, host: request.hostname, method: request.method, path: queryString ? `${request.path}?${queryString}` : request.path, port: request.port, agent: isSSL ? this.config.httpsAgent : this.config.httpAgent, }; const requestFunc = isSSL ? https_1.request : http_1.request; const req = requestFunc(nodeHttpsOptions, (res) => { const httpResponse = new protocol_http_1.HttpResponse({ statusCode: res.statusCode || -1, headers: get_transformed_headers_1.getTransformedHeaders(res.headers), body: res, }); resolve({ response: httpResponse }); }); req.on("error", (err) => { if (constants_1.NODEJS_TIMEOUT_ERROR_CODES.includes(err.code)) { reject(Object.assign(err, { name: "TimeoutError" })); } else { reject(err); } }); set_connection_timeout_1.setConnectionTimeout(req, reject, this.config.connectionTimeout); set_socket_timeout_1.setSocketTimeout(req, reject, this.config.socketTimeout); if (abortSignal) { abortSignal.onabort = () => { req.abort(); const abortError = new Error("Request aborted"); abortError.name = "AbortError"; reject(abortError); }; } write_request_body_1.writeRequestBody(req, request); }); } } exports.NodeHttpHandler = NodeHttpHandler;