summaryrefslogtreecommitdiff
path: root/scripts/lib/net.js
blob: b2c6cfa8ea87dfe9eb6977a2f5c6d185b8ddf4d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(function() {
    var tldEnd = new RegExp("\\.tld$");
    Object.defineProperties(net, {
        /**
         * Checks if two domain matches checking for .tld which matches all top level
         * domains 
         * @name domainMatch
         * @memberOf net 
         * @function 
         *
         * @param {String} domain
         *      The domain to test 
         * @param {String} match 
         *      A domain that may contain .tld as top level domain
         *
         * @returns {Boolean}
         *      Whether the domains match
         * 
         * @example 
         * net.domainMatch("example.com", "example.tld"); // true
         * net.domainMatch("example.com", "example.org"); // false
         * */
        "domainMatch" : 
        {
            value : function(domain, match) {
                var result = false;
                if (tldEnd.test(match))
                {
                    return domain.substring(0, domain.indexOf(".")) === match.substring(0, match.indexOf("."));
                }
                else 
                {
                    return domain === match;
                }
            }
        }, 
        /**
         * Checks if hostnames match checking for .tld which matches all top level
         * domains 
         * @name hostMatch
         * @memberOf net 
         * @function 
         *
         * @param {String} domain
         *      The host to test 
         * @param {String} match 
         *      A host name that may contain .tld as top level domain
         *
         * @returns {Boolean}
         *      Whether the hosts match
         * @example 
         * net.hostMatch("www.example.com", "www.example.tld"); // true
         * net.hostMatch("www.example.com", "example.tld"); // false
         * net.hostMatch("www.example.com", "www.example.com"); // false
         * */
        "hostMatch" : 
        {
            value : function(host, match) {
                if (tldEnd.test(match))
                {
                    var domain = net.domainFromHost(host);
                    if (domain == host)
                        return net.domainMatch(host, match);

                    var domainStart = host.indexOf(domain);
                    if (host.substring(0, domainStart) != match.substring(0, domainStart))
                        return false;

                    return net.domainMatch(host.indexOf(domainStart), match.indexOf(domainStart));
                }
                else 
                {
                    return host == match;
                }
            }
        }, 
    });
    Object.freeze(net);
})();