Skip to content

Commit

Permalink
feat: Configure global undici dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Dec 6, 2024
1 parent 7bee7ac commit 768d6b7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/domain/gitlab-wiki-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "path";
import fs from "fs-extra";

import serverEnv from "src/env/server";
import { setupUndiciHttpAgent } from "src/pages/api/http-agent";

import { getWikiPage as getStaticWikiPage } from "./gitlab-wiki-static";

Expand Down Expand Up @@ -36,6 +37,8 @@ const fetchWithTimeout = async (
? (id = setTimeout(() => reject(new Error("Timeout")), timeout))
: undefined
);
setupUndiciHttpAgent();

const fetcher = await fetch(url, options);
if (id !== undefined) {
clearTimeout(id);
Expand Down
18 changes: 18 additions & 0 deletions src/pages/api/http-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ProxyAgent, setGlobalDispatcher } from "undici";

declare global {
// eslint-disable-next-line no-var
var __undiciProxyAgent__: ProxyAgent | undefined;
}

export const setupUndiciHttpAgent = () => {
const HTTP_PROXY = process.env.HTTP_PROXY;
if (!HTTP_PROXY) {
return;
}
if (!globalThis.__undiciProxyAgent__) {
console.log("Setting up undici proxy agent: ", HTTP_PROXY);
globalThis.__undiciProxyAgent__ = new ProxyAgent(HTTP_PROXY);
setGlobalDispatcher(globalThis.__undiciProxyAgent__);
}
};

0 comments on commit 768d6b7

Please sign in to comment.