Skip to content

Commit 21c3259

Browse files
committed
rework failurs in server and remove bluebird usage from network
1 parent 158b493 commit 21c3259

File tree

7 files changed

+53
-33
lines changed

7 files changed

+53
-33
lines changed

packages/network/lib/agent.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const regenerateRequestHead = (req: http.ClientRequest) => {
150150
}
151151
}
152152

153-
export const getFirstWorkingFamily = (
153+
export const getFirstWorkingFamily = async (
154154
{ port, host }: Pick<http.RequestOptions, 'port' | 'host'>,
155155
familyCache: FamilyCache,
156156
cb: (family?: net.family) => void,
@@ -178,15 +178,15 @@ export const getFirstWorkingFamily = (
178178
return cb(familyCache[cacheKey])
179179
}
180180

181-
return getAddress(port, host)
182-
.then((firstWorkingAddress: net.Address) => {
181+
try {
182+
const firstWorkingAddress = await getAddress(port, host)
183+
183184
familyCache[cacheKey] = firstWorkingAddress.family
184185

185186
return cb(firstWorkingAddress.family)
186-
})
187-
.catch(() => {
187+
} catch (error) {
188188
return cb()
189-
})
189+
}
190190
}
191191

192192
export class CombinedAgent {

packages/network/lib/connect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Bluebird from 'bluebird'
1+
import { promisify } from 'node:util'
22
import debugModule from 'debug'
3-
import dns, { LookupAddress, LookupAllOptions } from 'dns'
3+
import dns from 'dns'
44
import _ from 'lodash'
55
import net from 'net'
66
import tls from 'tls'
@@ -27,7 +27,7 @@ export async function getAddress (port: number, hostname: string): Promise<net.A
2727

2828
// promisify at the very last second which enables us to
2929
// modify dns lookup function (via hosts overrides)
30-
const lookupAsync = Bluebird.promisify<LookupAddress[], string, LookupAllOptions>(dns.lookup, { context: dns })
30+
const lookupAsync = promisify(dns.lookup)
3131

3232
// this does not go out to the network to figure
3333
// out the addresses. in fact it respects the /etc/hosts file

packages/network/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
},
2323
"dependencies": {
2424
"@cypress/parse-domain": "2.4.0",
25-
"bluebird": "3.5.3",
2625
"concat-stream": "1.6.2",
2726
"debug": "^4.3.4",
2827
"fs-extra": "9.1.0",

packages/network/test/support/servers.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Bluebird from 'bluebird'
1+
import { promisify } from 'util'
22
import express from 'express'
33
import http from 'http'
44
import https from 'https'
@@ -58,17 +58,25 @@ export class Servers {
5858
getCAInformation(),
5959
])
6060

61-
this.httpServer = Bluebird.promisifyAll(
62-
allowDestroy(http.createServer(app)),
63-
) as http.Server & AsyncServer
61+
const httpServer = allowDestroy(http.createServer(app))
62+
63+
this.httpServer = Object.assign(httpServer, {
64+
closeAsync: promisify(httpServer.close.bind(httpServer)),
65+
destroyAsync: promisify(httpServer.destroy.bind(httpServer)),
66+
listenAsync: promisify(httpServer.listen.bind(httpServer)),
67+
}) as http.Server & AsyncServer
6468

6569
this.wsServer = new SocketIOServer(this.httpServer)
6670

6771
this.caCertificatePath = caCertificatePath
6872
this.https = { cert: serverCertificateKeys[0], key: serverCertificateKeys[1] }
69-
this.httpsServer = Bluebird.promisifyAll(
70-
allowDestroy(https.createServer(this.https, <http.RequestListener>app)),
71-
) as https.Server & AsyncServer
73+
const httpsServer = allowDestroy(https.createServer(this.https, <http.RequestListener>app))
74+
75+
this.httpsServer = Object.assign(httpsServer, {
76+
closeAsync: promisify(httpsServer.close.bind(httpsServer)),
77+
destroyAsync: promisify(httpsServer.destroy.bind(httpsServer)),
78+
listenAsync: promisify(httpsServer.listen.bind(httpsServer)),
79+
}) as https.Server & AsyncServer
7280

7381
this.wssServer = new SocketIOServer(this.httpsServer)
7482

packages/network/test/unit/agent.spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, beforeEach, afterEach, beforeAll, afterAll, vi } from 'vitest'
2-
import Bluebird from 'bluebird'
2+
import { promisify } from 'util'
33

