Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default brotliOptions does not applied to createBrotliCompress and createBrotliDecompress #284

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading