Skip to content

Commit ce05a97

Browse files
committed
refactors
1 parent 140ecb8 commit ce05a97

File tree

3 files changed

+65
-55
lines changed

3 files changed

+65
-55
lines changed

packages/network/lib/agent.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,27 @@ const CRLF = '\r\n'
1515
const statusCodeRe = /^HTTP\/1.[01] (\d*)/
1616

1717
let baseCaOptions: CaOptions | undefined
18-
const getCaOptionsPromise = (): Promise<CaOptions> => {
19-
return getCaOptions().then((options: CaOptions) => {
18+
const getCaOptionsAsync = async (): Promise<CaOptions> => {
19+
try {
20+
const options = await getCaOptions()
21+
2022
baseCaOptions = options
2123

2224
return options
23-
}).catch(() => {
25+
} catch (error) {
26+
debug('Error getting CA options', error)
27+
2428
// Errors reading the config are treated as warnings by npm and node and handled by those processes separately
2529
// from what we're doing here.
26-
return {}
27-
})
30+
return {} as CaOptions
31+
}
2832
}
29-
let baseCaOptionsPromise: Promise<CaOptions> = getCaOptionsPromise()
33+
let baseCaOptionsPromise: Promise<CaOptions> = getCaOptionsAsync()
3034

3135
// This is for testing purposes only
3236
export const _resetBaseCaOptionsPromise = () => {
3337
baseCaOptions = undefined
34-
baseCaOptionsPromise = getCaOptionsPromise()
38+
baseCaOptionsPromise = getCaOptionsAsync()
3539
}
3640

3741
const mergeCAOptions = (options: https.RequestOptions, caOptions: CaOptions): https.RequestOptions => {

packages/network/lib/ca.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,54 @@ import tls from 'tls'
33

44
export type CaOptions = { ca?: string[] }
55

6-
const getNpmConfigCAFileValue = (): Promise<string | undefined> => {
6+
const getNpmConfigCAFileValue = async (): Promise<string | undefined> => {
77
if (process.env.npm_config_cafile) {
8-
return fs.readFile(process.env.npm_config_cafile, 'utf8').then((ca) => {
8+
try {
9+
const ca = await fs.readFile(process.env.npm_config_cafile, 'utf8')
10+
911
return ca
10-
}).catch(() => {
12+
} catch (error) {
1113
return undefined
12-
})
14+
}
1315
}
1416

1517
return Promise.resolve(undefined)
1618
}
17-
const getNodeExtraCACertsFileValue = (): Promise<string | undefined> => {
19+
const getNodeExtraCACertsFileValue = async (): Promise<string | undefined> => {
1820
if (process.env.NODE_EXTRA_CA_CERTS) {
19-
return fs.readFile(process.env.NODE_EXTRA_CA_CERTS, 'utf8').then((ca) => {
21+
try {
22+
const ca = await fs.readFile(process.env.NODE_EXTRA_CA_CERTS, 'utf8')
23+
2024
return ca
21-
}).catch(() => {
25+
} catch (error) {
2226
return undefined
23-
})
27+
}
2428
}
2529

2630
return Promise.resolve(undefined)
2731
}
2832

29-
const getCaOptions = (): Promise<CaOptions> => {
33+
const getCaOptions = async (): Promise<CaOptions> => {
3034
// Load the contents of process.env.npm_config_cafile and process.env.NODE_EXTRA_CA_CERTS
3135
// They will be cached so we don't have to actually read them on every call
32-
return Promise.all([getNpmConfigCAFileValue(), getNodeExtraCACertsFileValue()]).then(([npm_config_cafile, NODE_EXTRA_CA_CERTS]) => {
33-
// Merge the contents of ca with the npm config options. These options are meant to be replacements, but we want to keep the tls client certificate
34-
// config that our consumers provide
35-
if (npm_config_cafile) {
36-
return { ca: [npm_config_cafile] }
37-
}
36+
const [npm_config_cafile, NODE_EXTRA_CA_CERTS] = await Promise.all([getNpmConfigCAFileValue(), getNodeExtraCACertsFileValue()])
3837

39-
if (process.env.npm_config_ca) {
40-
return { ca: [process.env.npm_config_ca] }
41-
}
38+
// Merge the contents of ca with the npm config options. These options are meant to be replacements, but we want to keep the tls client certificate
39+
// config that our consumers provide
40+
if (npm_config_cafile) {
41+
return { ca: [npm_config_cafile] }
42+
}
4243

43-
// Merge the contents of ca with the NODE_EXTRA_CA_CERTS options. This option is additive to the tls root certificates
44-
if (NODE_EXTRA_CA_CERTS) {
45-
return { ca: tls.rootCertificates.concat(NODE_EXTRA_CA_CERTS) }
46-
}
44+
if (process.env.npm_config_ca) {
45+
return { ca: [process.env.npm_config_ca] }
46+
}
47+
48+
// Merge the contents of ca with the NODE_EXTRA_CA_CERTS options. This option is additive to the tls root certificates
49+
if (NODE_EXTRA_CA_CERTS) {
50+
return { ca: tls.rootCertificates.concat(NODE_EXTRA_CA_CERTS) }
51+
}
4752

48-
return {}
49-
})
53+
return {}
5054
}
5155

5256
export {

packages/network/lib/connect.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const debug = debugModule('cypress:network:connect')
1010

1111
export function byPortAndAddress (port: number, address: net.Address) {
1212
// https://nodejs.org/api/net.html#net_net_connect_port_host_connectlistener
13-
return new Bluebird<net.Address>((resolve, reject) => {
13+
return new Promise<net.Address>((resolve, reject) => {
1414
const onConnect = () => {
1515
client.destroy()
1616
resolve(address)
@@ -22,11 +22,9 @@ export function byPortAndAddress (port: number, address: net.Address) {
2222
})
2323
}
2424

25-
export function getAddress (port: number, hostname: string): Bluebird<net.Address> {
25+
export async function getAddress (port: number, hostname: string): Promise<net.Address> {
2626
debug('beginning getAddress %o', { hostname, port })
2727

28-
const fn = byPortAndAddress.bind({}, port)
29-
3028
// promisify at the very last second which enables us to
3129
// modify dns lookup function (via hosts overrides)
3230
const lookupAsync = Bluebird.promisify<LookupAddress[], string, LookupAllOptions>(dns.lookup, { context: dns })
@@ -35,27 +33,31 @@ export function getAddress (port: number, hostname: string): Bluebird<net.Addres
3533
// out the addresses. in fact it respects the /etc/hosts file
3634
// https://github.com/nodejs/node/blob/dbdbdd4998e163deecefbb1d34cda84f749844a4/lib/dns.js#L108
3735
// https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback
38-
// @ts-ignore
39-
return lookupAsync(hostname, { all: true })
40-
.then((addresses) => {
41-
debug('got addresses %o', { hostname, port, addresses })
42-
43-
// ipv6 addresses are causing problems with cypress in cypress internal e2e tests
44-
// on windows, so we are filtering them out here
45-
if (process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT && os.platform() === 'win32') {
46-
debug('filtering ipv6 addresses %o', { hostname, port, addresses })
47-
addresses = addresses.filter((address) => {
48-
return address.family === 4
49-
})
50-
}
5136

52-
// convert to an array if string
53-
return Array.prototype.concat.call(addresses).map(fn)
54-
})
55-
.tapCatch((err) => {
56-
debug('error getting address %o', { hostname, port, err })
57-
})
58-
.any()
37+
let addresses = await lookupAsync(hostname, { all: true })
38+
39+
debug('got addresses %o', { hostname, port, addresses })
40+
41+
// ipv6 addresses are causing problems with cypress in cypress internal e2e tests
42+
// on windows, so we are filtering them out here
43+
if (process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT && os.platform() === 'win32') {
44+
debug('filtering ipv6 addresses %o', { hostname, port, addresses })
45+
addresses = addresses.filter((address) => {
46+
return address.family === 4
47+
})
48+
}
49+
50+
try {
51+
const address = await Promise.any(addresses.map((address) => {
52+
return byPortAndAddress(port, address as net.Address)
53+
}))
54+
55+
return address
56+
} catch (error) {
57+
debug('error getting address %o', { hostname, port, error })
58+
59+
throw error
60+
}
5961
}
6062

6163
export function getDelayForRetry (iteration: number) {

0 commit comments

Comments
 (0)