From 7aa6e2b98f501166c245cac30a959610d3cdadd0 Mon Sep 17 00:00:00 2001 From: Alexander O'Mara Date: Wed, 17 May 2017 20:09:00 -0400 Subject: [PATCH] zlib: expose amount of data read for engines Added bytesRead property to Zlib engines Refs: https://github.com/nodejs/node/issues/8874 --- lib/zlib.js | 4 ++ test/parallel/test-zlib-bytes-read.js | 92 +++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 test/parallel/test-zlib-bytes-read.js diff --git a/lib/zlib.js b/lib/zlib.js index 00be56dffc44fb..1d400973d21273 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -168,6 +168,8 @@ class Zlib extends Transform { opts = opts || {}; super(opts); + this.bytesRead = 0; + this._opts = opts; this._chunkSize = opts.chunkSize || constants.Z_DEFAULT_CHUNK; @@ -408,6 +410,8 @@ class Zlib extends Transform { var have = availOutBefore - availOutAfter; assert(have >= 0, 'have should not go down'); + self.bytesRead += availInBefore - availInAfter; + if (have > 0) { var out = self._buffer.slice(self._offset, self._offset + have); self._offset += have; diff --git a/test/parallel/test-zlib-bytes-read.js b/test/parallel/test-zlib-bytes-read.js new file mode 100644 index 00000000000000..f07d736afacc98 --- /dev/null +++ b/test/parallel/test-zlib-bytes-read.js @@ -0,0 +1,92 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const zlib = require('zlib'); + +const expectStr = 'abcdefghijklmnopqrstuvwxyz'.repeat(2); +const expectBuf = Buffer.from(expectStr); + +function createWriter(target, buffer) { + const writer = { size: 0 }; + const write = () => { + target.write(Buffer.from([buffer[writer.size++]])); + if (writer.size < buffer.length) { + setTimeout(write, 25); + } else { + target.end(); + } + }; + write(); + return writer; +} + +for (const method of [ + ['createGzip', 'createGunzip', false], + ['createGzip', 'createUnzip', false], + ['createDeflate', 'createInflate', true], + ['createDeflateRaw', 'createInflateRaw', true] +]) { + let compWriter; + let compData = new Buffer(0); + + const comp = zlib[method[0]](); + comp.on('data', function(d) { + compData = Buffer.concat([compData, d]); + assert.strictEqual(this.bytesRead, compWriter.size, + `Should get write size on ${method[0]} data.`); + }); + comp.on('end', common.mustCall(function() { + assert.strictEqual(this.bytesRead, compWriter.size, + `Should get write size on ${method[0]} end.`); + assert.strictEqual(this.bytesRead, expectStr.length, + `Should get data size on ${method[0]} end.`); + + { + let decompWriter; + let decompData = new Buffer(0); + + const decomp = zlib[method[1]](); + decomp.on('data', function(d) { + decompData = Buffer.concat([decompData, d]); + assert.strictEqual(this.bytesRead, decompWriter.size, + `Should get write size on ${method[0]}/` + + `${method[1]} data.`); + }); + decomp.on('end', common.mustCall(function() { + assert.strictEqual(this.bytesRead, compData.length, + `Should get compressed size on ${method[0]}/` + + `${method[1]} end.`); + assert.strictEqual(decompData.toString(), expectStr, + `Should get original string on ${method[0]}/` + + `${method[1]} end.`); + })); + decompWriter = createWriter(decomp, compData); + } + + // Some methods should allow extra data after the compressed data + if (method[2]) { + const compDataExtra = Buffer.concat([compData, new Buffer('extra')]); + + let decompWriter; + let decompData = new Buffer(0); + + const decomp = zlib[method[1]](); + decomp.on('data', function(d) { + decompData = Buffer.concat([decompData, d]); + assert.strictEqual(this.bytesRead, decompWriter.size, + `Should get write size on ${method[0]}/` + + `${method[1]} data.`); + }); + decomp.on('end', common.mustCall(function() { + assert.strictEqual(this.bytesRead, compData.length, + `Should get compressed size on ${method[0]}/` + + `${method[1]} end.`); + assert.strictEqual(decompData.toString(), expectStr, + `Should get original string on ${method[0]}/` + + `${method[1]} end.`); + })); + decompWriter = createWriter(decomp, compDataExtra); + } + })); + compWriter = createWriter(comp, expectBuf); +}