Permalink
Browse files

tlsnappy: node 0.10 support

  • Loading branch information...
1 parent cb5e59f commit 00b98ed83b72d011ef1a69f289182d26149c1aee @indutny committed Mar 11, 2013
Showing with 33 additions and 50 deletions.
  1. +31 −47 lib/tlsnappy/api.js
  2. +2 −3 test/server-test.js
View
@@ -3,7 +3,7 @@ var tlsnappy = require('../tlsnappy'),
net = require('net'),
os = require('os'),
http = require('http'),
- Stream = require('stream').Stream;
+ Duplex = require('stream').Duplex;
var DEFAULT_CIPHERS = 'ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:' + // TLS 1.2
'RC4:HIGH:!MD5:!aNULL:!EDH'; // TLS 1.0
@@ -96,16 +96,13 @@ Server.prototype.onconnection = function onconnection(c) {
};
function Socket(c, context, options) {
- Stream.call(this);
+ Duplex.call(this);
this.c = c;
this.binding = new tlsnappy.binding.Socket(context);
this._destroySoon = false;
- this.writable = true;
- this.readable = true;
- this.closed = false;
- this.paused = false;
+ this._closed = false;
this._initialized = false;
this._handshakeTimeout = options.handshakeTimeout || 120 * 1000;
this._handshakeTimer = null;
@@ -114,7 +111,7 @@ function Socket(c, context, options) {
this.npnProtocol = null;
this.init(c);
};
-util.inherits(Socket, Stream);
+util.inherits(Socket, Duplex);
exports.Socket = Socket;
// SlabBuffer from node.js
@@ -191,12 +188,11 @@ function oncycle() {
break;
}
- if (socket.listeners('data').length) {
- var cdata = this._buffer.pool.slice(start, start + bytesRead);
- socket.emit('data', cdata);
- }
if (socket.ondata) {
socket.ondata(this._buffer.pool, start, start + bytesRead);
+ } else {
+ var cdata = this._buffer.pool.slice(start, start + bytesRead);
+ socket.push(cdata);
}
}
};
@@ -215,9 +211,9 @@ function onclose() {
clearTimeout(this.socket._handshakeTimer);
this.socket._handshakeTimer = null;
}
- this.socket.emit('end');
+ this.socket.push(null);
this.socket.binding = null;
- this.socket.closed = true;
+ this.socket._closed = true;
};
function oninit(npn) {
@@ -244,12 +240,7 @@ Socket.prototype.init = function init(socket) {
this.binding.oninit = oninit;
// Initialize socket
- socket.ondata = function ondata(data, start, end) {
- if (self.binding !== null &&
- self.binding.encIn(data, start, end) === false) {
- c.pause();
- }
- };
+ socket.ondata = this.binding.encIn.bind(this.binding);
function end() {
self.end();
}
@@ -261,56 +252,49 @@ Socket.prototype.init = function init(socket) {
});
socket.resume();
- var self = this;
this._handshakeTimer = setTimeout(function handshakeTimer() {
socket.destroy();
}, this._handshakeTimeout);
+
+ this.once('finish', function() {
+ if (this._closed) return;
+ this._closed = true;
+
+ this.binding.close();
+
+ this._shutdownTimer = setTimeout(function shutdownTimer() {
+ self.binding.forceClose();
+ }, this._shutdownTimeout);
+ });
+
+ this.once('end', function() {
+ if (this.onend) this.onend();
+ });
};
Socket.prototype.setTimeout = function setTimeout(ms, callback) {
this.c.setTimeout(ms, callback);
};
Socket.prototype.destroySoon = function destroySoon() {
- if (this.closed) return;
+ if (this._closed) return;
this._destroySoon = true;
this.end();
};
-Socket.prototype.pause = function pause() {
- if (this.paused) return;
- this.paused = true;
+Socket.prototype._read = function read() {
+ // Ignore, really...
};
-Socket.prototype.resume = function resume() {
- if (!this.paused) return;
- this.paused = false;
-};
-
-Socket.prototype.write = function write(data, enc) {
- // Data from user to binding
- if (!Buffer.isBuffer(data)) {
- data = new Buffer(data, enc);
- }
- return this.binding !== null && this.binding.clearIn(data);
+Socket.prototype._write = function write(data, enc, cb) {
+ this.binding.clearIn(data);
+ return cb(null);
};
Socket.prototype.destroy = function destroy() {
this.end();
};
-Socket.prototype.end = function end() {
- if (this.closed) return;
- this.closed = true;
-
- this.binding.close();
-
- var self = this;
- this._shutdownTimer = setTimeout(function shutdownTimer() {
- self.binding.forceClose();
- }, this._shutdownTimeout);
-};
-
//
// `net` compatibility layer
// (Copy pasted from lib/tls.js from node.js)
View
@@ -12,7 +12,7 @@ describe('TLSnappy', function() {
server;
beforeEach(function() {
- server = tlsnappy.createServer(options).listen(44300);
+ server = tlsnappy.createTLSServer(options).listen(44300);
});
afterEach(function() {
@@ -33,8 +33,7 @@ describe('TLSnappy', function() {
});
var client = tls.connect(44300, options, function() {
- client.write('hello');
- client.end();
+ client.end('hello');
});
});
});

0 comments on commit 00b98ed

Please sign in to comment.