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

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +26 -21
app.js CHANGED
@@ -2145,46 +2145,51 @@ app.get('/ask', async (req, res) => {
2145
  }
2146
  });
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
-
2157
- app.post("/playwright", async (req, res) => {
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;
2189
 
2190
  app.listen(PORT, async () => {
 
2145
  }
2146
  });
2147
 
2148
+ app.post("/playwright", (req, res) => {
 
 
 
 
 
 
 
 
 
2149
  const { code } = req.body;
2150
  if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
2151
 
2152
+ let expressLogs = [];
2153
+ const originalLog = console.log;
2154
+
2155
+ console.log = (...args) => {
2156
+ const message = args.map(arg => (typeof arg === "object" ? JSON.stringify(arg) : arg)).join(" ");
2157
+ expressLogs.push(message);
2158
+ originalLog.apply(console, args);
2159
+ };
2160
+
2161
  const vm = new NodeVM({
2162
+ console: "inherit",
2163
  sandbox: {},
2164
  require: {
2165
  external: true,
2166
  builtin: ["fs", "path"],
2167
  root: "./",
2168
+ mock: {
2169
+ playwright: { chromium },
2170
+ },
2171
  },
2172
  });
2173
 
2174
+ const script = `
2175
+ module.exports = async () => {
2176
+ ${code}
2177
+ };
2178
+ `;
2179
 
2180
+ let runCode;
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
  }
2186
 
2187
+ Promise.resolve(runCode())
2188
+ .then(() => res.json({ logs: expressLogs }))
2189
+ .catch(err => res.status(500).json({ error: "Kesalahan saat menjalankan kode", details: err.message }));
2190
  });
2191
 
2192
+
2193
  const PORT = process.env.PORT || 7860;
2194
 
2195
  app.listen(PORT, async () => {