From fa63f4d72bf851974e0235481250b1dbe2ef23d6 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Sat, 1 Jun 2024 10:55:04 +0530 Subject: [PATCH] feat: initiallize RemoteBrowser class --- .../classes/RemoteBrowser.ts | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 server/src/browser-management/classes/RemoteBrowser.ts diff --git a/server/src/browser-management/classes/RemoteBrowser.ts b/server/src/browser-management/classes/RemoteBrowser.ts new file mode 100644 index 00000000..707bcebd --- /dev/null +++ b/server/src/browser-management/classes/RemoteBrowser.ts @@ -0,0 +1,80 @@ +import { + Page, + Browser, + CDPSession, + BrowserContext, +} from 'playwright'; +import { Socket } from "socket.io"; + +import logger from '../../logger'; +import { InterpreterSettings, RemoteBrowserOptions } from "../../types"; +import { WorkflowGenerator } from "../../workflow-management/classes/Generator"; +import { WorkflowInterpreter } from "../../workflow-management/classes/Interpreter"; + +/** + * This class represents a remote browser instance. + * It is used to allow a variety of interaction with the Playwright's browser instance. + * Every remote browser holds an instance of a generator and interpreter classes with + * the purpose of generating and interpreting workflows. + * @category BrowserManagement + */ +export class RemoteBrowser { + + /** + * Playwright's [browser](https://playwright.dev/docs/api/class-browser) instance. + * @private + */ + private browser: Browser | null = null; + + /** + * The Playwright's [CDPSession](https://playwright.dev/docs/api/class-cdpsession) instance, + * used to talk raw Chrome Devtools Protocol. + * @private + */ + private client : CDPSession | null | undefined = null; + + /** + * Socket.io socket instance enabling communication with the client (frontend) side. + * @private + */ + private socket : Socket; + + /** + * The Playwright's [Page](https://playwright.dev/docs/api/class-page) instance + * as current interactive remote browser's page. + * @private + */ + private currentPage : Page | null | undefined = null; + + /** + * Interpreter settings for any started interpretation. + * @private + */ + private interpreterSettings: InterpreterSettings = { + debug: false, + maxConcurrency: 1, + maxRepeats: 1, + }; + + /** + * {@link WorkflowGenerator} instance specific to the remote browser. + */ + public generator: WorkflowGenerator; + + /** + * {@link WorkflowInterpreter} instance specific to the remote browser. + */ + public interpreter: WorkflowInterpreter; + + /** + * Initializes a new instances of the {@link Generator} and {@link WorkflowInterpreter} classes and + * assigns the socket instance everywhere. + * @param socket socket.io socket instance used to communicate with the client side + * @constructor + */ + public constructor(socket: Socket) { + this.socket = socket; + this.interpreter = new WorkflowInterpreter(socket); + this.generator = new WorkflowGenerator(socket); + } +}