1
|
'use strict';
|
2
|
|
3
|
var parse = require('url-parse');
|
4
|
|
5
|
/**
|
6
|
* Transform an URL to a valid origin value.
|
7
|
*
|
8
|
* @param {String|Object} url URL to transform to it's origin.
|
9
|
* @returns {String} The origin.
|
10
|
* @api public
|
11
|
*/
|
12
|
function origin(url) {
|
13
|
if ('string' === typeof url) url = parse(url);
|
14
|
|
15
|
//
|
16
|
// 6.2. ASCII Serialization of an Origin
|
17
|
// http://tools.ietf.org/html/rfc6454#section-6.2
|
18
|
//
|
19
|
if (!url.protocol || !url.hostname) return 'null';
|
20
|
|
21
|
//
|
22
|
// 4. Origin of a URI
|
23
|
// http://tools.ietf.org/html/rfc6454#section-4
|
24
|
//
|
25
|
// States that url.scheme, host should be converted to lower case. This also
|
26
|
// makes it easier to match origins as everything is just lower case.
|
27
|
//
|
28
|
return (url.protocol +'//'+ url.host).toLowerCase();
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Check if the origins are the same.
|
33
|
*
|
34
|
* @param {String} a URL or origin of a.
|
35
|
* @param {String} b URL or origin of b.
|
36
|
* @returns {Boolean}
|
37
|
* @api public
|
38
|
*/
|
39
|
origin.same = function same(a, b) {
|
40
|
return origin(a) === origin(b);
|
41
|
};
|
42
|
|
43
|
//
|
44
|
// Expose the origin
|
45
|
//
|
46
|
module.exports = origin;
|