range-request-with-synth-head-worker.js (1020B)
1 // This worker is meant to test range requests where the initial response 2 // synthesizes a 206 Partial Content response with leading bytes. 3 // Then the worker lets subsequent range requests fall back to network. 4 5 let initial = true; 6 function is_initial_request() { 7 const old = initial; 8 initial = false; 9 return old; 10 } 11 12 self.addEventListener('fetch', e => { 13 const url = new URL(e.request.url); 14 if (url.search.indexOf('VIDEO') == -1) { 15 // Fall back for non-video. 16 return; 17 } 18 19 // Synthesize the response to the first request. 20 if (is_initial_request()) { 21 // Consistent with fetch-access-control.py?VIDEO 22 const init = { 23 status: 206, 24 headers: { 25 "Accept-Ranges": "bytes", 26 "Content-Type": "video/webm", 27 "Content-Range": "bytes 0-1/*", 28 "Content-Length": "2", 29 }, 30 }; 31 e.respondWith(new Response(new Uint8Array([0x1a, 0x45]), init)); 32 return; 33 } 34 35 // Fall back for subsequent range requests. 36 });