Skip to content

Commit cce1c87

Browse files
committed
http和https协议
1 parent efee8ce commit cce1c87

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/request.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import https from 'node:https'
1+
import http from 'node:http'
22

33
import {
44
handleRequestConfig,
@@ -12,13 +12,13 @@ import { IIntervalTime, IRequest, IRequestConfig } from './types'
1212

1313
export function request(config: IRequestConfig) {
1414
return new Promise<IRequest>((resolve, reject) => {
15-
const data = (config.data = config.data
16-
? JSON.stringify(config.data ?? '')
17-
: config.data)
15+
const isDataUndefine = isUndefined(config.data)
16+
config.data = !isDataUndefine ? JSON.stringify(config.data) : config.data
17+
1818
const requestConfig = handleRequestConfig(config)
1919

20-
const req = https.request(requestConfig, (res) => {
21-
const { headers } = res
20+
const req = http.request(requestConfig, (res) => {
21+
const { statusCode, headers } = res
2222

2323
const container: Buffer[] = []
2424

@@ -27,6 +27,7 @@ export function request(config: IRequestConfig) {
2727
res.on('end', () => {
2828
const data = Buffer.concat(container)
2929
const resolveRes: IRequest = {
30+
statusCode,
3031
headers,
3132
data
3233
}
@@ -36,18 +37,16 @@ export function request(config: IRequestConfig) {
3637
})
3738

3839
req.on('timeout', () => {
39-
console.log(`Timeout Error`)
40-
reject(new Error('Timeout'))
40+
reject(new Error(`Timeout ${config.timeout}ms`))
4141
})
4242

4343
req.on('error', (err) => {
44-
console.log('Error: ', err.message)
4544
reject(err)
4645
})
4746

4847
// 其他处理
49-
if (requestConfig.method === 'POST') {
50-
req.write(data)
48+
if (requestConfig.method === 'POST' && !isDataUndefine) {
49+
req.write(config.data)
5150
}
5251

5352
req.end()

src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type IMapTypeEmptyObject<T extends object, E extends string = ''> = {
1313
}
1414

1515
export interface IRequest {
16+
statusCode: number | undefined
1617
headers: IncomingHttpHeaders
1718
data: Buffer
1819
}
@@ -69,6 +70,12 @@ export interface IFetchFileConfig extends IFetchBaseConifg {
6970
}
7071
}
7172

73+
export type IFetch<T> = {
74+
statusCode: number | undefined
75+
headers: IncomingHttpHeaders
76+
data: T
77+
}[]
78+
7279
export type IFetchFile = {
7380
fileName: string
7481
mimeType: string

src/utils.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { RequestOptions } from 'http'
1+
import { Agent } from 'http'
2+
import { Agent as httpsAgent } from 'https'
23
import Url, { URL } from 'node:url'
3-
import { Agent } from 'https'
44

5+
import { RequestOptions } from 'http'
56
import {
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(
4748
export 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

Comments
 (0)