Skip to content

Commit e710036

Browse files
AlexanderOMaraaddaleax
authored andcommitted
zlib: expose amount of data read for engines
Added bytesRead property to Zlib engines Fixes: #8874 PR-URL: #13088 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
1 parent d0b1b52 commit e710036

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

doc/api/zlib.md

+11
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,17 @@ added: v0.5.8
386386
Not exported by the `zlib` module. It is documented here because it is the base
387387
class of the compressor/decompressor classes.
388388

389+
### zlib.bytesRead
390+
<!-- YAML
391+
added: REPLACEME
392+
-->
393+
394+
* {number}
395+
396+
The `zlib.bytesRead` property specifies the number of bytes read by the engine
397+
before the bytes are processed (compressed or decompressed, as appropriate for
398+
the derived class).
399+
389400
### zlib.flush([kind], callback)
390401
<!-- YAML
391402
added: v0.5.8

lib/zlib.js

+4
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class Zlib extends Transform {
175175
opts = opts || {};
176176
super(opts);
177177

178+
this.bytesRead = 0;
179+
178180
this._opts = opts;
179181
this._chunkSize = opts.chunkSize || constants.Z_DEFAULT_CHUNK;
180182

@@ -429,6 +431,8 @@ class Zlib extends Transform {
429431
var have = availOutBefore - availOutAfter;
430432
assert(have >= 0, 'have should not go down');
431433

434+
self.bytesRead += availInBefore - availInAfter;
435+
432436
if (have > 0) {
433437
var out = self._buffer.slice(self._offset, self._offset + have);
434438
self._offset += have;

test/parallel/test-zlib-bytes-read.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const zlib = require('zlib');
5+
6+
const expectStr = 'abcdefghijklmnopqrstuvwxyz'.repeat(2);
7+
const expectBuf = Buffer.from(expectStr);
8+
9+
function createWriter(target, buffer) {
10+
const writer = { size: 0 };
11+
const write = () => {
12+
target.write(Buffer.from([buffer[writer.size++]]), () => {
13+
if (writer.size < buffer.length) {
14+
target.flush(write);
15+
} else {
16+
target.end();
17+
}
18+
});
19+
};
20+
write();
21+
return writer;
22+
}
23+
24+
for (const method of [
25+
['createGzip', 'createGunzip', false],
26+
['createGzip', 'createUnzip', false],
27+
['createDeflate', 'createInflate', true],
28+
['createDeflateRaw', 'createInflateRaw', true]
29+
]) {
30+
let compWriter;
31+
let compData = new Buffer(0);
32+
33+
const comp = zlib[method[0]]();
34+
comp.on('data', function(d) {
35+
compData = Buffer.concat([compData, d]);
36+
assert.strictEqual(this.bytesRead, compWriter.size,
37+
`Should get write size on ${method[0]} data.`);
38+
});
39+
comp.on('end', common.mustCall(function() {
40+
assert.strictEqual(this.bytesRead, compWriter.size,
41+
`Should get write size on ${method[0]} end.`);
42+
assert.strictEqual(this.bytesRead, expectStr.length,
43+
`Should get data size on ${method[0]} end.`);
44+
45+
{
46+
let decompWriter;
47+
let decompData = new Buffer(0);
48+
49+
const decomp = zlib[method[1]]();
50+
decomp.on('data', function(d) {
51+
decompData = Buffer.concat([decompData, d]);
52+
assert.strictEqual(this.bytesRead, decompWriter.size,
53+
`Should get write size on ${method[0]}/` +
54+
`${method[1]} data.`);
55+
});
56+
decomp.on('end', common.mustCall(function() {
57+
assert.strictEqual(this.bytesRead, compData.length,
58+
`Should get compressed size on ${method[0]}/` +
59+
`${method[1]} end.`);
60+
assert.strictEqual(decompData.toString(), expectStr,
61+
`Should get original string on ${method[0]}/` +
62+
`${method[1]} end.`);
63+
}));
64+
decompWriter = createWriter(decomp, compData);
65+
}
66+
67+
// Some methods should allow extra data after the compressed data
68+
if (method[2]) {
69+
const compDataExtra = Buffer.concat([compData, new Buffer('extra')]);
70+
71+
let decompWriter;
72+
let decompData = new Buffer(0);
73+
74+
const decomp = zlib[method[1]]();
75+
decomp.on('data', function(d) {
76+
decompData = Buffer.concat([decompData, d]);
77+
assert.strictEqual(this.bytesRead, decompWriter.size,
78+
`Should get write size on ${method[0]}/` +
79+
`${method[1]} data.`);
80+
});
81+
decomp.on('end', common.mustCall(function() {
82+
assert.strictEqual(this.bytesRead, compData.length,
83+
`Should get compressed size on ${method[0]}/` +
84+
`${method[1]} end.`);
85+
assert.strictEqual(decompData.toString(), expectStr,
86+
`Should get original string on ${method[0]}/` +
87+
`${method[1]} end.`);
88+
}));
89+
decompWriter = createWriter(decomp, compDataExtra);
90+
}
91+
}));
92+
compWriter = createWriter(comp, expectBuf);
93+
}

0 commit comments

Comments
 (0)