simplified httpclient lib

This commit is contained in:
Bojan Kucera 2025-06-07 12:57:55 -04:00
parent 38b523d2c0
commit f70a8be1cb
6 changed files with 256 additions and 203 deletions

View file

@ -1,10 +1,12 @@
import { ProxyAgent } from 'undici';
import got from 'got';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
import type { ProxyConfig } from './types.js';
export class ProxyManager {
/**
* Determine if we should use Bun fetch (HTTP/HTTPS) or Undici (SOCKS)
* Determine if we should use Bun fetch (HTTP/HTTPS) or Got (SOCKS)
*/
static shouldUseBunFetch(proxy: ProxyConfig): boolean {
return proxy.protocol === 'http' || proxy.protocol === 'https';
@ -23,35 +25,56 @@ export class ProxyManager {
}
/**
* Create agent for SOCKS proxies (used with undici)
* Create appropriate agent for Got based on proxy type
*/
static createSocksAgent(proxy: ProxyConfig): SocksProxyAgent {
const { protocol, host, port, username, password } = proxy;
static createGotAgent(proxy: ProxyConfig) {
this.validateConfig(proxy);
let proxyUrl: string;
if (username && password) {
proxyUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
} else {
proxyUrl = `${protocol}://${host}:${port}`;
}
const proxyUrl = this.buildProxyUrl(proxy);
return new SocksProxyAgent(proxyUrl);
switch (proxy.protocol) {
case 'socks4':
case 'socks5':
return new SocksProxyAgent(proxyUrl);
case 'http':
return new HttpProxyAgent(proxyUrl);
case 'https':
return new HttpsProxyAgent(proxyUrl);
default:
throw new Error(`Unsupported proxy protocol: ${proxy.protocol}`);
}
}
/**
* Create Undici proxy agent for HTTP/HTTPS proxies (fallback)
* Create Got instance with proxy configuration
*/
static createUndiciAgent(proxy: ProxyConfig): ProxyAgent {
static createGotInstance(proxy: ProxyConfig) {
const agent = this.createGotAgent(proxy);
return got.extend({
agent: {
http: agent,
https: agent
},
timeout: {
request: 30000,
connect: 10000
},
retry: {
limit: 3,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
},
throwHttpErrors: false // We'll handle errors ourselves
});
}
private static buildProxyUrl(proxy: ProxyConfig): string {
const { protocol, host, port, username, password } = proxy;
let proxyUrl: string;
if (username && password) {
proxyUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
} else {
proxyUrl = `${protocol}://${host}:${port}`;
return `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
}
return new ProxyAgent(proxyUrl);
return `${protocol}://${host}:${port}`;
}
/**