summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/npm/node_modules/funcaptcha/src/http.ts
blob: e0fa3acba6823057014adf651d578ab937ddc713 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { request, ProxyAgent } from "undici";
// @ts-ignore
import { RequestOptions } from "undici/types/dispatcher";

async function req(url: string, options: RequestOptions, proxy?: string) {
    let auth = undefined;
    if (proxy) {
        let proxyUrl = new URL(proxy);
        if(proxyUrl.username && proxyUrl.password) {
            auth = Buffer.from(proxyUrl.username + ":" + proxyUrl.password).toString("base64")
        }
    }
    let dispatcher = proxy ? new ProxyAgent({
        uri: proxy,
        auth
    }) : undefined;

    let req = await request(url, {
        ...options,
        dispatcher,
    });
    return {
        headers: req.headers,
        body: Buffer.from(await req.body.arrayBuffer()),
    };
}

export default req;