diff --git a/index.js b/index.js index a29e710..2a94fcb 100644 --- a/index.js +++ b/index.js @@ -109,6 +109,13 @@ function fastifyCompress (fastify, opts, next) { } const defaultCompressibleTypes = /^text\/(?!event-stream)|(?:\+|\/)json(?:;|$)|(?:\+|\/)text(?:;|$)|(?:\+|\/)xml(?:;|$)|octet-stream(?:;|$)/u +const recommendedDefaultBrotliOptions = { + params: { + // Default of 4 as 11 has a heavy impact on performance. + // https://blog.cloudflare.com/this-is-brotli-from-origin#testing + [zlib.constants.BROTLI_PARAM_QUALITY]: 4 + } +} function processCompressParams (opts) { /* istanbul ignore next */ @@ -117,16 +124,13 @@ 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 = params.global + ? { ...recommendedDefaultBrotliOptions, ...opts.brotliOptions } + : opts.brotliOptions params.zlibOptions = opts.zlibOptions params.onUnsupportedEncoding = opts.onUnsupportedEncoding params.inflateIfDeflated = opts.inflateIfDeflated === true diff --git a/test/global-compress.test.js b/test/global-compress.test.js index 659e2b4..a2c9595 100644 --- a/test/global-compress.test.js +++ b/test/global-compress.test.js @@ -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 compressedPayload = zlib.brotliCompressSync(file, brotliOptions) + t.same(response.rawPayload, compressedPayload) }) - 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 compressedPayload = zlib.brotliCompressSync(file, defaultBrotliOptions) + t.same(response.rawPayload, compressedPayload) }) - - 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) => { diff --git a/types/index.test-d.ts b/types/index.test-d.ts index c102923..959709e 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -40,7 +40,7 @@ app.get('/test-two', async (request, reply) => { expectError(reply.compress()) }) -// Instanciation of an app without global +// Instantiation of an app without global const appWithoutGlobal: FastifyInstance = fastify() appWithoutGlobal.register(fastifyCompress, { global: false }) @@ -107,11 +107,11 @@ appWithoutGlobal.inject( } }, (err) => { - expectType(err) + expectType(err) } ) -// Instanciation of an app that should trigger a typescript error +// Instantiation of an app that should trigger a typescript error const appThatTriggerAnError = fastify() expectError(appThatTriggerAnError.register(fastifyCompress, { global: true,