@grasplabs/grasp
Version:
TypeScript SDK for browser automation and secure command execution in highly available and scalable cloud browser environments
115 lines • 4.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.servers = exports.GraspServer = void 0;
const config_1 = require("../utils/config");
const logger_1 = require("../utils/logger");
const browser_service_1 = require("../services/browser.service");
const servers = {};
exports.servers = servers;
class GraspServer {
constructor(sandboxConfig = { type: 'chromium', headless: true }) {
this.browserService = null;
this.browserConfig = {};
this.browserType = 'chromium';
const { type, headless, adblock, logLevel, keepAliveMS, liveview, ...options } = sandboxConfig;
const config = (0, config_1.getConfig)();
config.sandbox = { ...config.sandbox, ...options };
this.config = config;
this.browserConfig = {
headless: headless ?? true,
envs: {
ADBLOCK: adblock ? 'true' : 'false',
KEEP_ALIVE_MS: (keepAliveMS || 10000).toString(),
ENABLE_LIVEVIEW: liveview ? 'true' : 'false',
},
};
this.browserType = type || 'chromium';
// Initialize logger first
const loggerConfig = { ...config.logger };
loggerConfig.level = logLevel || (sandboxConfig.debug ? 'debug' : 'info');
(0, logger_1.initLogger)(loggerConfig);
this.logger = (0, logger_1.getLogger)().child('GraspServer');
this.logger.info('GraspServer initialized');
}
get sandbox() {
return this.browserService?.getSandbox();
}
/**
* Gets current sandbox status
* @returns Sandbox status
*/
getStatus() {
return this.sandbox?.getStatus();
}
/**
* Gets sandbox ID
* @returns Sandbox ID or null
*/
getSandboxId() {
return this.sandbox?.getSandboxId();
}
async connectBrowserTask(sandboxId) {
if (this.browserService) {
throw new Error('Browser service can only be initialized once');
}
this.browserService = new browser_service_1.BrowserService(this.config.sandbox);
const cdpConnection = await this.browserService.connect(sandboxId);
servers[this.browserService.id] = this;
this.logger.info('🚀 Browser service initialized', {
id: this.browserService.id,
});
return cdpConnection;
}
getCDPConnection() {
return this.browserService?.getCDPConnection();
}
async createBrowserTask() {
if (this.browserService) {
throw new Error('Browser service can only be initialized once');
}
this.browserService = new browser_service_1.BrowserService(this.config.sandbox, {
headless: true,
launchTimeout: 30000,
args: [
'--disable-web-security',
'--disable-features=VizDisplayCompositor',
],
...this.browserConfig,
});
await this.browserService.initialize(this.browserType);
servers[this.browserService.id] = this;
this.logger.info('🚀 Browser service initialized', {
id: this.browserService.id,
});
this.logger.info('🌐 Launching Chromium browser with CDP...');
const cdpConnection = await this.browserService.launchBrowser();
this.logger.info('✅ Browser launched successfully!');
this.logger.debug('CDP Connection Info:', {
wsUrl: cdpConnection.wsUrl,
httpUrl: cdpConnection.httpUrl,
});
return cdpConnection;
}
/**
* Cleanup resources
* @returns Promise that resolves when cleanup is complete
*/
async cleanup() {
this.logger.info('Starting cleanup process');
try {
if (this.browserService) {
const id = this.browserService.id;
await this.browserService.cleanup();
if (id)
delete servers[id];
}
this.logger.info('Cleanup completed successfully');
}
catch (error) {
this.logger.error('Cleanup failed', error);
throw error;
}
}
}
exports.GraspServer = GraspServer;
//# sourceMappingURL=server.js.map