UNPKG

aws-sdk-v3-proxy

Version:

A wrapper for adding proxy support to AWS SDK v3 clients

57 lines (56 loc) 3.04 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addProxyToClient = exports.getHttpsProxy = exports.getHttpProxy = void 0; const node_http_handler_1 = require("@smithy/node-http-handler"); const hpagent_1 = require("hpagent"); const getHttpProxy = () => process.env.http_proxy || process.env.HTTP_PROXY || ''; exports.getHttpProxy = getHttpProxy; const getHttpsProxy = () => process.env.https_proxy || process.env.HTTPS_PROXY || ''; exports.getHttpsProxy = getHttpsProxy; const addProxyToClient = (client, _a = {}) => { var { debug = false, httpsOnly = false, throwOnNoProxy = true, agentOptions = {}, httpProxy = (0, exports.getHttpProxy)(), httpsProxy = (0, exports.getHttpsProxy)() } = _a, opts = __rest(_a, ["debug", "httpsOnly", "throwOnNoProxy", "agentOptions", "httpProxy", "httpsProxy"]); const httpAgent = httpProxy ? new hpagent_1.HttpsProxyAgent(Object.assign({ proxy: httpProxy }, agentOptions)) : undefined; const httpsAgent = httpsProxy ? new hpagent_1.HttpsProxyAgent(Object.assign({ proxy: httpsProxy }, agentOptions)) : undefined; const log = debug ? console.log : () => null; if (httpProxy && httpsProxy) { if (httpsOnly) { log(`Setting https proxy to ${httpsProxy} (httpsOnly enabled with both https and http found in env)`); client.config.requestHandler = new node_http_handler_1.NodeHttpHandler(Object.assign({ httpAgent: httpsAgent, httpsAgent }, opts)); } else { log(`Setting http proxy to ${httpProxy} and https proxy to ${httpsProxy}`); client.config.requestHandler = new node_http_handler_1.NodeHttpHandler(Object.assign({ httpAgent, httpsAgent }, opts)); } return client; } if (httpProxy && !httpsOnly) { log(`Setting http proxy to ${httpProxy}`); client.config.requestHandler = new node_http_handler_1.NodeHttpHandler(Object.assign({ httpAgent, httpsAgent: httpAgent }, opts)); } else if (httpsProxy) { log(`Setting https proxy to ${httpsProxy}`); client.config.requestHandler = new node_http_handler_1.NodeHttpHandler(Object.assign({ httpAgent: httpsAgent, httpsAgent }, opts)); } else if (throwOnNoProxy) { log('No proxy found in env, and throwOnNoProxy is set to true, throwing error'); throw new Error('Unable to add proxy to AWS SDK client. No proxy found in process.env'); } return client; }; exports.addProxyToClient = addProxyToClient;