import type { CompressConfig, CompressionOptions, CompressionModules, CompressionType } from "./types.ts"; import { createCompressConfig } from "./config.ts"; import { encode } from "@bundle/utils/encode-decode"; import { bytes } from "@bundle/utils/fmt"; /** * Use multiple compression algorithims & @std/fmt/bytes for the total gzip, brotli and/or lz4 compressed size * * @param inputs An array of input files to compress * @param opts * The compression algorithim to use, there are currently 4 options "gzip", "zstd", "brotli", and "lz4". * You can also configure the quality of the compression using an object, * * e.g. * ```ts * { * ... * "compression": { * "type": "brotli", * "quality": 5 * } * } * ``` * * @param modules Precompiled compression modules supplied by runtimes that forbid WASM compilation from bytes * @returns The compressed size, the initial size, and the compression format used */ export async function compress( inputs: Uint8Array[] | string[] = [], opts: CompressConfig = {} as CompressionOptions, modules: CompressionModules = {} ) { const { type, quality } = createCompressConfig(opts); // Convert inputs to binary in Uint8Array form const contents = inputs.map((input: Uint8Array | string) => { return input instanceof Uint8Array ? input : encode(input); }); // Total uncompressed size const rawUncompressedSize = contents.reduce((acc, content) => acc + content.byteLength, 0); const uncompressedSize = bytes.format(rawUncompressedSize) as string; // Choose a different compression function based on the compression type const compressionMap = await (async (type?: CompressionType) => { switch (type) { case "brotli": { const { compress, init } = await import("./deno/brotli/mod.ts"); await init(modules.brotli); return async (code: Uint8Array) => await compress(code, code.length, quality ?? undefined); } case "zstd": { const { compress, init } = await import("./deno/zstd/mod.ts"); await init(modules.zstd); return async (code: Uint8Array) => await compress(code, quality ?? undefined); } case "lz4": { const { compress, init } = await import("./deno/lz4/mod.ts"); await init(modules.lz4); return async (code: Uint8Array) => await compress(code); } case "gzip": default: { const { compress } = await import("./deno/gzip/mod.ts"); return async (code: Uint8Array) => { if (!('CompressionStream' in globalThis)) { return await Promise.resolve(code) } return await compress(code); }; } } })(type); // Compress all binary contents according to the compression map const compressedContent = await Promise.all( contents.map((content) => compressionMap(content as Uint8Array)) ); // Convert sizes to human readable formats, e.g. 10000 bytes to 10MB const rawCompressedSize = compressedContent.reduce((acc, { length }) => acc + length, 0); const compressedSize = bytes.format(rawCompressedSize); return { type, content: compressedContent, rawUncompressedSize, uncompressedSize, rawCompressedSize, compressedSize, size: `${compressedSize} (${type})` }; }