Update app.js
Browse files
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 |
-
//
|
| 2212 |
-
|
| 2213 |
(async () => {
|
| 2214 |
-
|
| 2215 |
-
${code}
|
| 2216 |
-
})();
|
| 2217 |
})();
|
| 2218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2219 |
|
| 2220 |
-
|
| 2221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2222 |
|
| 2223 |
-
// Return the result directly
|
| 2224 |
res.json({ output: result });
|
| 2225 |
-
} catch (
|
| 2226 |
-
|
|
|
|
| 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 |
|