1 addEventListener("install", function (evt) {
2 evt.waitUntil(self.skipWaiting());
6 * Given a multipart/form-data encoded string that we know to have only a single
7 * part, return the contents of the part. (MIME multipart encoding is too
8 * exciting to delve into.)
10 function extractBlobFromMultipartFormData(text) {
11 const lines = text.split(/\r\n/g);
12 const firstBlank = lines.indexOf("");
13 const foo = lines.slice(firstBlank + 1, -2).join("\n");
17 self.addEventListener("fetch", event => {
18 const url = new URL(event.request.url);
19 const mode = url.searchParams.get("mode");
21 if (mode === "synthetic") {
24 // This works even if there wasn't a body explicitly associated with the
25 // request. We just get a zero-length string in that case.
26 const requestBodyContents = await event.request.text();
28 extractBlobFromMultipartFormData(requestBodyContents);
31 `<!DOCTYPE HTML><head><meta charset="utf-8"/></head><body>
32 <h1 id="url">${event.request.url}</h1>
33 <div id="source">ServiceWorker</div>
34 <div id="blob">${blobContents}</div>
36 { headers: { "Content-Type": "text/html" } }
40 } else if (mode === "fetch") {
41 event.respondWith(fetch(event.request));
42 } else if (mode === "clone") {
43 // In order for the act of cloning to be interesting, we want the original
44 // request to remain alive so that any pipes end up having to buffer.
45 self.originalRequest = event.request;
46 event.respondWith(fetch(event.request.clone()));
50 `<!DOCTYPE HTML><head><meta charset="utf-8"/></head><body>
51 <h1 id="error">Bad mode: ${mode}</h1>
52 <div id="source">ServiceWorker::Error</div>
53 <div id="blob">No, this is an error.</div>
55 { headers: { "Content-Type": "text/html" }, status: 400 }