[go: up one dir, main page]

Skip to content

Async listen, connect, and close servers or sockets on ideal ports

License

Notifications You must be signed in to change notification settings

LuKks/like-bind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

like-bind

Async listen, connect, and close servers or sockets on ideal ports

npm i like-bind

Usage

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)

API

const port = await bind.free.tcp(port[, options])

const port = await bind.free.udp(port[, options])

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
}

await bind.listen(serverOrSocket[, port, address, cb])

Async listen a server or socket.

await bind.connect(socket[, port, address, cb])

Async connect to a server.

await bind.close(serverOrSocket[, options])

Async close a server or socket.

Available options

{
  force: false // Only for servers, and you must have used `bind.listen(..)`
}

License

MIT

About

Async listen, connect, and close servers or sockets on ideal ports

Resources

License

Stars

Watchers

Forks

Packages

No packages published