chore: use node-fetch-native support for proxy env vars (#3483)

This commit is contained in:
Peter Evans 2024-11-04 12:59:34 +00:00 committed by GitHub
parent 6d751ce675
commit 0c478ad1a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 3 additions and 134 deletions

122
dist/index.js vendored
View file

@ -1616,7 +1616,6 @@ const core_1 = __nccwpck_require__(767);
const plugin_paginate_rest_1 = __nccwpck_require__(3779);
const plugin_rest_endpoint_methods_1 = __nccwpck_require__(9210);
const plugin_throttling_1 = __nccwpck_require__(6856);
const proxy_from_env_1 = __nccwpck_require__(7777);
const proxy_1 = __nccwpck_require__(3459);
exports.Octokit = core_1.Octokit.plugin(plugin_paginate_rest_1.paginateRest, plugin_rest_endpoint_methods_1.restEndpointMethods, plugin_throttling_1.throttling, autoProxyAgent);
exports.throttleOptions = {
@ -1636,10 +1635,7 @@ exports.throttleOptions = {
// Octokit plugin to support the standard environment variables http_proxy, https_proxy and no_proxy
function autoProxyAgent(octokit) {
octokit.hook.before('request', options => {
const proxy = (0, proxy_from_env_1.getProxyForUrl)(options.baseUrl);
if (proxy) {
options.request.fetch = (0, proxy_1.createFetch)(proxy);
}
options.request.fetch = proxy_1.fetch;
});
}
@ -28666,122 +28662,6 @@ function copyFile(srcFile, destFile, force) {
})));
/***/ }),
/***/ 7777:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
"use strict";
var parseUrl = (__nccwpck_require__(7016).parse);
var DEFAULT_PORTS = {
ftp: 21,
gopher: 70,
http: 80,
https: 443,
ws: 80,
wss: 443,
};
var stringEndsWith = String.prototype.endsWith || function(s) {
return s.length <= this.length &&
this.indexOf(s, this.length - s.length) !== -1;
};
/**
* @param {string|object} url - The URL, or the result from url.parse.
* @return {string} The URL of the proxy that should handle the request to the
* given URL. If no proxy is set, this will be an empty string.
*/
function getProxyForUrl(url) {
var parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {};
var proto = parsedUrl.protocol;
var hostname = parsedUrl.host;
var port = parsedUrl.port;
if (typeof hostname !== 'string' || !hostname || typeof proto !== 'string') {
return ''; // Don't proxy URLs without a valid scheme or host.
}
proto = proto.split(':', 1)[0];
// Stripping ports in this way instead of using parsedUrl.hostname to make
// sure that the brackets around IPv6 addresses are kept.
hostname = hostname.replace(/:\d*$/, '');
port = parseInt(port) || DEFAULT_PORTS[proto] || 0;
if (!shouldProxy(hostname, port)) {
return ''; // Don't proxy URLs that match NO_PROXY.
}
var proxy =
getEnv('npm_config_' + proto + '_proxy') ||
getEnv(proto + '_proxy') ||
getEnv('npm_config_proxy') ||
getEnv('all_proxy');
if (proxy && proxy.indexOf('://') === -1) {
// Missing scheme in proxy, default to the requested URL's scheme.
proxy = proto + '://' + proxy;
}
return proxy;
}
/**
* Determines whether a given URL should be proxied.
*
* @param {string} hostname - The host name of the URL.
* @param {number} port - The effective port of the URL.
* @returns {boolean} Whether the given URL should be proxied.
* @private
*/
function shouldProxy(hostname, port) {
var NO_PROXY =
(getEnv('npm_config_no_proxy') || getEnv('no_proxy')).toLowerCase();
if (!NO_PROXY) {
return true; // Always proxy if NO_PROXY is not set.
}
if (NO_PROXY === '*') {
return false; // Never proxy if wildcard is set.
}
return NO_PROXY.split(/[,\s]/).every(function(proxy) {
if (!proxy) {
return true; // Skip zero-length hosts.
}
var parsedProxy = proxy.match(/^(.+):(\d+)$/);
var parsedProxyHostname = parsedProxy ? parsedProxy[1] : proxy;
var parsedProxyPort = parsedProxy ? parseInt(parsedProxy[2]) : 0;
if (parsedProxyPort && parsedProxyPort !== port) {
return true; // Skip if ports don't match.
}
if (!/^[.*]/.test(parsedProxyHostname)) {
// No wildcards, so stop proxying if there is an exact match.
return hostname !== parsedProxyHostname;
}
if (parsedProxyHostname.charAt(0) === '*') {
// Remove leading wildcard.
parsedProxyHostname = parsedProxyHostname.slice(1);
}
// Stop proxying if the hostname ends with the no_proxy host.
return !stringEndsWith.call(hostname, parsedProxyHostname);
});
}
/**
* Get the value for an environment variable.
*
* @param {string} key - The name of the environment variable.
* @return {string} The value of the environment variable.
* @private
*/
function getEnv(key) {
return process.env[key.toLowerCase()] || process.env[key.toUpperCase()] || '';
}
exports.getProxyForUrl = getProxyForUrl;
/***/ }),
/***/ 770:

6
package-lock.json generated
View file

@ -17,7 +17,6 @@
"@octokit/plugin-throttling": "^9.3.2",
"node-fetch-native": "^1.6.4",
"p-limit": "^6.1.0",
"proxy-from-env": "^1.1.0",
"uuid": "^9.0.1"
},
"devDependencies": {
@ -6797,11 +6796,6 @@
"node": ">= 6"
}
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/psl": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz",

View file

@ -37,7 +37,6 @@
"@octokit/plugin-throttling": "^9.3.2",
"node-fetch-native": "^1.6.4",
"p-limit": "^6.1.0",
"proxy-from-env": "^1.1.0",
"uuid": "^9.0.1"
},
"devDependencies": {

View file

@ -3,8 +3,7 @@ import {Octokit as OctokitCore} from '@octokit/core'
import {paginateRest} from '@octokit/plugin-paginate-rest'
import {restEndpointMethods} from '@octokit/plugin-rest-endpoint-methods'
import {throttling} from '@octokit/plugin-throttling'
import {getProxyForUrl} from 'proxy-from-env'
import {createFetch} from 'node-fetch-native/proxy'
import {fetch} from 'node-fetch-native/proxy'
export {RestEndpointMethodTypes} from '@octokit/plugin-rest-endpoint-methods'
// eslint-disable-next-line import/no-unresolved
export {OctokitOptions} from '@octokit/core/dist-types/types'
@ -36,9 +35,6 @@ export const throttleOptions = {
// Octokit plugin to support the standard environment variables http_proxy, https_proxy and no_proxy
function autoProxyAgent(octokit: OctokitCore) {
octokit.hook.before('request', options => {
const proxy = getProxyForUrl(options.baseUrl)
if (proxy) {
options.request.fetch = createFetch(proxy)
}
options.request.fetch = fetch
})
}