detect-port.js
2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';
const debug = require('debug')('detect-port');
const net = require('net');
const address = require('address');
module.exports = (port, host, callback) => {
if (typeof port === 'function') {
callback = port;
port = null;
} else if (typeof host === 'function') {
callback = host;
host = null;
}
port = parseInt(port) || 0;
let maxPort = port + 10;
if (maxPort > 65535) {
maxPort = 65535;
}
debug('detect free port between [%s, %s)', port, maxPort);
if (typeof callback === 'function') {
return tryListen(host, port, maxPort, callback);
}
// promise
return new Promise((resolve, reject) => {
tryListen(host, port, maxPort, (error, realPort) => {
if (error) {
reject(error);
} else {
resolve(realPort);
}
});
});
};
function tryListen(host, port, maxPort, callback) {
function handleError() {
port++;
if (port >= maxPort) {
debug(
'port: %s >= maxPort: %s, give up and use random port',
port,
maxPort
);
port = 0;
maxPort = 0;
}
tryListen(host, port, maxPort, callback);
}
// 1. check specified host (or null)
listen(port, host, (err, realPort) => {
// ignore random listening
if (port === 0) {
return callback(err, realPort);
}
if (err) {
return handleError(err);
}
// 2. check default host
listen(port, null, err => {
if (err) {
return handleError(err);
}
// 3. check localhost
listen(port, 'localhost', err => {
if (err) {
return handleError(err);
}
// 4. check current ip
let ip;
try {
ip = address.ip();
} catch (err) {
// Skip the `ip` check if `address.ip()` fails
return callback(null, realPort);
}
listen(port, ip, (err, realPort) => {
if (err) {
return handleError(err);
}
callback(null, realPort);
});
});
});
});
}
function listen(port, hostname, callback) {
const server = new net.Server();
server.on('error', err => {
debug('listen %s:%s error: %s', hostname, port, err);
server.close();
if (err.code === 'ENOTFOUND') {
debug('ignore dns ENOTFOUND error, get free %s:%s', hostname, port);
return callback(null, port);
}
return callback(err);
});
server.listen(port, hostname, () => {
port = server.address().port;
server.close();
debug('get free %s:%s', hostname, port);
return callback(null, port);
});
}