Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
fix(sw): exercise clips are cached whole and range requests get a real 206
Duarte Santosb92b2b49b223
diff --git a/frontend/public/sw.js b/frontend/public/sw.jsindex f96fccd42..f6822658d 100644--- a/frontend/public/sw.js+++ b/frontend/public/sw.js@@ -148,9 +148,34 @@ async function trimMedia() {}}+// A <video> asks for its clip in byte ranges. The network's 206 cannot be cached (cache.put+// refuses a partial answer), so a clip was never kept; and a whole 200 handed to a range request+// is a clip iOS Safari will not play. So the cache holds the whole file, fetched without a Range,+// and a range request is answered with a 206 cut from it. One range, the only kind a player asks+// for; anything else gets the whole file.+const rangeOf = req => (req.headers && typeof req.headers.get === 'function' && req.headers.get('range')) || ''+async function slice(res, range) {+ const m = /^bytes=(\d*)-(\d*)$/.exec(range.trim())+ const type = res.headers.get('content-type') || 'application/octet-stream'+ const body = await res.arrayBuffer()+ const size = body.byteLength+ let start = m && m[1] !== '' ? Number(m[1]) : NaN+ let end = m && m[2] !== '' ? Number(m[2]) : size - 1+ if (m && m[1] === '' && m[2] !== '') { start = Math.max(0, size - Number(m[2])); end = size - 1 }+ if (!m || !Number.isFinite(start)) return new Response(body, { status: 200, headers: { 'content-type': type, 'content-length': String(size), 'accept-ranges': 'bytes' } })+ if (start >= size || end < start) return new Response(null, { status: 416, headers: { 'content-range': 'bytes */' + size } })+ end = Math.min(end, size - 1)+ return new Response(body.slice(start, end + 1), {+ status: 206,+ statusText: 'Partial Content',+ headers: { 'content-type': type, 'content-length': String(end - start + 1), 'content-range': `bytes ${start}-${end}/${size}`, 'accept-ranges': 'bytes' }+ })+}+// URLs written back this worker lifetime; see MEDIA above.const touched = new Set()function media(e) {+ const range = rangeOf(e.request)return caches.open(MEDIA).then(c => c.match(e.request).then(hit => {if (hit) {if (!touched.has(e.request.url)) {@@ -158,13 +183,16 @@ function media(e) {const copy = hit.clone()e.waitUntil(c.put(e.request, copy).catch(() => {}))}- return hit+ return range ? slice(hit, range) : hit}- return fetch(e.request).then(res => {- if (realMedia(res)) {+ // The whole file for a range request: the one answer that can be kept.+ return fetch(range ? e.request.url : e.request).then(res => {+ const whole = range ? res.status === 200 : true+ if (whole && realMedia(res)) {touched.add(e.request.url)const copy = res.clone()- e.waitUntil(c.put(e.request, copy).then(() => { if (++mediaPuts >= MEDIA_TRIM_EVERY) { mediaPuts = 0; return trimMedia() } }).catch(() => {}))+ e.waitUntil(c.put(e.request.url, copy).then(() => { if (++mediaPuts >= MEDIA_TRIM_EVERY) { mediaPuts = 0; return trimMedia() } }).catch(() => {}))+ if (range) return slice(res, range)}return res})diff --git a/frontend/src/lib/sw-media.test.js b/frontend/src/lib/sw-media.test.jsindex b97d1a226..32b09bb24 100644--- a/frontend/src/lib/sw-media.test.js+++ b/frontend/src/lib/sw-media.test.js@@ -34,20 +34,21 @@ function worker(all = new Map()) {delete: async n => all.delete(n),match: async req => { for (const c of all.values()) { const hit = await c.match(req); if (hit) return hit } return undefined },}- const env = { net: { up: true, answer: null }, fetched: [], all, caches }+ const env = { net: { up: true, answer: null }, fetched: [], ranges: [], all, caches }const fetch = async req => {const url = keyOf(req)env.fetched.push(url)+ env.ranges.push(typeof req === 'string' ? '' : req.headers?.get('range') || '')if (!env.net.up) throw new TypeError('Failed to fetch')if (env.net.answer) return env.net.answer(url)return new Response('GIF89a' + url, { status: 200, headers: { 'content-type': 'image/gif', 'content-length': String(40 * 1024 * 1024) } })}const self = { addEventListener: (t, f) => { handlers[t] = f }, skipWaiting: () => {}, clients: { claim: async () => {} }, registration: {} }new Function('self', 'caches', 'fetch', 'location', SW)(self, caches, fetch, new URL(ORIGIN + '/sw.js'))- env.get = async path => {+ env.get = async (path, headers) => {const pending = []let responded = null- const e = { request: { url: ORIGIN + path, method: 'GET', mode: 'no-cors' }, respondWith: p => { responded = p }, waitUntil: p => { pending.push(p) } }+ const e = { request: { url: ORIGIN + path, method: 'GET', mode: 'no-cors', headers: new Headers(headers || {}) }, respondWith: p => { responded = p }, waitUntil: p => { pending.push(p) } }handlers.fetch(e)const res = await respondedawait Promise.all(pending)@@ -153,6 +154,58 @@ describe('sw.js exercise media', () => {expect(left).toHaveLength(22)})+ describe('a video clip, asked for in ranges', () => {+ const CLIP = '0123456789abcdefghij' // 20 bytes+ const clip = () => new Response(CLIP, { status: 200, headers: { 'content-type': 'video/mp4', 'content-length': '20' } })++ it('a miss fetches the whole file without the Range, keeps it, and answers with the 206 asked for', async () => {+ const w = worker()+ w.net.answer = clip+ const res = await w.get('/exercise-media/clip/0001.mp4', { range: 'bytes=0-' })+ expect(w.ranges).toEqual([''])+ expect(res.status).toBe(206)+ expect(res.headers.get('content-range')).toBe('bytes 0-19/20')+ expect(res.headers.get('content-type')).toBe('video/mp4')+ expect(await res.text()).toBe(CLIP)+ expect(w.media().urls()).toEqual([ORIGIN + '/exercise-media/clip/0001.mp4'])+ const kept = await w.media().match(ORIGIN + '/exercise-media/clip/0001.mp4')+ expect(kept.status).toBe(200)+ })++ it('a hit answers each range with a 206 cut from the cached file, offline too', async () => {+ const w = worker()+ w.net.answer = clip+ await w.get('/exercise-media/clip/0002.mp4')+ w.net.up = false+ const part = await w.get('/exercise-media/clip/0002.mp4', { range: 'bytes=2-5' })+ expect(part.status).toBe(206)+ expect(part.headers.get('content-range')).toBe('bytes 2-5/20')+ expect(part.headers.get('content-length')).toBe('4')+ expect(await part.text()).toBe('2345')+ const tail = await w.get('/exercise-media/clip/0002.mp4', { range: 'bytes=-3' })+ expect(tail.headers.get('content-range')).toBe('bytes 17-19/20')+ expect(await tail.text()).toBe('hij')+ const past = await w.get('/exercise-media/clip/0002.mp4', { range: 'bytes=10-99' })+ expect(past.headers.get('content-range')).toBe('bytes 10-19/20')+ const beyond = await w.get('/exercise-media/clip/0002.mp4', { range: 'bytes=40-' })+ expect(beyond.status).toBe(416)+ expect(beyond.headers.get('content-range')).toBe('bytes */20')+ // without a Range, still the whole file as before+ const whole = await w.get('/exercise-media/clip/0002.mp4')+ expect(whole.status).toBe(200)+ expect(await whole.text()).toBe(CLIP)+ })++ it('a refusal or a partial answer from the network is passed on and not kept', async () => {+ const w = worker()+ w.net.answer = () => new Response('no', { status: 401 })+ expect((await w.get('/exercise-media/clip/0003.mp4', { range: 'bytes=0-' })).status).toBe(401)+ w.net.answer = () => new Response('01', { status: 206, headers: { 'content-type': 'video/mp4', 'content-range': 'bytes 0-1/20' } })+ expect((await w.get('/exercise-media/clip/0003.mp4', { range: 'bytes=0-' })).status).toBe(206)+ expect(w.media().urls()).toEqual([])+ })+ })+it('names the same cache as the page-side prefetch', () => {expect(SW).toContain(`const MEDIA = '${MEDIA_CACHE}'`)expect(MEDIA_CACHE).toBe(MEDIA)