Async listen, connect, and close servers or sockets on ideal ports
npm i like-bind
const bind = require('like-bind')
const server = net.createServer()
const port = await bind.free.tcp(1337)
await bind.listen(server, port)
const socket = new net.Socket()
await bind.connect(socket, server.address().port)
await bind.close(socket)
await bind.close(server, { force: true })
const socket = dgram.createSocket('udp4')
const port = await bind.free.udp(1337)
await bind.listen(socket, port)
// ...
await bind.close(socket)
Creates a temporary server or socket to check if the port is available.
By default, if port
is already used then it returns a random one.
Available options
{
any: true // Disable to throw when the port is already used
}
Async listen a server or socket.
Async connect to a server.
Async close a server or socket.
Available options
{
force: false // Only for servers, and you must have used `bind.listen(..)`
}
MIT