Skip to content

Commit e6df380

Browse files
authored
Merge pull request #7 from SerayaEryn/support-asterisk-in-accept-encoding
Support asterisk in accept-encoding header
2 parents 9c4086d + f6249ed commit e6df380

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Currently the following headers are supported:
1717
- `'deflate'`
1818
- `'gzip'`
1919
- `'br'`
20+
- `'*'`
21+
22+
If the `'accept-encoding'` header specifies no preferred encoding with an asterisk `*` the payload will be compressed with `gzip`.
2023

2124
If an unsupported encoding is received, it will automatically return a `406` error, if the `'accept-encoding'` header is missing, it will not compress the payload.
2225

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ function getEncodingHeader (request) {
142142
if (supportedEncodings.indexOf(acceptEncodings[i]) > -1) {
143143
return acceptEncodings[i]
144144
}
145+
if (acceptEncodings[i].indexOf('*') > -1) {
146+
return 'gzip'
147+
}
145148
}
146149
return null
147150
}

test.js

+23
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ test('should send a gzipped data', t => {
5656
})
5757
})
5858

59+
test('should send a gzipped data for * header', t => {
60+
t.plan(2)
61+
const fastify = Fastify()
62+
fastify.register(compressPlugin, { global: false })
63+
64+
fastify.get('/', (req, reply) => {
65+
reply.type('text/plain').compress(createReadStream('./package.json'))
66+
})
67+
68+
fastify.inject({
69+
url: '/',
70+
method: 'GET',
71+
headers: {
72+
'accept-encoding': '*'
73+
}
74+
}, res => {
75+
t.strictEqual(res.headers['content-encoding'], 'gzip')
76+
const file = readFileSync('./package.json', 'utf8')
77+
const payload = zlib.gunzipSync(res.rawPayload)
78+
t.strictEqual(payload.toString('utf-8'), file)
79+
})
80+
})
81+
5982
test('should send a brotli data', t => {
6083
t.plan(2)
6184
const fastify = Fastify()

0 commit comments

Comments
 (0)