Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use proxy env variables and abort-controller package to prevent hanging behind proxies #1173

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,30 @@
],
"dependencies": {
"@redocly/openapi-core": "1.0.0-beta.131",
"abort-controller": "^3.0.0",
"assert-node-version": "^1.0.3",
"chokidar": "^3.5.1",
"colorette": "^1.2.0",
"glob": "^7.1.6",
"glob-promise": "^3.4.0",
"handlebars": "^4.7.6",
"mobx": "^6.0.4",
"portfinder": "^1.0.26",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"redoc": "~2.0.0",
"semver": "^7.5.2",
"simple-websocket": "^9.0.0",
"yargs": "17.0.1",
"mobx": "^6.0.4",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"styled-components": "^5.1.1"
"styled-components": "^5.1.1",
"yargs": "17.0.1"
},
"devDependencies": {
"@types/configstore": "^5.0.1",
"@types/react": "^17.0.8",
"@types/react-dom": "^17.0.5",
"@types/semver": "^7.5.0",
"@types/styled-components": "^5.1.1",
"@types/yargs": "17.0.5",
"@types/semver": "^7.5.0",
"typescript": "^4.0.3"
}
}
18 changes: 3 additions & 15 deletions packages/cli/src/__tests__/fetch-with-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AbortController from 'abort-controller';
import fetchWithTimeout from '../fetch-with-timeout';
import nodeFetch from 'node-fetch';

Expand All @@ -8,28 +9,15 @@ describe('fetchWithTimeout', () => {
jest.clearAllMocks();
});

it('should use bare node-fetch if AbortController is not available', async () => {
// @ts-ignore
global.AbortController = undefined;
// @ts-ignore
global.setTimeout = jest.fn();
await fetchWithTimeout('url', { method: 'GET' });

expect(nodeFetch).toHaveBeenCalledWith('url', { method: 'GET' });

expect(global.setTimeout).toHaveBeenCalledTimes(0);
});

it('should call node-fetch with signal if AbortController is available', async () => {
global.AbortController = jest.fn().mockImplementation(() => ({ signal: 'something' }));
it('should call node-fetch with signal', async () => {
// @ts-ignore
global.setTimeout = jest.fn();

global.clearTimeout = jest.fn();
await fetchWithTimeout('url');

expect(global.setTimeout).toHaveBeenCalledTimes(1);
expect(nodeFetch).toHaveBeenCalledWith('url', { signal: 'something' });
expect(nodeFetch).toHaveBeenCalledWith('url', { signal: new AbortController().signal });
expect(global.clearTimeout).toHaveBeenCalledTimes(1);
});
});
4 changes: 1 addition & 3 deletions packages/cli/src/fetch-with-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import nodeFetch from 'node-fetch';
import AbortController from 'abort-controller';

const TIMEOUT = 3000;

export default async (url: string, options = {}) => {
try {
if (!global.AbortController) {
return nodeFetch(url, options);
}
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/update-version-notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const SPACE_TO_BORDER = 4;

const INTERVAL_TO_CHECK = 1000 * 60 * 60 * 12;
const SHOULD_NOT_NOTIFY =
process.env.NODE_ENV === 'test' || process.env.CI || !!process.env.LAMBDA_TASK_ROOT;
process.env.NODE_ENV === 'test' ||
process.env.CI ||
!!process.env.LAMBDA_TASK_ROOT ||
process.env.HTTP_PROXY ||
process.env.HTTPS_PROXY;

export const notifyUpdateCliVersion = () => {
if (SHOULD_NOT_NOTIFY) {
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export function commandWrapper<T extends CommandOptions>(
} catch (err) {
// Do nothing
} finally {
if (process.env.REDOCLY_TELEMETRY !== 'off' && telemetry !== 'off') {
if (
process.env.REDOCLY_TELEMETRY !== 'off' &&
telemetry !== 'off' &&
!process.env.HTTP_PROXY &&
!process.env.HTTPS_PROXY
) {
await sendTelemetry(argv, code, hasConfig);
}
process.once('beforeExit', () => {
Expand Down