getSocketServerImplementation.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. function getSocketServerImplementation(options) {
  3. let ServerImplementation;
  4. let serverImplFound = true;
  5. switch (typeof options.transportMode.server) {
  6. case 'string':
  7. // could be 'sockjs', in the future 'ws', or a path that should be required
  8. if (options.transportMode.server === 'sockjs') {
  9. ServerImplementation = require('../servers/SockJSServer');
  10. } else if (options.transportMode.server === 'ws') {
  11. ServerImplementation = require('../servers/WebsocketServer');
  12. } else {
  13. try {
  14. // eslint-disable-next-line import/no-dynamic-require
  15. ServerImplementation = require(options.transportMode.server);
  16. } catch (e) {
  17. serverImplFound = false;
  18. }
  19. }
  20. break;
  21. case 'function':
  22. // potentially do more checks here to confirm that the user implemented this properlly
  23. // since errors could be difficult to understand
  24. ServerImplementation = options.transportMode.server;
  25. break;
  26. default:
  27. serverImplFound = false;
  28. }
  29. if (!serverImplFound) {
  30. throw new Error(
  31. "transportMode.server must be a string denoting a default implementation (e.g. 'sockjs', 'ws'), a full path to " +
  32. 'a JS file which exports a class extending BaseServer (webpack-dev-server/lib/servers/BaseServer) ' +
  33. 'via require.resolve(...), or the class itself which extends BaseServer'
  34. );
  35. }
  36. return ServerImplementation;
  37. }
  38. module.exports = getSocketServerImplementation;