getSocketServerImplementation.js 1.4 KB

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