Permalink
Browse files

Added truncated text test and small cosmetic changes

  • Loading branch information...
1 parent b476733 commit 4415457617ab0497d432f8d99bf25617d7ecc6ee @sergi committed Apr 18, 2013
Showing with 80 additions and 55 deletions.
  1. +1 −0 .gitignore
  2. +0 −1 lib/ftpParser.js
  3. +29 −24 lib/jsftp.js
  4. +1 −1 package.json
  5. +49 −29 test/jsftp_test.js
View
@@ -1,4 +1,5 @@
*.c9save
+.idea
*.swp
*~
*.bak
View
@@ -124,7 +124,6 @@ exports.entryParser = function(entry) {
return parsers.msdos(entry);
}
else {
- console.log("Unrecognized format: \n" + entry);
return null;
}
};
View
@@ -94,10 +94,16 @@ Ftp.isMark = function(code) {
return code > 100 && code < 200;
};
-Ftp.parseEntry = function(entries) {
+/**
+ * Parse raw output of a file listing, trying in to clean up broken listings in
+ * the process
+ * @param {String} listing Raw file listing coming from a 'list' or 'stat'
+ * @returns {Object[]}
+ */
+Ftp.parseEntry = function(listing) {
var t, entry, parsedEntry;
var parsed = [];
- var splitEntries = entries.split(/\r\n|\n/);
+ var splitEntries = listing.split(/\r\n|\n/);
for (var i = 0; i < splitEntries.length; i++) {
entry = splitEntries[i];
@@ -107,18 +113,18 @@ Ftp.parseEntry = function(entries) {
parsedEntry = Parser.entryParser(entry);
if (parsedEntry === null) {
- if (splitEntries[i+1]) {
- t = Parser.entryParser(entry + splitEntries[i+1])
+ if (splitEntries[i + 1]) {
+ t = Parser.entryParser(entry + splitEntries[i + 1]);
if (t !== null) {
- splitEntries[i+1] = entry + splitEntries[i+1];
+ splitEntries[i + 1] = entry + splitEntries[i + 1];
continue;
}
}
- if (splitEntries[i-1] && parsed.length > 0) {
- t = Parser.entryParser(splitEntries[i-1] + entry)
+ if (splitEntries[i - 1] && parsed.length > 0) {
+ t = Parser.entryParser(splitEntries[i - 1] + entry);
if (t !== null) {
- parsed[parsed.length-1] = t
+ parsed[parsed.length - 1] = t
}
}
}
@@ -135,10 +141,8 @@ Ftp.getPasvPort = function(text) {
if (!match) return false;
// Array containing the passive host and the port number
- var address = [match[1].replace(/,/g, "."),
- (parseInt(match[2], 10) & 255) * 256 + (parseInt(match[3], 10) & 255)];
-
- return address
+ return [match[1].replace(/,/g, "."),
+ (parseInt(match[2], 10) & 255) * 256 + (parseInt(match[3], 10) & 255)];
};
(function() {
@@ -229,7 +233,7 @@ Ftp.getPasvPort = function(text) {
};
this._enqueueCmd = function(action, callback) {
- if (!callback) callback = function(){};
+ if (!callback) callback = function() {};
if (this.socket && this.socket.writable) {
this._authAndEnqueue(action, callback);
}
@@ -394,7 +398,7 @@ Ftp.getPasvPort = function(text) {
return;
var ftpResponse = action[0];
- var command = action[1];
+ var command = action[1];
var callback = command[1];
if (ftpResponse != null && Ftp.isMark(ftpResponse.code)) {
if (callback != null && callback.acceptsMarks) {
@@ -454,7 +458,7 @@ Ftp.getPasvPort = function(text) {
/**
* Returns true if the current server has the requested feature. False otherwise.
*
- * @param {String} Feature to look for
+ * @param {String} feature Feature to look for
* @returns {Boolean} Whether the current server has the feature
*/
this.hasFeat = function(feature) {
@@ -464,8 +468,8 @@ Ftp.getPasvPort = function(text) {
/**
* Returns an array of features supported by the current FTP server
*
- * @param {String} Server response for the 'FEAT' command
- * @returns {Array} Array of feature names
+ * @param {String} features Server response for the 'FEAT' command
+ * @returns {String[]} Array of feature names
*/
this._parseFeats = function(features) {
// Ignore header and footer
@@ -551,7 +555,8 @@ Ftp.getPasvPort = function(text) {
/**
* Lists a folder's contents using a passive connection.
*
- * @param filePath {String} Remote file/folder path
+ * @param {String} filePath Remote file/folder path
+ * @param {Function} callback Function to call with results
*/
this.list = function(filePath, callback) {
if (arguments.length === 1) {
@@ -720,7 +725,7 @@ Ftp.getPasvPort = function(text) {
this.currentPasv = fn;
this.currentPasv();
}
- },
+ };
/**
* Provides information about files. It lists a directory contents or
@@ -771,9 +776,9 @@ Ftp.getPasvPort = function(text) {
// trips to the server to check.
if ((err && (err.code === 502 || err.code === 500)) ||
(self.system && self.system.indexOf("hummingbird") > -1))
- // Not sure if the "hummingbird" system check ^^^ is still
- // necessary. If they support any standards, the 500 error
- // should have us covered. Let's leave it for now.
+ // Not sure if the "hummingbird" system check ^^^ is still
+ // necessary. If they support any standards, the 500 error
+ // should have us covered. Let's leave it for now.
{
self.useList = true;
self.list(filePath, entriesToList);
@@ -808,14 +813,14 @@ Ftp._concat = function(bufs) {
if (!Array.isArray(bufs))
bufs = Array.prototype.slice.call(arguments);
- for (var i=0, l=bufs.length; i<l; i++) {
+ for (var i = 0, l = bufs.length; i < l; i++) {
buffer = bufs[i];
length += buffer.length;
}
buffer = new Buffer(length);
- bufs.forEach(function(buf, i) {
+ bufs.forEach(function(buf) {
buf.copy(buffer, index, 0, buf.length);
index += buf.length;
});
View
@@ -1,7 +1,7 @@
{
"name": "jsftp",
"id": "jsftp",
- "version": "0.5.9",
+ "version": "0.6.0",
"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
@@ -4,7 +4,7 @@
* @author Sergi Mansilla <sergi.mansilla@gmail.com>
* @license https://github.com/sergi/jsFTP/blob/master/LICENSE MIT License
*/
- /*global it describe beforeEach afterEach */
+/*global it describe beforeEach afterEach */
"use strict";
@@ -36,11 +36,11 @@ describe("jsftp test suite", function() {
try {
daemon = exec('python', ['test/basic_ftpd.py']);
}
- catch(e) {
+ catch (e) {
console.log(
"There was a problem trying to start the FTP service." +
- " . This could be because you don't have enough permissions" +
- "to run the FTP service on the given port.\n\n" + e
+ " . This could be because you don't have enough permissions" +
+ "to run the FTP service on the given port.\n\n" + e
);
}
}
@@ -64,17 +64,17 @@ describe("jsftp test suite", function() {
}, 200);
}),
- it("test features command", function(next) {
- ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
- assert.ok(Array.isArray(ftp.features));
+ it("test features command", function(next) {
+ ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
+ assert.ok(Array.isArray(ftp.features));
- if (ftp.features.length) {
- var feat = ftp.features[0];
- assert.ok(ftp.hasFeat(feat));
- }
- next();
+ if (ftp.features.length) {
+ var feat = ftp.features[0];
+ assert.ok(ftp.hasFeat(feat));
+ }
+ next();
+ });
});
- });
it("test initialize", function(next) {
assert.equal(ftp.host, FTPCredentials.host);
@@ -128,8 +128,28 @@ describe("jsftp test suite", function() {
});
});
+ it("test listing with bad line breaks", function(next) {
+ var badStr = "\
+213-Status follows:\r\n\
+-rw-r--r-- 1 0 0 105981956 Dec 20 18:07 GAT\r\n\
+SBY.MPG\r\n\
+-rw-r--r-- 1 0 0 74450948 Jan 17 18:16 GIJO.MPG\r\n\
+drwxr-xr-x 3 0 0 4096 Apr 16 2011 bourd\n\
+arie\r\n\
+drwxr-xr-x 2 0 0 4096 Apr 16 2011 denton\r\n\
+213 End of status";
+
+ var entries = Ftp.parseEntry(badStr);
+ assert.equal("GATSBY.MPG", entries[0].name)
+ assert.equal("GIJO.MPG", entries[1].name)
+ assert.equal("bourdarie", entries[2].name)
+ assert.equal("denton", entries[3].name)
+
+ next();
+ });
+
it("test passive listing of current directory", function(next) {
- ftp.list(remoteCWD, function(err, res){
+ ftp.list(remoteCWD, function(err, res) {
assert.ok(!err, err);
next();
});
@@ -268,40 +288,40 @@ describe("jsftp test suite", function() {
it("test get a big file stream", function(next) {
var remotePath = remoteCWD + "/bigfile.test";
- var data = (new Array(1*1024*1024)).join("x");
+ var data = (new Array(1 * 1024 * 1024)).join("x");
var buffer = new Buffer(data, "binary");
ftp.put(remotePath, buffer, function(err, res) {
assert.ok(!err, err);
ftp.getGetSocket(remotePath, function(err, socket) {
- assert.ok(!err, err);
+ assert.ok(!err, err);
- socket.resume();
+ socket.resume();
- var counter = 0;
+ var counter = 0;
- socket.on('data', function(data) {
- counter += data.length;
- });
+ socket.on('data', function(data) {
+ counter += data.length;
+ });
- socket.on('close', function() {
- assert.equal(buffer.length, counter);
+ socket.on('close', function() {
+ assert.equal(buffer.length, counter);
- ftp.raw.dele(remotePath, function(err, data) {
- assert.ok(!err);
- next();
+ ftp.raw.dele(remotePath, function(err, data) {
+ assert.ok(!err);
+ next();
+ });
});
- });
});
});
});
it("test put a big file stream", function(next) {
var remotePath = remoteCWD + "/bigfile.test";
- var data = (new Array(1*1024*1024)).join("x");
+ var data = (new Array(1 * 1024 * 1024)).join("x");
var buffer = new Buffer(data, "binary");
@@ -390,7 +410,7 @@ describe("jsftp test suite", function() {
var count = 0;
function handler(err, res) {
- assert.ok(!err);
+ assert.ok(!err);
if (++count == 3)
next();
}

0 comments on commit 4415457

Please sign in to comment.