supoprt undici proxying in the cli

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2025-01-16 14:42:14 +01:00
parent 116cd46adc
commit f21b1256c2
6 changed files with 37 additions and 13 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Ensure that both global-agent and undici agents are enabled when proxying is enabled.
@@ -468,6 +468,7 @@ typeahead
ui
unbreak
Unconference
undici
unicode
unmanaged
unmount
+1
View File
@@ -147,6 +147,7 @@
"tar": "^6.1.12",
"terser-webpack-plugin": "^5.1.3",
"ts-morph": "^24.0.0",
"undici": "^7.2.3",
"util": "^0.12.3",
"webpack": "^5.94.0",
"webpack-dev-server": "^5.0.0",
@@ -28,8 +28,14 @@ import {
createMockDirectory,
} from '@backstage/backend-test-utils';
// Avoid mutating the global http(s) agent used in other tests
jest.mock('global-agent/bootstrap', () => {});
// Avoid mutating the global agents used in other tests
jest.mock('global-agent', () => ({
bootstrap: jest.fn(),
}));
jest.mock('undici', () => ({
setGlobalDispatcher: jest.fn(),
EnvHttpProxyAgent: class {},
}));
// Remove log coloring to simplify log matching
jest.mock('chalk', () => ({
+14 -11
View File
@@ -14,11 +14,7 @@
* limitations under the License.
*/
import { bootstrap } from 'global-agent';
if (shouldUseGlobalAgent()) {
bootstrap();
}
maybeBootstrapProxy();
import fs from 'fs-extra';
import chalk from 'chalk';
@@ -46,17 +42,24 @@ import { migrateMovedPackages } from './migrate';
import { runYarnInstall } from './utils';
import { run } from '../../lib/run';
function shouldUseGlobalAgent(): boolean {
function maybeBootstrapProxy() {
// see https://www.npmjs.com/package/global-agent
const namespace =
const globalAgentNamespace =
process.env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE ?? 'GLOBAL_AGENT_';
if (
process.env[`${namespace}HTTP_PROXY`] ||
process.env[`${namespace}HTTPS_PROXY`]
process.env[`${globalAgentNamespace}HTTP_PROXY`] ||
process.env[`${globalAgentNamespace}HTTPS_PROXY`]
) {
return true;
const globalAgent =
require('global-agent') as typeof import('global-agent');
globalAgent.bootstrap();
}
if (process.env.HTTP_PROXY || process.env.HTTPS_PROXY) {
const { setGlobalDispatcher, EnvHttpProxyAgent } =
require('undici') as typeof import('undici');
setGlobalDispatcher(new EnvHttpProxyAgent());
}
return false;
}
const DEP_TYPES = [
+8
View File
@@ -4020,6 +4020,7 @@ __metadata:
tar: ^6.1.12
terser-webpack-plugin: ^5.1.3
ts-morph: ^24.0.0
undici: ^7.2.3
util: ^0.12.3
webpack: ^5.94.0
webpack-dev-server: ^5.0.0
@@ -45468,6 +45469,13 @@ __metadata:
languageName: node
linkType: hard
"undici@npm:^7.2.3":
version: 7.2.3
resolution: "undici@npm:7.2.3"
checksum: 93bfe6b1f5acc1c7180a2d6a04b22bda8223ee4c683a7f240b261b272eeb3bd46e9514f70afe8ad3e55e82ca06ade19cd7bc590202a0b4118803a75daf776b5d
languageName: node
linkType: hard
"uni-global@npm:^1.0.0":
version: 1.0.0
resolution: "uni-global@npm:1.0.0"