1
- import { RequestOptions } from 'http'
1
+ import { Agent } from 'http'
2
+ import { Agent as httpsAgent } from 'https'
2
3
import Url , { URL } from 'node:url'
3
- import { Agent } from 'https'
4
4
5
+ import { RequestOptions } from 'http'
5
6
import {
6
7
IAnyObject ,
7
8
IFetchBaseConifg ,
8
- IFetchConfig ,
9
9
IMapTypeEmptyObject ,
10
10
IRequestConfig ,
11
11
IXCrawlBaseConifg
@@ -30,10 +30,11 @@ export function parseHeaders(
30
30
rawConfig : IRequestConfig ,
31
31
config : RequestOptions & IMapTypeEmptyObject < URL >
32
32
) {
33
+ const rawHeaders = rawConfig . headers ?? { }
33
34
const headers : IAnyObject = {
34
35
'User-Agent' :
35
36
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36' ,
36
- ...config
37
+ ...rawHeaders
37
38
}
38
39
39
40
if ( config . method === 'POST' && rawConfig . data ) {
@@ -47,11 +48,12 @@ export function parseHeaders(
47
48
export function handleRequestConfig (
48
49
rawConfig : IRequestConfig
49
50
) : RequestOptions & IMapTypeEmptyObject < URL > {
50
- const { hostname, port, pathname, search } = new Url . URL ( rawConfig . url )
51
+ const { protocol, hostname, port, pathname, search } = new Url . URL (
52
+ rawConfig . url
53
+ )
51
54
52
55
const config : RequestOptions & IMapTypeEmptyObject < URL > = {
53
- agent : new Agent ( { } ) ,
54
-
56
+ protocol,
55
57
hostname,
56
58
port,
57
59
path : pathname ,
@@ -64,10 +66,16 @@ export function handleRequestConfig(
64
66
65
67
config . headers = parseHeaders ( rawConfig , config )
66
68
69
+ if ( protocol === 'http:' ) {
70
+ config . agent = new Agent ( )
71
+ } else {
72
+ config . agent = new httpsAgent ( )
73
+ }
74
+
67
75
return config
68
76
}
69
77
70
- export function mergeConfig < T extends IFetchConfig > (
78
+ export function mergeConfig < T extends IFetchBaseConifg > (
71
79
baseConfig : IXCrawlBaseConifg ,
72
80
config : T
73
81
) : IFetchBaseConifg & T {
0 commit comments