Skip to content

Commit

Permalink
Extract parse listing away into parse-listing module
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi committed Nov 19, 2013
1 parent f3dafa9 commit 6cab1a9
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 89 deletions.
4 changes: 3 additions & 1 deletion lib/jsftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Net = require("net");
var EventEmitter = require("events").EventEmitter;
var es = require("event-stream");
var ResponseParser = require("ftp-response-parser");
var ListingParser = require("parse-listing");
var Utils = require("./utils");
var util = require("util");
var fs = require("fs");
Expand Down Expand Up @@ -575,7 +576,8 @@ Ftp.prototype.ls = function(filePath, callback) {
if (err) {
return callback(err);
}
callback(null, Utils.parseEntry(entries.text || entries));

ListingParser.parseFtpEntries(entries.text || entries, callback);
}

if (this.useList) {
Expand Down
65 changes: 0 additions & 65 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,4 @@
var Parser = require("parse-listing");
var async = require("async");

var RE_RES = /^(\d\d\d)\s(.*)/;
var RE_MULTI = /^(\d\d\d)-/;
var RE_SERVER_RESPONSE = /^(\d\d\d)(.*)/;

var Utils = module.exports = {
/**
* 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[]}
*/
parseEntry: function(listing) {
var t, parsedEntry;
var i = 0;
var parsed = [];
var splitEntries = listing.split(/\r\n|\n/);
async.eachSeries(splitEntries, function(entry, next) {
function _next() {
i += 1;
next();
}

// Some servers include an official code-multiline sign at the beginning
// of every string. We must strip it if that's the case.
if (RE_MULTI.test(entry))
entry = entry.substr(3);

entry = entry.trim();

// Filter file-listing results from 'STAT' command, since they include
// server responses before and after the file listing.
// Issue: https://github.com/sergi/jsftp/issues/3
if (RE_SERVER_RESPONSE.test(entry) ||
RE_RES.test(entry) || RE_MULTI.test(entry)) {
return _next();
}

parsedEntry = Parser.parseEntry(entry);
if (parsedEntry === null) {
if (splitEntries[i + 1]) {
t = Parser.parseEntry(entry + splitEntries[i + 1]);
if (t !== null) {
splitEntries[i + 1] = entry + splitEntries[i + 1];
return _next();
}
}

if (splitEntries[i - 1] && parsed.length > 0) {
t = Parser.parseEntry(splitEntries[i - 1] + entry);
if (t !== null) {
parsed[parsed.length - 1] = t;
}
}
}
else if (parsedEntry) {
parsed.push(parsedEntry);
}
_next();
});

return parsed;
},

getPasvPort: function(text) {
var RE_PASV = /([-\d]+,[-\d]+,[-\d]+,[-\d]+),([-\d]+),([-\d]+)/;
var match = RE_PASV.exec(text);
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jsftp",
"id": "jsftp",
"version": "1.1.1",
"version": "1.2.0",
"description": "A sane FTP client implementation for NodeJS",
"keywords": [ "ftp", "protocol", "files", "server", "client", "async" ],
"author": "Sergi Mansilla <[email protected]> (http://sergimansilla.com)",
Expand All @@ -15,8 +15,7 @@
},
"dependencies": {
"event-stream": "~3.0.14",
"parse-listing": "~1.0.0",
"async": "~0.2.9",
"parse-listing": "~1.1.0",
"once": "~1.3.0"
},
"devDependencies": {
Expand Down
20 changes: 0 additions & 20 deletions test/jsftp_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,26 +274,6 @@ 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 = Utils.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) {
assert.ok(!err, err);
Expand Down

0 comments on commit 6cab1a9

Please sign in to comment.