The connectSession function connects a client to a session by managing existing sessions, creating a new one if necessary, and configuring the client's connection retry timeout. It returns the client's session ID if successful, or throws an error if an issue occurs.
var importer = require('../Core');
var readSessions = importer.import("load webdriver sessions");
var {
    getSessions,
    lockPromise,
    updateOrAddSession
} = importer.import("manage webdriver sessions");
var TIMEOUT = 10000;
var MAX_SESSIONS = 4;
function connectSession(client) {
    var isError = false;
    return lockPromise(true, true)
        .then(() => getSessions(client, true))
        // save current session
        .then(validSessions => {
            isError = false;
            var sessions = readSessions();
            // the next null or end will be the next available profile id
            var index = sessions.map(s => s[1]).indexOf(validSessions[0] || 0);
            if(index === -1) {
                console.log('session not found ' + validSessions[0]);
                index = sessions.length;
            }
            if(index >= MAX_SESSIONS) {
                throw new Error('Already running max sessions ' + MAX_SESSIONS);
            }
            client.options.connectionRetryTimeout = TIMEOUT;
            //client.options.capabilities['goog:chromeOptions'].args[0] = 'user-data-dir=/tmp/profile-' + index;
            // TODO: fix this, doesn't work on second init, keeps opening new windows if chrome profile path is alreading open for read/write
            if(typeof validSessions[0] !== 'undefined') {
                console.log('using existing session ' + index + ' - ' + validSessions[0]);
                client.sessionId = validSessions[0];
            } else {
                console.log('new session ' + index);
            }
        })
        .then(() => client.status())
        .then(r => updateOrAddSession(client.sessionId))
        .catch(e => {
            console.log(e);
            client.sessionId = null;
            isError = e;
        })
        .then(() => lockPromise(false, true))
        .then(() => {
            if(isError)
                throw isError;
            return client.sessionId;
        })
}
module.exports = connectSession;
const { importCore, manageWebdriverSessions, loadWebdriverSessions } = require('../Core');
const {
  getSessions,
  lockPromise,
  updateOrAddSession,
} = importCore('manage webdriver sessions');
const { readSessions } = importCore('load webdriver sessions');
const TIMEOUT = 10 * 1000; // 10 seconds
const MAX_SESSIONS = 4;
/**
 * Connect to a Webdriver session.
 * @param {object} client - Webdriver client instance.
 * @returns {Promise<string>} The ID of the connected session, or throws an error if failed.
 */
function connectSession(client) {
  let isError = false;
  return lockPromise(true, true)
   .then(() => {
      const validSessions = getSessions(client, true);
      return validSessions;
    })
   .then((validSessions) => {
      const sessions = loadWebdriverSessions();
      const lastIndex = sessions.map((s) => s[1]).indexOf(validSessions[0] || 0);
      if (lastIndex === -1) {
        console.log(`Session not found: ${validSessions[0]}`);
        lastIndex = sessions.length;
      }
      if (lastIndex >= MAX_SESSIONS) {
        throw new Error(`Already running max sessions: ${MAX_SESSIONS}`);
      }
      // Set connection retry timeout
      client.options.connectionRetryTimeout = TIMEOUT;
      // TODO: Fix Chrome profile path conflict issue
      if (typeof validSessions[0]!== 'undefined') {
        console.log(`Using existing session ${lastIndex} - ${validSessions[0]}`);
        client.sessionId = validSessions[0];
      } else {
        console.log(`New session ${lastIndex}`);
      }
    })
   .then(() => client.status())
   .then((r) => updateOrAddSession(client.sessionId))
   .catch((e) => {
      console.log(e);
      client.sessionId = null;
      isError = e;
    })
   .then(() => lockPromise(false, true))
   .then(() => {
      if (isError) {
        throw isError;
      }
      return client.sessionId;
    });
}
module.exports = connectSession;Function Breakdown: connectSession
client: An object representing a client that needs to be connected to a session.importer: An imported module that provides functions for loading and managing webdriver sessions.readSessions: A function imported from importer that loads existing webdriver sessions.getSessions, lockPromise, updateOrAddSession: Functions imported from importer that manage webdriver sessions.TIMEOUT: A constant representing the connection retry timeout in milliseconds (10 seconds).MAX_SESSIONS: A constant representing the maximum number of allowed sessions (4).lockPromise to ensure exclusive access to session management.getSessions and updates the sessions array.MAX_SESSIONS).client.options.connectionRetryTimeout.client.status.updateOrAddSession.lockPromise.