From cbf9466827f6b13fb25c1b995afd9f5fb482634b Mon Sep 17 00:00:00 2001 From: ngot Date: Tue, 19 Jun 2018 00:07:35 +0800 Subject: [PATCH 1/4] feat: add address support --- index.js | 6 +++--- readme.md | 7 +++++++ test/index.test.js | 18 ++++++++++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index b445bb2..d78c387 100644 --- a/index.js +++ b/index.js @@ -2,13 +2,13 @@ const TcpServer = require('net').TcpServer; -module.exports = function detectPort(port) { +module.exports = function detectPort(port, address = '') { let svr; try { - svr = new TcpServer(port, () => { }); + svr = new TcpServer(address, port, () => { }); svr.run(() => {}); } catch (error) { - svr = new TcpServer(0, () => { }); + svr = new TcpServer(address, 0, () => { }); svr.run(() => {}); } finally { port = svr.socket.localPort; 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..8304605 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,5 +1,5 @@ const test = require("test"); -const http = require("http"); +const TcpServer = require('net').TcpServer; const detectPort = require('../'); test.setup(); @@ -10,11 +10,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)); From fa4fc125033dd6095da9e08677bc9469302b78bb Mon Sep 17 00:00:00 2001 From: ngot Date: Tue, 19 Jun 2018 00:09:53 +0800 Subject: [PATCH 2/4] f --- test/index.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/index.test.js b/test/index.test.js index 8304605..0177794 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,3 +1,5 @@ +'use strict'; + const test = require("test"); const TcpServer = require('net').TcpServer; const detectPort = require('../'); From a2b5784effb0d64b37a9d7ff34d341f60a33e480 Mon Sep 17 00:00:00 2001 From: ngot Date: Tue, 19 Jun 2018 00:26:35 +0800 Subject: [PATCH 3/4] f --- index.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index d78c387..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, address = '') { let svr; + svr = new Socket(); try { - svr = new TcpServer(address, port, () => { }); - svr.run(() => {}); + svr.bind(address, port); } catch (error) { - svr = new TcpServer(address, 0, () => { }); - svr.run(() => {}); + svr.bind(address, 0); } finally { - port = svr.socket.localPort; - svr.stop(); + port = svr.localPort; + svr.close(); return port; } }; From 44876da12577bd59723962cd2f03b4ec76a893cf Mon Sep 17 00:00:00 2001 From: ngot Date: Tue, 19 Jun 2018 01:52:05 +0800 Subject: [PATCH 4/4] f --- .travis.yml | 55 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) 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