From a4614ecded6526be12e89720b937bc1e487b303f Mon Sep 17 00:00:00 2001 From: karlr42 Date: Thu, 2 Oct 2014 15:01:43 +0000 Subject: [PATCH 1/3] If a RangeError is thrown when calling apply() in JSONC.unzip, use a loop instead. --- src/JSONC.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/JSONC.js b/src/JSONC.js index 8372433..8dca354 100644 --- a/src/JSONC.js +++ b/src/JSONC.js @@ -307,9 +307,27 @@ * @returns {Object} */ JSONC.unpack = function (gzipped, bDecompress) { - var aArr = getArr(Base64.decode(gzipped)), - str = String.fromCharCode.apply(String, gzip.unzip(aArr,{level:9})), - json = JSON.parse(str); + var aArr = getArr(Base64.decode(gzipped)); + var str, unzipped = gzip.unzip(aArr,{level:9}); + try{ + str = String.fromCharCode.apply(String, unzipped); + }catch(e){ + if(e instanceof RangeError){ + //Hit the max number of arguments for the JS engine + str = function(buffer) { + var binary = ''; + var bytes = new Uint8Array(buffer); + var len = bytes.byteLength; + for (var i=0; i Date: Fri, 10 Oct 2014 13:52:49 +0000 Subject: [PATCH 2/3] Remove a line-ending substitution that can lead to corruption of data, causing random failures in decompression of such data. --- vendor/base64.js | 1 - 1 file changed, 1 deletion(-) diff --git a/vendor/base64.js b/vendor/base64.js index a8761aa..b8ffab5 100644 --- a/vendor/base64.js +++ b/vendor/base64.js @@ -83,7 +83,6 @@ var Base64 = { // private method for UTF-8 encoding _utf8_encode : function (string) { - string = string.replace(/\r\n/g,"\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { From 9b176457ccf2b9e5a61e5c4701478f89e922bc1a Mon Sep 17 00:00:00 2001 From: karlr42 Date: Mon, 13 Oct 2014 13:36:10 +0000 Subject: [PATCH 3/3] Add unit tests for large object decompression. --- karma.conf.js | 2 +- src/JSONC.js | 26 +- test/JSONC.js | 28 + test/largeData.js | 248180 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 248232 insertions(+), 4 deletions(-) create mode 100644 test/largeData.js diff --git a/karma.conf.js b/karma.conf.js index 2745ce9..715efa9 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -52,7 +52,7 @@ module.exports = function(config) { // - PhantomJS // - IE (only Windows) // CLI --browsers Chrome,Firefox,Safari - browsers: ['PhantomJS'], + browsers: ['Chrome'], // If browser does not capture in given timeout [ms], kill it // CLI --capture-timeout 5000 diff --git a/src/JSONC.js b/src/JSONC.js index 8dca354..a33b989 100644 --- a/src/JSONC.js +++ b/src/JSONC.js @@ -272,7 +272,27 @@ */ JSONC.pack = function (json, bCompress) { var str = JSON.stringify((bCompress ? JSONC.compress(json) : json)); - return Base64.encode(String.fromCharCode.apply(String, gzip.zip(str,{level:9}))); + var zipped = gzip.zip(str,{level:9}); + var data; + try{ + data = String.fromCharCode.apply(String,zipped); + }catch(e){ + if(e instanceof RangeError){ + //Hit the max number of arguments for the JS engine + data = (function(buffer) { + var binary = ''; + var bytes = new Uint8Array(buffer); + var len = bytes.byteLength; + for (var i=0; i