LAB 4

OPEN SOURCE CONTRIBUTION

https://github.com/node-muneem/anumargak

What is the Project

Anumargak is a self professed “fastest http router” on NODEjs.  It is built off another http router program called numeen.

The issue https://github.com/node-muneem/anumargak/issues/12

The member opened an issue basically asking for 2 printed lists.  One of just a normal printed list of all static and dynamic routes, and another printed list of all static and dynamic routes in a tree like structure.

Problems I ran into

The issue seemed easy at first, but lack of documentation and guidance has turned this into quite the ordeal.  Where to start…

The lack of documentation for newcomers is the biggest drawback, second biggest is the member whom I am communicating with clearly is not a native English speaker which makes asking for what he really wants my produce difficult and his responses are quite ambiguous I feel.  It isn’t really only after a few hours of digging into the project and then going back to look what he wrote that I can make a connection and say “ahh, I understand what you meant”.

The biggest problem I keep coming into is understanding how this project works. Especially the objects which house the dynamic and static routes lets call them staticRoutes and dynamicRoutes.

If I perform a console.log(this.staticRoutes) we get a long list returned of objects within objects

***** return below *****

{ ACL: {},

  BIND: {},

  CHECKOUT: {},

  CONNECT: {},

  COPY: {},

  DELETE: {},

  GET:

   { ‘/’: { data: [Object], verMap: undefined, params: undefined },

     ‘/a’: { data: [Object], verMap: undefined, params: undefined },

     ‘/login/as/admin’: { data: [Object], verMap: undefined, params: [Object] },

     ‘/login/as/user’: { data: [Object], verMap: undefined, params: [Object] },

     ‘/login/as/staff’: { data: [Object], verMap: undefined, params: [Object] },

     ‘/some/route’:

      { data: undefined, verMap: [SemVerStore], params: undefined } },

  HEAD: {},

  LINK: {},

  LOCK: {},

  ‘M-SEARCH’: {},

  MERGE: {},

  MKACTIVITY: {},

  MKCALENDAR: {},

  MKCOL: {},

  MOVE: {},

  NOTIFY: {},

  OPTIONS: {},

  PATCH: {},

  POST:

   { ‘/’: { data: [Object], verMap: undefined, params: undefined },

     ‘/this/is/static’: { data: [Object], verMap: undefined, params: undefined } },

  PROPFIND: {},

  PROPPATCH: {},

  PURGE: {},

  PUT:

   { ‘/this/is/static’: { data: [Object], verMap: undefined, params: undefined } },

  REBIND: {},

  REPORT: {},

  SEARCH: {},

  SOURCE: {},

  SUBSCRIBE: {},

  TRACE: {},

  UNBIND: {},

  UNLINK: {},

  UNLOCK: {},

  UNSUBSCRIBE: {} }

**************************

So, at first I was quite overwhelmed with the amount of information inside of these two objects because I needed to get the METHOD , URL and VERSION to print and it seems like this will be a lot of parsing to get that information.

Another big headache was dealing with versions inside these objects, to create a versioned route one would execute this command using Anumargak:

router.on(‘GET’, ‘/some/route’, { version: ‘1.2.0’ }, (req, res, params) => {

//       

})

And then the above object will be displayed like this from a console.log

‘/some/route’: { data: undefined, verMap: [SemVerStore], params: undefined } }

The version is inside the verMap but it is the most confusing thing as I will outline below.

If You just try to execute this

                console.dir(this.staticRoutes.GET[‘/some/route’].verMap.tree);

**** It prints below

 Node {

  prefix: 0,

  children:

   { ‘1’:

      Node {

        prefix: 1,

        children: [Object],

        childrenPrefixes: [Array],

        store: null } },

  childrenPrefixes: [ 1 ],

  store: null }

********

The above output isn’t enough information and was causing me a ton stress.  Trying to append any of the other objects to that console output so I can see the other objects produced nothing nut “undefined” objects :

console.dir(this.staticRoutes.GET[‘/some/route’].verMap.prefix);

console.dir(this.staticRoutes.GET[‘/some/route’].verMap.children);

console.dir(this.staticRoutes.GET[‘/some/route’].verMap.tree.childrenPrefix);

Well it seems like the issue was I needed to reference a string inside the object, which let me to my next headache displayed with 3 different calls.

 console.dir(this.staticRoutes.GET[‘/some/route’].verMap.tree.children);

 console.dir(this.staticRoutes.GET[‘/some/route’].verMap.tree.children[‘1’].children);

 console.dir(this.staticRoutes.GET[‘/some/route’].verMap.tree.children[‘1’].children[‘2’].children);

* I created 2 VERSION routes, both for the same route above, version 1.2.3 and version 1.2.0.  This is what is printed out after the above is called

                { ‘1’:

                   Node {

                                 prefix: 1,

                                 children: { ‘2’: [Node] },

                                 childrenPrefixes: [ 2 ],

                                 store: null } }

                { ‘2’:

                   Node {

                                 prefix: 2,

                                 children: { ‘0’: [Node], ‘3’: [Node] },

                                 childrenPrefixes: [ 0, 3 ],

                                 store: null } }

                { ‘0’:

                   Node {

                                 prefix: 0,

                                 children: null,

                                 childrenPrefixes: [],

                                 store: { handler: [Function], store: undefined } },

                  ‘3’:

                   Node {

                                 prefix: 3,

                                 children: null,

                                 childrenPrefixes: [],

                                 store: { handler: [Function], store: undefined } } }

So I managed to find the version, but as you can see it is not user friendly at all. Basically my versions 1.2.0 and 1.2.3 both share the 1.2 which is highlighted above by only having one object of both 1 and 2, and because the last digits of the version were different it created two separate objects for them 0 and 3.

You can see how the above is confusing and would be difficult to parse.

SAVING GRACE

I managed to find the function which saves all the parameters into the objects which will save me lot of time. I only wish I was pointed to this function by the member to begin with rather than finding it myself as it could have saved me a lot of time. This is what I was referring to as bad documentation for newcomers, because it would be beneficial for anyone new to understand that there is a function which is called first once the program executes which saves every route in your app.

What I learned

There is a .prototype infront of the function names which I learned was just a more efficient way of managing objects in JS. Basically it separates the methods from the properties in an object which will save space in memory.

article can be found here -> https://hackernoon.com/prototypes-in-javascript-5bba2990e04b

In order to better understand the project I needed to learn how to view all the properties inside of objects which helps understand the structure of the project.

You can find some helpful articles here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

https://flaviocopes.com/how-to-inspect-javascript-object/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s