Comparing version 0.1.9 to 0.1.10
185
lib/rai.js
@@ -27,5 +27,5 @@ /** | ||
* <p>Creates instance of RAIServer</p> | ||
* | ||
* | ||
* <p>Options object has the following properties:</p> | ||
* | ||
* | ||
* <ul> | ||
@@ -39,5 +39,5 @@ * <li><b>debug</b> - if set to true print traffic to console</li> | ||
* </ul> | ||
* | ||
* | ||
* <p><b>Events</b></p> | ||
* | ||
* | ||
* <ul> | ||
@@ -47,4 +47,4 @@ * <li><b>'connect'</b> - emitted if a client connects to the server, param | ||
* <li><b>'error'</b> - emitted on error, has an error object as a param</li> | ||
* </ul> | ||
* | ||
* </ul> | ||
* | ||
* @constructor | ||
@@ -55,5 +55,11 @@ * @param {Object} [options] Optional options object | ||
EventEmitter.call(this); | ||
this.options = options || {}; | ||
if (this.options.debug) { | ||
this._logger = this.options.debug === true ? console.log : this.options.debug; | ||
} else { | ||
this._logger = function(){}; | ||
} | ||
this._createServer(); | ||
@@ -65,7 +71,7 @@ } | ||
* <p>Starts listening on selected port</p> | ||
* | ||
* | ||
* @param {Number} port The port to listen | ||
* @param {String} [host] The IP address to listen | ||
* @param {Function} callback The callback function to be run after the server | ||
* is listening, the only param is an error message if the operation failed | ||
* is listening, the only param is an error message if the operation failed | ||
*/ | ||
@@ -79,5 +85,5 @@ RAIServer.prototype.listen = function(port, host, callback){ | ||
this._host = host; | ||
this._connected = false; | ||
if(callback){ | ||
if(callback){ | ||
this._server.on("listening", (function(){ | ||
@@ -87,3 +93,3 @@ this._connected = true; | ||
}).bind(this)); | ||
this._server.on("error", (function(err){ | ||
@@ -95,3 +101,3 @@ if(!this._connected){ | ||
} | ||
this._server.listen(this._port, this._host); | ||
@@ -102,4 +108,4 @@ }; | ||
* <p>Stops the server</p> | ||
* | ||
* @param {Function} callback Is run when the server is closed | ||
* | ||
* @param {Function} callback Is run when the server is closed | ||
*/ | ||
@@ -112,3 +118,3 @@ RAIServer.prototype.end = function(callback){ | ||
/** | ||
* <p>Creates a server with listener callback</p> | ||
* <p>Creates a server with listener callback</p> | ||
*/ | ||
@@ -128,3 +134,3 @@ RAIServer.prototype._createServer = function(){ | ||
* <p>Listens for errors</p> | ||
* | ||
* | ||
* @event | ||
@@ -141,15 +147,13 @@ * @param {Object} err Error object | ||
* <p>Server listener that is run on client connection</p> | ||
* | ||
* | ||
* <p>{@link RAISocket} object instance is created based on the client socket | ||
* and a <code>'connection'</code> event is emitted</p> | ||
* | ||
* @param {Object} socket The socket to the client | ||
* | ||
* @param {Object} socket The socket to the client | ||
*/ | ||
RAIServer.prototype._serverListener = function(socket){ | ||
if(this.options.debug){ | ||
console.log("CONNECTION FROM "+socket.remoteAddress); | ||
} | ||
this._logger("CONNECTION FROM "+socket.remoteAddress); | ||
var handler = new RAISocket(socket, this.options); | ||
socket.on("data", handler._onReceiveData.bind(handler)); | ||
@@ -166,3 +170,3 @@ socket.on("end", handler._onEnd.bind(handler)); | ||
} | ||
this.emit("connect", handler); | ||
@@ -173,8 +177,8 @@ }; | ||
* <p>Creates a instance for interacting with a client (socket)</p> | ||
* | ||
* | ||
* <p>Optional options object is the same that is passed to the parent | ||
* {@link RAIServer} object</p> | ||
* | ||
* | ||
* <p><b>Events</b></p> | ||
* | ||
* | ||
* <ul> | ||
@@ -191,7 +195,7 @@ * <li><b>'command'</b> - emitted if a client sends a command. Gets two | ||
* <li><b>'timeout'</b> - emitted when a timeout occurs. Connection to the | ||
* client is disconnected automatically if disconnectOnTimeout option | ||
* client is disconnected automatically if disconnectOnTimeout option | ||
* is set to true.</l> | ||
* <li><b>'end'</b> - emitted when the client disconnects</l> | ||
* </ul> | ||
* | ||
* | ||
* @constructor | ||
@@ -203,18 +207,24 @@ * @param {Object} socket Socket for the client | ||
EventEmitter.call(this); | ||
this.socket = socket; | ||
this.options = options || {}; | ||
if (this.options.debug) { | ||
this._logger = this.options.debug === true ? console.log : this.options.debug; | ||
} else { | ||
this._logger = function(){}; | ||
} | ||
this.remoteAddress = socket.remoteAddress; | ||
this._dataMode = false; | ||
this._endDataModeSequence = "\r\n.\r\n"; | ||
this._endDataModeSequenceRegEx = /\r\n\.\r\n|^\.\r\n/; | ||
this.secureConnection = !!this.options.secureConnection; | ||
this._destroyed = false; | ||
this._remainder = ""; | ||
this._ignore_data = false; | ||
if(this.options.timeout){ | ||
@@ -229,3 +239,3 @@ socket.setTimeout(this.options.timeout); | ||
* the data</p> | ||
* | ||
* | ||
* @param {String|Buffer} data Data to be sent to the client | ||
@@ -243,7 +253,5 @@ */ | ||
} | ||
if(this.options.debug){ | ||
console.log("OUT: \"" +buffer.toString("utf-8").trim()+"\""); | ||
} | ||
this._logger("OUT: \"" +buffer.toString("utf-8").trim()+"\""); | ||
if(this.socket && this.socket.writable){ | ||
@@ -259,3 +267,3 @@ this.socket.write(buffer); | ||
* commands</p> | ||
* | ||
* | ||
* @param {String} [sequence="."] - optional sequence on separate line for | ||
@@ -275,7 +283,7 @@ * matching the data end | ||
* <p>Instructs the server to upgrade the connection to secure TLS connection</p> | ||
* | ||
* <p>Fires <code>callback</code> on successful connection upgrade if set, | ||
* | ||
* <p>Fires <code>callback</code> on successful connection upgrade if set, | ||
* otherwise emits <code>'tls'</code></p> | ||
* | ||
* @param {Object} [credentials] An object with PEM encoded key and | ||
* | ||
* @param {Object} [credentials] An object with PEM encoded key and | ||
* certificate <code>{key:"---BEGIN...", cert:"---BEGIN..."}</code>, | ||
@@ -291,3 +299,3 @@ * if not set autogenerated values will be used. | ||
} | ||
if(!callback && typeof credentials == "function"){ | ||
@@ -297,26 +305,24 @@ callback = credentials; | ||
} | ||
credentials = credentials || this.options.credentials || defaultCredentials; | ||
credentials = credentials || this.options.credentials || defaultCredentials; | ||
this._ignore_data = true; | ||
var secure_connector = starttls(this.socket, credentials, (function(ssl_socket){ | ||
if(this.options.debug && !ssl_socket.authorized){ | ||
console.log("WARNING: TLS ERROR ("+ssl_socket.authorizationError+")"); | ||
if(!ssl_socket.authorized){ | ||
this._logger("WARNING: TLS ERROR ("+ssl_socket.authorizationError+")"); | ||
} | ||
this._remainder = ""; | ||
this._ignore_data = false; | ||
this.secureConnection = true; | ||
this.socket = ssl_socket; | ||
this.socket.on("data", this._onReceiveData.bind(this)); | ||
this.socket.on("error", this._onError.bind(this)); | ||
if(this.options.debug){ | ||
console.log("TLS CONNECTION STARTED"); | ||
} | ||
this._logger("TLS CONNECTION STARTED"); | ||
if(callback){ | ||
@@ -327,5 +333,5 @@ callback(); | ||
} | ||
}).bind(this)); | ||
secure_connector.on("error", (function(err){ | ||
@@ -346,3 +352,3 @@ this._onError(err); | ||
* mode, transmit the data otherwise send it to <code>_processData</code></p> | ||
* | ||
* | ||
* @event | ||
@@ -356,8 +362,8 @@ * @param {Buffer|String} chunk Data sent by the client | ||
} | ||
var str = typeof chunk=="string"?chunk:chunk.toString("binary"), | ||
dataEndMatch, dataRemainderMatch, data, match; | ||
if(this._dataMode){ | ||
str = this._remainder + str; | ||
@@ -378,5 +384,3 @@ if((dataEndMatch = str.match(/\r\n.*?$/))){ | ||
data = new Buffer(str.substr(0, dataRemainderMatch.index), "binary"); | ||
if(this.options.debug){ | ||
console.log("DATA:", data.toString("utf-8")); | ||
} | ||
this._logger("DATA:", data.toString("utf-8")); | ||
this.emit("data", data); | ||
@@ -406,5 +410,3 @@ } | ||
data = new Buffer(str, "binary"); | ||
if(this.options.debug){ | ||
console.log("DATA:", data.toString("utf-8")); | ||
} | ||
this._logger("DATA:", data.toString("utf-8")); | ||
this.emit("data", data); | ||
@@ -420,17 +422,14 @@ } | ||
/** | ||
* <p>Processed incoming command lines and emits found data as | ||
* <p>Processed incoming command lines and emits found data as | ||
* <code>'command'</code> with the command name as the first param and the rest | ||
* of the data as second (Buffer)</p> | ||
* | ||
* | ||
* @param {String} str Binary string to be processed | ||
*/ | ||
RAISocket.prototype._processData = function(str){ | ||
if(!str.length){ | ||
return; | ||
} | ||
var lines = (this._remainder+str).split("\r\n"), | ||
match, command; | ||
this._remainder = lines.pop(); | ||
for(var i=0, len = lines.length; i<len; i++){ | ||
@@ -443,7 +442,5 @@ if(this._ignore_data){ | ||
if(!this._dataMode){ | ||
if((match = lines[i].match(/\s*[\S]+\s?/))){ | ||
if((match = lines[i].match(/\s*[\S]+\s?/)) || (match = lines[i].match(/^$/))){ | ||
command = (match[0] || "").trim(); | ||
if(this.options.debug){ | ||
console.log("COMMAND:", lines[i]); | ||
} | ||
this._logger("COMMAND:", lines[i]); | ||
this.emit("command", command, new Buffer(lines[i].substr(match.index + match[0].length), "binary")); | ||
@@ -458,7 +455,7 @@ } | ||
} | ||
} | ||
} | ||
}; | ||
/** | ||
* <p>Called when the connection is or is going to be ended</p> | ||
* <p>Called when the connection is or is going to be ended</p> | ||
*/ | ||
@@ -468,3 +465,3 @@ RAISocket.prototype._destroy = function(){ | ||
this._destroyed = true; | ||
this.removeAllListeners(); | ||
@@ -475,3 +472,3 @@ }; | ||
* <p>Called when the connection is ended. Emits <code>'end'</code></p> | ||
* | ||
* | ||
* @event | ||
@@ -487,3 +484,3 @@ */ | ||
* the error object as a parameter.</p> | ||
* | ||
* | ||
* @event | ||
@@ -500,3 +497,3 @@ * @param {Object} err Error object | ||
* <code>'timeout'</code> is emitted.</p> | ||
* | ||
* | ||
* @event | ||
@@ -518,3 +515,3 @@ */ | ||
* <p>Called when the connection is closed</p> | ||
* | ||
* | ||
* @event | ||
@@ -521,0 +518,0 @@ * @param {Boolean} hadError did the connection end because of an error? |
{ | ||
"name": "rai", | ||
"description": "Request-Answer-Interface for generating text based command servers (SMTP, POP etc)", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"author" : "Andris Reinman", | ||
@@ -6,0 +6,0 @@ "maintainers":[ |
Sorry, the diff of this file is not supported yet
57956
1276