-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
300 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// proxyFetch.node.ts | ||
import { ProxyAgent, fetch as undici } from "undici"; | ||
import type { ProxyFetchFn } from "./proxyFetch"; | ||
|
||
export const createProxyFetch = async (): Promise<ProxyFetchFn | undefined> => { | ||
const proxyUrl = | ||
process.env.HTTP_PROXY || | ||
process.env.HTTPS_PROXY || | ||
process.env.http_proxy || | ||
process.env.https_proxy; | ||
|
||
if (!proxyUrl) { | ||
return undefined; | ||
} | ||
|
||
const dispatcher = new ProxyAgent(proxyUrl); | ||
|
||
return function proxy( | ||
...args: Parameters<typeof fetch> | ||
): ReturnType<typeof fetch> { | ||
// @ts-expect-error `undici` has a `duplex` option | ||
return undici(args[0], { ...args[1], dispatcher }); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,11 @@ | ||
// proxyFetch.ts | ||
let proxyFetch: typeof fetch | undefined; | ||
|
||
export async function initProxyFetch() { | ||
const proxyUrl = | ||
process.env.HTTP_PROXY || | ||
process.env.HTTPS_PROXY || | ||
process.env.http_proxy || | ||
process.env.https_proxy; | ||
// We only export the type from this file | ||
export type ProxyFetchFn = ( | ||
...args: Parameters<typeof fetch> | ||
) => ReturnType<typeof fetch>; | ||
|
||
if (proxyUrl && typeof window === "undefined") { | ||
const { ProxyAgent, fetch: undici } = await import("undici"); | ||
const dispatcher = new ProxyAgent(proxyUrl); | ||
return (...args: Parameters<typeof fetch>): ReturnType<typeof fetch> => { | ||
// @ts-expect-error `undici` has a `duplex` option | ||
return undici(args[0], { ...args[1], dispatcher }); | ||
}; | ||
} | ||
// This function will be imported dynamically only in Node.js environment | ||
export const createProxyFetch = async (): Promise<ProxyFetchFn | undefined> => { | ||
return undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.