pierrec / node-lz4

LZ4 fast compression algorithm for NodeJS
MIT License
438 stars 98 forks source link

browser examples do not work with latest build? #83

Closed mbec-lbto closed 5 years ago

mbec-lbto commented 5 years ago

example/uncompress.html produces

Error: Invalid stream descriptor checksum @6

[edited out irrelevant compress.html output]

mbec-lbto commented 5 years ago

Here's another example that might provides additional insights:

<script type="text/javascript">

  var Buffer = require('buffer').Buffer
  var lz4 = require('lz4')

  var data = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
  data += data

  const inp = new Buffer(data)
  console.log("inp", inp.toString('hex'))

  const enc = lz4.encode(inp)
  console.log( "enc", enc.toString('hex' ))

  const dec = lz4.decode(enc)
  console.log( "dec", dec.toString('hex' ) )

</script>

produces:

inp 4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e7365637465747572206164697069736963696e6720656c69742c2073656420646f20656975736d6f642074656d706f7220696e6369646964756e74207574206c61626f726520657420646f6c6f7265206d61676e6120616c697175612e4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e7365637465747572206164697069736963696e6720656c69742c2073656420646f20656975736d6f642074656d706f7220696e6369646964756e74207574206c61626f726520657420646f6c6f7265206d61676e6120616c697175612e

enc
64704d180000808c00000072656d20697073756d20646f6c6f722073697420616d65742c20636f6e7365637465747572206164697069736963696e6720656c69742c2073656420646f20656975736d6f642074656d706f7220696e6369646964756e74207574206c61626f72652065745c00ff0065206d61676e6120616c697175612e7c005dc061676e6120616c697175612e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007c9767e3

Invalid magic number: 184D7064

magic expected value is 184D2204

mbec-lbto commented 5 years ago

Going down the rabbit hole, there is an issue with Buffer.prototype.slice - that does not slice.

In chrome debugger:

    output.writeInt32LE(lz4_static.MAGICNUMBER, 0, true)

    this.state = STATES.DESCRIPTOR
    var descriptor = output.slice(SIZES.MAGIC, output.length - 1)

descriptor shows as Uint8Array(7) [4, 34, 77, 24, 0, 0, 0 ...

when it should really be Uint8Array(2) [0, 0, ... and naturally, things start going bad from there

mbec-lbto commented 5 years ago
pierrec commented 5 years ago

Thanks a lot for looking into this.