Skip to content

Commit

Permalink
Apply PR from karlr42:
Browse files Browse the repository at this point in the history
  • Loading branch information
Alamantus authored and robbie-inquisicorp committed Dec 3, 2019
1 parent 2230d7e commit 3fc3e19
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
46 changes: 42 additions & 4 deletions src/JSONC.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return binary;
}(zipped));
} else {
throw (e);
}
}
return Base64.encode(data);
};
/**
* Decompress a compressed JSON
Expand Down Expand Up @@ -307,9 +327,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 < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return binary;
}(unzipped));
} else {
throw (e);
}
}
var json = JSON.parse(str);
return bDecompress ? JSONC.decompress(json) : json;
};
/*
Expand Down
26 changes: 26 additions & 0 deletions test/JSONC.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,32 @@ describe('JSONC.decompress', function (){

expect(retrieved).toEqual(obj);

delete window.Base64;
delete window.gzip;
});
it('should test decompressing a large object', function () {
var retrieved,
obj = largeTestObj.origData,
packed = largeTestObj.gzData;

window.gzip = {
unzip: function () {
/*
* need to split the array in two because PhantomJS can't even parse the full size
*/
return largeTestObj.gunzippedData_0.concat(largeTestObj.gunzippedData_1);
}
};
window.Base64 = {
decode: function (str) {
return str;
}
};

retrieved = JSONC.unpack(packed);

expect(retrieved).toEqual(obj);

delete window.Base64;
delete window.gzip;
});
Expand Down
1 change: 0 additions & 1 deletion vendor/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down

0 comments on commit 3fc3e19

Please sign in to comment.