@@ -54,13 +54,14 @@ function CryptoStream(pair) {
54
54
this . _writeState = true ;
55
55
this . _pending = [ ] ;
56
56
this . _pendingCallbacks = [ ] ;
57
+ this . _pendingBytes = 0 ;
57
58
}
58
59
util . inherits ( CryptoStream , stream . Stream ) ;
59
60
60
61
61
62
CryptoStream . prototype . write = function ( data /* , encoding, cb */ ) {
62
63
if ( this == this . pair . cleartext ) {
63
- debug ( 'cleartext.write called with ((( ' + data . toString ( ) + '))) ' ) ;
64
+ debug ( 'cleartext.write called with ' + data . length + ' bytes ' ) ;
64
65
} else {
65
66
debug ( 'encrypted.write called with ' + data . length + ' bytes' ) ;
66
67
}
@@ -90,10 +91,12 @@ CryptoStream.prototype.write = function(data /* , encoding, cb */) {
90
91
this . _pending . push ( data ) ;
91
92
this . _pendingCallbacks . push ( cb ) ;
92
93
94
+ this . _pendingBytes += data . length ;
95
+
93
96
this . pair . _writeCalled = true ;
94
97
this . pair . _cycle ( ) ;
95
98
96
- return this . _writeState ;
99
+ return this . _pendingBytes < 128 * 1024 ;
97
100
} ;
98
101
99
102
@@ -263,7 +266,7 @@ CryptoStream.prototype._push = function() {
263
266
264
267
// Bail out if we didn't read any data.
265
268
if ( bytesRead == 0 ) {
266
- if ( this . _pendingBytes ( ) == 0 && this . _destroyAfterPush ) {
269
+ if ( this . _internallyPendingBytes ( ) == 0 && this . _destroyAfterPush ) {
267
270
this . _done ( ) ;
268
271
}
269
272
return ;
@@ -272,7 +275,7 @@ CryptoStream.prototype._push = function() {
272
275
var chunk = pool . slice ( 0 , bytesRead ) ;
273
276
274
277
if ( this === this . pair . cleartext ) {
275
- debug ( 'cleartext emit "data" called with ((( ' + chunk . toString ( ) + '))) ' ) ;
278
+ debug ( 'cleartext emit "data" with ' + bytesRead + ' bytes ' ) ;
276
279
} else {
277
280
debug ( 'encrypted emit "data" with ' + bytesRead + ' bytes' ) ;
278
281
}
@@ -307,6 +310,8 @@ CryptoStream.prototype._push = function() {
307
310
CryptoStream . prototype . _pull = function ( ) {
308
311
var havePending = this . _pending . length > 0 ;
309
312
313
+ assert ( havePending || this . _pendingBytes == 0 ) ;
314
+
310
315
while ( this . _pending . length > 0 ) {
311
316
if ( ! this . pair . _ssl ) break ;
312
317
@@ -355,6 +360,9 @@ CryptoStream.prototype._pull = function() {
355
360
break ;
356
361
}
357
362
363
+ this . _pendingBytes -= tmp . length ;
364
+ assert ( this . _pendingBytes >= 0 ) ;
365
+
358
366
if ( cb ) cb ( ) ;
359
367
360
368
assert ( rv === tmp . length ) ;
@@ -375,7 +383,7 @@ function CleartextStream(pair) {
375
383
util . inherits ( CleartextStream , CryptoStream ) ;
376
384
377
385
378
- CleartextStream . prototype . _pendingBytes = function ( ) {
386
+ CleartextStream . prototype . _internallyPendingBytes = function ( ) {
379
387
if ( this . pair . _ssl ) {
380
388
return this . pair . _ssl . clearPending ( ) ;
381
389
} else {
@@ -403,7 +411,7 @@ function EncryptedStream(pair) {
403
411
util . inherits ( EncryptedStream , CryptoStream ) ;
404
412
405
413
406
- EncryptedStream . prototype . _pendingBytes = function ( ) {
414
+ EncryptedStream . prototype . _internallyPendingBytes = function ( ) {
407
415
if ( this . pair . _ssl ) {
408
416
return this . pair . _ssl . encPending ( ) ;
409
417
} else {
@@ -539,24 +547,28 @@ SecurePair.prototype._cycle = function(depth) {
539
547
540
548
if ( ! this . _cycleEncryptedPullLock ) {
541
549
this . _cycleEncryptedPullLock = true ;
550
+ debug ( "encrypted._pull" ) ;
542
551
this . encrypted . _pull ( ) ;
543
552
this . _cycleEncryptedPullLock = false ;
544
553
}
545
554
546
555
if ( ! this . _cycleCleartextPullLock ) {
547
556
this . _cycleCleartextPullLock = true ;
557
+ debug ( "cleartext._pull" ) ;
548
558
this . cleartext . _pull ( ) ;
549
559
this . _cycleCleartextPullLock = false ;
550
560
}
551
561
552
562
if ( ! this . _cycleCleartextPushLock ) {
553
563
this . _cycleCleartextPushLock = true ;
564
+ debug ( "cleartext._push" ) ;
554
565
this . cleartext . _push ( ) ;
555
566
this . _cycleCleartextPushLock = false ;
556
567
}
557
568
558
569
if ( ! this . _cycleEncryptedPushLock ) {
559
570
this . _cycleEncryptedPushLock = true ;
571
+ debug ( "encrypted._push" ) ;
560
572
this . encrypted . _push ( ) ;
561
573
this . _cycleEncryptedPushLock = false ;
562
574
}
0 commit comments