I'm trying to write compress and decompress a JSON string, but the decompression always fails with:
Error: Decompression failed
at BrotliDecoder.zlibOnError [as onerror] (zlib.js:170:17) {
errno: -6,
code: 'ERR_CL_SPACE'
}
I can't send the buffer returned by the compressor, because I need to send the compressed string through an HTTP response to get it back.
Moreover, if I use base64
as format, all work but the string output is bigger than the simple JSON.stringify
, so I would avoid it.
My code:
const zlib = require('zlib')
function compress (json) {
zlib.brotliCompress(JSON.stringify(json), {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT
}
}, (err, data) => {
console.log(err)
console.log(data.toString('utf8'))
console.log('-------------------------------')
decompress(Buffer.from(data.toString('utf8'), 'utf8'))
})
}
function decompress (str) {
zlib.brotliDecompress(str, (err, data) => {
console.log(err)
console.log(data)
})
}
const obj = {
wt: '5de52e98aa54253147060a01',
ex: ['b9ac4a6b-2e72-4bf3-abd5-debf2ece4ba4']
}
compress(obj)
Are there any params to get a valid utf8 string output from the compression?