|
1 |
| -import { Agent } from 'http' |
2 |
| -import { Agent as httpsAgent } from 'https' |
3 |
| -import Url, { URL } from 'node:url' |
4 |
| - |
5 |
| -import { RequestOptions } from 'http' |
6 |
| -import { |
7 |
| - IAnyObject, |
8 |
| - IFetchBaseConifg, |
9 |
| - IMapTypeEmptyObject, |
10 |
| - IRequestConfig, |
11 |
| - IXCrawlBaseConifg |
12 |
| -} from './types' |
13 |
| - |
14 |
| -export function parseParams(urlSearch: string, params?: IAnyObject): string { |
15 |
| - let res = urlSearch ? `${urlSearch}` : '?' |
16 |
| - |
17 |
| - if (params) { |
18 |
| - for (const key in params) { |
19 |
| - const value = params[key] |
20 |
| - res += `&${key}=${value}` |
21 |
| - } |
22 |
| - } else { |
23 |
| - res = urlSearch |
24 |
| - } |
25 |
| - |
26 |
| - return res |
27 |
| -} |
28 |
| - |
29 |
| -export function parseHeaders( |
30 |
| - rawConfig: IRequestConfig, |
31 |
| - config: RequestOptions & IMapTypeEmptyObject<URL> |
32 |
| -) { |
33 |
| - const rawHeaders = rawConfig.headers ?? {} |
34 |
| - const headers: IAnyObject = { |
35 |
| - 'User-Agent': |
36 |
| - 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36', |
37 |
| - ...rawHeaders |
38 |
| - } |
39 |
| - |
40 |
| - if (config.method === 'POST' && rawConfig.data) { |
41 |
| - headers['Content-Type'] = 'application/json' |
42 |
| - headers['Content-Length'] = Buffer.byteLength(rawConfig.data) |
43 |
| - } |
44 |
| - |
45 |
| - return headers |
46 |
| -} |
47 |
| - |
48 |
| -export function handleRequestConfig( |
49 |
| - rawConfig: IRequestConfig |
50 |
| -): RequestOptions & IMapTypeEmptyObject<URL> { |
51 |
| - const { protocol, hostname, port, pathname, search } = new Url.URL( |
52 |
| - rawConfig.url |
53 |
| - ) |
54 |
| - |
55 |
| - const config: RequestOptions & IMapTypeEmptyObject<URL> = { |
56 |
| - protocol, |
57 |
| - hostname, |
58 |
| - port, |
59 |
| - path: pathname, |
60 |
| - search: parseParams(search, rawConfig.params), |
61 |
| - |
62 |
| - method: rawConfig.method?.toLocaleUpperCase() ?? 'GET', |
63 |
| - headers: {}, |
64 |
| - timeout: rawConfig.timeout |
65 |
| - } |
66 |
| - |
67 |
| - config.headers = parseHeaders(rawConfig, config) |
68 |
| - |
69 |
| - if (protocol === 'http:') { |
70 |
| - config.agent = new Agent() |
71 |
| - } else { |
72 |
| - config.agent = new httpsAgent() |
73 |
| - } |
74 |
| - |
75 |
| - return config |
76 |
| -} |
77 |
| - |
78 |
| -export function mergeConfig<T extends IFetchBaseConifg>( |
79 |
| - baseConfig: IXCrawlBaseConifg, |
80 |
| - config: T |
81 |
| -): IFetchBaseConifg & T { |
82 |
| - const { |
83 |
| - baseUrl, |
84 |
| - timeout: baseTimeout, |
85 |
| - intervalTime: baseIntervalTime |
86 |
| - } = baseConfig |
87 |
| - const { requestConifg, intervalTime } = config |
88 |
| - |
89 |
| - const requestConifgArr = isArray(requestConifg) |
90 |
| - ? requestConifg |
91 |
| - : [requestConifg] |
92 |
| - |
93 |
| - for (const requestItem of requestConifgArr) { |
94 |
| - const { url, timeout } = requestItem |
95 |
| - |
96 |
| - if (!isUndefined(baseUrl)) { |
97 |
| - requestItem.url = baseUrl + url |
98 |
| - } |
99 |
| - |
100 |
| - if (isUndefined(timeout) && !isUndefined(baseTimeout)) { |
101 |
| - requestItem.timeout = baseTimeout |
102 |
| - } |
103 |
| - } |
104 |
| - |
105 |
| - if (isUndefined(intervalTime) && !isUndefined(baseIntervalTime)) { |
106 |
| - config.intervalTime = baseIntervalTime |
107 |
| - } |
108 |
| - |
109 |
| - return config |
110 |
| -} |
111 |
| - |
112 | 1 | export function sleep(timeout: number) {
|
113 | 2 | return new Promise((resolve) => setTimeout(resolve, timeout))
|
114 | 3 | }
|
|
0 commit comments