Code coverage report for sockjs-client/lib/helpers.js

Statements: 94.12% (64 / 68)      Branches: 82.76% (24 / 29)      Functions: 94.44% (17 / 18)      Lines: 94.12% (64 / 68)      Ignored: none     

All files » sockjs-client/lib/ » helpers.js
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174    1   1   1     1     1 1       1 65536     1 1 2852 2852   1   1     1 77     77 77 69     8 1     8 16           1   1 17         17 238     17                 42       14         14 2   14 14 14     14 14     14   1 14 14 14               13       13 13   13     13                   13     13       26 11   15         70       70     70       19       21 2   19 2       17   17           1  
'use strict';
 
var _all_protocols = ['websocket'];
 
var protocols = require('./protocols.js');
 
var helpers = {
 
    quote: (function () {
        var extra_escapable = /[\x00-\x1f\ud800-\udfff\ufffe\uffff\u0300-\u0333\u033d-\u0346\u034a-\u034c\u0350-\u0352\u0357-\u0358\u035c-\u0362\u0374\u037e\u0387\u0591-\u05af\u05c4\u0610-\u0617\u0653-\u0654\u0657-\u065b\u065d-\u065e\u06df-\u06e2\u06eb-\u06ec\u0730\u0732-\u0733\u0735-\u0736\u073a\u073d\u073f-\u0741\u0743\u0745\u0747\u07eb-\u07f1\u0951\u0958-\u095f\u09dc-\u09dd\u09df\u0a33\u0a36\u0a59-\u0a5b\u0a5e\u0b5c-\u0b5d\u0e38-\u0e39\u0f43\u0f4d\u0f52\u0f57\u0f5c\u0f69\u0f72-\u0f76\u0f78\u0f80-\u0f83\u0f93\u0f9d\u0fa2\u0fa7\u0fac\u0fb9\u1939-\u193a\u1a17\u1b6b\u1cda-\u1cdb\u1dc0-\u1dcf\u1dfc\u1dfe\u1f71\u1f73\u1f75\u1f77\u1f79\u1f7b\u1f7d\u1fbb\u1fbe\u1fc9\u1fcb\u1fd3\u1fdb\u1fe3\u1feb\u1fee-\u1fef\u1ff9\u1ffb\u1ffd\u2000-\u2001\u20d0-\u20d1\u20d4-\u20d7\u20e7-\u20e9\u2126\u212a-\u212b\u2329-\u232a\u2adc\u302b-\u302c\uaab2-\uaab3\uf900-\ufa0d\ufa10\ufa12\ufa15-\ufa1e\ufa20\ufa22\ufa25-\ufa26\ufa2a-\ufa2d\ufa30-\ufa6d\ufa70-\ufad9\ufb1d\ufb1f\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40-\ufb41\ufb43-\ufb44\ufb46-\ufb4e\ufff0-\uffff]/g,
            extra_lookup;
 
        function unroll_lookup(escapable) {
            var unrolled = {},
                c = [],
                i;
 
            for (i = 0; i < 65536; i++) {
                c.push(String.fromCharCode(i));
            }
 
            escapable.lastIndex = 0;
            c.join('').replace(escapable, function (a) {
                unrolled[ a ] = '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
                return '';
            });
            escapable.lastIndex = 0;
 
            return unrolled;
        }
 
        return function quote(string) {
            var quoted = JSON.stringify(string);
 
            // In most cases this should be very fast and good enough.
            extra_escapable.lastIndex = 0;
            if (!extra_escapable.test(quoted)) {
                return quoted;
            }
 
            if (!extra_lookup) {
                extra_lookup = unroll_lookup(extra_escapable);
            }
 
            return quoted.replace(extra_escapable, function(a) {
                return extra_lookup[a];
            });
        };
    }()),
 
    random_string: (function (){
        var random_string_chars = 'abcdefghijklmnopqrstuvwxyz012345';
 
        return function random_string(length) {
            var max = random_string_chars.length,
                bytes = require('crypto').randomBytes(length),
                ret = [],
                i;
 
            for (i = 0; i < length; i++) {
                ret.push( random_string_chars[bytes[i] % max] );
            }
 
            return ret.join('');
        };
    }()),
 
    closeFrame: function closeFrame(code, reason) {
        return 'c' + JSON.stringify([code, reason]);
    },
 
    userSetCode: function userSetCode(code) {
        return code === 1000 || (code >= 3000 && code <= 4999);
    },
 
    detectProtocols: function detectProtocols(probed, protocols_whitelist, info) {
        var pe = {},
            detected = [],
            protocol,
            i;
 
        if (!protocols_whitelist || !protocols_whitelist.length) {
            protocols_whitelist = _all_protocols;
        }
        for (i = 0; i < protocols_whitelist.length; i++) {
            protocol = protocols_whitelist[i];
            pe[protocol] = probed[protocol];
        }
        // 1. Websocket
        Eif (info.websocket !== false) {
            maybePush(['websocket']);
        }
 
        return detected;
 
        function maybePush(protos) {
            var proto = protos.shift();
            Eif (pe[proto]) {
                detected.push(proto);
            } else if (protos.length > 0) {
                maybePush(protos);
            }
        }
    },
 
    probeProtocols: function probeProtocols(url) {
        var probed = {},
            protocol,
            i;
 
        for (i = 0; i < _all_protocols.length; i++) {
            protocol = _all_protocols[i];
            // User can have a typo in protocol name.
            probed[protocol] = protocols[protocol] && protocols[protocol].enabled(url);
        }
 
        return probed;
    },
 
    countRTO: function countRTO(rtt) {
        // In a local environment, when using IE8/9 and the `jsonp-polling`
        // transport the time needed to establish a connection (the time that pass
        // from the opening of the transport to the call of `_dispatchOpen`) is
        // around 200msec (the lower bound used in the article above) and this
        // causes spurious timeouts. For this reason we calculate a value slightly
        // larger than that used in the article.
        Iif (rtt > 100) {
            return 4 * rtt; // rto > 400msec
        }
        return 300 + rtt;              // 300msec < rto <= 400msec
    },
 
    delay: function delay(ms, fn) {
        if ('function' === typeof fn) {
            setTimeout(fn, ms);
        } else {
            process.nextTick(ms);
        }
    },
 
    random_number: function random_number(max) {
        return Math.floor(Math.random() * max);
    },
 
    random_number_string: function random_number_string(max) {
        var t = ('' + (max - 1)).length,
            p = new Array(t + 1).join('0');
 
        return (p + helpers.random_number(max)).slice(-t);
    },
 
    flatUrl: function flatUrl(url) {
        return url.indexOf('?') === -1 && url.indexOf('#') === -1;
    },
 
    amendUrl: function amendUrl(url) {
        if (!url) {
            throw new Error('Wrong url for SockJS');
        }
        if (!helpers.flatUrl(url)) {
            throw new Error('Only basic urls are supported in SockJS');
        }
 
        // strip trailing slashes
        url = url.replace(/[/]+$/,'');
 
        return url;
    },
 
    log: console.log.bind(console)
};
 
module.exports = helpers;