1
1
/* Copyright Contributors to the Open Cluster Management project */
2
- import { createReadStream } from 'fs'
2
+ import { createReadStream , Stats } from 'fs'
3
+ import { stat } from 'fs/promises'
3
4
import { constants , Http2ServerRequest , Http2ServerResponse } from 'http2'
4
5
import { extname } from 'path'
5
6
import { pipeline } from 'stream'
6
- import { parseCookies } from '../lib/cookies'
7
7
import { logger } from '../lib/logger'
8
- import { redirect } from '../lib/respond'
9
8
10
9
const cacheControl = process . env . NODE_ENV === 'production' ? 'public, max-age=604800' : 'no-store'
11
10
12
- export function serve ( req : Http2ServerRequest , res : Http2ServerResponse ) : void {
11
+ export async function serve ( req : Http2ServerRequest , res : Http2ServerResponse ) : Promise < void > {
13
12
try {
14
13
let url = req . url
15
14
@@ -41,15 +40,50 @@ export function serve(req: Http2ServerRequest, res: Http2ServerResponse): void {
41
40
logger . debug ( 'unknown content type' , `ext=${ ext } ` )
42
41
return res . writeHead ( 404 ) . end ( )
43
42
}
43
+
44
+ const filePath = './public' + url
45
+ let stats : Stats
46
+ try {
47
+ stats = await stat ( filePath )
48
+ } catch {
49
+ return res . writeHead ( 404 ) . end ( )
50
+ }
51
+
52
+ if ( / \b b r \b / . test ( acceptEncoding ) ) {
53
+ try {
54
+ const brStats = await stat ( filePath + '.br' )
55
+ const readStream = createReadStream ( './public' + url + '.br' , { autoClose : true } )
56
+ readStream
57
+ . on ( 'open' , ( ) => {
58
+ res . writeHead ( 200 , {
59
+ [ constants . HTTP2_HEADER_CONTENT_ENCODING ] : 'br' ,
60
+ [ constants . HTTP2_HEADER_CONTENT_TYPE ] : contentType ,
61
+ [ constants . HTTP2_HEADER_CONTENT_LENGTH ] : brStats . size . toString ( ) ,
62
+ } )
63
+ } )
64
+ . on ( 'error' , ( err ) => {
65
+ // logger.error(err)
66
+ res . writeHead ( 404 ) . end ( )
67
+ } )
68
+ pipeline ( readStream , res as unknown as NodeJS . WritableStream , ( err ) => {
69
+ // if (err) logger.error(err)
70
+ } )
71
+ return
72
+ } catch {
73
+ // Do nothing
74
+ }
75
+ }
76
+
44
77
if ( / \b g z i p \b / . test ( acceptEncoding ) ) {
45
78
try {
79
+ const gzStats = await stat ( filePath + '.gz' )
46
80
const readStream = createReadStream ( './public' + url + '.gz' , { autoClose : true } )
47
81
readStream
48
82
. on ( 'open' , ( ) => {
49
83
res . writeHead ( 200 , {
50
84
[ constants . HTTP2_HEADER_CONTENT_ENCODING ] : 'gzip' ,
51
85
[ constants . HTTP2_HEADER_CONTENT_TYPE ] : contentType ,
52
- // [constants.HTTP2_HEADER_CONTENT_LENGTH]: stats .size.toString(),
86
+ [ constants . HTTP2_HEADER_CONTENT_LENGTH ] : gzStats . size . toString ( ) ,
53
87
} )
54
88
} )
55
89
. on ( 'error' , ( err ) => {
@@ -59,27 +93,27 @@ export function serve(req: Http2ServerRequest, res: Http2ServerResponse): void {
59
93
pipeline ( readStream , res as unknown as NodeJS . WritableStream , ( err ) => {
60
94
// if (err) logger.error(err)
61
95
} )
62
- } catch ( err ) {
63
- logger . error ( err )
64
- return res . writeHead ( 404 ) . end ( )
96
+ return
97
+ } catch {
98
+ // Do nothing
65
99
}
66
- } else {
67
- const readStream = createReadStream ( './public' + url , { autoClose : true } )
68
- readStream
69
- . on ( 'open' , ( ) => {
70
- res . writeHead ( 200 , {
71
- [ constants . HTTP2_HEADER_CONTENT_TYPE ] : contentType ,
72
- } )
73
- } )
74
- . on ( 'error' , ( err ) => {
75
- // logger.error(err)
76
- res . writeHead ( 404 ) . end ( )
100
+ }
101
+
102
+ const readStream = createReadStream ( './public' + url , { autoClose : true } )
103
+ readStream
104
+ . on ( 'open' , ( ) => {
105
+ res . writeHead ( 200 , {
106
+ [ constants . HTTP2_HEADER_CONTENT_TYPE ] : contentType ,
107
+ [ constants . HTTP2_HEADER_CONTENT_LENGTH ] : stats . size . toString ( ) ,
77
108
} )
78
- pipeline ( readStream , res as unknown as NodeJS . WritableStream , ( err ) => {
79
- // if (err) logger.error(err)
80
109
} )
81
- }
82
- return
110
+ . on ( 'error' , ( err ) => {
111
+ // logger.error(err)
112
+ res . writeHead ( 404 ) . end ( )
113
+ } )
114
+ pipeline ( readStream , res as unknown as NodeJS . WritableStream , ( err ) => {
115
+ // if (err) logger.error(err)
116
+ } )
83
117
} catch ( err ) {
84
118
logger . error ( err )
85
119
return res . writeHead ( 404 ) . end ( )
0 commit comments