Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
detect-port-alt
Advanced tools
The detect-port-alt npm package is a utility that helps in checking if a port is already in use and if so, it can suggest an alternative port that is free. This is particularly useful in network programming and development environments where handling port conflicts efficiently is crucial.
Check if a port is in use
This feature allows you to check if a specific port (e.g., 3000) is available. If the port is occupied, it suggests another free port.
const detectPort = require('detect-port-alt');
detectPort(3000, (err, port) => {
if (err) {
console.error(err);
} else if (port === 3000) {
console.log('Port 3000 is available');
} else {
console.log('Port 3000 is occupied. Try port:', port);
}
});
Promise-based port detection
This feature provides a promise-based API to check for port availability, making it easier to integrate with modern JavaScript applications using async/await.
const detectPort = require('detect-port-alt');
async function getAvailablePort(startingPort) {
try {
const port = await detectPort(startingPort);
console.log(`Port ${port} is available`);
return port;
} catch (error) {
console.error('Error:', error);
}
}
getAvailablePort(3000);
Portfinder is a popular npm package that also helps in finding an open port on the current machine. Unlike detect-port-alt, portfinder provides more configuration options such as setting a range of ports to check. This can be particularly useful when you need to find multiple free ports within a specific range.
Get-port is another similar package that provides an easy-to-use API to get an available port. It's very minimalistic compared to detect-port-alt and does not provide callback support, focusing solely on promises, which makes it suitable for use in applications fully based on modern JavaScript features.
JavaScript Implementation of Port Detector
$ npm i detect-port --save
const detect = require('detect-port');
/**
* callback usage
*/
detect(port, (err, _port) => {
if (err) {
console.log(err);
}
if (port == _port) {
console.log(`port: ${port} was not occupied`);
} else {
console.log(`port: ${port} was occupied, try port: ${_port}`);
}
});
/**
* for a yield syntax instead of callback function implement
*/
const co = require('co');
co(function *() {
const _port = yield detect(port);
if (port == _port) {
console.log(`port: ${port} was not occupied`);
} else {
console.log(`port: ${port} was occupied, try port: ${_port}`);
}
});
/**
* use as a promise
*/
detect(port)
.then(_port => {
if (port == _port) {
console.log(`port: ${port} was not occupied`);
} else {
console.log(`port: ${port} was occupied, try port: ${_port}`);
}
})
.catch(err => {
console.log(err);
});
$ npm i detect-port -g
# get an available port randomly
$ detect
# detect pointed port
$ detect 80
# more help
$ detect --help
FAQs
detect available port
The npm package detect-port-alt receives a total of 3,347,228 weekly downloads. As such, detect-port-alt popularity was classified as popular.
We found that detect-port-alt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.