Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved description and added reverting encoding to null

  • Loading branch information...
commit c42bf5380b1f0944d08683d1391e9c8bb5e989e0 1 parent afcf508
@Rush authored
Showing with 27 additions and 10 deletions.
  1. +27 −10 lib/tls.js
View
37 lib/tls.js
@@ -1172,27 +1172,44 @@ exports.connect = function(/* [port, host], options, cb */) {
return cleartext;
};
-// Target API:
-//
+// starts TLS on a given socket, be it client or server
+// for a server-side socket, asServer has to be passed as a boolean
+// var assert = require('assert');
// var net = require('net');
// var tls = require('tls');
-// var s = net.connect(8000, "google"com");
-// s.on("connect", function() {
-// var encryptedStream = tls.start(s, {function() {
-// encryptedStream
+// var client = net.connect(443, 'google.com', function() {
+// var encryptedStream = tls.start(client, function() {
+// // secure connection in here!
+// client.end();
// });
-//
+//});
exports.start = function(socket /* [options], [asServer], cb */) {
- var cb, options, asServer;
+ var cb, options = {}, asServer;
+
+ if(!socket) {
+ throw new Error("First parameter as socket is required");
+ }
+
+
+ // callback is always last argument
if (typeof arguments[arguments.length - 1] === 'function') {
cb = arguments[arguments.length - 1];
}
+ // options passed as second argument
if (typeof arguments[1] === 'object') {
options = arguments[1];
+ if (typeof arguments[2] == 'boolean') {
+ asServer = arguments[2];
+ }
}
- if (typeof arguments[2] == 'boolean') {
- asServer = arguments[2];
+ // no options passed
+ else if(typeof arguments[1] == 'boolean') {
+ asServer = arguments[1];
}
+
+ // revert encoding as TLS requires a binary stream
+ socket.setEncoding(null);
+
options = util._extend({
socket: socket,
// allow asServer to be passed either through argument
Please sign in to comment.
Something went wrong with that request. Please try again.