Skip to content

Commit

Permalink
Big file stream bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bancek committed Nov 26, 2012
1 parent 1ab6fc0 commit 08d3859
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions lib/jsftp.js
Expand Up @@ -74,6 +74,7 @@ Ftp.queue = function() {
var stream = function stream($, stop) {
next = $;
stream._update();
stream.next = $;
};

stream._update = function _update() {
Expand Down Expand Up @@ -133,13 +134,7 @@ Ftp.getPasvPort = function(text) {
// a stream that keeps yielding command/response pairs as soon as each pair
// becomes available.
var pairStreamer = S.zip(
// We ignore FTP marks for now. They don't convey useful
// information. A more elegant solution should be found in the
// future.
S.filter(
function(x) { return !Ftp.isMark(x.code); },
self.serverResponse(input)
),
self.serverResponse(input),
// Stream of FTP commands from the client.
S.append(S.list(null), function(next, stop) { cmd = next; })
);
Expand Down Expand Up @@ -370,23 +365,30 @@ Ftp.getPasvPort = function(text) {
var ftpResponse = action[0];
var command = action[1];
var callback = command[1];
if (callback) {
if (!ftpResponse) {
callback(new Error("FTP response not defined"));
}
// In FTP every response code above 399 means error in some way.
// Since the RFC is not respected by many servers, we are going to
// overgeneralize and consider every value above 399 as an error.
else if (ftpResponse.code > 399) {
var err = new Error(ftpResponse.text || "Unknown FTP error.");
err.code = ftpResponse.code;
callback(err);
}
else {
if (Ftp.isMark(ftpResponse.code)) {
if (callback.acceptsMarks) {
callback(null, ftpResponse);
}
this.cmdQueue.next(['', callback]);
} else {
if (callback) {
if (!ftpResponse) {
callback(new Error("FTP response not defined"));
}
// In FTP every response code above 399 means error in some way.
// Since the RFC is not respected by many servers, we are going to
// overgeneralize and consider every value above 399 as an error.
else if (ftpResponse.code > 399) {
var err = new Error(ftpResponse.text || "Unknown FTP error.");
err.code = ftpResponse.code;
callback(err);
}
else {
callback(null, ftpResponse);
}
}
this.nextCmd();
}
this.nextCmd();
};

this._initialize = function(callback) {
Expand Down Expand Up @@ -553,9 +555,18 @@ Ftp.getPasvPort = function(text) {
if (err) return callback(err);

socket.pause();
self._enqueueCmd("retr " + path, function(err, res) {
callback(err, socket);
});

var called = false;

var cmdCallback = function(err, res) {
if (!called) {
called = true;
callback(err, socket);
}
};
cmdCallback.acceptsMarks = true;

self._enqueueCmd("retr " + path, cmdCallback);
});
};

Expand Down

0 comments on commit 08d3859

Please sign in to comment.