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.

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. const assert = require('bsert');
  3. const URL = require('url');
  4. exports.parseURL = function parseURL(url) {
  5. if (url.indexOf('://') === -1)
  6. url = `ws://${url}`;
  7. const data = URL.parse(url);
  8. if (data.protocol !== 'http:'
  9. && data.protocol !== 'https:'
  10. && data.protocol !== 'ws:'
  11. && data.protocol !== 'wss:') {
  12. throw new Error('Invalid protocol for websocket URL.');
  13. }
  14. if (!data.hostname)
  15. throw new Error('Malformed URL.');
  16. const host = data.hostname;
  17. let port = 80;
  18. let ssl = false;
  19. if (data.protocol === 'https:' || data.protocol === 'wss:') {
  20. port = 443;
  21. ssl = true;
  22. }
  23. if (data.port) {
  24. port = parseInt(data.port, 10);
  25. assert((port & 0xffff) === port);
  26. assert(port !== 0);
  27. }
  28. return [port, host, ssl];
  29. };