Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make sure to buffer events in pause

  • Loading branch information...
commit 7b845773719cfe5d7ef5e829320e36919e271379 1 parent 6ce72ce
@fjakobs fjakobs authored
Showing with 49 additions and 12 deletions.
  1. +43 −2 lib/jsftp.js
  2. +6 −10 test/jsftp_test.js
View
45 lib/jsftp.js
@@ -516,9 +516,9 @@ Ftp.getPasvPort = function(text) {
var self = this;
this.getPasvSocket(function(err, socket) {
if (err) return callback(err);
- if (socket.pause) socket.pause();
+
+ socket.pause();
self._enqueueCmd("retr " + path, function(err, res) {
- if (socket.resume) socket.resume();
callback(err, socket);
});
});
@@ -582,6 +582,47 @@ Ftp.getPasvPort = function(text) {
return callback(new Error("PASV: Bad port"));
var socket = Net.createConnection(port, self.host);
+
+ var buffer = [];
+ var pause = socket.pause;
+ var pauseCount = 0;
+ socket.pause = function() {
+ if (++pauseCount !== 1)
+ return;
+ pause.call(this);
+ socket.on("data", onData);
+ socket.on("end", onEnd);
+ socket.on("close", onClose);
+ socket.on("error", onError);
+ }
+ function onData(chunk) {
+ buffer.push(["data", chunk]);
+ }
+ function onEnd() {
+ buffer.push(["end"]);
+ }
+ function onClose(hadError) {
+ buffer.push(["close", hadError]);
+ }
+ function onError(err) {
+ buffer.push(["error", err]);
+ }
+ var resume = socket.resume;
+ socket.resume = function() {
+ if (pauseCount == 0)
+ return;
+ if (--pauseCount !== 0)
+ return;
+ socket.removeListener("data", onData);
+ socket.removeListener("end", onEnd);
+ socket.removeListener("close", onClose);
+ socket.removeListener("error", onError);
+ buffer.forEach(function (event) {
+ socket.emit.apply(socket, event);
+ });
+ resume.call(this);
+ }
+
socket.setTimeout(self.timeout || TIMEOUT);
// Send the passive socket to the callback.
callback(null, socket);
View
16 test/jsftp_test.js
@@ -385,9 +385,6 @@ describe("jsftp test suite", function() {
assert(!err, err);
assert.ok(readable);
- if (readable.pause)
- readable.pause();
-
readable.on("error", error);
function error(err) {
@@ -404,10 +401,11 @@ describe("jsftp test suite", function() {
assert.ok(!err);
readable.pipe(socket);
- if (readable.resume)
- readable.resume();
+ readable.on("close", finish);
+ readable.on("error", error);
+ readable.resume();
- var finish = function(hadError) {
+ function finish(hadError) {
assert.ok(!hadError);
ftp.get(remoteCopy, function(err, data) {
assert.ok(!err, err);
@@ -418,10 +416,7 @@ describe("jsftp test suite", function() {
next();
});
});
- };
-
- readable.on("close", finish);
- readable.on("error", error);
+ }
});
});
});
@@ -485,6 +480,7 @@ function concatStream(err, socket, callback) {
callback(null, concat(pieces));
});
+ socket.resume();
}
function concat(bufs) {
Please sign in to comment.
Something went wrong with that request. Please try again.