aboutsummaryrefslogtreecommitdiff
path: root/node_modules/get-port/index.js
blob: de99a36626eb55089f097ff94e017110967d193f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'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);