title: Reading an LZ4 compressed text file (mozlz4) in WebExtensions (JavaScript, Firefox)
body: <p>I'm porting a Firefox Add-on SDK extension to WebExtensions. Previously I could access the browser's search engines, but now I can't, so a helpful user suggested I try reading the search.json.mozlz4 file, which has every installed engine. However, this file is json with LZ4 compression, and it's in Mozilla's <a href="https://dxr.mozilla.org/mozilla-central/rev/bfa85d23df57c8a1db17c99b267667becc1c4afd/toolkit/components/lz4/lz4.js" rel="nofollow noreferrer">own LZ4 format</a>, with a custom magic number, 'mozLz40\0'.</p>
  
  <p>Before, one could use this to read a text file that uses LZ4 compression, including a mozlz4 file:</p>
  
  <pre><code>let bytes = OS.File.read(path, { compression: "lz4" });
  let content = new TextDecoder().decode(bytes);
  </code></pre>
  
  <p>(although I couldn't find documentation about the "compression" field, it works)</p>
  
  <p>Now, using WebExtensions, the best I could come up with to read a file is</p>
  
  <pre><code>var reader = new FileReader();
  reader.readAsText(file);
  reader.onload = function(ev) {
      let content = ev.target.result;
  };
  </code></pre>
  
  <p>This does not handle compression in any way. <a href="https://github.com/pierrec/node-lz4" rel="nofollow noreferrer">This library</a> handles LZ4<s>, but it is for node.js so I can't use that.</s> [edit: it works standalone too]. However, even if I remove the custom magic number processing I can't get it to decompress the file, while this Python code, in comparison, works as expected:</p>
  
  <pre><code>import lz4
  file_obj = open("search.json.mozlz4", "rb")
  if file_obj.read(8) != b"mozLz40\0":
      raise InvalidHeader("Invalid magic number")
  print(lz4.block.decompress(file_obj.read()))
  </code></pre>
  
  <p>How can I do this in JS?</p>