Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dev IPC request #47877

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import { IncrementalCache } from '../lib/incremental-cache'
import LRUCache from 'next/dist/compiled/lru-cache'
import { NextUrlWithParsedQuery } from '../request-meta'
import { deserializeErr, errorToJSON } from '../render'
import { invokeRequest } from '../lib/server-ipc'

// Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: FunctionComponent
Expand Down Expand Up @@ -1257,12 +1258,23 @@ export default class DevServer extends Server {
private async invokeIpcMethod(method: string, args: any[]): Promise<any> {
const ipcPort = process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT
if (ipcPort) {
const res = await this.originalFetch(
const res = await invokeRequest(
`http://${this.hostname}:${ipcPort}?method=${
method as string
}&args=${encodeURIComponent(JSON.stringify(args))}`
}&args=${encodeURIComponent(JSON.stringify(args))}`,
{
method: 'GET',
headers: {},
}
)
const body = await res.text()
const chunks = []

for await (const chunk of res) {
if (chunk) {
chunks.push(chunk)
}
}
const body = Buffer.concat(chunks).toString()

if (body.startsWith('{') && body.endsWith('}')) {
const parsedBody = JSON.parse(body)
Expand Down
7 changes: 7 additions & 0 deletions packages/next/src/server/lib/server-ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ export const invokeRequest = async (
},
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']
Expand Down