Add proxy to create-app tasks

Signed-off-by: enyineer <nico.enking@gmail.com>
This commit is contained in:
enyineer
2025-10-10 10:09:47 +02:00
parent 944d3fa82e
commit cdbec2e0d5
8 changed files with 171 additions and 42 deletions
+1
View File
@@ -23,3 +23,4 @@
export { findPaths, BACKSTAGE_JSON } from './paths';
export { isChildPath } from './isChildPath';
export type { Paths, ResolveFunc } from './paths';
export { maybeBootstrapProxy } from './proxyBootstrap';
@@ -0,0 +1,108 @@
/*
* Copyright 2025 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { maybeBootstrapProxy } from './proxyBootstrap';
jest.mock('global-agent', () => ({
bootstrap: jest.fn(),
}));
jest.mock('undici', () => ({
setGlobalDispatcher: jest.fn(),
EnvHttpProxyAgent: jest.fn(),
}));
describe('maybeBootstrapProxy', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = { ...originalEnv };
});
afterEach(() => {
process.env = originalEnv;
jest.clearAllMocks();
});
it('should bootstrap global-agent if GLOBAL_AGENT_HTTP_PROXY is set', () => {
process.env.GLOBAL_AGENT_HTTP_PROXY = 'http://proxy.example.com';
const { bootstrap } =
require('global-agent') as typeof import('global-agent');
maybeBootstrapProxy();
expect(bootstrap).toHaveBeenCalledTimes(1);
});
it('should bootstrap global-agent if GLOBAL_AGENT_HTTPS_PROXY is set', () => {
process.env.GLOBAL_AGENT_HTTPS_PROXY = 'https://proxy.example.com';
const { bootstrap } =
require('global-agent') as typeof import('global-agent');
maybeBootstrapProxy();
expect(bootstrap).toHaveBeenCalledTimes(1);
});
it('should use undici EnvHttpProxyAgent if HTTP_PROXY is set', () => {
process.env.HTTP_PROXY = 'http://proxy.example.com';
const { setGlobalDispatcher, EnvHttpProxyAgent } =
require('undici') as typeof import('undici');
maybeBootstrapProxy();
expect(EnvHttpProxyAgent).toHaveBeenCalledTimes(1);
expect(setGlobalDispatcher).toHaveBeenCalledWith(
expect.any(EnvHttpProxyAgent),
);
});
it('should use undici EnvHttpProxyAgent if HTTPS_PROXY is set', () => {
process.env.HTTPS_PROXY = 'https://proxy.example.com';
const { setGlobalDispatcher, EnvHttpProxyAgent } =
require('undici') as typeof import('undici');
maybeBootstrapProxy();
expect(EnvHttpProxyAgent).toHaveBeenCalledTimes(1);
expect(setGlobalDispatcher).toHaveBeenCalledWith(
expect.any(EnvHttpProxyAgent),
);
});
it('should not bootstrap global-agent or set undici dispatcher if no proxy is set', () => {
const { bootstrap } =
require('global-agent') as typeof import('global-agent');
const { setGlobalDispatcher } =
require('undici') as typeof import('undici');
maybeBootstrapProxy();
expect(bootstrap).not.toHaveBeenCalled();
expect(setGlobalDispatcher).not.toHaveBeenCalled();
});
it('should respect GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE', () => {
process.env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE = 'CUSTOM_AGENT_';
process.env.CUSTOM_AGENT_HTTP_PROXY = 'http://proxy.example.com';
const { bootstrap } =
require('global-agent') as typeof import('global-agent');
maybeBootstrapProxy();
expect(bootstrap).toHaveBeenCalledTimes(1);
});
});
+35
View File
@@ -0,0 +1,35 @@
/*
* Copyright 2025 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function maybeBootstrapProxy() {
// see https://www.npmjs.com/package/global-agent
const globalAgentNamespace =
process.env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE ?? 'GLOBAL_AGENT_';
if (
process.env[`${globalAgentNamespace}HTTP_PROXY`] ||
process.env[`${globalAgentNamespace}HTTPS_PROXY`]
) {
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());
}
}