diff --git a/index.js b/index.js index 7c434e5..224d9f6 100644 --- a/index.js +++ b/index.js @@ -49,11 +49,6 @@ function fastifyCompress (fastify, opts, next) { // add onSend hook onto each route as needed fastify.addHook('onRoute', (routeOptions) => { - // If route config.compress has been set it takes precedence over compress - if (routeOptions.config?.compress !== undefined) { - routeOptions.compress = routeOptions.config.compress - } - // Manage compression options if (routeOptions.compress !== undefined) { if (typeof routeOptions.compress === 'object') { @@ -78,11 +73,6 @@ function fastifyCompress (fastify, opts, next) { buildRouteCompress(fastify, globalCompressParams, routeOptions, true) } - // If route config.decompress has been set it takes precedence over compress - if (routeOptions.config?.decompress !== undefined) { - routeOptions.decompress = routeOptions.config.decompress - } - // Manage decompression options if (routeOptions.decompress !== undefined) { if (typeof routeOptions.decompress === 'object') { diff --git a/test/routes-compress.test.js b/test/routes-compress.test.js index fc5e5bf..165c621 100644 --- a/test/routes-compress.test.js +++ b/test/routes-compress.test.js @@ -330,95 +330,6 @@ describe('When `compress.removeContentLengthHeader` is `false`, it should not re }) }) -describe('When using the old routes `{ config: compress }` option :', async () => { - test('it should compress data using the route custom provided `createGzip` method', async (t) => { - t.plan(10) - const equal = t.assert.equal - - let usedCustomGlobal = false - let usedCustom = false - const customZlibGlobal = { createGzip: () => (usedCustomGlobal = true) && zlib.createGzip() } - const customZlib = { createGzip: () => (usedCustom = true) && zlib.createGzip() } - - const fastify = Fastify() - await fastify.register(compressPlugin, { global: false, zlib: customZlibGlobal }) - - fastify.get('/', (_request, reply) => { - reply - .type('text/plain') - .compress(createReadStream('./package.json')) - }) - - fastify.get('/custom', { - config: { - compress: { zlib: customZlib } - } - }, (_request, reply) => { - reply - .type('text/plain') - .compress(createReadStream('./package.json')) - }) - - await fastify.inject({ - url: '/', - method: 'GET', - headers: { - 'accept-encoding': 'gzip' - } - }).then((response) => { - equal(usedCustom, false) - equal(usedCustomGlobal, true) - - const file = readFileSync('./package.json', 'utf8') - const payload = zlib.gunzipSync(response.rawPayload) - equal(response.headers.vary, 'accept-encoding') - equal(response.headers['content-encoding'], 'gzip') - equal(payload.toString('utf-8'), file) - - usedCustom = false - usedCustomGlobal = false - }) - - const response = await fastify.inject({ - url: '/custom', - method: 'GET', - headers: { - 'accept-encoding': 'gzip' - } - }) - equal(usedCustom, true) - equal(usedCustomGlobal, false) - - const file = readFileSync('./package.json', 'utf8') - const payload = zlib.gunzipSync(response.rawPayload) - equal(response.headers.vary, 'accept-encoding') - equal(response.headers['content-encoding'], 'gzip') - equal(payload.toString('utf-8'), file) - }) - - test('it should use the old routes `{ config: compress }` options over routes `compress` options', async (t) => { - t.plan(1) - - const fastify = Fastify() - await fastify.register(compressPlugin, { global: false }) - - try { - fastify.get('/', { - compress: { - zlib: { createGzip: () => zlib.createGzip() } - }, - config: { - compress: 'bad config' - } - }, (_request, reply) => { - reply.send('') - }) - } catch (err) { - t.assert.equal(err.message, 'Unknown value for route compress configuration') - } - }) -}) - test('It should avoid to trigger `onSend` hook twice', async (t) => { t.plan(1) diff --git a/test/routes-decompress.test.js b/test/routes-decompress.test.js index 36f32a5..6521cb6 100644 --- a/test/routes-decompress.test.js +++ b/test/routes-decompress.test.js @@ -233,88 +233,3 @@ describe('When using routes `decompress` settings :', async () => { } }) }) - -describe('When using the old routes `{ config: decompress }` option :', async () => { - test('it should decompress data using the route custom provided `createGunzip` method', async (t) => { - t.plan(8) - const equal = t.assert.equal - - let usedCustomGlobal = false - let usedCustom = false - const customZlibGlobal = { createGunzip: () => (usedCustomGlobal = true) && zlib.createGunzip() } - const customZlib = { createGunzip: () => (usedCustom = true) && zlib.createGunzip() } - - const fastify = Fastify() - await fastify.register(compressPlugin, { zlib: customZlibGlobal }) - - fastify.post('/', (request, reply) => { - reply.send(request.body.name) - }) - - fastify.post('/custom', { - config: { - decompress: { - zlib: customZlib - } - } - }, (request, reply) => { - reply.send(request.body.name) - }) - - await fastify.inject({ - url: '/', - method: 'POST', - headers: { - 'content-type': 'application/json', - 'content-encoding': 'gzip' - }, - payload: createPayload(zlib.createGzip) - }).then((response) => { - equal(usedCustom, false) - equal(usedCustomGlobal, true) - - equal(response.statusCode, 200) - equal(response.body, '@fastify/compress') - - usedCustom = false - usedCustomGlobal = false - }) - - const response = await fastify.inject({ - url: '/custom', - method: 'POST', - headers: { - 'content-type': 'application/json', - 'content-encoding': 'gzip' - }, - payload: createPayload(zlib.createGzip) - }) - equal(usedCustom, true) - equal(usedCustomGlobal, false) - - equal(response.statusCode, 200) - equal(response.body, '@fastify/compress') - }) - - test('it should use the old routes `{ config: decompress }` options over routes `decompress` options', async (t) => { - t.plan(1) - - const fastify = Fastify() - await fastify.register(compressPlugin, { global: false }) - - try { - fastify.post('/', { - decompress: { - zlib: { createGunzip: () => zlib.createGunzip() } - }, - config: { - decompress: 'bad config' - } - }, (request, reply) => { - reply.send(request.body.name) - }) - } catch (err) { - t.assert.equal(err.message, 'Unknown value for route decompress configuration') - } - }) -}) diff --git a/types/index.d.ts b/types/index.d.ts index 77142cb..c805dbb 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -10,13 +10,6 @@ import { Stream } from 'node:stream' import { BrotliOptions, ZlibOptions } from 'node:zlib' declare module 'fastify' { - export interface FastifyContextConfig { - /** @deprecated `config.compress` is deprecated, use `compress` shorthand option instead */ - compress?: RouteCompressOptions | false; - /** @deprecated `config.decompress` is deprecated, use `decompress` shorthand option instead */ - decompress?: RouteDecompressOptions | false; - } - export interface RouteShorthandOptions< // eslint-disable-next-line @typescript-eslint/no-unused-vars RawServer extends RawServerBase = RawServerDefault @@ -97,13 +90,6 @@ declare namespace fastifyCompress { export interface RouteOptions extends FastifyRouteOptions, FastifyCompressRouteOptions { } - export interface RoutesConfigCompressOptions { - /** @deprecated `config.compress` is deprecated, use `compress` shorthand option instead */ - compress?: RouteCompressOptions | false; - /** @deprecated `config.decompress` is deprecated, use `decompress` shorthand option instead */ - decompress?: RouteDecompressOptions | false; - } - export const fastifyCompress: FastifyCompress export { fastifyCompress as default } }