diff --git a/src/commands/registration/appe.js b/src/commands/registration/appe.js index 01fced5..afeb329 100644 --- a/src/commands/registration/appe.js +++ b/src/commands/registration/appe.js @@ -7,4 +7,4 @@ module.exports = { }, syntax: '{{cmd}} [path]', description: 'Append to a file' -} +}; diff --git a/src/commands/registration/auth.js b/src/commands/registration/auth.js index 4d570ca..4ad9ec6 100644 --- a/src/commands/registration/auth.js +++ b/src/commands/registration/auth.js @@ -16,7 +16,7 @@ module.exports = { flags: { no_auth: true } -} +}; function handleTLS() { return this.reply(504); diff --git a/src/commands/registration/cdup.js b/src/commands/registration/cdup.js index a37a6e7..ac72f8d 100644 --- a/src/commands/registration/cdup.js +++ b/src/commands/registration/cdup.js @@ -2,10 +2,10 @@ const cwd = require('./cwd').handler; module.exports = { directive: ['CDUP', 'XCUP'], - handler: function(args) { + handler: function (args) { args.command._ = [args.command._[0], '..']; return cwd.call(this, args); }, syntax: '{{cmd}}', description: 'Change to Parent Directory' -} +}; diff --git a/src/commands/registration/cwd.js b/src/commands/registration/cwd.js index d8dfa57..1977ce9 100644 --- a/src/commands/registration/cwd.js +++ b/src/commands/registration/cwd.js @@ -7,7 +7,7 @@ module.exports = { if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs.chdir) return this.reply(402, 'Not supported by file system'); - return when(this.fs.chdir(command._[1])) + return when.try(this.fs.chdir.bind(this.fs), command._[1]) .then(cwd => { const path = cwd ? `"${escapePath(cwd)}"` : undefined; return this.reply(250, path); @@ -19,4 +19,4 @@ module.exports = { }, syntax: '{{cmd}}[path]', description: 'Change working directory' -} +}; diff --git a/src/commands/registration/dele.js b/src/commands/registration/dele.js index ca06ba3..e843c28 100644 --- a/src/commands/registration/dele.js +++ b/src/commands/registration/dele.js @@ -6,7 +6,7 @@ module.exports = { if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs.delete) return this.reply(402, 'Not supported by file system'); - return when(this.fs.delete(command._[1])) + return when.try(this.fs.delete.bind(this.fs), command._[1]) .then(() => { return this.reply(250); }) @@ -17,4 +17,4 @@ module.exports = { }, syntax: '{{cmd}} [path]', description: 'Delete file' -} +}; diff --git a/src/commands/registration/feat.js b/src/commands/registration/feat.js index 2135e5d..56ea0f0 100644 --- a/src/commands/registration/feat.js +++ b/src/commands/registration/feat.js @@ -18,4 +18,4 @@ module.exports = { flags: { no_auth: true } -} +}; diff --git a/src/commands/registration/help.js b/src/commands/registration/help.js index 450023d..a6d8ba4 100644 --- a/src/commands/registration/help.js +++ b/src/commands/registration/help.js @@ -21,4 +21,4 @@ module.exports = { flags: { no_auth: true } -} +}; diff --git a/src/commands/registration/list.js b/src/commands/registration/list.js index 4c54f4a..8952d4d 100644 --- a/src/commands/registration/list.js +++ b/src/commands/registration/list.js @@ -6,7 +6,7 @@ const getFileStat = require('../../helpers/file-stat'); // http://cr.yp.to/ftp/list/eplf.html module.exports = { directive: 'LIST', - handler: function ({log, command, previous_command} = {}) { + handler: function ({log, command} = {}) { if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs.list) return this.reply(402, 'Not supported by file system'); @@ -19,9 +19,9 @@ module.exports = { this.commandSocket.pause(); dataSocket = socket; }) - .then(() => when(this.fs.list(directory))) + .then(() => when.try(this.fs.list.bind(this.fs), directory)) .then(files => { - const getFileMessage = (file) => { + const getFileMessage = file => { if (simple) return file.name; return getFileStat(file, _.get(this, 'server.options.file_format', 'ls')); }; @@ -33,7 +33,7 @@ module.exports = { message, socket: dataSocket }; - }) + }); return this.reply(150) .then(() => { if (fileList.length) return this.reply({}, ...fileList); @@ -57,4 +57,4 @@ module.exports = { }, syntax: '{{cmd}} [path(optional)]', description: 'Returns information of a file or directory if specified, else information of the current working directory is returned' -} +}; diff --git a/src/commands/registration/mdtm.js b/src/commands/registration/mdtm.js index af01a68..9e59c70 100644 --- a/src/commands/registration/mdtm.js +++ b/src/commands/registration/mdtm.js @@ -7,10 +7,10 @@ module.exports = { if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs.get) return this.reply(402, 'Not supported by file system'); - return when(this.fs.get(command._[1])) + return when.try(this.fs.get.bind(this.fs), command._[1]) .then(fileStat => { const modificationTime = format(fileStat.mtime, 'YYYYMMDDHHmmss.SSS'); - return this.reply(213, modificationTime) + return this.reply(213, modificationTime); }) .catch(err => { log.error(err); @@ -22,4 +22,4 @@ module.exports = { flags: { feat: 'MDTM' } -} +}; diff --git a/src/commands/registration/mkd.js b/src/commands/registration/mkd.js index 3898905..2b48c85 100644 --- a/src/commands/registration/mkd.js +++ b/src/commands/registration/mkd.js @@ -7,7 +7,7 @@ module.exports = { if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs.mkdir) return this.reply(402, 'Not supported by file system'); - return when(this.fs.mkdir(command._[1])) + return when.try(this.fs.mkdir.bind(this.fs), command._[1]) .then(dir => { const path = dir ? `"${escapePath(dir)}"` : undefined; return this.reply(257, path); @@ -19,4 +19,4 @@ module.exports = { }, syntax: '{{cmd}}[path]', description: 'Make directory' -} +}; diff --git a/src/commands/registration/mode.js b/src/commands/registration/mode.js index 994a6ad..3b89672 100644 --- a/src/commands/registration/mode.js +++ b/src/commands/registration/mode.js @@ -8,4 +8,4 @@ module.exports = { flags: { obsolete: true } -} +}; diff --git a/src/commands/registration/nlst.js b/src/commands/registration/nlst.js index 7cea946..65e1f89 100644 --- a/src/commands/registration/nlst.js +++ b/src/commands/registration/nlst.js @@ -7,4 +7,4 @@ module.exports = { }, syntax: '{{cmd}} [path(optional)]', description: 'Returns a list of file names in a specified directory' -} +}; diff --git a/src/commands/registration/noop.js b/src/commands/registration/noop.js index b462a3d..a6cbe6f 100644 --- a/src/commands/registration/noop.js +++ b/src/commands/registration/noop.js @@ -8,4 +8,4 @@ module.exports = { flags: { no_auth: true } -} +}; diff --git a/src/commands/registration/opts.js b/src/commands/registration/opts.js index 10ac98d..1f27155 100644 --- a/src/commands/registration/opts.js +++ b/src/commands/registration/opts.js @@ -5,4 +5,4 @@ module.exports = { }, syntax: '{{cmd}}', description: 'Select options for a feature' -} +}; diff --git a/src/commands/registration/pass.js b/src/commands/registration/pass.js index a774a8a..d52af50 100644 --- a/src/commands/registration/pass.js +++ b/src/commands/registration/pass.js @@ -24,4 +24,4 @@ module.exports = { flags: { no_auth: true } -} +}; diff --git a/src/commands/registration/pasv.js b/src/commands/registration/pasv.js index d2eeb1c..3687eea 100644 --- a/src/commands/registration/pasv.js +++ b/src/commands/registration/pasv.js @@ -2,7 +2,7 @@ const PassiveConnector = require('../../connector/passive'); module.exports = { directive: 'PASV', - handler: function ({command} = {}) { + handler: function () { this.connector = new PassiveConnector(this); return this.connector.setupServer() .then(server => { @@ -17,4 +17,4 @@ module.exports = { }, syntax: '{{cmd}}', description: 'Initiate passive mode' -} +}; diff --git a/src/commands/registration/port.js b/src/commands/registration/port.js index d3d2556..f64d287 100644 --- a/src/commands/registration/port.js +++ b/src/commands/registration/port.js @@ -12,10 +12,10 @@ module.exports = { const port = portBytes[0] * 256 + portBytes[1]; return this.connector.setupConnection(ip, port) - .then(socket => { + .then(() => { return this.reply(200); - }) + }); }, syntax: '{{cmd}} [x,x,x,x,y,y]', description: 'Specifies an address and port to which the server should connect' -} +}; diff --git a/src/commands/registration/pwd.js b/src/commands/registration/pwd.js index dce520e..def35db 100644 --- a/src/commands/registration/pwd.js +++ b/src/commands/registration/pwd.js @@ -3,11 +3,11 @@ const escapePath = require('../../helpers/escape-path'); module.exports = { directive: ['PWD', 'XPWD'], - handler: function ({log, command} = {}) { + handler: function ({log} = {}) { if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs.currentDirectory) return this.reply(402, 'Not supported by file system'); - return when(this.fs.currentDirectory()) + return when.try(this.fs.currentDirectory.bind(this.fs)) .then(cwd => { const path = cwd ? `"${escapePath(cwd)}"` : undefined; return this.reply(257, path); @@ -15,8 +15,8 @@ module.exports = { .catch(err => { log.error(err); return this.reply(550, err.message); - }) + }); }, syntax: '{{cmd}}', description: 'Print current working directory' -} +}; diff --git a/src/commands/registration/quit.js b/src/commands/registration/quit.js index d3350fa..8214146 100644 --- a/src/commands/registration/quit.js +++ b/src/commands/registration/quit.js @@ -8,4 +8,4 @@ module.exports = { flags: { no_auth: true } -} +}; diff --git a/src/commands/registration/retr.js b/src/commands/registration/retr.js index e557b1c..1e8f4b8 100644 --- a/src/commands/registration/retr.js +++ b/src/commands/registration/retr.js @@ -12,7 +12,7 @@ module.exports = { this.commandSocket.pause(); dataSocket = socket; }) - .then(() => when(this.fs.read(command._[1]))) + .then(() => when.try(this.fs.read.bind(this.fs), command._[1])) .then(stream => { return when.promise((resolve, reject) => { dataSocket.on('error', err => stream.emit('error', err)); @@ -34,8 +34,8 @@ module.exports = { .finally(() => { this.connector.end(); this.commandSocket.resume(); - }) + }); }, syntax: '{{cmd}} [path]', description: 'Retrieve a copy of the file' -} +}; diff --git a/src/commands/registration/rmd.js b/src/commands/registration/rmd.js index 3bdaf44..65f5f9a 100644 --- a/src/commands/registration/rmd.js +++ b/src/commands/registration/rmd.js @@ -7,4 +7,4 @@ module.exports = { }, syntax: '{{cmd}} [path]', description: 'Remove a directory' -} +}; diff --git a/src/commands/registration/rnfr.js b/src/commands/registration/rnfr.js index 020b93a..c84e59f 100644 --- a/src/commands/registration/rnfr.js +++ b/src/commands/registration/rnfr.js @@ -7,7 +7,7 @@ module.exports = { if (!this.fs.get) return this.reply(402, 'Not supported by file system'); const fileName = command._[1]; - return when(this.fs.get(fileName)) + return when.try(this.fs.get.bind(this.fs), fileName) .then(() => { this.renameFrom = fileName; return this.reply(350); diff --git a/src/commands/registration/rnto.js b/src/commands/registration/rnto.js index dcfb4b3..755c714 100644 --- a/src/commands/registration/rnto.js +++ b/src/commands/registration/rnto.js @@ -11,7 +11,7 @@ module.exports = { const from = this.renameFrom; const to = command._[1]; - return when(this.fs.rename(from, to)) + return when.try(this.fs.rename.bind(this.fs), from, to) .then(() => { return this.reply(250); }) @@ -21,8 +21,8 @@ module.exports = { }) .finally(() => { delete this.renameFrom; - }) + }); }, syntax: '{{cmd}} [name]', description: 'Rename to' -} +}; diff --git a/src/commands/registration/site/chmod.js b/src/commands/registration/site/chmod.js index 9210a95..92b6d64 100644 --- a/src/commands/registration/site/chmod.js +++ b/src/commands/registration/site/chmod.js @@ -10,5 +10,5 @@ module.exports = function ({log, command} = {}) { .catch(err => { log.error(err); return this.reply(500); - }) + }); }; diff --git a/src/commands/registration/site/index.js b/src/commands/registration/site/index.js index bc3b0be..ee1a04a 100644 --- a/src/commands/registration/site/index.js +++ b/src/commands/registration/site/index.js @@ -14,10 +14,10 @@ module.exports = { const subCommand = { _: [subverb, ...subparameters], directive: subverb - } + }; const handler = registry[subverb].handler.bind(this); return when.try(handler, { log: subLog, command: subCommand }); }, syntax: '{{cmd}} [subVerb] [subParams]', description: 'Sends site specific commands to remote server' -} +}; diff --git a/src/commands/registration/size.js b/src/commands/registration/size.js index d9fc660..2f1a625 100644 --- a/src/commands/registration/size.js +++ b/src/commands/registration/size.js @@ -6,7 +6,7 @@ module.exports = { if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs.get) return this.reply(402, 'Not supported by file system'); - return when(this.fs.get(command._[1])) + return when.try(this.fs.get.bind(this.fs), command._[1]) .then(fileStat => { return this.reply(213, {message: fileStat.size}); }) @@ -20,4 +20,4 @@ module.exports = { flags: { feat: 'SIZE' } -} +}; diff --git a/src/commands/registration/stat.js b/src/commands/registration/stat.js index ab236c5..4aa1843 100644 --- a/src/commands/registration/stat.js +++ b/src/commands/registration/stat.js @@ -6,15 +6,17 @@ module.exports = { directive: 'STAT', handler: function (args = {}) { const {log, command} = args; - const path = command._[1]; + const path = _.get(command, '_[1]'); if (path) { if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs.get) return this.reply(402, 'Not supported by file system'); - return when(this.fs.get(path)) + return when.try(this.fs.get.bind(this.fs), path) .then(stat => { if (stat.isDirectory()) { - return when(this.fs.list(path)) + if (!this.fs.list) return this.reply(402, 'Not supported by file system'); + + return when.try(this.fs.list.bind(this.fs), path) .then(files => { const fileList = files.map(file => { const message = getFileStat(file, _.get(this, 'server.options.file_format', 'ls')); @@ -22,21 +24,21 @@ module.exports = { raw: true, message }; - }) + }); return this.reply(213, 'Status begin', ...fileList, 'Status end'); - }) + }); } else { - return this.reply(212, getFileStat(stat, _.get(this, 'server.options.file_format', 'ls'))) + return this.reply(212, getFileStat(stat, _.get(this, 'server.options.file_format', 'ls'))); } }) .catch(err => { log.error(err); return this.reply(450); - }) + }); } else { return this.reply(211, 'Status OK'); } }, syntax: '{{cmd}} [path(optional)]', description: 'Returns the current status' -} +}; diff --git a/src/commands/registration/stor.js b/src/commands/registration/stor.js index cc8d910..2575d67 100644 --- a/src/commands/registration/stor.js +++ b/src/commands/registration/stor.js @@ -15,7 +15,7 @@ module.exports = { this.commandSocket.pause(); dataSocket = socket; }) - .then(() => when(this.fs.write(fileName, {append}))) + .then(() => when.try(this.fs.write.bind(this.fs), fileName, {append})) .then(stream => { return when.promise((resolve, reject) => { stream.on('error', err => dataSocket.emit('error', err)); @@ -41,4 +41,4 @@ module.exports = { }, syntax: '{{cmd}} [path]', description: 'Store data as a file at the server site' -} +}; diff --git a/src/commands/registration/stou.js b/src/commands/registration/stou.js index 9582315..6720bb6 100644 --- a/src/commands/registration/stou.js +++ b/src/commands/registration/stou.js @@ -1,3 +1,5 @@ +const when = require('when'); + const stor = require('./stor').handler; module.exports = { @@ -7,9 +9,11 @@ module.exports = { if (!this.fs.get || !this.fs.getUniqueName) return this.reply(402, 'Not supported by file system'); const fileName = args.command._[1]; - return this.fs.get(fileName) - .catch(() => fileName) // does not exist, name is unique - .then(() => this.fs.getUniqueName()) // exists, must create new unique name + return when.try(() => { + return when.try(this.fs.get.bind(this.fs), fileName) + .then(() => when.try(this.fs.getUniqueName.bind(this.fs))) + .catch(() => when.resolve(fileName)); + }) .then(name => { args.command._[1] = name; return stor.call(this, args); diff --git a/src/commands/registration/stru.js b/src/commands/registration/stru.js index 68470c1..cb4484d 100644 --- a/src/commands/registration/stru.js +++ b/src/commands/registration/stru.js @@ -8,4 +8,4 @@ module.exports = { flags: { obsolete: true } -} +}; diff --git a/src/commands/registration/syst.js b/src/commands/registration/syst.js index 1b1e155..5e65d81 100644 --- a/src/commands/registration/syst.js +++ b/src/commands/registration/syst.js @@ -8,4 +8,4 @@ module.exports = { flags: { no_auth: true } -} +}; diff --git a/src/commands/registration/user.js b/src/commands/registration/user.js index b8a2608..b7cb8ff 100644 --- a/src/commands/registration/user.js +++ b/src/commands/registration/user.js @@ -2,7 +2,10 @@ module.exports = { directive: 'USER', handler: function ({log, command} = {}) { if (this.username) return this.reply(530, 'Username already set'); + this.username = command._[1]; + if (!this.username) return this.reply(501, 'Must send username requirement'); + if (this.server.options.anonymous === true) { return this.login(this.username, '@anonymous') .then(() => { @@ -20,4 +23,4 @@ module.exports = { flags: { no_auth: true } -} +}; diff --git a/src/commands/registry.js b/src/commands/registry.js index 12ab572..86f19e3 100644 --- a/src/commands/registry.js +++ b/src/commands/registry.js @@ -1,3 +1,4 @@ +/* eslint no-return-assign: 0 */ const commands = [ require('./registration/abor'), require('./registration/allo'),