Comparing version 1.0.0 to 1.1.0
@@ -76,3 +76,24 @@ #!/usr/bin/env node | ||
}); }); | ||
commander_1.default | ||
.command('decompress [file]') | ||
.description('Creates a decompressed file in the same location. Argument can be relative/absolute/glob paths [default:*]. Check https://www.npmjs.com/package/brotli to know more about the following options') | ||
.option('-p, --parallel <count>', "Processes <count> number of files in parallel. [default: 1]", function (val) { return +val; }) | ||
.action(function (file, cmdObj) { return __awaiter(void 0, void 0, void 0, function () { | ||
var start, decompressedFiles; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
start = process.hrtime(); | ||
return [4 /*yield*/, index_1.decompression({ | ||
path: file || '*', | ||
parallelJobCount: cmdObj.parallel | ||
})]; | ||
case 1: | ||
decompressedFiles = _a.sent(); | ||
console.log("\n[brotlin] Processed " + decompressedFiles.length + " files in " + Math.round((convert_hrtime_1.default(process.hrtime(start)).seconds + Number.EPSILON) * 100) / 100 + " secs"); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
commander_1.default.parse(process.argv); | ||
//# sourceMappingURL=brotlin.js.map |
/** | ||
* Main decompression handler function | ||
* @param {object} options | ||
*/ | ||
export declare function decompression(options: IDecompression): Promise<string[]>; | ||
export interface IDecompression { | ||
path: string; | ||
parallelJobCount?: number; | ||
} | ||
/** | ||
* Main compression handler function | ||
@@ -3,0 +12,0 @@ * @param {object} options |
@@ -43,3 +43,2 @@ "use strict"; | ||
var fs_1 = require("fs"); | ||
var stream_1 = require("stream"); | ||
var globby_1 = __importDefault(require("globby")); | ||
@@ -50,3 +49,34 @@ var p_limit_1 = __importDefault(require("p-limit")); | ||
var brCompress = br.compress; | ||
var brDecompress = br.decompress; | ||
/** | ||
* Main decompression handler function | ||
* @param {object} options | ||
*/ | ||
function decompression(options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var paths, throttle, throttledPromises; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, pathResolver(options.path)]; | ||
case 1: | ||
paths = _a.sent(); | ||
if (!paths) { | ||
throw new Error("Couldn't resolve path: " + options.path); | ||
} | ||
throttle = p_limit_1.default(options.parallelJobCount || 1); | ||
throttledPromises = paths.map(function (path) { | ||
return throttle(function () { | ||
return decompressor(path).then(function (outFile) { return console.log("created: " + outFile); }); | ||
}); | ||
}); | ||
return [4 /*yield*/, Promise.all(throttledPromises)]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/, paths]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.decompression = decompression; | ||
/** | ||
* Main compression handler function | ||
@@ -116,36 +146,81 @@ * @param {object} options | ||
function getCompressor(_a) { | ||
var _this = this; | ||
var _b = _a.mode, mode = _b === void 0 ? 0 : _b, _c = _a.quality, quality = _c === void 0 ? 11 : _c, _d = _a.windowSize, windowSize = _d === void 0 ? 22 : _d; | ||
return function (filePath) { | ||
return new Promise(function (resolve, reject) { | ||
var OUT_FILE = filePath + '.br'; | ||
var reader = fs_1.createReadStream(filePath); | ||
var writer = fs_1.createWriteStream(OUT_FILE); | ||
var compressor = new stream_1.Transform({ | ||
transform: function (chunk, enc, cb) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var compressed; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, brCompress(chunk, { | ||
mode: mode, | ||
quality: quality, | ||
lgwin: windowSize | ||
})]; | ||
case 1: | ||
compressed = _a.sent(); | ||
this.push(compressed); | ||
return [2 /*return*/, cb()]; | ||
} | ||
}); | ||
return function (filePath) { return __awaiter(_this, void 0, void 0, function () { | ||
var OUT_FILE, content, compressed; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
OUT_FILE = filePath + '.br'; | ||
return [4 /*yield*/, readFile(filePath)]; | ||
case 1: | ||
content = _a.sent(); | ||
compressed = brCompress(content, { | ||
mode: mode, | ||
quality: quality, | ||
lgwin: windowSize | ||
}); | ||
} | ||
}); | ||
reader | ||
.pipe(compressor) | ||
.on('error', function (err) { return reject(err); }) | ||
.pipe(writer) | ||
.on('close', function () { return resolve(OUT_FILE); }); | ||
return [4 /*yield*/, writeFile(OUT_FILE, compressed)]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/, OUT_FILE]; | ||
} | ||
}); | ||
}; | ||
}); }; | ||
} | ||
/** | ||
* | ||
* @param {string} filePath path to file to decompress | ||
* @return {Promise} out-file | ||
*/ | ||
function decompressor(filePath) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var OUT_FILE, content, compressed; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
OUT_FILE = filePath.substr(0, filePath.lastIndexOf('.')); | ||
return [4 /*yield*/, readFile(filePath)]; | ||
case 1: | ||
content = _a.sent(); | ||
compressed = brDecompress(content); | ||
return [4 /*yield*/, writeFile(OUT_FILE, compressed)]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/, OUT_FILE]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Promisified read | ||
* @param {string} path path to file | ||
* @return {Promise} resolves to read data as Buffer | ||
*/ | ||
function readFile(path) { | ||
return new Promise(function (resolve, reject) { | ||
fs_1.readFile(path, function (err, data) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(data); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Promisified write | ||
* @param {string} path path to file | ||
* @param {Buffer} content file content | ||
* @return {Promise} resolves to read data as Buffer | ||
*/ | ||
function writeFile(path, content) { | ||
return new Promise(function (resolve, reject) { | ||
fs_1.writeFile(path, content, function (err) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(); | ||
}); | ||
}); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "brotlin", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A handy CLI/API for Brotli compression", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -38,2 +38,5 @@ # brotlin | ||
# decompress | ||
brotlin decompress index.html.br | ||
# you've options (--help for more) | ||
@@ -54,10 +57,14 @@ brotlin compress style.css --quality 8 # default is 11 | ||
Options: | ||
-V, --version output the version number | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-h, --help output usage information | ||
Commands: | ||
compress [options] [file] Creates a compressed file in the same location. Argument can be relative/absolute/glob | ||
paths [default:*]. Check https://www.npmjs.com/package/brotli to know more about the | ||
following options | ||
compress [options] [file] Creates a compressed file in the same location. Argument can be | ||
relative/absolute/glob paths [default:*]. Check | ||
https://www.npmjs.com/package/brotli to know more about the | ||
following options | ||
decompress [options] [file] Creates a decompressed file in the same location. Argument can be | ||
relative/absolute/glob paths [default:*]. Check | ||
https://www.npmjs.com/package/brotli to know more about the | ||
following options | ||
``` | ||
@@ -83,2 +90,4 @@ | ||
For decompress, see `brotlin decompress --help` | ||
### One more example | ||
@@ -95,5 +104,5 @@ | ||
```ts | ||
import { compression } from 'brotlin'; | ||
import { compression, decompression } from 'brotlin'; | ||
// or | ||
const { compression } = require('brotlin'); | ||
const { compression, decompression } = require('brotlin'); | ||
@@ -107,4 +116,13 @@ compression({ | ||
}).then(files => console.log(`Compressed ${files.length} files`)); | ||
decompression({ | ||
path: '*.html.br', // required | ||
parallelJobCount: 1 // optional | ||
}).then(files => console.log(`Decompressed ${files.length} files`)); | ||
``` | ||
### Having issues with compression? | ||
This module uses the [npm/brotli](https://www.npmjs.com/package/brotli) module. Hence check the reported [issues](https://github.com/foliojs/brotli.js/issues) also. | ||
[![](https://img.shields.io/badge/built%20with-ts--np%203-lightgrey?style=flat-square)](https://github.com/vajahath/generator-ts-np) <!--(TSNP VERSION: 3.2.0)--> | ||
@@ -111,0 +129,0 @@ |
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
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
23479
357
128