summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/sample-worker-interceptor.js
blob: c06f8dd77b79d4c6c6e8b3a15048a79338d9b42f (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
importScripts('/common/get-host-info.sub.js');

const text = 'worker loading intercepted by service worker';
const dedicated_worker_script = `postMessage('${text}');`;
const shared_worker_script =
    `onconnect = evt => evt.ports[0].postMessage('${text}');`;

let source;
let resolveDone;
let done = new Promise(resolve => resolveDone = resolve);

// The page messages this worker to ask for the result. Keep the worker alive
// via waitUntil() until the result is sent.
self.addEventListener('message', event => {
  source = event.data.port;
  source.postMessage({id: event.source.id});
  source.onmessage = resolveDone;
  event.waitUntil(done);
});

self.onfetch = event => {
  const url = event.request.url;
  const destination = event.request.destination;

  if (source)
     source.postMessage({clientId:event.clientId, resultingClientId: event.resultingClientId});

  // Request handler for a synthesized response.
  if (url.indexOf('synthesized') != -1) {
    let script_headers = new Headers({ "Content-Type": "text/javascript" });
    if (destination === 'worker')
      event.respondWith(new Response(dedicated_worker_script, { 'headers': script_headers }));
    else if (destination === 'sharedworker')
      event.respondWith(new Response(shared_worker_script, { 'headers': script_headers }));
    else
      event.respondWith(new Response('Unexpected request! ' + destination));
    return;
  }

  // Request handler for a same-origin response.
  if (url.indexOf('same-origin') != -1) {
    event.respondWith(fetch('postmessage-on-load-worker.js'));
    return;
  }

  // Request handler for a cross-origin response.
  if (url.indexOf('cors') != -1) {
    const filename = 'postmessage-on-load-worker.js';
    const path = (new URL(filename, self.location)).pathname;
    let new_url = get_host_info()['HTTPS_REMOTE_ORIGIN'] + path;
    let mode;
    if (url.indexOf('no-cors') != -1) {
      // Test no-cors mode.
      mode = 'no-cors';
    } else {
      // Test cors mode.
      new_url += '?pipe=header(Access-Control-Allow-Origin,*)';
      mode = 'cors';
    }
    event.respondWith(fetch(new_url, { mode: mode }));
  }
};