Skip to content

Commit

Permalink
feat(web): add verbose logging to waitForPortOpen function (nrwl#20260)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Nov 21, 2023
1 parent 2354577 commit cd38d51
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/web/src/utils/wait-for-port-open.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { logger } from '@nx/devkit';
import * as net from 'net';

export function waitForPortOpen(
port: number,
options: { host?: string; retries?: number; retryDelay?: number } = {}
): Promise<void> {
const allowedErrorCodes = ['ECONNREFUSED', 'ECONNRESET'];
const host = options.host ?? '127.0.0.1';
const allowedErrorCodes = ['ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT'];

return new Promise((resolve, reject) => {
const checkPort = (retries = options.retries ?? 120) => {
Expand All @@ -23,6 +25,11 @@ export function waitForPortOpen(

client.once('error', (err) => {
if (retries === 0 || !allowedErrorCodes.includes(err['code'])) {
if (process.env['NX_VERBOSE_LOGGING'] === 'true') {
logger.info(
`Error connecting on ${host}:${port}: ${err['code'] || err}`
);
}
cleanupClient();
reject(err);
} else {
Expand All @@ -32,7 +39,10 @@ export function waitForPortOpen(

// Node will use IPv6 if it is available, but this can cause issues if the server is only listening on IPv4.
// Hard-coding to look on 127.0.0.1 to avoid using the IPv6 loopback address "::1".
client.connect({ port, host: options.host ?? '127.0.0.1' });
if (process.env['NX_VERBOSE_LOGGING'] === 'true') {
logger.info(`Connecting on ${host}:${port}`);
}
client.connect({ port, host });
};

checkPort();
Expand Down

0 comments on commit cd38d51

Please sign in to comment.