wudysoft commited on
Commit
5ba4726
·
verified ·
1 Parent(s): bd34e7f

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +22 -19
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
- expressLogs.push(args.map(arg => (typeof arg === "object" ? JSON.stringify(arg) : arg)).join(" "));
 
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
- try {
2160
- let vmLogs = [];
2161
- const vm = new NodeVM({
2162
- console: "redirect",
2163
- sandbox: {},
2164
- require: {
2165
- external: true,
2166
- builtin: ["fs", "path"],
2167
- root: "./",
2168
- mock: { playwright: { chromium } },
2169
- },
2170
- });
 
2171
 
2172
- vm.on("console.log", (msg) => vmLogs.push(msg));
2173
 
2174
- const startIndex = expressLogs.length;
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
- res.status(500).json({ error: error.message, log: [...expressLogs, ...vmLogs].pop() || "" });
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;