Comparing version 2.1.0 to 2.2.0
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
158
Gzipper.js
@@ -9,9 +9,8 @@ const zlib = require('zlib') | ||
const compressFile = Symbol('compressFile') | ||
const compressionTypeLog = Symbol('compressionTypeLog') | ||
const selectCompression = Symbol('selectCompression') | ||
const getCompressionType = Symbol('getCompressionType') | ||
const DEFAULT_GZIP_LEVEL = -1 | ||
const DEFAULT_GZIP_MEMORY_LEVEL = 8 | ||
const DEFAULT_GZIP_STRATEGY = 0 | ||
/** | ||
* Gzipping files. | ||
* Compressing files. | ||
* | ||
@@ -41,18 +40,6 @@ * @class Gzipper | ||
this.target = path.resolve(process.cwd(), target) | ||
this.compressionMechanism = zlib.createGzip({ | ||
level: | ||
this.options.gzipLevel !== undefined | ||
? this.options.gzipLevel | ||
: DEFAULT_GZIP_LEVEL, | ||
memLevel: | ||
this.options.gzipMemoryLevel !== undefined | ||
? this.options.gzipMemoryLevel | ||
: DEFAULT_GZIP_MEMORY_LEVEL, | ||
strategy: | ||
this.options.gzipStrategy !== undefined | ||
? this.options.gzipStrategy | ||
: DEFAULT_GZIP_STRATEGY, | ||
}) | ||
this.selectCompressionMechanismLog() | ||
const [compression, compressionOptions] = this[selectCompression]() | ||
this.compression = compression | ||
this.compressionOptions = compressionOptions | ||
this[compressionTypeLog]() | ||
} | ||
@@ -121,11 +108,14 @@ | ||
[compressFile](filename, target, outputDir, callback) { | ||
const compressionType = this[getCompressionType]() | ||
const inputPath = path.join(target, filename) | ||
const outputPath = `${path.join(outputDir || target, filename)}.gz` | ||
const outputPath = `${path.join(outputDir || target, filename)}.${ | ||
compressionType.ext | ||
}` | ||
const input = fs.createReadStream(inputPath) | ||
const output = fs.createWriteStream(outputPath) | ||
input.pipe(this.compressionMechanism).pipe(output) | ||
input.pipe(this.compression).pipe(output) | ||
if (callback) { | ||
output.on('finish', () => | ||
output.once('finish', () => { | ||
callback( | ||
@@ -135,4 +125,4 @@ fs.statSync(inputPath).size / 1024, | ||
) | ||
) | ||
output.on('error', error => this.logger.error(error, true)) | ||
}) | ||
output.once('error', error => this.logger.error(error, true)) | ||
} | ||
@@ -155,19 +145,117 @@ } | ||
*/ | ||
selectCompressionMechanismLog() { | ||
let compressionType, | ||
optionsStr = '', | ||
options = new Map([['level', '_level'], ['strategy', '_strategy']]) | ||
[compressionTypeLog]() { | ||
let compressionType = this[getCompressionType](), | ||
options = '' | ||
if (this.compressionMechanism instanceof zlib.Gzip) { | ||
compressionType = 'GZIP' | ||
for (const [key, value] of Object.entries(this.compressionOptions)) { | ||
options += `${key}: ${value}, ` | ||
} | ||
for (const [key, value] of options) { | ||
optionsStr += `${key}: ${this.compressionMechanism[value]}, ` | ||
this.logger.warn(`${compressionType.name} -> ${options.slice(0, -2)}`, true) | ||
} | ||
/** | ||
* Select compression type and options. | ||
* | ||
* @returns {[object, object]} compression instance (Gzip or BrotliCompress) and options | ||
* @memberof Gzipper | ||
*/ | ||
[selectCompression]() { | ||
let options = {} | ||
if (this.options.gzipLevel !== undefined) { | ||
options.gzipLevel = this.options.gzipLevel | ||
} | ||
this.logger.warn(`${compressionType} -> ${optionsStr.slice(0, -2)}`, true) | ||
if (this.options.gzipMemoryLevel !== undefined) { | ||
options.gzipMemoryLevel = this.options.gzipMemoryLevel | ||
} | ||
if (this.options.gzipStrategy !== undefined) { | ||
options.gzipStrategy = this.options.gzipStrategy | ||
} | ||
let compression = zlib.createGzip(options) | ||
if ( | ||
this.options.brotli && | ||
typeof zlib.createBrotliCompress !== 'function' | ||
) { | ||
throw new Error( | ||
`Can't use brotli compression, Node.js >= v11.7.0 required.` | ||
) | ||
} | ||
if (this.options.brotli) { | ||
options = {} | ||
if (this.options.brotliParamMode !== undefined) { | ||
switch (this.options.brotliParamMode) { | ||
case 'default': | ||
options[zlib.constants.BROTLI_PARAM_MODE] = | ||
zlib.constants.BROTLI_MODE_GENERIC | ||
break | ||
case 'text': | ||
options[zlib.constants.BROTLI_PARAM_MODE] = | ||
zlib.constants.BROTLI_MODE_TEXT | ||
break | ||
case 'font': | ||
options[zlib.constants.BROTLI_PARAM_MODE] = | ||
zlib.constants.BROTLI_MODE_FONT | ||
break | ||
default: | ||
options[zlib.constants.BROTLI_PARAM_MODE] = | ||
zlib.constants.BROTLI_MODE_GENERIC | ||
break | ||
} | ||
} | ||
if (this.options.brotliQuality !== undefined) { | ||
options[ | ||
zlib.constants.BROTLI_PARAM_QUALITY | ||
] = this.options.brotliQuality | ||
} | ||
if (this.options.brotliSizeHint !== undefined) { | ||
options[ | ||
zlib.constants.BROTLI_PARAM_SIZE_HINT | ||
] = this.options.brotliSizeHint | ||
} | ||
compression = zlib.createBrotliCompress({ | ||
params: options, | ||
}) | ||
} | ||
return [compression, options] | ||
} | ||
/** | ||
* Get compression type and extension. | ||
* | ||
* @typedef {Object} CompressionType | ||
* @property {string} name compression name | ||
* @property {string} ext compression extension | ||
* | ||
* @returns {CompressionType} compression type and extension | ||
* @memberof Gzipper | ||
*/ | ||
[getCompressionType]() { | ||
if (this.compression instanceof zlib.Gzip) { | ||
return { | ||
name: 'GZIP', | ||
ext: 'gz', | ||
} | ||
} else if (this.compression instanceof zlib.BrotliCompress) { | ||
return { | ||
name: 'BROTLI', | ||
ext: 'br', | ||
} | ||
} | ||
} | ||
} | ||
module.exports = Gzipper |
44
index.js
@@ -7,2 +7,13 @@ #!/usr/bin/env node | ||
const { | ||
GZIPPER_VERBOSE, | ||
GZIPPER_GZIP_LEVEL, | ||
GZIPPER_GZIP_MEMORY_LEVEL, | ||
GZIPPER_GZIP_STRATEGY, | ||
GZIPPER_BROTLI, | ||
GZIPPER_BROTLI_PARAM_MODE, | ||
GZIPPER_BROTLI_QUALITY, | ||
GZIPPER_BROTLI_SIZE_HINT, | ||
} = process.env | ||
program | ||
@@ -24,2 +35,15 @@ .version(version) | ||
) | ||
.option('--brotli', 'enable brotli compression, Node.js >= v11.7.0') | ||
.option( | ||
'-bp, --brotli-param-mode [brotliParamMode]', | ||
'default, text (for UTF-8 text), font (for WOFF 2.0 fonts)' | ||
) | ||
.option( | ||
'-bq, --brotli-quality [brotliQuality]', | ||
'brotli compression quality 11 (default), 0 - 11' | ||
) | ||
.option( | ||
'-bs, --brotli-size-hint [brotliSizeHint]', | ||
'expected input size 0 (default)' | ||
) | ||
.parse(process.argv) | ||
@@ -29,15 +53,15 @@ | ||
const options = { | ||
verbose: program.verbose, | ||
gzipLevel: program.gzipLevel, | ||
gzipMemoryLevel: program.gzipMemoryLevel, | ||
gzipStrategy: program.gzipStrategy, | ||
verbose: Boolean(GZIPPER_VERBOSE) || program.verbose, | ||
gzipLevel: +GZIPPER_GZIP_LEVEL || +program.gzipLevel, | ||
gzipMemoryLevel: +GZIPPER_GZIP_MEMORY_LEVEL || +program.gzipMemoryLevel, | ||
gzipStrategy: +GZIPPER_GZIP_STRATEGY || +program.gzipStrategy, | ||
brotli: Boolean(GZIPPER_BROTLI) || program.brotli, | ||
brotliParamMode: GZIPPER_BROTLI_PARAM_MODE || program.brotliParamMode, | ||
brotliQuality: +GZIPPER_BROTLI_QUALITY || +program.brotliQuality, | ||
brotliSizeHint: +GZIPPER_BROTLI_SIZE_HINT || +program.brotliSizeHint, | ||
} | ||
Object.keys(options).forEach( | ||
key => options[key] === undefined && delete options[key] | ||
) | ||
Object.keys(options).forEach(key => { | ||
if (!isNaN(+options[key])) { | ||
options[key] = +options[key] | ||
if (options[key] === undefined || isNaN(options[key])) { | ||
delete options[key] | ||
} | ||
@@ -44,0 +68,0 @@ }) |
@@ -0,0 +0,0 @@ const logger = Symbol('logger') |
{ | ||
"name": "gzipper", | ||
"version": "2.1.0", | ||
"description": "CLI for gzipping files.", | ||
"version": "2.2.0", | ||
"description": "CLI for compressing files.", | ||
"main": "index.js", | ||
@@ -25,2 +25,3 @@ "scripts": { | ||
"gzipper", | ||
"brotli", | ||
"angular", | ||
@@ -27,0 +28,0 @@ "compression" |
@@ -1,8 +0,13 @@ | ||
# gzipper | ||
# Gzipper | ||
CLI for gzipping files. | ||
CLI for compressing files. | ||
## How to use: | ||
- [Gzipper](#gzipper) | ||
- [Install](#install) | ||
- [Run script](#run-script) | ||
- [Options](#options) | ||
- [Contribution](#contribution) | ||
- [Requirements](#requirements) | ||
### Install globally the package. | ||
## Install | ||
@@ -15,3 +20,3 @@ `npm i gzipper -g` | ||
### Run script from global scope or from your package.json as a script; | ||
## Run script | ||
@@ -24,3 +29,3 @@ Globally usage. | ||
- add to scripts property in your package.json | ||
- add to scripts in your package.json | ||
@@ -42,17 +47,31 @@ ``` | ||
### Options: | ||
- compress files to a certain directory `./gzipped` | ||
- `-V, --version` output the version number | ||
- `-v, --verbose` detailed level of logs | ||
- `gl, --gzip-level` gzip compression level 0 (no compression) - 9 (best compression) | ||
- `gm, --gzip-memory-level` amount of memory which will be allocated for compression 1 (minimum memory) - 9 (maximum memory) | ||
- `gs, --gzip-strategy` compression strategy 1 (filtered) - 2 (huffman only) - 3 (RLE) - 4 (fixed) | ||
- `-h, --help` output usage information | ||
``` | ||
"scripts": { | ||
"build": "ng build && gzipper --verbose ./dist ./gzipped" | ||
} | ||
``` | ||
### Contribution | ||
## Options | ||
| Option | ENV | Description | | ||
| -------------------------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------- | | ||
| `-V, --version` | | output the version number | | ||
| `-v, --verbose` | `GZIPPER_VERBOSE` | detailed level of logs | | ||
| `-gl, --gzip-level [level]` | `GZIPPER_GZIP_LEVEL` | gzip compression level -1 (default), 0 (no compression) - 9 (best compression) | | ||
| `-gm, --gzip-memory-level [memoryLevel]` | `GZIPPER_GZIP_MEMORY_LEVEL` | amount of memory which will be allocated for compression 8 (default), 1 (minimum memory) - 9 (maximum memory) | | ||
| `-gs, --gzip-strategy [strategy]` | `GZIPPER_GZIP_STRATEGY` | compression strategy 0 (default), 1 (filtered), 2 (huffman only), 3 (RLE), 4 (fixed) | | ||
| `--brotli` | `GZIPPER_BROTLI` | enable brotli compression, Node.js >= v11.7.0 | | ||
| `-bp, --brotli-param-mode [brotliParamMode]` | `GZIPPER_BROTLI_PARAM_MODE` | default, text (for UTF-8 text), font (for WOFF 2.0 fonts) | | ||
| `-bq, --brotli-quality [brotliQuality]` | `GZIPPER_BROTLI_QUALITY` | brotli compression quality 11 (default), 0 - 11 | | ||
| `-bs, --brotli-size-hint [brotliSizeHint]` | `GZIPPER_BROTLI_SIZE_HINT` | expected input size 0 (default) | | ||
| `-h, --help` | | output usage information | | ||
## Contribution | ||
I appreciate every contribution, just fork the repository and send the pull request with your changes. | ||
### Requirements | ||
## Requirements | ||
- Node.js >= 8 |
@@ -0,0 +0,0 @@ // const assert = require('assert') |
console.log('Hello Gzipper!') |
console.log('Hello Gzipper!') |
console.log('Hello Gzipper!') |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52109
16
439
75
2