Solved

Serverside Javascript

Posted on 2012-03-28
13
201 Views
Last Modified: 2012-10-19
I've been working with javascript for a long time now and one thing I'd always disliked is the inability to be able to hide the code so you can protect your intellectual property when deploying scripts to a customer. The application framework I work within uses serverside javascript as it's scripting language. The framework is a web based application. When certain events occur in the framework such as a record update, insert or delete, I can add additional code into these steps to make other things happen similar to triggers in SQL. However, these additional functions that I can add are scripts written in javascript that run directly on the same server as the framework and as scripts they are easily readable. Obviously if I have created a component I don't necessarily want my customers, or potentially competitors, wanting to be able to see the code and use it. So, I'd like to be able to embed these functions in a code library or DLL. Any suggestions?

Thanks.
0
Comment
Question by:Lee Savidge
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37775916
there are loads of obfuscators available that makes it harder to read your code,  but there is nothing till date that hides your code:-

here is a link:-
http://www.javascriptobfuscator.com/

see these discussions as well:-
http://stackoverflow.com/questions/1660060/how-to-prevent-your-javascript-code-from-being-stolen-copied-and-viewed
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37775928
JavaScript runs on the browser, and you know it.
There are only 2 ways to run JavaScript into the browser:
1. One, is running it like we've all been doing. You can obfuscate it, but if it's readable by the browser it's also readable by the user.
2. The other, is by developing a browser plugin that users will need to install in their browsers.
That's been done before. There used to be these guys from a company named "Futuresplash" that (way back when) wrapped a little javascript animation library in a plugin. That eventually evolved into Flash.

Needless to say, your only hope (door number 2) is kinda cumbersome.
0
 
LVL 25

Author Comment

by:Lee Savidge
ID: 37775931
I know with Javascript the end response is that you can't hide your code. I know there are code obfuscators but you can decode them easily. I'm happy to rewrite the functions in C# and embed them in a DLL so they return the data I need as long as the functionality is hidden. For example many of my functions will return strings after processing, or booleans. These can easily be written in C# as a DLL. So how would I call that DLL from a javascript?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 25

Author Comment

by:Lee Savidge
ID: 37775935
Just so we're clear, the javascript does not run client side. It is serverside javascript. It is never sent to the browser.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37775938
well you can always use AJAX to call a server side page, make your dll part of asp.net http handler and connect to that handler using AJAX. Here is an example:-

http://www.misfitgeek.com/2011/05/calling-web-service-page-methods-with-jquery/
http://brijbhushan.net/2011/05/29/call-httphandler-from-jquery-pass-data-and-retrieve-in-json-format/
0
 
LVL 5

Expert Comment

by:mvdeveloper
ID: 37775984
To be clear - what application framework are you using to support the serverside javascript?
0
 
LVL 25

Author Comment

by:Lee Savidge
ID: 37775987
I think I need to explain more here.

The framework is an ISAPI DLL that lives on the server in IIS. The javascript code exists inside that framework. The javascript code makes calls INTO that ISAPI DLL via it's standard object interface. This javascript is not/does not/will not exist anywhere on the client because it is called by the ISAPI DLL on the server to process data on the server. The page at the point this code runs, does not exist. I cannot do alerts because there is no place to display alerts as there is no page at this point. The code does not interact with the end page or browser. It exists BEFORE the page is built on the server in IIS and runs in IIS's memory space. That is where my code already exists. I would like to move the functions and rewrite them as C#. I can do that already. I can write the DLL. What I can't seem to find out is how to call that DLL that will live on the server from that script.

For example, my code may do this:

var oOrg = FW.GetRec("Org", "IDField=" + IDField);

This returns an object from the object interface of the ISAPI dll. At this point I have full access to that Organisation object because the GetRec function requests an Org object type and the search parameter is the unique ID. The ISAPI dll then searches the database on the same server for that record. I can then play around with my record like this:

oOrg.name = "new name";
oOrg.status = "deleted";
oOrg.save;

This will write the data back through the existing DLL. At this point, the HTTP response has not even started building the page to send back to the browser. That bit is fine and nothing will change there. However I have some code libraries which do things that the standard framework interface does not do. For example:

Date handling and conversions
Basic logic on iterating through multiple objects when the GetRec returns more than one object. The search parameter field doesn't necessarily have to be the ID field. I can do a partial name search ie "NameField like '%" + name + "%'"

This returns an array that I can iterate through. All of my custom functions I would like to have in my DLL. I can already convert the code to C#. That's simple. I'm just not clear on instantiating an object from a class in a DLL on the server.
0
 
LVL 5

Expert Comment

by:mvdeveloper
ID: 37776110
Ok I think I get the picture now.

In a sense, you're asking the wrong question .. javaScript is a scripting language, and so depends on the features provided to it in whatever context it is running. If, for example, you were running JS within Windows Scripting Host (WSH) you could instantiate a COM object and control that (e.g. System.FileScriptingObject for file access) but only because WSH makes that interface available. The JavaScript itself has no native means of doing that.

So the question is really, what features does your specific ISAPI DLL expose to support this?

If the DLL is itself allowing you to expose COM objects to your JavaScript - which seems likely from what you say above - then you can simply create a C# assembly and expose it for COM interop by either
a) going into the Assembly Properties and checking the Make Assembly COM Visible box, or b) by explicitly setting thye ComClass attribute on any class you wish to expose and using the GUIDGEN tool to set the class id and interface id.

But you need to contact whoever developed the ISAPI DLL you are using to find out what that supports. The JavaScript is only automating that.
0
 
LVL 25

Author Comment

by:Lee Savidge
ID: 37776183
The ISAPI dll is not what I want to interact with. I can already do that because the interface is already created for me. I have additional functions which work well as SSJS and I can easily convert to C#. These functions take a string and return a processed string. There are plenty of others. It's just a library of functions that I find useful.

For example, I can make a connection to a database using SSJS with code like this:

var rs = new ActiveXObject("ADODB.Recordset")
var conn = new ActiveXObject("ADODB.Connection")

I can create a COM object myself in C# that contains equivalent functions to what my existing javascript library contains.

My code library contains functions like:

RemoveNonNumeric(inputString)
ProperCase(inputString)
ProcessLimited(inputString)
ProcessQuotes(inputString)

All of them take an input and return a string. This can be done in javascript, or if I were creating a Windows app, I may have a FunctionsLib class that contained a bunch of public functions that did the same as these examples above. If I created the FunctionLib class in C# and made the functions plublic I would call them in a way similar to this:

string sOut = "";
FunctionLib oFL = new FunctionLib();
sOut = oFL.ProcessQuotes(inputString);

If I were to do this from serverside javascript where I had registered the FunctionLib.dll, how would I call that from the javascript on the server?
0
 
LVL 5

Accepted Solution

by:
mvdeveloper earned 250 total points
ID: 37776262
Since your SSJS has ActiveXObject,

var funcLib = new ActiveXObject("myAssembly.myClass");

Once you've exposed your C# class for com.
0
 
LVL 25

Author Comment

by:Lee Savidge
ID: 37826905
I will be testing this sometime this week. Thanks for the suggestions so far.
0
 
LVL 25

Author Comment

by:Lee Savidge
ID: 38007155
Other project work has got in the way of this. I have not yet managed to try this out. When I do I will update the question. Apologies for the delay.
0
 
LVL 25

Author Closing Comment

by:Lee Savidge
ID: 38514935
I got this working in the end. Finally had time to try it.

Many thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question