detect-port
Advanced tools
Comparing version
@@ -0,1 +1,6 @@ | ||
0.1.4 / 2015-08-24 | ||
=================== | ||
* Support promise | ||
0.1.2 / 2014-05-31 | ||
@@ -2,0 +7,0 @@ =================== |
@@ -29,53 +29,55 @@ /* ================================================================ | ||
var _detect = function(_fn) { | ||
var socket = new net.Socket(); | ||
socket.once('error', function() { | ||
socket.removeAllListeners('connect'); | ||
socket.removeAllListeners('error'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
var server = new net.Server(); | ||
server.on('error', function() { | ||
var _detect = function(port) { | ||
return new Promise(function(resolve, reject) { | ||
var socket = new net.Socket(); | ||
socket.once('error', function() { | ||
socket.removeAllListeners('connect'); | ||
socket.removeAllListeners('error'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
var server = new net.Server(); | ||
server.on('error', function() { | ||
inject(port); | ||
port++; | ||
resolve(_detect(port)); | ||
}); | ||
server.listen(port, function() { | ||
server.once('close', function() { | ||
resolve(port); | ||
}); | ||
server.close(); | ||
}); | ||
}); | ||
socket.once('connect', function() { | ||
inject(port); | ||
port++; | ||
if (fn) { | ||
_detect(_fn); | ||
} else { | ||
detect(port)(_fn); | ||
} | ||
resolve(_detect(port)); | ||
socket.removeAllListeners('connect'); | ||
socket.removeAllListeners('error'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
}); | ||
server.listen(port, function() { | ||
server.once('close', function() { | ||
_fn(null, port); | ||
}); | ||
server.close(); | ||
socket.connect({ | ||
port: port, | ||
host: ipv4 | ||
}); | ||
}); | ||
socket.once('connect', function() { | ||
inject(port); | ||
port++; | ||
} | ||
if (fn) { | ||
_detect(_fn); | ||
} else { | ||
detect(port)(_fn); | ||
} | ||
socket.removeAllListeners('connect'); | ||
socket.removeAllListeners('error'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
}); | ||
socket.connect({ | ||
port: port, | ||
host: ipv4 | ||
}); | ||
var _detect_with_cb = function(_fn) { | ||
_detect(port) | ||
.then(function(result) { | ||
_fn(null, result); | ||
}) | ||
.catch(function(e) { | ||
_fn(e); | ||
}); | ||
}; | ||
return fn ? _detect(fn) : _detect; | ||
return fn ? _detect_with_cb(fn) : _detect(port); | ||
} | ||
module.exports = detect; |
{ | ||
"name": "detect-port", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "port detector", | ||
@@ -38,3 +38,3 @@ "keywords": [ | ||
"engines": { | ||
"node": ">= 11.14.x" | ||
"node": ">= 0.11.14" | ||
}, | ||
@@ -41,0 +41,0 @@ "author": "xudafeng", |
@@ -83,2 +83,16 @@ detect-port | ||
/** | ||
* use as a promise | ||
*/ | ||
var promisePort = detect(port); | ||
promisePort.then(function(_port) { | ||
if (port === _port) { | ||
console.log('port: %d was not occupied', port); | ||
} else { | ||
console.log('port: %d was occupied, try port: %d', port, _port); | ||
} | ||
}); | ||
``` | ||
@@ -85,0 +99,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
9775
5.62%86
3.61%120
13.21%2
100%