Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
"consola": "^3.4.2",
"fetch-event-stream": "^0.1.5",
"gpt-tokenizer": "^3.0.1",
"hono": "^4.9.6",
"srvx": "^0.8.7",
"hono": "^4.9.4",
"proxy-from-env": "^1.1.0",
"srvx": "^0.8.6",
"tiny-invariant": "^1.3.3",
"undici": "^6.19.8",
},
"devDependencies": {
"@echristian/eslint-config": "^0.0.54",
Expand Down Expand Up @@ -762,6 +764,8 @@

"prettier-plugin-packagejson": ["prettier-plugin-packagejson@2.5.19", "", { "dependencies": { "sort-package-json": "3.4.0", "synckit": "0.11.11" }, "peerDependencies": { "prettier": ">= 1.16.0" }, "optionalPeers": ["prettier"] }, "sha512-Qsqp4+jsZbKMpEGZB1UP1pxeAT8sCzne2IwnKkr+QhUe665EXUo3BAvTf1kAPCqyMv9kg3ZmO0+7eOni/C6Uag=="],

"proxy-from-env": ["proxy-from-env@1.1.0", "", {}, "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="],

"punycode": ["punycode@2.3.1", "", {}, "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg=="],

"quansync": ["quansync@0.2.11", "", {}, "sha512-AifT7QEbW9Nri4tAwR5M/uzpBuqfZf+zwaEM/QkzEjj7NBuFD2rBuy0K3dE+8wltbezDV7JMA0WfnCPYRSYbXA=="],
Expand Down Expand Up @@ -918,6 +922,8 @@

"unconfig": ["unconfig@7.3.3", "", { "dependencies": { "@quansync/fs": "^0.1.5", "defu": "^6.1.4", "jiti": "^2.5.1", "quansync": "^0.2.11" } }, "sha512-QCkQoOnJF8L107gxfHL0uavn7WD9b3dpBcFX6HtfQYmjw2YzWxGuFQ0N0J6tE9oguCBJn9KOvfqYDCMPHIZrBA=="],

"undici": ["undici@6.21.3", "", {}, "sha512-gBLkYIlEnSp8pFbT64yFgGE6UIB9tAkhukC23PmMDCe5Nd+cRqKxSjw5y54MK2AZMgZfJWMaNE4nYUHgi1XEOw=="],

"undici-types": ["undici-types@7.8.0", "", {}, "sha512-9UJ2xGDvQ43tYyVMpuHlsgApydB8ZKfVYTsLDhXkFL/6gfkp+U8xTGdh8pMJv1SpZna0zxG1DwsKZsreLbXBxw=="],

"update-browserslist-db": ["update-browserslist-db@1.1.3", "", { "dependencies": { "escalade": "^3.2.0", "picocolors": "^1.1.1" }, "peerDependencies": { "browserslist": ">= 4.21.0" }, "bin": { "update-browserslist-db": "cli.js" } }, "sha512-UxhIZQ+QInVdunkDAaiazvvT/+fXL5Osr0JZlJulepYu6Jd7qJtDZjlur0emRlT71EN3ScPoE7gvsuIKKNavKw=="],
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
"consola": "^3.4.2",
"fetch-event-stream": "^0.1.5",
"gpt-tokenizer": "^3.0.1",
"hono": "^4.9.6",
"srvx": "^0.8.7",
"tiny-invariant": "^1.3.3"
"hono": "^4.9.4",
"proxy-from-env": "^1.1.0",
"srvx": "^0.8.6",
"tiny-invariant": "^1.3.3",
"undici": "^6.19.8"
},
"devDependencies": {
"@echristian/eslint-config": "^0.0.54",
Expand Down
62 changes: 62 additions & 0 deletions src/lib/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import consola from "consola"
import { getProxyForUrl } from "proxy-from-env"
import { Agent, ProxyAgent, setGlobalDispatcher, type Dispatcher } from "undici"

export function initProxyFromEnv(): void {
if (typeof Bun !== "undefined") return

try {
const direct = new Agent()
const proxies = new Map<string, ProxyAgent>()

const dispatcher: Dispatcher = {
dispatch(
options: Dispatcher.RequestOptions,
handler: Dispatcher.DispatchHandlers,
) {
try {
const origin =
typeof options.origin === "string" ?
new URL(options.origin)
: (options.origin as URL)
const get = getProxyForUrl as unknown as (
u: string,
) => string | undefined
const raw = get(origin.toString())
const proxyUrl = raw && raw.length > 0 ? raw : undefined
if (!proxyUrl) {
consola.debug(`HTTP proxy bypass: ${origin.hostname}`)
return (direct as unknown as Dispatcher).dispatch(options, handler)
}
let agent = proxies.get(proxyUrl)
if (!agent) {
agent = new ProxyAgent(proxyUrl)
proxies.set(proxyUrl, agent)
}
let label = proxyUrl
try {
const u = new URL(proxyUrl)
label = `${u.protocol}//${u.host}`
} catch {
/* noop */
}
consola.debug(`HTTP proxy route: ${origin.hostname} via ${label}`)
return (agent as unknown as Dispatcher).dispatch(options, handler)
} catch {
return (direct as unknown as Dispatcher).dispatch(options, handler)
}
},
close() {
return direct.close()
},
destroy() {
return direct.destroy()
},
}

setGlobalDispatcher(dispatcher)
consola.debug("HTTP proxy configured from environment (per-URL)")
} catch (err) {
consola.debug("Proxy setup skipped:", err)
}
}
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { defineCommand, runMain } from "citty"
import { auth } from "./auth"
import { checkUsage } from "./check-usage"
import { debug } from "./debug"
import { initProxyFromEnv } from "./lib/proxy"
import { start } from "./start"

const main = defineCommand({
Expand All @@ -16,4 +17,6 @@ const main = defineCommand({
subCommands: { auth, start, "check-usage": checkUsage, debug },
})

initProxyFromEnv()

await runMain(main)