1- import { RequestOptions } from 'http'
1+ import { Agent } from 'http'
2+ import { Agent as httpsAgent } from 'https'
23import Url , { URL } from 'node:url'
3- import { Agent } from 'https'
44
5+ import { RequestOptions } from 'http'
56import {
67 IAnyObject ,
78 IFetchBaseConifg ,
8- IFetchConfig ,
99 IMapTypeEmptyObject ,
1010 IRequestConfig ,
1111 IXCrawlBaseConifg
@@ -30,10 +30,11 @@ export function parseHeaders(
3030 rawConfig : IRequestConfig ,
3131 config : RequestOptions & IMapTypeEmptyObject < URL >
3232) {
33+ const rawHeaders = rawConfig . headers ?? { }
3334 const headers : IAnyObject = {
3435 'User-Agent' :
3536 '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
3738 }
3839
3940 if ( config . method === 'POST' && rawConfig . data ) {
@@ -47,11 +48,12 @@ export function parseHeaders(
4748export function handleRequestConfig (
4849 rawConfig : IRequestConfig
4950) : 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+ )
5154
5255 const config : RequestOptions & IMapTypeEmptyObject < URL > = {
53- agent : new Agent ( { } ) ,
54-
56+ protocol,
5557 hostname,
5658 port,
5759 path : pathname ,
@@ -64,10 +66,16 @@ export function handleRequestConfig(
6466
6567 config . headers = parseHeaders ( rawConfig , config )
6668
69+ if ( protocol === 'http:' ) {
70+ config . agent = new Agent ( )
71+ } else {
72+ config . agent = new httpsAgent ( )
73+ }
74+
6775 return config
6876}
6977
70- export function mergeConfig < T extends IFetchConfig > (
78+ export function mergeConfig < T extends IFetchBaseConifg > (
7179 baseConfig : IXCrawlBaseConifg ,
7280 config : T
7381) : IFetchBaseConifg & T {
0 commit comments