wudysoft commited on
Commit
775f3bc
·
verified ·
1 Parent(s): 763517a

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +15 -6
app.js CHANGED
@@ -2145,8 +2145,8 @@ app.get('/ask', async (req, res) => {
2145
  }
2146
  });
2147
 
2148
- app.post("/playwright", (req, res) => {
2149
- const { code } = req.body;
2150
  if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
2151
 
2152
  let expressLogs = [];
@@ -2182,13 +2182,22 @@ app.post("/playwright", (req, res) => {
2182
  runCode = vm.run(script, "sandbox.js");
2183
  } catch (error) {
2184
  return res.status(500).json({ error: "Gagal menjalankan kode", details: error.message });
 
2185
  }
2186
 
2187
- Promise.resolve(runCode())
2188
- .then(() => res.json({ logs: expressLogs }))
2189
- .catch(err => res.status(500).json({ error: "Kesalahan saat menjalankan kode", details: err.message }));
2190
- });
 
 
 
2191
 
 
 
 
 
 
2192
 
2193
  const PORT = process.env.PORT || 7860;
2194
 
 
2145
  }
2146
  });
2147
 
2148
+ app.post("/playwright", async (req, res) => {
2149
+ const { code, timeout = 300000 } = req.body; // Timeout default 5 menit
2150
  if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
2151
 
2152
  let expressLogs = [];
 
2182
  runCode = vm.run(script, "sandbox.js");
2183
  } catch (error) {
2184
  return res.status(500).json({ error: "Gagal menjalankan kode", details: error.message });
2185
+ return;
2186
  }
2187
 
2188
+ try {
2189
+ await runCode();
2190
+
2191
+ const startTime = Date.now();
2192
+ while (expressLogs.length === 0 && Date.now() - startTime < timeout) {
2193
+ await new Promise(resolve => setTimeout(resolve, 1000));
2194
+ }
2195
 
2196
+ res.json({ logs: expressLogs });
2197
+ } catch (err) {
2198
+ res.status(500).json({ error: "Kesalahan saat menjalankan kode", details: err.message });
2199
+ }
2200
+ });
2201
 
2202
  const PORT = process.env.PORT || 7860;
2203