-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'canary' into feedthejim/next-1078-change-prefetch-heuri…
…stics-to-traverse
- Loading branch information
Showing
6 changed files
with
95 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { IncomingMessage } from 'http' | ||
import { filterReqHeaders } from './utils' | ||
|
||
export const invokeRequest = async ( | ||
targetUrl: string, | ||
requestInit: { | ||
headers: IncomingMessage['headers'] | ||
method: IncomingMessage['method'] | ||
}, | ||
readableBody?: import('stream').Readable | ||
) => { | ||
const parsedUrl = new URL(targetUrl) | ||
|
||
// force localhost to IPv4 as some DNS may | ||
// resolve to IPv6 instead | ||
if (parsedUrl.hostname === 'localhost') { | ||
parsedUrl.hostname = '127.0.0.1' | ||
} | ||
const invokeHeaders = filterReqHeaders({ | ||
...requestInit.headers, | ||
}) as IncomingMessage['headers'] | ||
|
||
const invokeRes = await new Promise<IncomingMessage>( | ||
(resolveInvoke, rejectInvoke) => { | ||
const http = require('http') as typeof import('http') | ||
|
||
try { | ||
const invokeReq = http.request( | ||
targetUrl, | ||
{ | ||
headers: invokeHeaders, | ||
method: requestInit.method, | ||
}, | ||
(res) => { | ||
resolveInvoke(res) | ||
} | ||
) | ||
invokeReq.on('error', (err) => { | ||
rejectInvoke(err) | ||
}) | ||
|
||
if (requestInit.method !== 'GET' && requestInit.method !== 'HEAD') { | ||
if (readableBody) { | ||
readableBody.pipe(invokeReq) | ||
readableBody.on('close', () => { | ||
invokeReq.end() | ||
}) | ||
} | ||
} else { | ||
invokeReq.end() | ||
} | ||
} catch (err) { | ||
rejectInvoke(err) | ||
} | ||
} | ||
) | ||
|
||
return invokeRes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const forbiddenHeaders = [ | ||
'accept-encoding', | ||
'content-length', | ||
'keepalive', | ||
'content-encoding', | ||
'transfer-encoding', | ||
// https://github.com/nodejs/undici/issues/1470 | ||
'connection', | ||
] | ||
|
||
export const filterReqHeaders = ( | ||
headers: Record<string, undefined | string | string[]> | ||
) => { | ||
for (const [key, value] of Object.entries(headers)) { | ||
if ( | ||
forbiddenHeaders.includes(key) || | ||
!(Array.isArray(value) || typeof value === 'string') | ||
) { | ||
delete headers[key] | ||
} | ||
} | ||
return headers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters