// Target API:
//
-// var s = tls.connect(8000, "google.com", options, function() {
+// var s = tls.connect({port: 8000, host: "google.com"}, function() {
// if (!s.authorized) {
// s.destroy();
// return;
// });
//
//
-// TODO: make port, host part of options!
-exports.connect = function(port /* host, options, cb */) {
- // parse args
- var host, options = {}, cb;
- for (var i = 1; i < arguments.length; i++) {
- switch (typeof arguments[i]) {
- case 'string':
- host = arguments[i];
- break;
-
- case 'object':
- options = arguments[i];
- break;
-
- case 'function':
- cb = arguments[i];
- break;
+exports.connect = function(/* [port, host], options, cb */) {
+ var options = {}, cb;
+
+ if (typeof arguments[0] === 'object') {
+ options = arguments[0];
+ } else if (typeof arguments[1] === 'object') {
+ options = arguments[1];
+ options.port = arguments[0];
+ } else if (typeof arguments[2] === 'object') {
+ options = arguments[2];
+ options.port = arguments[0];
+ options.host = arguments[1];
+ } else {
+ // This is what happens when user passes no `options` argument, we can't
+ // throw `TypeError` here because it would be incompatible with old API
+ if (typeof arguments[0] === 'number') {
+ options.port = arguments[0];
}
+ if (typeof arguments[1] === 'string') {
+ options.host = arguments[1];
+ }
+ }
+
+ if (typeof arguments[arguments.length - 1] === 'function') {
+ cb = arguments[arguments.length - 1];
}
var socket = new net.Stream();
options.rejectUnauthorized === true ? true : false,
{
NPNProtocols: this.NPNProtocols,
- servername: options.servername || host
+ servername: options.servername || options.host
});
if (options.session) {
cleartext.on('secureConnect', cb);
}
- socket.connect(port, host);
+ socket.connect(options.port, options.host);
pair.on('secure', function() {
var verifyError = pair.ssl.verifyError();