File: file_block_script_wrong_mime_sw.js

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (49 lines) | stat: -rw-r--r-- 1,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Service Worker that runs in 2 modes: 1) direct pass-through via
 * fetch(event.request) and 2) indirect pass-through via
 * fetch(event.request.url).
 *
 * Because this is updating a pre-existing mochitest that didn't use a SW and
 * used a single test document, we use a SW idiom where the SW claims the
 * existing window client.  And because we operate in two modes and we
 * parameterize via URL, we also ensure that we skipWaiting.
 */

// We are parameterized by "mode".
const params = new URLSearchParams(location.search);
const fetchMode = params.get("fetchMode");

// When activating on initial install, claim the existing window client.
// For synchronziation, also message the controlled document to report our mode.
self.addEventListener("activate", event => {
  event.waitUntil(
    (async () => {
      await clients.claim();
      const allClients = await clients.matchAll();
      for (const client of allClients) {
        client.postMessage({
          fetchMode,
        });
      }
    })()
  );
});

// When updating the SW to change our mode of operation, skipWaiting so we
// advance directly to activating without waiting for the test window client
// to stop being controlled by our previous configuration.
self.addEventListener("install", () => {
  self.skipWaiting();
});

self.addEventListener("fetch", event => {
  switch (fetchMode) {
    case "direct":
      event.respondWith(fetch(event.request));
      break;

    case "indirect":
      event.respondWith(fetch(event.request.url));
      break;
  }
});