From 7d8a85f01ebcf4cde6eda1450747b311398dee78 Mon Sep 17 00:00:00 2001 From: Juan Diego Moreno Upegui Date: Wed, 15 Oct 2025 15:19:34 -0500 Subject: [PATCH] feat: add setupConnection and setSession methods for session restoration --- src/client.ts | 27 +++++++++++++++++++++++++++ src/jsonrpc.ts | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/client.ts b/src/client.ts index 5a9eb94..a557ecf 100644 --- a/src/client.ts +++ b/src/client.ts @@ -158,6 +158,28 @@ export class TrytonClient { } } + /** + * Setup connection pool without authentication + * Used when restoring an existing session + * @returns {Promise} + */ + async setupConnection(): Promise { + this.connection = new ServerPool( + this.hostname, + this.port, + this.database, + { + session: undefined, // Session will be set by restoreSession() + cache: this.options.cache !== false ? [] : null, + verbose: this.options.verbose || false, + connectTimeout: this.options.connectTimeout || CONNECT_TIMEOUT, + timeout: this.options.timeout || DEFAULT_TIMEOUT, + keepMax: this.options.keepMax || 4, + useHttps: this.useHttps, + } + ); + } + /** * Call RPC method on server * @param {string} methodName - RPC method name (e.g., 'model.party.party.read') @@ -521,6 +543,11 @@ export class TrytonClient { // Establecer la sesión sin hacer login this.session = sessionToken; + // Actualizar la sesión en el connection pool + if (this.connection) { + this.connection.setSession(sessionToken); + } + // Verificar que la sesión sigue siendo válida const proxy = this.connection?.getConnection(); if (!proxy) { diff --git a/src/jsonrpc.ts b/src/jsonrpc.ts index 4186849..8975933 100644 --- a/src/jsonrpc.ts +++ b/src/jsonrpc.ts @@ -646,6 +646,23 @@ export class ServerPool { } } + /** + * Update session for all connections + * @param {string} session - New session string + */ + setSession(session: string): void { + this.session = session; + this.options.session = session; + + // Update session for all existing connections + for (const conn of this.pool) { + (conn as any).session = session; + } + for (const conn of this.used) { + (conn as any).session = session; + } + } + /** * Get connection from pool or create new one * @returns {ServerProxy} - Server proxy instance