@grasplabs/grasp
Version:
TypeScript SDK for browser automation and secure command execution in highly available and scalable cloud browser environments
59 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Grasp = void 0;
const server_1 = require("./server");
const session_1 = require("./session");
const shutdown_1 = require("../utils/shutdown");
const config_1 = require("../utils/config");
class Grasp {
constructor(options = { apiKey: process.env.GRASP_KEY || '' }) {
this.key = options.apiKey;
}
async launch(options = {}) {
const config = (0, config_1.getConfig)();
const server = new server_1.GraspServer({
...options.browser,
key: this.key || config.sandbox.key,
keepAliveMS: options.keepAliveMS != null
? options.keepAliveMS
: config.sandbox.keepAliveMs,
debug: options.debug || config.sandbox.debug,
timeout: options.timeout || config.sandbox.timeout,
logLevel: options.logLevel || config.logger.level,
});
const cdpConnection = await server.createBrowserTask();
return new session_1.GraspSession(cdpConnection);
}
async connect(sessionId) {
let server = server_1.servers[sessionId];
let cdpConnection;
if (server) {
cdpConnection = server.getCDPConnection();
}
server = new server_1.GraspServer({ key: this.key, timeout: 0 }); // 使用创建时默认的 timeout
cdpConnection = await server.connectBrowserTask(sessionId);
return new session_1.GraspSession(cdpConnection);
}
// deprecated 向后兼容
/**
* @deprecated Use new Grasp().launch() instead
*/
static launchBrowser(options) {
const server = new server_1.GraspServer(options);
return server.createBrowserTask();
}
/**
* @deprecated Use session.shutdown() instead
*/
static shutdown(connection) {
if (connection) {
server_1.servers[connection.id].cleanup();
}
else {
(0, shutdown_1.gracefulShutdown)('USER_SHUTDOWN');
}
}
}
exports.Grasp = Grasp;
exports.default = Grasp;
//# sourceMappingURL=index.js.map