Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zopfli support #3

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,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
10 changes: 9 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ exports.DEFAULT_COMPRESS_SETTINGS = {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY
}
},
['ZOPFLI']: {
verbose: false,
verbose_more: false,
numiterations: 15,
blocksplitting: true,
blocksplittingmax: 15,
}
}

exports.FORMAT_TO_EXT = {
[FORMATS.GZIP]: '.gz',
[FORMATS.BROTLI]: '.br'
[FORMATS.BROTLI]: '.br',
['ZOPFLI']: '.gz'
}

exports.EXT_WHITE_LIST = [
Expand Down
29 changes: 20 additions & 9 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,15 +17,24 @@ const {
flatMap
} = require('./generators-utils');


const FORMAT_TO_STREAM = {
[CONSTANTS.FORMATS.GZIP]: zlib.createGzip,
[CONSTANTS.FORMATS.BROTLI]: zlib.createBrotliCompress
[CONSTANTS.FORMATS.BROTLI]: zlib.createBrotliCompress,
['ZOPFLI']: function createZopfli() {
return stream.Duplex.from(async function* (source) {
const chunks = [];
for await (const chunk of source) {
chunks.push(chunk);
}
yield await gzipAsync(Buffer.concat(chunks));
})
}
}


function createCompressStream({ fromPath, toPath, format, compressOptions }) {
return pipeline(
function createCompressStream({ fromPath, toPath, format, compressOptions, withZopfli }) {
format = (format === CONSTANTS.FORMATS.GZIP && withZopfli) ? 'ZOPFLI' : format;
return stream.promises.pipeline(
fs.createReadStream(fromPath),
FORMAT_TO_STREAM[format](compressOptions || CONSTANTS.DEFAULT_COMPRESS_SETTINGS[format]),
fs.createWriteStream(toPath + CONSTANTS.FORMAT_TO_EXT[format])
Expand All @@ -48,14 +58,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 @@ -80,7 +91,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 @@ -32,5 +32,8 @@
"brotli",
"archive",
"CLI"
]
],
"dependencies": {
"@gfx/zopfli": "^1.0.15"
}
}