Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
647 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const net = require('net')
|
---|
| 4 |
|
---|
| 5 | const NetUtils = {
|
---|
| 6 | bindAvailablePort (port, listenAddress) {
|
---|
| 7 | return new Promise((resolve, reject) => {
|
---|
| 8 | const server = net.createServer()
|
---|
| 9 |
|
---|
| 10 | server
|
---|
| 11 | .on('error', (err) => {
|
---|
| 12 | server.close()
|
---|
| 13 | if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
|
---|
| 14 | server.listen(++port, listenAddress)
|
---|
| 15 | } else {
|
---|
| 16 | reject(new Error(`Failed to bind ${port}: ` + (err.stack || err)))
|
---|
| 17 | }
|
---|
| 18 | })
|
---|
| 19 | .on('listening', () => {
|
---|
| 20 | resolve(server)
|
---|
| 21 | })
|
---|
| 22 | .listen(port, listenAddress)
|
---|
| 23 | })
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | module.exports = NetUtils
|
---|
Note:
See
TracBrowser
for help on using the repository browser.