@@ -105,7 +105,6 @@ export const serveStatic = <E extends Env = any>(
105105 await options . onNotFound ?.( path , c )
106106 return next ( )
107107 }
108- await options . onFound ?.( path , c )
109108
110109 const mimeType = getMimeType ( path )
111110 c . header ( 'Content-Type' , mimeType || 'application/octet-stream' )
@@ -133,37 +132,38 @@ export const serveStatic = <E extends Env = any>(
133132 }
134133 }
135134
135+ let result
136136 const size = stats . size
137+ const range = c . req . header ( 'range' ) || ''
137138
138139 if ( c . req . method == 'HEAD' || c . req . method == 'OPTIONS' ) {
139140 c . header ( 'Content-Length' , size . toString ( ) )
140141 c . status ( 200 )
141- return c . body ( null )
142- }
143-
144- const range = c . req . header ( 'range' ) || ''
145-
146- if ( ! range ) {
142+ result = c . body ( null )
143+ } else if ( ! range ) {
147144 c . header ( 'Content-Length' , size . toString ( ) )
148- return c . body ( createStreamBody ( createReadStream ( path ) ) , 200 )
149- }
150-
151- c . header ( 'Accept-Ranges' , 'bytes' )
152- c . header ( 'Date' , stats . birthtime . toUTCString ( ) )
145+ result = c . body ( createStreamBody ( createReadStream ( path ) ) , 200 )
146+ } else {
147+ c . header ( 'Accept-Ranges' , 'bytes' )
148+ c . header ( 'Date' , stats . birthtime . toUTCString ( ) )
149+
150+ const parts = range . replace ( / b y t e s = / , '' ) . split ( '-' , 2 )
151+ const start = parseInt ( parts [ 0 ] , 10 ) || 0
152+ let end = parseInt ( parts [ 1 ] , 10 ) || size - 1
153+ if ( size < end - start + 1 ) {
154+ end = size - 1
155+ }
153156
154- const parts = range . replace ( / b y t e s = / , '' ) . split ( '-' , 2 )
155- const start = parseInt ( parts [ 0 ] , 10 ) || 0
156- let end = parseInt ( parts [ 1 ] , 10 ) || size - 1
157- if ( size < end - start + 1 ) {
158- end = size - 1
159- }
157+ const chunksize = end - start + 1
158+ const stream = createReadStream ( path , { start, end } )
160159
161- const chunksize = end - start + 1
162- const stream = createReadStream ( path , { start, end } )
160+ c . header ( 'Content-Length' , chunksize . toString ( ) )
161+ c . header ( 'Content-Range' , `bytes ${ start } - ${ end } / ${ stats . size } ` )
163162
164- c . header ( 'Content-Length' , chunksize . toString ( ) )
165- c . header ( 'Content-Range' , `bytes ${ start } - ${ end } / ${ stats . size } ` )
163+ result = c . body ( createStreamBody ( stream ) , 206 )
164+ }
166165
167- return c . body ( createStreamBody ( stream ) , 206 )
166+ await options . onFound ?.( path , c )
167+ return result
168168 }
169169}
0 commit comments