fix: respect certificates for app ping (#2198)
This commit is contained in:
@@ -22,8 +22,10 @@
|
|||||||
},
|
},
|
||||||
"prettier": "@homarr/prettier-config",
|
"prettier": "@homarr/prettier-config",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@homarr/certificates": "workspace:^0.1.0",
|
||||||
"@homarr/common": "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": {
|
"devDependencies": {
|
||||||
"@homarr/eslint-config": "workspace:^0.2.0",
|
"@homarr/eslint-config": "workspace:^0.2.0",
|
||||||
|
|||||||
@@ -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";
|
import { logger } from "@homarr/log";
|
||||||
|
|
||||||
export const sendPingRequestAsync = async (url: string) => {
|
export const sendPingRequestAsync = async (url: string) => {
|
||||||
try {
|
try {
|
||||||
return await fetchWithTimeout(url).then((response) => ({ statusCode: response.status }));
|
return await fetchWithTimeoutAndCertificates(url).then((response) => ({ statusCode: response.status }));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("packages/ping/src/index.ts:", error);
|
logger.error("packages/ping/src/index.ts:", formatError(error));
|
||||||
return {
|
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<typeof fetch>) => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
// 10 seconds timeout:
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
||||||
|
|
||||||
|
return fetchWithTrustedCertificatesAsync(url, { signal: controller.signal, ...requestInit }).finally(() => {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
@@ -1469,12 +1469,18 @@ importers:
|
|||||||
|
|
||||||
packages/ping:
|
packages/ping:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
'@homarr/certificates':
|
||||||
|
specifier: workspace:^0.1.0
|
||||||
|
version: link:../certificates
|
||||||
'@homarr/common':
|
'@homarr/common':
|
||||||
specifier: workspace:^0.1.0
|
specifier: workspace:^0.1.0
|
||||||
version: link:../common
|
version: link:../common
|
||||||
'@homarr/log':
|
'@homarr/log':
|
||||||
specifier: workspace:^0.1.0
|
specifier: workspace:^0.1.0
|
||||||
version: link:../log
|
version: link:../log
|
||||||
|
pretty-print-error:
|
||||||
|
specifier: ^1.1.2
|
||||||
|
version: 1.1.2(patch_hash=4arrfgbz7em6s4gqywse7esg4u)
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@homarr/eslint-config':
|
'@homarr/eslint-config':
|
||||||
specifier: workspace:^0.2.0
|
specifier: workspace:^0.2.0
|
||||||
|
|||||||
Reference in New Issue
Block a user