Skip to content

Commit

Permalink
Merge pull request backstage#28528 from backstage/freben/cli-proxy
Browse files Browse the repository at this point in the history
support undici proxying in the cli
  • Loading branch information
freben authored Jan 16, 2025
2 parents 59a8f43 + f21b125 commit 53097d1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-apes-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---

Ensure that both global-agent and undici agents are enabled when proxying is enabled.
1 change: 1 addition & 0 deletions .github/vale/config/vocabularies/Backstage/accept.txt
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ typeahead
ui
unbreak
Unconference
undici
unicode
unmanaged
unmount
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/commands/versions/bump.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand Down
25 changes: 14 additions & 11 deletions packages/cli/src/commands/versions/bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = [
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4024,6 +4024,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
Expand Down Expand Up @@ -45665,6 +45666,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"
Expand Down

0 comments on commit 53097d1

Please sign in to comment.