Skip to content

Commit c33a961

Browse files
authored
Merge pull request #161 from MasterOdin/chore-const-let
Replace var with const/let
2 parents 1aa5ebd + 9dde634 commit c33a961

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 {
@@ -252,7 +253,7 @@ exports.getPorts = function (count, options, callback) {
252253
options = {};
253254
}
254255

255-
var lastPort = null;
256+
let lastPort = null;
256257
_async.timesSeries(count, function(index, asyncCallback) {
257258
if (lastPort) {
258259
options.port = exports.nextPort(lastPort);
@@ -355,7 +356,7 @@ exports.getSocket = function (options, callback) {
355356
// then test connection.
356357
//
357358
function checkAndTestSocket () {
358-
var dir = path.dirname(options.path);
359+
const dir = path.dirname(options.path);
359360

360361
fs.stat(dir, function (err, stats) {
361362
if (err || !stats.isDirectory()) {
@@ -394,11 +395,11 @@ exports.nextPort = function (port) {
394395
// specified `socketPath`.
395396
//
396397
exports.nextSocket = function (socketPath) {
397-
var dir = path.dirname(socketPath),
398-
name = path.basename(socketPath, '.sock'),
399-
match = name.match(/^([a-zA-z]+)(\d*)$/i),
400-
index = parseInt(match[2]),
401-
base = match[1];
398+
const dir = path.dirname(socketPath),
399+
name = path.basename(socketPath, '.sock'),
400+
match = name.match(/^([a-zA-z]+)(\d*)$/i),
401+
base = match[1];
402+
let index = parseInt(match[2]);
402403
if (isNaN(index)) {
403404
index = 0;
404405
}
@@ -484,7 +485,7 @@ exports.nextSocket = function (socketPath) {
484485
* Note we export this so we can use it in our tests, otherwise this API is private
485486
*/
486487
exports._defaultHosts = (function() {
487-
var interfaces = {};
488+
let interfaces = {};
488489
try{
489490
interfaces = os.networkInterfaces();
490491
}
@@ -506,13 +507,13 @@ exports._defaultHosts = (function() {
506507
}
507508
}
508509

509-
var interfaceNames = Object.keys(interfaces),
510-
hiddenButImportantHost = '0.0.0.0', // !important - dont remove, hence the naming :)
511-
results = [hiddenButImportantHost];
512-
for (var i = 0; i < interfaceNames.length; i++) {
513-
var _interface = interfaces[interfaceNames[i]];
514-
for (var j = 0; j < _interface.length; j++) {
515-
var curr = _interface[j];
510+
const interfaceNames = Object.keys(interfaces),
511+
hiddenButImportantHost = '0.0.0.0', // !important - dont remove, hence the naming :)
512+
results = [hiddenButImportantHost];
513+
for (let i = 0; i < interfaceNames.length; i++) {
514+
const _interface = interfaces[interfaceNames[i]];
515+
for (let j = 0; j < _interface.length; j++) {
516+
const curr = _interface[j];
516517
results.push(curr.address);
517518
}
518519
}

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 (cb) { cb(null, 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 (cb) { cb(null, 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)