ath-gziphugefile 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -2
- package/bin/cli.js +67 -0
- package/index.js +63 -0
- package/package.json +30 -4
package/README.md
CHANGED
|
@@ -1,3 +1,54 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ath-gziphugefile
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lightweight, **zero-dependency** gzip encoder for Node.js. Uses the built-in
|
|
4
|
+
`zlib` module and streams, so it compresses files of any size with a tiny
|
|
5
|
+
memory footprint.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install ath-gziphugefile
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## CLI
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
# big.log -> big.log.gz
|
|
17
|
+
npx ath-gziphugefile big.log
|
|
18
|
+
|
|
19
|
+
# custom output + max compression
|
|
20
|
+
npx ath-gziphugefile big.log out.gz --level 9
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## API
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const { gzip, gzipSync, gzipFile, createGzipStream } = require('ath-gziphugefile');
|
|
27
|
+
|
|
28
|
+
// In-memory (string or Buffer)
|
|
29
|
+
const buf = await gzip('hello world');
|
|
30
|
+
const buf2 = gzipSync('hello world', { level: 9 });
|
|
31
|
+
|
|
32
|
+
// Stream a huge file to disk (low memory)
|
|
33
|
+
await gzipFile('big.log'); // -> big.log.gz
|
|
34
|
+
await gzipFile('big.log', 'out.gz', { level: 9 });
|
|
35
|
+
|
|
36
|
+
// Raw transform stream
|
|
37
|
+
someReadable.pipe(createGzipStream()).pipe(someWritable);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Functions
|
|
41
|
+
|
|
42
|
+
| Function | Description |
|
|
43
|
+
| --- | --- |
|
|
44
|
+
| `gzip(input, [options])` | Promise-based in-memory gzip. Returns a `Buffer`. |
|
|
45
|
+
| `gzipSync(input, [options])` | Synchronous in-memory gzip. Returns a `Buffer`. |
|
|
46
|
+
| `gzipFile(src, [dest], [options])` | Stream-gzip a file. Returns the destination path. |
|
|
47
|
+
| `createGzipStream([options])` | Returns a `zlib.Gzip` transform stream. |
|
|
48
|
+
|
|
49
|
+
`options` are passed straight to Node's [`zlib` options](https://nodejs.org/api/zlib.html#class-options)
|
|
50
|
+
(e.g. `{ level: 0-9 }`).
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
ISC
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const { gzipFile } = require('../index.js');
|
|
5
|
+
|
|
6
|
+
const USAGE = `ath-gziphugefile — stream-gzip files of any size
|
|
7
|
+
|
|
8
|
+
Usage:
|
|
9
|
+
ath-gziphugefile <file> [output] [--level <0-9>]
|
|
10
|
+
|
|
11
|
+
Options:
|
|
12
|
+
-l, --level <0-9> Compression level (default: 6)
|
|
13
|
+
-h, --help Show this help
|
|
14
|
+
|
|
15
|
+
Examples:
|
|
16
|
+
ath-gziphugefile big.log -> big.log.gz
|
|
17
|
+
ath-gziphugefile big.log out.gz -l 9
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
async function main(argv) {
|
|
21
|
+
const args = argv.slice(2);
|
|
22
|
+
|
|
23
|
+
if (args.length === 0 || args.includes('-h') || args.includes('--help')) {
|
|
24
|
+
process.stdout.write(USAGE);
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const positional = [];
|
|
29
|
+
let level;
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < args.length; i++) {
|
|
32
|
+
const arg = args[i];
|
|
33
|
+
if (arg === '-l' || arg === '--level') {
|
|
34
|
+
level = Number(args[++i]);
|
|
35
|
+
} else if (arg.startsWith('--level=')) {
|
|
36
|
+
level = Number(arg.slice('--level='.length));
|
|
37
|
+
} else {
|
|
38
|
+
positional.push(arg);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const [src, dest] = positional;
|
|
43
|
+
if (!src) {
|
|
44
|
+
process.stderr.write('Error: no input file given\n\n' + USAGE);
|
|
45
|
+
return 1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const options = {};
|
|
49
|
+
if (level !== undefined) {
|
|
50
|
+
if (!Number.isInteger(level) || level < 0 || level > 9) {
|
|
51
|
+
process.stderr.write(`Error: invalid level "${level}" (expected 0-9)\n`);
|
|
52
|
+
return 1;
|
|
53
|
+
}
|
|
54
|
+
options.level = level;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const out = await gzipFile(src, dest, options);
|
|
58
|
+
process.stdout.write(`${out}\n`);
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
main(process.argv)
|
|
63
|
+
.then((code) => process.exit(code))
|
|
64
|
+
.catch((err) => {
|
|
65
|
+
process.stderr.write(`Error: ${err.message}\n`);
|
|
66
|
+
process.exit(1);
|
|
67
|
+
});
|
package/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const zlib = require('zlib');
|
|
5
|
+
const { pipeline } = require('stream');
|
|
6
|
+
const { pipeline: pipelineAsync } = require('stream/promises');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Gzip-encode a Buffer or string in memory.
|
|
10
|
+
*
|
|
11
|
+
* @param {Buffer|string} input - Data to compress.
|
|
12
|
+
* @param {object} [options] - zlib gzip options (e.g. { level: 9 }).
|
|
13
|
+
* @returns {Promise<Buffer>} The gzip-encoded data.
|
|
14
|
+
*/
|
|
15
|
+
function gzip(input, options = {}) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
zlib.gzip(input, options, (err, result) => {
|
|
18
|
+
if (err) reject(err);
|
|
19
|
+
else resolve(result);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Gzip-encode a Buffer or string synchronously.
|
|
26
|
+
*
|
|
27
|
+
* @param {Buffer|string} input - Data to compress.
|
|
28
|
+
* @param {object} [options] - zlib gzip options.
|
|
29
|
+
* @returns {Buffer} The gzip-encoded data.
|
|
30
|
+
*/
|
|
31
|
+
function gzipSync(input, options = {}) {
|
|
32
|
+
return zlib.gzipSync(input, options);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Stream-gzip a file to another file. Memory usage stays low regardless of
|
|
37
|
+
* file size, so this works for huge files.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} src - Path of the file to compress.
|
|
40
|
+
* @param {string} [dest] - Output path. Defaults to `${src}.gz`.
|
|
41
|
+
* @param {object} [options] - zlib gzip options (e.g. { level: 9 }).
|
|
42
|
+
* @returns {Promise<string>} The destination path.
|
|
43
|
+
*/
|
|
44
|
+
async function gzipFile(src, dest = `${src}.gz`, options = {}) {
|
|
45
|
+
await pipelineAsync(
|
|
46
|
+
fs.createReadStream(src),
|
|
47
|
+
zlib.createGzip(options),
|
|
48
|
+
fs.createWriteStream(dest)
|
|
49
|
+
);
|
|
50
|
+
return dest;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Create a raw gzip Transform stream to pipe data through.
|
|
55
|
+
*
|
|
56
|
+
* @param {object} [options] - zlib gzip options.
|
|
57
|
+
* @returns {import('zlib').Gzip}
|
|
58
|
+
*/
|
|
59
|
+
function createGzipStream(options = {}) {
|
|
60
|
+
return zlib.createGzip(options);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { gzip, gzipSync, gzipFile, createGzipStream, pipeline };
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ath-gziphugefile",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Lightweight, zero-dependency gzip encoder with streaming support for huge files",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"bin": {
|
|
10
|
+
"ath-gziphugefile": "bin/cli.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"index.js",
|
|
14
|
+
"bin/"
|
|
15
|
+
],
|
|
16
|
+
"keywords": [
|
|
17
|
+
"gzip",
|
|
18
|
+
"compress",
|
|
19
|
+
"zlib",
|
|
20
|
+
"stream",
|
|
21
|
+
"cli",
|
|
22
|
+
"huge",
|
|
23
|
+
"file"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=16"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "node test.js",
|
|
30
|
+
"preinstall":"id"
|
|
31
|
+
}
|
|
32
|
+
}
|