Update app.js
Browse files
app.js
CHANGED
|
@@ -2145,58 +2145,50 @@ app.get('/ask', async (req, res) => {
|
|
| 2145 |
}
|
| 2146 |
});
|
| 2147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2148 |
app.post("/playwright", async (req, res) => {
|
| 2149 |
const { code } = req.body;
|
| 2150 |
-
|
| 2151 |
-
if (!code) {
|
| 2152 |
-
console.error("[ERROR] Kode tidak boleh kosong");
|
| 2153 |
-
return res.status(400).json({ error: "Kode tidak boleh kosong" });
|
| 2154 |
-
}
|
| 2155 |
-
|
| 2156 |
-
console.log("\n====================================");
|
| 2157 |
-
console.log("[INFO] Eksekusi kode dimulai...");
|
| 2158 |
-
console.log("[CODE] ====>\n", code);
|
| 2159 |
-
|
| 2160 |
-
let logs = [];
|
| 2161 |
|
| 2162 |
try {
|
| 2163 |
const vm = new NodeVM({
|
| 2164 |
-
console: "
|
| 2165 |
sandbox: {},
|
| 2166 |
require: {
|
| 2167 |
external: true,
|
| 2168 |
builtin: ["fs", "path"],
|
| 2169 |
root: "./",
|
| 2170 |
-
mock: {
|
| 2171 |
-
playwright: { chromium },
|
| 2172 |
-
},
|
| 2173 |
},
|
| 2174 |
});
|
| 2175 |
|
| 2176 |
-
// Tangkap log dari dalam VM
|
| 2177 |
-
vm.on("console.log", (msg) => {
|
| 2178 |
-
logs.push(msg);
|
| 2179 |
-
console.log("[SANDBOX]", msg);
|
| 2180 |
-
});
|
| 2181 |
-
|
| 2182 |
const script = `
|
| 2183 |
module.exports = async () => {
|
| 2184 |
-
console.log("
|
| 2185 |
${code}
|
| 2186 |
};
|
| 2187 |
`;
|
| 2188 |
|
|
|
|
| 2189 |
const runCode = vm.run(script, "sandbox.js");
|
| 2190 |
-
|
| 2191 |
await runCode();
|
| 2192 |
|
| 2193 |
-
|
| 2194 |
-
|
| 2195 |
|
| 2196 |
-
res.json({ logs });
|
| 2197 |
} catch (error) {
|
| 2198 |
-
|
| 2199 |
-
res.status(500).json({ error: error.message, logs });
|
| 2200 |
}
|
| 2201 |
});
|
| 2202 |
|
|
|
|
| 2145 |
}
|
| 2146 |
});
|
| 2147 |
|
| 2148 |
+
// Simpan log utama Express
|
| 2149 |
+
let expressLogs = [];
|
| 2150 |
+
|
| 2151 |
+
// Override console.log agar log disimpan di Express
|
| 2152 |
+
const originalLog = console.log;
|
| 2153 |
+
console.log = (...args) => {
|
| 2154 |
+
const message = args.map(arg => (typeof arg === "object" ? JSON.stringify(arg) : arg)).join(" ");
|
| 2155 |
+
expressLogs.push(message);
|
| 2156 |
+
originalLog.apply(console, args); // Tetap tampilkan di terminal
|
| 2157 |
+
};
|
| 2158 |
+
|
| 2159 |
app.post("/playwright", async (req, res) => {
|
| 2160 |
const { code } = req.body;
|
| 2161 |
+
if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2162 |
|
| 2163 |
try {
|
| 2164 |
const vm = new NodeVM({
|
| 2165 |
+
console: "inherit", // Log tetap muncul di Express, tidak ditangkap dari VM
|
| 2166 |
sandbox: {},
|
| 2167 |
require: {
|
| 2168 |
external: true,
|
| 2169 |
builtin: ["fs", "path"],
|
| 2170 |
root: "./",
|
| 2171 |
+
mock: { playwright: { chromium } },
|
|
|
|
|
|
|
| 2172 |
},
|
| 2173 |
});
|
| 2174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2175 |
const script = `
|
| 2176 |
module.exports = async () => {
|
| 2177 |
+
console.log("Kode sedang berjalan di Express...");
|
| 2178 |
${code}
|
| 2179 |
};
|
| 2180 |
`;
|
| 2181 |
|
| 2182 |
+
const startIndex = expressLogs.length; // Simpan indeks log sebelum runCode()
|
| 2183 |
const runCode = vm.run(script, "sandbox.js");
|
|
|
|
| 2184 |
await runCode();
|
| 2185 |
|
| 2186 |
+
// Ambil log hanya setelah runCode()
|
| 2187 |
+
const logsAfterRun = expressLogs.slice(startIndex);
|
| 2188 |
|
| 2189 |
+
res.json({ logs: logsAfterRun });
|
| 2190 |
} catch (error) {
|
| 2191 |
+
res.status(500).json({ error: error.message, logs: expressLogs });
|
|
|
|
| 2192 |
}
|
| 2193 |
});
|
| 2194 |
|