detect-port
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -16,32 +16,37 @@ /* ================================================================ | ||
var net = require('net'); | ||
const net = require('net'); | ||
var inject = function(port) { | ||
var options = global.__detect ? global.__detect.options : {}; | ||
module.exports = function() { | ||
const args = Array.prototype.slice.call(arguments); | ||
if (options.verbose) { | ||
console.log('port %d was occupied', port); | ||
} | ||
}; | ||
const promise = new Promise((resolve, reject) => { | ||
if (!args.length) { | ||
reject('wrong number of arguments'); | ||
} | ||
function detect(port, fn) { | ||
const port = args[0]; | ||
var _detect = function(port) { | ||
return new Promise(function(resolve, reject) { | ||
var socket = new net.Socket(); | ||
socket.once('error', function() { | ||
if (typeof port !== 'number') { | ||
reject(`wrong type of arguments with: ${port}`); | ||
} | ||
const loop = port => { | ||
const socket = new net.Socket(); | ||
socket.once('error', () => { | ||
socket.removeAllListeners('error'); | ||
socket.removeAllListeners('connect'); | ||
socket.removeAllListeners('error'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
var server = new net.Server(); | ||
server.on('error', function() { | ||
inject(port); | ||
const server = new net.Server(); | ||
server.on('error', () => { | ||
port++; | ||
resolve(_detect(port)); | ||
loop(port); | ||
}); | ||
server.listen(port, function() { | ||
server.once('close', function() { | ||
server.listen(port, () => { | ||
server.once('close', () => { | ||
resolve(port); | ||
@@ -52,8 +57,8 @@ }); | ||
}); | ||
socket.once('connect', function() { | ||
inject(port); | ||
socket.once('connect', () => { | ||
port++; | ||
resolve(_detect(port)); | ||
loop(port); | ||
socket.removeAllListeners('error'); | ||
socket.removeAllListeners('connect'); | ||
socket.removeAllListeners('error'); | ||
socket.end(); | ||
@@ -63,21 +68,22 @@ socket.destroy(); | ||
}); | ||
socket.connect({ | ||
port: port | ||
}); | ||
}); | ||
} | ||
}; | ||
var _detect_with_cb = function(_fn) { | ||
_detect(port) | ||
.then(function(result) { | ||
_fn(null, result); | ||
}) | ||
.catch(function(e) { | ||
_fn(e); | ||
}); | ||
}; | ||
loop(port); | ||
}); | ||
return fn ? _detect_with_cb(fn) : _detect(port); | ||
} | ||
if (args.length > 1) { | ||
const cb = args[1]; | ||
module.exports = detect; | ||
promise.then(data => { | ||
cb.call(this, null, data); | ||
}).catch(err => { | ||
cb.call(this, err); | ||
}); | ||
} else { | ||
return promise; | ||
} | ||
}; |
{ | ||
"name": "detect-port", | ||
"version": "1.0.1", | ||
"description": "port detector", | ||
"version": "1.0.2", | ||
"description": "detect available port", | ||
"keywords": [ | ||
"detect", | ||
"port", | ||
"detect-port" | ||
"port" | ||
], | ||
@@ -19,11 +18,11 @@ "bin": { | ||
}, | ||
"dependencies": { | ||
"commander": "~2.8.1" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "2.2.4", | ||
"co-mocha": "*", | ||
"command-line-test": "^1.0.8", | ||
"istanbul": "*", | ||
"should": "~6.0.3", | ||
"jshint": "*" | ||
"jshint": "*", | ||
"mocha": "2.2.4", | ||
"pre-commit": "^1.1.3", | ||
"should": "~6.0.3" | ||
}, | ||
@@ -38,9 +37,6 @@ "scripts": { | ||
"engines": { | ||
"node": ">= 0.11.14" | ||
"node": ">= 4.2.1" | ||
}, | ||
"author": "xudafeng", | ||
"homepage": "https://github.com/xudafeng/detect-port", | ||
"email": "xudafeng@126.com", | ||
"blog": "http://xdf.me", | ||
"license": "MIT" | ||
} |
@@ -15,3 +15,3 @@ # detect-port | ||
[coveralls-url]: https://coveralls.io/r/xudafeng/detect-port?branch=master | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_0.11.14-red.svg?style=flat-square | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_4-red.svg?style=flat-square | ||
[node-url]: http://nodejs.org/download/ | ||
@@ -21,32 +21,7 @@ [download-image]: https://img.shields.io/npm/dm/detect-port.svg?style=flat-square | ||
> port detector | ||
> JavaScript Implementation of Port Detector | ||
## Installment | ||
## Usage | ||
```shell | ||
$ npm i detect-port -g | ||
``` | ||
## Quick Start | ||
```shell | ||
# detect port 80 | ||
$ detect -p 80 | ||
# or like this | ||
$ detect --port 80 | ||
# will get result below | ||
$ port: 80 was occupied, try port: 1024 | ||
# with verbose | ||
$ detect --port 80 --verbose | ||
# more help? | ||
$ detect -h | ||
``` | ||
## Use As Module | ||
```shell | ||
$ npm i detect-port --save | ||
@@ -56,14 +31,17 @@ ``` | ||
```javascript | ||
var detect = require('detect-port'); | ||
const detect = require('detect-port'); | ||
/** | ||
* normal usage | ||
* callback usage | ||
*/ | ||
detect(port, function(error, _port) { | ||
detect(port, (err, _port) => { | ||
if (err) { | ||
console.log(err); | ||
} | ||
if (port === _port) { | ||
console.log('port: %d was not occupied', port); | ||
console.log(`port: ${port} was not occupied`); | ||
} else { | ||
console.log('port: %d was occupied, try port: %d', port, _port); | ||
console.log(`port: ${port} was occupied, try port: ${_port}`); | ||
} | ||
@@ -73,3 +51,2 @@ }); | ||
/** | ||
* use in co v3 | ||
* for a yield syntax instead of callback function implement | ||
@@ -84,5 +61,5 @@ */ | ||
if (port === _port) { | ||
console.log('port: %d was not occupied', port); | ||
console.log(`port: ${port} was not occupied`); | ||
} else { | ||
console.log('port: %d was occupied, try port: %d', port, _port); | ||
console.log(`port: ${port} was occupied, try port: ${_port}`); | ||
} | ||
@@ -95,34 +72,42 @@ })(); | ||
var promisePort = detect(port); | ||
detect(port) | ||
.then(_port => { | ||
if (port === _port) { | ||
console.log(`port: ${port} was not occupied`); | ||
} else { | ||
console.log(`port: ${port} was occupied, try port: ${_port}`); | ||
} | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
promisePort.then(function(_port) { | ||
if (port === _port) { | ||
console.log('port: %d was not occupied', port); | ||
} else { | ||
console.log('port: %d was occupied, try port: %d', port, _port); | ||
} | ||
}); | ||
``` | ||
## Clone and Run test | ||
## Cli Tool | ||
```shell | ||
$ npm i detect-port -g | ||
``` | ||
# clone from git | ||
$ git clone git://github.com/xudafeng/detect-port.git | ||
### Quick Start | ||
$ cd detect-port | ||
```shell | ||
# get an available port randomly | ||
$ detect | ||
# install dependencies | ||
$ make install | ||
# detect pointed port | ||
$ detect 80 | ||
# test and coverage | ||
$ make test | ||
# more help | ||
$ detect --help | ||
``` | ||
## Authors | ||
- [xudafeng](//github.com/xudafeng) | ||
- [zenzhu](//github.com/zenzhu) | ||
## License | ||
[MIT](LICENSE) | ||
Copyright (c) 2015 xdf |
Sorry, the diff of this file is not supported yet
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
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
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
0
8785
7
2
108
- Removedcommander@~2.8.1
- Removedcommander@2.8.1(transitive)
- Removedgraceful-readlink@1.0.1(transitive)