fast-proxy-balancer
Version:
Smart and flexible proxy balancer for Node.js
162 lines (108 loc) โข 4.41 kB
Markdown
# fast-proxy-balancer
> TypeScript-ready, fast, and extensible proxy balancer using `proxy-agent`. Supports all proxy types: `http`, `https`, `socks`, PAC, and more.


**Smart and flexible proxy balancer for Node.js**, written in TypeScript. Automatically selects the best proxy based on performance. Built-in testing, failure handling, real-time stats, and CLI/debug tooling.
## ๐ Features
- โ
Auto-testing proxies (latency, connectivity)
- โ๏ธ Smart selection based on performance
- โ Failure counting & exclusion logic
- ๐ Auto-refreshing with concurrency limit
- ๐ Proxy stats, summaries, latency history
- ๐ Load from array or `.txt` file
- ๐งฑ Type-safe and fully written in TypeScript
- ๐ Works with any proxy supported by `proxy-agent`
## ๐ฆ Installation
```bash
npm install fast-proxy-balancer
```
## ๐ป Quick Start
```ts
import { ProxyBalancer } from 'fast-proxy-balancer';
const balancer = new ProxyBalancer([
'http://proxy1.com',
'http://proxy2.com'
], {
testUrl: 'https://example.com',
refreshInterval: 30000
});
await balancer.init();
await balancer.refreshProxies();
const best = await balancer.getBestProxy();
console.log('๐ Best proxy:', best.url);
```
Or load proxies from a file:
```ts
const balancer = new ProxyBalancer('./proxies.txt');
```
> ๐ก Text file should contain one proxy URL per line. `#` starts a comment.
## ๐ง Constructor Options
| Option | Type | Default | Description |
|-------------------|----------|----------------------|------------------------------------------------------|
| `maxFailures` | number | `3` | Max allowed failures before excluding a proxy |
| `testTimeout` | number | `5000` | Timeout (ms) for testing proxy connectivity |
| `refreshInterval` | number | `60000` | Interval (ms) for automatic proxy re-testing |
| `concurrentTests` | number | `5` | Number of proxies to test in parallel |
| `testUrl` | string | `'https://example.com'` | URL used to test proxies |
## ๐ API Reference
### `await balancer.init()`
Initializes the proxy list. Call before using.
### `await balancer.refreshProxies()`
Runs latency tests, resets failures, updates metrics.
### `await balancer.getBestProxy()` โ `{ url, agent }`
Returns the best proxy and its agent instance (from `proxy-agent`).
### `balancer.getProxyStats()` โ `ProxyStats[]`
Detailed stats for all proxies (latency, failures, last used).
### `balancer.sortByLatency()` โ `ProxyStats[]`
All proxies sorted from fastest to slowest.
### `balancer.getFailedProxies()` โ `ProxyStats[]`
List of proxies that failed too often.
### `balancer.getStatsSummary()` โ `{ total, active, failed, avgLatency }`
Returns overall pool summary.
### `balancer.startAutoRefresh()`
Starts background auto-refresh loop based on `refreshInterval`.
## ๐ก Advanced Usage
### ๐ง Use with axios:
```ts
const { agent } = await balancer.getBestProxy();
const res = await axios.get('https://example.com', {
httpAgent: agent,
httpsAgent: agent,
timeout: 3000
});
```
### ๐งช Manual test
```bash
npm run build && node dist/test/test-manual.js
```
## ๐งฑ Built with TypeScript
- Full `.d.ts` typings
- Exports included via `dist/index.d.ts`
- Works in strict mode with ESM
## ๐ง Roadmap
- [ ] Redis support for shared state
- [ ] Web dashboard (live updates)
- [ ] Event hooks: `onFail`, `onRecover`, etc.
- [ ] JSON logging/export for analytics
- [ ] Built-in CLI
## ๐ฆ Final Notes
- โ
Supports **any proxy type** that `proxy-agent` can handle:
- `http://`, `https://`
- `socks://`, `socks4://`, `socks5://`
- PAC files and custom proxy protocols
- ๐ Works seamlessly with authenticated proxies (via user:pass in the URL)
## ๐ License
MIT
## ๐ช Author
MIT ยฉ [badasya](https://github.com/0xbadasya) โ PRs and stars welcome โญ