Skip to content

Commit f3dafa9

Browse files
committed
Use external ftp-response-parser module to parse responses
1 parent e064af4 commit f3dafa9

4 files changed

Lines changed: 25 additions & 93 deletions

File tree

lib/jsftp.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
var Net = require("net");
1111
var EventEmitter = require("events").EventEmitter;
1212
var es = require("event-stream");
13-
var responseHandler = require("./response");
13+
var ResponseParser = require("ftp-response-parser");
1414
var Utils = require("./utils");
1515
var util = require("util");
1616
var fs = require("fs");
@@ -63,7 +63,7 @@ var Ftp = module.exports = function(cfg) {
6363
this.port = this.port || FTP_PORT;
6464
this.pending = []; // Pending requests
6565
this.cmdBuffer_ = [];
66-
this.responseHandler = responseHandler();
66+
this.resParser = new ResponseParser();
6767

6868
// Generate generic methods from parameter names. they can easily be
6969
// overriden if we need special behavior. they accept any parameters given,
@@ -98,10 +98,7 @@ Ftp.prototype._createSocket = function(port, host, firstAction) {
9898
};
9999

100100
Ftp.prototype._createStreams = function(socket) {
101-
this.pipeline = es.pipeline(
102-
socket,
103-
es.split(),
104-
es.mapSync(this.responseHandler));
101+
this.pipeline = es.pipeline(socket, this.resParser);
105102

106103
var self = this;
107104
this.pipeline.on('data', function(data) {
@@ -119,7 +116,7 @@ Ftp.prototype.parseResponse = function(data) {
119116
return;
120117

121118
var next = this.cmdBuffer_[0][1];
122-
if (Utils.isMark(data.code)) {
119+
if (data.isMark) {
123120
// If we receive a Mark and it is not expected, we ignore
124121
// that command
125122
if (!next.expectsMark || next.expectsMark.marks.indexOf(data.code) === -1)
@@ -189,7 +186,6 @@ Ftp.prototype.execute = function(action, callback) {
189186

190187
Ftp.prototype._executeCommand = function(action, callback) {
191188
var self = this;
192-
193189
function executeCmd() {
194190
self.cmdBuffer_.push([action, callback]);
195191
self.nextCmd();
@@ -215,11 +211,8 @@ Ftp.prototype._executeCommand = function(action, callback) {
215211
* @param action {Array} Contains server response and client command info.
216212
*/
217213
Ftp.prototype.parse = function(response, command) {
218-
// In FTP every response code above 399 means error in some way.
219-
// Since the RFC is not respected by many servers, we are going to
220-
// overgeneralize and consider every value above 399 as an error.
221214
var err = null;
222-
if (response.code > 399) {
215+
if (response.isError) {
223216
err = new Error(response.text || "Unknown FTP error.");
224217
err.code = response.code;
225218
}
@@ -259,18 +252,15 @@ Ftp.prototype._parseFeats = function(features) {
259252

260253
Ftp.prototype.getFeatures = function(callback) {
261254
var self = this;
262-
if (!this.features)
263-
this.raw.feat(function(err, response) {
264-
self.features = err ? [] : self._parseFeats(response.text);
265-
self.raw.syst(function(err, res) {
266-
if (!err && res.code === 215)
267-
self.system = res.text.toLowerCase();
268-
269-
callback(null, self.features);
270-
});
255+
this.raw.feat(function(err, response) {
256+
self.features = err ? [] : self._parseFeats(response.text);
257+
self.raw.syst(function(err, res) {
258+
if (!err && res.code === 215)
259+
self.system = res.text.toLowerCase();
260+
261+
callback(null, self.features);
271262
});
272-
else
273-
callback(null, self.features);
263+
});
274264
};
275265

276266
/**

lib/response.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/utils.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ var RE_MULTI = /^(\d\d\d)-/;
66
var RE_SERVER_RESPONSE = /^(\d\d\d)(.*)/;
77

88
var Utils = module.exports = {
9-
// Codes from 100 to 200 are FTP marks
10-
isMark: function(code) {
11-
code = parseInt(code, 10);
12-
return code > 100 && code < 200;
13-
},
14-
159
/**
1610
* Parse raw output of a file listing, trying in to clean up broken listings
1711
* in the process

test/jsftp_test.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ describe("jsftp test suite", function() {
145145
};
146146
var data = {
147147
code: 150,
148-
text: "150 File status okay; about to open data connection."
148+
text: "150 File status okay; about to open data connection.",
149+
isMark: true
149150
};
150151

151152
ftp.cmdBuffer_ = [
@@ -163,7 +164,8 @@ describe("jsftp test suite", function() {
163164
var cb = sinon.spy();
164165
var data = {
165166
code: 150,
166-
text: "150 File status okay; about to open data connection."
167+
text: "150 File status okay; about to open data connection.",
168+
isMark: true
167169
};
168170

169171
ftp.cmdBuffer_ = [
@@ -195,16 +197,20 @@ describe("jsftp test suite", function() {
195197
};
196198
var data1 = {
197199
code: 150,
198-
text: "150 File status okay; about to open data connection."
200+
text: "150 File status okay; about to open data connection.",
201+
isMark: true
199202
};
200203
var data2 = {
201204
code: 226,
202-
text: "226 Transfer complete."
205+
text: "226 Transfer complete.",
206+
isMark: false
203207
};
204208

205209
ftp.cmdBuffer_ = [
206210
["retr fakefile.txt", cb],
207-
["list /", function() {}]
211+
["list /",
212+
function() {}
213+
]
208214
];
209215
ftp.parse = sinon.spy();
210216
ftp.ignoreCmdCode = 150;
@@ -230,19 +236,6 @@ describe("jsftp test suite", function() {
230236
});
231237
});
232238

233-
it("test getFeatures", function(next) {
234-
ftp.getFeatures(function(err, feats) {
235-
assert.ok(Array.isArray(feats));
236-
assert.ok(Array.isArray(ftp.features));
237-
assert.ok(ftp.system.length > 0);
238-
239-
var feat = ftp.features[0];
240-
assert.ok(ftp.hasFeat(feat));
241-
assert.equal(false, ftp.hasFeat("madeup-feat"));
242-
next();
243-
});
244-
});
245-
246239
it("test print working directory", function(next) {
247240
ftp.raw.pwd(function(err, res) {
248241
assert(!err, err);
@@ -689,7 +682,7 @@ drwxr-xr-x 2 0 0 4096 Apr 16 2011 denton\r\n\
689682
});
690683

691684
ftp.ls(paths[1], function showFile(err, res1) {
692-
assert.ok(!!err);
685+
assert.ok( !! err);
693686
processed += 1;
694687
if (processed === 2) next();
695688
});

0 commit comments

Comments
 (0)