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