b8b104506d
The PR further reduces the `create-next-app` installation size by another 80 KiB: - Replace the callback version of Node.js built-in `dns` API usage with `dns/promise` + async/await - Replace `got` w/ `fetch` since Next.js and `create-next-app` now target Node.js 18.17.0+ - Download and extract the tar.gz file in the memory (without creating temporary files). This improves the performance. - Some other minor refinements. Following these changes, the size of `dist/index.js` is now 536 KiB.
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { execSync } from 'child_process'
|
|
import dns from 'dns/promises'
|
|
import url from 'url'
|
|
|
|
function getProxy(): string | undefined {
|
|
if (process.env.https_proxy) {
|
|
return process.env.https_proxy
|
|
}
|
|
|
|
try {
|
|
const httpsProxy = execSync('npm config get https-proxy').toString().trim()
|
|
return httpsProxy !== 'null' ? httpsProxy : undefined
|
|
} catch (e) {
|
|
return
|
|
}
|
|
}
|
|
|
|
export async function getOnline(): Promise<boolean> {
|
|
try {
|
|
await dns.lookup('registry.yarnpkg.com')
|
|
// If DNS lookup succeeds, we are online
|
|
return true
|
|
} catch {
|
|
// The DNS lookup failed, but we are still fine as long as a proxy has been set
|
|
const proxy = getProxy()
|
|
if (!proxy) {
|
|
return false
|
|
}
|
|
|
|
const { hostname } = url.parse(proxy)
|
|
if (!hostname) {
|
|
// Invalid proxy URL
|
|
return false
|
|
}
|
|
|
|
try {
|
|
await dns.lookup(hostname)
|
|
// If DNS lookup succeeds for the proxy server, we are online
|
|
return true
|
|
} catch {
|
|
// The DNS lookup for the proxy server also failed, so we are offline
|
|
return false
|
|
}
|
|
}
|
|
}
|