wudysoft commited on
Commit
5575ff8
·
verified ·
1 Parent(s): 138b12e

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +31 -11
app.js CHANGED
@@ -2207,23 +2207,43 @@ app.post("/playwright/v2", async (req, res) => {
2207
  const { code, timeout = 300000 } = req.body;
2208
  if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
2209
 
 
 
 
 
 
 
 
 
 
 
2210
  try {
2211
- // Wrap the code in a function and execute it
2212
- const script = `
2213
  (async () => {
2214
- return await (async () => {
2215
- ${code}
2216
- })();
2217
  })();
2218
- `;
 
 
 
 
2219
 
2220
- // Use eval to run the script and get the result
2221
- const result = await eval(script);
 
 
 
 
 
 
 
 
2222
 
2223
- // Return the result directly
2224
  res.json({ output: result });
2225
- } catch (error) {
2226
- res.status(500).json({ error: "Gagal menjalankan kode", details: error.message });
 
2227
  }
2228
  });
2229
 
 
2207
  const { code, timeout = 300000 } = req.body;
2208
  if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
2209
 
2210
+ let output = [];
2211
+ const originalLog = console.log;
2212
+
2213
+ console.log = (...args) => {
2214
+ const message = args.map(arg => (typeof arg === "object" ? JSON.stringify(arg) : arg)).join(" ");
2215
+ output.push(message);
2216
+ originalLog.apply(console, args);
2217
+ };
2218
+
2219
+ let runCode;
2220
  try {
2221
+ // Menggunakan eval untuk mengeksekusi kode
2222
+ runCode = eval(`
2223
  (async () => {
2224
+ ${code}
 
 
2225
  })();
2226
+ `);
2227
+ } catch (error) {
2228
+ console.log = originalLog;
2229
+ return res.status(500).json({ error: "Gagal menjalankan kode", details: error.message });
2230
+ }
2231
 
2232
+ try {
2233
+ await runCode;
2234
+
2235
+ const startTime = Date.now();
2236
+ while (output.length === 0 && Date.now() - startTime < timeout) {
2237
+ await new Promise(resolve => setTimeout(resolve, 1000));
2238
+ }
2239
+
2240
+ const result = output.length > 0 ? output.pop() : "Tidak ada hasil yang ditemukan.";
2241
+ console.log = originalLog;
2242
 
 
2243
  res.json({ output: result });
2244
+ } catch (err) {
2245
+ console.log = originalLog;
2246
+ res.status(500).json({ error: "Kesalahan saat menjalankan kode", details: err.message });
2247
  }
2248
  });
2249