Skip to content

Commit 9dde634

Browse files
committed
Replace var with const/let
Signed-off-by: Matthew Peveler <[email protected]>
1 parent 64b49a5 commit 9dde634

File tree

7 files changed

+71
-69
lines changed

7 files changed

+71
-69
lines changed

lib/portfinder.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
"use strict";
99

10-
var fs = require('fs'),
11-
os = require('os'),
12-
net = require('net'),
13-
path = require('path'),
14-
_async = require('async'),
15-
debug = require('debug'),
16-
mkdirp = require('mkdirp').mkdirp;
10+
const fs = require('fs'),
11+
os = require('os'),
12+
net = require('net'),
13+
path = require('path'),
14+
_async = require('async'),
15+
debug = require('debug'),
16+
mkdirp = require('mkdirp').mkdirp;
1717

18-
var debugTestPort = debug('portfinder:testPort'),
19-
debugGetPort = debug('portfinder:getPort'),
20-
debugDefaultHosts = debug('portfinder:defaultHosts');
18+
const debugTestPort = debug('portfinder:testPort'),
19+
debugGetPort = debug('portfinder:getPort'),
20+
debugDefaultHosts = debug('portfinder:defaultHosts');
2121

22-
var internals = {};
22+
const internals = {};
2323

2424
internals.testPort = function(options, callback) {
2525
if (!callback) {
@@ -54,7 +54,7 @@ internals.testPort = function(options, callback) {
5454
return callback(err);
5555
}
5656

57-
var nextPort = exports.nextPort(options.port);
57+
const nextPort = exports.nextPort(options.port);
5858

5959
if (nextPort > exports.highestPort) {
6060
return callback(new Error('No open ports available'));
@@ -154,7 +154,8 @@ exports.getPort = function (options, callback) {
154154
exports._defaultHosts.push(options.host)
155155
}
156156

157-
var openPorts = [], currentHost;
157+
const openPorts = [];
158+
let currentHost;
158159
return _async.eachSeries(exports._defaultHosts, function(host, next) {
159160
debugGetPort("in eachSeries() iteration callback: host is", host);
160161

@@ -183,10 +184,10 @@ exports.getPort = function (options, callback) {
183184
// NOTE: We may need to one day handle `my-non-existent-host.local` if users
184185
// report frustration with passing in hostnames that DONT map to bindable
185186
// hosts, without showing them a good error.
186-
var msg = 'Provided host ' + options.host + ' could NOT be bound. Please provide a different host address or hostname';
187+
const msg = 'Provided host ' + options.host + ' could NOT be bound. Please provide a different host address or hostname';
187188
return callback(Error(msg));
188189
} else {
189-
var idx = exports._defaultHosts.indexOf(currentHost);
190+
const idx = exports._defaultHosts.indexOf(currentHost);
190191
exports._defaultHosts.splice(idx, 1);
191192
return exports.getPort(options, callback);
192193
}
@@ -209,7 +210,7 @@ exports.getPort = function (options, callback) {
209210
return callback(null, openPorts[0]);
210211
}
211212
else {
212-
var msg = 'No open ports found in between '+ options.startPort + ' and ' + options.stopPort;
213+
const msg = 'No open ports found in between '+ options.startPort + ' and ' + options.stopPort;
213214
return callback(Error(msg));
214215
}
215216
} else {
@@ -256,7 +257,7 @@ exports.getPorts = function (count, options, callback) {
256257
options = {};
257258
}
258259

259-
var lastPort = null;
260+
let lastPort = null;
260261
_async.timesSeries(count, function(index, asyncCallback) {
261262
if (lastPort) {
262263
options.port = exports.nextPort(lastPort);
@@ -363,7 +364,7 @@ exports.getSocket = function (options, callback) {
363364
// then test connection.
364365
//
365366
function checkAndTestSocket () {
366-
var dir = path.dirname(options.path);
367+
const dir = path.dirname(options.path);
367368

368369
fs.stat(dir, function (err, stats) {
369370
if (err || !stats.isDirectory()) {
@@ -402,11 +403,11 @@ exports.nextPort = function (port) {
402403
// specified `socketPath`.
403404
//
404405
exports.nextSocket = function (socketPath) {
405-
var dir = path.dirname(socketPath),
406-
name = path.basename(socketPath, '.sock'),
407-
match = name.match(/^([a-zA-z]+)(\d*)$/i),
408-
index = parseInt(match[2]),
409-
base = match[1];
406+
const dir = path.dirname(socketPath),
407+
name = path.basename(socketPath, '.sock'),
408+
match = name.match(/^([a-zA-z]+)(\d*)$/i),
409+
base = match[1];
410+
let index = parseInt(match[2]);
410411
if (isNaN(index)) {
411412
index = 0;
412413
}
@@ -492,7 +493,7 @@ exports.nextSocket = function (socketPath) {
492493
* Note we export this so we can use it in our tests, otherwise this API is private
493494
*/
494495
exports._defaultHosts = (function() {
495-
var interfaces = {};
496+
let interfaces = {};
496497
try{
497498
interfaces = os.networkInterfaces();
498499
}
@@ -514,13 +515,13 @@ exports._defaultHosts = (function() {
514515
}
515516
}
516517

517-
var interfaceNames = Object.keys(interfaces),
518-
hiddenButImportantHost = '0.0.0.0', // !important - dont remove, hence the naming :)
519-
results = [hiddenButImportantHost];
520-
for (var i = 0; i < interfaceNames.length; i++) {
521-
var _interface = interfaces[interfaceNames[i]];
522-
for (var j = 0; j < _interface.length; j++) {
523-
var curr = _interface[j];
518+
const interfaceNames = Object.keys(interfaces),
519+
hiddenButImportantHost = '0.0.0.0', // !important - dont remove, hence the naming :)
520+
results = [hiddenButImportantHost];
521+
for (let i = 0; i < interfaceNames.length; i++) {
522+
const _interface = interfaces[interfaceNames[i]];
523+
for (let j = 0; j < _interface.length; j++) {
524+
const curr = _interface[j];
524525
results.push(curr.address);
525526
}
526527
}

test/getPort.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
var portfinder = require('../lib/portfinder');
1+
const portfinder = require('../lib/portfinder');
22

3-
if(process.argv[2]) {
4-
var port = process.argv[2];
5-
if ( isNaN(port) ) {
3+
if (process.argv[2]) {
4+
const port = process.argv[2];
5+
if (isNaN(port)) {
66
console.log('passed value is not a port');
77
} else {
88
portfinder.basePort = port;
99
}
1010
}
11+
1112
portfinder.getPort(function(err, port) {
12-
if(err) {
13+
if (err) {
1314
console.error(err);
1415
} else {
1516
console.log(port);

test/helper.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22

3-
var _async = require('async'),
4-
http = require('http');
3+
const _async = require('async'),
4+
http = require('http');
55

66

77
function createServer(base, host, next) {
8-
var server = http.createServer(function () {});
8+
const server = http.createServer(function () {});
99

1010
if (!next) {
1111
server.listen(base, host);
@@ -27,13 +27,13 @@ module.exports.startServers = function(servers, startPort, endPort, callback) {
2727
startPort = undefined;
2828
}
2929

30-
var base = startPort || 32768;
30+
let base = startPort || 32768;
3131
endPort = endPort || 32773;
3232

3333
_async.whilst(
3434
function () { return base < endPort; },
3535
function (next) {
36-
var hosts = ['localhost'];
36+
const hosts = ['localhost'];
3737
while (hosts.length > 1) { servers.push(createServer(base, hosts.shift())); }
3838
servers.push(createServer(base, hosts.shift(), next)); // call next for host
3939
base++;

test/port-finder-integration.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66

77

8-
var portfinder = require('../lib/portfinder'),
9-
child_process = require('child_process'),
10-
path = require('path'),
11-
http = require('http');
8+
const portfinder = require('../lib/portfinder'),
9+
child_process = require('child_process'),
10+
path = require('path'),
11+
http = require('http');
1212

1313
const host = "localhost";
14-
var server;
14+
let server;
1515

1616
describe('portfinder', function () {
1717
afterAll(function (done) {
@@ -24,14 +24,14 @@ describe('portfinder', function () {
2424
portfinder.getPort(function (err, port) {
2525
expect(err).toBeNull();
2626
server = http.createServer(function () {}).listen(port, host, function () {
27-
var timeout = false;
28-
var fileToExec = path.join(__dirname, 'getPort.js');
29-
var timer = setTimeout(function () {
27+
let timeout = false;
28+
const fileToExec = path.join(__dirname, 'getPort.js');
29+
const timer = setTimeout(function () {
3030
timeout = true;
3131
process.kill(child.pid);
3232
done("timeout");
3333
}, 10000); // 10 seconds
34-
var child = child_process.spawn('node', [fileToExec, port]);
34+
const child = child_process.spawn('node', [fileToExec, port]);
3535
child.on('close', function () {
3636
if (timeout === false) {
3737
clearTimeout(timer);

test/port-finder-multiple.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
"use strict";
99

10-
var portfinder = require('../lib/portfinder'),
11-
helper = require('./helper');
10+
const portfinder = require('../lib/portfinder'),
11+
helper = require('./helper');
1212

1313
portfinder.basePort = 32768;
1414

1515
describe('with 5 existing servers', function () {
16-
var servers = [];
16+
const servers = [];
1717
beforeAll(function (done) {
1818
helper.startServers(servers, done);
1919
});

test/port-finder-socket.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77

88
"use strict";
99

10-
var net = require('net'),
11-
path = require('path'),
12-
_async = require('async'),
13-
portfinder = require('../lib/portfinder'),
14-
fs = require('fs');
10+
const net = require('net'),
11+
path = require('path'),
12+
_async = require('async'),
13+
portfinder = require('../lib/portfinder'),
14+
fs = require('fs');
1515

16-
var servers = [],
17-
socketDir = path.join(__dirname, 'fixtures'),
18-
badDir = path.join(__dirname, 'bad-dir');
16+
const servers = [],
17+
socketDir = path.join(__dirname, 'fixtures'),
18+
badDir = path.join(__dirname, 'bad-dir');
1919

2020
function createServers (callback) {
21-
var base = 0;
21+
let base = 0;
2222

2323
_async.whilst(
2424
function () { return base < 5 },
2525
function (next) {
26-
var server = net.createServer(function () { }),
27-
name = base === 0 ? 'test.sock' : 'test' + base + '.sock',
28-
sock = path.join(socketDir, name);
26+
const server = net.createServer(function () { }),
27+
name = base === 0 ? 'test.sock' : 'test' + base + '.sock';
28+
let sock = path.join(socketDir, name);
2929

3030
// shamelessly stolen from foreverjs,
3131
// https://github.com/foreverjs/forever/blob/6d143609dd3712a1cf1bc515d24ac6b9d32b2588/lib/forever/worker.js#L141-L154

test/port-finder.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
"use strict";
99

10-
var portfinder = require('../lib/portfinder'),
11-
helper = require('./helper');
10+
const portfinder = require('../lib/portfinder'),
11+
helper = require('./helper');
1212

1313
portfinder.basePort = 32768;
1414

1515
describe('with 5 existing servers', function () {
16-
var servers = [];
16+
const servers = [];
1717
beforeAll(function (done) {
1818
helper.startServers(servers, done);
1919
});
@@ -97,7 +97,7 @@ test('the getPort() method with startPort less than or equal to 80', function (d
9797
});
9898

9999
describe('with no available ports above the start port', function () {
100-
var servers = [];
100+
const servers = [];
101101
beforeEach(function (done) {
102102
helper.startServers(servers, 65530, 65536, done);
103103
});

0 commit comments

Comments
 (0)