Update app.js
Browse files
app.js
CHANGED
|
@@ -2143,69 +2143,25 @@ app.get('/ask', async (req, res) => {
|
|
| 2143 |
}
|
| 2144 |
});
|
| 2145 |
|
| 2146 |
-
app.post("/playwright
|
| 2147 |
const { code } = req.body;
|
| 2148 |
-
|
| 2149 |
-
if (!code) {
|
| 2150 |
-
return res.status(400).json({ output: "code is required" });
|
| 2151 |
-
}
|
| 2152 |
|
| 2153 |
try {
|
| 2154 |
-
let output =
|
| 2155 |
-
|
| 2156 |
-
const sandboxConsole = {
|
| 2157 |
-
log: (...args) => {
|
| 2158 |
-
output += args.map(arg => String(arg)).join(" ") + "\n";
|
| 2159 |
-
},
|
| 2160 |
-
};
|
| 2161 |
-
|
| 2162 |
const vm = new NodeVM({
|
| 2163 |
-
sandbox: {
|
| 2164 |
-
|
| 2165 |
-
|
| 2166 |
-
builtin: [],
|
| 2167 |
},
|
|
|
|
| 2168 |
});
|
| 2169 |
|
| 2170 |
-
|
| 2171 |
-
|
| 2172 |
-
|
| 2173 |
-
} catch (error) {
|
| 2174 |
-
res.status(500).json({ output: error.message });
|
| 2175 |
-
}
|
| 2176 |
-
});
|
| 2177 |
-
|
| 2178 |
-
app.post("/playwright/v2", async (req, res) => {
|
| 2179 |
-
const { code } = req.body;
|
| 2180 |
-
|
| 2181 |
-
if (!code) {
|
| 2182 |
-
return res.status(400).json({ output: "code is required" });
|
| 2183 |
-
}
|
| 2184 |
-
|
| 2185 |
-
try {
|
| 2186 |
-
let output = "";
|
| 2187 |
|
| 2188 |
-
|
| 2189 |
-
console.log = (...args) => {
|
| 2190 |
-
output += args.map(arg => String(arg)).join(" ") + "\n";
|
| 2191 |
-
originalConsoleLog(...args);
|
| 2192 |
-
};
|
| 2193 |
-
|
| 2194 |
-
await (async () => {
|
| 2195 |
-
try {
|
| 2196 |
-
eval(`
|
| 2197 |
-
(async () => {
|
| 2198 |
-
${code}
|
| 2199 |
-
})();
|
| 2200 |
-
`);
|
| 2201 |
-
} catch (err) {
|
| 2202 |
-
console.log("Error:", err.message);
|
| 2203 |
-
}
|
| 2204 |
-
})();
|
| 2205 |
-
|
| 2206 |
-
console.log = originalConsoleLog;
|
| 2207 |
-
|
| 2208 |
-
res.json({ output: output.trim() });
|
| 2209 |
} catch (error) {
|
| 2210 |
res.status(500).json({ output: error.message });
|
| 2211 |
}
|
|
|
|
| 2143 |
}
|
| 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 |
+
let output = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2152 |
const vm = new NodeVM({
|
| 2153 |
+
sandbox: {
|
| 2154 |
+
output,
|
| 2155 |
+
console: { log: (...args) => output.push(args.join(" ")) }
|
|
|
|
| 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 |
+
res.json({ output: output.join("\n") || "No output captured" });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2165 |
} catch (error) {
|
| 2166 |
res.status(500).json({ output: error.message });
|
| 2167 |
}
|