zlib-organic
v2.0.1
Published
zlib with bare-bones interface for the advanced user
Downloads
12
Readme
zlib-organic
This is a bare-bones, synchronous wrapper around zlib. It is meant to replace the one that comes with nodejs, because that one is multi-threaded and seems to use memory unpredictably.
Install
$ npm install
$ npm test
API
The API consists of only two stream transform classes: Compressor
and Decompressor
.
new zlib_organic.Compressor(options?: ZlibOptions)
new zlib_organic.Decompressor(options?: ZlibOptions)
The options object is passed to node's Transform
, and has two additional optional fields:
preset: number
: an abstraction of the compression difficulty level, from 1 to 9, where 1 puts in the least effort. The default is 6.bufferSize: number
: minimum buffer size to use for encoding/decoding blocks of data. The default is 1KB, but it will grow to match the block size of its input as it processes data. (You shouldn't normally need to care about this.)
Both objects are stream transforms that consume and produce Buffers. Here's example code to compress the sample file included with this distribution:
var fs = require("fs");
var zlib_organic = require("zlib-organic");
var compression = new zlib_organic.Compressor(9);
var inFile = fs.createReadStream("./testdata/minecraft.png");
var outFile = fs.createWriteStream("./testdata/minecraft.png.zlib");
inFile.pipe(compression).pipe(outFile);
Non-streaming API
If you aren't using nodejs streams, an API similar to the crypto API is available on both Compressor
and Decompressor
:
process(input: Buffer, flags: number): Buffer
final(): Buffer
reset()
The process
method feeds a Buffer
into the zlib engine, and inflates/deflates inline (synchronously). The returned Buffer
may have a length of 0: zlib keeps a large internal buffer while compressing, so it's common to receive empty Buffer
s while you compress, followed by a large final Buffer
when you end the stream with final
.
flags
has the same meaning and name as the zlib flags (Z_SYNC_FLUSH
and so on).
final()
flushes any remaining data from the internal buffers and returns the final chunk.
reset()
is the same as discarding the Compressor/Decompressor and creating a new one, but doesn't discard or reallocate memory. It can be useful if you plan to keep a pool of pre-initialized zlib objects around.
License
Apache 2 (open-source) license, included in 'LICENSE.txt'.
Authors
- @robey - Robey Pointer [email protected]