Benzinga / lz4js

Lz4 for the browser.
89 stars 21 forks source link

Unable to decompress output of `lz4c` #7

Open paddor opened 4 years ago

paddor commented 4 years ago

It seems lz4js is unable to decompress the output of lz4c. I have simply Base64-encoded the output of echo 'hello world' | lz4c, put it in a JSON message, sent it over a websocket to the browser, had the browser do this:

  onMessage(event) {
    const encoder = new TextEncoder();
    const decoder = new TextDecoder();

    try  {
      let payload_b64 = event.data;
      console.info("b64 payload", payload_b64);
      let payload_bin = atob(payload_b64);
      console.info("bin payload", payload_bin);
      let payload_uint8 = encoder.encode(payload_bin);
      console.info("compressed bin payload (Array)", payload_uint8);
      let payload = lz4js.decompress(payload_uint8);
      console.info("decompressed payload (Uint8Array)", payload);
      payload = decoder.decode(payload);
      console.info("payload (JSON)", payload);
      payload = JSON.parse(payload);
      console.info("payload", payload);

    } catch (e) {
      if (e instanceof SyntaxError) {
        console.error("received invalid message", event.data);
        return;
      } else {
        console.error("error", e);
      }
    }

Btw, maybe this is related to the same issue, but this does not work as intended:

console.log('LZ4 round trip', lz4js.decompress(lz4js.compress("bla bla bla")));
kylebarron commented 2 years ago

but this does not work as intended

The lz4js API works on arrays. You can't pass a string to it. This works:

> lz4js.decompress(lz4js.compress(new Uint8Array([1, 2, 3, 4])))
Uint8Array(4) [ 1, 2, 3, 4 ]