-
Notifications
You must be signed in to change notification settings - Fork 769
/
ppp-sw.js
210 lines (180 loc) · 6.18 KB
/
ppp-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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Incrementing OFFLINE_VERSION will kick off the install event and force
// previously cached resources to be updated from the network.
// This variable is intentionally declared and unused.
// Add a comment for your linter if you want:
// eslint-disable-next-line no-unused-vars
const OFFLINE_VERSION = 8;
const PPP_CACHE_NAME = 'offline';
const OFFLINE_URL = 'offline.html';
// noinspection DuplicatedCode
function placeDecorators(decorators = []) {
let result = '';
decorators.forEach(({ d, c, t, l }) => {
if (t === 'class') {
result += `${c} = __decorate([${d}], ${c});\n`;
} else if (t === 'method') {
result += `__decorate([${d}], ${c}.prototype, '${l
.split(/\(/i)[0]
.trim()}', null);\n`;
} else if (t === 'prop') {
result += `__decorate([${d}], ${c}.prototype, '${l
.split(/=/i)[0]
.replace(/;/, '')
.trim()}', void 0);\n`;
}
});
return result;
}
function removeDecorators(source) {
const decorators = [];
// Should be moved to the bottom after __decorate
const exports = [];
const lines = source.split(/\n/gi);
let result = '';
let currentClass = '';
let hasDefaultExport = false;
lines.forEach((l, i) => {
const line = l.trim();
if (/class\s+/.test(line)) {
currentClass = line.split(/class /)[1].split(/\s/)[0];
}
if (line.startsWith('@') && !/^@keyframes/.test(line) && !/=/.test(line)) {
const nextLine = lines[i + 1]?.trim();
if (/class\s+/.test(nextLine)) {
// Class decorator
currentClass = nextLine.split(/class /)[1].split(/\s/)[0];
decorators.push({
d: line.substring(1),
c: currentClass,
t: 'class'
});
} else {
// Member decorator
const t = /\)\s+{/.test(nextLine) ? 'method' : 'prop';
decorators.unshift({
d: line.substring(1),
c: currentClass,
t,
l: nextLine
});
t === 'prop' && (lines[i + 1] = '');
}
} else if (/(^export default)|(\/\/ export default)/.test(line)) {
hasDefaultExport = true;
result += placeDecorators(decorators);
result += l + '\n';
} else result += l + '\n';
});
if (decorators.length) {
result =
'const __decorate = function (decorators, target, key, desc) {\n' +
' let c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n' +
' for (let i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n' +
' return c > 3 && r && Object.defineProperty(target, key, r), r;\n' +
'};\n' +
result;
if (!hasDefaultExport) {
result += placeDecorators(decorators);
}
}
return result;
}
self.onmessage = (event) => {
// For hard resets
if (event.data === 'reclaim') {
return self.clients.claim();
}
};
self.addEventListener('install', (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(PPP_CACHE_NAME);
// Setting { cache: 'reload' } in the new request will ensure that the
// response isn't fulfilled from the HTTP cache; i.e., it will be from
// the network.
await cache.add(new Request(OFFLINE_URL, { cache: 'reload' }));
})()
);
// Force the waiting service worker to become the active service worker.
return self.skipWaiting();
});
self.addEventListener('activate', () => {
// Tell the active service worker to take control of the page immediately.
return self.clients.claim();
});
self.addEventListener('fetch', async (event) => {
// If no fetch handlers call event.respondWith(), the request will be handled
// by the browser as if there were no service worker involvement.
if (
(event.request.destination === 'image' &&
event.request.url?.endsWith('.png')) ||
event.request.url?.endsWith('.sql') ||
event.request.url?.endsWith('.json') ||
event.request.url?.startsWith('chrome-extension')
) {
return;
}
if (
event.request.destination &&
event.request.method === 'GET' &&
(new URL(event.request.url).pathname.endsWith('.js') ||
/\?page=/i.test(event.request.url))
) {
return event.respondWith(
(async () => {
try {
if (location.origin.endsWith('.io.dev')) {
return await fetch(event.request, {
cache: 'no-store'
}).then(async (r) => {
const ct = r.headers.get('content-type');
const text = await r.text();
const init = {
status: r.status,
statusText: r.statusText,
headers: r.headers
};
if (
/javascript/gi.test(ct) &&
text.startsWith('/** @decorator */')
) {
return new Response(removeDecorators(text), init);
} else {
return new Response(text, init);
}
});
}
const cache = await caches.open(PPP_CACHE_NAME);
const cachedResponse = await cache.match(event.request);
const fetchedResponse = fetch(event.request, {
cache: 'no-store'
}).then(async (networkResponse) => {
const clone = networkResponse.clone();
const ct = clone.headers.get('content-type');
const text = await clone.text();
const init = {
status: clone.status,
statusText: clone.statusText,
headers: clone.headers
};
if (
/javascript/gi.test(ct) &&
text.startsWith('/** @decorator */')
) {
const r = new Response(removeDecorators(text), init);
void cache.put(event.request, r.clone());
return r;
} else {
void cache.put(event.request, new Response(text, init));
return networkResponse;
}
});
return cachedResponse ?? fetchedResponse;
} catch (e) {
const cache = await caches.open(PPP_CACHE_NAME);
return await cache.match(OFFLINE_URL);
}
})()
);
}
});