UNPKG

fast-proxy-balancer

Version:
162 lines (108 loc) โ€ข 4.41 kB
# fast-proxy-balancer > TypeScript-ready, fast, and extensible proxy balancer using `proxy-agent`. Supports all proxy types: `http`, `https`, `socks`, PAC, and more. ![npm](https://img.shields.io/npm/v/fast-proxy-balancer?color=blue) ![license](https://img.shields.io/github/license/0xbadasya/fast-proxy-balancer) **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 โญ