UNPKG

@tmlmobilidade/connectors

Version:

This package provides pre-made database connectors to streamline development and reduce boilerplate. By using these connectors, you can avoid re-implementing controller classes every time, ensuring consistency and saving development time.

56 lines (55 loc) 2.17 kB
import { type Server } from 'node:net'; import { type ForwardOptions, type ServerOptions, type SshOptions, type TunnelOptions } from 'tunnel-ssh'; export interface SshConfig { forwardOptions: ForwardOptions; serverOptions: ServerOptions; sshOptions: SshOptions; tunnelOptions: TunnelOptions; } export interface SshTunnelConnectorOptions { maxRetries?: number; } export declare class SshTunnelConnector { private static _instance; get server(): Server | undefined; private _server; private config; private options; private retries; constructor(config: SshConfig, options?: SshTunnelConnectorOptions); /** * Get the singleton instance of SshTunnelConnector. */ static getInstance(config?: SshConfig, options?: SshTunnelConnectorOptions): SshTunnelConnector; /** * Establishes an SSH tunnel connection using the provided configuration options. * * @throws {Error} Throws an error if the connection fails after the maximum number of retries. * * @remarks * - The method attempts to create an SSH tunnel using the `createTunnel` function with the specified options. * - If the connection is successful, it logs the connected host port and sets up an error listener on the server. * - If the connection fails, it retries the connection up to a maximum number of retries specified in the options. * * @example * ```typescript * const sshTunnelConnector = new SshTunnelConnector(config); * sshTunnelConnector.connect(); * ``` */ connect(): Promise<void>; /** * Disconnects the SSH tunnel by closing the server. * * @returns {Promise<void>} A promise that resolves when the server is successfully closed. * @throws Will log an error message if the server fails to close. */ disconnect(): Promise<void>; /** * Reconnects the SSH tunnel by first disconnecting and then connecting again. * This method ensures that the connection is reset. * * @returns {Promise<void>} A promise that resolves when the reconnection process is complete. */ reconnect(): Promise<void>; }