Update app.js
Browse files
app.js
CHANGED
|
@@ -2147,8 +2147,10 @@ app.get('/ask', async (req, res) => {
|
|
| 2147 |
|
| 2148 |
let expressLogs = [];
|
| 2149 |
const originalLog = console.log;
|
|
|
|
| 2150 |
console.log = (...args) => {
|
| 2151 |
-
|
|
|
|
| 2152 |
originalLog.apply(console, args);
|
| 2153 |
};
|
| 2154 |
|
|
@@ -2156,30 +2158,31 @@ app.post("/playwright", async (req, res) => {
|
|
| 2156 |
const { code } = req.body;
|
| 2157 |
if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
|
| 2158 |
|
| 2159 |
-
|
| 2160 |
-
|
| 2161 |
-
|
| 2162 |
-
|
| 2163 |
-
|
| 2164 |
-
|
| 2165 |
-
|
| 2166 |
-
|
| 2167 |
-
|
| 2168 |
-
|
| 2169 |
-
|
| 2170 |
-
|
|
|
|
| 2171 |
|
| 2172 |
-
|
| 2173 |
|
| 2174 |
-
|
| 2175 |
const runCode = vm.run(`module.exports = async () => { ${code} };`, "sandbox.js");
|
| 2176 |
await runCode();
|
| 2177 |
-
|
| 2178 |
-
const logsAfterRun = [...expressLogs.slice(startIndex), ...vmLogs];
|
| 2179 |
-
res.json({ log: logsAfterRun.pop() || "" });
|
| 2180 |
} catch (error) {
|
| 2181 |
-
|
| 2182 |
}
|
|
|
|
|
|
|
|
|
|
| 2183 |
});
|
| 2184 |
|
| 2185 |
const PORT = process.env.PORT || 7860;
|
|
|
|
| 2147 |
|
| 2148 |
let expressLogs = [];
|
| 2149 |
const originalLog = console.log;
|
| 2150 |
+
|
| 2151 |
console.log = (...args) => {
|
| 2152 |
+
const message = args.map(arg => (typeof arg === "object" ? JSON.stringify(arg) : arg)).join(" ");
|
| 2153 |
+
expressLogs.push(message);
|
| 2154 |
originalLog.apply(console, args);
|
| 2155 |
};
|
| 2156 |
|
|
|
|
| 2158 |
const { code } = req.body;
|
| 2159 |
if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
|
| 2160 |
|
| 2161 |
+
let vmLogs = [];
|
| 2162 |
+
const vm = new NodeVM({
|
| 2163 |
+
console: 'inherit',
|
| 2164 |
+
sandbox: {},
|
| 2165 |
+
require: {
|
| 2166 |
+
external: true,
|
| 2167 |
+
builtin: ["fs", "path"],
|
| 2168 |
+
root: "./",
|
| 2169 |
+
mock: { playwright: { chromium: {} } },
|
| 2170 |
+
},
|
| 2171 |
+
});
|
| 2172 |
+
|
| 2173 |
+
vm.on("console.log", (msg) => vmLogs.push(msg));
|
| 2174 |
|
| 2175 |
+
expressLogs = [];
|
| 2176 |
|
| 2177 |
+
try {
|
| 2178 |
const runCode = vm.run(`module.exports = async () => { ${code} };`, "sandbox.js");
|
| 2179 |
await runCode();
|
|
|
|
|
|
|
|
|
|
| 2180 |
} catch (error) {
|
| 2181 |
+
vmLogs.push(`Error during execution: ${error.message}`);
|
| 2182 |
}
|
| 2183 |
+
|
| 2184 |
+
const logsAfterRun = [...vmLogs, ...expressLogs];
|
| 2185 |
+
res.json({ logs: logsAfterRun });
|
| 2186 |
});
|
| 2187 |
|
| 2188 |
const PORT = process.env.PORT || 7860;
|