0

I have a gzipped response from a web request that I need to decompress in JavaScript (actually, in the success function of an AJAX call - my code is running in a headless browser, and doesn't have the built-in gzip processing support offered by a full browser). I've been looking around for an answer but I'm a bit stumped.

Ideally, what I'd like to have code for is:

var my_decompressed_string = someGzipDecompressor(xhr.responseText);

I found, what I thought was an answer at JavaScript implementation of Gzip but that may not be the answer I was hoping for. When trying to use the mentioned jsxcompressor library by way of the following code snippet

var my_decompressed_string = JXG.decompress(xhr.responseText);

... I get ...

TypeError: 'undefined' is not an object (evaluating '(new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(str))).unzip()[0][0]')

Looking at that function in more detail, if I break up the code being executed by the decompress() function, I get what I assume is something good returned by the inner part ...

JXG.Util.Base64.decodeAsArray(xhr.responseText)

... but undefined returned for the outer part...

JXG.Util.Unzip( ... )

So, that may be a dead end of course, but if anyone knows a way that my original question should be possible, or has had any better luck with jsxcompressor.js I'd appreciate it.

Of course, I can force my AJAX request to return a 'deflate' response but the size of the page (for which I have no control over) is pretty large and requesting gzip is an attempt at speeding up my page load time.

Community
  • 1
  • 1
timecode
  • 187
  • 3
  • 12
  • First thing to do is dump xhr.responseText to the console and see what it is and what its format is – Radiotrib Mar 23 '13 at 11:49
  • 1
    So the response is gzipped, but your browser doesn't decompress that transparently? Why not? – robertklep Mar 23 '13 at 11:52
  • That's what I was wondering ... I think the OP is trying to gunzip something that the browser has already done for him ... but it still doesn't resolve the 'undefined' error. Looking at the data request/response Firebug Net tab and checking the returned value and headers might be the start point in this case. – Radiotrib Mar 23 '13 at 11:58
  • Forgot to mention that my code is running in a headless browser, and doesn't have the built-in gzip processing support that a full browser has :-/ Hence, having to do it myself :-) Console output of the gzipped xhr.responseText is a lot of strange characters. – timecode Mar 23 '13 at 13:23

1 Answers1

1

jsxcompressor.js requires base64 encoded string to decompress, you should use:

  var my_decompressed_string = JXG.decompress(btoa(xhr.responseText));

if your headless browser does not support btoa then you should use a base64 encoding library, if your node or iojs there are plenty of base64 npm packages.

user2144406
  • 300
  • 6
  • 10
  • Many thanks. I thought it was due to the base64 encoding but couldn't see where/how to deal with it. I almost had it ! – timecode Jan 05 '16 at 13:27