Skip to content

Commit ed3d333

Browse files
committed
Lower the memory consumption
A pako object contains a 64k buffer. We create a `FlateWorker` for each zip entry, meaning a zip file with a lot of entries would take **a lot** of memory. Lazy-loading the pako object isn't the best solution but it's the quickest. The best solution is to lazy-load the worker list. Mitigate the issue Stuk#446.
1 parent c978642 commit ed3d333

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

lib/flate.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,12 @@ exports.magic = "\x08\x00";
1818
function FlateWorker(action, options) {
1919
GenericWorker.call(this, "FlateWorker/" + action);
2020

21-
this._pako = new pako[action]({
22-
raw:true,
23-
level : options.level || -1 // default compression
24-
});
21+
this._pako = null;
22+
this._pakoAction = action;
23+
this._pakoOptions = options;
2524
// the `meta` object from the last chunk received
2625
// this allow this worker to pass around metadata
2726
this.meta = {};
28-
29-
var self = this;
30-
this._pako.onData = function(data) {
31-
self.push({
32-
data : data,
33-
meta : self.meta
34-
});
35-
};
3627
}
3728

3829
utils.inherits(FlateWorker, GenericWorker);
@@ -42,6 +33,9 @@ utils.inherits(FlateWorker, GenericWorker);
4233
*/
4334
FlateWorker.prototype.processChunk = function (chunk) {
4435
this.meta = chunk.meta;
36+
if (this._pako === null) {
37+
this._createPako();
38+
}
4539
this._pako.push(utils.transformTo(ARRAY_TYPE, chunk.data), false);
4640
};
4741

@@ -50,6 +44,9 @@ FlateWorker.prototype.processChunk = function (chunk) {
5044
*/
5145
FlateWorker.prototype.flush = function () {
5246
GenericWorker.prototype.flush.call(this);
47+
if (this._pako === null) {
48+
this._createPako();
49+
}
5350
this._pako.push([], true);
5451
};
5552
/**
@@ -60,6 +57,26 @@ FlateWorker.prototype.cleanUp = function () {
6057
this._pako = null;
6158
};
6259

60+
/**
61+
* Create the _pako object.
62+
* TODO: lazy-loading this object isn't the best solution but it's the
63+
* quickest. The best solution is to lazy-load the worker list. See also the
64+
* issue #446.
65+
*/
66+
FlateWorker.prototype._createPako = function () {
67+
this._pako = new pako[this._pakoAction]({
68+
raw: true,
69+
level: this._pakoOptions.level || -1 // default compression
70+
});
71+
var self = this;
72+
this._pako.onData = function(data) {
73+
self.push({
74+
data : data,
75+
meta : self.meta
76+
});
77+
};
78+
};
79+
6380
exports.compressWorker = function (compressionOptions) {
6481
return new FlateWorker("Deflate", compressionOptions);
6582
};

0 commit comments

Comments
 (0)