Getting Started with Esprima

Thanks to two awesome people (Nick and Andy), for introducing me to Esprima. If you have never heard about Esprima, here is a quick intro.

Esprima is a high performance, standard-compliant ECMAScript parser written in ECMAScript

In simple words, Esprima is a Javascript parser. It reads a string that consists of Javascript code and makes sense of it.

So where and why exactly you need a Javascript parser? Simple answer is for code completion in IDEs. When you start typing  ale, you expect  alert to be suggested by your IDE.

Another powerful use case is for minification. If you would want to read a Javascript source file and rewrite it by stripping comments and minimizing the variable names, you need a Javascript parser, that understands Javascript and then helps you rewrite the code.

Esprima was created by Ariya Hidayat and is maintained by Ariya and a bunch of contributors. Ariya also gave a talk on Esprima a couple of years. I would recommend watching it before you continue.

As you can see from the above video, Esprima can be used for Static Analysis using the AST (Abstract Syntax Tree). It can be used for Dynamic tracing, which helps us in identifying how many times each function is called. It can also be used in editing tools and code transformations to implement code minification.

In this post, we will check if any variables are leaked to the global scope from our Javascript code using Esprima.

And this is how we are going to do it. First, we are going to set up esprima and read a Javascript file. Once we read the file, we will build the AST (Abstract Syntax Tree) using the esprima’s parse(). Next, using the  escope module, we fetch the global scope and see if there are any variables attached to this and then warn the developer.

So, let us get started.

Continue reading

Re-Architecting a Firebase app to work with Node.js and MongoDB

In the last few days, I have see people worrying about two major problems. One of which is what is going to happen when Angular 2.0 releases and next one is how to migrate an existing Firebase code to a Node.js/MongoDB platform.

I am not going to answer problem one, as I don’t know what is going to happen (ahem chaos) but I will try and propose an experimental solution to the second issue on how you can re-architecting a Firebase app to work with Node.js and MongoDB.

I have met a few people who used Firebase to develop their product and hit the market in a short span of time. Now that the product is doing good, they want to stabilize it further and add more features. And they have realized that a few things may not be that easy to achieve using Firebase.

So they have built another layer with Node.js and MongoDB which kind-of replicates the data and performs actions on it. Another solution I have seen is to use apibase – a Node.js module to communicate with Firebase. I was not really satisfied with the solutions.

So, I went back and asked why Firebase and why are you fitting Firebase in your application architecture? Here are 2 major answers

  • Real time data sync
  • Offline storage

After giving the above reasons a good thought, I have come up with the below architecture that will “emulate” Firebase using Websockets and Local Storage inside a Node.js/MongoD.

If this solution gets stable over a period of time, I may create a yeoman/slush generator to scaffold a Node.js/Express.js and a MongoDB app with real time data sync and offline storage capabilities.

For this post, I will use a simple Todo app as an example. You can find the completed code here.

So let us get architecting!

Continue reading

Text Based Adventure Games with Node.js

Coming from a fraternity of GTA and Age of Empires, text based games were a new twist to my gaming world. I was not aware that such a thing as a text based game existed till Sheldon Cooper introduced it (#geek). I Googled and found the emulator online and without much effort, I was hooked to it. I keep playing the game on and off when I get time. 4 years later, I realized why don’t I build one such on my own on Node.js and let the community build more text based games sharpening the imagination of the players.

So, here it is Command Line Interface Adventure Games Engine : cli-adventure-games.

Below is a quick demo of a game in action

Sweet right!

A special thanks to the awesome Anurag Jain for his contribution towards the Game Script JSON and continuous feedback during the initial Game Engine development. And also a big thanks to Jayesh Choudhari for his insights while developing the game engine and suggestions on lexical processing. It was a pleasure working with both of you!

In this post, I will walk you through the process of playing the game as well as how you can contribute by building your own text based adventure game.

So, let us get started.

Continue reading