Repository navigation
Expand file tree
/
Copy pathrequest.ts
More file actions
112 lines (98 loc) · 3.46 KB
/
Copy pathrequest.ts
File metadata and controls
112 lines (98 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { RequestOptions, IncomingMessage, ClientRequest } from 'http'
import { RequestDetail } from '../common'
import { MainProcess } from './fork'
export interface RequestFn {
(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest
(
url: string | URL,
options: RequestOptions,
callback?: (res: IncomingMessage) => void
): ClientRequest
}
function proxyClientRequestFactory(
actualRequest: ClientRequest,
requestDetail: RequestDetail,
mainProcess: MainProcess
) {
const actualFn = actualRequest.write
actualRequest.write = (data: any) => {
try {
requestDetail.requestData = JSON.parse(data.toString())
} catch (err) {
requestDetail.requestData = data
}
return actualFn.bind(actualRequest)(data)
}
actualRequest.on('error', () => {
requestDetail.responseStatusCode = 0
requestDetail.requestEndTime = new Date().getTime()
mainProcess.endRequest(requestDetail)
})
return actualRequest
}
function proxyCallbackFactory(
actualCallBack: any,
requestDetail: RequestDetail,
mainProcess: MainProcess
) {
return (response: IncomingMessage) => {
requestDetail.responseHeaders = response.headers
if (typeof actualCallBack === 'function') {
actualCallBack(response)
}
mainProcess.responseRequest(requestDetail.id, response)
}
}
export function requestProxyFactory(
this: any,
actualRequestHandler: any,
isHttps: boolean,
mainProcess: MainProcess
) {
const fn: RequestFn = (arg1: any, arg2?: any, arg3?: any) => {
// #region resolve arguments
let url: string | URL | undefined
let options: RequestOptions | string | URL | undefined
let callback: ((res: IncomingMessage) => void) | undefined
if (typeof arg1 === 'string' || arg1 instanceof URL) {
// Signature: (url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
url = arg1
options = arg2
callback = arg3
} else {
// Signature: (options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
options = arg1
callback = arg2
}
const requestDetail = new RequestDetail()
if (typeof url === 'string') {
requestDetail.url = url
requestDetail.method = 'GET'
} else if (url instanceof URL) {
requestDetail.url = url.toString()
requestDetail.method = 'GET'
} else if (options && typeof options !== 'string' && !(options instanceof URL)) {
const connectionType = isHttps ? 'https' : 'http'
requestDetail.url = `${connectionType}://${options.hostname || options.host}${options.path}`
requestDetail.method = options.method
requestDetail.requestHeaders = options.headers
}
// #endregion
mainProcess.registerRequest(requestDetail)
const proxyCallback = proxyCallbackFactory(callback, requestDetail, mainProcess)
if (typeof arg1 === 'string' || arg1 instanceof URL) {
// Call actualRequestHandler with 3 parameters
const request: ClientRequest = actualRequestHandler(
url!,
options as RequestOptions,
proxyCallback
)
return proxyClientRequestFactory(request, requestDetail, mainProcess)
} else {
// Call actualRequestHandler with 2 parameters
const request: ClientRequest = actualRequestHandler(options as RequestOptions, proxyCallback)
return proxyClientRequestFactory(request, requestDetail, mainProcess)
}
}
return fn
}