Skip to main content
Home

API wrapper around the "VeePN" extension from the Firefox store (they use HTTP proxies)

This package works with Cloudflare Workers, Node.js, Deno, Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
JSR Score
76%
Published
2 months ago (0.0.1)
Package root>domainFetcher.ts
/** * Fetches all API domains from their S3 bucket, * filters for only working domains, and returns them. */ export interface Domains { /** main url for the free API */ free: string; /** main url for the premium API */ premium: string; domains: { /** all domains for the free API */ free: string[]; /** all domains for the premium API */ premium: string[]; }; } export interface Ret { main: string; domains: string[]; } export const MAIN_DOMAINS = { free: "https://antpeak.com", premium: "https://techvaso.com", }; /** Fetches the alternate domains */ export class DomainFetcher { /** * Fetches the alternate domains * @param [type="free"] the domain type. Free or Premium, use All for both. * @param [useMainDomains=false] just return the main domains lol */ static async fetchDomains( type: "free" | "premium" | "all" = "free", useMainDomains = false, ): Promise<Ret> { const data: Domains = await fetch( "https://s3-oregon-1.s3-us-west-2.amazonaws.com/api.json", ).then((r) => r.json()); const domains = []; switch (type) { case "free": domains.push(...data.domains.free); break; case "premium": domains.push(...data.domains.premium); break; case "all": domains.push(...data.domains.free, ...data.domains.premium); } const dm = await Promise.all(domains.map(async (domain: string) => { const available = await DomainFetcher.isAvailable(domain); return available ? domain : null; })); const nat = type === "all" ? "free" : type; return { main: (useMainDomains ? MAIN_DOMAINS : data)[nat], domains: dm.filter((d): d is string => d !== null), }; } /** * checks if a domain is available by `GET`ting /api/available, * checking if the response is ok, and the `success` field is true. * If the server doesn't respond in 4.5 seconds, * it will be skipped. */ private static async isAvailable(domain: string): Promise<boolean> { // console.time(`Checking domain ${domain}`); try { const res = await fetch(new URL("/api/available", domain), { signal: AbortSignal.timeout(4.5e3), }); // console.timeEnd(`Checking domain ${domain}`); if (!res.ok) return false; const j = await res.json(); return j.success && j.errors.length === 0; } catch (_) { // console.warn(`Error checking domain ${domain}:`, _); // console.timeEnd(`Checking domain ${domain}`); return false; } } } export default DomainFetcher;