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'
3
3
import Url , { URL } from 'node:url'
4
+ import HttpsProxyAgent from 'https-proxy-agent'
4
5
5
6
import {
6
7
isNumber ,
@@ -62,8 +63,15 @@ function handleRequestConfig(
62
63
const { protocol, hostname, port, pathname, search } = new Url . URL (
63
64
rawConfig . url
64
65
)
66
+ const isHttp = protocol === 'http:'
65
67
66
68
const config : RequestOptions & IMapTypeEmptyObject < URL > = {
69
+ agent : rawConfig . proxy
70
+ ? HttpsProxyAgent ( rawConfig . proxy )
71
+ : isHttp
72
+ ? new http . Agent ( )
73
+ : new https . Agent ( ) ,
74
+
67
75
protocol,
68
76
hostname,
69
77
port,
@@ -77,46 +85,17 @@ function handleRequestConfig(
77
85
78
86
config . headers = parseHeaders ( rawConfig , config )
79
87
80
- if ( protocol === 'http:' ) {
81
- config . agent = new Agent ( )
82
- } else {
83
- config . agent = new httpsAgent ( )
84
- }
85
-
86
88
return config
87
89
}
88
90
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
-
112
91
export function request ( config : IRequestConfig ) {
113
92
return new Promise < IRequest > ( ( resolve , reject ) => {
114
93
const isDataUndefine = isUndefined ( config . data )
115
94
config . data = ! isDataUndefine ? JSON . stringify ( config . data ) : config . data
116
95
117
96
const requestConfig = handleRequestConfig ( config )
118
97
119
- const req = http . request ( requestConfig , ( res ) => {
98
+ function handleRes ( res : IncomingMessage ) {
120
99
const { statusCode, headers } = res
121
100
122
101
const container : Buffer [ ] = [ ]
@@ -133,7 +112,14 @@ export function request(config: IRequestConfig) {
133
112
134
113
resolve ( resolveRes )
135
114
} )
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
+ }
137
123
138
124
req . on ( 'timeout' , ( ) => {
139
125
reject ( new Error ( `Timeout ${ config . timeout } ms` ) )
@@ -152,6 +138,29 @@ export function request(config: IRequestConfig) {
152
138
} )
153
139
}
154
140
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
+
155
164
export async function batchRequest (
156
165
requestConifgs : IRequestConfig [ ] ,
157
166
intervalTime : IIntervalTime | undefined
0 commit comments