wallet-core/node_modules/get-port/index.js

29 lines
590 B
JavaScript
Raw Normal View History

2017-05-28 00:38:50 +02:00
'use strict';
const net = require('net');
2017-12-27 19:33:54 +01:00
const getPort = options => new Promise((resolve, reject) => {
// For backwards compatibility with number-only input
// TODO: Remove this in the next major version
if (typeof options === 'number') {
options = {
port: options
};
}
2017-05-28 00:38:50 +02:00
const server = net.createServer();
server.unref();
server.on('error', reject);
2017-12-27 19:33:54 +01:00
server.listen(options, () => {
2017-05-28 00:38:50 +02:00
const port = server.address().port;
server.close(() => {
resolve(port);
});
});
});
2017-12-27 19:33:54 +01:00
module.exports = options => options ?
getPort(options).catch(() => getPort(0)) :
2017-05-28 00:38:50 +02:00
getPort(0);