f8ff2ef7dd
Previously, we used a fixed chunkSize of 100KiB for Pako's output buffer. Using a hardcoded size caused issues, since Pako would assume we wanted to use multiple chunks, and we didn't deal with this. Now, `Inflator#inflate()` takes a new `expected` argument, which indicates the expected output size. If this is bigger than the current chunkSize, Inflator allocates a new output buffer that's big enough to hold the output. Fixes #531
41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
var zlib = require('../node_modules/pako/lib/zlib/inflate.js');
|
|
var ZStream = require('../node_modules/pako/lib/zlib/zstream.js');
|
|
|
|
var Inflate = function () {
|
|
this.strm = new ZStream();
|
|
this.chunkSize = 1024 * 10 * 10;
|
|
this.strm.output = new Uint8Array(this.chunkSize);
|
|
this.windowBits = 5;
|
|
|
|
zlib.inflateInit(this.strm, this.windowBits);
|
|
};
|
|
|
|
Inflate.prototype = {
|
|
inflate: function (data, flush, expected) {
|
|
this.strm.input = data;
|
|
this.strm.avail_in = this.strm.input.length;
|
|
this.strm.next_in = 0;
|
|
this.strm.next_out = 0;
|
|
|
|
// resize our output buffer if it's too small
|
|
// (we could just use multiple chunks, but that would cause an extra
|
|
// allocation each time to flatten the chunks)
|
|
if (expected > this.chunkSize) {
|
|
this.chunkSize = expected;
|
|
this.strm.output = new Uint8Array(this.chunkSize);
|
|
}
|
|
|
|
this.strm.avail_out = this.chunkSize;
|
|
|
|
zlib.inflate(this.strm, flush);
|
|
|
|
return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
|
|
},
|
|
|
|
reset: function () {
|
|
zlib.inflateReset(this.strm);
|
|
}
|
|
};
|
|
|
|
module.exports = {Inflate: Inflate};
|