Browse Source

Add setEncoding to CryptoStream

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
2f6cb66009
  1. 17
      lib/tls.js
  2. 45
      test/simple/test-tls-set-encoding.js

17
lib/tls.js

@ -86,6 +86,12 @@ CryptoStream.prototype.setNoDelay = function() {
};
CryptoStream.prototype.setEncoding = function(encoding) {
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
this._decoder = new StringDecoder(encoding);
};
// EG '/C=US/ST=CA/L=SF/O=Joyent/OU=Node.js/CN=ca1/emailAddress=ry@clouds.org'
function parseCertString(s) {
var out = {};
@ -205,9 +211,14 @@ CryptoStream.prototype._blow = function() {
} while ((chunkBytes > 0) && (pool.used + bytesRead < pool.length));
if (bytesRead > 0) {
if (this._events && this._events['data']) {
chunk = pool.slice(0, bytesRead);
this.emit('data', chunk);
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);
}
}
// Optimization: emit the original buffer with end points

45
test/simple/test-tls-set-encoding.js

@ -0,0 +1,45 @@
var common = require('../common');
var assert = require('assert');
var tls = require('tls');
var fs = require('fs');
var options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem')
};
var connections = 0;
var message = "hello world\n";
var server = tls.Server(options, function(socket) {
socket.end(message);
connections++;
});
server.listen(common.PORT, function() {
var client = tls.connect(common.PORT);
var buffer = '';
client.setEncoding('utf8');
client.on('data', function(d) {
assert.ok(typeof d === 'string');
buffer += d;
});
client.on('close', function() {
assert.equal(buffer, message);
console.log(message);
server.close();
});
});
process.on('exit', function() {
assert.equal(1, connections);
});
Loading…
Cancel
Save