cloudfunctions/ClearingRedundantFolders/node_modules/@cloudbase/manager-node/src/utils/http-request.ts

(完整代码) 1KB

import { URL } from 'url'
import _fetch from 'node-fetch'
import HttpsProxyAgent from 'https-proxy-agent'
import { CloudBaseError } from '../error'

// 使用 fetch + 代理
export async function fetch(url: string, config: Record<string, any> = {}, proxy) {
    if (proxy || process.env.http_proxy) {
        config.agent = new HttpsProxyAgent(proxy || process.env.http_proxy)
    }
    // 解决中文编码问题
    const escapeUrl = new URL(url).toString()
    let json
    let text
    try {
        const res = await _fetch(escapeUrl, config)
        text = await res.text()
        json = JSON.parse(text)
    } catch (e) {
        // 某些情况下回返回 HTML 文本异常
        // JSON 解析错误,抛出原响应文本
        if (e.name === 'SyntaxError') {
            throw new CloudBaseError(text)
        }
        throw new CloudBaseError(e)
    }
    return json
}

export async function fetchStream(url: string, config: Record<string, any> = {}, proxy) {
    if (proxy || process.env.http_proxy) {
        config.agent = new HttpsProxyAgent(proxy || process.env.http_proxy)
    }
    // 解决中文编码问题
    const escapeUrl = new URL(url).toString()
    return _fetch(escapeUrl, config)
}

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。