http-content-encoding
This middleware take the preferredEncoding output from @middy/http-content-negotiation and applies the encoding to response.body when a string.
Install
To install this middleware you can use NPM:
- npm
 - Yarn
 - pnpm
 - Bun
 
npm install --save @middy/http-content-encoding
yarn add @middy/http-content-encoding
pnpm add @middy/http-content-encoding
bun add @middy/http-content-encoding
Options
br(object) (default{}):zlib.createBrotliCompressbrotliOptionsgzip(object) (default{}):zlib.createGzipgzipOptionsdeflate(object) (default{}):zlib.createDeflatedeflateOptionsoverridePreferredEncoding(array[string]) (optional): Override the preferred encoding order, most browsers prefergzipoverbr, even thoughbrhas higher compression. Default:[]
NOTES:
- Important For 
brencoding NodeJS defaults to11. Levels10&11have been shown to have lower performance for the level of compression they apply. Testing is recommended to ensure the right balance of compression & performance. 
Sample usage
import middy from '@middy/core'
import httpContentNegotiation from '@middy/http-content-negotiation'
import httpContentEncoding from '@middy/http-content-encoding'
import { constants } from 'node:zlib'
export const handler = middy()
  .use(httpContentNegotiation())
  .use(httpContentEncoding({
    br: {
      params: {
        [constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT, // adjusted for UTF-8 text
        [constants.BROTLI_PARAM_QUALITY]: 7
      }
    },
    overridePreferredEncoding: ['br', 'gzip', 'deflate']
  })
  .handler((event, context) => {
    return {
      statusCode: 200,
      body: '{...}'
    }
  })
Using streams
import middy from '@middy/core'
import httpContentNegotiation from '@middy/http-content-negotiation'
import httpContentEncoding from '@middy/http-content-encoding'
import { constants } from 'node:zlib'
import { createReadableStream } from '@datastream/core'
const lambdaHandler = (event, context) => {
  return {
    statusCode: 200,
    body: createReadableStream('{...}')
  }
}
export const handler = middy({ streamifyResponse: true })
  .use(httpContentNegotiation())
  .use(httpContentEncoding({
    br: {
      params: {
        [constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT, // adjusted for UTF-8 text
        [constants.BROTLI_PARAM_QUALITY]: 7
      }
    },
    overridePreferredEncoding: ['br', 'gzip', 'deflate']
  })
  .handler(lambdaHandler)