(url, opts = {})
| 2854 | } |
| 2855 | // 请求 |
| 2856 | async function http(url, opts = {}) { |
| 2857 | const http_start = Date.now() |
| 2858 | let timeout = HTTP_TIMEOUT + 1 * 1000 |
| 2859 | timeout = $.isSurge() ? timeout / 1000 : timeout |
| 2860 | const reqOpts = { |
| 2861 | timeout, |
| 2862 | url, |
| 2863 | ...opts, |
| 2864 | } |
| 2865 | try { |
| 2866 | const res = await Promise.race([ |
| 2867 | $.http.get(reqOpts), |
| 2868 | new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), HTTP_TIMEOUT)), |
| 2869 | ]) |
| 2870 | $.log(`⏱ 请求耗时:${Math.round(((Date.now() - http_start) / 1000) * 100) / 100} 秒\n └ ${reqOpts.url}`) |
| 2871 | return res |
| 2872 | } catch (e) { |
| 2873 | $.logErr(e) |
| 2874 | let msg = String($.lodash_get(e, 'message') || e) |
| 2875 | let info |
| 2876 | if (msg.includes('timeout')) { |
| 2877 | info = `请求超时(${Math.round((HTTP_TIMEOUT / 1000) * 100) / 100} 秒)` |
| 2878 | } else { |
| 2879 | throw new Error(e) |
| 2880 | } |
| 2881 | throw new Error(info) |
| 2882 | } |
| 2883 | } |
| 2884 | function parseJsonPath(_path) { |
| 2885 | const path = _path.trim() |
| 2886 | const output = [] |
no test coverage detected