Skip to content

Commit

Permalink
feat: compress tool zlib has be changed to lz4
Browse files Browse the repository at this point in the history
  • Loading branch information
easy1090 committed Jul 23, 2024
1 parent d3c9a19 commit ada0625
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/utils/src/common/algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ export function mergeIntervals(intervals: [number, number][]) {
}

export function compressText(input: string): string {
// LZ4 can only work on Buffers
const data = Buffer.from(input);
const output = lz4.encode(data);
const inputBuffer = Buffer.from(input);
let output = Buffer.alloc(lz4.encodeBound(input.length));
const compressedSize = lz4.encodeBlock(inputBuffer, output);
// remove unnecessary bytes
output = output.slice(0, compressedSize);
return output.toString('base64');
}

export function decompressText(input: string): string {
const inputB = Buffer.from(input, 'utf8');
const uncompressed = lz4.decode(inputB);
return uncompressed.toString('utf-8');
let uncompressedBlock = Buffer.alloc(input.length * 10);
const inputBuffer = Buffer.from(input, 'base64');
const uncompressedSize = lz4.decodeBlock(inputBuffer, uncompressedBlock);
uncompressedBlock = uncompressedBlock.slice(0, uncompressedSize);
return uncompressedBlock.toString('utf-8');
}

export function random(min: number, max: number) {
Expand Down
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ada0625

Please sign in to comment.