Skip to content

Commit

Permalink
Fix compression
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed Aug 27, 2022
1 parent 5b821f3 commit 0067f9c
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,37 @@ import { createBrotliCompress, createGzip } from "node:zlib";
import { Logger } from "./logger.js";

const filterFile = (file: string): boolean => {
return [".css", ".js", ".html", ".xml", ".cjs", ".mjs", ".svg", ".txt"].every((ext) =>
return [".css", ".js", ".html", ".xml", ".cjs", ".mjs", ".svg", ".txt"].some((ext) =>
file.endsWith(ext),
);
};

export const gzip = async (dir: URL): Promise<void> => {
const start = hrtime.bigint();

const files = (await globby(dir.pathname)).filter(filterFile);
const files = (await globby(`${dir.pathname}/**/*`)).filter(filterFile);
for (const file of files) {
if (filterFile(file)) continue;
const source = createReadStream(file);
const destination = createWriteStream(`${file}.gz`);
const gzip = createGzip({ level: 9 });
await stream.pipeline(source, gzip, destination);
}

const end = hrtime.bigint();

Logger.info(`finished gzip of ${files.length} files in ${(end - start) / 1000000n}m`);
Logger.success(`finished gzip of ${files.length} files in ${(end - start) / 1000000n}m`);
};

export const brotli = async (dir: URL): Promise<void> => {
const start = hrtime.bigint();

const files = (await globby(dir.pathname)).filter(filterFile);
const files = (await globby(`${dir.pathname}/**/*`)).filter(filterFile);
for (const file of files) {
if (filterFile(file)) continue;
const source = createReadStream(file);
const destination = createWriteStream(`${file}.br`);
const brotli = createBrotliCompress();
await stream.pipeline(source, brotli, destination);
}

const end = hrtime.bigint();

Logger.info(`finished brotli of ${files.length} files in ${(end - start) / 1000000n}m`);
Logger.success(`finished brotli of ${files.length} files in ${(end - start) / 1000000n}m`);
};

0 comments on commit 0067f9c

Please sign in to comment.