Skip to content

Commit bb621f7

Browse files
committed
CryptoStream.write returns false when queue > 128kb
Previously the return value of write was dependent on if it was paused or not which was causing a strange error demoed in the previous commit. Fixes #892
1 parent 050bbf0 commit bb621f7

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/tls.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ function CryptoStream(pair) {
5454
this._writeState = true;
5555
this._pending = [];
5656
this._pendingCallbacks = [];
57+
this._pendingBytes = 0;
5758
}
5859
util.inherits(CryptoStream, stream.Stream);
5960

6061

6162
CryptoStream.prototype.write = function(data /* , encoding, cb */) {
6263
if (this == this.pair.cleartext) {
63-
debug('cleartext.write called with (((' + data.toString() + ')))');
64+
debug('cleartext.write called with ' + data.length + ' bytes');
6465
} else {
6566
debug('encrypted.write called with ' + data.length + ' bytes');
6667
}
@@ -90,10 +91,12 @@ CryptoStream.prototype.write = function(data /* , encoding, cb */) {
9091
this._pending.push(data);
9192
this._pendingCallbacks.push(cb);
9293

94+
this._pendingBytes += data.length;
95+
9396
this.pair._writeCalled = true;
9497
this.pair._cycle();
9598

96-
return this._writeState;
99+
return this._pendingBytes < 128 * 1024;
97100
};
98101

99102

@@ -263,7 +266,7 @@ CryptoStream.prototype._push = function() {
263266

264267
// Bail out if we didn't read any data.
265268
if (bytesRead == 0) {
266-
if (this._pendingBytes() == 0 && this._destroyAfterPush) {
269+
if (this._internallyPendingBytes() == 0 && this._destroyAfterPush) {
267270
this._done();
268271
}
269272
return;
@@ -272,7 +275,7 @@ CryptoStream.prototype._push = function() {
272275
var chunk = pool.slice(0, bytesRead);
273276

274277
if (this === this.pair.cleartext) {
275-
debug('cleartext emit "data" called with (((' + chunk.toString() + ')))');
278+
debug('cleartext emit "data" with ' + bytesRead + ' bytes');
276279
} else {
277280
debug('encrypted emit "data" with ' + bytesRead + ' bytes');
278281
}
@@ -307,6 +310,8 @@ CryptoStream.prototype._push = function() {
307310
CryptoStream.prototype._pull = function() {
308311
var havePending = this._pending.length > 0;
309312

313+
assert(havePending || this._pendingBytes == 0);
314+
310315
while (this._pending.length > 0) {
311316
if (!this.pair._ssl) break;
312317

@@ -355,6 +360,9 @@ CryptoStream.prototype._pull = function() {
355360
break;
356361
}
357362

363+
this._pendingBytes -= tmp.length;
364+
assert(this._pendingBytes >= 0);
365+
358366
if (cb) cb();
359367

360368
assert(rv === tmp.length);
@@ -375,7 +383,7 @@ function CleartextStream(pair) {
375383
util.inherits(CleartextStream, CryptoStream);
376384

377385

378-
CleartextStream.prototype._pendingBytes = function() {
386+
CleartextStream.prototype._internallyPendingBytes = function() {
379387
if (this.pair._ssl) {
380388
return this.pair._ssl.clearPending();
381389
} else {
@@ -403,7 +411,7 @@ function EncryptedStream(pair) {
403411
util.inherits(EncryptedStream, CryptoStream);
404412

405413

406-
EncryptedStream.prototype._pendingBytes = function() {
414+
EncryptedStream.prototype._internallyPendingBytes = function() {
407415
if (this.pair._ssl) {
408416
return this.pair._ssl.encPending();
409417
} else {
@@ -539,24 +547,28 @@ SecurePair.prototype._cycle = function(depth) {
539547

540548
if (!this._cycleEncryptedPullLock) {
541549
this._cycleEncryptedPullLock = true;
550+
debug("encrypted._pull");
542551
this.encrypted._pull();
543552
this._cycleEncryptedPullLock = false;
544553
}
545554

546555
if (!this._cycleCleartextPullLock) {
547556
this._cycleCleartextPullLock = true;
557+
debug("cleartext._pull");
548558
this.cleartext._pull();
549559
this._cycleCleartextPullLock = false;
550560
}
551561

552562
if (!this._cycleCleartextPushLock) {
553563
this._cycleCleartextPushLock = true;
564+
debug("cleartext._push");
554565
this.cleartext._push();
555566
this._cycleCleartextPushLock = false;
556567
}
557568

558569
if (!this._cycleEncryptedPushLock) {
559570
this._cycleEncryptedPushLock = true;
571+
debug("encrypted._push");
560572
this.encrypted._push();
561573
this._cycleEncryptedPushLock = false;
562574
}

0 commit comments

Comments
 (0)