Serverside Javascript

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?

LVL 25
Lee SavidgeAsked:
Who is Participating?
Since your SSJS has ActiveXObject,

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

Once you've exposed your C# class for com.
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:-
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.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Lee SavidgeAuthor Commented:
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?
Lee SavidgeAuthor Commented:
Just so we're clear, the javascript does not run client side. It is serverside javascript. It is never sent to the browser.
well you can always use AJAX to call a server side page, make your dll part of http handler and connect to that handler using AJAX. Here is an example:-
To be clear - what application framework are you using to support the serverside javascript?
Lee SavidgeAuthor Commented:
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: = "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.
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.
Lee SavidgeAuthor Commented:
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?
Lee SavidgeAuthor Commented:
I will be testing this sometime this week. Thanks for the suggestions so far.
Lee SavidgeAuthor Commented:
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.
Lee SavidgeAuthor Commented:
I got this working in the end. Finally had time to try it.

Many thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.