Skip to content
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

feat: add address support #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
55 changes: 48 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,56 @@
language: node_js
node_js: '6'
env:
- VERSION=0.3.1
- VERSION=0.4.1
- VERSION=0.16.0
- VERSION=0.24.0
- VERSION=0.25.0
matrix:
include:
- os: linux
env:
- VERSION=0.3.1
- os: linux
env:
- VERSION=0.4.1
- os: linux
env:
- VERSION=0.16.0
- os: linux
env:
- VERSION=0.24.0
- os: linux
env:
- VERSION=0.25.0
- os: osx
osx_image: xcode9
env:
- VERSION=0.3.1
- os: osx
osx_image: xcode9
env:
- VERSION=0.4.1
- os: osx
osx_image: xcode9
env:
- VERSION=0.16.0
- os: osx
osx_image: xcode9
env:
- VERSION=0.24.0
- os: osx
osx_image: xcode9
env:
- VERSION=0.25.0
before_install:
- OS=linux
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
rvm install ruby-2.4.0;
rvm --default use 2.4.0;
ruby -v;
brew update;
brew install xz;
OS=darwin;
fi
script:
- npm i
- mkdir -p ./node_modules/.bin
- curl -SL "https://github.com/fibjs/fibjs/releases/download/v${VERSION}/fibjs-v${VERSION}-linux-x64.xz" -o ./node_modules/.bin/fibjs.xz
- curl -SL "https://github.com/fibjs/fibjs/releases/download/v${VERSION}/fibjs-v${VERSION}-${OS}-x64.xz" -o ./node_modules/.bin/fibjs.xz
- xz -d ./node_modules/.bin/fibjs.xz
- chmod a+x ./node_modules/.bin/fibjs
- npm run ci
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
'use strict';

const TcpServer = require('net').TcpServer;
const Socket = require('net').Socket;

module.exports = function detectPort(port) {
module.exports = function detectPort(port, address = '') {
let svr;
svr = new Socket();
try {
svr = new TcpServer(port, () => { });
svr.run(() => {});
svr.bind(address, port);
} catch (error) {
svr = new TcpServer(0, () => { });
svr.run(() => {});
svr.bind(address, 0);
} finally {
port = svr.socket.localPort;
svr.stop();
port = svr.localPort;
svr.close();
return port;
}
};
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ if (availablePort === port) {
}
```

## API

### detectPort(port, address = '')

- port Integer. the port to be detected.
- address String. Specific the address to detect, default to "*".

## Questions & Suggestions

Please open an issue [here](https://github.com/fibjs-modules/detect-port/issues).
Expand Down
20 changes: 18 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const test = require("test");
const http = require("http");
const TcpServer = require('net').TcpServer;
const detectPort = require('../');

test.setup();
Expand All @@ -10,11 +12,25 @@ describe('detectPort', () => {
});

it("random port", () => {
let svr = new http.Server(3000, () => { });
let svr = new TcpServer(3000, () => { });
svr.run(() => {});
assert.notEqual(detectPort(3000), 3000);
svr.stop();
});

it("same address", () => {
let svr = new TcpServer('127.0.0.1', 3000, () => { });
svr.run(() => {});
assert.notEqual(detectPort(3000, '127.0.0.1'), 3000);
svr.stop();
});

it("different address", () => {
let svr = new TcpServer('127.0.0.1', 3000, () => { });
svr.run(() => {});
assert.equal(detectPort(3000), 3000);
svr.stop();
});
});

process.exit(test.run(console.DEBUG));