Permalink
Browse files

emit error events for tls connections instead of sending errors throu…

…gh the session argument
  • Loading branch information...
1 parent a08aa7a commit b216bf69bd54f1c8b18e27ad4af4e00662d164c4 @substack committed Feb 3, 2012
Showing with 33 additions and 50 deletions.
  1. +20 −40 index.js
  2. +13 −10 test/tls.js
View
@@ -17,17 +17,15 @@ exports.createServer = function (domain, cb) {
});
};
-exports.connect = function (port, host, options, cb) {
+exports.connect = function () {
var args = [].slice.call(arguments).reduce(function (acc, arg) {
acc[typeof arg] = arg;
return acc;
}, {});
var stream;
- var tlsOpts;
-
- cb = args["function"];
- args.options = args.object || {};
+ var cb = args.function;
+ var options = args.object || {};
if (args.string && args.string.match(/^[.\/]/)) {
// unix socket
@@ -36,54 +34,36 @@ exports.connect = function (port, host, options, cb) {
else {
port = args.number || 25;
host = args.string || 'localhost';
- tlsOpts = args.options.tls;
-
+ var tlsOpts = options.tls;
+
if (tlsOpts) {
-
- // TODO: is this the right thing to do?
- var wrapError = function(error) {
- return error instanceof Error ? error : new Error(error);
- };
-
var onSecureConnect = tlsOpts.onSecureConnect;
delete tlsOpts.onSecureConnect;
-
- stream = tls.connect(
- port,
- host,
- tlsOpts,
- function() {
-
- var ok;
- var result;
-
- if (onSecureConnect) ok = onSecureConnect(stream);
- else ok = stream.authorized;
-
- if (!ok) {
- stream.end();
- result = wrapError(stream.authorizationError);
- }
- else result = proto.server(stream);
-
- cb(result);
+
+ stream = tls.connect(port, host, tlsOpts, function () {
+ var ok = onSecureConnect
+ ? onSecureConnect(stream)
+ : stream.authorized
+ ;
+
+ if (!ok) {
+ stream.end();
+ stream.emit('error', new Error(stream.authorizationError));
}
- );
+ else cb(proto.server(stream));
+ });
}
- else if (args.options.stream) {
- cb(proto.server(args.options.stream));
+ else if (options.stream) {
+ cb(proto.server(options.stream));
}
else {
stream = net.createConnection(port, host);
-
stream.on('connect', function () {
cb(proto.server(stream));
});
-
}
-
}
-
+
return stream;
};
View
@@ -28,17 +28,21 @@ test('TLS - unauthorized', {timeout: 1000}, function(t) {
var server = makeServer();
server.listen(serverPort, function() {
- smtp.connect(serverPort, options, function(err) {
+ var c = smtp.connect(serverPort, options, function (session) {
+ t.fail('connect should have failed');
+ });
+
+ c.on('error', function (err) {;
server.close();
- t.ok(err instanceof Error, "connect should fail");
- t.equals(err.message, "UNABLE_TO_VERIFY_LEAF_SIGNATURE", "connect should fail");
+ t.ok(err, 'connect should fail');
+ t.equals(err.message, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE');
t.end();
});
});
});
test('TLS - unauthorized with callback', {timeout: 1000}, function(t) {
- t.plan(4);
+ t.plan(3);
var server = makeServer();
var options = {
@@ -51,10 +55,9 @@ test('TLS - unauthorized with callback', {timeout: 1000}, function(t) {
};
server.listen(serverPort, function() {
- smtp.connect(serverPort, options, function(session) {
+ var c = smtp.connect(serverPort, options, function (session) {
server.close();
- t.error(session instanceof Error, "connect should succeed");
- session.on('greeting', function(code, messages) {
+ session.on('greeting', function (code, messages) {
t.equal(code, 220);
t.equal(messages[0], "localhost ESMTP");
t.end();
@@ -65,7 +68,7 @@ test('TLS - unauthorized with callback', {timeout: 1000}, function(t) {
});
test('TLS - authorized', {timeout: 1000}, function(t) {
- t.plan(4);
+ t.plan(3);
var server = makeServer();
var options = {
@@ -79,9 +82,9 @@ test('TLS - authorized', {timeout: 1000}, function(t) {
};
server.listen(serverPort, function() {
- smtp.connect(serverPort, options, function(session) {
+ smtp.connect(serverPort, options, function (session) {
server.close();
- t.error(session instanceof Error, "connect should succeed");
+
session.on('greeting', function(code, messages) {
t.equal(code, 220);
t.equal(messages[0], "localhost ESMTP");

0 comments on commit b216bf6

Please sign in to comment.