January 2008 Archives

I'm currently experimenting with some stuff related to using JavaScript on the server and the client. The results I'm getting are somewhat mixed. The idea I had was to see about letting someone customize some aspects of an application I'm developing that is intended to help you live more healthy. Basically, based upon the statistics of a food or other parts of the application, you could create custom metrics to measure your success or failure according to your particular preferences. (Some people monitor carbs, some monitor Weight Watcher points, others are interested in fat and protein, etc.)

I've successfully added tools that will allow you to add your custom metrics to the system using your own JavaScript snippet. This snippet is passed a object representing the food item (and eventually to include other information). I can then calculate your metric based upon the current food your viewing and your code.

This calculation is performed on the server side using Mozilla Spidermonkey
and the JavaScript
module on CPAN. On the server, this ends up looking something like this:

use JavaScript;
my $runtime = JavaScript::Runtime->new;
my $context = $runtime->create_context;
$context->eval_file('benchmark.js'); # base API library

# do some other setup of the objects I need in __tmp.food

$context->eval(
q/function evalIndicator(food) {/
.$user_calculation # this variable contains code from the end-user
.q/}/
);
$result = $context->eval(q/evalIndicator(__tmp.food)/);

I've added a little more in there for error handling and such, but that's the core of how I handle the work on the server. The really nice thing here is that the user's JavaScript can't really do much bad other than a denial of service by looping or recursing internally. The server-side JavaScript doesn't include any BOM objects that could be used to do terrible things like steal user information, etc. Furthermore, if I run this code as a separate process that communicates back to the server, I can easily wipe it out if it takes longer than a few seconds to work.

Then, using a combination of Dean Edward's sandbox
and some custom hacking of my own, I can then provide on-the-fly updates from the client using most of the same JavaScript code. This allows the JavaScript to run in a relatively safe container within a hidden IFRAME.

On the client, I've implemented the sandbox in this form. I've left out some of the error handling and other details for simplicity:

// Dean Edward's sandbox in OO form:
function Sandbox(preloads) {
  this.iframe = document.createElement("iframe");
  this.iframe.style.display = "none";
  document.body.appendChild(this.iframe);

this.frame = frames{eval:function(s){return eval(s)}};"+
"top=opener=parent=null"+ // cut off access back to the parent
"<\/script>"
);
this.frame.document.close(); // stop the throbbing
}

