@@ -16,13 +16,16 @@ export const handleFile = (
1616 const size = response . size
1717
1818 const rangeHeader = request ?. headers . get ( 'range' )
19- if ( rangeHeader && size ) {
19+ if ( rangeHeader ) {
2020 const match = / b y t e s = ( \d * ) - ( \d * ) / . exec ( rangeHeader )
2121 if ( match ) {
2222 if ( ! match [ 1 ] && ! match [ 2 ] )
2323 return new Response ( null , {
2424 status : 416 ,
25- headers : { 'content-range' : `bytes */${ size } ` }
25+ headers : mergeHeaders (
26+ new Headers ( { 'content-range' : `bytes */${ size } ` } ) ,
27+ set ?. headers ?? { }
28+ )
2629 } )
2730
2831 let start : number
@@ -42,34 +45,34 @@ export const handleFile = (
4245 if ( start >= size || start > end ) {
4346 return new Response ( null , {
4447 status : 416 ,
45- headers : { 'content-range' : `bytes */${ size } ` }
48+ headers : mergeHeaders (
49+ new Headers ( { 'content-range' : `bytes */${ size } ` } ) ,
50+ set ?. headers ?? { }
51+ )
4652 } )
4753 }
4854
4955 const contentLength = end - start + 1
50- const rangeHeaders : Record < string , string | number > = {
56+ const rangeHeaders = new Headers ( {
5157 'accept-ranges' : 'bytes' ,
5258 'content-range' : `bytes ${ start } -${ end } /${ size } ` ,
53- 'content-length' : contentLength
54- }
55-
56- if ( set ?. headers && isNotEmpty ( set . headers as Record < string , unknown > ) )
57- Object . assign ( rangeHeaders , set . headers , {
58- 'content-range' : `bytes ${ start } -${ end } /${ size } ` ,
59- 'content-length' : contentLength
60- } )
59+ 'content-length' : String ( contentLength )
60+ } )
6161
6262 // Blob.slice() exists at runtime but is absent from the ESNext lib typings
63- // (no DOM lib). Cast through unknown to the minimal interface we need.
64- return new Response (
65- ( response as unknown as { slice ( start : number , end : number ) : Blob } ) . slice (
66- start ,
67- end + 1
68- ) ,
69- {
70- status : 206 ,
71- headers : rangeHeaders as any
72- } )
63+ // (no DOM lib). Cast through unknown to the minimal interface we need.
64+ // Pass response.type as third arg so the sliced blob preserves MIME type.
65+ return new Response (
66+ (
67+ response as unknown as {
68+ slice ( start : number , end : number , contentType ?: string ) : Blob
69+ }
70+ ) . slice ( start , end + 1 , response . type ) ,
71+ {
72+ status : 206 ,
73+ headers : mergeHeaders ( rangeHeaders , set ?. headers ?? { } )
74+ }
75+ )
7376 }
7477 }
7578
0 commit comments