Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

1.3 KB
Raw Blame
type PromiseVoid = () => Promise<void>;

/** @internal */
export interface AnonymizeProxySugarOptions {
    ignoreProxyCertificate?: boolean;
}

/** @internal */
export const anonymizeProxySugar = async (
    proxyUrl?: string,
    username?: string,
    password?: string,
    options?: AnonymizeProxySugarOptions,
): Promise<[string | undefined, PromiseVoid]> => {
    if (proxyUrl) {
        const url = new URL(proxyUrl);

        if (username || password) {
            url.username = username ?? '';
            url.password = password ?? '';
        }

        if (url.username || url.password || options?.ignoreProxyCertificate) {
            // trim off trailing slash if it's present
            const proxyUrlString = url.href.endsWith('/') ? url.href.slice(0, -1) : url.href;
            const { anonymizeProxy, closeAnonymizedProxy } = await import('proxy-chain');
            const anonymized = await anonymizeProxy({
                url: proxyUrlString,
                port: 0,
                ignoreProxyCertificate: options?.ignoreProxyCertificate ?? false,
            });

            return [
                anonymized,
                async () => {
                    await closeAnonymizedProxy(anonymized, true);
                },
            ];
        }

        return [undefined, async () => {}];
    }

    return [undefined, async () => {}];
};