-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
153 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
const Promise = require('bluebird') | ||
const net = require('net') | ||
|
||
const NetUtils = { | ||
isPortAvailable (port, listenAddress) { | ||
return new Promise((resolve, reject) => { | ||
const server = net.createServer() | ||
|
||
server.unref() | ||
server.on('error', (err) => { | ||
server.close() | ||
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') { | ||
resolve(false) | ||
} else { | ||
reject(err) | ||
} | ||
}) | ||
|
||
server.listen(port, listenAddress, () => { | ||
server.close(() => resolve(true)) | ||
}) | ||
}) | ||
}, | ||
|
||
getAvailablePort (port, listenAddress) { | ||
return NetUtils.isPortAvailable(port, listenAddress) | ||
.then((available) => available ? port : NetUtils.getAvailablePort(port + 1, listenAddress)) | ||
} | ||
} | ||
|
||
module.exports = NetUtils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
const NetUtils = require('../../../lib/utils/net-utils') | ||
const connect = require('connect') | ||
const net = require('net') | ||
|
||
describe('NetUtils.isPortAvailable', () => { | ||
it('it is possible to run server on available port', (done) => { | ||
NetUtils.isPortAvailable(9876, '127.0.0.1').then((available) => { | ||
expect(available).to.be.true | ||
const server = net | ||
.createServer(connect()) | ||
.listen(9876, '127.0.0.1', () => { | ||
server.close(done) | ||
}) | ||
}) | ||
}) | ||
|
||
it('resolves with false when port is used', (done) => { | ||
const server = net | ||
.createServer(connect()) | ||
.listen(9876, '127.0.0.1', () => { | ||
NetUtils.isPortAvailable(9876, '127.0.0.1').then((available) => { | ||
expect(available).to.be.false | ||
server.close(done) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('NetUtils.getAvailablePort', () => { | ||
it('resolves with port when is available', (done) => { | ||
NetUtils.getAvailablePort(9876, '127.0.0.1').then((port) => { | ||
expect(port).to.equal(9876) | ||
done() | ||
}) | ||
}) | ||
|
||
it('resolves with next available port', (done) => { | ||
const stub = sinon.stub(NetUtils, 'isPortAvailable') | ||
stub.withArgs(9876).resolves(false) | ||
stub.withArgs(9877).resolves(false) | ||
stub.withArgs(9878).resolves(true) | ||
|
||
NetUtils.getAvailablePort(9876, '127.0.0.1').then((port) => { | ||
expect(port).to.equal(9878) | ||
done() | ||
}) | ||
}) | ||
}) |