@@ -12,12 +12,15 @@ var Buffer = require('safe-buffer').Buffer;
12
12
var BufferList = require ( './BufferList' ) ;
13
13
var EventEmitter = require ( 'events' ) . EventEmitter ;
14
14
var Util = require ( 'util' ) ;
15
+ var Zlib = require ( 'zlib' ) ;
15
16
16
17
module . exports = PacketWriter ;
17
18
function PacketWriter ( ) {
18
- this . _buffer = null ;
19
- this . _offset = 0 ;
20
- this . _sync = false ;
19
+ this . _buffer = null ;
20
+ this . _deflateQueue = [ ] ;
21
+ this . _deflating = false ;
22
+ this . _offset = 0 ;
23
+ this . _sync = false ;
21
24
}
22
25
Util . inherits ( PacketWriter , EventEmitter ) ;
23
26
@@ -48,10 +51,18 @@ PacketWriter.prototype.finalize = function finalize(parser) {
48
51
49
52
if ( parser . _compressed ) {
50
53
num = parser . incrementCompressedPacketNumber ( ) ;
51
- buf = this . _toCompressedPacket ( num , buf ) ;
54
+
55
+ if ( this . _sync ) {
56
+ buf = this . _toCompressedPacket ( num , buf ) ;
57
+ } else {
58
+ this . _toCompressedPacketAsync ( num , buf ) ;
59
+ buf = null ;
60
+ }
52
61
}
53
62
54
- this . emit ( 'data' , buf ) ;
63
+ if ( buf ) {
64
+ this . emit ( 'data' , buf ) ;
65
+ }
55
66
}
56
67
} ;
57
68
@@ -239,7 +250,31 @@ PacketWriter.prototype._allocate = function _allocate(bytes) {
239
250
oldBuffer . copy ( this . _buffer ) ;
240
251
} ;
241
252
242
- PacketWriter . prototype . _toCompressedPacket = function _toCompressedPacket ( num , buf ) {
253
+ PacketWriter . prototype . _deflateNextPacket = function _deflateNextPacket ( ) {
254
+ if ( this . _deflating ) {
255
+ return ;
256
+ }
257
+
258
+ var item = this . _deflateQueue . shift ( ) ;
259
+ var buf = item [ 1 ] ;
260
+ var num = item [ 0 ] ;
261
+ var len = buf . length ;
262
+ var self = this ;
263
+
264
+ this . _deflating = true ;
265
+ Zlib . deflate ( buf , function ( err , data ) {
266
+ if ( err ) {
267
+ self . emit ( 'error' , err ) ;
268
+ return ;
269
+ }
270
+
271
+ self . _deflating = false ;
272
+ self . emit ( 'data' , self . _toCompressedPacket ( num , data , len ) ) ;
273
+ self . _deflateNextPacket ( ) ;
274
+ } ) ;
275
+ } ;
276
+
277
+ PacketWriter . prototype . _toCompressedPacket = function _toCompressedPacket ( num , buf , len ) {
243
278
var origBuffer = this . _buffer ;
244
279
var origOffset = this . _offset ;
245
280
@@ -248,7 +283,7 @@ PacketWriter.prototype._toCompressedPacket = function _toCompressedPacket(num, b
248
283
249
284
this . writeUnsignedNumber ( 3 , buf . length ) ;
250
285
this . writeUnsignedNumber ( 1 , num ) ;
251
- this . writeUnsignedNumber ( 3 , 0 ) ;
286
+ this . writeUnsignedNumber ( 3 , ( len || 0 ) ) ;
252
287
this . writeBuffer ( buf ) ;
253
288
254
289
var packet = this . _buffer ;
@@ -259,6 +294,11 @@ PacketWriter.prototype._toCompressedPacket = function _toCompressedPacket(num, b
259
294
return packet ;
260
295
} ;
261
296
297
+ PacketWriter . prototype . _toCompressedPacketAsync = function _toCompressedPacketAsync ( num , buf ) {
298
+ this . _deflateQueue . push ( buf ) ;
299
+ this . _deflateNextPacket ( ) ;
300
+ } ;
301
+
262
302
PacketWriter . prototype . _toPacket = function _toPacket ( num , buf ) {
263
303
var origBuffer = this . _buffer ;
264
304
var origOffset = this . _offset ;
0 commit comments