diff --git a/.travis.yml b/.travis.yml index 1013daa..4994fd0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/index.js b/index.js index b445bb2..bc9d88d 100644 --- a/index.js +++ b/index.js @@ -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; } }; diff --git a/readme.md b/readme.md index c331345..742b4a4 100644 --- a/readme.md +++ b/readme.md @@ -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). diff --git a/test/index.test.js b/test/index.test.js index 29ada63..0177794 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,5 +1,7 @@ +'use strict'; + const test = require("test"); -const http = require("http"); +const TcpServer = require('net').TcpServer; const detectPort = require('../'); test.setup(); @@ -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));