Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 0 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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') {
Expand Down
89 changes: 0 additions & 89 deletions test/routes-compress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
85 changes: 0 additions & 85 deletions test/routes-decompress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
})
})
14 changes: 0 additions & 14 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
}
Expand Down
Loading