Skip to content

Commit

Permalink
fix: handle 0.0.0.0:port binding (#26)
Browse files Browse the repository at this point in the history
* fix: handle 0.0.0.0:port binding

* typo
  • Loading branch information
fengmk2 authored and xudafeng committed Jun 12, 2017
1 parent 4a9e127 commit 398bc4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
21 changes: 14 additions & 7 deletions 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') {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
});
});
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/cli.test.js
Expand Up @@ -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]);

Expand Down
18 changes: 15 additions & 3 deletions test/detect-port.test.js
Expand Up @@ -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 => {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -85,14 +89,22 @@ 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);
done();
});
});

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) => {
Expand Down

0 comments on commit 398bc4f

Please sign in to comment.