MCPcopy Create free account
hub / github.com/Script-Hub-Org/Script-Hub / http

Function http

rule-parser.js:414–441  ·  view source on GitHub ↗
(url, opts = {})

Source from the content-addressed store, hash-verified

412
413// 请求
414async 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}
442function done(...args) {
443 $.log(`⏱ 总耗时:${Math.round(((Date.now() - script_start) / 1000) * 100) / 100} 秒`)
444 $.done(...args)

Callers 1

rule-parser.jsFile · 0.70

Calls 5

isSurgeMethod · 0.45
getMethod · 0.45
logMethod · 0.45
logErrMethod · 0.45
lodash_getMethod · 0.45

Tested by

no test coverage detected