forked from nettleweb/nettleweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
117 lines (106 loc) · 2.86 KB
/
sw.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/// <reference lib="WebWorker" />
"use strict"; ((_) => {
/**
* @type {ServiceWorkerGlobalScope}
*/
const self = _;
const origin = self.location.origin;
const cacheName = "167e1f07-b59a-4742-bb45-15cf3caabcce";
const errorResponse = Response.error();
/**
*
* @param {Request} request
* @returns {Promise<Response>}
*/
async function optFetch(request) {
try {
return await self.fetch(request);
} catch (err) {
return errorResponse;
}
}
/**
* @param {FetchEvent} e
* @returns {Promise<Response>}
*/
async function handleFetch(e) {
const request = e.request;
switch (request.method) {
case "GET":
case "HEAD":
break;
default:
return await optFetch(request);
}
const url = new URL(request.url);
switch (url.protocol) {
case "http:":
case "https:":
break;
default:
return await optFetch(request);
}
if (url.origin === origin && url.pathname === "/manifest.json") {
return await optFetch(url, {
body: null,
mode: "same-origin",
cache: "no-cache",
method: request.method,
headers: request.headers
});
}
const res = await caches.match(request, { cacheName }) || await e.preloadResponse || await optFetch(request);
if (origin !== "http://localhost:8000") {
try {
await (await caches.open(cacheName)).put(request, res.clone());
} catch (err) {
// ignore
}
}
return res;
}
/**
* @param {ExtendableMessageEvent} e
*/
async function handleMessage(e) {
}
/**
* @param {ExtendableEvent} e
*/
async function handleInstall(e) {
const cache = await caches.open(cacheName);
await cache.addAll(["manifest.json", "sw.js",]);
}
/**
* @param {ExtendableEvent} e
*/
async function handleActivate(e) {
await self.clients.claim();
for (const k of await caches.keys()) {
if (k != cacheName)
await caches.delete(k);
}
}
async function handlePaymentRequest(e) {
let resolve, reject;
e.respondWith(new Promise((res, rej) => {
resolve = res;
reject = rej;
}));
/**
* @type {WindowClient | null}
*/
const client = await e.openWindow("/auth.html?url=" + encodeURIComponent(e.methodData[0].data[0]));
if (client == null || client.type !== "window") {
reject("Error: Failed to open payment window");
return;
}
client.postMessage("dummy");
}
self.addEventListener("fetch", (e) => e.respondWith(handleFetch(e)), { passive: true });
self.addEventListener("message", (e) => e.waitUntil(handleMessage(e)), { passive: true });
self.addEventListener("install", (e) => e.waitUntil(handleInstall(e)), { passive: true });
self.addEventListener("activate", (e) => e.waitUntil(handleActivate(e)), { passive: true });
self.addEventListener("canmakepayment", (e) => e.respondWith(Promise.resolve(true)), { passive: true });
self.addEventListener("paymentrequest", (e) => e.waitUntil(handlePaymentRequest(e)), { passive: true });
})(self);