Skip to content

Commit 4697d6c

Browse files
fix: resolve lint errors from merge
- Properly type proxy dispatcher with explicit return types - Extract port validation to separate function to fix max-lines-per-function - Improve type safety in proxy.ts without using eslint-disable Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
1 parent fd68098 commit 4697d6c

File tree

2 files changed

+36
-41
lines changed

2 files changed

+36
-41
lines changed

src/lib/proxy.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,24 @@ export function initProxyFromEnv(): void {
66
if (typeof Bun !== "undefined") return
77

88
try {
9-
const direct = new Agent()
9+
const direct = new Agent() as Dispatcher
1010
const proxies = new Map<string, ProxyAgent>()
1111

12-
// We only need a minimal dispatcher that implements `dispatch` at runtime.
13-
// Typing the object as `Dispatcher` forces TypeScript to require many
14-
// additional methods. Instead, keep a plain object and cast when passing
15-
// to `setGlobalDispatcher`.
16-
const dispatcher = {
12+
const dispatcher: Dispatcher = {
1713
dispatch(
1814
options: Dispatcher.DispatchOptions,
1915
handler: Dispatcher.DispatchHandler,
20-
) {
16+
): boolean {
2117
try {
2218
const origin =
2319
typeof options.origin === "string" ?
2420
new URL(options.origin)
2521
: (options.origin as URL)
26-
const get = getProxyForUrl as unknown as (
27-
u: string,
28-
) => string | undefined
29-
const raw = get(origin.toString())
22+
const raw = getProxyForUrl(origin.toString())
3023
const proxyUrl = raw && raw.length > 0 ? raw : undefined
3124
if (!proxyUrl) {
3225
consola.debug(`HTTP proxy bypass: ${origin.hostname}`)
33-
return (direct as unknown as Dispatcher).dispatch(options, handler)
26+
return direct.dispatch(options, handler)
3427
}
3528
let agent = proxies.get(proxyUrl)
3629
if (!agent) {
@@ -45,20 +38,20 @@ export function initProxyFromEnv(): void {
4538
/* noop */
4639
}
4740
consola.debug(`HTTP proxy route: ${origin.hostname} via ${label}`)
48-
return (agent as unknown as Dispatcher).dispatch(options, handler)
41+
return (agent as Dispatcher).dispatch(options, handler)
4942
} catch {
50-
return (direct as unknown as Dispatcher).dispatch(options, handler)
43+
return direct.dispatch(options, handler)
5144
}
5245
},
53-
close() {
46+
close(): Promise<void> {
5447
return direct.close()
5548
},
56-
destroy() {
49+
destroy(): Promise<void> {
5750
return direct.destroy()
5851
},
5952
}
6053

61-
setGlobalDispatcher(dispatcher as unknown as Dispatcher)
54+
setGlobalDispatcher(dispatcher)
6255
consola.debug("HTTP proxy configured from environment (per-URL)")
6356
} catch (err) {
6457
consola.debug("Proxy setup skipped:", err)

src/start.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ interface RunServerOptions {
2929
proxyEnv: boolean
3030
}
3131

32+
async function validatePort(port: number): Promise<void> {
33+
try {
34+
const testServer = await import("node:net").then((net) =>
35+
net.createServer(),
36+
)
37+
await new Promise<void>((resolve, reject) => {
38+
testServer.listen(port, () => {
39+
testServer.close(() => resolve())
40+
})
41+
testServer.on("error", (err: NodeJS.ErrnoException) => {
42+
if (err.code === "EADDRINUSE") {
43+
reject(new Error(`Port ${port} is already in use`))
44+
} else {
45+
reject(err)
46+
}
47+
})
48+
})
49+
} catch (error: unknown) {
50+
const errorMessage =
51+
error instanceof Error ? error.message : "Unknown error occurred"
52+
consola.error(errorMessage)
53+
process.exit(1)
54+
}
55+
}
56+
3257
export async function runServer(options: RunServerOptions): Promise<void> {
3358
if (options.proxyEnv) {
3459
initProxyFromEnv()
@@ -118,30 +143,7 @@ export async function runServer(options: RunServerOptions): Promise<void> {
118143
`🌐 Usage Viewer: https://ericc-ch.github.io/copilot-api?endpoint=${serverUrl}/usage`,
119144
)
120145

121-
// Check if port is already in use
122-
try {
123-
// Try to create a temporary server to test the port
124-
const testServer = await import("node:net").then((net) =>
125-
net.createServer(),
126-
)
127-
await new Promise<void>((resolve, reject) => {
128-
testServer.listen(options.port, () => {
129-
testServer.close(() => resolve())
130-
})
131-
testServer.on("error", (err: NodeJS.ErrnoException) => {
132-
if (err.code === "EADDRINUSE") {
133-
reject(new Error(`Port ${options.port} is already in use`))
134-
} else {
135-
reject(err)
136-
}
137-
})
138-
})
139-
} catch (error: unknown) {
140-
const errorMessage =
141-
error instanceof Error ? error.message : "Unknown error occurred"
142-
consola.error(errorMessage)
143-
process.exit(1)
144-
}
146+
await validatePort(options.port)
145147

146148
serve({
147149
fetch: server.fetch as ServerHandler,

0 commit comments

Comments
 (0)