title: How to decompress gzip xhr response in javascript
body: <p>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.</p>
  
  <p>Ideally, what I'd like to have code for is:</p>
  
  <pre><code>var my_decompressed_string = someGzipDecompressor(xhr.responseText);
  </code></pre>
  
  <p>I found, what I thought was an answer at <a href="https://stackoverflow.com/questions/294297/javascript-implementation-of-gzip">JavaScript implementation of Gzip</a> 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</p>
  
  <pre><code>var my_decompressed_string = JXG.decompress(xhr.responseText);
  </code></pre>
  
  <p>... I get ...</p>
  
  <pre><code>TypeError: 'undefined' is not an object (evaluating '(new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(str))).unzip()[0][0]')
  </code></pre>
  
  <p>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 ...</p>
  
  <pre><code>JXG.Util.Base64.decodeAsArray(xhr.responseText)
  </code></pre>
  
  <p>... but <code>undefined</code> returned for the outer part...</p>
  
  <pre><code>JXG.Util.Unzip( ... )
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>