Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ compress({
| `--from` (alias `--input`) | `from` (alias `input`) | Input folder | `process.cwd()` |
| `--to` (alias `--output`) | `to` (alias `output`) | Output folder | `from` param value |
| `--formats` | `formats` | Formats of output files | `['gzip', 'brotli']`|
| `--with-zopfli` | `withZopfli` | Enable zopfli algorithm for gzip | `false` |
| `--ext-white-list` | `extWhiteList` | A list of extensions that will be used to filter the necessary files | `['.html', '.css', '.js', '.json', '.svg', '.txt', '.xml']` |
| `--concurrency` | `concurrency` | number of parallel handlers | `os.cpus().length` |
| `--file-size` | `fileSize` | File size treshold in bytes. Files smaller than this size will be ignored | `0` |
Expand Down
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ compress({
formats,
extWhiteList: params['ext-white-list'],
concurrency: params['concurrency'],
fileSize: params['file-size']
fileSize: params['file-size'],
withZopfli: params['with-zopfli']
})
.then(() => {
const diffTime = Math.round(performance.now() - startTime);
Expand Down
39 changes: 25 additions & 14 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,31 @@ const FORMATS = {

exports.FORMATS = FORMATS;

exports.DEFAULT_COMPRESS_SETTINGS = {
[FORMATS.GZIP]: {
level: zlib.constants.Z_BEST_COMPRESSION
},
[FORMATS.BROTLI]: {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY
}
},
[FORMATS.ZSTD]: {
params: {
[zlib.constants.ZSTD_c_compressionLevel]: 22,
[zlib.constants.ZSTD_c_checksumFlag]: 1,
exports.DEFAULT_COMPRESS_SETTINGS = (params) => {
const { withZopfli = false } = params;
return {
[FORMATS.GZIP]: withZopfli
? {
verbose: false,
verbose_more: false,
numiterations: 15,
blocksplitting: true,
blocksplittingmax: 15,
}
: {
level: zlib.constants.Z_BEST_COMPRESSION
},
[FORMATS.BROTLI]: {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY
}
},
[FORMATS.ZSTD]: {
params: {
[zlib.constants.ZSTD_c_compressionLevel]: 22,
[zlib.constants.ZSTD_c_checksumFlag]: 1,
}
}
}
}
Expand Down
49 changes: 34 additions & 15 deletions lib/functions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const os = require('os');
const fs = require('fs');
const fsp = require('fs/promises');
const path = require('path');
const { pipeline } = require('stream/promises');
const stream = require('stream');
const zlib = require('zlib');
const { Buffer } = require('buffer')
const { gzipAsync } = require('@gfx/zopfli');

const CONSTANTS = require('./constants');
const { walk, makeDir } = require('./fs-utils');
Expand All @@ -16,20 +17,37 @@ const {
flatMap
} = require('./generators-utils');


const FORMAT_TO_STREAM = {
[CONSTANTS.FORMATS.GZIP]: zlib.createGzip,
[CONSTANTS.FORMATS.BROTLI]: zlib.createBrotliCompress,
[CONSTANTS.FORMATS.ZSTD]: zlib.createZstdCompress || (() => {
throw new Error(`Your version of Node.js doesn't support zstd. Node.js has zstd support since 23.8.0 and 22.15.0.`)
})
const FORMAT_TO_STREAM = (params) => {
const { withZopfli = false } = params;
return {
[CONSTANTS.FORMATS.GZIP]:
withZopfli
? (
function createZopfli(options) {
return stream.Duplex.from(async function* (source) {
const chunks = [];
for await (const chunk of source) {
chunks.push(chunk);
}
yield await gzipAsync(Buffer.concat(chunks), options);
})
}
)
: zlib.createGzip,
[CONSTANTS.FORMATS.BROTLI]: zlib.createBrotliCompress,
[CONSTANTS.FORMATS.ZSTD]: zlib.createZstdCompress || (() => {
throw new Error(`Your version of Node.js doesn't support zstd. Node.js has zstd support since 23.8.0 and 22.15.0.`)
})
}
}


function createCompressStream({ fromPath, toPath, format, compressOptions }) {
return pipeline(
function createCompressStream({ fromPath, toPath, format, compressOptions, withZopfli }) {
const params = { withZopfli }

return stream.promises.pipeline(
fs.createReadStream(fromPath),
FORMAT_TO_STREAM[format](compressOptions || CONSTANTS.DEFAULT_COMPRESS_SETTINGS[format]),
FORMAT_TO_STREAM(params)[format](compressOptions || CONSTANTS.DEFAULT_COMPRESS_SETTINGS[format]),
fs.createWriteStream(toPath + CONSTANTS.FORMAT_TO_EXT[format])
);
}
Expand All @@ -51,14 +69,15 @@ async function compress(params) {
formats = [CONSTANTS.FORMATS.GZIP],
extWhiteList = CONSTANTS.EXT_WHITE_LIST,
concurrency = getConcurrency(),
fileSize
fileSize,
withZopfli = false
} = params;

const handlers = [
filter(item => item.dirent.isFile()),
filter(item => extWhiteList.includes(path.extname(item.direntPath))),
fileSize && map(async item => {
const stat = await fsp.stat(item.direntPath);
const stat = await fs.promises.stat(item.direntPath);
return {
...item,
size: stat.size
Expand All @@ -83,7 +102,7 @@ async function compress(params) {

await makeDir(toBasePath);

return createCompressStream({ fromPath, toPath, format: item.format });
return createCompressStream({ fromPath, toPath, format: item.format, withZopfli });
}
}),
].filter(Boolean);
Expand Down
51 changes: 51 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
"zstandard",
"archive",
"CLI"
]
],
"dependencies": {
"@gfx/zopfli": "^1.0.15"
}
}