From be370beb054678e5154d84c9711cc6e4863cf2ed Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sat, 29 Oct 2016 22:00:07 +0300 Subject: [PATCH] feature(package) rm freeport --- lib/server.js | 26 ++++++++------------------ package.json | 1 - test/cloudcmd.js | 27 ++++++++++----------------- 3 files changed, 18 insertions(+), 36 deletions(-) diff --git a/lib/server.js b/lib/server.js index 41441639..ee9b7f97 100644 --- a/lib/server.js +++ b/lib/server.js @@ -11,7 +11,6 @@ var DIR_LIB = './', config = require(DIR_SERVER + 'config'), prefixer = require(DIR_SERVER + 'prefixer'), express = require('express'), - freeport = require('freeport'), tryRequire = require('tryrequire'), logger = tryRequire('morgan'), io = require('socket.io'), @@ -25,7 +24,7 @@ var DIR_LIB = './', */ module.exports = function(options) { var server, - getPort, onError, + getPort, port = process.env.PORT || /* c9 */ process.env.VCAP_APP_PORT || /* cloudfoundry */ @@ -52,28 +51,19 @@ module.exports = function(options) { if (port < 0 || port > 65535) exit('cloudcmd --port: %s', 'port number could be 1..65535, 0 means any available port'); - getPort = function(fn) { - port ? fn(null, port) : freeport(fn); - }; - - onError = function(error) { - exit('cloudcmd --port: %s', error.message); - }; - - getPort(function(error, port) { + server.listen(port, ip, function() { var host = config('ip') || 'localhost'; - var url = 'http://' + host + ':' + port + prefix() + '/'; - - if (error) - return onError(error); - - server.listen(port, ip); - server.on('error', onError); + var port0 = port || server.address().port; + var url = 'http://' + host + ':' + port0 + prefix() + '/'; console.log('url:', url); if (config('open')) opn(url); }); + + server.on('error', function(error) { + exit('cloudcmd --port: %s', error.message); + }); }; diff --git a/package.json b/package.json index d65a2a6d..b94d32b7 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,6 @@ "files-io": "^1.2.0", "flop": "^1.4.0", "format-io": "^0.9.6", - "freeport": "^1.0.5", "http-auth": "^2.3.1", "ishtar": "^1.5.0", "jaguar": "^1.1.0", diff --git a/test/cloudcmd.js b/test/cloudcmd.js index 45f9cfd8..b8f33454 100644 --- a/test/cloudcmd.js +++ b/test/cloudcmd.js @@ -7,10 +7,8 @@ const promisify = require('es6-promisify'); const pullout = require('pullout'); const request = require('request'); -const wrap = (fn, ...a) => (...b) => fn(...a, ...b); const warp = (fn, ...a) => (...b) => fn(...b, ...a); -const freeport = promisify(require('freeport')); const _pullout = promisify(pullout); const get = promisify((url, fn) => { @@ -23,8 +21,6 @@ const put = promisify((options, fn) => { const cloudcmd = require('..'); -const host = '127.0.0.1'; - const before = (fn) => { const app = express(); const server = http.createServer(app); @@ -32,10 +28,6 @@ const before = (fn) => { server.close(); }; - const listen = (port) => { - server.listen(port, host, wrap(fn, port, after)); - }; - app.use(cloudcmd({ config: { auth: false, @@ -43,13 +35,14 @@ const before = (fn) => { } })); - freeport() - .then(listen) + server.listen(() => { + fn(server.address().port, after); + }); }; test('cloudcmd: rest: fs: path', (t) => { before((port, after) => { - get(`http://${host}:${port}/api/v1/fs`) + get(`http://localhost:${port}/api/v1/fs`) .then(warp(_pullout, 'string')) .then(JSON.parse) .then((dir) => { @@ -65,7 +58,7 @@ test('cloudcmd: rest: fs: path', (t) => { test('cloudcmd: rest: pack: get', (t) => { before((port, after) => { - get(`http://${host}:${port}/api/v1/pack/fixture/pack`) + get(`http://localhost:${port}/api/v1/pack/fixture/pack`) .then(_pullout) .then((pack) => { const fixture = fs.readFileSync(__dirname + '/fixture/pack.tar.gz'); @@ -82,7 +75,7 @@ test('cloudcmd: rest: pack: get', (t) => { test('cloudcmd: rest: pack: put: file', (t) => { before((port, after) => { const name = String(Math.random()) + '.tar.gz'; - const options = getPackOptions(host, port, name); + const options = getPackOptions(port, name); put(options) .then(warp(_pullout, 'string')) @@ -105,7 +98,7 @@ test('cloudcmd: rest: pack: put: file', (t) => { test('cloudcmd: rest: pack: put: response', (t) => { before((port, after) => { const name = String(Math.random()) + '.tar.gz'; - const options = getPackOptions(host, port, name); + const options = getPackOptions(port, name); put(options) .then(warp(_pullout, 'string')) @@ -125,7 +118,7 @@ test('cloudcmd: rest: pack: put: response', (t) => { test('cloudcmd: rest: pack: put: error', (t) => { before((port, after) => { - const options = getPackOptions(host, port, 'name', [ + const options = getPackOptions(port, 'name', [ 'not found' ]); @@ -143,9 +136,9 @@ test('cloudcmd: rest: pack: put: error', (t) => { }); }); -function getPackOptions(host, port, to, names = ['pack']) { +function getPackOptions(port, to, names = ['pack']) { return { - url: `http://${host}:${port}/api/v1/pack`, + url: `http://localhost:${port}/api/v1/pack`, json: { to, names,