mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { execSync } from 'node:child_process'
|
|
import { lookup } from 'node:dns/promises'
|
|
import { URL } from 'node: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 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
|
|
}
|
|
|
|
let url: URL
|
|
try {
|
|
url = new URL(proxy)
|
|
} catch {
|
|
// Invalid proxy URL
|
|
return false
|
|
}
|
|
const { hostname } = url
|
|
if (!hostname) {
|
|
// Invalid proxy URL
|
|
return false
|
|
}
|
|
|
|
try {
|
|
await 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
|
|
}
|
|
}
|
|
}
|