Skip to content

Commit 5520ee7

Browse files
committed
[js] random code cleanup
1 parent e8efc00 commit 5520ee7

File tree

2 files changed

+72
-94
lines changed

2 files changed

+72
-94
lines changed

javascript/node/selenium-webdriver/net/index.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
'use strict';
1919

20-
var os = require('os');
20+
const os = require('os');
2121

2222

2323
function getLoInterface() {
24-
var name;
24+
let name;
2525
if (process.platform === 'darwin') {
2626
name = 'lo0';
2727
} else if (process.platform === 'linux') {
@@ -34,32 +34,28 @@ function getLoInterface() {
3434
/**
3535
* Queries the system network interfaces for an IP address.
3636
* @param {boolean} loopback Whether to find a loopback address.
37-
* @param {string=} opt_family The IP family (IPv4 or IPv6). Defaults to IPv4.
38-
* @return {string} The located IP address or undefined.
37+
* @param {string} family The IP family (IPv4 or IPv6). Defaults to IPv4.
38+
* @return {(string|undefined)} The located IP address or undefined.
3939
*/
40-
function getAddress(loopback, opt_family) {
41-
var family = opt_family || 'IPv4';
42-
var addresses = [];
43-
44-
var interfaces;
40+
function getAddress(loopback, family) {
41+
let interfaces;
4542
if (loopback) {
46-
var lo = getLoInterface();
43+
let lo = getLoInterface();
4744
interfaces = lo ? [lo] : null;
4845
}
4946
interfaces = interfaces || os.networkInterfaces();
50-
for (var key in interfaces) {
47+
for (let key in interfaces) {
5148
if (!interfaces.hasOwnProperty(key)) {
5249
continue;
5350
}
5451

55-
interfaces[key].forEach(function(ipAddress) {
56-
if (ipAddress.family === family &&
57-
ipAddress.internal === loopback) {
58-
addresses.push(ipAddress.address);
52+
for (let ipAddress of interfaces[key]) {
53+
if (ipAddress.family === family && ipAddress.internal === loopback) {
54+
return ipAddress.address;
5955
}
60-
});
56+
}
6157
}
62-
return addresses[0];
58+
return undefined;
6359
}
6460

6561

@@ -68,21 +64,21 @@ function getAddress(loopback, opt_family) {
6864

6965
/**
7066
* Retrieves the external IP address for this host.
71-
* @param {string=} opt_family The IP family to retrieve. Defaults to "IPv4".
72-
* @return {string} The IP address or undefined if not available.
67+
* @param {string=} family The IP family to retrieve. Defaults to "IPv4".
68+
* @return {(string|undefined)} The IP address or undefined if not available.
7369
*/
74-
exports.getAddress = function(opt_family) {
75-
return getAddress(false, opt_family);
70+
exports.getAddress = function(family = 'IPv4') {
71+
return getAddress(false, family);
7672
};
7773

7874

7975
/**
8076
* Retrieves a loopback address for this machine.
81-
* @param {string=} opt_family The IP family to retrieve. Defaults to "IPv4".
82-
* @return {string} The IP address or undefined if not available.
77+
* @param {string=} family The IP family to retrieve. Defaults to "IPv4".
78+
* @return {(string|undefined)} The IP address or undefined if not available.
8379
*/
84-
exports.getLoopbackAddress = function(opt_family) {
85-
return getAddress(true, opt_family);
80+
exports.getLoopbackAddress = function(family = 'IPv4') {
81+
return getAddress(true, family);
8682
};
8783

8884

javascript/node/selenium-webdriver/net/portprober.js

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
'use strict';
1919

20-
var exec = require('child_process').exec,
21-
fs = require('fs'),
22-
net = require('net');
20+
const fs = require('fs');
21+
const net = require('net');
22+
const {exec} = require('child_process');
2323

2424

2525
/**
@@ -34,9 +34,9 @@ const DEFAULT_IANA_RANGE = {min: 49152, max: 65535};
3434
/**
3535
* The epheremal port range for the current system. Lazily computed on first
3636
* access.
37-
* @type {Promise.<{min: number, max: number}>}
37+
* @type {Promise<{min: number, max: number}>}
3838
*/
39-
var systemRange = null;
39+
let systemRange = null;
4040

4141

4242
/**
@@ -49,11 +49,10 @@ function findSystemPortRange() {
4949
if (systemRange) {
5050
return systemRange;
5151
}
52-
var range = process.platform === 'win32' ?
53-
findWindowsPortRange() : findUnixPortRange();
54-
return systemRange = range.catch(function() {
55-
return DEFAULT_IANA_RANGE;
56-
});
52+
let range =
53+
process.platform === 'win32'
54+
? findWindowsPortRange() : findUnixPortRange();
55+
return systemRange = range.catch(() => DEFAULT_IANA_RANGE);
5756
}
5857

5958

@@ -81,7 +80,7 @@ function execute(cmd) {
8180
* @return {!Promise<{min: number, max: number}>} A promise that will resolve
8281
* with the ephemeral port range on the current system.
8382
*/
84-
function findUnixPortRange() {
83+
async function findUnixPortRange() {
8584
var cmd;
8685
if (process.platform === 'sunos') {
8786
cmd =
@@ -94,12 +93,17 @@ function findUnixPortRange() {
9493
' | sed -e "s/.*:\\s*//"';
9594
}
9695

97-
return execute(cmd).then(function(stdout) {
98-
if (!stdout || !stdout.length) return DEFAULT_IANA_RANGE;
99-
var range = stdout.trim().split(/\s+/).map(Number);
100-
if (range.some(isNaN)) return DEFAULT_IANA_RANGE;
101-
return {min: range[0], max: range[1]};
102-
});
96+
let stdout = await execute(cmd);
97+
if (!stdout || !stdout.length) {
98+
return DEFAULT_IANA_RANGE;
99+
};
100+
101+
let range = stdout.trim().split(/\s+/).map(Number);
102+
if (range.some(isNaN)) {
103+
return DEFAULT_IANA_RANGE;
104+
};
105+
106+
return {min: range[0], max: range[1]};
103107
}
104108

105109

@@ -108,35 +112,29 @@ function findUnixPortRange() {
108112
* @return {!Promise<{min: number, max: number}>} A promise that will resolve
109113
* with the ephemeral port range on the current system.
110114
*/
111-
function findWindowsPortRange() {
115+
async function findWindowsPortRange() {
112116
// First, check if we're running on XP. If this initial command fails,
113117
// we just fallback on the default IANA range.
114-
return execute('cmd.exe /c ver').then(function(stdout) {
115-
if (/Windows XP/.test(stdout)) {
116-
// TODO: Try to read these values from the registry.
117-
return {min: 1025, max: 5000};
118-
} else {
119-
return execute('netsh int ipv4 show dynamicport tcp').
120-
then(function(stdout) {
121-
/* > netsh int ipv4 show dynamicport tcp
122-
Protocol tcp Dynamic Port Range
123-
---------------------------------
124-
Start Port : 49152
125-
Number of Ports : 16384
126-
*/
127-
var range = stdout.split(/\n/).filter(function(line) {
128-
return /.*:\s*\d+/.test(line);
129-
}).map(function(line) {
130-
return Number(line.split(/:\s*/)[1]);
131-
});
132-
133-
return {
134-
min: range[0],
135-
max: range[0] + range[1]
136-
};
137-
});
138-
}
139-
});
118+
let stdout = await execute('cmd.exe /c ver');
119+
if (/Windows XP/.test(stdout)) {
120+
// TODO: Try to read these values from the registry.
121+
return {min: 1025, max: 5000};
122+
} else {
123+
stdout = await execute('netsh int ipv4 show dynamicport tcp');
124+
/* > netsh int ipv4 show dynamicport tcp
125+
Protocol tcp Dynamic Port Range
126+
---------------------------------
127+
Start Port : 49152
128+
Number of Ports : 16384
129+
*/
130+
let range = stdout.split(/\n/)
131+
.filter((line) => /.*:\s*\d+/.test(line))
132+
.map((line) => Number(line.split(/:\s*/)[1]));
133+
return {
134+
min: range[0],
135+
max: range[0] + range[1]
136+
};
137+
}
140138
}
141139

142140

@@ -171,31 +169,15 @@ function isFree(port, opt_host) {
171169
* @return {!Promise<number>} A promise that will resolve to a free port. If a
172170
* port cannot be found, the promise will be rejected.
173171
*/
174-
function findFreePort(opt_host) {
175-
return findSystemPortRange().then(function(range) {
176-
var attempts = 0;
177-
var maxAttempts = 100;
178-
return new Promise((resolve, reject) => {
179-
findPort();
180-
181-
function findPort() {
182-
attempts += 1;
183-
if (attempts > maxAttempts) {
184-
reject(Error('Unable to find a free port'));
185-
}
186-
187-
var port = Math.floor(
188-
Math.random() * (range.max - range.min) + range.min);
189-
isFree(port, opt_host).then(function(isFree) {
190-
if (isFree) {
191-
resolve(port);
192-
} else {
193-
findPort();
194-
}
195-
}, findPort);
196-
}
197-
});
198-
});
172+
async function findFreePort(opt_host) {
173+
let range = await findSystemPortRange();
174+
for (let i = 0; i < 100; i++) {
175+
let port = Math.floor(Math.random() * (range.max - range.min) + range.min);
176+
if (await isFree(port, opt_host)) {
177+
return port;
178+
}
179+
}
180+
throw Error('Unable to find a free port');
199181
}
200182

201183

0 commit comments

Comments
 (0)