UNPKG

@mrtkrcm/mcp-puppeteer

Version:

Model Context Protocol server for browser automation using Puppeteer

140 lines 6.65 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; export function getBrowserConfig() { var config = { headless: process.env.DOCKER_CONTAINER === 'true' ? true : 'new', defaultViewport: { width: parseInt(process.env.PUPPETEER_VIEWPORT_WIDTH || '1280', 10), height: parseInt(process.env.PUPPETEER_VIEWPORT_HEIGHT || '800', 10) }, ignoreHTTPSErrors: true, args: [] }; // Add slowMo if specified if (process.env.PUPPETEER_SLOW_MO) { var slowMo = parseInt(process.env.PUPPETEER_SLOW_MO, 10); if (!isNaN(slowMo)) { config.slowMo = slowMo; } } // Add custom user agent if specified if (process.env.PUPPETEER_USER_AGENT) { config.userAgent = process.env.PUPPETEER_USER_AGENT; } // Add timeout if specified if (process.env.PUPPETEER_TIMEOUT) { var timeout = parseInt(process.env.PUPPETEER_TIMEOUT, 10); if (!isNaN(timeout)) { config.timeout = timeout; } } // Add Docker-specific arguments if running in Docker if (process.env.DOCKER_CONTAINER === 'true') { var extraArgs = ["--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage", "--single-process", "--no-zygote"]; config.args = __spreadArray(__spreadArray([], (config.args || []), true), extraArgs, true); } // Parse and add custom arguments if specified if (process.env.PUPPETEER_ARGS) { try { var customArgs = JSON.parse(process.env.PUPPETEER_ARGS); if (Array.isArray(customArgs)) { config.args = __spreadArray(__spreadArray([], (config.args || []), true), customArgs, true); } } catch (e) { console.error('Failed to parse PUPPETEER_ARGS:', e); } } return config; } export function isAllowedDomain(url) { // Get allowed domains from environment variable var allowedDomains = (process.env.ALLOWED_DOMAINS || "").split(',').map(function (d) { return d.trim(); }).filter(Boolean); // If no allowed domains are specified, allow all if (allowedDomains.length === 0) return true; try { var urlObj_1 = new URL(url); return allowedDomains.some(function (domain) { // Support wildcards (e.g., *.example.com) if (domain.startsWith('*.')) { var baseDomain = domain.substring(2); return urlObj_1.hostname.endsWith(baseDomain); } return urlObj_1.hostname === domain; }); } catch (e) { return false; // Invalid URL } } export function sanitizeScript(script) { // Basic sanitization - more advanced approaches could be implemented var disallowedPatterns = [ /process\s*\.\s*env/i, /require\s*\(/i, /eval\s*\(/i, /Function\s*\(/i, /document\.cookie/i ]; var containsDisallowed = disallowedPatterns.some(function (pattern) { return pattern.test(script); }); if (containsDisallowed) { throw new Error("Script contains potentially unsafe operations"); } return script; } export function withTimeout(promise, timeoutMs, operation) { return __awaiter(this, void 0, void 0, function () { var timeoutPromise; return __generator(this, function (_a) { timeoutPromise = new Promise(function (_, reject) { setTimeout(function () { return reject(new Error("Operation \"".concat(operation, "\" timed out after ").concat(timeoutMs, "ms"))); }, timeoutMs); }); return [2 /*return*/, Promise.race([promise, timeoutPromise])]; }); }); } //# sourceMappingURL=browserConfig.js.map