Git Product home page Git Product logo

Comments (4)

eustas avatar eustas commented on May 9, 2024 1

uInt8ArrayToStr at the end of compressStr should not be used. Result of compression is byte array. It can be even, it can be odd length. Moreover, JS is allowed to replace characters it does not understand with placeholders -> compressed data will be corrupted.

Fixed code is here: https://stackblitz.com/edit/node-5zrzir?file=index.js

from brotli.

ivanmem avatar ivanmem commented on May 9, 2024

Here is an example that I got, but I doubt its correctness. It often throws an error Uncaught RangeError: byte length of Uint16Array should be a multiple of 2 at new Uint16Array. So don't use it.

import { compress, decompress } from "brotli-compress";

function strToUint8Array(str: string) {
  const codeUnits = Uint16Array.from(
    { length: str.length },
    (element, index) => str.charCodeAt(index)
  );
  return new Uint8Array(codeUnits.buffer);
}

function uInt8ArrayToStr(bytes: Uint8Array) {
  const charCodes = new Uint16Array(bytes.buffer);

  let result = "";
  charCodes.forEach((char) => {
    result += String.fromCharCode(char);
  });
  return result;
}

export async function compressStr(str: string) {
  if (str.length === 0) {
    return "";
  }

  const bytes = strToUint8Array(str);
  const compressStr =  await compress(bytes);
  return uInt8ArrayToStr(compressStr);
}

export async function decompressStr(compressStr: string) {
  if (compressStr.length === 0) {
    return "";
  }

  const compressBytes = strToUint8Array(compressStr);
  const decompressBytes = await decompress(compressBytes);
  return uInt8ArrayToStr(
    decompressBytes
  );
}

from brotli.

eustas avatar eustas commented on May 9, 2024

"brotli-compress" is a third party module, so I can only guess.

Brotli compressor is byte-oriented, i.e. it takes (U)Int8Array for input and produces (U)Int8Array as output.

Indeed, internally JS stores strings as UTF-16, but there is no reason to compress UTF-16 data. Brotli does much better work in UTF-8.

Your code snippet looks well. Could you provide a sample that causes the error?

from brotli.

ivanmem avatar ivanmem commented on May 9, 2024

@eustas The error occurs if you run the following code:

const result = await compressStr("test test");

from brotli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.