1
1
import { Req , Res } from '../../../deps.ts'
2
- import { isAbsolute , join , extname } from 'https://deno.land/std@0.99 .0/path/mod.ts'
2
+ import { isAbsolute , join , extname } from 'https://deno.land/std@0.100 .0/path/mod.ts'
3
3
import { contentType } from '../../../deps.ts'
4
4
import { createETag } from '../utils.ts'
5
5
import { send } from './send.ts'
@@ -22,59 +22,56 @@ export type SendFileOptions = Partial<{
22
22
*
23
23
* @param res Response
24
24
*/
25
- export const sendFile = < Request extends Req = Req , Response extends Res = Res > ( req : Request , res : Response ) => (
26
- path : string ,
27
- opts : SendFileOptions = { }
28
- ) => {
29
- const { root, headers = { } , encoding = 'utf-8' , ...options } = opts
25
+ export const sendFile =
26
+ < Request extends Req = Req , Response extends Res = Res > ( req : Request , res : Response ) =>
27
+ ( path : string , opts : SendFileOptions = { } ) => {
28
+ const { root, headers = { } , encoding = 'utf-8' , ...options } = opts
30
29
31
- if ( ! isAbsolute ( path ) && ! root ) throw new TypeError ( 'path must be absolute' )
30
+ if ( ! isAbsolute ( path ) && ! root ) throw new TypeError ( 'path must be absolute' )
32
31
33
- const filePath = root ? join ( root , path ) : path
32
+ const filePath = root ? join ( root , path ) : path
34
33
35
- let stats : Deno . FileInfo
34
+ const stats = Deno . statSync ( filePath )
36
35
37
- stats = Deno . statSync ( filePath )
36
+ headers [ 'Content-Encoding' ] = encoding
38
37
39
- headers [ 'Content-Encoding ' ] = encoding
38
+ headers [ 'Last-Modified ' ] = stats . mtime ! . toUTCString ( )
40
39
41
- headers [ 'Last-Modified ' ] = stats . mtime ! . toUTCString ( )
40
+ headers [ 'Content-Type ' ] = contentType ( extname ( path ) ) || 'text/html'
42
41
43
- headers [ 'Content-Type ' ] = contentType ( extname ( path ) ) || 'text/html'
42
+ headers [ 'ETag ' ] = createETag ( stats )
44
43
45
- headers [ 'ETag ' ] = createETag ( stats )
44
+ headers [ 'Content-Length ' ] = ` ${ stats . size } `
46
45
47
- headers [ 'Content-Length' ] = `${ stats . size } `
46
+ headers [ 'Content-Security-Policy' ] = "default-src 'none'"
47
+ headers [ 'X-Content-Type-Options' ] = 'nosniff'
48
48
49
- headers [ 'Content-Security-Policy' ] = "default-src 'none'"
50
- headers [ 'X-Content-Type-Options' ] = 'nosniff'
49
+ let status = 200
51
50
52
- let status = 200
51
+ if ( req . headers . get ( 'range' ) ) {
52
+ status = 206
53
+ const [ x , y ] = req . headers ?. get ( 'range' ) ?. replace ( 'bytes=' , '' ) . split ( '-' ) as [ string , string ]
54
+ const end = ( options . end = parseInt ( y , 10 ) || stats . size - 1 )
55
+ const start = ( options . start = parseInt ( x , 10 ) || 0 )
53
56
54
- if ( req . headers . get ( 'range' ) ) {
55
- status = 206
56
- const [ x , y ] = req . headers ?. get ( 'range' ) ?. replace ( 'bytes=' , '' ) . split ( '-' ) as [ string , string ]
57
- const end = ( options . end = parseInt ( y , 10 ) || stats . size - 1 )
58
- const start = ( options . start = parseInt ( x , 10 ) || 0 )
59
-
60
- if ( start >= stats . size || end >= stats . size ) {
61
- res . status = 416
62
- res . headers ?. set ( 'Content-Range' , `bytes */${ stats . size } ` )
63
- req . respond ( { } )
64
- return res
57
+ if ( start >= stats . size || end >= stats . size ) {
58
+ res . status = 416
59
+ res . headers ?. set ( 'Content-Range' , `bytes */${ stats . size } ` )
60
+ req . respond ( { } )
61
+ return res
62
+ }
63
+ headers [ 'Content-Range' ] = `bytes ${ start } -${ end } /${ stats . size } `
64
+ headers [ 'Content-Length' ] = `${ end - start + 1 } `
65
+ headers [ 'Accept-Ranges' ] = 'bytes'
65
66
}
66
- headers [ 'Content-Range' ] = `bytes ${ start } -${ end } /${ stats . size } `
67
- headers [ 'Content-Length' ] = `${ end - start + 1 } `
68
- headers [ 'Accept-Ranges' ] = 'bytes'
69
- }
70
67
71
- for ( const [ k , v ] of Object . entries ( headers ) ) res . headers ?. set ( k , v )
68
+ for ( const [ k , v ] of Object . entries ( headers ) ) res . headers ?. set ( k , v )
72
69
73
- res . status = status
70
+ res . status = status
74
71
75
- const file = Deno . openSync ( filePath , { read : true , ...options } )
72
+ const file = Deno . openSync ( filePath , { read : true , ...options } )
76
73
77
- send ( req , res ) ( file )
74
+ send ( req , res ) ( file )
78
75
79
- return res
80
- }
76
+ return res
77
+ }
0 commit comments