<commit_before>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */
/*jshint worker:true*/
/*globals async, CacheTasks, self, mainSiteURLs, Response, fetch */

"use strict";

importScripts("js/lib/async.js"); // imports async()
importScripts("js/lib/responseRequestUtils.js"); // imports RequestUtils, ResponseUtils
importScripts("js/lib/cachetasks.js"); // imports CacheTasks
importScripts("js/mainSiteURLs.js"); // imports mainSiteURLs

const PageThumbTasks = {
  /**
   * Ensure we always resolve with a Response, to avoid Content Corrupted errors
   * being shown to the user.
   *
   * @param {Promise<Response>} promise The promise that should resolve to a
   *                                    Response.
   */
  ensureResponse: async(function*(promise) {
    var response;
    try {
      response = yield promise;
    }catch (err) {
      console.error(err);
      response = new Response();
    }
    return response;
  }),
  init: async(function*() {
    yield CacheTasks.deleteAllCaches();
    yield CacheTasks.populateCache(mainSiteURLs, "skeleton_cache");
  }),
};

self.addEventListener("install", (ev) => {
  ev.waitUntil(PageThumbTasks.init());
});

self.addEventListener("fetch", (ev) => {
  var key = getSwitchKeyFromURL(ev.request.url);
  var promise;
  switch (key) {
  case "pagethumbs":
    promise = CacheTasks.respondFromCache(ev.request, "thumbs_cache");
    ev.respondWith(PageThumbTasks.ensureResponse(promise));
    break;
  case "images":
    promise = CacheTasks.refreshCacheEntry(ev.request, "tiles_cache");
    ev.respondWith(PageThumbTasks.ensureResponse(promise));
    break;
  case "karma":
    ev.respondWith(fetch(ev.request));
    break;
  default:
    promise = CacheTasks.refreshCacheEntry(ev.request, "skeleton_cache");
    ev.respondWith(promise);
  }
});

function getSwitchKeyFromURL(url) {
  //check if we are running in Karma
  if (self.location.port === "9876") {
    return "karma";
  }
  // split and return the first path segment
  var key = new URL(url).pathname.split("/")[1];
  return key;
}<commit_msg>Rewrote how responses are selected in sw.js<commit_after>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */
/*jshint worker:true*/
/*globals async, CacheTasks, Response, fetch */
"use strict";
importScripts("js/lib/async.js"); // imports async()
importScripts("js/lib/cachetasks.js"); // imports CacheTasks

const SWTasks = {
  /**
   * Initialization tasks for service workers.
   */
  init() {
    return async.task(function*() {
      yield CacheTasks.deleteCaches([
        "skeleton_cache", // The html, js, css, of site
        "pagethumbs_cache", // User's history tiles
        "ads_cache", // Advertisement tiles
      ]);
      var request = yield fetch("js/mainSiteURLs.json");
      var mainSiteURLs = (yield request.json())
        .map(path => `${location.origin}${path}`);
      yield CacheTasks.addAll(mainSiteURLs, "skeleton_cache");
    }, this);
  },
  /**
   * Selects the appropriate response to return.
   *
   * @param {Request} request The request that the client made.
   * @return {Promise<response>} The response to serve to the client.
   */
  selectResponse(request) {
    return async.task(function*() {
      var response;
      var key = new URL(request.url).pathname.split("/")[1];
      switch (key) {
      case "pagethumbs":
        response = yield CacheTasks.match(request, "pagethumbs_cache");
        // TODO: Ensure page thumb.
        break;
      case "images":
        response = yield CacheTasks.match(request, "ads_cache");
        // TODO: Ensure dynamic tile
        break;
      default:
        response = yield CacheTasks.match(request, "skeleton_cache");
      }
      if (!response && navigator.onLine) {
        console.warn(`Going to network for ${request.url}`);
        response = yield fetch(request);
        yield CacheTasks.put(request, response, "skeleton_cache");
      }
      return response || new Response();
    }, this);
  },
};

self.addEventListener("install", (ev) => {
  ev.waitUntil(SWTasks.init());
});

self.addEventListener("fetch", (ev) => {
  ev.respondWith(SWTasks.selectResponse(ev.request));
});
{'fraction_non_alphanumeric': 0.13884520332509548, 'fraction_numerical': 0.0031453605931251404, 'mean_word_length': 3.2119205298013247, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2577501', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1343, 'n_words': 358}