import type { ConcreteNetwork, GeoTarget } from "@fetcha/core"; import { executeHttp } from "./http"; import type { ProviderHealth, ProviderRequest, ProviderResponse, ProxyEndpoint, ProxyProvider } from "./types"; import { probeHealth } from "./probe"; /** * Decodo (formerly Smartproxy) residential network. Gateway `gate.decodo.com`. * Port 7000 = rotating; ports 10001+ = sticky by port. We use the username grammar instead: * `user-[-country-xx][-state-us_xx][-city-xxx][-session-][-sessionduration-]`. * Documentation: https://help.decodo.com/docs/residential-authentication-methods */ export class DecodoProvider implements ProxyProvider { readonly id = "decodo" as const; readonly label = "Decodo"; readonly networks: ConcreteNetwork[] = ["residential"]; private readonly username: string | undefined; private readonly password: string | undefined; private readonly prices: Record; constructor(env: NodeJS.ProcessEnv = process.env, prices: Record = { residential: 7 }) { this.username = env.DECODO_USERNAME?.trim() || undefined; this.password = env.DECODO_PASSWORD?.trim() || undefined; this.prices = prices; } isConfigured(): boolean { return Boolean(this.username && this.password); } supportsGeo(_geo: GeoTarget): boolean { return true; } pricePerGb(network: ConcreteNetwork): number { return this.prices[network] ?? this.prices.residential ?? 7; } estimateCost(network: ConcreteNetwork, bytes: number): number { return (bytes / 1_073_741_824) * this.pricePerGb(network); } /** Build the upstream gateway endpoint for a request (exposed for contract tests; never returned to customers). */ endpoint(req: Pick): ProxyEndpoint { if (!this.username || !this.password) throw new Error("Decodo is not configured"); const base = this.username.replace(/^user-/, ""); let user = `user-${base}`; if (req.geo.country) user += `-country-${req.geo.country.toLowerCase()}`; if (req.geo.region && req.geo.country === "US") user += `-state-us_${req.geo.region}`; if (req.geo.city) user += `-city-${req.geo.city.replace(/_/g, "")}`; if (req.sessionKey) { user += `-session-${sanitize(req.sessionKey)}`; user += `-sessionduration-${Math.min(Math.max(req.sessionMinutes ?? 10, 1), 30)}`; } return { host: "gate.decodo.com", port: 7000, username: user, password: this.password }; } proxyEndpoint(req: Pick): ProxyEndpoint | null { return this.endpoint(req); } async fetch(request: ProviderRequest): Promise { return executeHttp(this.id, this.endpoint(request), request); } async health(network: ConcreteNetwork = "residential"): Promise { if (!this.isConfigured()) return { provider: this.id, network, status: "unconfigured", latencyMs: null, checkedAt: new Date(), detail: "Missing credentials" }; return probeHealth(this, network, "https://ip.decodo.com/json"); } } function sanitize(s: string): string { return s.replace(/[^a-zA-Z0-9]/g, "").slice(0, 32) || "s"; }