skonves / express-http-context

Get and set request-scoped context anywhere
MIT License
300 stars 26 forks source link

What's the difference with zonejs? #17

Open JSteunou opened 6 years ago

JSteunou commented 6 years ago

I was looking for a solution like this one and I saw several good candidates, but I did not search enough, yet, to understand the true differences between each. Until then maybe you can enlight me about the differences between express-http-context and zonejs (in a MW). Maybe it is more a question for the cls-hook author but as I saw your project I jump on the occasion :)

skonves commented 6 years ago

This library was built on Node's native continuation APIs. Originally it used the now deprecated domain API. More recently we have switched over to the new async_hooks API. In contrast Zone.js seems to have implemented their own internal functionality for persisting context over async operations.

From another perspective, this library aspires to the Unix philosophy of "do one thing and do it well." It provides an Express middleware to access native Node APIs. As such, it remains lightweight and (hopefully) a good foundation for other more complex features and functionality.

If you are looking for a small, lightweight, foundational library, then consider this library. If you are looking for a larger "batteries included" package, consider Zone.js.

I hope this feedback was helpful. Please let me know if you have any other questions 😄

JSteunou commented 6 years ago

Wow very helpful indeed! Thank you for the quick response @skonves

I scratched a little more and this is what I understand now, correct me if I'm wrong. This MW approach is to use hooks to listen to changes of internal stack thread or "false thread id" (I dont know how to name it but I hope you see the picture) in order to maintain a current list of http context and return the right one when needed. So this is very specific to this need. Whereas zonejs approach is more to patch everything to make things like it seems we are in real multi-thread, like http-context but for everything, using every tricks it can (so maybe async hook also) under the hood. Is that it?

skonves commented 6 years ago

I think that is a fair assessment :+1:

As a matter of transparency, I have never used Zone.js (other than a dependency of an Angular project) so my knowledge thus far comes from reading the docs and searching through their code on GitHub :)