Swap-Aggregator-Subgraph/node_modules/get-port/index.js
Richa-iitr d211083153 Revert "Revert "added handler""
This reverts commit c36ee8c5ca.
2022-07-03 07:30:05 +05:30

29 lines
590 B
JavaScript

'use strict';
const net = require('net');
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
};
}
const server = net.createServer();
server.unref();
server.on('error', reject);
server.listen(options, () => {
const port = server.address().port;
server.close(() => {
resolve(port);
});
});
});
module.exports = options => options ?
getPort(options).catch(() => getPort(0)) :
getPort(0);