44
import http from 'http'
55
import https from 'https'
@@ -451,13 +451,15 @@ describe('lib/agent', function () {
451451
})
452452

453453
it('#createUpstreamProxyConnection throws when connection is accepted then closed', async () => {
454-
const proxy = Bluebird.promisifyAll(
455-
allowDestroy(
456-
net.createServer((socket) => {
457-
socket.end()
458-
}),
459-
),
460-
) as net.Server & AsyncServer
454+
const proxyServer = allowDestroy(
455+
net.createServer((socket) => {
456+
socket.end()
457+
}),
458+
)
459+
const proxy = Object.assign(proxyServer, {
460+
destroyAsync: promisify(proxyServer.destroy.bind(proxyServer)),
461+
listenAsync: promisify(proxyServer.listen.bind(proxyServer)),
462+
}) as net.Server & AsyncServer
461463

462464
const proxyPort = PROXY_PORT + 2
463465

packages/server/lib/util/ensure-url.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,12 @@ export const isListening = (urlStr: string) => {
7070
.catch({ name: 'StatusCodeError' }, () => {}) // we just care if it can connect, not if it's a valid resource
7171
}
7272

73-
return connect.getAddress(Number(port), String(hostname))
73+
// With https://github.com/cypress-io/cypress/pull/32633, the @packages/network package has refactored some methods to use
74+
// native promises. This method wraps the native promise in a Bluebird promise to ensure that the method returns a Bluebird promise
75+
// until we are able to refactor it.
76+
return new Bluebird((resolve, reject) => {
77+
connect.getAddress(Number(port), String(hostname))
78+
.then(resolve)
79+
.catch(reject)
80+
})
7481
}

packages/server/test/unit/cloud/api/api_spec.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const encryption = require('../../../../lib/cloud/encryption')
1212
const {
1313
agent,
1414
} = require('@packages/network')
15+
const { CombinedAgent } = require('@packages/network/lib/agent')
1516
const pkg = require('@packages/root')
1617
const api = require('../../../../lib/cloud/api').default
1718
const cache = require('../../../../lib/cache').cache
@@ -140,7 +141,10 @@ describe('lib/cloud/api', () => {
140141

141142
context('.rp', () => {
142143
beforeEach(() => {
143-
sinon.spy(agent, 'addRequest')
144+
// Because @packages/network is a bundled ES6 class with https://github.com/cypress-io/cypress/pull/32633
145+
// we can no longer mock the addRequest method on the agent singleton instance directly
146+
// Until we are able to convert this test to Vitest/TypeScript, we need to spy on the prototype
147+
sinon.spy(CombinedAgent.prototype, 'addRequest')
144148

145149
return nock.enableNetConnect()
146150
}) // nock will prevent requests from reaching the agent
@@ -151,9 +155,9 @@ describe('lib/cloud/api', () => {
151155
return api.ping()
152156
.thenThrow()
153157
.catch(() => {
154-
expect(agent.addRequest).to.be.calledOnce
158+
expect(CombinedAgent.prototype.addRequest).to.be.calledOnce
155159

156-
expect(agent.addRequest).to.be.calledWithMatch(sinon.match.any, {
160+
expect(CombinedAgent.prototype.addRequest).to.be.calledWithMatch(sinon.match.any, {
157161
href: 'http://localhost:1234/ping',
158162
})
159163
})
@@ -165,9 +169,9 @@ describe('lib/cloud/api', () => {
165169
return api.ping()
166170
.thenThrow()
167171
.catch(() => {
168-
expect(agent.addRequest).to.be.calledOnce
172+
expect(CombinedAgent.prototype.addRequest).to.be.calledOnce
169173

170-
expect(agent.addRequest).to.be.calledWithMatch(sinon.match.any, {
174+
expect(CombinedAgent.prototype.addRequest).to.be.calledWithMatch(sinon.match.any, {
171175
rejectUnauthorized: true,
172176
})
173177
})
@@ -185,9 +189,9 @@ describe('lib/cloud/api', () => {
185189
return api.ping()
186190
.thenThrow()
187191
.catch(() => {
188-
expect(agent.addRequest).to.be.calledOnce
192+
expect(CombinedAgent.prototype.addRequest).to.be.calledOnce
189193

190-
expect(agent.addRequest).to.be.calledWithMatch(sinon.match.any, {
194+
expect(CombinedAgent.prototype.addRequest).to.be.calledWithMatch(sinon.match.any, {
191195
href: 'http://localhost:1234/ping',
192196
})
193197
})

0 commit comments

Comments
 (0)