ftpd.js
Version:
A FTP Server made in node.js
133 lines (120 loc) • 2.99 kB
JavaScript
//Imports
var path = require( "path" ),
packageJSON = require( path.join( "..", "..", "package.json" ) ),
Server = require( path.join( "..", "server", "index.js" ) ),
util = require( "util" ),
EventEmitter = require( "events" ).EventEmitter;
/**
*
* @access public
* @constructor
* @param {Object} options
* @since 0.6.0
*/
function ftpdjs( options ) {
this.options = options;
this.servers = [];
EventEmitter.call( this );
}
util.inherits( ftpdjs, EventEmitter );
/**
*
* This function is used to tell ftpdjs to start setting up
*
* @access public
* @since 0.6.0
*
*/
ftpdjs.prototype.setup = function () {
for ( var i = 0; i < this.getOptions().servers.length; i++ ) {
this.setupServer( this.getOptions().servers[ i ] );
}
};
/**
*
* This callback is used when a server have been setup
*
* @callback ftpdjs~didSetupServer
* @param {Server} the server that finished setting up
* @since 0.6.0
*
*/
/**
*
* This function is used to setup a new server from the options provided
*
* @access public
* @param {Object} options the server options
* @param {ftpdjs~didSetupServer} callback the callback to invoke when the server have finished setting up
* @since 0.6.0
*/
ftpdjs.prototype.setupServer = function ( options, callback ) {
var server = new Server( this, options ),
that = this;
server.on( "state", function ( state ) {
that.emit( "server:state", { state : state, server : server } )
} );
server.setup( function () {
that.getServers().push( server );
if ( undefined !== callback ) {
try {
callback( server );
} catch ( e ) {
}
}
} )
};
/**
* @callback ftpdjs~logCallback
* @param {Object} error the error if one occoured
* @since 0.6.0
*/
/**
*
* This function is used to log messages to the attached stream
*
* @access public
* @param {string} message the message to log
* @param {ftpdjs~logCallback} callback the callback to invoke when finished logging or an error occoured. Optional
* @since 0.6.0
*/
ftpdjs.prototype.log = function ( message, callback ) {
if ( typeof(this.getOptions().logger === "function") ) {
try {
this.getOptions().logger.write( message + "\n", "utf8", callback );
} catch ( error ) {
try {
callback( error );
} catch ( e ) {
}
}
}
};
/**
*
* @access public
* @returns {Object} the package.json for ftpd.js
* @since 0.6.0
*/
ftpdjs.prototype.getPackage = function () {
return packageJSON;
};
/**
*
* @access public
* @returns {Object} the options Object for ftpd.js
* @since 0.6.0
*/
ftpdjs.prototype.getOptions = function () {
return this.options;
};
ftpdjs.prototype.getServers = function () {
return this.servers;
};
/**
*
* @access public
* @type {ftpdjs}
* @since 0.6.0
*/
module.exports = ftpdjs;