diff --git a/lib/detect-port.js b/lib/detect-port.js index 0cf22c3..d501cf5 100644 --- a/lib/detect-port.js +++ b/lib/detect-port.js @@ -1,8 +1,8 @@ 'use strict'; -const debug = require('debug')('detect-port'); const net = require('net'); const address = require('address'); +const debug = require('debug')('detect-port'); module.exports = (port, callback) => { if (typeof port === 'function') { @@ -37,7 +37,7 @@ function tryListen(port, maxPort, callback) { tryListen(port, maxPort, callback); } - // 1. check 0.0.0.0 + // 1. check null listen(port, null, (err, realPort) => { // ignore random listening if (port === 0) { @@ -48,19 +48,26 @@ function tryListen(port, maxPort, callback) { return handleError(err); } - // 2. check localhost - listen(port, 'localhost', err => { + // 2. check 0.0.0.0 + listen(port, '0.0.0.0', err => { if (err) { return handleError(err); } - // 3. check current ip - listen(port, address.ip(), (err, realPort) => { + // 3. check localhost + listen(port, 'localhost', err => { if (err) { return handleError(err); } - callback(null, realPort); + // 4. check current ip + listen(port, address.ip(), (err, realPort) => { + if (err) { + return handleError(err); + } + + callback(null, realPort); + }); }); }); }); diff --git a/test/cli.test.js b/test/cli.test.js index d0eb137..114c373 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -2,9 +2,10 @@ const path = require('path'); const assert = require('assert'); -const pkg = require('../package'); const CliTest = require('command-line-test'); +const pkg = require('../package'); + const cliTest = new CliTest(); const binFile = path.resolve(pkg.bin[pkg.name]); diff --git a/test/detect-port.test.js b/test/detect-port.test.js index 9efd3e7..e7f6ba1 100644 --- a/test/detect-port.test.js +++ b/test/detect-port.test.js @@ -11,7 +11,7 @@ const address = require('address'); describe('detect port test', () => { const servers = []; before(done => { - done = pedding(12, done); + done = pedding(13, done); const server = new net.Server(); server.listen(3000, 'localhost', done); server.on('error', err => { @@ -23,6 +23,10 @@ describe('detect port test', () => { server2.listen(4000, address.ip(), done); servers.push(server2); + const server3 = new net.Server(); + server3.listen(8080, '0.0.0.0', done); + servers.push(server3); + for (let port = 7000; port < 7010; port++) { const server = new net.Server(); if (port % 3 === 0) { @@ -56,7 +60,7 @@ describe('detect port test', () => { }); }); - it('work with listening next port 3001 because 3000 was listen by localhost', done => { + it('work with listening next port 3001 because 3000 was listened to localhost', done => { const port = 3000; detectPort(port, (_, realPort) => { assert(realPort === 3001); @@ -85,7 +89,7 @@ describe('detect port test', () => { }); }); - it('work with listening next port 4001 because 4000 was listen by ' + address.ip(), done => { + it('work with listening next port 4001 because 4000 was listened to ' + address.ip(), done => { const port = 4000; detectPort(port, (_, realPort) => { assert(realPort === 4001); @@ -93,6 +97,14 @@ describe('detect port test', () => { }); }); + it('work with listening next port 8081 because 8080 was listened to 0.0.0.0:8080', done => { + const port = 8080; + detectPort(port, (_, realPort) => { + assert(realPort === 8081); + done(); + }); + }); + it('work with listening random port when try port hit maxPort', done => { const port = 7000; detectPort(port, (_, realPort) => {