(opt: any)
| 442 | } |
| 443 | |
| 444 | async proxyRequest(opt: any) { |
| 445 | const urlBody = new URLSearchParams(opt.form); |
| 446 | const reqOptions: Dict = { |
| 447 | method: opt.method, |
| 448 | headers: opt.headers, |
| 449 | }; |
| 450 | if (opt.method !== 'GET') { |
| 451 | reqOptions.body = urlBody; |
| 452 | } |
| 453 | if (this.Options.verbose) { |
| 454 | this.Options.log('HTTP Request:', opt.method, opt.url, reqOptions); |
| 455 | } |
| 456 | |
| 457 | // https-proxy |
| 458 | const httpsproxy = this.getHttpsProxy(); |
| 459 | const socksproxy = this.getSocksProxy(); |
| 460 | const urlProxy = this.getUrlProxy(); |
| 461 | if (httpsproxy) { |
| 462 | if (this.Options.verbose) this.Options.log('using https proxy: ' + httpsproxy); |
| 463 | reqOptions.agent = new HttpsProxyAgent(httpsproxy); |
| 464 | } else if (socksproxy) { |
| 465 | if (this.Options.verbose) this.Options.log('using socks proxy: ' + socksproxy); |
| 466 | reqOptions.agent = new SocksProxyAgent(socksproxy); |
| 467 | } |
| 468 | |
| 469 | if (urlProxy) { |
| 470 | opt.url = urlProxy + opt.url; |
| 471 | } |
| 472 | |
| 473 | // Apply timeout via AbortController |
| 474 | const timeout = opt.timeout || this.Options.recvWindow || 30000; |
| 475 | const controller = new AbortController(); |
| 476 | const timeoutId = setTimeout(() => controller.abort(), timeout); |
| 477 | reqOptions.signal = controller.signal; |
| 478 | |
| 479 | let fetchImplementation = fetch; |
| 480 | // require node-fetch |
| 481 | if (reqOptions.agent) { |
| 482 | fetchImplementation = nodeFetch; |
| 483 | } |
| 484 | |
| 485 | try { |
| 486 | const response = await fetchImplementation(opt.url, reqOptions); |
| 487 | clearTimeout(timeoutId); |
| 488 | |
| 489 | await this.reqHandler(response); |
| 490 | const json = await response.json(); |
| 491 | |
| 492 | if (this.Options.verbose) { |
| 493 | this.Options.log('HTTP Response:', json); |
| 494 | } |
| 495 | return json; |
| 496 | } catch (error) { |
| 497 | clearTimeout(timeoutId); |
| 498 | if (error.name === 'AbortError') { |
| 499 | throw new Error(`Request timeout: ${opt.method} ${opt.url} (${timeout}ms)`); |
| 500 | } |
| 501 | throw error; |
no test coverage detected