Skip to content

Commit edb727e

Browse files
committed
Add stream block event
This is useful for example to access how many values were de/encoded.
1 parent 69599b1 commit edb727e

File tree

4 files changed

+2736
-180
lines changed

4 files changed

+2736
-180
lines changed

lib/containers.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ BlockDecoder.prototype._writeChunk = function (chunk, encoding, cb) {
243243
nBlocks++;
244244
this._decompress(
245245
block.data,
246-
this._createBlockCallback(block.count, chunkCb)
246+
this._createBlockCallback(block.data.length, block.count, chunkCb)
247247
);
248248
}
249249
chunkCb();
@@ -255,7 +255,7 @@ BlockDecoder.prototype._writeChunk = function (chunk, encoding, cb) {
255255
}
256256
};
257257

258-
BlockDecoder.prototype._createBlockCallback = function (count, cb) {
258+
BlockDecoder.prototype._createBlockCallback = function (size, count, cb) {
259259
var self = this;
260260
var index = this._index++;
261261

@@ -266,6 +266,7 @@ BlockDecoder.prototype._createBlockCallback = function (count, cb) {
266266
self.emit('error', err);
267267
cb();
268268
} else {
269+
self.emit('block', new BlockInfo(count, data.length, size));
269270
self._queue.push(new BlockData(index, data, cb, count));
270271
if (self._needPush) {
271272
self._read();
@@ -525,7 +526,7 @@ BlockEncoder.prototype._write = function (val, encoding, cb) {
525526
BlockEncoder.prototype._flushChunk = function (pos, cb) {
526527
var tap = this._tap;
527528
pos = pos || tap.pos;
528-
this._compress(tap.buf.slice(0, pos), this._createBlockCallback(cb));
529+
this._compress(tap.buf.slice(0, pos), this._createBlockCallback(pos, cb));
529530
this._blockCount = 0;
530531
};
531532

@@ -551,7 +552,7 @@ BlockEncoder.prototype._read = function () {
551552
}
552553
};
553554

554-
BlockEncoder.prototype._createBlockCallback = function (cb) {
555+
BlockEncoder.prototype._createBlockCallback = function (size, cb) {
555556
var self = this;
556557
var index = this._index++;
557558
var count = this._blockCount;
@@ -565,6 +566,7 @@ BlockEncoder.prototype._createBlockCallback = function (cb) {
565566
return;
566567
}
567568
self._pending--;
569+
self.emit('block', new BlockInfo(count, size, data.length));
568570
self._queue.push(new BlockData(index, data, cb, count));
569571
if (self._needPush) {
570572
self._needPush = false;
@@ -576,6 +578,13 @@ BlockEncoder.prototype._createBlockCallback = function (cb) {
576578

577579
// Helpers.
578580

581+
/** Summary information about a block. */
582+
function BlockInfo(count, raw, compressed) {
583+
this.valueCount = count;
584+
this.rawDataLength = raw;
585+
this.compressedDataLength = compressed;
586+
}
587+
579588
/**
580589
* An indexed block.
581590
*

0 commit comments

Comments
 (0)