humus-old/common/parser-throughput.js

20 lines
598 B
JavaScript
Raw Normal View History

2019-04-12 12:10:40 -05:00
export function augmentParserWithThroughput(Parser) {
const {Decoder, Encoder} = Parser;
// Decoder.
Decoder.throughput = 0;
const {parseBinary} = Decoder.prototype;
Decoder.prototype.parseBinary = function (obj) {
Decoder.throughput += obj.length || obj.byteLength;
return parseBinary.call(this, obj);
};
// Encoder.
Encoder.throughput = 0;
const {pack} = Encoder.prototype;
Encoder.prototype.pack = function (packet) {
const buffer = pack.call(this, packet);
Encoder.throughput += buffer.length || buffer.byteLength;
return buffer;
};
return Parser;
}