Skip to content

Commit

Permalink
fix: default brotliOptions does not applied to createBrotliCompress a…
Browse files Browse the repository at this point in the history
…nd createBrotliDecompress
  • Loading branch information
stanleyxu2005 committed Mar 26, 2024
1 parent dd59de3 commit ff2f873
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 30 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ function processCompressParams (opts) {
}

const params = {
global: (typeof opts.global === 'boolean') ? opts.global : true,
/**
* Default of 4 as 11 has a heavy impact on performance.
* @see {@link https://blog.cloudflare.com/this-is-brotli-from-origin#testing}
*/
brotliOptions: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4, ...opts.brotliOptions }
global: (typeof opts.global === 'boolean') ? opts.global : true
}

params.removeContentLengthHeader = typeof opts.removeContentLengthHeader === 'boolean' ? opts.removeContentLengthHeader : true
params.brotliOptions = opts.brotliOptions
params.brotliOptions = {
// Default of 4 as 11 has a heavy impact on performance.
// https://blog.cloudflare.com/this-is-brotli-from-origin#testing
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4 },
...opts.brotliOptions
}
params.zlibOptions = opts.zlibOptions
params.onUnsupportedEncoding = opts.onUnsupportedEncoding
params.inflateIfDeflated = opts.inflateIfDeflated === true
Expand Down
79 changes: 56 additions & 23 deletions test/global-compress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2534,36 +2534,69 @@ test('When `encodings` option is set, it should only use the registered value',
t.equal(response.statusCode, 200)
})

test('It should send data compressed according to `brotliOptions`', async (t) => {
t.plan(2)
const brotliOptions = {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
test('It should send data compressed according to `brotliOptions` :', async (t) => {
t.test('when using br encoding', async (t) => {
t.plan(3)
const brotliOptions = {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: 8
}
}
}

const fastify = Fastify()
await fastify.register(compressPlugin, { global: true, brotliOptions })
const fastify = Fastify()
await fastify.register(compressPlugin, { global: true, brotliOptions })

fastify.get('/', (request, reply) => {
reply
.type('text/plain')
.compress(createReadStream('./package.json'))
fastify.get('/', (request, reply) => {
reply
.type('text/plain')
.compress(createReadStream('./package.json'))
})

const response = await fastify.inject({
url: '/',
method: 'GET',
headers: {
'accept-encoding': 'br'
}
})

const file = readFileSync('./package.json', 'utf8')
const payload = zlib.brotliDecompressSync(response.rawPayload, brotliOptions)
t.equal(response.headers['content-encoding'], 'br')
t.equal(payload.toString('utf-8'), file)

const compressedBufferPayload = zlib.brotliCompressSync(file, brotliOptions)
t.same(response.rawPayload, compressedBufferPayload)
})

const response = await fastify.inject({
url: '/',
method: 'GET',
headers: {
'accept-encoding': 'br'
t.test('default BROTLI_PARAM_QUALITY to be 4', async (t) => {
t.plan(1)

const fastify = Fastify()
await fastify.register(compressPlugin, { global: true })

const file = readFileSync('./package.json', 'utf8')
fastify.get('/', (request, reply) => {
reply
.type('text/plain')
.compress(file)
})

const response = await fastify.inject({
url: '/',
method: 'GET',
headers: {
'accept-encoding': 'br'
}
})

const defaultBrotliOptions = {
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4 }
}
const compressedBufferPayload = zlib.brotliCompressSync(file, defaultBrotliOptions)
t.same(response.rawPayload, compressedBufferPayload)
})

const file = readFileSync('./package.json', 'utf8')
const payload = zlib.brotliDecompressSync(response.rawPayload, brotliOptions)
t.equal(response.headers['content-encoding'], 'br')
t.equal(payload.toString('utf-8'), file)
})

test('It should send data compressed according to `zlibOptions` :', async (t) => {
Expand Down

0 comments on commit ff2f873

Please sign in to comment.