Update app.js
Browse files
app.js
CHANGED
|
@@ -2146,7 +2146,7 @@ app.get('/ask', async (req, res) => {
|
|
| 2146 |
});
|
| 2147 |
|
| 2148 |
app.post("/playwright", async (req, res) => {
|
| 2149 |
-
const { code, timeout = 300000 } = req.body;
|
| 2150 |
if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
|
| 2151 |
|
| 2152 |
let output = [];
|
|
@@ -2181,8 +2181,8 @@ app.post("/playwright", async (req, res) => {
|
|
| 2181 |
try {
|
| 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 {
|
|
@@ -2193,12 +2193,41 @@ app.post("/playwright", async (req, res) => {
|
|
| 2193 |
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 2194 |
}
|
| 2195 |
|
| 2196 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 2204 |
app.listen(PORT, async () => {
|
|
|
|
| 2146 |
});
|
| 2147 |
|
| 2148 |
app.post("/playwright", async (req, res) => {
|
| 2149 |
+
const { code, timeout = 300000 } = req.body;
|
| 2150 |
if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
|
| 2151 |
|
| 2152 |
let output = [];
|
|
|
|
| 2181 |
try {
|
| 2182 |
runCode = vm.run(script, "sandbox.js");
|
| 2183 |
} catch (error) {
|
| 2184 |
+
console.log = originalLog;
|
| 2185 |
return res.status(500).json({ error: "Gagal menjalankan kode", details: error.message });
|
|
|
|
| 2186 |
}
|
| 2187 |
|
| 2188 |
try {
|
|
|
|
| 2193 |
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 2194 |
}
|
| 2195 |
|
| 2196 |
+
const result = output.length > 0 ? output.pop() : "Tidak ada hasil yang ditemukan.";
|
| 2197 |
+
console.log = originalLog;
|
| 2198 |
+
|
| 2199 |
+
res.json({ output: result });
|
| 2200 |
} catch (err) {
|
| 2201 |
+
console.log = originalLog;
|
| 2202 |
res.status(500).json({ error: "Kesalahan saat menjalankan kode", details: err.message });
|
| 2203 |
}
|
| 2204 |
});
|
| 2205 |
|
| 2206 |
+
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 |
+
|
| 2230 |
+
|
| 2231 |
const PORT = process.env.PORT || 7860;
|
| 2232 |
|
| 2233 |
app.listen(PORT, async () => {
|