fix(basic.gblib): Upgrade to https://github.com/vasyas/push-rpc from WS to HTTPS.

This commit is contained in:
rodrigorodriguez 2023-03-05 16:59:48 -03:00
parent f3c756b015
commit 5534b416da
5 changed files with 61 additions and 55 deletions

View file

@ -113,17 +113,13 @@ export class GBBasicPackage implements IGBPackage {
listeners: {
unsubscribed(subscriptions: number): void {},
subscribed(subscriptions: number): void {},
disconnected(remoteId: string, connections: number): void {
GBLogEx.info(min, `[GBAPI]: New client ${remoteId} disconnected`);
},
connected(remoteId: string, connections: number): void {
GBLogEx.info(min, `[GBAPI]: New client ${remoteId} connected`);
},
disconnected(remoteId: string, connections: number): void {},
connected(remoteId: string, connections: number): void {},
messageIn(...params): void {
GBLogEx.info(min, '[GBAPI]: IN ' + params);
GBLogEx.info(min, 'API IN' + params);
},
messageOut(...params): void {
GBLogEx.info(min, '[GBAPI]: OUT ' + params);
GBLogEx.info(min, 'API OUT ' + params);
}
}
};
@ -151,7 +147,7 @@ export class GBBasicPackage implements IGBPackage {
opts
);
GBLogEx.info(min, '[GBAPI] RPC HTTP Server started at http://localhost:' + GBVMService.API_PORT);
GBLogEx.info(min, 'API RPC HTTP Server started at http://localhost:' + GBVMService.API_PORT);
GBServer.globals.debuggers[botId] = {};
GBServer.globals.debuggers[botId].state = 0;

View file

