1/**
2 * Create an absolute URL from `options` and defaulting unspecified properties to `window.location`.
3 * @param {Object} options - a `Location`-like object
4 * @param {string} options.hostname
5 * @param {string} options.subdomain - prepend subdomain to the hostname
6 * @param {string} options.port
7 * @param {string} options.path
8 * @param {string} options.query
9 * @param {string} options.hash
10 * @returns {string}
11 */
12function make_absolute_url(options) {
13    var loc = window.location;
14    var protocol = get(options, "protocol", loc.protocol);
15    if (protocol[protocol.length - 1] != ":") {
16        protocol += ":";
17    }
18
19    var hostname = get(options, "hostname", loc.hostname);
20
21    var subdomain = get(options, "subdomain");
22    if (subdomain) {
23        hostname = subdomain + "." + hostname;
24    }
25
26    var port = get(options, "port", loc.port)
27    var path = get(options, "path", loc.pathname);
28    var query = get(options, "query", loc.search);
29    var hash = get(options, "hash", loc.hash)
30
31    var url = protocol + "//" + hostname;
32    if (port) {
33        url += ":" + port;
34    }
35
36    if (path[0] != "/") {
37        url += "/";
38    }
39    url += path;
40    if (query) {
41        if (query[0] != "?") {
42            url += "?";
43        }
44        url += query;
45    }
46    if (hash) {
47        if (hash[0] != "#") {
48            url += "#";
49        }
50        url += hash;
51    }
52    return url;
53}
54
55/** @private */
56function get(obj, name, default_val) {
57    if (obj.hasOwnProperty(name)) {
58        return obj[name];
59    }
60    return default_val;
61}
62
63/**
64 * Generate a new UUID.
65 * @returns {string}
66 */
67function token() {
68    var uuid = [to_hex(rand_int(32), 8),
69                to_hex(rand_int(16), 4),
70                to_hex(0x4000 | rand_int(12), 4),
71                to_hex(0x8000 | rand_int(14), 4),
72                to_hex(rand_int(48), 12)].join("-")
73    return uuid;
74}
75
76/** @private */
77function rand_int(bits) {
78    if (bits < 1 || bits > 53) {
79        throw new TypeError();
80    } else {
81        if (bits >= 1 && bits <= 30) {
82            return 0 | ((1 << bits) * Math.random());
83        } else {
84            var high = (0 | ((1 << (bits - 30)) * Math.random())) * (1 << 30);
85            var low = 0 | ((1 << 30) * Math.random());
86            return  high + low;
87        }
88    }
89}
90
91/** @private */
92function to_hex(x, length) {
93    var rv = x.toString(16);
94    while (rv.length < length) {
95        rv = "0" + rv;
96    }
97    return rv;
98}
99