-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: direct ws connection fallback (#4474)
- Loading branch information
Showing
6 changed files
with
131 additions
and
16 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
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,71 @@ | ||
import { promises as dns } from 'node:dns'; | ||
import type { DevConfig, ServerConfig } from '../types/config'; | ||
|
||
type Hostname = { | ||
host?: string; | ||
name: string; | ||
}; | ||
|
||
const wildcardHosts = new Set([ | ||
'0.0.0.0', | ||
'::', | ||
'0000:0000:0000:0000:0000:0000:0000:0000', | ||
]); | ||
|
||
export async function resolveHostname( | ||
optionsHost: string | undefined, | ||
): Promise<Hostname> { | ||
let host: string | undefined; | ||
if (optionsHost === undefined) { | ||
// Use a secure default | ||
host = 'localhost'; | ||
} else { | ||
host = optionsHost; | ||
} | ||
|
||
// Set host name to localhost when possible | ||
let name = host === undefined || wildcardHosts.has(host) ? 'localhost' : host; | ||
|
||
if (host === 'localhost') { | ||
const localhostAddr = await getLocalhostAddressIfDiffersFromDNS(); | ||
if (localhostAddr) { | ||
name = localhostAddr; | ||
} | ||
} | ||
|
||
return { host, name }; | ||
} | ||
|
||
/** | ||
* Returns resolved localhost address when `dns.lookup` result differs from DNS | ||
* | ||
* `dns.lookup` result is same when defaultResultOrder is `verbatim`. | ||
* Even if defaultResultOrder is `ipv4first`, `dns.lookup` result maybe same. | ||
* For example, when IPv6 is not supported on that machine/network. | ||
*/ | ||
export async function getLocalhostAddressIfDiffersFromDNS(): Promise< | ||
string | undefined | ||
> { | ||
const [nodeResult, dnsResult] = await Promise.all([ | ||
dns.lookup('localhost'), | ||
dns.lookup('localhost', { verbatim: true }), | ||
]); | ||
const isSame = | ||
nodeResult.family === dnsResult.family && | ||
nodeResult.address === dnsResult.address; | ||
return isSame ? undefined : nodeResult.address; | ||
} | ||
|
||
export async function getResolvedClientConfig( | ||
clientConfig: DevConfig['client'], | ||
serverConfig: ServerConfig, | ||
): Promise<DevConfig['client']> { | ||
const resolvedServerHostname = (await resolveHostname(serverConfig.host)) | ||
.name; | ||
const resolvedServerPort = serverConfig.port!; | ||
return { | ||
...clientConfig, | ||
host: resolvedServerHostname, | ||
port: resolvedServerPort, | ||
}; | ||
} |
8ec8eed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open