-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop-worker.mjs
42 lines (34 loc) · 1.03 KB
/
loop-worker.mjs
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
/* eslint-env worker */
import HLSLoop from './src/hls-loop.mjs'
const loops = new Map()
self.addEventListener('fetch', (event) => {
const request = event.request
if (request.method !== 'GET') {
// only process GET requests
return
}
event.respondWith(fetch(request).then(async (response) => {
const contentType = response.headers.get('Content-Type')
if (!/application\/vnd\.apple\.mpegurl/i.test(contentType)) {
// return unmodified responses for non-playlist requests
return response
}
let loop = loops.get(request.url)
if (!loop) {
// create and cache a loop for every new playlist URL
// encountered
const m3u8 = await response.text()
loop = new HLSLoop(m3u8)
loops.set(request.url, loop)
}
return new Response(loop.render(), {
status: 200,
headers: {
...response.headers,
'Cache-Control': 'max-age=0'
}
})
}).catch((error) => {
console.error('Looper fetch error', request.method, request.url, error)
}))
})