From 9ef1867cd0236c5a0e199b8c1483ccba0dfec1f8 Mon Sep 17 00:00:00 2001 From: Devin Weaver Date: Fri, 12 Jul 2013 11:45:49 -0400 Subject: [PATCH 1/4] Fixes unnecessary semi-colons --- src/logger.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/logger.js b/src/logger.js index 25a1792..46108bc 100644 --- a/src/logger.js +++ b/src/logger.js @@ -19,7 +19,7 @@ Logger.prototype.connect = function() { eval('(function(){' + msg.data + '})()' ); } catch(ex) { Ti.API.debug(ex); - }; + } }); this.socket.addEventListener('readError', function(){ Ti.API.debug('Error Reading from Logging Server'); @@ -35,7 +35,7 @@ Logger.prototype.connect = function() { }; Logger.prototype.ensureConnection = function() { - if(this.socket.isValid) {return; }; + if (this.socket.isValid) { return; } this.connected = false; var self = this; var attempts = 0; @@ -45,18 +45,18 @@ Logger.prototype.ensureConnection = function() { if(attempts > 3) { clearInterval(checkSocketConnected); Ti.API.debug('Giving up trying to connect to Logging server'); - }; + } if(self.connected) { clearInterval(checkSocketConnected); self.log('==========================================='); self.log('Device ' + Titanium.Platform.macaddress + ' connected (' + String.formatDate( new Date(), 'medium') + ')'); for(var i = 0, len = self._msgs.length; i < len; i++ ) { self.log(self._msgs[i]); - }; + } self._msgs = []; } else { self.socket.connect(); // attempt to connect - }; + } }, 10000); }; /* @@ -64,17 +64,17 @@ Logger.prototype.ensureConnection = function() { If the socket is not ready, queue up the messages to an array that will be sent when there's a good connection */ Logger.prototype.log = function(msg) { - if(msg === null) { msg = ''; }; // make sure it doesn't bomb out on null objects + if (msg === null) { msg = ''; } // make sure it doesn't bomb out on null objects try { // this.ensureConnection(); if(this.connected) { this.socket.write(JSON.stringify(msg)); } else { this._msgs.push(msg); // queue up the msg - }; + } } catch (ex) { Ti.API.debug(ex); - }; + } }; @@ -88,6 +88,6 @@ exports.logger = function(host, post) { } catch (ex) { alert( 'Connection Error' ); Ti.API.debug(ex); - }; + } return logger; -}; \ No newline at end of file +}; From d64bc39d6b63d6d9501039deefb1cc988ac40ae2 Mon Sep 17 00:00:00 2001 From: Devin Weaver Date: Fri, 12 Jul 2013 11:55:03 -0400 Subject: [PATCH 2/4] Adds a better Ti.API.debug log function --- src/logger.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/logger.js b/src/logger.js index 46108bc..267912b 100644 --- a/src/logger.js +++ b/src/logger.js @@ -1,4 +1,9 @@ /* Logger class to take care of the logging and conneting to the server.*/ +/* jshint eqnull:true */ +function debug(message, method) { + Ti.API.debug("[Logger" + (method != null ? "." + method : "") + "] " + message); +} + var Logger = function(host,port) { this.host = host; this.port = port; @@ -18,16 +23,17 @@ Logger.prototype.connect = function() { try { eval('(function(){' + msg.data + '})()' ); } catch(ex) { - Ti.API.debug(ex); + self.log(ex); + debug(ex, "connect"); } }); this.socket.addEventListener('readError', function(){ - Ti.API.debug('Error Reading from Logging Server'); + debug('Error Reading from Logging Server', "connect"); self.connected = false; // self.ensureConnection(); }); this.socket.addEventListener('writeError', function() { - Ti.API.debug('Error Writing to Logging Server'); + debug('Error Writing to Logging Server', "connect"); self.connected = false; // self.ensureConnection(); }); @@ -44,7 +50,7 @@ Logger.prototype.ensureConnection = function() { attempts++; if(attempts > 3) { clearInterval(checkSocketConnected); - Ti.API.debug('Giving up trying to connect to Logging server'); + debug('Giving up trying to connect to Logging server', "ensureConnection"); } if(self.connected) { clearInterval(checkSocketConnected); @@ -64,7 +70,7 @@ Logger.prototype.ensureConnection = function() { If the socket is not ready, queue up the messages to an array that will be sent when there's a good connection */ Logger.prototype.log = function(msg) { - if (msg === null) { msg = ''; } // make sure it doesn't bomb out on null objects + if (msg == null) { return; } // make sure it doesn't bomb out on null objects try { // this.ensureConnection(); if(this.connected) { @@ -73,8 +79,9 @@ Logger.prototype.log = function(msg) { this._msgs.push(msg); // queue up the msg } } catch (ex) { - Ti.API.debug(ex); + debug(ex, "log"); } + debug(msg, "log"); // Redundent data is ok. }; @@ -87,7 +94,7 @@ exports.logger = function(host, post) { logger.connect(); } catch (ex) { alert( 'Connection Error' ); - Ti.API.debug(ex); + debug(ex); } return logger; }; From 98fa639b847e4dcc14863ec5714f043b2a721754 Mon Sep 17 00:00:00 2001 From: Devin Weaver Date: Fri, 12 Jul 2013 11:55:12 -0400 Subject: [PATCH 3/4] Adds a persistent Sandbox for evaluating code Uses a custom sandbox object to attach a context for any code being executed. This sandbox will store a JS context between eval executions. It prevent global name space pollution. Adds ability to send non JavaScript commands such as `/reset` (Only command supported so far). Reset command will create a new sandbox environment and garbage collect the last one. --- src/logger.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/logger.js b/src/logger.js index 267912b..faa08b6 100644 --- a/src/logger.js +++ b/src/logger.js @@ -4,12 +4,51 @@ function debug(message, method) { Ti.API.debug("[Logger" + (method != null ? "." + method : "") + "] " + message); } +var Sandbox = function(logger) { + // Normally saving a reference to logger whould no be a problem. However we + // use "this" as the context to execute the eval. So we want to hide logger + // and only expose the log function. Because of this we use a closure to + // hide the logger object but still offer a log function. + this.log = function() { return logger.log.apply(logger, arguments); }; + this.reset = function() { return logger.reset.apply(logger, arguments); }; +}; + +Sandbox.exec = function(code, sandbox) { + /* jshint evil:true, boss:true */ + var result; + if (result = code.match(/^\s*\/([A-za-z]+)(?:\s+(.+))?$/)) { + return Sandbox.command(sandbox, result[1].toLowerCase(), result[2]); + } + else { + return eval("(function(exports){" + code + "}).call(sandbox,sandbox)"); + } +}; + +Sandbox.command = function(/* sandbox, command, options... */) { + var + sandbox = arguments[0], + command = arguments[1], + options = 3 <= arguments.length ? [].slice.call(arguments, 2) : []; + switch (command) { + case "reset": + sandbox.reset(); + break; + default: + throw "Unknown command: " + command; + } +}; + var Logger = function(host,port) { this.host = host; this.port = port; this.socket = null; this.connected = false; this._msgs = []; // just to queued up the messages + this.reset(); +}; + +Logger.prototype.reset = function() { + this.sandbox = new Sandbox(this); }; Logger.prototype.connect = function() { @@ -21,7 +60,7 @@ Logger.prototype.connect = function() { }); this.socket.addEventListener('read', function(msg) { try { - eval('(function(){' + msg.data + '})()' ); + self.log(Sandbox.exec(msg.data,self.sandbox)); } catch(ex) { self.log(ex); debug(ex, "connect"); From cf7d271303458be06f65e24dbf07ce5a64b8ec12 Mon Sep 17 00:00:00 2001 From: Devin Weaver Date: Fri, 12 Jul 2013 12:01:24 -0400 Subject: [PATCH 4/4] Updates dist directory This should probubly NOT be in the repo. Shouldn't we gitignore these build files? --- dist/logger.js | 80 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/dist/logger.js b/dist/logger.js index 25a1792..faa08b6 100644 --- a/dist/logger.js +++ b/dist/logger.js @@ -1,10 +1,54 @@ /* Logger class to take care of the logging and conneting to the server.*/ +/* jshint eqnull:true */ +function debug(message, method) { + Ti.API.debug("[Logger" + (method != null ? "." + method : "") + "] " + message); +} + +var Sandbox = function(logger) { + // Normally saving a reference to logger whould no be a problem. However we + // use "this" as the context to execute the eval. So we want to hide logger + // and only expose the log function. Because of this we use a closure to + // hide the logger object but still offer a log function. + this.log = function() { return logger.log.apply(logger, arguments); }; + this.reset = function() { return logger.reset.apply(logger, arguments); }; +}; + +Sandbox.exec = function(code, sandbox) { + /* jshint evil:true, boss:true */ + var result; + if (result = code.match(/^\s*\/([A-za-z]+)(?:\s+(.+))?$/)) { + return Sandbox.command(sandbox, result[1].toLowerCase(), result[2]); + } + else { + return eval("(function(exports){" + code + "}).call(sandbox,sandbox)"); + } +}; + +Sandbox.command = function(/* sandbox, command, options... */) { + var + sandbox = arguments[0], + command = arguments[1], + options = 3 <= arguments.length ? [].slice.call(arguments, 2) : []; + switch (command) { + case "reset": + sandbox.reset(); + break; + default: + throw "Unknown command: " + command; + } +}; + var Logger = function(host,port) { this.host = host; this.port = port; this.socket = null; this.connected = false; this._msgs = []; // just to queued up the messages + this.reset(); +}; + +Logger.prototype.reset = function() { + this.sandbox = new Sandbox(this); }; Logger.prototype.connect = function() { @@ -16,18 +60,19 @@ Logger.prototype.connect = function() { }); this.socket.addEventListener('read', function(msg) { try { - eval('(function(){' + msg.data + '})()' ); + self.log(Sandbox.exec(msg.data,self.sandbox)); } catch(ex) { - Ti.API.debug(ex); - }; + self.log(ex); + debug(ex, "connect"); + } }); this.socket.addEventListener('readError', function(){ - Ti.API.debug('Error Reading from Logging Server'); + debug('Error Reading from Logging Server', "connect"); self.connected = false; // self.ensureConnection(); }); this.socket.addEventListener('writeError', function() { - Ti.API.debug('Error Writing to Logging Server'); + debug('Error Writing to Logging Server', "connect"); self.connected = false; // self.ensureConnection(); }); @@ -35,7 +80,7 @@ Logger.prototype.connect = function() { }; Logger.prototype.ensureConnection = function() { - if(this.socket.isValid) {return; }; + if (this.socket.isValid) { return; } this.connected = false; var self = this; var attempts = 0; @@ -44,19 +89,19 @@ Logger.prototype.ensureConnection = function() { attempts++; if(attempts > 3) { clearInterval(checkSocketConnected); - Ti.API.debug('Giving up trying to connect to Logging server'); - }; + debug('Giving up trying to connect to Logging server', "ensureConnection"); + } if(self.connected) { clearInterval(checkSocketConnected); self.log('==========================================='); self.log('Device ' + Titanium.Platform.macaddress + ' connected (' + String.formatDate( new Date(), 'medium') + ')'); for(var i = 0, len = self._msgs.length; i < len; i++ ) { self.log(self._msgs[i]); - }; + } self._msgs = []; } else { self.socket.connect(); // attempt to connect - }; + } }, 10000); }; /* @@ -64,17 +109,18 @@ Logger.prototype.ensureConnection = function() { If the socket is not ready, queue up the messages to an array that will be sent when there's a good connection */ Logger.prototype.log = function(msg) { - if(msg === null) { msg = ''; }; // make sure it doesn't bomb out on null objects + if (msg == null) { return; } // make sure it doesn't bomb out on null objects try { // this.ensureConnection(); if(this.connected) { this.socket.write(JSON.stringify(msg)); } else { this._msgs.push(msg); // queue up the msg - }; + } } catch (ex) { - Ti.API.debug(ex); - }; + debug(ex, "log"); + } + debug(msg, "log"); // Redundent data is ok. }; @@ -87,7 +133,7 @@ exports.logger = function(host, post) { logger.connect(); } catch (ex) { alert( 'Connection Error' ); - Ti.API.debug(ex); - }; + debug(ex); + } return logger; -}; \ No newline at end of file +};