73 lines
2.5 KiB
TypeScript
73 lines
2.5 KiB
TypeScript
/**
|
|
* `compressData` / `decompressData` — algorithm-by-name helpers backed by a
|
|
* `CompressionRegistry`. Mirrors upstream `compression.ts::compressData` /
|
|
* `decompressData` but routes through coded errors and a caller-supplied
|
|
* registry (defaults to the process-wide singleton).
|
|
*
|
|
* Hot-path consumers (`CompressedBitPacker`) use these directly. They
|
|
* throw `BitpackerError(UNKNOWN_COMPRESSION_ALGORITHM)` on missing adapter
|
|
* and wrap codec failures as `BitpackerError(COMPRESSION_FAILED)` /
|
|
* `BitpackerError(DECOMPRESSION_FAILED)` with the underlying error chained.
|
|
*/
|
|
|
|
import { BITPACKER_CODES, BitpackerError } from "@suaveplan/bitpacker";
|
|
import {
|
|
type CompressionRegistry,
|
|
defaultCompressionRegistry,
|
|
} from "../registry/registry.js";
|
|
import type {
|
|
BitPackerCompressionAlgorithm,
|
|
BitPackerCompressionOptions,
|
|
} from "../types/types.js";
|
|
|
|
export async function compressData(
|
|
data: Uint8Array,
|
|
algorithm: BitPackerCompressionAlgorithm,
|
|
options?: BitPackerCompressionOptions,
|
|
registry: CompressionRegistry = defaultCompressionRegistry()
|
|
): Promise<Uint8Array> {
|
|
const adapter = registry.resolve(algorithm);
|
|
try {
|
|
return await adapter.compress(data, options);
|
|
} catch (cause) {
|
|
if (cause instanceof BitpackerError) {
|
|
// Adapter already raised a coded error (e.g. unsupported host).
|
|
throw cause;
|
|
}
|
|
throw new BitpackerError(
|
|
BITPACKER_CODES.COMPRESSION_FAILED,
|
|
`Compression with algorithm "${algorithm}" failed`,
|
|
{
|
|
cause:
|
|
cause instanceof Error ? cause : new Error(String(cause)),
|
|
context: { algorithm },
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function decompressData(
|
|
data: Uint8Array,
|
|
algorithm: BitPackerCompressionAlgorithm,
|
|
options?: BitPackerCompressionOptions,
|
|
registry: CompressionRegistry = defaultCompressionRegistry()
|
|
): Promise<Uint8Array> {
|
|
const adapter = registry.resolve(algorithm);
|
|
try {
|
|
return await adapter.decompress(data, options);
|
|
} catch (cause) {
|
|
if (cause instanceof BitpackerError) {
|
|
throw cause;
|
|
}
|
|
throw new BitpackerError(
|
|
BITPACKER_CODES.DECOMPRESSION_FAILED,
|
|
`Decompression with algorithm "${algorithm}" failed`,
|
|
{
|
|
cause:
|
|
cause instanceof Error ? cause : new Error(String(cause)),
|
|
context: { algorithm },
|
|
}
|
|
);
|
|
}
|
|
}
|