Jump to content
Sign in to follow this  
DaveK

NodeJS and HTTPS

Recommended Posts

I am new to smart pascal (2 days experience so far)  - but I really like what I've seen so far - and I like that I could use the same language on both client and server.

I need to create an https server. I've seen the hello world http server that works with NodeJS and I've been looking for an https equivalent but found none so far. It looks like I could descend one from TNJCustomServer - which is going to be quite a learning curve for me. So first I would like to ask nicely - has anyone already done this?

Share this post


Link to post
Share on other sites

Hi Dave!

Im actually doing this now. Under node, http and https are implemented as separate modules - which are identical.
So you are right that you should inherit from TNJCustomServer, but I think you can just copy the whole http-server unit and then make sure it uses https instead.

Here are the docs: https://nodejs.org/api/https.html

You just have to do a require("https") and import the module.
Then define a "class external" for it. If you look at how we import the other modules it should be easy to understand.

But - if you can wait for the update Ill try my best to have it ready for you. We were going to wait until the next update (not the one lined up now), but ill see if i can squeeze it into our build for you

/Jon

Share this post


Link to post
Share on other sites

Hi Lennart,

Wow - yes please!

I can't believe I've only just come across Smart Pascal. I've thought for a long time that as a language Object Pascal deserves universality. You guys are proving that that's possible - bringing clarity to the web!

There's a reason that Wirth derived languages are used in safety critical engineering - you guessed it - clarity (structured text in safety PLCs, Ada in military avionics systems). Because that's what engineers with big responsibilities need!

So I for one believe you guys are going in the right direction. 

Share this post


Link to post
Share on other sites

Thank you for the warm praise!

Its taken a long time to make this and the challenges has been many, but like you I firmly believe that object pascal is a better language.
I even wrote a kernel in freepascal which says something about the power object pascal has.
So I am 100% onboard with your notes on Wirth, a brilliant man and scientist.

Great to have you onboard as well, happy hacking buddy!

Share this post


Link to post
Share on other sites

Since you've answered my question I wonder if I can ask an off topic question - out of interest.

I've read that the compiler compiles from Smart Pascal to human readable JavaScript. But I have also read that it recreates all the OOP capabilities of Smart Pascal by creating VMTs directly. Since JavaScript works by sending source code to browsers - and then the browser either interprets or compiles the JavaScript, surely the VMTs are created at that point - by the browser. So where is Smart Pascals opportunity to define the VMT? 

I'm sure this is because I know not a lot about JavaScript and I'm missing some information on what can and can't be done... It must be that Smart Pascal is actually producing something at a lower level than I regard as human readable JavaScript - and the browser also understands this lower level form. Is there something equivalent to assembler in JavaScript???

Share this post


Link to post
Share on other sites

Javascript doesnt use a VMT, it is based on prototypes which works differently (object in javascript is more like a b-tree node, that in turn can contain any number of sub nodes or name/value pairs).
Our model comes on the side of this, and we bypass all the cloning JS developers have to deal with.
If you look at the compiled code, you will see that all methods has a hidden "self" parameter. A VMT means that the data of the object and the methods are decoupled, and that the compiler operates with a method-table. Normally in a binary executable, the VMT is maintained at runtime. But since JavaScript is a written, source based language - the VMT is used at compile time to link up the symbols.

The reason this is powerful is because it allows inheritance and re-use of existing methods without copying.
JavaScript forces you to clone everything (prototypes) which limits how inheritance is dealt with by JS coders (not too elaborate, usually ad-hoc "clone my ancestor" and thats it).
Our model dont need cloning. Since we ship the instance data in as the "self" param, we are able to re-use existing methods directly.
In our model only the instance data is created and passed around, not the methods and everything else.

Had this been a compiled language then yes, then the VMT would be implemented inside the code as part of TObject (actually, just beneath the surface of TObject).
But since JS is text based we link up symbols during the compilation.
You can watch how this works if you run a program in the browser and set some breakpoints, then inspect the "self" parameter directly:
You could say that the JS instance both is a VMT and is sculpted from a VMT. But the sculpting is naturally applied in JS. TObject doesnt magically shape itself so to speak.

Share this post


Link to post
Share on other sites

I see - fascinating! And thank you for a detailed and well thought out answer.

I have an interest in such things because I myself have written a Pascal like compiler - from scratch. Well, it will eventually be a compiler when it is capable of actually generating object code. At present it only reads in a program and creates a tree out of objects. The constructor of each object reads in the program and creates sub objects that mirror the recursive structure of the program and resolving procedure, function calls and variable references along the way. It uses the shunting yard algorithm for constructing expressions. The tree is executable, and at present I'm using it as a simplified scripting language as a machinery control front end. I say simplified in that it is even more "wordy" than actual Pascal - i.e. many operators that are usually single character symbols can optionally be replaced with words. So a program can almost read like plain English for the benefit of the typical non programmer machine operators that use it... I also built in a feature where it can store its stack in a disc backed file so that it can resume execution after a power or computer failure etc. - that's useful in India where there are plenty of unpredictable power failures. The tree also stores pointers back to the corresponding source which allows the execution to be viewed in realtime etc. At present it only has 4 data types (Boolean, Integer, Real and String), and arrays - no type definitions or records yet. Variables can have read and write functions and procedures associated with them - so reading and writing to variables can have side effects - similar to properties.

My eventual plan for that (mainly out of fascination with this sort of thing) is to extend it to output arbitrary assembler or machine code by using itself as a backend generator - i.e. the definition of the target CPU or assembler would itself be written in the language - allowing easy extension to new CPUs and differing architectures etc. I mainly view it as an interesting hobby - certainly nothing that hasn't been done before!

 

 

Share this post


Link to post
Share on other sites

Deploying node-servers to Heroku requires these to be https-servers.

Attempting to produce such a https-server (based on the unmodified simplehttpserver demo) : 

  • copied NodeJS.http and changed last line to "result := Jhttp_Exports(requireModule('https') );"
  • copied SmartNJ.Server.Http and included the changed unit above
  • deployed to heroku

which compiles (sms) and builds (heroku) fine, but upon starting the server (https://webhook-sms.herokuapp.com:1881/) it keeps on trying to connect ... and then times out

heroku logs :

  • 2018-04-25T23:44:55.338067+00:00 app[web.1]: 25 Apr 23:44:55 - Server listening on port 1881
  • 2018-04-25T23:45:53.265133+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

any hints ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×