Skip to content

Commit

Permalink
Merge pull request #4 from stewarttylerr/set-fs-root
Browse files Browse the repository at this point in the history
feat(fs): allow default file system root to be set
  • Loading branch information
trs authored Mar 29, 2017
2 parents 0293752 + 6b0c06e commit 568833e
Show file tree
Hide file tree
Showing 78 changed files with 1,317 additions and 292 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
}
},
"dependencies": {
"bunyan": "^1.8.5",
"date-fns": "^1.28.0",
"bunyan": "^1.8.9",
"date-fns": "^1.28.2",
"lodash": "^4.17.4",
"minimist-string": "^1.0.2",
"uuid": "^3.0.1",
Expand All @@ -71,8 +71,7 @@
"npm-run-all": "4.0.1",
"rimraf": "2.5.4",
"semantic-release": "^6.3.2",
"sinon": "^1.17.7",
"sinon-as-promised": "^4.0.2"
"sinon": "^2.1.0"
},
"engines": {
"node": ">=6.x",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/registration/appe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ module.exports = {
},
syntax: '{{cmd}} [path]',
description: 'Append to a file'
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
flags: {
no_auth: true
}
}
};

function handleTLS() {
return this.reply(504);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/registration/cdup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
};
4 changes: 2 additions & 2 deletions src/commands/registration/cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -19,4 +19,4 @@ module.exports = {
},
syntax: '{{cmd}}[path]',
description: 'Change working directory'
}
};
4 changes: 2 additions & 2 deletions src/commands/registration/dele.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -17,4 +17,4 @@ module.exports = {
},
syntax: '{{cmd}} [path]',
description: 'Delete file'
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/feat.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ module.exports = {
flags: {
no_auth: true
}
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ module.exports = {
flags: {
no_auth: true
}
}
};
10 changes: 5 additions & 5 deletions src/commands/registration/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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'));
};
Expand All @@ -33,7 +33,7 @@ module.exports = {
message,
socket: dataSocket
};
})
});
return this.reply(150)
.then(() => {
if (fileList.length) return this.reply({}, ...fileList);
Expand All @@ -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'
}
};
6 changes: 3 additions & 3 deletions src/commands/registration/mdtm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -22,4 +22,4 @@ module.exports = {
flags: {
feat: 'MDTM'
}
}
};
4 changes: 2 additions & 2 deletions src/commands/registration/mkd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -19,4 +19,4 @@ module.exports = {
},
syntax: '{{cmd}}[path]',
description: 'Make directory'
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ module.exports = {
flags: {
obsolete: true
}
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/nlst.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ module.exports = {
},
syntax: '{{cmd}} [path(optional)]',
description: 'Returns a list of file names in a specified directory'
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ module.exports = {
flags: {
no_auth: true
}
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ module.exports = {
},
syntax: '{{cmd}}',
description: 'Select options for a feature'
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ module.exports = {
flags: {
no_auth: true
}
}
};
4 changes: 2 additions & 2 deletions src/commands/registration/pasv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -17,4 +17,4 @@ module.exports = {
},
syntax: '{{cmd}}',
description: 'Initiate passive mode'
}
};
11 changes: 5 additions & 6 deletions src/commands/registration/port.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
const _ = require('lodash');
const ActiveConnector = require('../../connector/active');

module.exports = {
directive: 'PORT',
handler: function ({command} = {}) {
this.connector = new ActiveConnector(this);
const rawConnection = command._[1].split(',');
const rawConnection = _.get(command, '_[1]', '').split(',');
if (rawConnection.length !== 6) return this.reply(425);

const ip = rawConnection.slice(0, 4).join('.');
const portBytes = rawConnection.slice(4).map(p => parseInt(p));
const port = portBytes[0] * 256 + portBytes[1];

return this.connector.setupConnection(ip, port)
.then(socket => {
return this.reply(200);
})
.then(() => 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'
}
};
8 changes: 4 additions & 4 deletions src/commands/registration/pwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ 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);
})
.catch(err => {
log.error(err);
return this.reply(550, err.message);
})
});
},
syntax: '{{cmd}}',
description: 'Print current working directory'
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/quit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ module.exports = {
flags: {
no_auth: true
}
}
};
6 changes: 3 additions & 3 deletions src/commands/registration/retr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -34,8 +34,8 @@ module.exports = {
.finally(() => {
this.connector.end();
this.commandSocket.resume();
})
});
},
syntax: '{{cmd}} [path]',
description: 'Retrieve a copy of the file'
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/rmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ module.exports = {
},
syntax: '{{cmd}} [path]',
description: 'Remove a directory'
}
};
2 changes: 1 addition & 1 deletion src/commands/registration/rnfr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/registration/rnto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -21,8 +21,8 @@ module.exports = {
})
.finally(() => {
delete this.renameFrom;
})
});
},
syntax: '{{cmd}} [name]',
description: 'Rename to'
}
};
6 changes: 4 additions & 2 deletions src/commands/registration/site/chmod.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const when = require('when');

module.exports = function ({log, command} = {}) {
if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.chmod) return this.reply(402, 'Not supported by file system');

const [, mode, fileName] = command._;
return this.fs.chmod(fileName, parseInt(mode, 8))
return when.try(this.fs.chmod.bind(this.fs), fileName, parseInt(mode, 8))
.then(() => {
return this.reply(200);
})
.catch(err => {
log.error(err);
return this.reply(500);
})
});
};
4 changes: 2 additions & 2 deletions src/commands/registration/site/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
};
4 changes: 2 additions & 2 deletions src/commands/registration/size.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
})
Expand All @@ -20,4 +20,4 @@ module.exports = {
flags: {
feat: 'SIZE'
}
}
};
Loading

0 comments on commit 568833e

Please sign in to comment.