tor-browser

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

credentials-worker.js (1801B)


      1 var cache_name = 'credentials';
      2 
      3 function assert_equals(actual, expected, message) {
      4  if (!Object.is(actual, expected))
      5    throw Error(message + ': expected: ' + expected + ', actual: ' + actual);
      6 }
      7 
      8 self.onfetch = function(e) {
      9  if (!/\.txt$/.test(e.request.url)) return;
     10  var content = e.request.url;
     11  var cache;
     12  e.respondWith(
     13    self.caches.open(cache_name)
     14      .then(function(result) {
     15        cache = result;
     16        return cache.put(e.request, new Response(content));
     17      })
     18 
     19      .then(function() { return cache.match(e.request); })
     20      .then(function(result) { return result.text(); })
     21      .then(function(text) {
     22        assert_equals(text, content, 'Cache.match() body should match');
     23      })
     24 
     25      .then(function() { return cache.matchAll(e.request); })
     26      .then(function(results) {
     27        assert_equals(results.length, 1, 'Should have one response');
     28        return results[0].text();
     29      })
     30      .then(function(text) {
     31        assert_equals(text, content, 'Cache.matchAll() body should match');
     32      })
     33 
     34      .then(function() { return self.caches.match(e.request); })
     35      .then(function(result) { return result.text(); })
     36      .then(function(text) {
     37        assert_equals(text, content, 'CacheStorage.match() body should match');
     38      })
     39 
     40     .then(function() {
     41        return new Response('dummy');
     42      })
     43  );
     44 };
     45 
     46 self.onmessage = function(e) {
     47  if (e.data === 'keys') {
     48    self.caches.open(cache_name)
     49      .then(function(cache) { return cache.keys(); })
     50      .then(function(requests) {
     51        var urls = requests.map(function(request) { return request.url; });
     52        self.clients.matchAll().then(function(clients) {
     53          clients.forEach(function(client) {
     54            client.postMessage(urls);
     55          });
     56        });
     57      });
     58  }
     59 };