Skip to content

Commit

Permalink
Extract out compression login to shared function
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed Nov 28, 2024
1 parent e7bc27d commit 2150125
Showing 1 changed file with 22 additions and 39 deletions.
61 changes: 22 additions & 39 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import * as logger from "./logger.js";
interface CompressionOptions {
dir: string;
extensions: Array<string>;
enabled?: boolean;
batchSize?: number;
batchSize: number;
enabled: boolean | undefined;
}

async function* walkDir(dir: string, extensions: Array<string>): AsyncGenerator<string> {
Expand All @@ -30,16 +30,14 @@ const filterFile = (file: string, extensions: Array<string>): boolean => {
return extensions.some((ext) => extname(file) === ext);
};

// const compress = async <T>(name: string, compressor: () => T, opts: CompressionOptions): Promise<void> => {};

export const gzip = async (
dir: string,
extensions: Array<string>,
enabled?: boolean,
batchSize = 10,
const compress = async <T extends NodeJS.WritableStream>(
name: string,
compressedFileNames: string,
compressor: () => T,
{ dir, extensions, batchSize, enabled }: CompressionOptions,
): Promise<void> => {
if (!enabled) {
logger.warn("gzip compression disabled, skipping...");
logger.warn(`${name} compression disabled, skipping...`);
return;
}

Expand All @@ -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<string>,
enabled?: boolean,
batchSize = 10,
): Promise<void> => {
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<string>,
enabled?: boolean,
batchSize = 10,
): Promise<void> => {
await compress("brotli", "br", createBrotliCompress, { dir, extensions, enabled, batchSize });
};

0 comments on commit 2150125

Please sign in to comment.