@@ -32,7 +32,7 @@ function decodePathname(pathname) {
32
32
33
33
34
34
// Check to see if we should try to compress a file with gzip.
35
- function shouldCompressGzip ( req ) {
35
+ function shouldCompress ( req ) {
36
36
const headers = req . headers ;
37
37
38
38
return headers && headers [ 'accept-encoding' ] &&
@@ -42,16 +42,6 @@ function shouldCompressGzip(req) {
42
42
;
43
43
}
44
44
45
- function shouldCompressBrotli ( req ) {
46
- const headers = req . headers ;
47
-
48
- return headers && headers [ 'accept-encoding' ] &&
49
- headers [ 'accept-encoding' ]
50
- . split ( ',' )
51
- . some ( el => [ '*' , 'br' ] . indexOf ( el . trim ( ) ) !== - 1 )
52
- ;
53
- }
54
-
55
45
function hasGzipId12 ( gzipped , cb ) {
56
46
const stream = fs . createReadStream ( gzipped , { start : 0 , end : 1 } ) ;
57
47
let buffer = Buffer ( '' ) ;
@@ -176,8 +166,7 @@ module.exports = function createMiddleware(_dir, _options) {
176
166
const parsed = url . parse ( req . url ) ;
177
167
let pathname = null ;
178
168
let file = null ;
179
- let gzippedFile = null ;
180
- let brotliFile = null ;
169
+ let gzipped = null ;
181
170
182
171
// Strip any null bytes from the url
183
172
// This was at one point necessary because of an old bug in url.parse
@@ -209,9 +198,7 @@ module.exports = function createMiddleware(_dir, _options) {
209
198
path . relative ( path . join ( '/' , baseDir ) , pathname )
210
199
)
211
200
) ;
212
- // determine compressed forms if they were to exist
213
- gzippedFile = `${ file } .gz` ;
214
- brotliFile = `${ file } .br` ;
201
+ gzipped = `${ file } .gz` ;
215
202
216
203
if ( serverHeader !== false ) {
217
204
// Set common headers.
@@ -242,7 +229,7 @@ module.exports = function createMiddleware(_dir, _options) {
242
229
243
230
function serve ( stat ) {
244
231
// Do a MIME lookup, fall back to octet-stream and handle gzip
245
- // and brotli special case.
232
+ // special case.
246
233
const defaultType = opts . contentType || 'application/octet-stream' ;
247
234
let contentType = mime . lookup ( file , defaultType ) ;
248
235
let charSet ;
@@ -251,21 +238,19 @@ module.exports = function createMiddleware(_dir, _options) {
251
238
const etag = generateEtag ( stat , weakEtags ) ;
252
239
let cacheControl = cache ;
253
240
let stream = null ;
241
+
254
242
if ( contentType ) {
255
243
charSet = mime . charsets . lookup ( contentType , 'utf-8' ) ;
256
244
if ( charSet ) {
257
245
contentType += `; charset=${ charSet } ` ;
258
246
}
259
247
}
260
248
261
- if ( file === gzippedFile ) { // is .gz picked up
249
+ if ( file === gzipped ) { // is .gz picked up
262
250
res . setHeader ( 'Content-Encoding' , 'gzip' ) ;
251
+
263
252
// strip gz ending and lookup mime type
264
253
contentType = mime . lookup ( path . basename ( file , '.gz' ) , defaultType ) ;
265
- } else if ( file === brotliFile ) { // is .br picked up
266
- res . setHeader ( 'Content-Encoding' , 'br' ) ;
267
- // strip br ending and lookup mime type
268
- contentType = mime . lookup ( path . basename ( file , '.br' ) , defaultType ) ;
269
254
}
270
255
271
256
if ( typeof cacheControl === 'function' ) {
@@ -416,13 +401,13 @@ module.exports = function createMiddleware(_dir, _options) {
416
401
} ) ;
417
402
}
418
403
419
- // serve gzip file if exists and is valid
420
- function tryServeWithGzip ( ) {
421
- fs . stat ( gzippedFile , ( err , stat ) => {
404
+ // Look for a gzipped file if this is turned on
405
+ if ( opts . gzip && shouldCompress ( req ) ) {
406
+ fs . stat ( gzipped , ( err , stat ) => {
422
407
if ( ! err && stat . isFile ( ) ) {
423
- hasGzipId12 ( gzippedFile , ( gzipErr , isGzip ) => {
408
+ hasGzipId12 ( gzipped , ( gzipErr , isGzip ) => {
424
409
if ( ! gzipErr && isGzip ) {
425
- file = gzippedFile ;
410
+ file = gzipped ;
426
411
serve ( stat ) ;
427
412
} else {
428
413
statFile ( ) ;
@@ -432,29 +417,6 @@ module.exports = function createMiddleware(_dir, _options) {
432
417
statFile ( ) ;
433
418
}
434
419
} ) ;
435
- }
436
-
437
- // serve brotli file if exists, otherwise try gzip
438
- function tryServeWithBrotli ( shouldTryGzip ) {
439
- fs . stat ( brotliFile , ( err , stat ) => {
440
- if ( ! err && stat . isFile ( ) ) {
441
- file = brotliFile ;
442
- serve ( stat ) ;
443
- } else if ( shouldTryGzip ) {
444
- tryServeWithGzip ( ) ;
445
- } else {
446
- statFile ( ) ;
447
- }
448
- } ) ;
449
- }
450
-
451
- const shouldTryBrotli = opts . brotli && shouldCompressBrotli ( req ) ;
452
- const shouldTryGzip = opts . gzip && shouldCompressGzip ( req ) ;
453
- // always try brotli first, next try gzip, finally serve without compression
454
- if ( shouldTryBrotli ) {
455
- tryServeWithBrotli ( shouldTryGzip ) ;
456
- } else if ( shouldTryGzip ) {
457
- tryServeWithGzip ( ) ;
458
420
} else {
459
421
statFile ( ) ;
460
422
}
0 commit comments