(url, opts = {})
| 412 | |
| 413 | // 请求 |
| 414 | async function http(url, opts = {}) { |
| 415 | const http_start = Date.now() |
| 416 | let timeout = HTTP_TIMEOUT + 1 * 1000 |
| 417 | timeout = $.isSurge() ? timeout / 1000 : timeout |
| 418 | const reqOpts = { |
| 419 | timeout, |
| 420 | url, |
| 421 | ...opts, |
| 422 | } |
| 423 | try { |
| 424 | const res = await Promise.race([ |
| 425 | $.http.get(reqOpts), |
| 426 | new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), HTTP_TIMEOUT)), |
| 427 | ]) |
| 428 | $.log(`⏱ 请求耗时:${Math.round(((Date.now() - http_start) / 1000) * 100) / 100} 秒\n └ ${reqOpts.url}`) |
| 429 | return res |
| 430 | } catch (e) { |
| 431 | $.logErr(e) |
| 432 | let msg = String($.lodash_get(e, 'message') || e) |
| 433 | let info |
| 434 | if (msg.includes('timeout')) { |
| 435 | info = `请求超时(${Math.round((HTTP_TIMEOUT / 1000) * 100) / 100} 秒)` |
| 436 | } else { |
| 437 | throw new Error(e) |
| 438 | } |
| 439 | throw new Error(info) |
| 440 | } |
| 441 | } |
| 442 | function done(...args) { |
| 443 | $.log(`⏱ 总耗时:${Math.round(((Date.now() - script_start) / 1000) * 100) / 100} 秒`) |
| 444 | $.done(...args) |
no test coverage detected