diff --git a/packages/ping/package.json b/packages/ping/package.json index 4972239e8..cf0b7104f 100644 --- a/packages/ping/package.json +++ b/packages/ping/package.json @@ -22,8 +22,10 @@ }, "prettier": "@homarr/prettier-config", "dependencies": { + "@homarr/certificates": "workspace:^0.1.0", "@homarr/common": "workspace:^0.1.0", - "@homarr/log": "workspace:^0.1.0" + "@homarr/log": "workspace:^0.1.0", + "pretty-print-error": "^1.1.2" }, "devDependencies": { "@homarr/eslint-config": "workspace:^0.2.0", diff --git a/packages/ping/src/index.ts b/packages/ping/src/index.ts index 93dbc88c4..d96d1a51a 100644 --- a/packages/ping/src/index.ts +++ b/packages/ping/src/index.ts @@ -1,13 +1,34 @@ -import { extractErrorMessage, fetchWithTimeout } from "@homarr/common"; +import { formatError } from "pretty-print-error"; +import type { fetch } from "undici"; + +import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server"; import { logger } from "@homarr/log"; export const sendPingRequestAsync = async (url: string) => { try { - return await fetchWithTimeout(url).then((response) => ({ statusCode: response.status })); + return await fetchWithTimeoutAndCertificates(url).then((response) => ({ statusCode: response.status })); } catch (error) { - logger.error("packages/ping/src/index.ts:", error); + logger.error("packages/ping/src/index.ts:", formatError(error)); return { - error: extractErrorMessage(error), + error: formatError(error), }; } }; + +/** + * Same as fetch, but with a timeout of 10 seconds. + * Also respects certificates. + * https://stackoverflow.com/questions/46946380/fetch-api-request-timeout + * @param param0 fetch arguments + * @returns fetch response + */ +export const fetchWithTimeoutAndCertificates = (...[url, requestInit]: Parameters) => { + const controller = new AbortController(); + + // 10 seconds timeout: + const timeoutId = setTimeout(() => controller.abort(), 10000); + + return fetchWithTrustedCertificatesAsync(url, { signal: controller.signal, ...requestInit }).finally(() => { + clearTimeout(timeoutId); + }); +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 738eda44d..b9e6eb97e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1469,12 +1469,18 @@ importers: packages/ping: dependencies: + '@homarr/certificates': + specifier: workspace:^0.1.0 + version: link:../certificates '@homarr/common': specifier: workspace:^0.1.0 version: link:../common '@homarr/log': specifier: workspace:^0.1.0 version: link:../log + pretty-print-error: + specifier: ^1.1.2 + version: 1.1.2(patch_hash=4arrfgbz7em6s4gqywse7esg4u) devDependencies: '@homarr/eslint-config': specifier: workspace:^0.2.0