From 1ccc1bfc1a797ce16ff5636010f1c8a6e15f2c9c Mon Sep 17 00:00:00 2001 From: Andres Correa Casablanca Date: Tue, 7 Feb 2023 16:24:21 +0100 Subject: [PATCH] fix: set undefined variables Signed-off-by: Andres Correa Casablanca --- src/index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0d3e553..f8f8671 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,18 +83,21 @@ export interface Options { function connect(connectionConfig: pg.ClientConfig | undefined, emitter: TypedEventEmitter, options: Options) { connectionLogger("Creating PostgreSQL client for notification streaming") - const { retryInterval = 500, retryLimit = Infinity, retryTimeout = 3000 } = options const effectiveConnectionConfig: pg.ClientConfig = { ...connectionConfig, keepAlive: true } const Client = options.native && pg.native ? pg.native.Client : pg.Client const dbClient = new Client(effectiveConnectionConfig) - const getRetryInterval = typeof retryInterval === "function" ? retryInterval : () => retryInterval + const retryInterval = options?.retryInterval + const getRetryInterval = typeof retryInterval === "function" + ? retryInterval + : () => retryInterval as number | undefined ?? 0 const reconnect = async (onAttempt: (attempt: number) => void): Promise => { connectionLogger("Reconnecting to PostgreSQL for notification streaming") const startTime = Date.now() - for (let attempt = 1; attempt < retryLimit || !retryLimit; attempt++) { + const retryLimit = options?.retryLimit + for (let attempt = 1; !retryLimit || attempt < retryLimit; attempt++) { connectionLogger(`PostgreSQL reconnection attempt #${attempt}...`) onAttempt(attempt) @@ -115,6 +118,7 @@ function connect(connectionConfig: pg.ClientConfig | undefined, emitter: TypedEv connectionLogger("PostgreSQL reconnection attempt failed:", error) await delay(getRetryInterval(attempt - 1)) + const retryTimeout = options?.retryTimeout if (retryTimeout && (Date.now() - startTime) > retryTimeout) { throw new Error(`Stopping PostgreSQL reconnection attempts after ${retryTimeout}ms timeout has been reached.`) }