HerzaJ commited on
Commit
bcad86d
·
verified ·
1 Parent(s): 97aeb9b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +17 -28
server.js CHANGED
@@ -1,5 +1,5 @@
1
  const express = require('express');
2
- const { chromium } = require('playwright');
3
  const cors = require('cors');
4
  const fs = require('fs').promises;
5
  const path = require('path');
@@ -24,7 +24,7 @@ app.post('/api/s-playwright', async (req, res) => {
24
  });
25
  }
26
 
27
- const supportedLangs = ['javascript', 'python', 'java', 'csharp'];
28
  const normalizedLang = lang.toLowerCase();
29
 
30
  if (!supportedLangs.includes(normalizedLang)) {
@@ -34,46 +34,35 @@ app.post('/api/s-playwright', async (req, res) => {
34
  });
35
  }
36
 
37
- let browser;
38
  try {
39
- browser = await chromium.launch({
40
- headless: true,
41
- args: ['--no-sandbox', '--disable-setuid-sandbox']
42
- });
43
-
44
- const context = await browser.newContext();
45
- const page = await context.newPage();
46
-
47
- let result;
48
- if (normalizedLang === 'javascript') {
49
- result = await page.evaluate(code);
50
- } else {
51
- result = { message: `Execution for ${normalizedLang} not yet implemented` };
52
- }
53
 
54
  const timestamp = Date.now();
55
- const screenshotPath = path.join(publicDir, `screenshot-${timestamp}.png`);
56
- await page.screenshot({ path: screenshotPath, fullPage: true });
57
-
58
- await browser.close();
 
 
 
59
 
60
  res.json({
61
  success: true,
62
  data: {
63
  result: result,
64
- files: [{
65
- name: `screenshot-${timestamp}.png`,
66
- publicURL: `/files/screenshot-${timestamp}.png`
67
- }]
68
  }
69
  });
70
 
71
  } catch (error) {
72
- if (browser) await browser.close();
73
-
74
  res.status(500).json({
75
  success: false,
76
- error: error.message
 
77
  });
78
  }
79
  });
 
1
  const express = require('express');
2
+ const { chromium, devices } = require('playwright');
3
  const cors = require('cors');
4
  const fs = require('fs').promises;
5
  const path = require('path');
 
24
  });
25
  }
26
 
27
+ const supportedLangs = ['javascript'];
28
  const normalizedLang = lang.toLowerCase();
29
 
30
  if (!supportedLangs.includes(normalizedLang)) {
 
34
  });
35
  }
36
 
 
37
  try {
38
+ const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
39
+ const executableCode = new AsyncFunction('chromium', 'devices', 'publicDir', 'path', code);
40
+
41
+ const result = await executableCode(chromium, devices, publicDir, path);
 
 
 
 
 
 
 
 
 
 
42
 
43
  const timestamp = Date.now();
44
+ const screenshotFiles = await fs.readdir(publicDir);
45
+ const recentFiles = screenshotFiles
46
+ .filter(f => f.startsWith('screenshot-') && f.endsWith('.png'))
47
+ .map(f => ({
48
+ name: f,
49
+ publicURL: `/files/${f}`
50
+ }));
51
 
52
  res.json({
53
  success: true,
54
  data: {
55
  result: result,
56
+ files: recentFiles,
57
+ timestamp: timestamp
 
 
58
  }
59
  });
60
 
61
  } catch (error) {
 
 
62
  res.status(500).json({
63
  success: false,
64
+ error: error.message,
65
+ stack: error.stack
66
  });
67
  }
68
  });