Skip to content

Commit

Permalink
Better feature parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi committed Dec 23, 2014
1 parent d0c8a93 commit 36b6d8a
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/jsftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ var COMMANDS = [
'chmod', 'size'
];

// Regular Expressions
var RE_PASV = /([-\d]+,[-\d]+,[-\d]+,[-\d]+),([-\d]+),([-\d]+)/;
var FTP_NEWLINE = /\r\n|\n/;

function getPasvPort(text, cb) {
var RE_PASV = /([-\d]+,[-\d]+,[-\d]+,[-\d]+),([-\d]+),([-\d]+)/;
var match = RE_PASV.exec(text);
if (!match) {
return cb(new Error('Bad passive host/port combination'));
Expand Down Expand Up @@ -260,10 +263,11 @@ Ftp.prototype.hasFeat = function(feature) {
* @return {String[]} Array of feature names
*/
Ftp.prototype._parseFeats = function(features) {
// Ignore header and footer
return features.split(/\r\n|\n/).slice(1, -1).map(function(feat) {
return (/^\s*(\w*)\s*/).exec(feat)[1].trim().toLowerCase();
});
// Split and ignore header and footer
var featureLines = features.split(FTP_NEWLINE).slice(1, -1);
return featureLines
.map(function(feat) { return feat.trim().toLowerCase(); })
.filter(function(feat) { return !!feat; });
};

// Below this point all the methods are action helpers for FTP that compose
Expand All @@ -273,8 +277,9 @@ Ftp.prototype.getFeatures = function(callback) {
this.raw.feat(function(err, response) {
self.features = err ? [] : self._parseFeats(response.text);
self.raw.syst(function(err, res) {
if (!err && res.code === 215)
if (!err && res.code === 215) {
self.system = res.text.toLowerCase();
}

callback(null, self.features);
});
Expand Down

0 comments on commit 36b6d8a

Please sign in to comment.