Skip to content

Commit 086d4cc

Browse files
mscdexbnoordhuis
authored andcommitted
zlib: allow custom flush type for flush()
1 parent 0004ffa commit 086d4cc

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

doc/api/zlib.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ Returns a new [Unzip](#zlib_class_zlib_unzip) object with an
144144
Not exported by the `zlib` module. It is documented here because it is the base
145145
class of the compressor/decompressor classes.
146146

147-
### zlib.flush(callback)
147+
### zlib.flush([kind], callback)
148+
149+
`kind` defaults to `zlib.Z_FULL_FLUSH`.
148150

149151
Flush pending data. Don't call this frivolously, premature flushes negatively
150152
impact the effectiveness of the compression algorithm.

lib/zlib.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,14 @@ Zlib.prototype._flush = function(callback) {
357357
this._transform(new Buffer(0), '', callback);
358358
};
359359

360-
Zlib.prototype.flush = function(callback) {
360+
Zlib.prototype.flush = function(kind, callback) {
361361
var ws = this._writableState;
362362

363+
if (typeof kind === 'function' || (kind === undefined && !callback)) {
364+
callback = kind;
365+
kind = binding.Z_FULL_FLUSH;
366+
}
367+
363368
if (ws.ended) {
364369
if (callback)
365370
process.nextTick(callback);
@@ -372,7 +377,7 @@ Zlib.prototype.flush = function(callback) {
372377
self.flush(callback);
373378
});
374379
} else {
375-
this._flushFlag = binding.Z_FULL_FLUSH;
380+
this._flushFlag = kind;
376381
this.write(new Buffer(0), '', callback);
377382
}
378383
};

test/simple/test-zlib-flush.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var common = require('../common.js');
2+
var assert = require('assert');
3+
var zlib = require('zlib');
4+
var path = require('path');
5+
var fs = require('fs');
6+
7+
var file = fs.readFileSync(path.resolve(common.fixturesDir, 'person.jpg')),
8+
chunkSize = 16,
9+
opts = { level: 0 },
10+
deflater = zlib.createDeflate(opts);
11+
12+
var chunk = file.slice(0, chunkSize),
13+
expectedNone = new Buffer([0x78, 0x01]),
14+
blkhdr = new Buffer([0x00, 0x10, 0x00, 0xef, 0xff]),
15+
adler32 = new Buffer([0x00, 0x00, 0x00, 0xff, 0xff]),
16+
expectedFull = Buffer.concat([blkhdr, chunk, adler32]),
17+
actualNone,
18+
actualFull;
19+
20+
deflater.write(chunk, function() {
21+
deflater.flush(zlib.Z_NO_FLUSH, function() {
22+
actualNone = deflater.read();
23+
deflater.flush(function() {
24+
var bufs = [], buf;
25+
while (buf = deflater.read())
26+
bufs.push(buf);
27+
actualFull = Buffer.concat(bufs);
28+
});
29+
});
30+
});
31+
32+
process.once('exit', function() {
33+
assert.deepEqual(actualNone, expectedNone);
34+
assert.deepEqual(actualFull, expectedFull);
35+
});

0 commit comments

Comments
 (0)