fix: handle aborted request in docker and remove redundant call (#1161)

This commit is contained in:
Ihor Karpiuk
2023-07-07 18:15:06 +03:00
committed by GitHub
parent dcf4c0fd4f
commit ac9a494c92
3 changed files with 21 additions and 14 deletions

View File

@@ -3,15 +3,19 @@ import nodeFetch from 'node-fetch';
const TIMEOUT = 3000; const TIMEOUT = 3000;
export default async (url: string, options = {}) => { export default async (url: string, options = {}) => {
try {
if (!global.AbortController) { if (!global.AbortController) {
return nodeFetch(url, options); return nodeFetch(url, options);
} }
const controller = new AbortController(); const controller = new AbortController();
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
controller.abort(); controller.abort();
}, TIMEOUT); }, TIMEOUT);
const res = await nodeFetch(url, { signal: controller.signal, ...options }); const res = await nodeFetch(url, { signal: controller.signal, ...options });
clearTimeout(timeout); clearTimeout(timeout);
return res; return res;
} catch (e) {
return;
}
}; };

View File

@@ -32,9 +32,10 @@ export const notifyUpdateCliVersion = () => {
const isNewVersionAvailable = (current: string, latest: string) => compare(current, latest) < 0; const isNewVersionAvailable = (current: string, latest: string) => compare(current, latest) < 0;
const getLatestVersion = async (packageName: string): Promise<string> => { const getLatestVersion = async (packageName: string): Promise<string | undefined> => {
const latestUrl = `http://registry.npmjs.org/${packageName}/latest`; const latestUrl = `http://registry.npmjs.org/${packageName}/latest`;
const response = await fetch(latestUrl); const response = await fetch(latestUrl);
if (!response) return;
const info = await response.json(); const info = await response.json();
return info.version; return info.version;
}; };
@@ -46,8 +47,10 @@ export const cacheLatestVersion = () => {
getLatestVersion(name) getLatestVersion(name)
.then((version) => { .then((version) => {
if (version) {
const lastCheckFile = join(tmpdir(), VERSION_CACHE_FILE); const lastCheckFile = join(tmpdir(), VERSION_CACHE_FILE);
writeFileSync(lastCheckFile, version); writeFileSync(lastCheckFile, version);
}
}) })
.catch(() => {}); .catch(() => {});
}; };

View File

@@ -496,7 +496,7 @@ export async function sendTelemetry(
} = argv; } = argv;
const event_time = new Date().toISOString(); const event_time = new Date().toISOString();
const redoclyClient = new RedoclyClient(); const redoclyClient = new RedoclyClient();
const logged_in = await redoclyClient.isAuthorizedWithRedoclyByRegion(); const logged_in = redoclyClient.hasTokens();
const data: Analytics = { const data: Analytics = {
event: 'cli_command', event: 'cli_command',
event_time, event_time,