tor-browser

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

json_multipart.sjs (867B)


      1 "use strict";
      2 
      3 const BOUNDARY = "boundary";
      4 const JSON = `Content-Type: application/vnd.mozilla.json.view
      5 
      6 { "hello": "world" }`;
      7 
      8 const HTML = `Content-Type: text/html
      9 
     10 <html>
     11   <body>Test multipart</body>
     12 </html>`;
     13 
     14 async function handleRequest(request, response) {
     15   response.processAsync();
     16 
     17   response.setHeader(
     18     "Content-Type",
     19     `multipart/x-mixed-replace; boundary=${BOUNDARY}`,
     20     false
     21   );
     22   response.setStatusLine(request.httpVersion, "200", "OK");
     23 
     24   response.write(`--${BOUNDARY}\n`);
     25   response.write(`${JSON}\n`);
     26   response.write(`--${BOUNDARY}\n`);
     27 
     28   await sleep(1000);
     29 
     30   response.write(`${HTML}\n`);
     31   response.write(`--${BOUNDARY}--\n`);
     32   response.finish();
     33 }
     34 
     35 function sleep(delay) {
     36   return new Promise(res =>
     37     Cc["@mozilla.org/timer;1"]
     38       .createInstance(Ci.nsITimer)
     39       .init(res, delay, Ci.nsITimer.TYPE_ONE_SHOT)
     40   );
     41 }