Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

createGlobalProxyAgent.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _http = _interopRequireDefault(require("http"));
  7. var _https = _interopRequireDefault(require("https"));
  8. var _boolean = require("boolean");
  9. var _semver = _interopRequireDefault(require("semver"));
  10. var _Logger = _interopRequireDefault(require("../Logger"));
  11. var _classes = require("../classes");
  12. var _errors = require("../errors");
  13. var _utilities = require("../utilities");
  14. var _createProxyController = _interopRequireDefault(require("./createProxyController"));
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. const httpGet = _http.default.get;
  17. const httpRequest = _http.default.request;
  18. const httpsGet = _https.default.get;
  19. const httpsRequest = _https.default.request;
  20. const log = _Logger.default.child({
  21. namespace: 'createGlobalProxyAgent'
  22. });
  23. const defaultConfigurationInput = {
  24. environmentVariableNamespace: undefined,
  25. forceGlobalAgent: undefined,
  26. socketConnectionTimeout: 60000
  27. };
  28. const omitUndefined = subject => {
  29. const keys = Object.keys(subject);
  30. const result = {};
  31. for (const key of keys) {
  32. const value = subject[key];
  33. if (value !== undefined) {
  34. result[key] = value;
  35. }
  36. }
  37. return result;
  38. };
  39. const createConfiguration = configurationInput => {
  40. // eslint-disable-next-line no-process-env
  41. const environment = process.env;
  42. const defaultConfiguration = {
  43. environmentVariableNamespace: typeof environment.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE === 'string' ? environment.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE : 'GLOBAL_AGENT_',
  44. forceGlobalAgent: typeof environment.GLOBAL_AGENT_FORCE_GLOBAL_AGENT === 'string' ? (0, _boolean.boolean)(environment.GLOBAL_AGENT_FORCE_GLOBAL_AGENT) : true,
  45. socketConnectionTimeout: typeof environment.GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT === 'string' ? Number.parseInt(environment.GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT, 10) : defaultConfigurationInput.socketConnectionTimeout
  46. }; // $FlowFixMe
  47. return { ...defaultConfiguration,
  48. ...omitUndefined(configurationInput)
  49. };
  50. };
  51. const createGlobalProxyAgent = (configurationInput = defaultConfigurationInput) => {
  52. const configuration = createConfiguration(configurationInput);
  53. const proxyController = (0, _createProxyController.default)(); // eslint-disable-next-line no-process-env
  54. proxyController.HTTP_PROXY = process.env[configuration.environmentVariableNamespace + 'HTTP_PROXY'] || null; // eslint-disable-next-line no-process-env
  55. proxyController.HTTPS_PROXY = process.env[configuration.environmentVariableNamespace + 'HTTPS_PROXY'] || null; // eslint-disable-next-line no-process-env
  56. proxyController.NO_PROXY = process.env[configuration.environmentVariableNamespace + 'NO_PROXY'] || null;
  57. log.info({
  58. configuration,
  59. state: proxyController
  60. }, 'global agent has been initialized');
  61. const mustUrlUseProxy = getProxy => {
  62. return url => {
  63. if (!getProxy()) {
  64. return false;
  65. }
  66. if (!proxyController.NO_PROXY) {
  67. return true;
  68. }
  69. return !(0, _utilities.isUrlMatchingNoProxy)(url, proxyController.NO_PROXY);
  70. };
  71. };
  72. const getUrlProxy = getProxy => {
  73. return () => {
  74. const proxy = getProxy();
  75. if (!proxy) {
  76. throw new _errors.UnexpectedStateError('HTTP(S) proxy must be configured.');
  77. }
  78. return (0, _utilities.parseProxyUrl)(proxy);
  79. };
  80. };
  81. const getHttpProxy = () => {
  82. return proxyController.HTTP_PROXY;
  83. };
  84. const BoundHttpProxyAgent = class extends _classes.HttpProxyAgent {
  85. constructor() {
  86. super(() => {
  87. return getHttpProxy();
  88. }, mustUrlUseProxy(getHttpProxy), getUrlProxy(getHttpProxy), _http.default.globalAgent, configuration.socketConnectionTimeout);
  89. }
  90. };
  91. const httpAgent = new BoundHttpProxyAgent();
  92. const getHttpsProxy = () => {
  93. return proxyController.HTTPS_PROXY || proxyController.HTTP_PROXY;
  94. };
  95. const BoundHttpsProxyAgent = class extends _classes.HttpsProxyAgent {
  96. constructor() {
  97. super(() => {
  98. return getHttpsProxy();
  99. }, mustUrlUseProxy(getHttpsProxy), getUrlProxy(getHttpsProxy), _https.default.globalAgent, configuration.socketConnectionTimeout);
  100. }
  101. };
  102. const httpsAgent = new BoundHttpsProxyAgent(); // Overriding globalAgent was added in v11.7.
  103. // @see https://nodejs.org/uk/blog/release/v11.7.0/
  104. if (_semver.default.gte(process.version, 'v11.7.0')) {
  105. // @see https://github.com/facebook/flow/issues/7670
  106. // $FlowFixMe
  107. _http.default.globalAgent = httpAgent; // $FlowFixMe
  108. _https.default.globalAgent = httpsAgent;
  109. } // The reason this logic is used in addition to overriding http(s).globalAgent
  110. // is because there is no guarantee that we set http(s).globalAgent variable
  111. // before an instance of http(s).Agent has been already constructed by someone,
  112. // e.g. Stripe SDK creates instances of http(s).Agent at the top-level.
  113. // @see https://github.com/gajus/global-agent/pull/13
  114. //
  115. // We still want to override http(s).globalAgent when possible to enable logic
  116. // in `bindHttpMethod`.
  117. if (_semver.default.gte(process.version, 'v10.0.0')) {
  118. // $FlowFixMe
  119. _http.default.get = (0, _utilities.bindHttpMethod)(httpGet, httpAgent, configuration.forceGlobalAgent); // $FlowFixMe
  120. _http.default.request = (0, _utilities.bindHttpMethod)(httpRequest, httpAgent, configuration.forceGlobalAgent); // $FlowFixMe
  121. _https.default.get = (0, _utilities.bindHttpMethod)(httpsGet, httpsAgent, configuration.forceGlobalAgent); // $FlowFixMe
  122. _https.default.request = (0, _utilities.bindHttpMethod)(httpsRequest, httpsAgent, configuration.forceGlobalAgent);
  123. } else {
  124. log.warn('attempt to initialize global-agent in unsupported Node.js version was ignored');
  125. }
  126. return proxyController;
  127. };
  128. var _default = createGlobalProxyAgent;
  129. exports.default = _default;
  130. //# sourceMappingURL=createGlobalProxyAgent.js.map