Unverified Commit 721a2709 authored by Yagiz Nizipli's avatar Yagiz Nizipli Committed by GitHub

refactor: modernize get-online with dns/promises (#55674)

Small refactor to use `dns/promises`, `URL`, and `encoding` parameter of `execSync`.
parent d7818d21
import { execSync } from 'child_process' import { execSync } from 'child_process'
import dns from 'dns' import dns from 'dns/promises'
import url from 'url'
function getProxy(): string | undefined { function getProxy(): string | undefined {
if (process.env.https_proxy) { if (process.env.https_proxy) {
...@@ -8,33 +7,31 @@ function getProxy(): string | undefined { ...@@ -8,33 +7,31 @@ function getProxy(): string | undefined {
} }
try { try {
const httpsProxy = execSync('npm config get https-proxy').toString().trim() const httpsProxy = execSync('npm config get https-proxy', {
encoding: 'utf8',
}).trim()
return httpsProxy !== 'null' ? httpsProxy : undefined return httpsProxy !== 'null' ? httpsProxy : undefined
} catch (e) { } catch (e) {
return return
} }
} }
export function getOnline(): Promise<boolean> { export async function getOnline(): Promise<boolean> {
return new Promise((resolve) => { try {
dns.lookup('registry.yarnpkg.com', (registryErr) => { await dns.lookup('registry.yarnpkg.com')
if (!registryErr) { return true
return resolve(true) } catch {
} const proxy = getProxy()
if (!proxy) {
const proxy = getProxy() return false
if (!proxy) { }
return resolve(false)
}
const { hostname } = url.parse(proxy)
if (!hostname) {
return resolve(false)
}
dns.lookup(hostname, (proxyErr) => { try {
resolve(proxyErr == null) const { hostname } = new URL(proxy)
}) await dns.lookup(hostname)
}) return true
}) } catch {
return false
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment