(url, opts = {})
| 2442 | } |
| 2443 | // 请求 |
| 2444 | async function http(url, opts = {}) { |
| 2445 | const http_start = Date.now() |
| 2446 | let timeout = HTTP_TIMEOUT + 1 * 1000 |
| 2447 | timeout = $.isSurge() ? timeout / 1000 : timeout |
| 2448 | const reqOpts = { |
| 2449 | timeout, |
| 2450 | url, |
| 2451 | ...opts, |
| 2452 | } |
| 2453 | try { |
| 2454 | const res = await Promise.race([ |
| 2455 | $.http.get(reqOpts), |
| 2456 | new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), HTTP_TIMEOUT)), |
| 2457 | ]) |
| 2458 | $.log(`⏱ 请求耗时:${Math.round(((Date.now() - http_start) / 1000) * 100) / 100} 秒\n └ ${reqOpts.url}`) |
| 2459 | return res |
| 2460 | } catch (e) { |
| 2461 | $.logErr(e) |
| 2462 | let msg = String($.lodash_get(e, 'message') || e) |
| 2463 | let info |
| 2464 | if (msg.includes('timeout')) { |
| 2465 | info = `请求超时(${Math.round((HTTP_TIMEOUT / 1000) * 100) / 100} 秒)` |
| 2466 | } else { |
| 2467 | throw new Error(e) |
| 2468 | } |
| 2469 | throw new Error(info) |
| 2470 | } |
| 2471 | } |
| 2472 | function parseJsonPath(_path) { |
| 2473 | const path = _path.trim() |
| 2474 | const output = [] |
no test coverage detected