wudysoft commited on
Commit
cebc7d5
·
verified ·
1 Parent(s): 811d145

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +18 -19
app.js CHANGED
@@ -2144,28 +2144,27 @@ app.get('/ask', async (req, res) => {
2144
  });
2145
 
2146
  app.post("/playwright", async (req, res) => {
2147
- const { code } = req.body;
2148
- if (!code) return res.status(400).json({ output: "code is required" });
2149
 
2150
- try {
2151
- const logs = [];
2152
- const originalConsoleLog = console.log;
2153
- console.log = (...args) => {
2154
- logs.push(args.map(arg => (typeof arg === "object" ? JSON.stringify(arg, null, 2) : String(arg))).join(" "));
2155
- originalConsoleLog(...args);
2156
- };
2157
 
2158
- const vm = new NodeVM({ require: { external: ["playwright"], builtin: [] } });
2159
- const wrappedCode = `(async () => { ${code} })();`;
2160
- const result = await vm.run(wrappedCode, import.meta.url);
2161
- if (result !== undefined) logs.push(String(result));
 
 
2162
 
2163
- console.log = originalConsoleLog;
2164
- res.json({ output: logs.length ? logs.join("\n") : "No output captured" });
2165
- } catch (error) {
2166
- console.log = originalConsoleLog;
2167
- res.status(500).json({ output: error.message });
2168
- }
 
 
 
 
2169
  });
2170
 
2171
  const PORT = process.env.PORT || 7860;
 
2144
  });
2145
 
2146
  app.post("/playwright", async (req, res) => {
2147
+ const { code } = req.body;
 
2148
 
2149
+ if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
 
 
 
 
 
 
2150
 
2151
+ try {
2152
+ const vm = new VM({
2153
+ sandbox: {
2154
+ playwright: { chromium },
2155
+ },
2156
+ });
2157
 
2158
+ const result = await vm.run(`
2159
+ (async () => {
2160
+ ${code}
2161
+ })();
2162
+ `);
2163
+
2164
+ res.json({ output: result });
2165
+ } catch (error) {
2166
+ res.status(500).json({ error: error.message });
2167
+ }
2168
  });
2169
 
2170
  const PORT = process.env.PORT || 7860;