Skip to content

fix: handle 0.0.0.0:port binding #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/detect-port.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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