Advertisement
aldikhan13

nodejs custom content encoding using zlib

Sep 23rd, 2020
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const zlib = require('zlib')
  2.  
  3. /**
  4.  * @description custom compression content encoding
  5.  */
  6.  
  7. module.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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement