foliojs / brotli.js

A JavaScript port of the Brotli compression algorithm, as used in WOFF2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[ReadHuffmanCode] invalid num_codes or space in decompress method

rajatraj733 opened this issue · comments

I'm using brotli with react version 16.13. By just import brotli in react component file shows this error.
This is the error stack:

Uncaught TypeError: Cannot read property 'slice' of null
    at encode.js:11
    at Object.<anonymous> (encode.js:11)
    at Object../node_modules/brotli/build/encode.js (encode.js:56)
    at __webpack_require__ (bootstrap:784)
    at fn (bootstrap:150)
    at Object../node_modules/brotli/compress.js (compress.js:1)
    at __webpack_require__ (bootstrap:784)
    at fn (bootstrap:150)
    at Object../node_modules/brotli/index.js (index.js:1)
    at __webpack_require__ (bootstrap:784)
    at fn (bootstrap:150)
    at Module../src/actions/appActions.js (role-resource-constants.js:91)

Brotli version - 1.3.2

As I wanted to use just decompress method hence, I avoided this issue by:

import decompress from 'brotli/decompress'

Now, I got into another issue:

index.js:1 Error: [ReadHuffmanCode] invalid num_codes or space
    at ReadHuffmanCode (decode.js:323)
    at HuffmanTreeGroup.push../node_modules/brotli/dec/decode.js.HuffmanTreeGroup.decode (decode.js:392)
    at BrotliDecompress (decode.js:747)
    at BrotliDecompressBuffer (decode.js:583)
    at _callee$ (appActions.js:104)

I'm pretty sure there is no space in the compressed string.

Had the same issue so ended up using the nodejs builtin zlib library

For me, I found that I got this error when I inadvertently passed an ArrayBuffer to decompress instead of a Buffer.

I used Buffer.from (https://www.npmjs.com/package/buffer) to create a Buffer from the ArrayBuffer first, then the decompress function worked.

compress

const foo = compress(Buffer.from(str));

decompress

const arrayBuff = decompress(Buffer.from(foo));
const str = Buffer.from(arrayBuff).toString("utf-8");

I ran into this as well and the solution was to pass Buffer.from(compressed, 'binary') the second argument 'binary'