summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/private-network-access/resources/shared-fetcher.js
blob: 30bde1e0542d144f9b5b0e47824802df13556ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
async function doFetch(url) {
  const response = await fetch(url);
  const body = await response.text();
  return {
    status: response.status,
    body,
  };
}

async function fetchAndPost(url, port) {
  try {
    const message = await doFetch(url);
    port.postMessage(message);
  } catch(e) {
    port.postMessage({ error: e.name });
  }
}

const url = new URL(self.location.href).searchParams.get("url");

self.addEventListener("connect", async (evt) => {
  await fetchAndPost(url, evt.ports[0]);
});