Skip to content

Instantly share code, notes, and snippets.

@mranney
Created July 7, 2012 03:35
Show Gist options
  • Save mranney/3064364 to your computer and use it in GitHub Desktop.
Save mranney/3064364 to your computer and use it in GitHub Desktop.
CryptoStream.prototype._push = function() {
if (this == this.pair.encrypted && !this.writable) {
// If the encrypted side got EOF, we do not attempt
// to write out data anymore.
return;
}
while (!this._paused) {
<<<<<<< HEAD
var bytesRead = 0;
var chunkBytes = 0;
var pool = new Buffer(16 * 4096); // alloc every time?
do {
chunkBytes = this._pusher(pool, bytesRead, pool.length - bytesRead);
=======
var chunkBytes = 0,
bytesRead = 0,
start = this._buffer.offset;
do {
chunkBytes = this._buffer.use(this, this._pusher);
if (chunkBytes > 0) bytesRead += chunkBytes;
>>>>>>> f210530... tls: use slab allocator
if (this.pair.ssl && this.pair.ssl.error) {
this.pair.error();
return;
}
this.pair.maybeInitFinished();
<<<<<<< HEAD
if (chunkBytes >= 0) {
bytesRead += chunkBytes;
}
} while (chunkBytes > 0 && bytesRead < pool.length);
=======
} while (chunkBytes > 0 && !this._buffer.isFull);
var pool = this._buffer.pool;
// Create new buffer if previous was filled up
if (this._buffer.isFull) this._buffer.create();
>>>>>>> f210530... tls: use slab allocator
assert(bytesRead >= 0);
// Bail out if we didn't read any data.
if (bytesRead == 0) {
if (this._internallyPendingBytes() == 0 && this._destroyAfterPush) {
this._done();
}
return;
}
<<<<<<< HEAD
var chunk = pool.slice(0, bytesRead);
=======
var chunk = pool.slice(start, start + bytesRead);
>>>>>>> f210530... tls: use slab allocator
if (this === this.pair.cleartext) {
debug('cleartext emit "data" with ' + bytesRead + ' bytes');
} else {
debug('encrypted emit "data" with ' + bytesRead + ' bytes');
}
if (this._decoder) {
var string = this._decoder.write(chunk);
if (string.length) this.emit('data', string);
} else {
this.emit('data', chunk);
}
// Optimization: emit the original buffer with end points
<<<<<<< HEAD
if (this.ondata) this.ondata(pool, 0, bytesRead);
=======
if (this.ondata) this.ondata(pool, start, start + bytesRead);
>>>>>>> f210530... tls: use slab allocator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment