nodejs zlib

Solutions on MaxInterview for nodejs zlib by the best coders in the world

showing results for - "nodejs zlib"
Elouan
23 May 2020
1const zlib = require('zlib')
2
3/**
4 * @description custom compression content encoding
5 */
6
7module.exports = () => {
8  return (req, res, next) => {
9    const compressType = req.headers['accept-encoding'].replace(/[,]/g, '').split(' ')
10
11    if (compressType[0] !== undefined && compressType[0] !== null) {
12      zlib.createGzip({
13        level: zlib.constants.Z_BEST_COMPRESSION,
14        strategy: zlib.constants.Z_RLE,
15        flush: zlib.constants.Z_FULL_FLUSH
16      })
17      return next()
18    }
19
20    if (compressType[1] !== undefined && compressType[1] !== null) {
21      zlib.createDeflate({
22        level: zlib.constants.Z_BEST_COMPRESSION,
23        strategy: zlib.constants.Z_RLE,
24        flush: zlib.constants.Z_FULL_FLUSH
25      })
26      return next()
27    }
28
29    if (compressType[2] !== undefined && compressType[2] !== null) {
30      zlib.createBrotliCompress({
31        flush: zlib.constants.BROTLI_OPERATION_FLUSH
32      })
33      return next()
34    }
35  }
36}