NET-Web-API-w-Angular/my-app/node_modules/karma/lib/utils/net-utils.js

28 lines
647 B
JavaScript
Raw Normal View History

2024-02-09 00:38:41 +00:00
'use strict'
const net = require('net')
const NetUtils = {
bindAvailablePort (port, listenAddress) {
return new Promise((resolve, reject) => {
const server = net.createServer()
server
.on('error', (err) => {
server.close()
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
server.listen(++port, listenAddress)
} else {
reject(new Error(`Failed to bind ${port}: ` + (err.stack || err)))
}
})
.on('listening', () => {
resolve(server)
})
.listen(port, listenAddress)
})
}
}
module.exports = NetUtils