CryptoStream.prototype.resume = function() {
debug('resumed cleartext');
this._writeState = true;
+ this.pair._cycle();
};
// });
//
CryptoStream.prototype._blow = function() {
- var bytesRead;
- var pool;
- var chunkBytes;
- var chunk;
- do {
- bytesRead = 0;
- chunkBytes = 0;
- pool = new Buffer(4096); // alloc every time?
- pool.used = 0;
+ while (this._writeState == true) {
+ var bytesRead = 0;
+ var chunkBytes = 0;
+ var pool = new Buffer(4096); // alloc every time?
do {
try {
- chunkBytes = this._blower(pool,
- pool.used + bytesRead,
- pool.length - pool.used - bytesRead);
+ chunkBytes = this._blower(pool, bytesRead, pool.length - bytesRead);
} catch (e) {
if (this.pair._secureEstablished) {
this.pair._error(e);
}
return;
}
+
if (chunkBytes >= 0) {
bytesRead += chunkBytes;
}
- } while ((chunkBytes > 0) && (pool.used + bytesRead < pool.length));
- if (bytesRead > 0) {
- chunk = pool.slice(0, bytesRead);
- if (this._decoder) {
- var string = this._decoder.write(chunk);
- if (string.length) this.emit('data', string);
- } else {
- if (this._events && this._events['data']) {
- this.emit('data', chunk);
- }
- }
+ } while ((chunkBytes > 0) && (bytesRead < pool.length));
+
+ assert(bytesRead >= 0);
- // Optimization: emit the original buffer with end points
- if (this.ondata) this.ondata(pool, 0, bytesRead);
+ // Bail out if we didn't read any data.
+ if (bytesRead == 0) return;
+
+ var chunk = pool.slice(0, bytesRead);
+
+ if (this._decoder) {
+ var string = this._decoder.write(chunk);
+ if (string.length) this.emit('data', string);
+ } else {
+ this.emit('data', chunk);
}
- } while (bytesRead > 0 && this._writeState === true);
+
+ // Optimization: emit the original buffer with end points
+ if (this.ondata) this.ondata(pool, 0, bytesRead);
+ }
};
// });
//
CryptoStream.prototype._suck = function() {
- var tmp, cb, rv;
+ var rv;
var havePending = this._pending.length > 0;
+
while (this._pending.length > 0) {
- tmp = this._pending.shift();
- cb = this._pendingCallbacks.shift();
+ var tmp = this._pending.shift();
+ var cb = this._pendingCallbacks.shift();
assert(this._pending.length === this._pendingCallbacks.length);
}
// If we've cleared all of incoming encrypted data, emit drain.
- if (havePending && this._pending && this._pending.length === 0) {
+ if (havePending && this._pending.length === 0) {
debug('drain');
this.emit('drain');
if (this.__destroyOnDrain) this.end();