From bc7860c6f821f1363c7eed2affb9268c36b7e066 Mon Sep 17 00:00:00 2001 From: Hasan Haghniya Date: Fri, 21 Jun 2024 15:32:40 +0330 Subject: [PATCH] fix: safely parse environment variables to prevent NaN values --- src/pulsar-client.config.ts | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/pulsar-client.config.ts b/src/pulsar-client.config.ts index dbeaf90..a632248 100644 --- a/src/pulsar-client.config.ts +++ b/src/pulsar-client.config.ts @@ -43,17 +43,28 @@ function getAuthentication(): } } +function safeParseInt(value: string | undefined, fallback: number): number { + const parsed = parseInt(value || '', 10); + return isNaN(parsed) ? fallback : parsed; +} + export default (): { pulsar: ClientConfig } => ({ pulsar: { serviceUrl: process.env.PULSAR_SERVICE_URL || 'pulsar://localhost:6650', authentication: getAuthentication(), - operationTimeoutSeconds: - parseInt(process.env.PULSAR_OPERATION_TIMEOUT_SECONDS, 10) || 30, - ioThreads: parseInt(process.env.PULSAR_IO_THREADS, 10) || 1, - messageListenerThreads: - parseInt(process.env.PULSAR_MESSAGE_LISTENER_THREADS, 10) || 1, - concurrentLookupRequest: - parseInt(process.env.PULSAR_CONCURRENT_LOOKUP_REQUEST, 10) || undefined, + operationTimeoutSeconds: safeParseInt( + process.env.PULSAR_OPERATION_TIMEOUT_SECONDS, + 30, + ), + ioThreads: safeParseInt(process.env.PULSAR_IO_THREADS, 1), + messageListenerThreads: safeParseInt( + process.env.PULSAR_MESSAGE_LISTENER_THREADS, + 1, + ), + concurrentLookupRequest: safeParseInt( + process.env.PULSAR_CONCURRENT_LOOKUP_REQUEST, + undefined, + ), useTls: process.env.PULSAR_USE_TLS === 'true' || undefined, tlsTrustCertsFilePath: process.env.PULSAR_TLS_TRUST_CERTS_FILE_PATH || undefined, @@ -61,8 +72,10 @@ export default (): { pulsar: ClientConfig } => ({ process.env.PULSAR_TLS_VALIDATE_HOSTNAME === 'true' || undefined, tlsAllowInsecureConnection: process.env.PULSAR_TLS_ALLOW_INSECURE_CONNECTION === 'true' || undefined, - statsIntervalInSeconds: - parseInt(process.env.PULSAR_STATS_INTERVAL_IN_SECONDS, 10) || undefined, + statsIntervalInSeconds: safeParseInt( + process.env.PULSAR_STATS_INTERVAL_IN_SECONDS, + undefined, + ), listenerName: process.env.PULSAR_LISTENER_NAME || undefined, }, });