Permalink
Browse files

Merge pull request #34 from ajaxorg/master

Added onConnect, onError and onDisconnect functions
  • Loading branch information...
2 parents 5c743cc + 5f6055e commit e070b868466a62ecf20ae917d5e2988950435b14 Sergi Mansilla committed Nov 26, 2012
Showing with 68 additions and 6 deletions.
  1. +20 −5 lib/jsftp.js
  2. +1 −1 package.json
  3. +47 −0 test/jsftp_test.js
View
@@ -145,8 +145,13 @@ Ftp.getPasvPort = function(text) {
);
pairStreamer(parse, function(hadError) {
- if (!hadError) // Error case handled by error event.
- self.emitter.emit("disconnect", "Ftp disconnected.");
+ if (!hadError) {// Error case handled by error event.
+ if (self.connected) {
+ self.emitter.emit("disconnect", "Ftp disconnected.");
+ self.onDisconnect && self.onDisconnect();
+ }
+ self.connected = false;
+ }
});
if (this.waitingForEnqueue.length > 0) {
@@ -252,7 +257,15 @@ Ftp.getPasvPort = function(text) {
this.socket.on("error", function(err) {
var errString = "Error on ftp socket: " + err;
- self.emitter.emit("disconnect", errString);
+
+ if (self.connected) {
+ self.emitter.emit("disconnect", errString);
+ self.onDisconnect && self.onDisconnect(err);
+ }
+ self.connected = false;
+
+ if (self.onError)
+ self.onError(err);
// We still want the error to appearin the logs if we are not in debug mode.
if (!DEBUG_MODE)
@@ -261,10 +274,12 @@ Ftp.getPasvPort = function(text) {
this.socket.on("connect", function() {
self.connecting = false;
+ self.connected = true;
+
self.emitter.emit("connect", "connect");
if (self.onConnect)
- self.onConnect();
+ self.onConnect(self.socket);
self._createStreams(self.socket);
@@ -821,4 +836,4 @@ Ftp._concatStream = function(err, socket, callback) {
callback(null, Ftp._concat(pieces));
});
socket.resume();
-};
+};
View
@@ -1,7 +1,7 @@
{
"name": "jsftp",
"id": "jsftp",
- "version": "0.5.1",
+ "version": "0.5.2",
"description": "A sane FTP client implementation for NodeJS",
"keywords": [ "ftp", "protocol", "files", "server", "client", "async" ],
"author": "Sergi Mansilla <sergi.mansilla@gmail.com> (http://sergimansilla.com)",
View
@@ -54,6 +54,7 @@ describe("jsftp test suite", function() {
afterEach(function(next) {
if (daemon)
daemon.kill();
+
setTimeout(function() {
if (ftp) {
ftp.destroy();
@@ -467,4 +468,50 @@ describe("jsftp test suite", function() {
next();
});
});
+
+ it("Test that onConnect is called", function(next) {
+ var FTPCredentials = {
+ host: "localhost",
+ user: "user",
+ port: 3334,
+ pass: "12345",
+ onConnect: function(socket) {
+ assert(ftp2.connected);
+ assert(socket);
+ next();
+ },
+ };
+
+ var ftp2 = new Ftp(FTPCredentials);
+ });
+
+ it("Test that onDisconnect is called", function(next) {
+ var FTPCredentials = {
+ host: "localhost",
+ user: "user",
+ port: 3334,
+ pass: "12345",
+ onDisconnect: function() {
+ next();
+ },
+ };
+
+ var ftp2 = new Ftp(FTPCredentials);
+ ftp2.raw.quit();
+ });
+
+ it("Test that onError is called", function(next) {
+ var FTPCredentials = { // Bad credentials
+ host: "localhost",
+ user: "user_",
+ port: 33342,
+ pass: "12345_",
+ onError: function(error) {
+ assert(error);
+ next();
+ },
+ };
+
+ var ftp2 = new Ftp(FTPCredentials);
+ });
});

0 comments on commit e070b86

Please sign in to comment.