random-port
Advanced tools
Comparing version 0.0.2 to 0.1.0
33
index.js
var net = require('net'); | ||
var random_port = function(cb) { | ||
/** @todo handle opts */ | ||
var from = 15000, | ||
range = 100, | ||
var random_port = function() { | ||
var cb, | ||
opts = {}; | ||
if (arguments.length == 0) { | ||
throw "no callback"; | ||
} | ||
else if (arguments.length == 1) { | ||
cb = arguments[0]; | ||
} | ||
else { | ||
opts = arguments[0]; | ||
cb = arguments[arguments.length - 1]; | ||
} | ||
if (typeof cb != 'function') { | ||
throw "callback is not a function"; | ||
} | ||
if (typeof opts != 'object') { | ||
throw "options is not a object"; | ||
} | ||
var from = opts.from > 0 ? opts.from : 15000, | ||
range = opts.range > 0 ? opts.range : 100, | ||
port = from + ~~(Math.random() * range); | ||
/** @todo only root can listen to ports less than 1024 */ | ||
var server = net.createServer(); | ||
@@ -17,3 +40,3 @@ server.listen(port, function (err) { | ||
server.on('error', function (err) { | ||
get_port(cb); | ||
random_port(opts, cb); | ||
}); | ||
@@ -20,0 +43,0 @@ }; |
{ | ||
"name": "random-port", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "get a random free tcp port and pass to the callback", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,1 +5,12 @@ node-random-port | ||
get a free random tcp port and pass to the callback | ||
```javascript | ||
var random_port = require('random-port'); | ||
random_port(console.log); // default will return a port from 15000 to 15099 | ||
random_port({from: 20000}, console.log); | ||
random_port({from: 20000, range: 10}, console.log); | ||
``` |
var random_port = require('./index.js'); | ||
random_port(console.log); | ||
random_port({from: 20000}, console.log); | ||
random_port({from: 20000, range: 10}, console.log); |
2558
40
16