From 2150125759add532175ffdb07d3415f54be1ff48 Mon Sep 17 00:00:00 2001 From: Sondre Aasemoen Date: Thu, 28 Nov 2024 19:49:41 +0100 Subject: [PATCH] Extract out compression login to shared function --- src/compress.ts | 61 ++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/src/compress.ts b/src/compress.ts index efe31b2..5e7acfb 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -10,8 +10,8 @@ import * as logger from "./logger.js"; interface CompressionOptions { dir: string; extensions: Array; - enabled?: boolean; - batchSize?: number; + batchSize: number; + enabled: boolean | undefined; } async function* walkDir(dir: string, extensions: Array): AsyncGenerator { @@ -30,16 +30,14 @@ const filterFile = (file: string, extensions: Array): boolean => { return extensions.some((ext) => extname(file) === ext); }; -// const compress = async (name: string, compressor: () => T, opts: CompressionOptions): Promise => {}; - -export const gzip = async ( - dir: string, - extensions: Array, - enabled?: boolean, - batchSize = 10, +const compress = async ( + name: string, + compressedFileNames: string, + compressor: () => T, + { dir, extensions, batchSize, enabled }: CompressionOptions, ): Promise => { if (!enabled) { - logger.warn("gzip compression disabled, skipping..."); + logger.warn(`${name} compression disabled, skipping...`); return; } @@ -54,46 +52,31 @@ export const gzip = async ( await Promise.all( batch.map(async (path) => { const source = createReadStream(path); - const destination = createWriteStream(`${path}.br`); - const brotli = createGzip({ level: 9 }); - await stream.pipeline(source, brotli, destination); + const destination = createWriteStream(`${path}.${compressedFileNames}`); + const comp = compressor(); + await stream.pipeline(source, comp, destination); }), ); } const end = hrtime.bigint(); - logger.success(`finished gzip of ${files.length} files in ${(end - start) / BigInt(1000000)}ms`); + logger.success(`finished ${name} of ${files.length} files in ${(end - start) / BigInt(1000000)}ms`); }; -export const brotli = async ( +export const gzip = async ( dir: string, extensions: Array, enabled?: boolean, batchSize = 10, ): Promise => { - if (!enabled) { - logger.warn("brotli compression disabled, skipping..."); - return; - } - - const start = hrtime.bigint(); - const files = []; - for await (const file of walkDir(dir, extensions)) { - files.push(file); - } - - for (let i = 0; i < files.length; i += batchSize) { - const batch = files.slice(i, i + batchSize); - await Promise.all( - batch.map(async (path) => { - const source = createReadStream(path); - const destination = createWriteStream(`${path}.br`); - const brotli = createBrotliCompress(); - await stream.pipeline(source, brotli, destination); - }), - ); - } + await compress("gzip", "gz", createGzip.bind({ level: 9 }), { dir, extensions, enabled, batchSize }); +}; - const end = hrtime.bigint(); - logger.success(`finished brotli of ${files.length} files in ${(end - start) / BigInt(1000000)}ms`); +export const brotli = async ( + dir: string, + extensions: Array, + enabled?: boolean, + batchSize = 10, +): Promise => { + await compress("brotli", "br", createBrotliCompress, { dir, extensions, enabled, batchSize }); };