Skip to content

Commit 0483640

Browse files
authored
fix: env HTTP_PROXY in push command (#2447)
Fixed a compatibility issue with `HTTP_PROXY` environment variable for the `push` command.
1 parent 5f869f1 commit 0483640

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

.changeset/brown-adults-pick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@redocly/cli": patch
3+
---
4+
5+
Fixed a compatibility issue with `HTTP_PROXY` environment variable for the `push` command.

packages/cli/src/__tests__/fetch-with-timeout.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import AbortController from 'abort-controller';
2-
import { HttpsProxyAgent } from 'https-proxy-agent';
3-
import { Agent } from 'undici';
2+
import { Agent, ProxyAgent } from 'undici';
43
import fetchWithTimeout from '../utils/fetch-with-timeout.js';
54
import * as proxyAgent from '../utils/proxy-agent.js';
65

@@ -64,12 +63,17 @@ describe('fetchWithTimeout', () => {
6463
});
6564

6665
it('should call fetch with proxy agent', async () => {
67-
const dispatcher = new HttpsProxyAgent('http://localhost');
68-
vi.spyOn(proxyAgent, 'getProxyAgent').mockReturnValueOnce(dispatcher);
66+
const proxyUrl = 'http://localhost:8080';
67+
vi.spyOn(proxyAgent, 'getProxyUrl').mockReturnValueOnce(proxyUrl);
6968

7069
await fetchWithTimeout('url');
7170

72-
expect(global.fetch).toHaveBeenCalledWith('url', { dispatcher });
71+
expect(global.fetch).toHaveBeenCalledWith(
72+
'url',
73+
expect.objectContaining({
74+
dispatcher: expect.any(ProxyAgent),
75+
})
76+
);
7377
});
7478

7579
it('should call fetch without signal and without dispatcher when timeout is not passed', async () => {

packages/cli/src/utils/fetch-with-timeout.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
import { getProxyAgent } from './proxy-agent.js';
2-
import { Agent } from 'undici';
1+
import { getProxyUrl } from './proxy-agent.js';
2+
import { Agent, ProxyAgent } from 'undici';
33

44
export type FetchWithTimeoutOptions = RequestInit & {
55
timeout?: number;
66
};
77

88
export default async (url: string, { timeout, ...options }: FetchWithTimeoutOptions = {}) => {
9-
const dispatcher = getProxyAgent() || (timeout ? new Agent({ connect: { timeout } }) : undefined);
9+
const proxyUrl = getProxyUrl();
10+
let dispatcher: Agent | ProxyAgent | undefined;
11+
12+
const connectOptions = timeout ? { connect: { timeout } } : {};
13+
14+
if (proxyUrl) {
15+
dispatcher = new ProxyAgent({
16+
uri: proxyUrl,
17+
...connectOptions,
18+
});
19+
} else if (timeout) {
20+
dispatcher = new Agent(connectOptions);
21+
}
1022

1123
const res = await fetch(url, {
1224
signal: timeout ? AbortSignal.timeout(timeout) : undefined,

0 commit comments

Comments
 (0)