detect-port
Advanced tools
Comparing version 1.0.7 to 1.1.0
@@ -0,1 +1,5 @@ | ||
# 1.1.0 / 2016-01-17 | ||
* Use server listen to detect port | ||
# 1.0.7 / 2016-12-11 | ||
@@ -2,0 +6,0 @@ |
'use strict'; | ||
const debug = require('debug')('detect-port'); | ||
const net = require('net'); | ||
module.exports = function() { | ||
const args = Array.prototype.slice.call(arguments); | ||
module.exports = (port, callback) => { | ||
if (typeof port === 'function') { | ||
callback = port; | ||
port = null; | ||
} | ||
if (typeof callback === 'function') { | ||
return tryListen(port, callback); | ||
} | ||
// promise | ||
return new Promise(resolve => { | ||
tryListen(port, (_, realPort) => { | ||
resolve(realPort); | ||
}); | ||
}); | ||
}; | ||
const promise = new Promise((resolve, reject) => { | ||
if (!args.length) { | ||
return reject('wrong number of arguments'); | ||
} | ||
function tryListen(port, callback) { | ||
port = parseInt(port) || 0; | ||
const server = new net.Server(); | ||
const port = parseInt(args[0], 10); | ||
server.on('error', err => { | ||
debug('listen %s error: %s', port, err); | ||
port = 0; | ||
server.close(); | ||
return tryListen(port, callback); | ||
}); | ||
if (isNaN(port)) { | ||
return reject(`wrong type of arguments with: '${args[0]}'`); | ||
} | ||
const loop = port => { | ||
const socket = new net.Socket(); | ||
socket.once('error', () => { | ||
socket.removeAllListeners('error'); | ||
socket.removeAllListeners('connect'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
const server = new net.Server(); | ||
server.on('error', () => { | ||
port++; | ||
loop(port); | ||
}); | ||
server.listen(port, () => { | ||
server.once('close', () => { | ||
resolve(port); | ||
}); | ||
server.close(); | ||
}); | ||
}); | ||
socket.once('connect', () => { | ||
port++; | ||
loop(port); | ||
socket.removeAllListeners('error'); | ||
socket.removeAllListeners('connect'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
}); | ||
socket.connect({ | ||
port: port | ||
}); | ||
}; | ||
loop(port); | ||
server.listen({ port }, () => { | ||
port = server.address().port; | ||
server.close(); | ||
debug('get free port: %s', port); | ||
callback(null, port); | ||
}); | ||
if (args.length > 1) { | ||
const cb = args[1]; | ||
promise.then(data => { | ||
process.nextTick(cb.bind(null, null, data)); | ||
}, err => { | ||
process.nextTick(cb.bind(null, err)); | ||
}); | ||
} else { | ||
return promise; | ||
} | ||
}; | ||
} |
{ | ||
"name": "detect-port", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "detect available port", | ||
@@ -18,24 +18,25 @@ "keywords": [ | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"debug": "^2.6.0" | ||
}, | ||
"devDependencies": { | ||
"co-mocha": "*", | ||
"command-line-test": "^1.0.8", | ||
"istanbul": "*", | ||
"jshint": "*", | ||
"mocha": "2.2.4", | ||
"pre-commit": "^1.1.3", | ||
"should": "~6.0.3" | ||
"egg-bin": "^2.0.0", | ||
"egg-ci": "^1.1.0", | ||
"eslint": "^3.13.1", | ||
"eslint-config-egg": "^3.1.0" | ||
}, | ||
"scripts": { | ||
"test": "make test", | ||
"jshint": "make jshint" | ||
"test": "egg-bin test", | ||
"ci": "npm run lint && egg-bin cov", | ||
"lint": "eslint ." | ||
}, | ||
"precommit": [ | ||
"jshint" | ||
], | ||
"engines": { | ||
"node": ">= 4.2.1" | ||
}, | ||
"ci": { | ||
"version": "4, 6, 7" | ||
}, | ||
"homepage": "https://github.com/node-modules/detect-port", | ||
"license": "MIT" | ||
} |
@@ -6,5 +6,4 @@ [![logo][logo-image]][logo-url] | ||
[![NPM version][npm-image]][npm-url] | ||
[![node version][node-image]][node-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Coveralls][coveralls-image]][coveralls-url] | ||
[![Test coverage][codecov-image]][codecov-url] | ||
[![npm download][download-image]][download-url] | ||
@@ -18,6 +17,4 @@ | ||
[travis-url]: https://travis-ci.org/node-modules/detect-port | ||
[coveralls-image]: https://img.shields.io/coveralls/node-modules/detect-port.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/node-modules/detect-port?branch=master | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_4-red.svg?style=flat-square | ||
[node-url]: http://nodejs.org/download/ | ||
[codecov-image]: https://codecov.io/gh/node-modules/detect-port/branch/master/graph/badge.svg | ||
[codecov-url]: https://codecov.io/gh/node-modules/detect-port | ||
[download-image]: https://img.shields.io/npm/dm/detect-port.svg?style=flat-square | ||
@@ -30,7 +27,7 @@ [download-url]: https://npmjs.org/package/detect-port | ||
```shell | ||
```bash | ||
$ npm i detect-port --save | ||
``` | ||
```javascript | ||
```js | ||
const detect = require('detect-port'); | ||
@@ -37,0 +34,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5
13
29534
1
36
109
+ Addeddebug@^2.6.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)