portfinder
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -79,36 +79,39 @@ /* | ||
options.host = options.host || '::1'; | ||
var hosts = [options.host, '0.0.0.0', '127.0.0.1']; | ||
// remove duplicate host arg if already in hosts array | ||
for (var i = 1; i < hosts.length; i++) { | ||
if (options.host === hosts[i]) { | ||
hosts.shift(); | ||
break; | ||
} | ||
var hosts = ['::1', '127.0.0.1', '0.0.0.0']; | ||
if (options.host) { | ||
hosts.push(options.host); | ||
} | ||
// create array of eventual found ports for each host | ||
var testPortFunctions = []; | ||
for (var j = 0; j < hosts.length; j++) { | ||
(function() { | ||
var jj = j; | ||
var testPortFn = function(callback) { | ||
internals.testPort({ host: hosts[jj], port: options.port }, callback); | ||
}; | ||
function findCommonOpenPort(portToTest) { | ||
var ports = []; | ||
var error; | ||
async.everyLimit(hosts, 1, function(host, next) { | ||
internals.testPort({"host": host, "port": portToTest}, function(err, port) { | ||
if (err) { | ||
error = err; | ||
return next(false); | ||
} else { | ||
ports.push(port); | ||
next(port); | ||
} | ||
}); | ||
}, function(result) { | ||
if (!result) { | ||
return callback(error); | ||
} | ||
testPortFunctions.push(testPortFn); | ||
}()); | ||
ports.sort(function(a, b) { | ||
return a - b; | ||
}); | ||
if (ports[0] === ports[ports.length-1]) { | ||
callback(null, ports[0]); | ||
} else { | ||
findCommonOpenPort(ports.pop()); | ||
} | ||
}); | ||
} | ||
async.series(testPortFunctions, function(err, ports) { | ||
if (err) { return callback(err); } | ||
var port = ports.pop(); | ||
for (var i = 0; i < ports.length; i++) { | ||
if (port !== ports[i]) { return exports.getPort({ host: options.hosts, port: ports[i] }, callback); } | ||
} | ||
return callback(null, port); | ||
}); | ||
findCommonOpenPort(options.port); | ||
}; | ||
@@ -115,0 +118,0 @@ |
{ | ||
"name": "portfinder", | ||
"description": "A simple tool to find an open port on the current machine", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Charlie Robbins <charlie.robbins@gmail.com>", | ||
@@ -20,2 +20,3 @@ "repository": { | ||
"devDependencies": { | ||
"glob": "^6.0.4", | ||
"vows": "0.8.0" | ||
@@ -22,0 +23,0 @@ }, |
var async = require('async'), | ||
net = require('net'); | ||
http = require('http'); | ||
function createServer(base, host, next) { | ||
var server = net.createServer(function () {}); | ||
var server = http.createServer(function () {}); | ||
@@ -8,0 +8,0 @@ if (!next) { |
@@ -14,3 +14,5 @@ /* | ||
vows = require('vows'), | ||
portfinder = require('../lib/portfinder'); | ||
portfinder = require('../lib/portfinder'), | ||
fs = require('fs'), | ||
glob = require('glob'); | ||
@@ -28,5 +30,23 @@ var servers = [], | ||
var server = net.createServer(function () { }), | ||
name = base === 0 ? 'test.sock' : 'test' + base + '.sock'; | ||
name = base === 0 ? 'test.sock' : 'test' + base + '.sock', | ||
sock = path.join(socketDir, name); | ||
server.listen(path.join(socketDir, name), next); | ||
// shamelessly stolen from foreverjs, | ||
// https://github.com/foreverjs/forever/blob/6d143609dd3712a1cf1bc515d24ac6b9d32b2588/lib/forever/worker.js#L141-L154 | ||
if (process.platform === 'win32') { | ||
// | ||
// Create 'symbolic' file on the system, so it can be later | ||
// found via "forever list" since the `\\.pipe\\*` "files" can't | ||
// be enumerated because ... Windows. | ||
// | ||
fs.openSync(sock, 'w'); | ||
// | ||
// It needs the prefix, otherwise EACCESS error happens on Windows | ||
// (no .sock extension, only named pipes with .pipe prefixes) | ||
// | ||
sock = '\\\\.\\pipe\\' + sock; | ||
} | ||
server.listen(sock, next); | ||
base++; | ||
@@ -37,2 +57,11 @@ servers.push(server); | ||
function cleanup(callback) { | ||
fs.rmdirSync(badDir); | ||
glob(path.resolve(socketDir, '*'), function (err, files) { | ||
if (err) { callback(err); } | ||
for (var i = 0; i < files.length; i++) { fs.unlinkSync(files[i]); } | ||
callback(null, true); | ||
}); | ||
} | ||
vows.describe('portfinder').addBatch({ | ||
@@ -42,3 +71,7 @@ "When using portfinder module": { | ||
topic: function () { | ||
createServers(this.callback); | ||
createServers(function() { | ||
portfinder.getSocket({ | ||
path: path.join(badDir, 'test.sock') | ||
}, this.callback); | ||
}.bind(this)); | ||
}, | ||
@@ -64,8 +97,7 @@ "the getPort() method": { | ||
topic: function () { | ||
var that = this; | ||
exec('rm -rf ' + badDir, function () { | ||
fs.rmdir(badDir, function () { | ||
portfinder.getSocket({ | ||
path: path.join(badDir, 'test.sock') | ||
}, that.callback); | ||
}); | ||
}, this.callback); | ||
}.bind(this)); | ||
}, | ||
@@ -93,6 +125,10 @@ "should respond with the first free socket (test.sock)": function (err, socket) { | ||
"When the tests are over": { | ||
"necessary cleanup should take place": function () { | ||
exec('rm -rf ' + badDir + ' ' + path.join(socketDir, '*'), function () { }); | ||
topic: function() { | ||
cleanup(this.callback); | ||
}, | ||
"necessary cleanup should have taken place": function (err, wasRun) { | ||
assert.isTrue(!err); | ||
assert.isTrue(wasRun); | ||
} | ||
} | ||
}).export(module); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19024
14
535
2
2
6