tor-browser

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

sw_serverupdate.sjs (931B)


      1 "use strict";
      2 
      3 const BODY = `
      4 function err(s) {
      5   dump("ERROR: " + s + "\\n");
      6   throw new Error(s);
      7 }
      8 
      9 function checkNumClients(actual, expected) {
     10   if (actual != expected) {
     11     let s = 'Expected ' + expected + ' clients, found ' + actual;
     12     err(s);
     13   }
     14 }
     15 
     16 var updateCount = 0;
     17 
     18 registration.onupdatefound = (e) => {
     19   clients.matchAll().then((clients) => {
     20     switch (updateCount) {
     21       case 0: checkNumClients(clients.length, 0); break;
     22       case 1: checkNumClients(clients.length, 1); break;
     23       default: err("Too many updates, sorry."); break;
     24     }
     25     updateCount++;
     26 
     27     if (clients.length) {
     28       clients[0].postMessage("updatefound");
     29     }
     30   });
     31 }
     32 `;
     33 
     34 function handleRequest(request, response) {
     35   // This header is necessary for making this script able to be loaded.
     36   response.setHeader("Content-Type", "application/javascript");
     37 
     38   var body = "/* " + Date.now() + " */\n" + BODY;
     39   response.write(body);
     40 }