new(all): #327 OPEN AS web automation.

This commit is contained in:
rodrigorodriguez 2023-02-16 18:12:21 -03:00
parent 649e08d7f2
commit de84283b5d

View file

@ -45,6 +45,7 @@ import Fs from 'fs';
import Path from 'path'; import Path from 'path';
import url from 'url'; import url from 'url';
import { Mutex, Semaphore, withTimeout } from 'async-mutex'; import { Mutex, Semaphore, withTimeout } from 'async-mutex';
import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
/** /**
* Web Automation services of conversation to be called by BASIC. * Web Automation services of conversation to be called by BASIC.
@ -86,8 +87,6 @@ export class WebAutomationServices {
*/ */
maxLines: number = 2000; maxLines: number = 2000;
pageMap = {};
public static cyrb53 = (str, seed = 0) => { public static cyrb53 = (str, seed = 0) => {
let h1 = 0xdeadbeef ^ seed, let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed; h2 = 0x41c6ce57 ^ seed;
@ -123,12 +122,25 @@ export class WebAutomationServices {
public async getPage({ pid, sessionKind, sessionName, url, username, password }) { public async getPage({ pid, sessionKind, sessionName, url, username, password }) {
GBLog.info(`BASIC: Web Automation GET PAGE ${sessionName ? sessionName : ''} ${url}.`); GBLog.info(`BASIC: Web Automation GET PAGE ${sessionName ? sessionName : ''} ${url}.`);
const { min, user } = await DialogKeywords.getProcessInfo(pid);
let handle;
// Try to find an existing handle.
let session = GBServer.globals.webSessions[sessionName];
let keys = Object.keys(GBServer.globals.webSessions);
for (let i = 0; i < keys.length; i++) {
const session = GBServer.globals.webSessions[keys[i]];
if (session.sessionName === sessionName) {
handle = keys[i];
break;
}
}
// Semaphore logic to block multiple entries on the same session. // Semaphore logic to block multiple entries on the same session.
let page; let page;
let session = GBServer.globals.webSessions[sessionName];
if (session) { if (session) {
const [value, release] = await session.semaphore.acquire(); const [value, release] = await session.semaphore.acquire();
try { try {
@ -139,40 +151,49 @@ export class WebAutomationServices {
} }
} }
// Creates the page if it is the first time.
let browser;
if (!page) {
browser = await createBrowser(null);
page = (await this.browser.pages())[0];
if (username || password) {
await page.authenticate({ pid, username: username, password: password });
}
}
// There is no session yet, // There is no session yet,
if (!session && sessionKind === 'AS') { if (!session && sessionKind === 'AS') {
// A new web session is being created. // A new web session is being created.
GBServer.globals.webSessions[sessionName] = {}; handle = WebAutomationServices.cyrb53(this.min.botId + url);
GBServer.globals.webSessions[sessionName].pid = pid; GBServer.globals.webSessions[handle] = {};
GBServer.globals.webSessions[sessionName].page = page; GBServer.globals.webSessions[handle].sessionName = sessionName;
GBServer.globals.webSessions[sessionName].semaphore = withTimeout( GBServer.globals.webSessions[handle].pid = pid;
GBServer.globals.webSessions[handle].page = page;
GBServer.globals.webSessions[handle].browser = browser;
GBServer.globals.webSessions[handle].semaphore = withTimeout(
new Semaphore(5), new Semaphore(5),
60 * 1000, 60 * 1000,
new Error('Error waiting for OPEN keyword.') new Error('Error waiting for OPEN keyword.')
); );
} }
if (url.startsWith('#') && sessionKind == 'WITH') { // WITH is only valid in a previously defined session.
} else {
if (!this.browser) { if (!session && sessionKind == 'WITH') {
this.browser = await createBrowser(null); GBLogEx.error(min, `NULL session for OPEN WITH #${sessionName}.`);
}
page = (await this.browser.pages())[0];
if (username || password) {
await page.authenticate({ pid, username: username, password: password });
}
} }
await page.goto(url); await page.goto(url);
const handle = WebAutomationServices.cyrb53(this.min.botId + url);
this.pageMap[handle] = page;
return handle; return handle;
} }
public getPageByHandle(hash) { public getPageByHandle(handle) {
return this.pageMap[hash]; return GBServer.globals.webSessions[handle].page;
} }
/** /**