tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gzipped.sjs (1339B)


      1 "use strict";
      2 
      3 function gzipCompressString(string, obs) {
      4   const scs = Cc["@mozilla.org/streamConverters;1"].getService(
      5     Ci.nsIStreamConverterService
      6   );
      7   const listener = Cc["@mozilla.org/network/stream-loader;1"].createInstance(
      8     Ci.nsIStreamLoader
      9   );
     10   listener.init(obs);
     11   const converter = scs.asyncConvertData(
     12     "uncompressed",
     13     "gzip",
     14     listener,
     15     null
     16   );
     17   const stringStream = Cc[
     18     "@mozilla.org/io/string-input-stream;1"
     19   ].createInstance(Ci.nsIStringInputStream);
     20   stringStream.setByteStringData(string);
     21   converter.onStartRequest(null, null);
     22   converter.onDataAvailable(null, stringStream, 0, string.length);
     23   converter.onStopRequest(null, null, null);
     24 }
     25 
     26 const ORIGINAL_JS_CONTENT = `console.log("original javascript content");`;
     27 
     28 function handleRequest(request, response) {
     29   response.processAsync();
     30 
     31   // Generate data
     32   response.setHeader("Content-Type", "application/javascript", false);
     33   response.setHeader("Content-Encoding", "gzip", false);
     34 
     35   const observer = {
     36     onStreamComplete(loader, context, status, length, result) {
     37       const buffer = String.fromCharCode.apply(this, result);
     38       response.setHeader("Content-Length", "" + buffer.length, false);
     39       response.write(buffer);
     40       response.finish();
     41     },
     42   };
     43   gzipCompressString(ORIGINAL_JS_CONTENT, observer);
     44 }