Neodymium Plated Magnet

    @middy/http-content-encoding
    TypeScript icon, indicating that this package has built-in type declarations

    3.0.3 • Public • Published

    Middy http-content-encoding middleware

    Middy logo

    HTTP content encoding middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda

    npm version npm install size GitHub Actions CI status badge
    Standard Code Style Known Vulnerabilities Language grade: JavaScript Core Infrastructure Initiative (CII) Best Practices
    Chat on Gitter Ask questions on StackOverflow

    You can read the documentation at: https://middy.js.org/docs/middlewares/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 install --save @middy/http-content-encoding

    Options

    • br (object) (default {}): zlib.createBrotliCompress brotliOptions
    • gzip (object) (default {}): zlib.createGzip gzipOptions
    • deflate (object) (default {}): zlib.createDeflate deflateOptions
    • overridePreferredEncoding (array[string]) (optional): Override the preferred encoding order, most browsers prefer gzip over br, even though br has higher compression. Default: []

    NOTES:

    • Important For br encoding NodeJS defaults to 11. Levels 10 & 11 have 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 'zlib'
    
    const handler = middy((event, context) => {
      return {
        statusCode: 200,
        body: '{...}'
      }
    })
    
    handler
      .use(httpContentNegotiation())
      .use(httpCompressMiddleware({
        br: {
          params: {
            [constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT,  // adjusted for UTF-8 text
            [constants.BROTLI_PARAM_QUALITY]: 7
          }
        },
        overridePreferredEncoding: ['br', 'gzip', 'deflate']
      }))
    
    export default { handler }

    Middy documentation and examples

    For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

    Contributing

    Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.

    License

    Licensed under MIT License. Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the Middy team.

    FOSSA Status

    Install

    npm i @middy/http-content-encoding

    Homepage

    middy.js.org

    DownloadsWeekly Downloads

    20

    Version

    3.0.3

    License

    MIT

    Unpacked Size

    14.2 kB

    Total Files

    6

    Last publish

    Collaborators

    • lmammino
    • middyjs
    • willfarrell