How to compress utf-16 string and decompress? #462

Closed
opened 2026-01-29 20:44:18 +00:00 by claunia · 4 comments
Owner

Originally created by @ivanmem on GitHub (May 31, 2023).

Would love to have an example in the documentation for getting a compressed string and getting the original UTF-16 string (string in javascript).

Originally created by @ivanmem on GitHub (May 31, 2023). Would love to have an example in the documentation for getting a compressed string and getting the original UTF-16 string (string in javascript).
Author
Owner

@ivanmem commented on GitHub (May 31, 2023):

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
  );
}
@ivanmem commented on GitHub (May 31, 2023): 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. ```typescript 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 ); } ```
Author
Owner

@eustas commented on GitHub (Jun 20, 2023):

"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?

@eustas commented on GitHub (Jun 20, 2023): "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?
Author
Owner

@ivanmem commented on GitHub (Jun 20, 2023):

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

const result = await compressStr("test test");
@ivanmem commented on GitHub (Jun 20, 2023): @eustas The error occurs if you run the following code: ``` const result = await compressStr("test test"); ```
Author
Owner

@eustas commented on GitHub (Jul 4, 2023):

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

@eustas commented on GitHub (Jul 4, 2023): `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
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/brotli#462