sendou.ink/app/utils/compression.ts
dependabot[bot] 23cb3878e6
build(deps): bump pako from 2.1.0 to 3.0.1 (#3277)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Kalle <38327916+Sendouc@users.noreply.github.com>
2026-07-31 14:53:27 +03:00

43 lines
1.0 KiB
TypeScript

import { deflateRaw, inflateRaw } from "pako";
/**
* Compresses a string with raw deflate and encodes the result as base64.
* With `urlSafe` the output uses the URL-safe base64 alphabet without padding.
*/
export function compressToBase64(
value: string,
options?: { urlSafe?: boolean },
) {
const bytes = deflateRaw(value, { level: 9 });
let binary = "";
for (const byte of bytes) {
binary += String.fromCharCode(byte);
}
const base64 = btoa(binary);
if (!options?.urlSafe) return base64;
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
}
/**
* Decompresses a base64 encoded (standard or URL-safe alphabet) raw deflate
* string. Returns `null` if the input is corrupt.
*/
export function decompressFromBase64(compressed: string) {
try {
const base64 = compressed.replace(/-/g, "+").replace(/_/g, "/");
const value = inflateRaw(
Uint8Array.from(atob(base64), (c) => c.charCodeAt(0)),
{ toText: true },
);
if (!value) return null;
return value;
} catch {
return null;
}
}