-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
Copy pathinvoke-request.ts
47 lines (42 loc) · 1.15 KB
/
invoke-request.ts
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
import type { IncomingMessage } from 'http'
import type { Readable } from 'stream'
import { filterReqHeaders, ipcForbiddenHeaders } from './utils'
export const invokeRequest = async (
targetUrl: string,
requestInit: {
headers: IncomingMessage['headers']
method: IncomingMessage['method']
signal?: AbortSignal
},
readableBody?: Readable | ReadableStream
) => {
// force to 127.0.0.1 as IPC always runs on this hostname
// to avoid localhost issues
const parsedTargetUrl = new URL(targetUrl)
parsedTargetUrl.hostname = '127.0.0.1'
const invokeHeaders = filterReqHeaders(
{
'cache-control': '',
...requestInit.headers,
},
ipcForbiddenHeaders
) as IncomingMessage['headers']
return await fetch(parsedTargetUrl.toString(), {
headers: invokeHeaders as any as Headers,
method: requestInit.method,
redirect: 'manual',
signal: requestInit.signal,
...(requestInit.method !== 'GET' &&
requestInit.method !== 'HEAD' &&
readableBody
? {
body: readableBody as BodyInit,
duplex: 'half',
}
: {}),
next: {
// @ts-ignore
internal: true,
},
})
}