tor-browser

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

preload_helper.js (2051B)


      1 function stashPutUrl(token) {
      2  return `/preload/resources/stash-put.py?key=${token}`;
      3 }
      4 
      5 function encodedStashPutUrl(token) {
      6  return encodeURIComponent(stashPutUrl(token));
      7 }
      8 
      9 async function hasArrivedAtServer(token) {
     10  const res = await fetch(`/preload/resources/stash-take.py?key=${token}`);
     11  assert_true(res.status === 200 || res.status === 404,
     12              'status must be either 200 or 404');
     13  return res.status === 200;
     14 }
     15 
     16 function verifyPreloadAndRTSupport()
     17 {
     18    var link = window.document.createElement("link");
     19    assert_true(link.relList && link.relList.supports("preload"), "Preload not supported");
     20    assert_true(!!window.PerformanceResourceTiming, "ResourceTiming not supported");
     21 }
     22 
     23 function getAbsoluteURL(url)
     24 {
     25    return new URL(url, location.href).href;
     26 }
     27 
     28 function verifyNumberOfResourceTimingEntries(url, number)
     29 {
     30    assert_equals(numberOfResourceTimingEntries(url), number, url);
     31 }
     32 
     33 function numberOfResourceTimingEntries(url)
     34 {
     35    return performance.getEntriesByName(getAbsoluteURL(url)).length;
     36 }
     37 
     38 // Verifies that the resource is loaded, but not downloaded from network
     39 // more than once. This can be used to verify that a preloaded resource is
     40 // not downloaded again when used.
     41 function verifyLoadedAndNoDoubleDownload(url) {
     42    var entries = performance.getEntriesByName(getAbsoluteURL(url));
     43    // UA may create separate RT entries for preload and normal load,
     44    // so we just check (entries.length > 0).
     45    assert_greater_than(entries.length, 0, url + ' should be loaded');
     46 
     47    var numDownloads = 0;
     48    entries.forEach(entry => {
     49        // transferSize is zero if the resource is loaded from cache.
     50        if (entry.transferSize > 0) {
     51            numDownloads++;
     52        }
     53    });
     54    // numDownloads can be zero if the resource was already cached before running
     55    // the test (for example, when the test is running repeatedly without
     56    // clearing cache between runs).
     57    assert_less_than_equal(
     58        numDownloads, 1,
     59        url + ' should be downloaded from network at most once');
     60 }