From da2fb46c5ad827cfe6f4eae74e7a2daf9960cac4 Mon Sep 17 00:00:00 2001 From: Balte de Wit Date: Mon, 4 Dec 2023 13:09:50 +0100 Subject: [PATCH] feat: fetch versions of external components --- src/controllers/serverVersions.ts | 7 +++++++ src/lib/lib.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/controllers/serverVersions.ts b/src/controllers/serverVersions.ts index 920bf80..7110b2d 100644 --- a/src/controllers/serverVersions.ts +++ b/src/controllers/serverVersions.ts @@ -23,6 +23,13 @@ function compileVersions(component: any, prefix = ''): Versions { Object.keys(compiledVersions).forEach((key) => (versions[key] = compiledVersions[key])) }) } + if (component._extVersions) { + component._extVersions.forEach((child: PromiseSettledResult) => { + if (child.status === 'rejected' || child.value.error) return + const compiledVersions = compileVersions(child.value, '~' + child.value.name + '.') + Object.keys(compiledVersions).forEach((key) => (versions[key] = compiledVersions[key])) + }) + } return versions } diff --git a/src/lib/lib.ts b/src/lib/lib.ts index 1e77b7d..3140adc 100644 --- a/src/lib/lib.ts +++ b/src/lib/lib.ts @@ -3,6 +3,7 @@ import fetch from 'node-fetch' import { URL, URLSearchParams } from 'url' const HEALTH_PATH = 'health' +const EXT_VERSIONS = ['external/sisyfos/health'] export async function getServerHealth(serverHost: string): Promise { if (serverHost !== '') { @@ -26,6 +27,30 @@ export async function getServerHealth(serverHost: string): Promise { } } +export async function getExternalVersions(serverHost: string): Promise[]> { + const result: any[] = [] + if (serverHost !== '') { + const serverLocation = /^https?:\/\//.test(serverHost) ? new URL(serverHost) : new URL(`http://${serverHost}`) + + return Promise.allSettled( + EXT_VERSIONS.map(async (extUrl) => { + const url = `${serverLocation.href}${extUrl}` + + try { + const response = await fetch(url, { method: 'GET' }) + return await response.json() + } catch (error) { + return { + error: new Error(`Unable to fetch resource ${url}: ${error}`), + } + } + }) + ) + } + + return result +} + /** * Accepts a list of server hosts and returns the responses from a query to * each server's /health service. @@ -41,6 +66,7 @@ export async function getServerData(serverHosts: Array): Promise { const serverHealth = await getServerHealth(host) + serverHealth._extVersions = await getExternalVersions(host) serverHealth._host = host return serverHealth })