@grasplabs/grasp
Version:
TypeScript SDK for browser automation and secure command execution in highly available and scalable cloud browser environments
117 lines • 4.41 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraspBrowser = void 0;
const logger_1 = require("../utils/logger");
const node_path_1 = __importDefault(require("node:path"));
class GraspBrowser {
constructor(connection, session) {
this.connection = connection;
this.session = session;
this.logger = this.getDefaultLogger();
}
/**
* Gets or creates a default logger instance
* @returns Logger instance
*/
getDefaultLogger() {
try {
return (0, logger_1.getLogger)().child('BrowserService');
}
catch (error) {
// If logger is not initialized, create a default one
const defaultLogger = new logger_1.Logger({
level: 'info',
console: true,
});
return defaultLogger.child('BrowserService');
}
}
getHost() {
return new URL(this.connection.httpUrl).host;
}
getEndpoint() {
return this.connection.wsUrl;
}
/**
* @experimental This method is experimental and may change or be removed in future versions.
* Use with caution in production environments.
*
* @param localPath Local path to save the video
*/
async downloadReplayVideo(localPath) {
const terminal = this.session.terminal;
const command = await terminal.runCommand(`cd /home/user/downloads/grasp-screenshots && ls -1 | grep -v '^filelist.txt$' | sort | awk '{print "file '\\''" $0 "'\\''"}' > filelist.txt`);
await command.end();
const command2 = await terminal.runCommand(`cd /home/user/downloads/grasp-screenshots && ffmpeg -r 25 -f concat -safe 0 -i filelist.txt -vsync vfr -pix_fmt yuv420p output.mp4`);
command2.stdout.pipe(process.stdout);
command2.stderr.pipe(process.stderr);
await command2.end();
// check if localPath is mp4 file
if (!localPath.endsWith('.mp4')) {
localPath = node_path_1.default.join(localPath, 'output.mp4');
}
await this.session.files.downloadFile('/home/user/downloads/grasp-screenshots/output.mp4', localPath);
}
/**
* @experimental This method is experimental and may change or be removed in future versions.
* Use with caution in production environments.
*/
async getCurrentPageTargetInfo() {
const host = this.connection.httpUrl;
const api = `${host}/api/page/info`;
const response = await fetch(api);
if (!response.ok) {
return null;
}
const data = await response.json();
const { lastScreenshot, sessionId, targets, targetId, pageLoaded } = data.pageInfo;
let currentTarget = {};
for (const target of Object.values(targets)) {
if (target.targetId === targetId) {
currentTarget = target;
}
}
return {
// pageInfo: data.pageInfo,
targetId,
sessionId,
pageLoaded,
...currentTarget,
lastScreenshot,
targets,
};
}
/**
* @experimental This method is experimental and may change or be removed in future versions.
* Use with caution in production environments.
*/
async getLiveviewStreamingUrl() {
const host = this.connection.httpUrl;
const api = `${host}/api/session/liveview/stream`;
const response = await fetch(api);
if (!response.ok || !response.body) {
this.logger.warn('liveview stream not ready', response);
return null;
}
return api;
}
/**
* @experimental This method is experimental and may change or be removed in future versions.
* Use with caution in production environments.
*/
async getLiveviewPageUrl() {
const host = this.connection.httpUrl;
const api = `${host}/api/session/liveview/preview`;
const response = await fetch(api);
if (!response.ok || !response.body) {
this.logger.warn('liveview page not ready', response);
return null;
}
return api;
}
}
exports.GraspBrowser = GraspBrowser;
//# sourceMappingURL=browser.js.map