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._writeState = true;
this._pending = []; this._pending = [];
this._pendingCallbacks = [];
} }
util.inherits(CryptoStream, stream.Stream); util.inherits(CryptoStream, stream.Stream);
CryptoStream.prototype.write = function(data) { CryptoStream.prototype.write = function(data /* , encoding, cb */) {
if (typeof data == 'string') data = Buffer(data); 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'); debug('clearIn data');
this._pending.push(data); this._pending.push(data);
this._pendingCallbacks.push(cb);
this.pair._cycle(); this.pair._cycle();
return this._writeState; return this._writeState;
}; };
@ -187,10 +206,13 @@ CryptoStream.prototype._blow = function() {
// }); // });
// //
CryptoStream.prototype._suck = function() { CryptoStream.prototype._suck = function() {
var tmp, rv; var tmp, cb, rv;
var havePending = this._pending.length > 0; var havePending = this._pending.length > 0;
while (this._pending.length > 0) { while (this._pending.length > 0) {
tmp = this._pending.shift(); tmp = this._pending.shift();
cb = this._pendingCallbacks.shift();
assert(this._pending.length === this._pendingCallbacks.length);
try { try {
rv = this._sucker(tmp); rv = this._sucker(tmp);
@ -205,9 +227,12 @@ CryptoStream.prototype._suck = function() {
if (rv === 0) { if (rv === 0) {
this._pending.unshift(tmp); this._pending.unshift(tmp);
this._pendingCallbacks.unshift(cb);
break; break;
} }
if (cb) cb();
assert(rv === tmp.length); assert(rv === tmp.length);
} }

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

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

Loading…
Cancel
Save