1- import http , { Agent , RequestOptions } from 'node:http'
2- import { Agent as httpsAgent } from 'https'
1+ import http , { RequestOptions , IncomingMessage , ClientRequest } from 'node:http'
2+ import https from 'node: https'
33import Url , { URL } from 'node:url'
4+ import HttpsProxyAgent from 'https-proxy-agent'
45
56import {
67 isNumber ,
@@ -62,8 +63,15 @@ function handleRequestConfig(
6263 const { protocol, hostname, port, pathname, search } = new Url . URL (
6364 rawConfig . url
6465 )
66+ const isHttp = protocol === 'http:'
6567
6668 const config : RequestOptions & IMapTypeEmptyObject < URL > = {
69+ agent : rawConfig . proxy
70+ ? HttpsProxyAgent ( rawConfig . proxy )
71+ : isHttp
72+ ? new http . Agent ( )
73+ : new https . Agent ( ) ,
74+
6775 protocol,
6876 hostname,
6977 port,
@@ -77,46 +85,17 @@ function handleRequestConfig(
7785
7886 config . headers = parseHeaders ( rawConfig , config )
7987
80- if ( protocol === 'http:' ) {
81- config . agent = new Agent ( )
82- } else {
83- config . agent = new httpsAgent ( )
84- }
85-
8688 return config
8789}
8890
89- async function useSleepByBatch (
90- isHaveIntervalTime : boolean ,
91- isNumberIntervalTime : boolean ,
92- intervalTime : any ,
93- id : number
94- ) {
95- if ( isHaveIntervalTime && id > 1 ) {
96- const timeout : number = isNumberIntervalTime
97- ? intervalTime
98- : random ( intervalTime . max , intervalTime . min )
99-
100- log (
101- `Request ${ logNumber ( id ) } needs to sleep for ${ logNumber (
102- timeout + 'ms'
103- ) } milliseconds before sending`
104- )
105-
106- await sleep ( timeout )
107- } else {
108- log ( `Request ${ logNumber ( id ) } does not need to sleep, send immediately` )
109- }
110- }
111-
11291export function request ( config : IRequestConfig ) {
11392 return new Promise < IRequest > ( ( resolve , reject ) => {
11493 const isDataUndefine = isUndefined ( config . data )
11594 config . data = ! isDataUndefine ? JSON . stringify ( config . data ) : config . data
11695
11796 const requestConfig = handleRequestConfig ( config )
11897
119- const req = http . request ( requestConfig , ( res ) => {
98+ function handleRes ( res : IncomingMessage ) {
12099 const { statusCode, headers } = res
121100
122101 const container : Buffer [ ] = [ ]
@@ -133,7 +112,14 @@ export function request(config: IRequestConfig) {
133112
134113 resolve ( resolveRes )
135114 } )
136- } )
115+ }
116+
117+ let req : ClientRequest
118+ if ( requestConfig . protocol === 'http:' ) {
119+ req = http . request ( requestConfig , handleRes )
120+ } else {
121+ req = https . request ( requestConfig , handleRes )
122+ }
137123
138124 req . on ( 'timeout' , ( ) => {
139125 reject ( new Error ( `Timeout ${ config . timeout } ms` ) )
@@ -152,6 +138,29 @@ export function request(config: IRequestConfig) {
152138 } )
153139}
154140
141+ async function useSleepByBatch (
142+ isHaveIntervalTime : boolean ,
143+ isNumberIntervalTime : boolean ,
144+ intervalTime : any ,
145+ id : number
146+ ) {
147+ if ( isHaveIntervalTime && id > 1 ) {
148+ const timeout : number = isNumberIntervalTime
149+ ? intervalTime
150+ : random ( intervalTime . max , intervalTime . min )
151+
152+ log (
153+ `Request ${ logNumber ( id ) } needs to sleep for ${ logNumber (
154+ timeout + 'ms'
155+ ) } milliseconds before sending`
156+ )
157+
158+ await sleep ( timeout )
159+ } else {
160+ log ( `Request ${ logNumber ( id ) } does not need to sleep, send immediately` )
161+ }
162+ }
163+
155164export async function batchRequest (
156165 requestConifgs : IRequestConfig [ ] ,
157166 intervalTime : IIntervalTime | undefined
0 commit comments