Files
parcer/server/src/routes/record.ts

115 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-06-08 23:18:20 +05:30
/**
* RESTful API endpoints handling remote browser recording sessions.
*/
import { Router } from 'express';
import {
initializeRemoteBrowserForRecording,
destroyRemoteBrowser,
getActiveBrowserId,
interpretWholeWorkflow,
stopRunningInterpretation,
getRemoteBrowserCurrentUrl, getRemoteBrowserCurrentTabs,
} from '../browser-management/controller'
import { chromium } from 'playwright-extra';
2024-07-19 13:38:59 +05:30
import stealthPlugin from 'puppeteer-extra-plugin-stealth';
2024-06-08 23:18:20 +05:30
import logger from "../logger";
export const router = Router();
2024-07-19 13:38:59 +05:30
chromium.use(stealthPlugin());
2024-06-08 23:18:20 +05:30
/**
* Logs information about remote browser recording session.
*/
router.all('/', (req, res, next) => {
2024-06-09 00:40:13 +05:30
logger.log('debug', `The record API was invoked: ${req.url}`)
next() // pass control to the next handler
})
2024-06-08 23:19:31 +05:30
/**
* GET endpoint for starting the remote browser recording session.
* returns session's id
*/
router.get('/start', (req, res) => {
const id = initializeRemoteBrowserForRecording({
browser: chromium,
launchOptions: {
headless: true,
}
});
return res.send(id);
});
2024-06-09 00:36:36 +05:30
/**
* POST endpoint for starting the remote browser recording session accepting browser launch options.
* returns session's id
*/
2024-06-09 00:36:12 +05:30
router.post('/start', (req, res) => {
const id = initializeRemoteBrowserForRecording({
browser: chromium,
launchOptions: req.body,
});
return res.send(id);
});
2024-06-09 00:37:11 +05:30
/**
* GET endpoint for terminating the remote browser recording session.
* returns whether the termination was successful
*/
router.get('/stop/:browserId', async (req, res) => {
const success = await destroyRemoteBrowser(req.params.browserId);
return res.send(success);
});
2024-06-09 00:37:35 +05:30
/**
* GET endpoint for getting the id of the active remote browser.
*/
router.get('/active', (req, res) => {
const id = getActiveBrowserId();
return res.send(id);
});
2024-06-09 00:37:55 +05:30
/**
* GET endpoint for getting the current url of the active remote browser.
*/
router.get('/active/url', (req, res) => {
const id = getActiveBrowserId();
if (id) {
const url = getRemoteBrowserCurrentUrl(id);
return res.send(url);
}
return res.send(null);
});
2024-06-09 00:38:16 +05:30
/**
* GET endpoint for getting the current tabs of the active remote browser.
*/
router.get('/active/tabs', (req, res) => {
const id = getActiveBrowserId();
if (id) {
const hosts = getRemoteBrowserCurrentTabs(id);
return res.send(hosts);
}
return res.send([]);
});
/**
* GET endpoint for starting an interpretation of the currently generated workflow.
*/
router.get('/interpret', async (req, res) => {
try {
await interpretWholeWorkflow();
return res.send('interpretation done');
} catch (e) {
2024-07-03 23:15:06 +05:30
return res.send('interpretation failed');
}
});
2024-06-09 00:39:57 +05:30
/**
* GET endpoint for stopping an ongoing interpretation of the currently generated workflow.
*/
router.get('/interpret/stop', async (req, res) => {
await stopRunningInterpretation();
return res.send('interpretation stopped');
});