Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

freeport-async

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

freeport-async - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

62

index.js
var net = require('net');
module.exports = function (rangeStart) {
var DEFAULT_PORT_RANGE_START = 11000;
function testPortAsync(port) {
return new Promise(function (fulfill, reject) {
var portrange = rangeStart || 11000;
function getPort (cb) {
var port = portrange
portrange += 1
var server = net.createServer()
server.listen(port, function (err) {
server.once('close', function () {
fulfill(true);
});
server.close();
});
server.on('error', function (err) {
fulfill(false);
});
});
}
var server = net.createServer()
server.listen(port, function (err) {
server.once('close', function () {
cb(port)
})
server.close()
})
server.on('error', function (err) {
getPort(cb)
})
function freePortRangeAsync(rangeSize, rangeStart) {
rangeSize = rangeSize || 1;
return new Promise(function (fulfill, reject) {
var lowPort = rangeStart || DEFAULT_PORT_RANGE_START;
var awaitables = [];
for (var i = 0; i < rangeSize; i++) {
awaitables.push(testPortAsync(lowPort + i));
}
getPort(fulfill);
return Promise.all(awaitables).then(function (results) {
var ports = [];
for (var i = 0; i < results.length; i++) {
if (!results[i]) {
return freePortRangeAsync(rangeSize, lowPort + rangeSize).then(fulfill, reject);
}
ports.push(lowPort + i);
}
fulfill(ports);
});
});
};
}
function freePortAsync(rangeStart) {
return freePortRangeAsync(1, rangeStart).then(function (result) {
return result[0];
});
}
module.exports = freePortAsync;
module.exports.availableAsync = testPortAsync;
module.exports.rangeAsync = freePortRangeAsync;
{
"name": "freeport-async",
"version": "1.0.0",
"version": "1.1.0",
"description": "Uses mikeal's code to find an open port in a given range",

@@ -5,0 +5,0 @@ "main": "index.js",

# freeport
Uses mikeal's code to find an open port in a given range,
and presents a Promise-based interface
Finds an available port for your application to use.
You can specify a range where to look for an available port.
And can also find a range of available ports for you to use.
You can also be used to test to see if a given port is available.
All functions are async and return Promises.
Usage:

@@ -14,5 +19,11 @@ ```js

var isPort5000Available = await freeportAsync.availableAsync(5000);
var listOf5ConsecutiveAvailablePorts = await freeportAsync.rangeAsync(5);
var freeRangeIn12000Range = await freeportAsync.rangeAsync(3, 12000);
```
See https://gist.github.com/mikeal/1840641
See also https://gist.github.com/mikeal/1840641
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc