feat: add image-proxy key ttl and shared http dispatcher
This commit is contained in:
1 parent
3ff0d72ba1
commit
e1ff84830f
3 files changed
+69
-11
No files matched your search
@@ -9,11 +9,18 @@ import {
|
||||
getAllTrustedCertificatesAsync,
|
||||
getTrustedCertificateHostnamesAsync,
|
||||
} from "@homarr/core/infrastructure/certificates";
|
||||
import { UndiciHttpAgent } from "@homarr/core/infrastructure/http";
|
||||
import { UndiciHttpAgent } from "@homarr/core/infrastructure/http/http-agent";
|
||||
|
||||
import type { TrustedCertificateHostname } from "../certificates/hostnames";
|
||||
import { withTimeoutAsync } from "./timeout";
|
||||
|
||||
const CERTIFICATE_DISPATCHER_TTL_MS = 5 * 60 * 1000;
|
||||
let trustedCertificateDispatcher: {
|
||||
dispatcher: UndiciHttpAgent;
|
||||
expiresAt: number;
|
||||
} | null = null;
|
||||
let creatingTrustedCertificateDispatcherPromise: Promise<UndiciHttpAgent> | null = null;
|
||||
|
||||
export const createCustomCheckServerIdentity = (
|
||||
trustedHostnames: TrustedCertificateHostname[],
|
||||
): typeof checkServerIdentity => {
|
||||
@@ -41,6 +48,36 @@ export const createCertificateAgentAsync = async (override?: {
|
||||
});
|
||||
};
|
||||
|
||||
const getSharedCertificateAgentAsync = async () => {
|
||||
const now = Date.now();
|
||||
if (trustedCertificateDispatcher && trustedCertificateDispatcher.expiresAt > now) {
|
||||
return trustedCertificateDispatcher.dispatcher;
|
||||
}
|
||||
if (creatingTrustedCertificateDispatcherPromise) {
|
||||
return await creatingTrustedCertificateDispatcherPromise;
|
||||
}
|
||||
|
||||
creatingTrustedCertificateDispatcherPromise = createCertificateAgentAsync(undefined)
|
||||
.then((dispatcher) => {
|
||||
const previousDispatcher = trustedCertificateDispatcher?.dispatcher;
|
||||
trustedCertificateDispatcher = {
|
||||
dispatcher,
|
||||
expiresAt: Date.now() + CERTIFICATE_DISPATCHER_TTL_MS,
|
||||
};
|
||||
if (previousDispatcher) {
|
||||
void previousDispatcher.close().catch(() => {
|
||||
previousDispatcher.destroy();
|
||||
});
|
||||
}
|
||||
return dispatcher;
|
||||
})
|
||||
.finally(() => {
|
||||
creatingTrustedCertificateDispatcherPromise = null;
|
||||
});
|
||||
|
||||
return await creatingTrustedCertificateDispatcherPromise;
|
||||
};
|
||||
|
||||
export const createHttpsAgentAsync = async (override?: Pick<AgentOptions, "ca" | "checkServerIdentity">) => {
|
||||
return new HttpsAgent({
|
||||
ca: await getAllTrustedCertificatesAsync(),
|
||||
@@ -63,7 +100,7 @@ export const fetchWithTrustedCertificatesAsync = async (
|
||||
url: RequestInfo,
|
||||
options?: RequestInit & { timeout?: number },
|
||||
): Promise<Response> => {
|
||||
const agent = await createCertificateAgentAsync(undefined);
|
||||
const agent = await getSharedCertificateAgentAsync();
|
||||
if (options?.timeout) {
|
||||
return await withTimeoutAsync(
|
||||
async (signal) =>
|
||||
|
||||
Reference in new issue
Block a user