-
Notifications
You must be signed in to change notification settings - Fork 18
/
range-requests.sw.js
69 lines (61 loc) · 1.99 KB
/
range-requests.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
/**
* Safari video range request fix, inspired by
* @see https://philna.sh/blog/2018/10/23/service-workers-beware-safaris-range-request
* @see https://bugs.webkit.org/show_bug.cgi?id=232076#c5
*/
addEventListener("fetch", (event) => {
const proxyVideoRequestsTo = "___range-requests___/";
const request = event.request;
const [, url] = request.url.split(proxyVideoRequestsTo);
if (!url) return;
const cacheName = "___range-requests___";
const response = caches
.open(cacheName)
.then((cache) => cache.match(request.url))
.then(
(res) =>
res ||
fetch(new Request(decodeURIComponent(url), request)).then((res) =>
caches
.open(cacheName)
.then((cache) => cache.put(request, res.clone()))
.then(() => res),
),
)
.then((res) =>
Promise.all([res.arrayBuffer(), res.headers.get("Content-Type")]),
)
.then(([arrayBuffer, type]) => {
const bytes = /^bytes\=(\d+)\-(\d+)?$/g.exec(
request.headers.get("range"),
);
if (bytes) {
const length = arrayBuffer.byteLength;
const start = +bytes[1] || 0;
const end = +bytes[2] || length - 1;
return new Response(arrayBuffer.slice(start, end + 1), {
status: 206,
statusText: "Partial Content",
headers: {
"Content-Type": type,
"Content-Range": `bytes ${start}-${end}/${length}`,
"Content-Length": end - start + 1,
},
});
} else {
return new Response(null, {
status: 416,
statusText: "Range Not Satisfiable",
headers: {
"Content-Range": `*/${length}`,
},
});
}
});
event.respondWith(response);
});
/**
* You may also want to add the `clients.claim()` to install the worker as early as possible
* @see https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim
*/
addEventListener("activate", (event) => event.waitUntil(clients.claim()));