fix find available port

This commit is contained in:
pompurin404 2024-11-14 19:47:59 +08:00
parent dac2336eb6
commit 9f6aac708b
No known key found for this signature in database

View File

@ -24,7 +24,6 @@ function FindProxyForURL(url, host) {
export function findAvailablePort(startPort: number): Promise<number> { export function findAvailablePort(startPort: number): Promise<number> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const server = net.createServer() const server = net.createServer()
server.unref()
server.on('error', (err) => { server.on('error', (err) => {
if (startPort <= 65535) { if (startPort <= 65535) {
resolve(findAvailablePort(startPort + 1)) resolve(findAvailablePort(startPort + 1))
@ -32,13 +31,12 @@ export function findAvailablePort(startPort: number): Promise<number> {
reject(err) reject(err)
} }
}) })
server.on('listening', () => {
server.listen(startPort, () => {
// 端口可用
server.close(() => { server.close(() => {
resolve(startPort) resolve(startPort)
}) })
}) })
server.listen(startPort, '127.0.0.1')
}) })
} }