Advertisement
SReject

JFM w/ async & timeouts & gzip (WIP)

Aug 26th, 2017
1,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     //*********************************//
  3.     //    Native Feature extensions    //
  4.     //*********************************//
  5.     Array.fromArguments = (function (slice) {
  6.         return function (args) {
  7.             return slice.call(args);
  8.         };
  9.     }(Array.prototype.slice));
  10.     Array.isArray = (function (toString) {
  11.         return function (arr) {
  12.             return "[object Array]" === toString.call(arr);
  13.         };
  14.     }(Object.prototype.toString));
  15.     Array.prototype.forEach = function (callback, thisArg) {
  16.         var index  = 0, length = this.length;
  17.         for (; index < length; index += 1) {
  18.             callback.call(thisArg, this[index], index, this);
  19.         }
  20.     };
  21.     Array.prototype.find = function (callback, thisArg) {
  22.         var index  = 0, length = this.length;
  23.         for (; index < length; index += 1) {
  24.             if (callback.call(thisArg, this[index], index, this)) {
  25.                 return this[index];
  26.             }
  27.         }
  28.     };
  29.     Array.prototype.reduce = function (callback, value) {
  30.         var index  = 0, length = this.length;
  31.         if (len > 0) {
  32.             if (arguments.length < 2) {
  33.                 value = this[0];
  34.                 index = 1;
  35.             }
  36.             for (; index < length; index += 1) {
  37.                 value = callback(value, this[index], index, this);
  38.             }
  39.         }
  40.         return value;
  41.     };
  42.     Object.hasOwnProperty = (function (hasOwnProperty) {
  43.         return function (obj, prop) {
  44.             return hasOwnProperty.call(obj, prop)
  45.         };
  46.     }(Object.prototype.hasOwnProperty));
  47.     Object.type = (function (toString) {
  48.         return function (input) {
  49.             if (input === undefined) {
  50.                 return 'undefined';
  51.             } else if (input === null) {
  52.                 return 'null';
  53.             } else {
  54.                 var res = toString.call(input).replace(/^\[(?:object )?(.+)\]$/, '$1');
  55.                 if (typeof input === 'unknown' && res.toLowerCase() === 'Object') {
  56.                     return 'unknown';
  57.                 } else if (/ /.test(res)) {
  58.                     return res;
  59.                 } else {
  60.                     return res.toLowerCase();
  61.                 }
  62.             }
  63.         }
  64.     }(Object.prototype.toString));
  65.     Object.keys = function (obj) {
  66.         var keys = [], prop;
  67.         for (prop in obj) {
  68.             if (Object.hasOwnProperty(obj, prop)) {
  69.                 keys.push(prop);
  70.             }
  71.         }
  72.         return keys;
  73.     };
  74.     Function.prototype.bind = (function (argumentsToArray, concat) {
  75.         return function () {
  76.             var self = this, bindArgs = argumentsToArray(arguments.length ? arguments : [undefined]), thisArg = bindArgs.shift();
  77.             return function () {
  78.                 self.apply(thisArg, concat.call(bindArgs, argumentsToArray(arguments)));
  79.             };
  80.         };
  81.     }(Array.fromArguments, Array.prototype.concat));
  82.     JSON = {
  83.         stringify: function (value) {
  84.             var type = Object.type(value);
  85.             if (value === null) {
  86.                 return 'null';
  87.             }
  88.             if (type === 'boolean') {
  89.                 return value.toString();
  90.             }
  91.             if (type === 'number') {
  92.                 return isFinite(value) ? value.toString() : 'null';
  93.             }
  94.             if (type === 'string') {
  95.                 return '"' + value.replace(/[\\"\u0000-\u001F\u2028\u2029]/g, function(chr) {
  96.                     return {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'}[chr] || '\\u' + (chr.charCodeAt(0) + 0x10000).toString(16).substr(1);
  97.                 }) + '"';
  98.             }
  99.             if (type === 'array') {
  100.                 return '[' + value.reduce(function (val, item) {
  101.                     item = JSON.stringify(item);
  102.                     if (item) {
  103.                         return val + (val ? ',' : '') + item;
  104.                     }
  105.                 }, '') + ']';
  106.             }
  107.             if (type !== 'function' && type !== 'undefined' && type !== 'unknown') {
  108.                 return '{' + Object.keys(value).reduce(function (val, item) {
  109.                     var res = JSON.stringify(value[key]);
  110.                     if (res) {
  111.                         return val + (val ? ',' : '') + JSON.stringify(key) + ':' + val;
  112.                     }
  113.                 }, '') + '}';
  114.             }
  115.         },
  116.         parse: function (value) {
  117.             value = String(value).replace(/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, function(chr) {
  118.                 return '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4);
  119.             });
  120.             if (!/^[\],:{}\s]*$/.test(value.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
  121.                 throw new Error("INVALID_JSON");
  122.             }
  123.             try {
  124.                 return eval('(' + value + ')');
  125.             } catch (e) {
  126.                 throw new Error("INVALID_JSON");
  127.             }  
  128.         }
  129.     };
  130.  
  131.    
  132.  
  133.     //**************************//
  134.     //    Binary Array Class    //
  135.     //**************************//
  136.     BinaryArray = function (fromCodepageMap, toCodepageMap) {
  137.         function BinaryArray(input) {
  138.             var output = [], type = Object.type(input), index = 0, charCode, nextCharCode, length, stream;
  139.             output.__proto__ = BinaryArray.prototype;
  140.             if (type === 'array') {
  141.                 input.forEach(function (item, idx) {
  142.                     if (Object.type(item) !== 'number' || item < 0 || parseInt(item, 10) !== item || item > 255) {
  143.                         throw TypeError("array must consist of only unsigned 8bit integer values");
  144.                     }
  145.                     output[idx] = item;
  146.                 });
  147.             } else if (type === 'string' || type === 'unknown') {
  148.                 stream = new ActiveXObject("ADODB.Stream");
  149.                 stream.mode = 3;
  150.                 stream.type = 1;
  151.                 stream.open();
  152.                 if (typeof input === 'string') {
  153.                     stream.charSet = 'utf-8';
  154.                     stream.type = 2;
  155.                     stream.writeText(input);
  156.                 } else {
  157.                     try {
  158.                         stream.write(input);
  159.                     } catch (ignore) {
  160.                         throw new TypeError("Cannot convert unknown to BinaryArray");
  161.                     }
  162.                 }
  163.                 stream.position = 0;
  164.                 stream.type     = 2;
  165.                 stream.charSet  = '437';
  166.                 input           = stream.readText();
  167.                 stream.close();
  168.                 for (length = input.length; index < length; index += 1) {
  169.                     charCode = input.charCodeAt(index);
  170.                     if (charCode > 127) {
  171.                         charCode = fromCodepageMap[charCode];
  172.                     }
  173.                     output.push(charCode);
  174.                 }
  175.             } else if (input !== undefined && input !== null) {
  176.                 throw new TypeError("Cannot convert " + type + " to BinaryArray");
  177.             }
  178.             return output;
  179.         }
  180.         BinaryArray.prototype = new Array;
  181.         BinaryArray.toString = function () {
  182.             var output, stream = new ActiveXObject("ADODB.Stream");
  183.             stream.mode = 3;
  184.             stream.type = 2;
  185.             stream.charSet = '437';
  186.             stream.open();
  187.             this.forEach(function (octet) {
  188.                 octet > 127 ? toCodepageMap[octet] : octet;
  189.                 stream.writeText(String.fromCharCode(octet));
  190.             });
  191.             stream.position = 0;
  192.             stream.charSet = 'utf-8';
  193.             output = stream.readText();
  194.             stream.close();
  195.             return output;
  196.         };
  197.         BinaryArray.toSafearray = function () {
  198.             var output, stream = new ActiveXObject("ADODB.Stream");
  199.             stream.mode = 3;
  200.             stream.type = 2;
  201.             stream.charSet = '437';
  202.             stream.open();
  203.             this.forEach(function (octet) {
  204.                 octet > 127 ? toCodepageMap[octet] : octet;
  205.                 stream.writeText(String.fromCharCode(octet));
  206.             });
  207.             stream.position = 0;
  208.             stream.type = 1;
  209.             output = stream.read();
  210.             stream.close();
  211.             return output;
  212.         };
  213.     }({
  214.         '199': 128, '252': 129, '233': 130, '226': 131, '228': 132, '224': 133, '229': 134, '231': 135, '234': 136, '235': 137,
  215.         '232': 138, '239': 139, '238': 140, '236': 141, '196': 142, '197': 143, '201': 144, '230': 145, '198': 146, '244': 147,
  216.         '246': 148, '242': 149, '251': 150, '249': 151, '255': 152, '214': 153, '220': 154, '162': 155, '163': 156, '165': 157,
  217.         '8359':158, '402': 159, '225': 160, '237': 161, '243': 162, '250': 163, '241': 164, '209': 165, '170': 166, '186': 167,
  218.         '191': 168, '8976':169, '172': 170, '189': 171, '188': 172, '161': 173, '171': 174, '187': 175, '9617':176, '9618':177,
  219.         '9619':178, '9474':179, '9508':180, '9569':181, '9570':182, '9558':183, '9557':184, '9571':185, '9553':186, '9559':187,
  220.         '9565':188, '9564':189, '9563':190, '9488':191, '9492':192, '9524':193, '9516':194, '9500':195, '9472':196, '9532':197,
  221.         '9566':198, '9567':199, '9562':200, '9556':201, '9577':202, '9574':203, '9568':204, '9552':205, '9580':206, '9575':207,
  222.         '9576':208, '9572':209, '9573':210, '9561':211, '9560':212, '9554':213, '9555':214, '9579':215, '9578':216, '9496':217,
  223.         '9484':218, '9608':219, '9604':220, '9612':221, '9616':222, '9600':223, '945': 224, '223': 225, '915': 226, '960': 227,
  224.         '931': 228, '963': 229, '181': 230, '964': 231, '934': 232, '920': 233, '937': 234, '948': 235, '8734':236, '966': 237,
  225.         '949': 238, '8745':239, '8801':240, '177': 241, '8805':242, '8804':243, '8992':244, '8993':245, '247': 246, '8776':247,
  226.         '176': 248, '8729':249, '183': 250, '8730':251, '8319':252, '178': 253, '9632':254, '160': 255
  227.     },{
  228.         '128':  199,'129':  252,'130':  233,'131':  226,'132':  228,'133':  224,'134':  229,'135':  231,'136':  234,'137':  235,
  229.         '138':  232,'139':  239,'140':  238,'141':  236,'142':  196,'143':  197,'144':  201,'145':  230,'146':  198,'147':  244,
  230.         '148':  246,'149':  242,'150':  251,'151':  249,'152':  255,'153':  214,'154':  220,'155':  162,'156':  163,'157':  165,
  231.         '158': 8359,'159':  402,'160':  225,'161':  237,'162':  243,'163':  250,'164':  241,'165':  209,'166':  170,'167':  186,
  232.         '168':  191,'169': 8976,'170':  172,'171':  189,'172':  188,'173':  161,'174':  171,'175':  187,'176': 9617,'177': 9618,
  233.         '178': 9619,'179': 9474,'180': 9508,'181': 9569,'182': 9570,'183': 9558,'184': 9557,'185': 9571,'186': 9553,'187': 9559,
  234.         '188': 9565,'189': 9564,'190': 9563,'191': 9488,'192': 9492,'193': 9524,'194': 9516,'195': 9500,'196': 9472,'197': 9532,
  235.         '198': 9566,'199': 9567,'200': 9562,'201': 9556,'202': 9577,'203': 9574,'204': 9568,'205': 9552,'206': 9580,'207': 9575,
  236.         '208': 9576,'209': 9572,'210': 9573,'211': 9561,'212': 9560,'213': 9554,'214': 9555,'215': 9579,'216': 9578,'217': 9496,
  237.         '218': 9484,'219': 9608,'220': 9604,'221': 9612,'222': 9616,'223': 9600,'224':  945,'225':  223,'226':  915,'227':  960,
  238.         '228':  931,'229':  963,'230':  181,'231':  964,'232':  934,'233':  920,'234':  937,'235':  948,'236': 8734,'237':  966,
  239.         '238':  949,'239': 8745,'240': 8801,'241':  177,'242': 8805,'243': 8804,'244': 8992,'245': 8993,'246':  247,'247': 8776,
  240.         '248':  176,'249': 8729,'250':  183,'251': 8730,'252': 8319,'253':  178,'254': 9632,'255':  160
  241.     });
  242.    
  243.  
  244.    
  245.     //**************************//
  246.     //    GZIP Decompression    //
  247.     //**************************//
  248.     gunzip = (function () {
  249.         var ORDER                   = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
  250.         var LENGTHCODETABLE         = [0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000d, 0x000f, 0x0011, 0x0013, 0x0017, 0x001b, 0x001f, 0x0023, 0x002b, 0x0033, 0x003b, 0x0043, 0x0053, 0x0063, 0x0073, 0x0083, 0x00a3, 0x00c3, 0x00e3, 0x0102, 0x0102, 0x0102];
  251.         var LENGTHEXTRATABLE        = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0];
  252.         var DISTCODETABLE           = [0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0007, 0x0009, 0x000d, 0x0011, 0x0019, 0x0021, 0x0031, 0x0041, 0x0061, 0x0081, 0x00c1, 0x0101, 0x0181, 0x0201, 0x0301, 0x0401, 0x0601, 0x0801, 0x0c01, 0x1001, 0x1801, 0x2001, 0x3001, 0x4001, 0x6001];
  253.         var DISTEXTRATABLE          = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];
  254.         var FIXEDLITERALLENGTHTABLE = [[459008,524368,524304,524568,459024,524400,524336,590016,459016,524384,524320,589984,524288,524416,524352,590048,459012,524376,524312,589968,459028,524408,524344,590032,459020,524392,524328,590000,524296,524424,524360,590064,459010,524372,524308,524572,459026,524404,524340,590024,459018,524388,524324,589992,524292,524420,524356,590056,459014,524380,524316,589976,459030,524412,524348,590040,459022,524396,524332,590008,524300,524428,524364,590072,459009,524370,524306,524570,459025,524402,524338,590020,459017,524386,524322,589988,524290,524418,524354,590052,459013,524378,524314,589972,459029,524410,524346,590036,459021,524394,524330,590004,524298,524426,524362,590068,459011,524374,524310,524574,459027,524406,524342,590028,459019,524390,524326,589996,524294,524422,524358,590060,459015,524382,524318,589980,459031,524414,524350,590044,459023,524398,524334,590012,524302,524430,524366,590076,459008,524369,524305,524569,459024,524401,524337,590018,459016,524385,524321,589986,524289,524417,524353,590050,459012,524377,524313,589970,459028,524409,524345,590034,459020,524393,524329,590002,524297,524425,524361,590066,459010,524373,524309,524573,459026,524405,524341,590026,459018,524389,524325,589994,524293,524421,524357,590058,459014,524381,524317,589978,459030,524413,524349,590042,459022,524397,524333,590010,524301,524429,524365,590074,459009,524371,524307,524571,459025,524403,524339,590022,459017,524387,524323,589990,524291,524419,524355,590054,459013,524379,524315,589974,459029,524411,524347,590038,459021,524395,524331,590006,524299,524427,524363,590070,459011,524375,524311,524575,459027,524407,524343,590030,459019,524391,524327,589998,524295,524423,524359,590062,459015,524383,524319,589982,459031,524415,524351,590046,459023,524399,524335,590014,524303,524431,524367,590078,459008,524368,524304,524568,459024,524400,524336,590017,459016,524384,524320,589985,524288,524416,524352,590049,459012,524376,524312,589969,459028,524408,524344,590033,459020,524392,524328,590001,524296,524424,524360,590065,459010,524372,524308,524572,459026,524404,524340,590025,459018,524388,524324,589993,524292,524420,524356,590057,459014,524380,524316,589977,459030,524412,524348,590041,459022,524396,524332,590009,524300,524428,524364,590073,459009,524370,524306,524570,459025,524402,524338,590021,459017,524386,524322,589989,524290,524418,524354,590053,459013,524378,524314,589973,459029,524410,524346,590037,459021,524394,524330,590005,524298,524426,524362,590069,459011,524374,524310,524574,459027,524406,524342,590029,459019,524390,524326,589997,524294,524422,524358,590061,459015,524382,524318,589981,459031,524414,524350,590045,459023,524398,524334,590013,524302,524430,524366,590077,459008,524369,524305,524569,459024,524401,524337,590019,459016,524385,524321,589987,524289,524417,524353,590051,459012,524377,524313,589971,459028,524409,524345,590035,459020,524393,524329,590003,524297,524425,524361,590067,459010,524373,524309,524573,459026,524405,524341,590027,459018,524389,524325,589995,524293,524421,524357,590059,459014,524381,524317,589979,459030,524413,524349,590043,459022,524397,524333,590011,524301,524429,524365,590075,459009,524371,524307,524571,459025,524403,524339,590023,459017,524387,524323,589991,524291,524419,524355,590055,459013,524379,524315,589975,459029,524411,524347,590039,459021,524395,524331,590007,524299,524427,524363,590071,459011,524375,524311,524575,459027,524407,524343,590031,459019,524391,524327,589999,524295,524423,524359,590063,459015,524383,524319,589983,459031,524415,524351,590047,459023,524399,524335,590015,524303,524431,524367,590079],9,7]
  255.         var FIXEDISTANCETABLE       = [[327680,327696,327688,327704,327684,327700,327692,327708,327682,327698,327690,327706,327686,327702,327694,null,327681,327697,327689,327705,327685,327701,327693,327709,327683,327699,327691,327707,327687,327703,327695,null],5,5];
  256.         var CRC32TABLE              = [0,1996959894,3993919788,2567524794,124634137,1886057615,3915621685,2657392035,249268274,2044508324,3772115230,2547177864,162941995,2125561021,3887607047,2428444049,498536548,1789927666,4089016648,2227061214,450548861,1843258603,4107580753,2211677639,325883990,1684777152,4251122042,2321926636,335633487,1661365465,4195302755,2366115317,997073096,1281953886,3579855332,2724688242,1006888145,1258607687,3524101629,2768942443,901097722,1119000684,3686517206,2898065728,853044451,1172266101,3705015759,2882616665,651767980,1373503546,3369554304,3218104598,565507253,1454621731,3485111705,3099436303,671266974,1594198024,3322730930,2970347812,795835527,1483230225,3244367275,3060149565,1994146192,31158534,2563907772,4023717930,1907459465,112637215,2680153253,3904427059,2013776290,251722036,2517215374,3775830040,2137656763,141376813,2439277719,3865271297,1802195444,476864866,2238001368,4066508878,1812370925,453092731,2181625025,4111451223,1706088902,314042704,2344532202,4240017532,1658658271,366619977,2362670323,4224994405,1303535960,984961486,2747007092,3569037538,1256170817,1037604311,2765210733,3554079995,1131014506,879679996,2909243462,3663771856,1141124467,855842277,2852801631,3708648649,1342533948,654459306,3188396048,3373015174,1466479909,544179635,3110523913,3462522015,1591671054,702138776,2966460450,3352799412,1504918807,783551873,3082640443,3233442989,3988292384,2596254646,62317068,1957810842,3939845945,2647816111,81470997,1943803523,3814918930,2489596804,225274430,2053790376,3826175755,2466906013,167816743,2097651377,4027552580,2265490386,503444072,1762050814,4150417245,2154129355,426522225,1852507879,4275313526,2312317920,282753626,1742555852,4189708143,2394877945,397917763,1622183637,3604390888,2714866558,953729732,1340076626,3518719985,2797360999,1068828381,1219638859,3624741850,2936675148,906185462,1090812512,3747672003,2825379669,829329135,1181335161,3412177804,3160834842,628085408,1382605366,3423369109,3138078467,570562233,1426400815,3317316542,2998733608,733239954,1555261956,3268935591,3050360625,752459403,1541320221,2607071920,3965973030,1969922972,40735498,2617837225,3943577151,1913087877,83908371,2512341634,3803740692,2075208622,213261112,2463272603,3855990285,2094854071,198958881,2262029012,4057260610,1759359992,534414190,2176718541,4139329115,1873836001,414664567,2282248934,4279200368,1711684554,285281116,2405801727,4167216745,1634467795,376229701,2685067896,3608007406,1308918612,956543938,2808555105,3495958263,1231636301,1047427035,2932959818,3654703836,1088359270,936918000,2847714899,3736837829,1202900863,817233897,3183342108,3401237130,1404277552,615818150,3134207493,3453421203,1423857449,601450431,3009837614,3294710456,1567103746,711928724,3020668471,3272380065,1510334235,755167117];
  257.         function buildHuffmanTable(lengths) {
  258.             var listSize      = lengths.length,
  259.                 maxCodeLength = 0,
  260.                 minCodeLength = Number.POSITIVE_INFINITY,
  261.                 size, table, bitLength, code, skip, reversed, rtemp, i, il, j, value;
  262.             for (i = 0, il = listSize; i < il; ++i) {
  263.                 if (lengths[i] > maxCodeLength) {
  264.                     maxCodeLength = lengths[i];
  265.                 }
  266.                 if (lengths[i] < minCodeLength) {
  267.                     minCodeLength = lengths[i];
  268.                 }
  269.             }
  270.             size = 1 << maxCodeLength;
  271.             table = new Array(size);
  272.             for (bitLength = 1, code = 0, skip = 2; bitLength <= maxCodeLength;) {
  273.                 for (i = 0; i < listSize; ++i) {
  274.                     if (lengths[i] === bitLength) {
  275.                         for (reversed = 0, rtemp = code, j = 0; j < bitLength; ++j) {
  276.                             reversed = (reversed << 1) | (rtemp & 1);
  277.                             rtemp >>= 1;
  278.                         }
  279.                         value = (bitLength << 16) | i;
  280.                         for (j = reversed; j < size; j += skip) {
  281.                             table[j] = value;
  282.                         }
  283.                         ++code;
  284.                     }
  285.                 }
  286.                 ++bitLength;
  287.                 code <<= 1;
  288.                 skip <<= 1;
  289.             }
  290.             return [table, maxCodeLength, minCodeLength];
  291.         }
  292.         function calcCRC32(data, pos, length) {
  293.             var i  = (typeof pos === 'number') ? pos : (pos = 0),
  294.                 il = (typeof length === 'number') ? length : data.length,
  295.                 crc = -1;
  296.             for (i = il & 7; i--; ++pos) {
  297.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos]) & 0xff];
  298.             }
  299.             for (i = il >> 3; i--; pos += 8) {
  300.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos    ]) & 0xff];
  301.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 1]) & 0xff];
  302.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 2]) & 0xff];
  303.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 3]) & 0xff];
  304.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 4]) & 0xff];
  305.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 5]) & 0xff];
  306.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 6]) & 0xff];
  307.                 crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 7]) & 0xff];
  308.             }
  309.             return (crc ^ 4294967295) >>> 0;
  310.         }
  311.         function RawInflate(input, options) {
  312.             this.buffer;
  313.             this.blocks     = [];
  314.             this.bufferSize = options.bufferSize || 32768;
  315.             this.totalpos   = 0;
  316.             this.ip         = options.index;
  317.             this.bitsbuf    = 0;
  318.             this.bitsbuflen = 0;
  319.             this.input      = input;
  320.             this.output     = new Array(this.bufferSize);
  321.             this.op         = 0;
  322.             this.bfinal     = false;
  323.             this.bufferType = 1;
  324.             this.resize     = false;
  325.         };
  326.         RawInflate.prototype.readBits = function(length) {
  327.             var bitsbuf     = this.bitsbuf,
  328.                 bitsbuflen  = this.bitsbuflen,
  329.                 input       = this.input,
  330.                 ip          = this.ip,
  331.                 inputLength = input.length,
  332.                 octet;
  333.             if (ip + ((length - bitsbuflen + 7) >> 3) >= inputLength) {
  334.                 throw new Error('input buffer is broken');
  335.             }
  336.             while (bitsbuflen < length) {
  337.                 bitsbuf |= input[ip++] << bitsbuflen;
  338.                 bitsbuflen += 8;
  339.             }
  340.             octet = bitsbuf & ((1 << length) - 1);
  341.             bitsbuf >>>= length;
  342.             bitsbuflen -= length;
  343.             this.bitsbuf = bitsbuf;
  344.             this.bitsbuflen = bitsbuflen;
  345.             this.ip = ip;
  346.             return octet;
  347.         };
  348.         RawInflate.prototype.readCodeByTable = function(table) {
  349.             var bitsbuf       = this.bitsbuf,
  350.                 bitsbuflen    = this.bitsbuflen,
  351.                 input         = this.input,
  352.                 ip            = this.ip,
  353.                 inputLength   = input.length,
  354.                 codeTable     = table[0],
  355.                 maxCodeLength = table[1],
  356.                 codeWithLength, codeLength;
  357.             while (bitsbuflen < maxCodeLength) {
  358.                 if (ip >= inputLength) {
  359.                     break;
  360.                 }
  361.                 bitsbuf |= input[ip++] << bitsbuflen;
  362.                 bitsbuflen += 8;
  363.             }
  364.             codeWithLength = codeTable[bitsbuf & ((1 << maxCodeLength) - 1)];
  365.             codeLength = codeWithLength >>> 16;
  366.             if (codeLength > bitsbuflen) {
  367.                 throw new Error('invalid code length: ' + codeLength);
  368.             }
  369.             this.bitsbuf = bitsbuf >> codeLength;
  370.             this.bitsbuflen = bitsbuflen - codeLength;
  371.             this.ip = ip;
  372.             return codeWithLength & 0xffff;
  373.         };
  374.         RawInflate.prototype.decodeHuffmanAdaptive = function(litlen, dist) {
  375.             var output           = this.output,
  376.                 op               = this.op,
  377.                 olength          = output.length,
  378.                 lengthCodeTable  = LENGTHCODETABLE,
  379.                 lengthExtraTable = LENGTHEXTRATABLE,
  380.                 distCodeTable    = DISTCODETABLE,
  381.                 distExtraTable   = DISTEXTRATABLE,
  382.                 code, ti, codeDist, codeLength;
  383.  
  384.             this.currentLitlenTable = litlen;
  385.             while ((code = this.readCodeByTable(litlen)) !== 256) {
  386.                 if (code < 256) {
  387.                     if (op >= olength) {
  388.                         output = this.expandBufferAdaptive();
  389.                         olength = output.length;
  390.                     }
  391.                     output[op++] = code;
  392.                     continue;
  393.                 }
  394.                 ti = code - 257;
  395.                 codeLength = lengthCodeTable[ti];
  396.                 if (lengthExtraTable[ti] > 0) {
  397.                     codeLength += this.readBits(lengthExtraTable[ti]);
  398.                 }
  399.                 code = this.readCodeByTable(dist);
  400.                 codeDist = distCodeTable[code];
  401.                 if (distExtraTable[code] > 0) {
  402.                     codeDist += this.readBits(distExtraTable[code]);
  403.                 }
  404.                 if (op + codeLength > olength) {
  405.                     output = this.expandBufferAdaptive();
  406.                     olength = output.length;
  407.                 }
  408.                 while (codeLength--) {
  409.                     output[op] = output[(op++) - codeDist];
  410.                 }
  411.             }
  412.             while (this.bitsbuflen >= 8) {
  413.                 this.bitsbuflen -= 8;
  414.                 this.ip--;
  415.             }
  416.             this.op = op;
  417.         };
  418.         RawInflate.prototype.expandBufferAdaptive = function(opt_param) {
  419.             var ratio  = (this.input.length / this.ip + 1) | 0,
  420.                 input  = this.input,
  421.                 output = this.output,
  422.                 maxHuffCode, newSize, maxInflateSize;
  423.             if (opt_param) {
  424.                 if (typeof opt_param.fixRatio === 'number') {
  425.                     ratio = opt_param.fixRatio;
  426.                 }
  427.                 if (typeof opt_param.addRatio === 'number') {
  428.                     ratio += opt_param.addRatio;
  429.                 }
  430.             }
  431.             if (ratio < 2) {
  432.                 maxHuffCode = (input.length - this.ip) / this.currentLitlenTable[2];
  433.                 maxInflateSize = (maxHuffCode / 2 * 258) | 0;
  434.                 newSize = maxInflateSize < output.length ?
  435.                 output.length + maxInflateSize :
  436.                 output.length << 1;
  437.             } else {
  438.                 newSize = output.length * ratio;
  439.             }
  440.             return this.output = output;
  441.         };
  442.         RawInflate.prototype.decompress = function () {
  443.             var input       = this.input,
  444.                 inputLength = this.input.length,
  445.                 ip          = this.ip,
  446.                 output      = this.output,
  447.                 olength     = this.output.length,
  448.                 op          = this.op,
  449.                 hdr, len, nlen, hlit, hdist, hclen, codeLengths, codeLengthsTable, litlenTable, distTable, lengthTable, code, prev, repeat, i, il;
  450.             while (!this.bfinal) {
  451.                 hdr = this.readBits(3);
  452.                 if (hdr & 0x1) {
  453.                     this.bfinal = true;
  454.                 }
  455.                 hdr >>>= 1;
  456.                 switch (hdr) {
  457.                     case 0: // uncompressed
  458.                         this.bitsbuf    = 0;
  459.                         this.bitsbuflen = 0;
  460.                         if (ip + 1 >= inputLength) {
  461.                             throw new Error('invalid uncompressed block header: LEN');
  462.                         }
  463.                         len = input[ip++] | (input[ip++] << 8);
  464.                         if (ip + 1 >= inputLength) {
  465.                             throw new Error('invalid uncompressed block header: NLEN');
  466.                         }
  467.                         nlen = input[ip++] | (input[ip++] << 8);
  468.                         if (len === ~nlen) {
  469.                             throw new Error('invalid uncompressed block header: length verify');
  470.                         }
  471.                         if (ip + len > input.length) { throw new Error('input buffer is broken'); }
  472.                         while (op + len > output.length) {
  473.                             output = this.expandBufferAdaptive({fixRatio: 2});
  474.                         }
  475.                         while (len--) {
  476.                             output[op++] = input[ip++];
  477.                         }
  478.                         this.ip = ip;
  479.                         this.op = op;
  480.                         this.output = output;
  481.                         break;
  482.                     case 1: // fixed huffman
  483.                         this.decodeHuffmanAdaptive(FIXEDLITERALLENGTHTABLE, FIXEDDISTANCETABLE);
  484.                         break;
  485.                     case 2: // dynamic huffman
  486.                         hlit = this.readBits(5) + 257;
  487.                         hdist = this.readBits(5) + 1;
  488.                         hclen = this.readBits(4) + 4;
  489.                         codeLengths = new Array(ORDER.length);
  490.                         for (i = 0; i < hclen; ++i) {
  491.                             codeLengths[ORDER[i]] = this.readBits(3);
  492.                         }
  493.                         for (i = hclen, hclen = codeLengths.length; i < hclen; ++i) {
  494.                             codeLengths[ORDER[i]] = 0;
  495.                         }
  496.                         codeLengthsTable = buildHuffmanTable(codeLengths);
  497.                         lengthTable = new Array(hlit + hdist);
  498.                         for (i = 0, il = hlit + hdist; i < il;) {
  499.                             code = this.readCodeByTable(codeLengthsTable);
  500.                             switch (code) {
  501.                                 case 16:
  502.                                     repeat = 3 + this.readBits(2);
  503.                                     while (repeat--) { lengthTable[i++] = prev; }
  504.                                     break;
  505.                                 case 17:
  506.                                     repeat = 3 + this.readBits(3);
  507.                                     while (repeat--) { lengthTable[i++] = 0; }
  508.                                     prev = 0;
  509.                                     break;
  510.                                 case 18:
  511.                                     repeat = 11 + this.readBits(7);
  512.                                     while (repeat--) { lengthTable[i++] = 0; }
  513.                                     prev = 0;
  514.                                     break;
  515.                                 default:
  516.                                     lengthTable[i++] = code;
  517.                                     prev = code;
  518.                                     break;
  519.                             }
  520.                         }
  521.                         litlenTable = buildHuffmanTable(lengthTable.slice(0, hlit));
  522.                         distTable   = buildHuffmanTable(lengthTable.slice(hlit));
  523.                         this.decodeHuffmanAdaptive(litlenTable, distTable);  
  524.                         break;
  525.                     default:
  526.                         throw new Error('unknown BTYPE: ' + hdr);
  527.                 }
  528.             }
  529.             if (this.output.length > op) {
  530.                 this.output.length = op;
  531.             }
  532.             return this.buffer = this.output;
  533.         };
  534.         return function gunzip(input) {
  535.             input = new BinaryArray(input);
  536.             var members = [], len = input.length, index = 0, flags, isize, inflated, data;
  537.             while (index < len) {
  538.                 if (input[index++] !== 0x1f || input[index++] !== 0x8b) {
  539.                   throw new Error('invalid signature');
  540.                 }
  541.                 if (input[index++] !== 8) {
  542.                     throw new Error("unknown compression method: " + input[index]);
  543.                 }
  544.                 flags = input[index++];
  545.                 index += 6;
  546.                 if ((flags & 4) > 0) {
  547.                     index += input[index++] | (input[index++] << 8);
  548.                 }
  549.                 if ((flags & 8) > 0) {
  550.                     index += 1;
  551.                     while (input[index] > 0) {
  552.                         index += 1;
  553.                     }
  554.                 }
  555.                 if (0 < (flags & 16)) {
  556.                     index += 1;
  557.                     while (input[index] > 0) {
  558.                         index += 1;
  559.                     }
  560.                 }
  561.                 if (0 < (flags & 2)) {
  562.                     if ((calcCRC32(input, 0, index) & 65535) !== (input[index++] | input[index++] << 8)) {
  563.                         throw new Error("invalid header crc16");
  564.                     }
  565.                 }
  566.                 isize = input[input.length - 4] | input[input.length - 3] << 8 | input[input.length - 2] << 16 | input[input.length - 1] << 24;
  567.                 inflated = new RawInflate(input, {index: index, bufferSize: input.length - index - 8 < 512 * isize ? isize : undefined});
  568.                 data     = inflated.decompress();
  569.                 index    = inflated.ip;
  570.  
  571.                 crc = (input[index++] | input[index++]<<8 | input[index++]<<16 | input[index++]<<24) >>> 0;
  572.                
  573.                 if (calcCRC32(data) !== crc) {
  574.                     throw new Error("invalid CRC-32 checksum");
  575.                 }
  576.                 isize = (input[index++] | input[index++]<<8 | input[index++]<<16 | input[index++] << 24) >>> 0;
  577.                 if ((data.length & 4294967295) !== isize) {
  578.                     throw new Error("invalid input size");
  579.                 }
  580.                 members.concat(data);
  581.             }
  582.             return new BinaryArray(members);
  583.         };
  584.     }());
  585.  
  586.  
  587.    
  588.     //**************************//
  589.     //    HTTP Request Class    //
  590.     //**************************//
  591.     HttpRequest = (function (xhr) {
  592.         if (!(xhr = xhr.find(function (testXHR, idx) {
  593.             try {
  594.                 new ActiveXObject(testXHR);
  595.                 return true;
  596.             } catch (e) {}
  597.         }))) {
  598.             return function () {
  599.                 throw new Error("HTTP Requester not found");
  600.             }
  601.         }
  602.         function forceHttpInit(self) {
  603.             if (self.reasyState !== 0) {
  604.                 throw new Error("HTTP not initializing");
  605.             }
  606.             return self;
  607.         }
  608.         function forceHttpDone(self) {
  609.             if (self.readyState !== 4) {
  610.                 throw new Error("HTTP request not completed");
  611.             }
  612.             return self;
  613.         }
  614.         function prepareHttp(async) {
  615.             var setDefHeaders = this.requestData ? !0 : !1,
  616.                 typeHeaderSet = false,
  617.                 lenHeadrSert  = false;
  618.  
  619.             this.request.open(this.requestMethod, this.requestUrl, async);
  620.             this.requestHeaders.forEach(function (header) {
  621.                 var name = header[0];
  622.                 this.request.setRequestHeader(name, header[2]);
  623.                 if (name === "content-type") {
  624.                     typeHeaderSet = true;
  625.                 }
  626.                 if (name === "content-length") {
  627.                     lenHeaderSet = true;
  628.                 }
  629.             });
  630.             if (setDefHeaders) {
  631.                 if (!typeHeaderSet) {
  632.                     this.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  633.                 }
  634.                 if (!lenHeaderSet) {
  635.                     this.setRequestHeader("Content-Length", this.requestData.length);
  636.                 }
  637.             }
  638.         }
  639.         function onReadyStateChange() {
  640.             var err, cb = this.callback || function(){};
  641.             this.readyState = this.request.readyState;
  642.             if (this.readyState === 4) {
  643.                 try {
  644.                     this.responseStatusCode = this.request.status;
  645.                     this.responseStatusText = this.request.statusText;
  646.                     this.responseHeaders    = this.request.getAllResponseHeaders().split('\r\n');
  647.                     this.responseBody       = new BinaryArray(this.request.responseBody);
  648.                     this.getResponseHeader("content-encoding").find(function (item) {
  649.                         if (/\x20*gzip\x20*/.test(item)) {
  650.                             this.responseBody = gunzip(this.responseBody);
  651.                             return true;
  652.                         }
  653.                     });
  654.                     this.responseText = this.responseBody.toString();
  655.                     this.request.onreadystatechange = null;
  656.                     delete this.request;
  657.                 } catch (e) {
  658.                     err = e;
  659.                 }
  660.                 cb(err);
  661.             }
  662.         }
  663.         function HttpRequest(method, url, wait, async, timeout) {
  664.             this.wait               = wait    || false;
  665.             this.async              = async   || false;
  666.             this.timeout            = timeout || 0;
  667.             this.error              = null;
  668.             this.readyState         = 0;
  669.             this.requestUrl         = url;
  670.             this.requestMethod      = method || "GET";
  671.             this.requestHeaders     = [];
  672.             this.requestData        = null;
  673.             this.responseStatusCode = 0;
  674.             this.responseStatusText = '';
  675.             this.responseHeaders    = [];
  676.             this.responseBody       = null;
  677.             this.responseText       = '';
  678.             if (wait !== true) {
  679.                 this.fetch();
  680.             }
  681.         }
  682.         HttpRequest.REQUESTER = xhr;
  683.         HttpRequest.prototype.getAsync = function () {
  684.             return this.async;
  685.         };
  686.         HttpRequest.prototype.setAsync = function (state) {
  687.             forceHttpInit(this).async = state;
  688.             return this;
  689.         };
  690.         HttpRequest.prototype.getTimeout = function () {
  691.             return this.timeout;
  692.         };
  693.         HttpRequest.prototype.setTimeout = function (timeout) {
  694.             forceHttpInit(this).timeout = (timeout === infinity || isNaN(timeout)) ? 0 : parseInt(timeout, 10);
  695.         };
  696.         HttpRequest.prototype.getReadyState = function () {
  697.             return this.readyState;
  698.         };
  699.         HttpRequest.prototype.getRequestUrl = function () {
  700.             return this.requestUrl;
  701.         }
  702.         HttpRequest.prototype.getRequestMethod = function () {
  703.             return this.requestMethod;
  704.         };
  705.         HttpRequest.prototype.setRequestMethod = function (method) {
  706.             forceHttpInit(this).requestMethod = method;
  707.         };
  708.         HttpRequest.prototype.getRequestHeaders = function () {
  709.             return this.requestHeaders;
  710.         };
  711.         HttpRequest.prototype.addRequestHeader = function (name, value) {
  712.             forceHttpInit(this).requestHeaders.push([name, value]);
  713.         };
  714.         HttpRequest.prototype.getRequestData = function () {
  715.             return this.requestData;
  716.         };
  717.         HttpRequest.prototype.setRequestData = function (data) {
  718.             forceHttpInit(this);
  719.             if (data === null && data === undefined) {
  720.                 this.requestData = null;
  721.             }
  722.             forceHttpInit(this).requestData = new BinaryArray(data);
  723.         };
  724.         HttpRequest.prototype.getResponseStatusCode = function () {
  725.             return forceHttpDone(this).responseStatusCode
  726.         };
  727.         HttpRequest.prototype.getResponseStatusCode = function () {
  728.             return forceHttpDone(this).responseStatusText
  729.         };
  730.         HttpRequest.prototype.getResponseHeaders = function () {
  731.             return forceHttpDone(this).responseHeaders.join('\r\n');
  732.         };
  733.         HttpRequest.prototype.getResponseHeader = function (name, index) {
  734.             var matchName = name.toLowerCase().replace(/(?:^\x20+)|(\x20+$)/g),
  735.                 headers = forceHttpDone(this).responseHeaders.reduce(function (val, header) {
  736.                     header = header.split(/\s*:\s*/);
  737.                     var hName  = String(header.shift()).toLowerCase();
  738.                     if (hName === matchName) {
  739.                         val.push(val);
  740.                     }
  741.                     return val;
  742.                 }, []);
  743.             if (!headers.length) {
  744.                 return [];
  745.             }
  746.             if (typeof index === 'number') {
  747.                 if (index < headers.length) {
  748.                     return [headers[index]];
  749.                 }
  750.             }
  751.             return headers;
  752.         };
  753.         HttpRequest.prototype.getResponseHead = function () {
  754.             return this.getResponseStatusCode() + ' ' + this.getResponseStatusText() + '\r\n' + this.getResponseHeaders();
  755.         };
  756.         HttpRequest.prototype.getResponseBody = function () {
  757.             return forceHttpDone(this).responseBody;
  758.         };
  759.         HttpRequest.prototype.getResponseText = function () {
  760.             forceHttpDone(this);
  761.             if (this.responseBody === null) {
  762.                 return '';
  763.             }
  764.             return this.responseBody.toString();
  765.         };
  766.         HttpRequest.prototype.getResponse = function () {
  767.             return this.getResponseHead() + '\r\n' + this.getResponseText;
  768.         };
  769.         HttpRequest.prototype.fetch = function (callback) {
  770.             var timeout;
  771.             this.callback = callback || function () {};
  772.             this.request = new ActiveXObject(HttpRequest.REQUESTER);
  773.             this.request.setTimeouts(Infinity, Infinity, Infinity, Infinity);
  774.             if (!this.timeout || this.timeout < 1) {
  775.                 prepaseHttp.call(this, this.async);
  776.                 if (this.async) {
  777.                     this.request.onreadystatechange = onreadystatechange.bind(this);
  778.                     this.request.send(this.requestData ? this.requestData.toSafearray() : null);
  779.                 } else {
  780.                     this.request.send(this.requestData ? this.requestData.toSafearray() : null);
  781.                     onreadystatechange.call(this);
  782.                 }
  783.             } else if (this.ascync === false) {
  784.                 timeout = (new Date()).getTime() = this.timeout;
  785.                 prepareHttp.call(this, true);
  786.                 this.request.send(this.requestData ? this.requestData.toSafearray() : null);
  787.                 do {
  788.                     if (this.request.readyState === 4) {
  789.                         break;
  790.                     }
  791.                     if (timeout <= (new Date()).getTime()) {
  792.                         this.readyState = 4;
  793.                         this.error = "HTTP_TIMEOUT"
  794.                         throw new Error("HTTP_TIMEOUT");
  795.                     }
  796.                     this.request.waitForResponse(1);
  797.                 } while (1);
  798.                 onreadystatechange.call(this);
  799.             } else {
  800.                 // TO-DO: async w/ timeout
  801.             }
  802.         };
  803.         return HttpRequest;
  804.     }(['MSXML2.SERVERXMLHTTP.6.0', 'MSXML2.SERVERXMLHTTP.3.0', 'MSXML2.SERVERXMLHTTP']));
  805.  
  806.    
  807.    
  808.     //***************************//
  809.     //    JSON Instance Class    //
  810.     //***************************//
  811.     // todo
  812.    
  813.    
  814.    
  815.     //*******************************//
  816.     //    HTML/XML Instance Class    //
  817.     //*******************************//
  818.     // todo
  819.    
  820.  
  821.  
  822. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement