fix: wrap fs calls with when, linting

This commit is contained in:
Tyler Stewart
2017-03-27 17:48:08 -06:00
parent 043d9369cc
commit ef6134d91b
33 changed files with 74 additions and 64 deletions

View File

@@ -7,4 +7,4 @@ module.exports = {
}, },
syntax: '{{cmd}} [path]', syntax: '{{cmd}} [path]',
description: 'Append to a file' description: 'Append to a file'
} };

View File

@@ -16,7 +16,7 @@ module.exports = {
flags: { flags: {
no_auth: true no_auth: true
} }
} };
function handleTLS() { function handleTLS() {
return this.reply(504); return this.reply(504);

View File

@@ -2,10 +2,10 @@ const cwd = require('./cwd').handler;
module.exports = { module.exports = {
directive: ['CDUP', 'XCUP'], directive: ['CDUP', 'XCUP'],
handler: function(args) { handler: function (args) {
args.command._ = [args.command._[0], '..']; args.command._ = [args.command._[0], '..'];
return cwd.call(this, args); return cwd.call(this, args);
}, },
syntax: '{{cmd}}', syntax: '{{cmd}}',
description: 'Change to Parent Directory' description: 'Change to Parent Directory'
} };

View File

@@ -7,7 +7,7 @@ module.exports = {
if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.chdir) return this.reply(402, 'Not supported by file system'); 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 => { .then(cwd => {
const path = cwd ? `"${escapePath(cwd)}"` : undefined; const path = cwd ? `"${escapePath(cwd)}"` : undefined;
return this.reply(250, path); return this.reply(250, path);
@@ -19,4 +19,4 @@ module.exports = {
}, },
syntax: '{{cmd}}[path]', syntax: '{{cmd}}[path]',
description: 'Change working directory' description: 'Change working directory'
} };

View File

@@ -6,7 +6,7 @@ module.exports = {
if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.delete) return this.reply(402, 'Not supported by file system'); 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(() => { .then(() => {
return this.reply(250); return this.reply(250);
}) })
@@ -17,4 +17,4 @@ module.exports = {
}, },
syntax: '{{cmd}} [path]', syntax: '{{cmd}} [path]',
description: 'Delete file' description: 'Delete file'
} };

View File

@@ -18,4 +18,4 @@ module.exports = {
flags: { flags: {
no_auth: true no_auth: true
} }
} };

View File

@@ -21,4 +21,4 @@ module.exports = {
flags: { flags: {
no_auth: true no_auth: true
} }
} };

View File

@@ -6,7 +6,7 @@ const getFileStat = require('../../helpers/file-stat');
// http://cr.yp.to/ftp/list/eplf.html // http://cr.yp.to/ftp/list/eplf.html
module.exports = { module.exports = {
directive: 'LIST', 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) return this.reply(550, 'File system not instantiated');
if (!this.fs.list) return this.reply(402, 'Not supported by file system'); if (!this.fs.list) return this.reply(402, 'Not supported by file system');
@@ -19,9 +19,9 @@ module.exports = {
this.commandSocket.pause(); this.commandSocket.pause();
dataSocket = socket; dataSocket = socket;
}) })
.then(() => when(this.fs.list(directory))) .then(() => when.try(this.fs.list.bind(this.fs), directory))
.then(files => { .then(files => {
const getFileMessage = (file) => { const getFileMessage = file => {
if (simple) return file.name; if (simple) return file.name;
return getFileStat(file, _.get(this, 'server.options.file_format', 'ls')); return getFileStat(file, _.get(this, 'server.options.file_format', 'ls'));
}; };
@@ -33,7 +33,7 @@ module.exports = {
message, message,
socket: dataSocket socket: dataSocket
}; };
}) });
return this.reply(150) return this.reply(150)
.then(() => { .then(() => {
if (fileList.length) return this.reply({}, ...fileList); if (fileList.length) return this.reply({}, ...fileList);
@@ -57,4 +57,4 @@ module.exports = {
}, },
syntax: '{{cmd}} [path(optional)]', syntax: '{{cmd}} [path(optional)]',
description: 'Returns information of a file or directory if specified, else information of the current working directory is returned' description: 'Returns information of a file or directory if specified, else information of the current working directory is returned'
} };

View File

@@ -7,10 +7,10 @@ module.exports = {
if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.get) return this.reply(402, 'Not supported by file system'); 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 => { .then(fileStat => {
const modificationTime = format(fileStat.mtime, 'YYYYMMDDHHmmss.SSS'); const modificationTime = format(fileStat.mtime, 'YYYYMMDDHHmmss.SSS');
return this.reply(213, modificationTime) return this.reply(213, modificationTime);
}) })
.catch(err => { .catch(err => {
log.error(err); log.error(err);
@@ -22,4 +22,4 @@ module.exports = {
flags: { flags: {
feat: 'MDTM' feat: 'MDTM'
} }
} };

View File

@@ -7,7 +7,7 @@ module.exports = {
if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.mkdir) return this.reply(402, 'Not supported by file system'); 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 => { .then(dir => {
const path = dir ? `"${escapePath(dir)}"` : undefined; const path = dir ? `"${escapePath(dir)}"` : undefined;
return this.reply(257, path); return this.reply(257, path);
@@ -19,4 +19,4 @@ module.exports = {
}, },
syntax: '{{cmd}}[path]', syntax: '{{cmd}}[path]',
description: 'Make directory' description: 'Make directory'
} };

View File

@@ -8,4 +8,4 @@ module.exports = {
flags: { flags: {
obsolete: true obsolete: true
} }
} };

View File

@@ -7,4 +7,4 @@ module.exports = {
}, },
syntax: '{{cmd}} [path(optional)]', syntax: '{{cmd}} [path(optional)]',
description: 'Returns a list of file names in a specified directory' description: 'Returns a list of file names in a specified directory'
} };

View File

@@ -8,4 +8,4 @@ module.exports = {
flags: { flags: {
no_auth: true no_auth: true
} }
} };

View File

@@ -5,4 +5,4 @@ module.exports = {
}, },
syntax: '{{cmd}}', syntax: '{{cmd}}',
description: 'Select options for a feature' description: 'Select options for a feature'
} };

View File

@@ -24,4 +24,4 @@ module.exports = {
flags: { flags: {
no_auth: true no_auth: true
} }
} };

View File

@@ -2,7 +2,7 @@ const PassiveConnector = require('../../connector/passive');
module.exports = { module.exports = {
directive: 'PASV', directive: 'PASV',
handler: function ({command} = {}) { handler: function () {
this.connector = new PassiveConnector(this); this.connector = new PassiveConnector(this);
return this.connector.setupServer() return this.connector.setupServer()
.then(server => { .then(server => {
@@ -17,4 +17,4 @@ module.exports = {
}, },
syntax: '{{cmd}}', syntax: '{{cmd}}',
description: 'Initiate passive mode' description: 'Initiate passive mode'
} };

View File

@@ -12,10 +12,10 @@ module.exports = {
const port = portBytes[0] * 256 + portBytes[1]; const port = portBytes[0] * 256 + portBytes[1];
return this.connector.setupConnection(ip, port) return this.connector.setupConnection(ip, port)
.then(socket => { .then(() => {
return this.reply(200); return this.reply(200);
}) });
}, },
syntax: '{{cmd}} [x,x,x,x,y,y]', syntax: '{{cmd}} [x,x,x,x,y,y]',
description: 'Specifies an address and port to which the server should connect' description: 'Specifies an address and port to which the server should connect'
} };

View File

@@ -3,11 +3,11 @@ const escapePath = require('../../helpers/escape-path');
module.exports = { module.exports = {
directive: ['PWD', 'XPWD'], directive: ['PWD', 'XPWD'],
handler: function ({log, command} = {}) { handler: function ({log} = {}) {
if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.currentDirectory) return this.reply(402, 'Not supported by file system'); 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 => { .then(cwd => {
const path = cwd ? `"${escapePath(cwd)}"` : undefined; const path = cwd ? `"${escapePath(cwd)}"` : undefined;
return this.reply(257, path); return this.reply(257, path);
@@ -15,8 +15,8 @@ module.exports = {
.catch(err => { .catch(err => {
log.error(err); log.error(err);
return this.reply(550, err.message); return this.reply(550, err.message);
}) });
}, },
syntax: '{{cmd}}', syntax: '{{cmd}}',
description: 'Print current working directory' description: 'Print current working directory'
} };

View File

@@ -8,4 +8,4 @@ module.exports = {
flags: { flags: {
no_auth: true no_auth: true
} }
} };

View File

@@ -12,7 +12,7 @@ module.exports = {
this.commandSocket.pause(); this.commandSocket.pause();
dataSocket = socket; dataSocket = socket;
}) })
.then(() => when(this.fs.read(command._[1]))) .then(() => when.try(this.fs.read.bind(this.fs), command._[1]))
.then(stream => { .then(stream => {
return when.promise((resolve, reject) => { return when.promise((resolve, reject) => {
dataSocket.on('error', err => stream.emit('error', err)); dataSocket.on('error', err => stream.emit('error', err));
@@ -34,8 +34,8 @@ module.exports = {
.finally(() => { .finally(() => {
this.connector.end(); this.connector.end();
this.commandSocket.resume(); this.commandSocket.resume();
}) });
}, },
syntax: '{{cmd}} [path]', syntax: '{{cmd}} [path]',
description: 'Retrieve a copy of the file' description: 'Retrieve a copy of the file'
} };

View File

@@ -7,4 +7,4 @@ module.exports = {
}, },
syntax: '{{cmd}} [path]', syntax: '{{cmd}} [path]',
description: 'Remove a directory' description: 'Remove a directory'
} };

View File

@@ -7,7 +7,7 @@ module.exports = {
if (!this.fs.get) return this.reply(402, 'Not supported by file system'); if (!this.fs.get) return this.reply(402, 'Not supported by file system');
const fileName = command._[1]; const fileName = command._[1];
return when(this.fs.get(fileName)) return when.try(this.fs.get.bind(this.fs), fileName)
.then(() => { .then(() => {
this.renameFrom = fileName; this.renameFrom = fileName;
return this.reply(350); return this.reply(350);

View File

@@ -11,7 +11,7 @@ module.exports = {
const from = this.renameFrom; const from = this.renameFrom;
const to = command._[1]; const to = command._[1];
return when(this.fs.rename(from, to)) return when.try(this.fs.rename.bind(this.fs), from, to)
.then(() => { .then(() => {
return this.reply(250); return this.reply(250);
}) })
@@ -21,8 +21,8 @@ module.exports = {
}) })
.finally(() => { .finally(() => {
delete this.renameFrom; delete this.renameFrom;
}) });
}, },
syntax: '{{cmd}} [name]', syntax: '{{cmd}} [name]',
description: 'Rename to' description: 'Rename to'
} };

View File

@@ -10,5 +10,5 @@ module.exports = function ({log, command} = {}) {
.catch(err => { .catch(err => {
log.error(err); log.error(err);
return this.reply(500); return this.reply(500);
}) });
}; };

View File

@@ -14,10 +14,10 @@ module.exports = {
const subCommand = { const subCommand = {
_: [subverb, ...subparameters], _: [subverb, ...subparameters],
directive: subverb directive: subverb
} };
const handler = registry[subverb].handler.bind(this); const handler = registry[subverb].handler.bind(this);
return when.try(handler, { log: subLog, command: subCommand }); return when.try(handler, { log: subLog, command: subCommand });
}, },
syntax: '{{cmd}} [subVerb] [subParams]', syntax: '{{cmd}} [subVerb] [subParams]',
description: 'Sends site specific commands to remote server' description: 'Sends site specific commands to remote server'
} };

View File

@@ -6,7 +6,7 @@ module.exports = {
if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.get) return this.reply(402, 'Not supported by file system'); 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 => { .then(fileStat => {
return this.reply(213, {message: fileStat.size}); return this.reply(213, {message: fileStat.size});
}) })
@@ -20,4 +20,4 @@ module.exports = {
flags: { flags: {
feat: 'SIZE' feat: 'SIZE'
} }
} };

View File

@@ -6,15 +6,17 @@ module.exports = {
directive: 'STAT', directive: 'STAT',
handler: function (args = {}) { handler: function (args = {}) {
const {log, command} = args; const {log, command} = args;
const path = command._[1]; const path = _.get(command, '_[1]');
if (path) { if (path) {
if (!this.fs) return this.reply(550, 'File system not instantiated'); if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.get) return this.reply(402, 'Not supported by file system'); 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 => { .then(stat => {
if (stat.isDirectory()) { 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 => { .then(files => {
const fileList = files.map(file => { const fileList = files.map(file => {
const message = getFileStat(file, _.get(this, 'server.options.file_format', 'ls')); const message = getFileStat(file, _.get(this, 'server.options.file_format', 'ls'));
@@ -22,21 +24,21 @@ module.exports = {
raw: true, raw: true,
message message
}; };
}) });
return this.reply(213, 'Status begin', ...fileList, 'Status end'); return this.reply(213, 'Status begin', ...fileList, 'Status end');
}) });
} else { } 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 => { .catch(err => {
log.error(err); log.error(err);
return this.reply(450); return this.reply(450);
}) });
} else { } else {
return this.reply(211, 'Status OK'); return this.reply(211, 'Status OK');
} }
}, },
syntax: '{{cmd}} [path(optional)]', syntax: '{{cmd}} [path(optional)]',
description: 'Returns the current status' description: 'Returns the current status'
} };

View File

@@ -15,7 +15,7 @@ module.exports = {
this.commandSocket.pause(); this.commandSocket.pause();
dataSocket = socket; dataSocket = socket;
}) })
.then(() => when(this.fs.write(fileName, {append}))) .then(() => when.try(this.fs.write.bind(this.fs), fileName, {append}))
.then(stream => { .then(stream => {
return when.promise((resolve, reject) => { return when.promise((resolve, reject) => {
stream.on('error', err => dataSocket.emit('error', err)); stream.on('error', err => dataSocket.emit('error', err));
@@ -41,4 +41,4 @@ module.exports = {
}, },
syntax: '{{cmd}} [path]', syntax: '{{cmd}} [path]',
description: 'Store data as a file at the server site' description: 'Store data as a file at the server site'
} };

View File

@@ -1,3 +1,5 @@
const when = require('when');
const stor = require('./stor').handler; const stor = require('./stor').handler;
module.exports = { module.exports = {
@@ -7,9 +9,11 @@ module.exports = {
if (!this.fs.get || !this.fs.getUniqueName) return this.reply(402, 'Not supported by file system'); if (!this.fs.get || !this.fs.getUniqueName) return this.reply(402, 'Not supported by file system');
const fileName = args.command._[1]; const fileName = args.command._[1];
return this.fs.get(fileName) return when.try(() => {
.catch(() => fileName) // does not exist, name is unique return when.try(this.fs.get.bind(this.fs), fileName)
.then(() => this.fs.getUniqueName()) // exists, must create new unique name .then(() => when.try(this.fs.getUniqueName.bind(this.fs)))
.catch(() => when.resolve(fileName));
})
.then(name => { .then(name => {
args.command._[1] = name; args.command._[1] = name;
return stor.call(this, args); return stor.call(this, args);

View File

@@ -8,4 +8,4 @@ module.exports = {
flags: { flags: {
obsolete: true obsolete: true
} }
} };

View File

@@ -8,4 +8,4 @@ module.exports = {
flags: { flags: {
no_auth: true no_auth: true
} }
} };

View File

@@ -2,7 +2,10 @@ module.exports = {
directive: 'USER', directive: 'USER',
handler: function ({log, command} = {}) { handler: function ({log, command} = {}) {
if (this.username) return this.reply(530, 'Username already set'); if (this.username) return this.reply(530, 'Username already set');
this.username = command._[1]; this.username = command._[1];
if (!this.username) return this.reply(501, 'Must send username requirement');
if (this.server.options.anonymous === true) { if (this.server.options.anonymous === true) {
return this.login(this.username, '@anonymous') return this.login(this.username, '@anonymous')
.then(() => { .then(() => {
@@ -20,4 +23,4 @@ module.exports = {
flags: { flags: {
no_auth: true no_auth: true
} }
} };

View File

@@ -1,3 +1,4 @@
/* eslint no-return-assign: 0 */
const commands = [ const commands = [
require('./registration/abor'), require('./registration/abor'),
require('./registration/allo'), require('./registration/allo'),