Spaces:
Paused
Paused
Commit ·
d8f7b5b
0
Parent(s):
hmm
Browse filesFiles changed (4)
- DockerfileAdded+22
- README.mdAdded+12
- app.jsAdded+137
- package.jsonAdded+26
12345678910111213141516171819202122FROM mcr.microsoft.com/playwright:focal
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD 1
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN apt-get update && apt-get install -y \ imagemagick \ ffmpeg \ && rm -rf /var/lib/apt/lists/*
COPY . .
RUN npx playwright install --with-deps
EXPOSE 7860
CMD ["npm", "start"]123456789101112---title: Playwright Apiemoji: 🔥colorFrom: bluecolorTo: yellowsdk: dockerpinned: falselicense: mitshort_description: API for executing Playwright scripts---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137const express = require('express');const { chromium } = require('playwright');const cors = require('cors');const dotenv = require('dotenv');const { NodeVM } = require('vm2');const os = require('os');const axios = require('axios');const fetch = require('node-fetch');
dotenv.config();
const app = express();
app.set('json spaces', 2);app.use(express.json());app.use(cors());
app.get('/', (req, res) => { const formatMemory = (bytes) => { const gb = bytes / 1024 / 1024 / 1024; if (gb >= 1) { return gb.toFixed(2) + ' GB'; } else { return (bytes / 1024 / 1024).toFixed(2) + ' MB'; } }; const cpuModel = os.cpus().length > 0 ? os.cpus()[0].model.replace(/\s+/g, ' ').trim() : 'Unknown'; const uptime = (() => { const totalSeconds = os.uptime(); const days = Math.floor(totalSeconds / (3600 * 24)); const hours = Math.floor((totalSeconds % (3600 * 24)) / 3600); const minutes = Math.floor((totalSeconds % 3600) / 60); return `${days}d ${hours}h ${minutes}m`; })(); const memoryUsedPercentage = ((os.totalmem() - os.freemem()) / os.totalmem() * 100).toFixed(2); res.json({ status: "online", uptime: uptime, cpu: { model: cpuModel, cores: os.cpus().length, load: os.loadavg()[0].toFixed(2) + '%' }, memory: { total: formatMemory(os.totalmem()), used: formatMemory(os.totalmem() - os.freemem()) + ` (${memoryUsedPercentage}%)`, free: formatMemory(os.freemem()) }, info_api: [ { path: '/run', method: 'POST', description: 'Run Playwright code in a sandboxed VM environment', body: { code: 'JavaScript code to execute (required)', timeout: 'Execution timeout in milliseconds (optional, default: 300000)' } } ] });});
app.post('/run', async (req, res) => { const { code, timeout = 300000 } = req.body; if (!code) return res.status(400).json({ error: 'Code cannot be empty' });
let output = []; const originalLog = console.log;
console.log = (...args) => { const message = args.map(arg => (typeof arg === 'object' ? JSON.stringify(arg) : arg)).join(' '); output.push(message); originalLog.apply(console, args); };
const vm = new NodeVM({ console: 'inherit', sandbox: {}, require: { external: true, builtin: ['fs', 'path'], root: './', mock: { playwright: { chromium }, axios, 'node-fetch': fetch, }, }, });
const script = ` module.exports = async () => { ${code} }; `;
let runCode; try { runCode = vm.run(script, 'sandbox.js'); } catch (error) { console.log = originalLog; return res.status(500).json({ error: 'Failed to run code', details: error.message }); }
try { await runCode();
const startTime = Date.now(); while (output.length === 0 && Date.now() - startTime < timeout) { await new Promise(resolve => setTimeout(resolve, 1000)); }
const result = output.length > 0 ? output.pop() : 'No results found.'; console.log = originalLog;
res.json({ output: result }); } catch (err) { console.log = originalLog; res.status(500).json({ error: 'Error while executing code', details: err.message }); }});
const PORT = process.env.PORT || 7860;
app.listen(PORT, async () => { console.log(`Server running on port ${PORT}`);});
process.on('SIGINT', async () => { process.exit(0);});1234567891011121314151617181920212223242526{ "name": "playwright-api", "version": "1.0.0", "description": "API for executing Playwright scripts with system information", "main": "index.js", "type": "commonjs", "scripts": { "start": "node app.js", "dev": "nodemon app.js" }, "keywords": ["playwright", "express", "API"], "author": "Rynn", "license": "MIT", "dependencies": { "axios": "latest", "node-fetch": "latest", "express": "latest", "playwright": "latest", "cors": "latest", "dotenv": "latest", "vm2": "latest" }, "devDependencies": { "nodemon": "latest" }}DockerfileAdded+22
README.mdAdded+12
app.jsAdded+137
package.jsonAdded+26