Sandbox.prototype.eval = function(code) {
var sandboxObject = Sandbox.functions[this.serialNumber

;
return sandboxObject.eval(code);
}

Sandbox.prototype.close = function() {
document.body.removeChild(this.iframe);
Sandbox.functions[this.serialNumber] = null;

this.frame = null;
this.iframe = null;
this.serialNumber = null;
}

Sandbox.serialNumber = 0;
Sandbox.functions = new Array();

Now that I have the sandbox to run things in, I need to setup a similar environment as I use on the server. My initial implementation embeds the user's code as a special attribute in the middle of the page, but it could be special block in the header or an Ajax request once to fetch the code string into a variable just after load, etc.

// This code runs onload, to make sure benchmark.js is loaded completely
var sandbox = new Sandbox(['benchmark.js']); // base API library

// When the user changes a value inthe page, the calculation can be made again on the fly...
var result = sandbox.eval(
""

// do some other setup of the objects I need in __tmp.food

+ "function evalIndicator(food) {"
+ user_calculation // this variable contains code from the end-user
+ "}"
+ "evalIndicator(__tmp.food)"
);

Again, I've omitted some of the other details to keep this article at a manageable length. That's the gist of how I run this on the client.

Now, after I've done the work, I probably am going to drop the client-side effort and provide updates by contacting the server using Ajax-ish requests. Why? Well, I can control the server-side environment much more closely by running the code in a separate process that I can kill off if it runs away. If someone's code accidentally (or intentional) tries to do something malicious on the client, I get the blame. The browser is a much less controllable environment. They could fire off requests passing information regarding the end user off to an unknown third party or compromise something even worse if I'm not very careful. I'd have to do a very thorough job of stripping the sandbox area completely. Even then, I don't know if I can actually protect the user from a malicious attacker since there are so many unknowns regarding browser implementations. I'd need to do a lot more studying before I'd be willing to run untrusted code this way for sure.

On the other hand, this trick might work swimmingly in an environment where I trusted all the code being executed in both boxes. I'd certainly be willing to do this on a project at some point to provide calculations on both server and client using the same embedded code, which is why I'm writing the article for others. Anyway, this has been an interesting adventure in JavaScript.

Cheers.

If you ask my wife when I work, her answer would almost certainly be, "All the time." She means a couple different things by that. One, she doesn't really like it when I work away from the house, which I do when I work in my cube three days a week. Bee, she means that I work in the shower, while eating breakfast, when I'm playing with my son, when I'm relaxing in the evening, always. I eat, sleep, and breathe work. This is what I am calling, "Stream of Consciousness Work Style."

Round Peg, Cubicle Hole

The first aspect of what Terri, my wife, has described is a bad thing. When I work in the office, I'm conformed to work from my cubicle. There's no creative workspace in our finely decorated offices at Boomer Consulting. That's not a knock against the company, but a sign of the company's origin, as a department of a CPA firm. CPAs are more creative than they'll admit, but they don't always need creative workspaces. They need orderly workspaces. They need workspaces that impress clients. Boomer has posh looking, orderly workspaces.

Not only am I constrained to my cubicle, I'm constrained in my work hours. I am expected to work from 8 to 5 or 9 to 5 or 9 to 6 or 7 to 4 or something. There's something in the Human Capital arena called "flex time" which is neither flexible nor timely, in my estimation, but for a well-ordered firm with top-down management, it's the rule. You can work flexible hours (i.e., not the typical 8 to 5), but those hours should be set in a regular schedule and your coworkers/supervisors should be aware of deviations in advance, etc. For a guy who found college hours to his liking (i.e., whenever as long as you and your project team got the job done), this is hard for me. I try, but I'm very bad at it. I try not to resent it too much. I try to conform, but I seem to fail regularly.

Finally, I'm nearly always in the middle of something when five o'clock rolls around. This is because I usually finish a task around 4:15, 4:30, or 4:45 and then it's not yet 5:00. I could leave before 5pm, but appearances are what they are and where would I draw the line if not 5pm? Thus, I pick up something else I hope won't take long, if I can. That generally pushes me to 5:10, 5:30, or 6:00 before I'm done. Terri no likey. Me no likey. Fortunately, my drive home takes 12 minutes, but I still don't like working late at the office. When five rolls around, I must make one of two choices: (1) drop what I'm doing in the middle and risk the mess that leaves things in or (2) complete the task and get home after dark. I despise both of those options, but I choose one or the other regularly depending on how long I estimate the task will take or how troublesome dropping it in the middle will be.

That's the negative aspect of "working all the time." It's a bummer, but that's life and while I work to slowly move my life away from work schedules that interfere with my work process, I deal with it in the meantime. This traditional office life is traditional for useful reasons as well. It is not without value. I'm not really knocking it. I am basically saying that I, as a round peg, find the cubical hole an uncomfortable fit.

Stream of Consciousness

The other aspect of "working all the time" revolves around my creative thought process. An article I recently read from A List Apart discusses the difference between the hat heads and the bed heads
. I definitely fall into the latter category. The bad news is that I'm difficult to manage, tend to work on my own, and don't necessarily communicate clearly all the time. The good news is that I'm a creator and I get stuff done.

First, I am always thinking about something. My brain almost never shuts off. The only time it really shuts down is when I read a book right before bed. This is a habit I've cultivated to avoid insomnia. The goal of the book, even if it is intellectual or stimulating is to put my brain into the right mode to sleep. It takes about an hour for my motor to get up to speed in the morning, but after that, I'm again full on working. I begin thinking about work before I step into the shower, while I eat breakfast, when I drive to work (or walk upstairs to my home office), while I'm officially working, while I eat lunch, etc. If I'm not in front of a computer, white board, or tablet of graph paper doing what looks like work, I'm still working somewhere in the back of my mind.

Next, I'm not always working on work. I have several hobbies, most of which look like work, but aren't what I get paid to do. For example, I keep a blog, which feels the same as work since I write articles and blogs for work. I build web sites, contribute to Open Source projects, and do other computer science stuff, which feels like work, but might be projects I like on my own time rather than stuff Boomer pays me for. I study theology and my Bible, which is really not much different than studying whatever latest technology I need to know to get my job done.

As a really excellent article, The Nerd Handbook
, points out I see everything and everyone as a project. That's not far from the truth. Even wrestling with my son is something I see as a task to be completed. That doesn't at all diminish the preciousness of the experience, but after I put Gabe to bed, a little check box gets ticked off in my head next to, "Had a significant amount of quality time with the Goober Pants." (At least, it usually gets marked, sometimes I'm not satisfied and that box takes on a larger font in the tag cloud floating in my brain for tomorrow night.) The same goes for time with my wife or playing video games or any other form of fun and recreation. It all falls into the same category as work.

The interesting thing about this way of living is that work is no longer work, but it's life. I don't take a vacation from work to get away from work, but so that I don't have to structure my time to fit the needs of my coworkers. We have this mystical thing we talk about at Boomer called the "Work-Life Balance." I say "mystical," but I should say "mystifying to me" because work and life are synonyms as far as I am concerned. I will work until I die. I will live until I retire. I could say I'm a workaholic because I'm never "off", but that wouldn't really be accurate. That would be missing the point. I'm never off because I never need to be because sometimes my on-time is the same as my off-time. That really means that my vacation days are merely on-time where my time is structured according to my choice even more so than usual.

Another Boomer doctrine is that of Free Days versus Buffer Days versus Focus Days. Everyone should take Free Days (like weekends) to recharge, Buffer Days to get the crap work done, and Focus Days to get the really cool stuff done. My life doesn't work that way. That's just another set of boxes to constrain my work style. I see every day as a Freefercus Day. Part of my day is spent working on my job doing fun stuff, not fun stuff, and whatever other stuff needs to happen to keep everything moving towards my goals. I spend another part of my day working on eating while I think about my job or fun projects. I spend another part of my day focused on my family. I spend another part of my day working on paying the bills or taking out the trash. I spend another part of my day dinking around on a fun work and/or personal project, etc. All of this blends together in my brain without strict delineation.

If I really had my way, I would give myself over to this work style. I wouldn't constrain myself to a certain set of hours. I wouldn't put myself into a cubicle box. I wouldn't announce my work hours ahead of time (mostly). In order to satisfy my team members, I could seek a compromise position of having regular meetings or daily office hours or something, but even that would be part of this unstructured system of work/life I lead. The structure of my life/work would center around getting stuff done rather than following some pre-programmed formula of time management.

For the most part, I would work day or night on my job whenever it best fit to do so. I would spend time with my wife or work on fun projects or play with my son whenever it fit, even if it happened in the middle of the day. I would work in an environment that supported my creativity rather than constrained it. I would balance the work that recharges me (fun projects, hard tasks, family time, etc.) with the work that drains me (paying taxes, data entry, meetings, etc.) The nature of the structure of the time would morph over time as well.

What works to keep me organized this month rarely works six months from now. My life would be systematically unstructured, evolving from period to period depending on my current projects, team and family needs, and other factors in my life at the moment. This is what I call Stream of Consciousness Work Style. You can like my idea of work and life or not, but that's how I roll.

Cheers.

About this Archive

This page is an archive of entries from January 2008 listed from newest to oldest.

November 2007 is the previous archive.

April 2008 is the next archive.

Find recent content on the main index or look in the archives to find all content.