Browse Source

Add callback to CryptoStream.write

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
8db0bbe0dc
  1. 31
      lib/tls.js
  2. 8
      test/simple/test-tls-securepair-client.js

31
lib/tls.js

@ -34,14 +34,33 @@ function CryptoStream (pair) {
this._writeState = true;
this._pending = [];
this._pendingCallbacks = [];
}
util.inherits(CryptoStream, stream.Stream);
CryptoStream.prototype.write = function(data) {
if (typeof data == 'string') data = Buffer(data);
CryptoStream.prototype.write = function(data /* , encoding, cb */) {
var encoding, cb;
// parse arguments
if (typeof arguments[1] == 'string') {
encoding = arguments[1];
cb = arguments[2];
} else {
cb = arguments[1];
}
// Transform strings into buffers.
if (typeof data == 'string') {
data = new Buffer(data, encoding);
}
debug('clearIn data');
this._pending.push(data);
this._pendingCallbacks.push(cb);
this.pair._cycle();
return this._writeState;
};
@ -187,10 +206,13 @@ CryptoStream.prototype._blow = function() {
// });
//
CryptoStream.prototype._suck = function() {
var tmp, rv;
var tmp, cb, rv;
var havePending = this._pending.length > 0;
while (this._pending.length > 0) {
tmp = this._pending.shift();
cb = this._pendingCallbacks.shift();
assert(this._pending.length === this._pendingCallbacks.length);
try {
rv = this._sucker(tmp);
@ -205,9 +227,12 @@ CryptoStream.prototype._suck = function() {
if (rv === 0) {
this._pending.unshift(tmp);
this._pendingCallbacks.unshift(cb);
break;
}
if (cb) cb();
assert(rv === tmp.length);
}

8
test/simple/test-tls-securepair-client.js

@ -66,8 +66,9 @@ var timeout = setTimeout(function () {
process.exit(1);
}, 5000);
var gotWriteCallback = false;
var serverExitCode = -1;
server.on('exit', function(code) {
serverExitCode = code;
clearTimeout(timeout);
@ -101,7 +102,9 @@ function startClient() {
console.log('client pair.cleartext.getCipher(): %j',
pair.cleartext.getCipher());
setTimeout(function() {
pair.cleartext.write('hello\r\n');
pair.cleartext.write('hello\r\n', function () {
gotWriteCallback = true;
});
}, 500);
});
@ -130,4 +133,5 @@ function startClient() {
process.on('exit', function() {
assert.equal(0, serverExitCode);
assert.equal('WAIT-SERVER-CLOSE', state);
assert.ok(gotWriteCallback);
});

Loading…
Cancel
Save