Adjust endpoint checks (#7972)

This commit is contained in:
Jaco 2022-08-10 11:47:46 +02:00 committed by GitHub
parent f01a68a590
commit d4237c0879
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 29 deletions

View File

@ -41,8 +41,8 @@
"build:release:www": "yarn polkadot-ci-ghact-build && yarn build:release:ghpages && yarn build:release:ipfs",
"build:robohash": "node scripts/robohash.cjs",
"build:www": "rm -rf packages/apps/build && mkdir -p packages/apps/build && yarn run build:i18n && cd packages/apps && yarn webpack --config webpack.config.cjs",
"ci:chainEndpoints": "NODE_OPTIONS=--experimental-vm-modules polkadot-dev-run-test --config ./jest-ci.config.cjs --runInBand --detectOpenHandles packages/apps-config/src/ci/chainEndpoints",
"ci:chainTypes": "NODE_OPTIONS=--experimental-vm-modules polkadot-dev-run-test --config ./jest-ci.config.cjs --runInBand --detectOpenHandles packages/apps-config/src/ci/chainTypes",
"ci:chainEndpoints": "NODE_OPTIONS=--experimental-vm-modules polkadot-dev-run-test --config ./jest-ci.config.cjs --runInBand --forceExit packages/apps-config/src/ci/chainEndpoints",
"ci:chainTypes": "NODE_OPTIONS=--experimental-vm-modules polkadot-dev-run-test --config ./jest-ci.config.cjs --runInBand --forceExit packages/apps-config/src/ci/chainTypes",
"clean": "polkadot-dev-clean-build",
"clean:electronBuild": "cd packages/apps-electron && polkadot-dev-clean-build",
"clean:electronRelease": "cd packages/apps-electron && rm -rf release",

View File

@ -3,39 +3,42 @@
import { fetch } from '@polkadot/x-fetch';
// this is also in @polkadot/phishing - we may want to expose it
// inside x-fetch at some point in the future...
// a fetch with a 2s timeout
async function fetchWithTimeout (url: string, timeout = 2000): Promise<Response> {
function fetchWithTimeout (url: string, timeout = 2000): Promise<Response | null> {
const controller = new AbortController();
let isAborted = false;
const id = setTimeout((): void => {
console.log(`Timeout on ${url}`);
let id: null | ReturnType<typeof setTimeout> = null;
isAborted = true;
controller.abort();
}, timeout);
// This is a weird mess, however we seem to have issues with Jest & hanging connections
// in the case where things are (possibly) aborted. So we just swallow/log everything
// and return null in the cases where things don't quite go as planned
return Promise
.race([
fetch(url, { signal: controller.signal }).catch((error): null => {
console.error(error);
try {
const response = await fetch(url, { signal: controller.signal });
return null;
}),
new Promise<null>((resolve): void => {
id = setTimeout((): void => {
id = null;
controller.abort();
clearTimeout(id);
resolve(null);
}, timeout);
})
])
.then((response): Response | null => {
if (id) {
clearTimeout(id);
}
return response;
} catch (error) {
if (!isAborted) {
clearTimeout(id);
}
throw error;
}
return response;
});
}
export function fetchJson <T> (url: string, timeout?: number): Promise<T> {
return fetchWithTimeout(url, timeout).then<T>((r) => r.json());
export function fetchJson <T> (url: string, timeout?: number): Promise<T | null> {
return fetchWithTimeout(url, timeout).then<T | null>((r) => r && r.json());
}
export function fetchText (url: string, timeout?: number): Promise<string> {
return fetchWithTimeout(url, timeout).then((r) => r.text());
export function fetchText (url: string, timeout?: number): Promise<string | null> {
return fetchWithTimeout(url, timeout).then((r) => r && r.text());
}

View File

@ -34,7 +34,7 @@ function createChecker (issueFile: string, failures: string[]): (name: string, w
let isOk = true;
try {
assert(json.Answer, `No DNS entry for ${host}`);
assert(json && json.Answer, `No DNS entry for ${host}`);
provider = new WsProvider(ws, false);
api = new ApiPromise({