Serverside Javascript

Posted on 2012-03-28
Medium Priority
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?

Question by:Lee Savidge
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +1
LVL 20

Expert Comment

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:-

see these discussions as well:-

Expert Comment

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.
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?
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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.
LVL 20

Expert Comment

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:-


Expert Comment

ID: 37775984
To be clear - what application framework are you using to support the serverside javascript?
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";

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.

Expert Comment

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.
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:


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?

Accepted Solution

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

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

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

Author Comment

by:Lee Savidge
ID: 37826905
I will be testing this sometime this week. Thanks for the suggestions so far.
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.
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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

770 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