@@ -18,21 +18,12 @@ exports.magic = "\x08\x00";
18
18
function FlateWorker ( action , options ) {
19
19
GenericWorker . call ( this , "FlateWorker/" + action ) ;
20
20
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 ;
25
24
// the `meta` object from the last chunk received
26
25
// this allow this worker to pass around metadata
27
26
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
- } ;
36
27
}
37
28
38
29
utils . inherits ( FlateWorker , GenericWorker ) ;
@@ -42,6 +33,9 @@ utils.inherits(FlateWorker, GenericWorker);
42
33
*/
43
34
FlateWorker . prototype . processChunk = function ( chunk ) {
44
35
this . meta = chunk . meta ;
36
+ if ( this . _pako === null ) {
37
+ this . _createPako ( ) ;
38
+ }
45
39
this . _pako . push ( utils . transformTo ( ARRAY_TYPE , chunk . data ) , false ) ;
46
40
} ;
47
41
@@ -50,6 +44,9 @@ FlateWorker.prototype.processChunk = function (chunk) {
50
44
*/
51
45
FlateWorker . prototype . flush = function ( ) {
52
46
GenericWorker . prototype . flush . call ( this ) ;
47
+ if ( this . _pako === null ) {
48
+ this . _createPako ( ) ;
49
+ }
53
50
this . _pako . push ( [ ] , true ) ;
54
51
} ;
55
52
/**
@@ -60,6 +57,26 @@ FlateWorker.prototype.cleanUp = function () {
60
57
this . _pako = null ;
61
58
} ;
62
59
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
+
63
80
exports . compressWorker = function ( compressionOptions ) {
64
81
return new FlateWorker ( "Deflate" , compressionOptions ) ;
65
82
} ;
0 commit comments