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

21 lines
423 B
JavaScript
Raw Normal View History

2017-05-28 00:38:50 +02:00
'use strict';
const net = require('net');
const getPort = port => new Promise((resolve, reject) => {
const server = net.createServer();
server.unref();
server.on('error', reject);
server.listen(port, () => {
const port = server.address().port;
server.close(() => {
resolve(port);
});
});
});
module.exports = preferredPort => preferredPort ?
getPort(preferredPort).catch(() => getPort(0)) :
getPort(0);