refactor of data-service

This commit is contained in:
Boki 2025-06-12 08:03:09 -04:00
parent 6fb98c69f2
commit 09c97df1a8
49 changed files with 2394 additions and 112 deletions

18
libs/proxy/package.json Normal file
View file

@ -0,0 +1,18 @@
{
"name": "@stock-bot/proxy",
"version": "1.0.0",
"description": "Simple proxy management library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"build": "tsc",
"test": "bun test",
"dev": "tsc --watch"
},
"devDependencies": {
"@types/node": "^20.0.0",
"typescript": "^5.0.0"
},
"peerDependencies": {}
}

97
libs/proxy/src/index.ts Normal file
View file

@ -0,0 +1,97 @@
// Simple proxy list manager
let proxies: string[] = [];
let currentIndex = 0;
const DEFAULT_PROXY_URL =
'https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all';
/**
* Fetch proxy list from URL and store in module
*/
export async function refreshProxies(fetchUrl: string = DEFAULT_PROXY_URL): Promise<string[]> {
try {
const response = await fetch(fetchUrl);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.text();
const newProxies = data
.trim()
.split('\n')
.map(line => line.trim())
.filter(line => line && line.includes(':'))
.map(line => {
// Convert host:port to http://host:port format
return line.startsWith('http') ? line : `http://${line}`;
});
proxies = newProxies;
currentIndex = 0;
return proxies;
} catch (error) {
throw new Error(`Failed to fetch proxies: ${error}`);
}
}
/**
* Get next proxy URL in round-robin fashion
*/
export function getProxyURL(): string | null {
if (proxies.length === 0) {
return null;
}
const proxy = proxies[currentIndex];
currentIndex = (currentIndex + 1) % proxies.length;
return proxy;
}
/**
* Get multiple proxy URLs
*/
export function getProxyURLs(count: number): string[] {
const urls: string[] = [];
for (let i = 0; i < count; i++) {
const url = getProxyURL();
if (url) {
urls.push(url);
}
}
return urls;
}
/**
* Get random proxy URL
*/
export function getRandomProxyURL(): string | null {
if (proxies.length === 0) {
return null;
}
const randomIndex = Math.floor(Math.random() * proxies.length);
return proxies[randomIndex];
}
/**
* Get current proxy count
*/
export function getProxyCount(): number {
return proxies.length;
}
/**
* Get all proxies
*/
export function getAllProxies(): string[] {
return [...proxies];
}
/**
* Initialize proxy manager with initial fetch
*/
export async function initializeProxies(fetchUrl?: string): Promise<void> {
await refreshProxies(fetchUrl);
}

View file

22
libs/proxy/src/types.ts Normal file
View file

@ -0,0 +1,22 @@
export interface ProxyInfo {
host: string;
port: number;
protocol: 'http' | 'https' | 'socks4' | 'socks5';
username?: string;
password?: string;
country?: string;
isActive?: boolean;
}
export interface ProxyManagerOptions {
fetchUrl?: string;
refreshIntervalMs?: number;
maxRetries?: number;
timeout?: number;
}
export interface ProxyResponse {
proxies: ProxyInfo[];
totalCount: number;
activeCount: number;
}

10
libs/proxy/tsconfig.json Normal file
View file

@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.lib.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
}