js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
38 lines (37 loc) • 1.29 kB
JavaScript
let bunzipPromise = null;
let bufferCtor = null;
const loadBunzip = async () => {
if (!bunzipPromise) {
bunzipPromise = import("seek-bzip").then((mod) => mod.default || mod);
}
return bunzipPromise;
};
const ensureBufferCtor = async () => {
if (bufferCtor) {
return bufferCtor;
}
const existing = globalThis.Buffer;
if (existing) {
bufferCtor = existing;
return bufferCtor;
}
const { Buffer } = await import("buffer");
globalThis.Buffer = Buffer;
bufferCtor = Buffer;
return bufferCtor;
};
/**
* Decompress a bzip2-compressed payload into a Uint8Array.
* Works in both Node.js and browser environments by lazily loading
* the seek-bzip implementation and a Buffer polyfill when necessary.
*/
export const decompressBzip2 = async (data) => {
const input = data instanceof Uint8Array ? data : new Uint8Array(data);
const [Bunzip, BufferCtor] = await Promise.all([loadBunzip(), ensureBufferCtor()]);
const inputBuffer = BufferCtor.from(input);
const decoded = Bunzip.decode(inputBuffer);
const decodedBuffer = BufferCtor.isBuffer(decoded) ? decoded : BufferCtor.from(decoded);
const output = new Uint8Array(decodedBuffer.length);
output.set(decodedBuffer);
return output;
};