This commit is contained in:
2022-09-30 05:39:11 +00:00
parent 41ee9463ae
commit 4687fa49bc
11418 changed files with 1312504 additions and 0 deletions

View File

@ -0,0 +1,175 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _http = _interopRequireDefault(require("http"));
var _https = _interopRequireDefault(require("https"));
var _boolean = require("boolean");
var _semver = _interopRequireDefault(require("semver"));
var _Logger = _interopRequireDefault(require("../Logger"));
var _classes = require("../classes");
var _errors = require("../errors");
var _utilities = require("../utilities");
var _createProxyController = _interopRequireDefault(require("./createProxyController"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const httpGet = _http.default.get;
const httpRequest = _http.default.request;
const httpsGet = _https.default.get;
const httpsRequest = _https.default.request;
const log = _Logger.default.child({
namespace: 'createGlobalProxyAgent'
});
const defaultConfigurationInput = {
environmentVariableNamespace: undefined,
forceGlobalAgent: undefined,
socketConnectionTimeout: 60000
};
const omitUndefined = subject => {
const keys = Object.keys(subject);
const result = {};
for (const key of keys) {
const value = subject[key];
if (value !== undefined) {
result[key] = value;
}
}
return result;
};
const createConfiguration = configurationInput => {
// eslint-disable-next-line no-process-env
const environment = process.env;
const defaultConfiguration = {
environmentVariableNamespace: typeof environment.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE === 'string' ? environment.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE : 'GLOBAL_AGENT_',
forceGlobalAgent: typeof environment.GLOBAL_AGENT_FORCE_GLOBAL_AGENT === 'string' ? (0, _boolean.boolean)(environment.GLOBAL_AGENT_FORCE_GLOBAL_AGENT) : true,
socketConnectionTimeout: typeof environment.GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT === 'string' ? Number.parseInt(environment.GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT, 10) : defaultConfigurationInput.socketConnectionTimeout
}; // $FlowFixMe
return { ...defaultConfiguration,
...omitUndefined(configurationInput)
};
};
const createGlobalProxyAgent = (configurationInput = defaultConfigurationInput) => {
const configuration = createConfiguration(configurationInput);
const proxyController = (0, _createProxyController.default)(); // eslint-disable-next-line no-process-env
proxyController.HTTP_PROXY = process.env[configuration.environmentVariableNamespace + 'HTTP_PROXY'] || null; // eslint-disable-next-line no-process-env
proxyController.HTTPS_PROXY = process.env[configuration.environmentVariableNamespace + 'HTTPS_PROXY'] || null; // eslint-disable-next-line no-process-env
proxyController.NO_PROXY = process.env[configuration.environmentVariableNamespace + 'NO_PROXY'] || null;
log.info({
configuration,
state: proxyController
}, 'global agent has been initialized');
const mustUrlUseProxy = getProxy => {
return url => {
if (!getProxy()) {
return false;
}
if (!proxyController.NO_PROXY) {
return true;
}
return !(0, _utilities.isUrlMatchingNoProxy)(url, proxyController.NO_PROXY);
};
};
const getUrlProxy = getProxy => {
return () => {
const proxy = getProxy();
if (!proxy) {
throw new _errors.UnexpectedStateError('HTTP(S) proxy must be configured.');
}
return (0, _utilities.parseProxyUrl)(proxy);
};
};
const getHttpProxy = () => {
return proxyController.HTTP_PROXY;
};
const BoundHttpProxyAgent = class extends _classes.HttpProxyAgent {
constructor() {
super(() => {
return getHttpProxy();
}, mustUrlUseProxy(getHttpProxy), getUrlProxy(getHttpProxy), _http.default.globalAgent, configuration.socketConnectionTimeout);
}
};
const httpAgent = new BoundHttpProxyAgent();
const getHttpsProxy = () => {
return proxyController.HTTPS_PROXY || proxyController.HTTP_PROXY;
};
const BoundHttpsProxyAgent = class extends _classes.HttpsProxyAgent {
constructor() {
super(() => {
return getHttpsProxy();
}, mustUrlUseProxy(getHttpsProxy), getUrlProxy(getHttpsProxy), _https.default.globalAgent, configuration.socketConnectionTimeout);
}
};
const httpsAgent = new BoundHttpsProxyAgent(); // Overriding globalAgent was added in v11.7.
// @see https://nodejs.org/uk/blog/release/v11.7.0/
if (_semver.default.gte(process.version, 'v11.7.0')) {
// @see https://github.com/facebook/flow/issues/7670
// $FlowFixMe
_http.default.globalAgent = httpAgent; // $FlowFixMe
_https.default.globalAgent = httpsAgent;
} // The reason this logic is used in addition to overriding http(s).globalAgent
// is because there is no guarantee that we set http(s).globalAgent variable
// before an instance of http(s).Agent has been already constructed by someone,
// e.g. Stripe SDK creates instances of http(s).Agent at the top-level.
// @see https://github.com/gajus/global-agent/pull/13
//
// We still want to override http(s).globalAgent when possible to enable logic
// in `bindHttpMethod`.
if (_semver.default.gte(process.version, 'v10.0.0')) {
// $FlowFixMe
_http.default.get = (0, _utilities.bindHttpMethod)(httpGet, httpAgent, configuration.forceGlobalAgent); // $FlowFixMe
_http.default.request = (0, _utilities.bindHttpMethod)(httpRequest, httpAgent, configuration.forceGlobalAgent); // $FlowFixMe
_https.default.get = (0, _utilities.bindHttpMethod)(httpsGet, httpsAgent, configuration.forceGlobalAgent); // $FlowFixMe
_https.default.request = (0, _utilities.bindHttpMethod)(httpsRequest, httpsAgent, configuration.forceGlobalAgent);
} else {
log.warn('attempt to initialize global-agent in unsupported Node.js version was ignored');
}
return proxyController;
};
var _default = createGlobalProxyAgent;
exports.default = _default;
//# sourceMappingURL=createGlobalProxyAgent.js.map

View File

@ -0,0 +1,197 @@
// @flow
import http from 'http';
import https from 'https';
import {
boolean as parseBoolean,
} from 'boolean';
import semver from 'semver';
import Logger from '../Logger';
import {
HttpProxyAgent,
HttpsProxyAgent,
} from '../classes';
import {
UnexpectedStateError,
} from '../errors';
import {
bindHttpMethod,
isUrlMatchingNoProxy,
parseProxyUrl,
} from '../utilities';
import type {
ProxyAgentConfigurationInputType,
ProxyAgentConfigurationType,
} from '../types';
import createProxyController from './createProxyController';
const httpGet = http.get;
const httpRequest = http.request;
const httpsGet = https.get;
const httpsRequest = https.request;
const log = Logger.child({
namespace: 'createGlobalProxyAgent',
});
const defaultConfigurationInput = {
environmentVariableNamespace: undefined,
forceGlobalAgent: undefined,
socketConnectionTimeout: 60000,
};
const omitUndefined = (subject) => {
const keys = Object.keys(subject);
const result = {};
for (const key of keys) {
const value = subject[key];
if (value !== undefined) {
result[key] = value;
}
}
return result;
};
const createConfiguration = (configurationInput: ProxyAgentConfigurationInputType): ProxyAgentConfigurationType => {
// eslint-disable-next-line no-process-env
const environment = process.env;
const defaultConfiguration = {
environmentVariableNamespace: typeof environment.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE === 'string' ? environment.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE : 'GLOBAL_AGENT_',
forceGlobalAgent: typeof environment.GLOBAL_AGENT_FORCE_GLOBAL_AGENT === 'string' ? parseBoolean(environment.GLOBAL_AGENT_FORCE_GLOBAL_AGENT) : true,
socketConnectionTimeout: typeof environment.GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT === 'string' ? Number.parseInt(environment.GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT, 10) : defaultConfigurationInput.socketConnectionTimeout,
};
// $FlowFixMe
return {
...defaultConfiguration,
...omitUndefined(configurationInput),
};
};
export default (configurationInput: ProxyAgentConfigurationInputType = defaultConfigurationInput) => {
const configuration = createConfiguration(configurationInput);
const proxyController = createProxyController();
// eslint-disable-next-line no-process-env
proxyController.HTTP_PROXY = process.env[configuration.environmentVariableNamespace + 'HTTP_PROXY'] || null;
// eslint-disable-next-line no-process-env
proxyController.HTTPS_PROXY = process.env[configuration.environmentVariableNamespace + 'HTTPS_PROXY'] || null;
// eslint-disable-next-line no-process-env
proxyController.NO_PROXY = process.env[configuration.environmentVariableNamespace + 'NO_PROXY'] || null;
log.info({
configuration,
state: proxyController,
}, 'global agent has been initialized');
const mustUrlUseProxy = (getProxy) => {
return (url) => {
if (!getProxy()) {
return false;
}
if (!proxyController.NO_PROXY) {
return true;
}
return !isUrlMatchingNoProxy(url, proxyController.NO_PROXY);
};
};
const getUrlProxy = (getProxy) => {
return () => {
const proxy = getProxy();
if (!proxy) {
throw new UnexpectedStateError('HTTP(S) proxy must be configured.');
}
return parseProxyUrl(proxy);
};
};
const getHttpProxy = () => {
return proxyController.HTTP_PROXY;
};
const BoundHttpProxyAgent = class extends HttpProxyAgent {
constructor () {
super(
() => {
return getHttpProxy();
},
mustUrlUseProxy(getHttpProxy),
getUrlProxy(getHttpProxy),
http.globalAgent,
configuration.socketConnectionTimeout,
);
}
};
const httpAgent = new BoundHttpProxyAgent();
const getHttpsProxy = () => {
return proxyController.HTTPS_PROXY || proxyController.HTTP_PROXY;
};
const BoundHttpsProxyAgent = class extends HttpsProxyAgent {
constructor () {
super(
() => {
return getHttpsProxy();
},
mustUrlUseProxy(getHttpsProxy),
getUrlProxy(getHttpsProxy),
https.globalAgent,
configuration.socketConnectionTimeout,
);
}
};
const httpsAgent = new BoundHttpsProxyAgent();
// Overriding globalAgent was added in v11.7.
// @see https://nodejs.org/uk/blog/release/v11.7.0/
if (semver.gte(process.version, 'v11.7.0')) {
// @see https://github.com/facebook/flow/issues/7670
// $FlowFixMe
http.globalAgent = httpAgent;
// $FlowFixMe
https.globalAgent = httpsAgent;
}
// The reason this logic is used in addition to overriding http(s).globalAgent
// is because there is no guarantee that we set http(s).globalAgent variable
// before an instance of http(s).Agent has been already constructed by someone,
// e.g. Stripe SDK creates instances of http(s).Agent at the top-level.
// @see https://github.com/gajus/global-agent/pull/13
//
// We still want to override http(s).globalAgent when possible to enable logic
// in `bindHttpMethod`.
if (semver.gte(process.version, 'v10.0.0')) {
// $FlowFixMe
http.get = bindHttpMethod(httpGet, httpAgent, configuration.forceGlobalAgent);
// $FlowFixMe
http.request = bindHttpMethod(httpRequest, httpAgent, configuration.forceGlobalAgent);
// $FlowFixMe
https.get = bindHttpMethod(httpsGet, httpsAgent, configuration.forceGlobalAgent);
// $FlowFixMe
https.request = bindHttpMethod(httpsRequest, httpsAgent, configuration.forceGlobalAgent);
} else {
log.warn('attempt to initialize global-agent in unsupported Node.js version was ignored');
}
return proxyController;
};

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,45 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _Logger = _interopRequireDefault(require("../Logger"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const log = _Logger.default.child({
namespace: 'createProxyController'
});
const KNOWN_PROPERTY_NAMES = ['HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY'];
const createProxyController = () => {
// eslint-disable-next-line fp/no-proxy
return new Proxy({
HTTP_PROXY: null,
HTTPS_PROXY: null,
NO_PROXY: null
}, {
set: (subject, name, value) => {
if (!KNOWN_PROPERTY_NAMES.includes(name)) {
throw new Error('Cannot set an unmapped property "' + name + '".');
}
subject[name] = value;
log.info({
change: {
name,
value
},
newConfiguration: subject
}, 'configuration changed');
return true;
}
});
};
var _default = createProxyController;
exports.default = _default;
//# sourceMappingURL=createProxyController.js.map

View File

@ -0,0 +1,46 @@
// @flow
import Logger from '../Logger';
type ProxyControllerType = {|
HTTP_PROXY: string | null,
HTTPS_PROXY: string | null,
NO_PROXY: string | null,
|};
const log = Logger.child({
namespace: 'createProxyController',
});
const KNOWN_PROPERTY_NAMES = [
'HTTP_PROXY',
'HTTPS_PROXY',
'NO_PROXY',
];
export default (): ProxyControllerType => {
// eslint-disable-next-line fp/no-proxy
return new Proxy({
HTTP_PROXY: null,
HTTPS_PROXY: null,
NO_PROXY: null,
}, {
set: (subject, name, value) => {
if (!KNOWN_PROPERTY_NAMES.includes(name)) {
throw new Error('Cannot set an unmapped property "' + name + '".');
}
subject[name] = value;
log.info({
change: {
name,
value,
},
newConfiguration: subject,
}, 'configuration changed');
return true;
},
});
};

View File

@ -0,0 +1 @@
{"version":3,"sources":["../../src/factories/createProxyController.js"],"names":["log","Logger","child","namespace","KNOWN_PROPERTY_NAMES","Proxy","HTTP_PROXY","HTTPS_PROXY","NO_PROXY","set","subject","name","value","includes","Error","info","change","newConfiguration"],"mappings":";;;;;;;AAEA;;;;AAQA,MAAMA,GAAG,GAAGC,gBAAOC,KAAP,CAAa;AACvBC,EAAAA,SAAS,EAAE;AADY,CAAb,CAAZ;;AAIA,MAAMC,oBAAoB,GAAG,CAC3B,YAD2B,EAE3B,aAF2B,EAG3B,UAH2B,CAA7B;;oCAM0C;AACxC;AACA,SAAO,IAAIC,KAAJ,CAAU;AACfC,IAAAA,UAAU,EAAE,IADG;AAEfC,IAAAA,WAAW,EAAE,IAFE;AAGfC,IAAAA,QAAQ,EAAE;AAHK,GAAV,EAIJ;AACDC,IAAAA,GAAG,EAAE,CAACC,OAAD,EAAUC,IAAV,EAAgBC,KAAhB,KAA0B;AAC7B,UAAI,CAACR,oBAAoB,CAACS,QAArB,CAA8BF,IAA9B,CAAL,EAA0C;AACxC,cAAM,IAAIG,KAAJ,CAAU,sCAAsCH,IAAtC,GAA6C,IAAvD,CAAN;AACD;;AAEDD,MAAAA,OAAO,CAACC,IAAD,CAAP,GAAgBC,KAAhB;AAEAZ,MAAAA,GAAG,CAACe,IAAJ,CAAS;AACPC,QAAAA,MAAM,EAAE;AACNL,UAAAA,IADM;AAENC,UAAAA;AAFM,SADD;AAKPK,QAAAA,gBAAgB,EAAEP;AALX,OAAT,EAMG,uBANH;AAQA,aAAO,IAAP;AACD;AAjBA,GAJI,CAAP;AAuBD,C","sourcesContent":["// @flow\n\nimport Logger from '../Logger';\n\ntype ProxyControllerType = {|\n HTTP_PROXY: string | null,\n HTTPS_PROXY: string | null,\n NO_PROXY: string | null,\n|};\n\nconst log = Logger.child({\n namespace: 'createProxyController',\n});\n\nconst KNOWN_PROPERTY_NAMES = [\n 'HTTP_PROXY',\n 'HTTPS_PROXY',\n 'NO_PROXY',\n];\n\nexport default (): ProxyControllerType => {\n // eslint-disable-next-line fp/no-proxy\n return new Proxy({\n HTTP_PROXY: null,\n HTTPS_PROXY: null,\n NO_PROXY: null,\n }, {\n set: (subject, name, value) => {\n if (!KNOWN_PROPERTY_NAMES.includes(name)) {\n throw new Error('Cannot set an unmapped property \"' + name + '\".');\n }\n\n subject[name] = value;\n\n log.info({\n change: {\n name,\n value,\n },\n newConfiguration: subject,\n }, 'configuration changed');\n\n return true;\n },\n });\n};\n"],"file":"createProxyController.js"}

View File

@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "createGlobalProxyAgent", {
enumerable: true,
get: function () {
return _createGlobalProxyAgent.default;
}
});
Object.defineProperty(exports, "createProxyController", {
enumerable: true,
get: function () {
return _createProxyController.default;
}
});
var _createGlobalProxyAgent = _interopRequireDefault(require("./createGlobalProxyAgent"));
var _createProxyController = _interopRequireDefault(require("./createProxyController"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//# sourceMappingURL=index.js.map

View File

@ -0,0 +1,4 @@
// @flow
export {default as createGlobalProxyAgent} from './createGlobalProxyAgent';
export {default as createProxyController} from './createProxyController';

View File

@ -0,0 +1 @@
{"version":3,"sources":["../../src/factories/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAEA;;AACA","sourcesContent":["// @flow\n\nexport {default as createGlobalProxyAgent} from './createGlobalProxyAgent';\nexport {default as createProxyController} from './createProxyController';\n"],"file":"index.js"}