url.js 975 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. var URL = require('url-parse');
  3. var debug = function() {};
  4. if (process.env.NODE_ENV !== 'production') {
  5. debug = require('debug')('sockjs-client:utils:url');
  6. }
  7. module.exports = {
  8. getOrigin: function(url) {
  9. if (!url) {
  10. return null;
  11. }
  12. var p = new URL(url);
  13. if (p.protocol === 'file:') {
  14. return null;
  15. }
  16. var port = p.port;
  17. if (!port) {
  18. port = (p.protocol === 'https:') ? '443' : '80';
  19. }
  20. return p.protocol + '//' + p.hostname + ':' + port;
  21. }
  22. , isOriginEqual: function(a, b) {
  23. var res = this.getOrigin(a) === this.getOrigin(b);
  24. debug('same', a, b, res);
  25. return res;
  26. }
  27. , isSchemeEqual: function(a, b) {
  28. return (a.split(':')[0] === b.split(':')[0]);
  29. }
  30. , addPath: function (url, path) {
  31. var qs = url.split('?');
  32. return qs[0] + path + (qs[1] ? '?' + qs[1] : '');
  33. }
  34. , addQuery: function (url, q) {
  35. return url + (url.indexOf('?') === -1 ? ('?' + q) : ('&' + q));
  36. }
  37. };