Update app.js
Browse files
app.js
CHANGED
|
@@ -2145,26 +2145,25 @@ app.get('/ask', async (req, res) => {
|
|
| 2145 |
|
| 2146 |
app.post("/playwright", async (req, res) => {
|
| 2147 |
const { code } = req.body;
|
| 2148 |
-
if (!code) return res.status(400).
|
| 2149 |
|
| 2150 |
try {
|
| 2151 |
let output = [];
|
| 2152 |
const vm = new NodeVM({
|
| 2153 |
sandbox: {
|
| 2154 |
output,
|
| 2155 |
-
console: { log: (...args) => output.push(args.
|
| 2156 |
},
|
| 2157 |
require: { external: ["playwright"], builtin: [] },
|
| 2158 |
});
|
| 2159 |
|
| 2160 |
const wrappedCode = `(async () => { ${code} })();`;
|
| 2161 |
const result = await vm.run(wrappedCode, import.meta.url);
|
| 2162 |
-
if (result) output.push(String(result));
|
| 2163 |
|
| 2164 |
-
|
| 2165 |
-
res.send(parsedOutput);
|
| 2166 |
} catch (error) {
|
| 2167 |
-
res.status(500).
|
| 2168 |
}
|
| 2169 |
});
|
| 2170 |
|
|
|
|
| 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 |
let output = [];
|
| 2152 |
const vm = new NodeVM({
|
| 2153 |
sandbox: {
|
| 2154 |
output,
|
| 2155 |
+
console: { log: (...args) => output.push(...args.map(arg => typeof arg === "object" ? JSON.stringify(arg, null, 2) : String(arg))) }
|
| 2156 |
},
|
| 2157 |
require: { external: ["playwright"], builtin: [] },
|
| 2158 |
});
|
| 2159 |
|
| 2160 |
const wrappedCode = `(async () => { ${code} })();`;
|
| 2161 |
const result = await vm.run(wrappedCode, import.meta.url);
|
| 2162 |
+
if (result !== undefined) output.push(typeof result === "object" ? JSON.stringify(result, null, 2) : String(result));
|
| 2163 |
|
| 2164 |
+
res.json({ output: output.length ? output.join("\n") : "No output captured" });
|
|
|
|
| 2165 |
} catch (error) {
|
| 2166 |
+
res.status(500).json({ output: error.message });
|
| 2167 |
}
|
| 2168 |
});
|
| 2169 |
|