playwright-server 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env node
  2. const type = (arg =>
  3. arg == 'firefox' ? 'firefox' : arg == 'webkit' ? 'webkit' : 'chromium'
  4. )(process.argv[1]);
  5. const wsUrl = require('../config.json').playwright_ws_spec_endpoint ||
  6. require('../config.default.json').playwright_ws_spec_endpoint;
  7. const axios = require('axios').default;
  8. const playwright = require('playwright');
  9. axios.get('http://localhost:9222').then(res => {
  10. if (res.status !== 426) {
  11. console.error(Error('port 9222 is in use but not a websocket'));
  12. return setTimeout(() => process.exit(1), 30000);
  13. }
  14. return axios.get(wsUrl).then(jsonres => {
  15. const endpoint = jsonres.data[type];
  16. if (!endpoint) {
  17. console.error(Error('url does not return the required browser type'));
  18. return setTimeout(() => process.exit(1), 30000);
  19. }
  20. if (typeof endpoint !== 'string') throw Error('returned endpoint url is malformed');
  21. return playwright[type].connect(endpoint).catch(() => { throw Error('connection error'); });
  22. });
  23. }).catch(err => {
  24. if (!axios.isAxiosError(err)) console.log(err);
  25. playwright[type].launchServer({args: process.argv.slice(2), port: 9222}).then(svr =>
  26. require('http').createServer((req, res) => {
  27. const json = req.url === '/playwright-ws.json' &&
  28. JSON.stringify(Object.fromEntries([[type, svr.wsEndpoint()]]));
  29. if (json) { res.write(json); console.log(json); res.end(); }
  30. else res.destroy();
  31. }).listen(8080)
  32. );
  33. });