views:

63

answers:

3

I want to build a small library based on Raphael.JS for work, and none of us particularly like the idea of open sourcing our code. How insane would it be to write all the logic in Python and have it generate hardcoded JS for each "drawing", as a function of the input data? I know this could increase the size of the downloaded JS (especially if it involves unrolling loops), but our code would stay proprietary; no competitor would have the same capability as us the day after we release it.

Are there any other downsides than increased JS size? Can you guys talk me out of doing this?

+2  A: 

This is a standard technique called obsfucation. There are solutions already available, some free:

http://www.google.com/search?q=javascript+obfuscator

Typically they don't introduce another language unnecessarily. Nor do they have to make the code larger, they may make it smaller.

Based on your comment, I detect two orthogonal concerns:

  1. Write your code in Python to avoid having to write JS,
  2. Obsfucate in some way that is better than all the existing JS-to-JS obsfucators.

The first one sounds like a false economy. Invest in learning how to use JS, and it will repay itself, based on the obvious trends in the software industry. There's no harm in learning and using more than one language.

The second one sounds ambitious - good luck. Seriously! If you have time to spend on that problem, who knows? Maybe you'll think of something that none of the other people working on it have thought of. It does happen sometimes.

You want to solve the same problem that obsfucation attempts to solve - to make your code executable but not understandable. Compilation from one language to another sometimes solves this problem as a byproduct of going from high-level to low-level representation.

However, Python and JS are both very high-level languages. You do not need to use a compiler to raise the level of the target language. JS is high-level enough already. So this is why I keep saying that you need obsfucation, even if you have an idea that you could get it by implementing compilation.

It is probably possible (maybe easiest) to write a compiler from Python to JS that does not perform much obsfucation, so the end result happens to be directly reusable and understandable. To get the side-effect of obsfucation that occurs naturally in (say) C-to-assembler compilation, you would need to target JS as if it was a low-level virtual machine, avoiding high-level constructs. This might slow down your program to a crawl, as effectively the obsfucation would need to be expensively reversed at runtime. So you will probably need to learn from the techniques used in obsfucation to ensure you get good results.

But another concern I glossed over is: you want to stay ahead of your competitors. If you devoted your time to solving the obsfucation problem (by whatever method), while your competitors concentrate on writing great interactive widgets in JS with Raphael, I know who I'd bet my money on to stay ahead.

Daniel Earwicker
Daniel beat me to the "Enter" key on that one. +1 for his answer
InSane
Our site is a Django app, so plenty of Python's getting run already.And obfuscation doesn't really have the same effect (I should remove the obfuscation tag). Someone can still copy an obfuscated JS library and determine its API with minimal experimentation. No?
rfrankel
"Write your code in Python to avoid having to write JS,"Not really my concern (there's always CoffeeScript :) ). Rather, if I write Python that generates hardcoded JS, I'm not writing a JS library that my competitors can grab. I don't really care about obfuscation.
rfrankel
@rfrankel - see update on relationship between compilation and obsfucation. You're trying to use compilation to get obsfucation.
Daniel Earwicker
@Daniel: Are you sure you aren't making the code generation problem out to be harder than it is? There's not that much difference between `r.rect(x, y, w, h);` and `js += 'r.rect(' + ','.join([str(k) for k in [x, y, w, h]]) + ');\n'`. I'm not suggesting compiling arbitrary Python to JS.
rfrankel
I'm lost... If there's not much difference, why do it? Presumably to be effective, you need to generate JS that is *very* different from the hand-written equivalent. And so you will probably have bugs where your compilation process inadvertently causes differences in behaviour (even expensive obsfucation tools have that problem).
Daniel Earwicker
Sorry if I'm not explaining this well. The reason to do it is that if I write a JS library, anyone can grab the library and generate their own drawings. If I write a Python library, people can see our generated JS for a specific drawing, but they can't generate their own.
rfrankel
+2  A: 

If you're unrolling loops, inlining function calls, etc, you might also come up with the problem of increased parsing time for the JS code. Also, it will be way harder to debug.

If it is an option, I would go with server-side drawing of the images, Flash, Silverlight or something else of the kind.

Boris Chervenkov
+1 for server-side generation instead obfuscating client code.
Amadan
I settled on Raphael after quite a lot of thought...we want something cross-platform that doesn't rely on third-party plugins, and we're also interested in having interaction (which pretty much rules out images).That said, you're right about the increased parsing time (and likely the debugging as well, though I'm a little less concerned about that).
rfrankel
A: 

You can never fully hide your client side code. If the browser can get to it human eyes can pry.

You can try things like this. I have never used it but it was a quick Google search away. There are bound to be more out there.

John