You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

4 years ago
  1. 'use strict';
  2. var debug = function() {};
  3. if (process.env.NODE_ENV !== 'production') {
  4. debug = require('debug')('sockjs-client:utils:transport');
  5. }
  6. module.exports = function(availableTransports) {
  7. return {
  8. filterToEnabled: function(transportsWhitelist, info) {
  9. var transports = {
  10. main: []
  11. , facade: []
  12. };
  13. if (!transportsWhitelist) {
  14. transportsWhitelist = [];
  15. } else if (typeof transportsWhitelist === 'string') {
  16. transportsWhitelist = [transportsWhitelist];
  17. }
  18. availableTransports.forEach(function(trans) {
  19. if (!trans) {
  20. return;
  21. }
  22. if (trans.transportName === 'websocket' && info.websocket === false) {
  23. debug('disabled from server', 'websocket');
  24. return;
  25. }
  26. if (transportsWhitelist.length &&
  27. transportsWhitelist.indexOf(trans.transportName) === -1) {
  28. debug('not in whitelist', trans.transportName);
  29. return;
  30. }
  31. if (trans.enabled(info)) {
  32. debug('enabled', trans.transportName);
  33. transports.main.push(trans);
  34. if (trans.facadeTransport) {
  35. transports.facade.push(trans.facadeTransport);
  36. }
  37. } else {
  38. debug('disabled', trans.transportName);
  39. }
  40. });
  41. return transports;
  42. }
  43. };
  44. };