Skip to content

Commit 2f27769

Browse files
bredthauhaoxin
authored and
haoxin
committedSep 25, 2017
Fix brotli support
closes #83
1 parent f608522 commit 2f27769

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed
 

‎index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,18 @@ async function send (ctx, path, opts = {}) {
7070
// hidden file support, ignore
7171
if (!hidden && isHidden(root, path)) return
7272

73+
let encodingExt = ''
7374
// serve brotli file when possible otherwise gzipped file when possible
74-
if (ctx.acceptsEncodings('br', 'deflate', 'identity') === 'br' && brotli && (await fs.exists(path + '.br'))) {
75+
if (ctx.acceptsEncodings('br', 'identity') === 'br' && brotli && (await fs.exists(path + '.br'))) {
7576
path = path + '.br'
7677
ctx.set('Content-Encoding', 'br')
7778
ctx.res.removeHeader('Content-Length')
78-
} else if (ctx.acceptsEncodings('gzip', 'deflate', 'identity') === 'gzip' && gzip && (await fs.exists(path + '.gz'))) {
79+
encodingExt = '.br'
80+
} else if (ctx.acceptsEncodings('gzip', 'identity') === 'gzip' && gzip && (await fs.exists(path + '.gz'))) {
7981
path = path + '.gz'
8082
ctx.set('Content-Encoding', 'gzip')
8183
ctx.res.removeHeader('Content-Length')
84+
encodingExt = '.gz'
8285
}
8386

8487
if (extensions && !/\..*$/.exec(path)) {
@@ -133,9 +136,8 @@ async function send (ctx, path, opts = {}) {
133136
}
134137
ctx.set('Cache-Control', directives.join(','))
135138
}
136-
ctx.type = type(path)
139+
ctx.type = type(path, encodingExt)
137140
ctx.body = fs.createReadStream(path)
138-
139141
return path
140142
}
141143

@@ -155,8 +157,8 @@ function isHidden (root, path) {
155157
* File type.
156158
*/
157159

158-
function type (file) {
159-
return extname(basename(file, '.gz'))
160+
function type (file, ext) {
161+
return ext !== '' ? extname(basename(file, ext)) : extname(file)
160162
}
161163

162164
/**

0 commit comments

Comments
 (0)
Please sign in to comment.