foliojs / brotli.js

A JavaScript port of the Brotli compression algorithm, as used in WOFF2
500 stars 51 forks source link

[ReadHuffmanCode] invalid num_codes or space in decompress method #31

Open rajatraj733 opened 4 years ago

rajatraj733 commented 4 years ago

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

rajatraj733 commented 4 years ago

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.

crimson-med commented 4 years ago

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

auburnsummer commented 3 years ago

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.

darvesh commented 3 years ago

compress

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

decompress

const arrayBuff = decompress(Buffer.from(foo));
const str = Buffer.from(arrayBuff).toString("utf-8");
Amnesthesia commented 9 months ago

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