@ -288,7 +288,7 @@ export class DialogKeywords {
* @example days = DATEDIFF date1,date2,mode
*
*/
public getDateDiff(date1, date2, mode) {
public async dateDiff(date1, date2, mode) {
let dt1 = date1;
let dt2 = date2;
if (!(dt1 instanceof Date)) {

View file

@ -159,7 +159,7 @@ export class KeywordsExpressions {
}
const params = this.getParams($1, ['url', 'username', 'password']);
return `page = await wa.getPage({pid: pid, sessionKind: ${kind}, sessionName: ${sessionName}, ${params}})`;
return `page = await wa.getPage({pid: pid, page: page, sessionKind: ${kind}, sessionName: ${sessionName}, ${params}})`;
}
];
@ -661,8 +661,8 @@ export class KeywordsExpressions {
($0, $1, $2, $3) => {
// page is not string.
// https://github.com/GeneralBots/BotServer/issues/310
const params = this.getParams('page,' + $3, ['handle', 'frameOrSelector', 'selector']);
return `await wa.getClick ({pid: pid, ${params}})`;
const params = this.getParams($3, ['handle', 'frameOrSelector', 'selector']);
return `await wa.click ({pid: pid, ${params}})`;
}
];
@ -771,7 +771,7 @@ export class KeywordsExpressions {
/^\s*set\s*(.*)/gim,
($0, $1, $2) => {
const params = this.getParams($1, ['file', 'address', 'value']);
return `await sys.getSet ({pid: pid, handle:page, ${params}})`;
return `await sys.set ({pid: pid, handle: page, ${params}})`;
}
];
keywords[i++] = [

View file

@ -477,10 +477,11 @@ export class SystemKeywords {
// Handles calls for HTML stuff
if (file._javascriptEnabled) {
if (handle) {
GBLog.info(`BASIC: Web automation setting ${file}' to '${value}' (SET). `);
await new WebAutomationServices().setElementText({ pid, handle, selector: address, text: value });
GBLog.info(`BASIC: Web automation SET ${file}' to '${address}' . `);
await new WebAutomationServices()
.setElementText({ pid, handle, selector: file, text: address });
return;
}

View file

@ -40,7 +40,7 @@ import url from 'url';
import { GBLog, GBMinInstance } from 'botlib';
import { GBServer } from '../../../src/app.js';
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
import { GBSSR }from '../../core.gbapp/services/GBSSR.js';
import { GBSSR } from '../../core.gbapp/services/GBSSR.js';
import { GuaribasUser } from '../../security.gbapp/models/index.js';
import { DialogKeywords } from './DialogKeywords.js';
import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js';
@ -52,7 +52,6 @@ import { SystemKeywords } from './SystemKeywords.js';
* Web Automation services of conversation to be called by BASIC.
*/
export class WebAutomationServices {
/**
* The number used in this execution for HEAR calls (useful for SET SCHEDULE).
*/
@ -81,7 +80,6 @@ export class WebAutomationServices {
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};
public async getCloseHandles({ pid }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
// Releases previous allocated OPEN semaphores.
@ -102,37 +100,44 @@ export class WebAutomationServices {
* @example OPEN "https://wikipedia.org"
*/
public async getPage({ pid, sessionKind, sessionName, url, username, password }) {
GBLog.info(`BASIC: Web Automation GET PAGE ${sessionName ? sessionName : ''} ${url}.`);
public async getPage({ pid, handle, sessionKind, sessionName, url, username, password }) {
GBLog.info(`BASIC: Web Automation OPEN ${sessionName ? sessionName : ''} ${url}.`);
const { min, user } = await DialogKeywords.getProcessInfo(pid);
let handle;
// Try to find an existing handle.
let session;
let keys = Object.keys(GBServer.globals.webSessions);
for (let i = 0; i < keys.length; i++) {
if (GBServer.globals.webSessions[keys[i]].sessionName === sessionName) {
session = GBServer.globals.webSessions[keys[i]];
handle = keys[i];
break;
}
if (handle)
{
session = GBServer.globals.webSessions[handle];
}
// Semaphore logic to block multiple entries on the same session.
else if (sessionName) {
let keys = Object.keys(GBServer.globals.webSessions);
for (let i = 0; i < keys.length; i++) {
if (GBServer.globals.webSessions[keys[i]].sessionName === sessionName) {
session = GBServer.globals.webSessions[keys[i]];
handle = keys[i];
break;
}
}
}
let page;
if (session) {
GBLogEx.info(min, `Acquiring (1) for PID: ${pid}...`);
const release = await session.semaphore.acquire();
GBLogEx.info(min, `Acquire (1) for PID: ${pid} done.`);
try {
session.activePid = pid;
session.release = release;
page = session.page;
} catch {
release();
page = session.page;
// Semaphore logic to block multiple entries on the same session.
if (sessionName) {
GBLogEx.info(min, `Acquiring (1) for PID: ${pid}...`);
const release = await session.semaphore.acquire();
GBLogEx.info(min, `Acquire (1) for PID: ${pid} done.`);
try {
session.activePid = pid;
session.release = release;
} catch {
release();
}
}
}
@ -147,25 +152,30 @@ export class WebAutomationServices {
}
}
// There is no session yet.
if (!session && sessionKind === 'AS') {
// There is no session yet or it is an unamed session.
if ((!session && sessionKind === 'AS') || !sessionName) {
// A new web session is being created.
handle = WebAutomationServices.cyrb53(min.botId + url);
GBServer.globals.webSessions[handle] = session = {};
session = {};
session.sessionName = sessionName;
session.page = page;
session.browser = browser;
session.semaphore = new Mutex();
GBLogEx.info(min, `Acquiring (2) for PID: ${pid}...`);
const release = await session.semaphore.acquire();
GBLogEx.info(min, `Acquire (2) for PID: ${pid} done.`);
session.release = release;
session.activePid = pid;
GBServer.globals.webSessions[handle] = session;
// Only uses semaphore logic in named web sessions.
if (sessionName) {
GBLogEx.info(min, `Acquiring (2) for PID: ${pid}...`);
const release = await session.semaphore.acquire();
session.release = release;
GBLogEx.info(min, `Acquire (2) for PID: ${pid} done.`);
}
}
// WITH is only valid in a previously defined session.
@ -245,7 +255,7 @@ export class WebAutomationServices {
*
* @example CLICK page,"#idElement"
*/
public async getClick({ pid, handle, frameOrSelector, selector }) {
public async click({ pid, handle, frameOrSelector, selector }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation CLICK element: ${frameOrSelector}.`);
if (selector) {
@ -315,7 +325,7 @@ export class WebAutomationServices {
*
* @example file = SCREENSHOT page
*/
public async screenshot({pid, handle, selector }) {
public async screenshot({ pid, handle, selector }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation SCREENSHOT ${selector}.`);
@ -432,7 +442,7 @@ export class WebAutomationServices {
return file;
}
private async recursiveFindInFrames (inputFrame, selector) {
private async recursiveFindInFrames(inputFrame, selector) {
const frames = inputFrame.childFrames();
const results = await Promise.all(
frames.map(async frame => {
@ -446,5 +456,4 @@ export class WebAutomationServices {
);
return results.find(Boolean);
}
}