New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

wait-port

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wait-port - npm Package Compare versions

Comparing version 0.3.1 to 1.0.0

7

index.d.ts

@@ -33,4 +33,9 @@ interface ServerLocation {

declare const waitPort: (server: ServerLocation) => Promise<boolean>;
interface ReturnObject {
open: boolean;
ipVersion?: 4 | 6;
}
declare const waitPort: (server: ServerLocation) => Promise<ReturnObject>;
export = waitPort;

23

lib/wait-port.js

@@ -7,3 +7,3 @@ const debug = require('debug')('wait-port');

function createConnectionWithTimeout({ host, port }, timeout, callback) {
function createConnectionWithTimeout({ host, port, ipVersion }, timeout, callback) {
// Variable to hold the timer we'll use to kill the socket if we don't

@@ -14,3 +14,3 @@ // connect in time.

// Try and open the socket, with the params and callback.
const socket = net.createConnection({ host, port }, (err) => {
const socket = net.createConnection({ host, port, family: ipVersion }, (err) => {
if (!err) clearTimeout(timer);

@@ -31,3 +31,3 @@ return callback(err);

socket.destroy();
const error = new Error(`Timeout trying to open socket to ${host}:${port}`);
const error = new Error(`Timeout trying to open socket to ${host}:${port}, IPv${ipVersion}`);
error.code = 'ECONNTIMEOUT';

@@ -48,3 +48,3 @@ callback(error);

socket.destroy();
const error = new Error(`Timeout waiting for data from ${params.host}:${params.port}`);
const error = new Error(`Timeout waiting for data from ${params.host}:${params.port}, IPv${params.ipVersion}`);
error.code = 'EREQTIMEOUT';

@@ -129,3 +129,3 @@ callback(error);

}
// Boom, we connected!

@@ -197,5 +197,5 @@ debug('Socket connected!');

// Start trying to connect.
const loop = () => {
const loop = (ipVersion = 4) => {
outputFunction.tryConnect();
tryConnect({ protocol, host, port, path, waitForDns }, connectTimeout)
tryConnect({ protocol, host, port, path, waitForDns, ipVersion }, connectTimeout)
.then((open) => {

@@ -207,3 +207,3 @@ debug(`Socket status is: ${open}`);

outputFunction.connected();
return resolve(true);
return resolve({ open: true, ipVersion });
}

@@ -214,5 +214,10 @@

outputFunction.timeout();
return resolve(false);
return resolve({ open: false });
}
// Check for IPv6 next
if (ipVersion === 4 && !net.isIP(host)) {
return loop(6);
}
// Run the loop again.

@@ -219,0 +224,0 @@ return setTimeout(loop, interval);

@@ -8,3 +8,3 @@ const assert = require('assert');

it('should wait until a port is open', () => {
it('should wait until a port is open IPv4', () => {

@@ -17,8 +17,53 @@ const server = net.createServer();

return waitPort({ host: '127.0.0.1', port: 9021, output: 'silent' })
.then((open) => {
.then(({ open, ipVersion }) => {
server.close();
assert(open === true, 'Waiting for the port should find it to open.');
assert(ipVersion === 4, 'Waiting for the port should find it on IPv4.');
});
});
it('should wait until a port is open IPv6', () => {
const server = net.createServer();
server.listen(9021, '::1');
// Start waiting for port 9021 to open. If it opens we pass, otherwise we
// fail.
return waitPort({ host: '::1', port: 9021, output: 'silent' })
.then(({ open, ipVersion }) => {
server.close();
assert(open === true, 'Waiting for the port should find it to open.');
assert(ipVersion === 4, 'Waiting for the port should find it on IPv4.');
});
});
it('should wait until a port is open IPv4 with localhost', () => {
const server = net.createServer();
server.listen(9021, '127.0.0.1');
// Start waiting for port 9021 to open. If it opens we pass, otherwise we
// fail.
return waitPort({ host: 'localhost', port: 9021, output: 'silent' })
.then(({ open, ipVersion }) => {
server.close();
assert(open === true, 'Waiting for the port should find it to open.');
assert(ipVersion === 4, 'Waiting for the port should find it on IPv4.');
});
});
it('should wait until a port is open IPv6 with localhost', () => {
const server = net.createServer();
setTimeout( ()=> {
server.listen(9021, '::1');
}, 1000);
// Start waiting for port 9021 to open. If it opens we pass, otherwise we
// fail.
return waitPort({ host: 'localhost', port: 9021, output: 'silent' })
.then(({ open, ipVersion }) => {
server.close();
assert(open === true, 'Waiting for the port should find it to open.');
assert(ipVersion === 6, 'Waiting for the port should find it on IPv4.');
});
});
it('should timeout after the specified time', () => {

@@ -31,5 +76,5 @@ const timeout = 5000;

return waitPort({ host: '127.0.0.1', port: 9021, timeout, output: 'silent' })
.then((open) => {
.then(({ open }) => {
assert(open === false, 'The port should not be open.');
// Make sure we are close to the timeout.

@@ -41,6 +86,6 @@ const elapsed = new Date() - start;

});
it('should timeout after the specified time even with a non-routable address', () => {
return waitPort({ host: '10.255.255.1', port: 9021, timeout: 500, output: 'silent' })
.then((open) => {
.then(({ open }) => {
assert(open === false, 'The port should not be open.');

@@ -56,3 +101,3 @@ });

return waitPort({ protocol: 'http', host: '127.0.0.1', port: 9021, timeout: 500, output: 'silent' })
.then((open) => {
.then(({ open }) => {
server.close();

@@ -71,3 +116,3 @@ assert(open === false, 'The port should not be open for http.');

return waitPort({ protocol: 'http', host: '127.0.0.1', port: 9022, timeout: 3000, output: 'silent' })
.then((open) => {
.then(({ open }) => {
server.close();

@@ -99,5 +144,5 @@ assert(open === false, 'The success condition should not be met');

return waitPort({ host: 'ireallyhopethatthisdomainnamedoesnotexist.com', waitForDns: true, port: 9021, timeout, output: 'silent' })
.then((open) => {
.then(({ open }) => {
assert(open === false, 'The port should not be open.');
// Make sure we are close to the timeout.

@@ -109,4 +154,4 @@ const elapsed = new Date() - start;

});
it('should successfully wait for a valid http response', () => {

@@ -120,7 +165,8 @@ const server = http.createServer((req, res) => {

return waitPort({ protocol: 'http', host: '127.0.0.1', port: 9023, timeout: 3000, output: 'silent' })
.then((open) => {
.then(({ open, ipVersion }) => {
server.close();
assert(open === true, 'The success condition should be met');
assert(ipVersion === 4, 'It should be open on IPv4');
});
});
});
{
"name": "wait-port",
"version": "0.3.1",
"version": "1.0.0",
"description": "Utility to wait for a TCP port to open.",

@@ -5,0 +5,0 @@ "main": "./lib/wait-port.js",

@@ -105,4 +105,4 @@ # wait-port

waitPort(params)
.then((open) => {
if (open) console.log('The port is now open!');
.then(({ open, ipVersion }) => {
if (open) console.log(`The port is now open on IPv${ipVersion}!`);
else console.log('The port did not open before the timeout...');

@@ -109,0 +109,0 @@ })

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