diff options
Diffstat (limited to 'src/node/port_picker.js')
-rw-r--r-- | src/node/port_picker.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/node/port_picker.js b/src/node/port_picker.js new file mode 100644 index 0000000000..7b3cfd74ff --- /dev/null +++ b/src/node/port_picker.js @@ -0,0 +1,19 @@ +var net = require('net'); + +/** + * Finds a free port that a server can bind to, in the format + * "address:port" + * @param {function(string)} cb The callback that should execute when the port + * is available + */ +function nextAvailablePort(cb) { + var server = net.createServer(); + server.listen(function() { + var address = server.address(); + server.close(function() { + cb(address.address + ':' + address.port.toString()); + }); + }); +} + +exports.nextAvailablePort = nextAvailablePort; |