diff --git a/lib/jsftp.js b/lib/jsftp.js index 1dcb794..8cf3332 100644 --- a/lib/jsftp.js +++ b/lib/jsftp.js @@ -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"); @@ -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) { diff --git a/lib/utils.js b/lib/utils.js index 6101dff..e34eefe 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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); diff --git a/package.json b/package.json index e6c72bc..7ad3329 100644 --- a/package.json +++ b/package.json @@ -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 (http://sergimansilla.com)", @@ -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": { diff --git a/test/jsftp_test.js b/test/jsftp_test.js index e13a482..1389a11 100755 --- a/test/jsftp_test.js +++ b/test/jsftp_test.js @@ -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);