<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thanapon Jindapitak</title>
    <description>The latest articles on DEV Community by Thanapon Jindapitak (@tjindapitak).</description>
    <link>https://dev.to/tjindapitak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F268892%2F65404e7b-a617-43db-adb7-f9c4820d56d3.png</url>
      <title>DEV Community: Thanapon Jindapitak</title>
      <link>https://dev.to/tjindapitak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tjindapitak"/>
    <language>en</language>
    <item>
      <title>Why gRPC is not natively supported by Browsers</title>
      <dc:creator>Thanapon Jindapitak</dc:creator>
      <pubDate>Sun, 17 Dec 2023 20:09:44 +0000</pubDate>
      <link>https://dev.to/tjindapitak/why-grpc-is-not-natively-supported-by-browsers-3oj5</link>
      <guid>https://dev.to/tjindapitak/why-grpc-is-not-natively-supported-by-browsers-3oj5</guid>
      <description>&lt;p&gt;Hi there, &lt;/p&gt;

&lt;p&gt;Hope that this blog will give everyone some idea of why gRPC is not natively supported by the Browsers.&lt;/p&gt;

&lt;p&gt;Many mentioned "because of Browser limitation".&lt;br&gt;
But they don't explain why.&lt;/p&gt;

&lt;p&gt;Even in the &lt;a href="https://grpc.io"&gt;https://grpc.io&lt;/a&gt; blog says this&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is currently impossible to implement the HTTP/2 gRPC spec3 in the browser, as there is simply no browser API with enough fine-grained control over the requests. For example: there is no way to force the use of HTTP/2, and even if there was, raw HTTP/2 frames are inaccessible in browsers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://grpc.io/blog/state-of-grpc-web/#the-grpc-web-spec"&gt;https://grpc.io/blog/state-of-grpc-web/#the-grpc-web-spec&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;No one give explanations about why we cannot make Browser expose HTTP/2 frames&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;TL;DR&lt;/p&gt;

&lt;p&gt;There are 2 main reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;There is no way to force HTTP/2 on browser &lt;br&gt;
=&amp;gt; Because we need a server to support HTTP/2 too! and most of the server now in the internet only support HTTP/1.1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even we can force HTTP/2 on browser, raw HTTP/2 frames are inaccessible in browsers&lt;br&gt;
=&amp;gt; Browser nowadays supports HTTP/2, but it hides the communication part behind the scene. So it exposes only some small API to HTTP/2.&lt;br&gt;
gRPC uses HTTP/2, but not a trivial HTTP/2, it leverage a header frame to end the gRPC session (called 'Trailer' header frame).&lt;br&gt;
And this 'Trailer' header frame has to be accessible by JS, to determine the end of gRPC session along with grpc-status.&lt;br&gt;
But the Chromium team decided to 'WontFix' due to "the cost of this feature" is not worth it !! &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Believe it or not.&lt;/p&gt;

&lt;p&gt;I've attached the decision made by Chromium team member here&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As I explained previously, the time cost is not a single-cost, but an ongoing resource cost that will be perpetually paid, as every feature will need to consider the interactions with and implications of trailers. Writing the code is, unquestionably, the easy part - reviewing it for correctness, maintaining it for cross-browser interoperability, interacting with the rest of the networking and loading stack are significant and non-trivial costs. As we explained on the issue as well ( &lt;a href="https://github.com/whatwg/fetch/issues/34"&gt;https://github.com/whatwg/fetch/issues/34&lt;/a&gt; )&lt;br&gt;
That also discusses security risks. From a product perspective, only supporting it for non-semantic expression is equally not a desirable nor interoperable outcome.&lt;br&gt;
I appreciate your interest, but I think the decision to WontFix is based on a holistic evaluation of the cost of this feature, and leaving it Available for over a year is not a good representation that this has been reviewed, triaged, and actively rejected as something we want to support.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;See&lt;br&gt;
&lt;a href="https://bugs.chromium.org/p/chromium/issues/detail?id=691599"&gt;﻿bugs.chromium.org/p/chromium/issues/detail?id=691599&lt;/a&gt; &lt;/p&gt;




&lt;p&gt;Very well, let's try to understand their perspective a bit. We have to get some knowledge about gRPC over HTTP/2.&lt;/p&gt;

&lt;p&gt;Here is briefly how gRPC over HTTP/2 works&lt;/p&gt;

&lt;p&gt;Let's go&lt;/p&gt;

&lt;p&gt;HTTP/2 is an upgraded version of HTTP/1.1&lt;/p&gt;

&lt;p&gt;I'll cover only 2 gRPC related parts of HTTP/2 &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Instead of sending HEADER and DATA in the same packet as HTTP/1.1.
HTTP/2 introduces concept of Frames--Header frame and DATA frame.
Each frames will be sent on different packet&lt;/li&gt;
&lt;li&gt;The connection for HTTP/2 is now support multiplexing, e.g. Client can use the same connection to send data to server, server can send data to client at the same time&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are a lot more in HTTP/2 (I recommend to read more on this blog &lt;a href="https://ably.com/topic/http2"&gt;https://ably.com/topic/http2&lt;/a&gt;)&lt;/p&gt;




&lt;p&gt;That's HTTP/2 &lt;/p&gt;

&lt;p&gt;Now, gRPC.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Htw58AhY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29wxu1vas6vws0khqk31.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Htw58AhY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29wxu1vas6vws0khqk31.jpg" alt="Simple RPC call" width="800" height="351"&gt;&lt;/a&gt;&lt;br&gt;
ref: &lt;a href="https://www.oreilly.com/library/view/grpc-up-and/9781492058328/ch04.html#simple_rpc_message_flow"&gt;https://www.oreilly.com/library/view/grpc-up-and/9781492058328/ch04.html#simple_rpc_message_flow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a simple RPC call to remote server (Unary RPC, client sends single request to server and server sends single response to client), &lt;br&gt;
Request message will split into 3 frames: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HEADER frame (to start the gRPC channel)&lt;/li&gt;
&lt;li&gt;DATA frame (your function parameters, can be in protobuf format)&lt;/li&gt;
&lt;li&gt;another DATA frame (to tell the server that client is done sending)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look into a bit at the actual data&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HEADER frame (to start the gRPC channel)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HEADERS (flags = END_HEADERS)
:method = POST
:scheme = http
:path = /Service/method
:authority = example.com
te = trailers
grpc-timeout = 1S
content-type = application/grpc
grpc-encoding = gzip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;DATA frame (your function parameters, can be in protobuf format)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATA
010101010111010110 // hahaha it is encoded, how would I be able to read it, but you get it right?, it's just a bunch of alien bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;DATA frame (to tell the server that client is done sending)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATA (flags = END_STREAM)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok now the communicate is at a state called &lt;code&gt;half-closed&lt;/code&gt; specifies the HTTP/2 spec&lt;/p&gt;

&lt;p&gt;That state thingy have nothing to do to this blog at all, I just want to show off my knowledge a bit. Haha sorry let's continue.&lt;/p&gt;

&lt;p&gt;Then the server starts to process client request. Call the method, get the response.&lt;/p&gt;

&lt;p&gt;To response to client, server sends 3 frames (3 frame just to demonstrate a simple gRPC call) back to client:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HEADER frame (to tell client that server now want to send response to client)&lt;/li&gt;
&lt;li&gt;DATA frame (to return what ever server wants to return)&lt;/li&gt;
&lt;li&gt;HEADER frame (to close the gRPC channel), please pay attention to this, server uses HEADER frame to end the gRPC session, NOT DATA frame as when Client want to end the request sending frame. AND this last HEADER frame they called it &lt;code&gt;Trailers&lt;/code&gt; in HTTP/2&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Again, let's see the actual data&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HEADER frame (to tell client that server now want to send response to client)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HEADERS (flags = END_HEADERS)
:status = 200
grpc-encoding = gzip
content-type = application/grpc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;DATA frame (to return what ever server wants to return)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATA
1000011011100011010 // again it's encoded
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;HEADER frame (to close the gRPC channel) aka 'Trailers'
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HEADERS (flags = END_STREAM, END_HEADERS)
grpc-status = 0  // 0 means OK for gRPC
grpc-message = xxxxxx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all, when client receives this last frame (Trailers) it can read this &lt;code&gt;grpc-status&lt;/code&gt; to understand the status of the gRPC call.&lt;/p&gt;

&lt;p&gt;And JS want to read this Trailers frame but Trailers frame is not accessible in Browser since browser was implemented, even until now.&lt;/p&gt;

&lt;p&gt;The problem is, why don't they implement it, right?&lt;/p&gt;

&lt;p&gt;I ask the same question, everyone ask the same question, but the answer to that is "the cost of this feature" is not worth it!!&lt;/p&gt;




&lt;p&gt;Now they try to introduce another protocol called 'grpc-web' to not always relies on HTTP/2.&lt;/p&gt;

&lt;p&gt;YES I'M NOT MAD AT ALL&lt;/p&gt;

&lt;p&gt;If you want to see it yourself, please read it here (don't miss last comment): &lt;a href="https://bugs.chromium.org/p/chromium/issues/detail?id=691599"&gt;﻿bugs.chromium.org/p/chromium/issues/detail?id=691599&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bye&lt;/p&gt;

</description>
      <category>grpc</category>
      <category>chromium</category>
      <category>http2</category>
    </item>
    <item>
      <title>Server-Sent Events Explained</title>
      <dc:creator>Thanapon Jindapitak</dc:creator>
      <pubDate>Sat, 05 Aug 2023 18:48:15 +0000</pubDate>
      <link>https://dev.to/tjindapitak/server-sent-events-explained-2fd7</link>
      <guid>https://dev.to/tjindapitak/server-sent-events-explained-2fd7</guid>
      <description>&lt;p&gt;Hi there, it's been a while 👨🏼‍🦳&lt;/p&gt;

&lt;p&gt;In the era of Internet, people mostly are connected through Web technology, and even though we are talking about gigabit network nowadays, there are still some people who is obsessed about network utilization--make it faster, don't waste any of bytes! and they come up with different ideas to improve the protocol or even try to hack the existing protocol.&lt;/p&gt;

&lt;p&gt;I can give you an example, in the history of HTTP protocol, &lt;code&gt;HTTP/1.0&lt;/code&gt; has been introduced in 1996 but then suddenly next year, they introduced &lt;code&gt;HTTP/1.1&lt;/code&gt; this is still okay because &lt;code&gt;HTTP/1.0&lt;/code&gt; is really not usable--there are lot of limitation such Head-of-Line blocking (HOL blocking, see &lt;a href="https://engineering.cred.club/head-of-line-hol-blocking-in-http-1-and-http-2-50b24e9e3372" rel="noopener noreferrer"&gt;https://engineering.cred.club/head-of-line-hol-blocking-in-http-1-and-http-2-50b24e9e3372&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;OK, &lt;code&gt;HTTP/1.1&lt;/code&gt; is now a standard protocol (standardizing a protocol, it's a document published by Internet Engineering Task Force (IETF), you may heard of RFC-XXXX, &lt;code&gt;HTTP/1.1&lt;/code&gt; was standardized in RFC 2068) until year 2015, again, Google introduced another one called &lt;code&gt;SPDY&lt;/code&gt; but got renamed to &lt;code&gt;HTTP/2&lt;/code&gt;. It improves on many different areas and also bring many features such as keep-alive, server push.&lt;/p&gt;

&lt;p&gt;As you can predict. Google (again) are still not satisfied with just &lt;code&gt;HTTP/2&lt;/code&gt;, they introduced yet another improved version--named &lt;code&gt;QUIC&lt;/code&gt; which then got renamed to &lt;code&gt;HTTP/3&lt;/code&gt; in year 2022!. Earlier version they are using TCP, but now they go deeper in layer 4 (OSI Layer 4, Transport). They changed from TCP to UDP based on the idea that we are ok to lose some packet, how crazy! 🤯&lt;/p&gt;

&lt;p&gt;We are all been told that TCP is reliable and UDP is unreliable, but how comes &lt;code&gt;QUIC&lt;/code&gt; or &lt;code&gt;HTTP/3&lt;/code&gt; can use UDP?&lt;/p&gt;

&lt;p&gt;Here is the reason, the general idea is &lt;code&gt;QUIC&lt;/code&gt; uses &lt;code&gt;sequence number&lt;/code&gt; (same idea as TCP) to track the segment (Segment is the Protocol data unit, PDU for Transport Layer) ... there is more to that I'll cover more detail in my next blog, but if you want to read more detail, see RFC 9000: &lt;a href="https://datatracker.ietf.org/doc/html/rfc9000#section-13" rel="noopener noreferrer"&gt;https://datatracker.ietf.org/doc/html/rfc9000#section-13&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Phew ~&lt;br&gt;
Let's take a short break&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Alright, let's go&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;There is no such perfect thing in the world, this applies to the world of technology as well, I'm 100% sure that there will be more versions of HTTP to come in the future. &lt;/p&gt;

&lt;p&gt;But what we are discussing here ...&lt;br&gt;
Just to see, understand and use the non-perfect protocol ?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The answer is Yes! ✅&lt;br&gt;
There are pros/cons on every thing, but what we need is to understand tools/options we have on hands, try to understand why they implement it that way and understand when to use and when not to use. These reasoning is really useful when we have to analyze/estimate our solution in the future.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;So much talking, sorry, like I said, it's been a while 😁&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;There is another interesting topic that people tries to "utilize" every single bytes possible, it is &lt;strong&gt;Server-Sent Events&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Server-Sent Events is leveraging HTTP protocol, to keep the connection open until server decides to close it. It is one-direction communication (only server can send messages to client)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A bit of history about &lt;code&gt;SSE&lt;/code&gt; (I rephrase the original from Wikipedia: &lt;a href="https://en.wikipedia.org/wiki/Server-sent_events" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Server-sent_events&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;SSE was introduced in 2006 as part of &lt;code&gt;HTML5&lt;/code&gt; and during &lt;code&gt;HTTP/1.1&lt;/code&gt; day by Ian Hickson.&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;h2&gt;
  
  
  I'll cover 4 parts today
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;How SSE works&lt;/li&gt;
&lt;li&gt;Example&lt;/li&gt;
&lt;li&gt;Pros/Cons&lt;/li&gt;
&lt;li&gt;Learn&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Not wasting time, let's dive into part 1 (I already wasted too much time above 😅)
&lt;/h3&gt;

&lt;p&gt;Here we go ... part 1 how SSE actually works&lt;br&gt;
SSE is using HTTP protocol and HTTP rely on TCP to transfer the data. We have to understand that when it comes to TCP there are so many overhead because they will have to guarantee that the segment will be reliable, but in this blog I'll just mention only some part of TCP for the sake of simplicity.&lt;/p&gt;

&lt;p&gt;Every start of a connection using TCP, client has to do 3 way handshake with server to exchange something called sequence number, both side will have it's own sequence number. This number will be used to tell the integrity of the data sent (I'll explain more detail in the next blog)&lt;/p&gt;

&lt;p&gt;Here is how TCP's 3 way handshake works&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F95kvjm18bkt3b6wswkqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F95kvjm18bkt3b6wswkqo.png" alt="TCP 3 way handshake"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that we can say that the connection is &lt;code&gt;OPEN&lt;/code&gt;.&lt;br&gt;
And now client can send HTTP message to server via this connection.&lt;/p&gt;

&lt;p&gt;Alright, now SSE&lt;br&gt;
Because SSE is not our every day HTTP message, so, there should be a way for client to tell server that, "hey server this is a request for SSE, let's keep the connection open until I close it, and you can send me data via this open connection". &lt;/p&gt;

&lt;p&gt;Ok here is how, after connection is open, client will send HTTP request &lt;code&gt;GET /&lt;/code&gt; with header &lt;code&gt;Accept: text/event-stream&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;When server receives the request, server understands that it has to upgrade this connection to SSE, that's it!&lt;/p&gt;

&lt;p&gt;And when server wants to send data to client, it has to send with &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Headers:
Content-Type: text/event-stream
Tranfer-Encoding: chunked

Body:
"data: &amp;lt;text&amp;gt;\n\n"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And I'm not joking here... server has to send data in this format &lt;code&gt;data: &amp;lt;text&amp;gt;\n\n&lt;/code&gt;, or else, the protocol will not understands &lt;/p&gt;

&lt;p&gt;I'll demonstrate as diagram, to make it more clear&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfv3zbdxefcsf69ym2l4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfv3zbdxefcsf69ym2l4.png" alt="SSE"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not only just &lt;code&gt;data:&lt;/code&gt; there are more fields, if you are interested, please check this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#fields" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#fields&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;that is it! general idea of how SSE works, and I already covered part 1&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;h3&gt;
  
  
  Next is Part 2 Example
&lt;/h3&gt;

&lt;p&gt;I have a server on NodeJs Express running on port 8080&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/stream&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SSE Connected ....&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/event-stream&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Listening on 8080&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And I have multiple clients connects to it&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4l6kf3ksdzxdybsgui92.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4l6kf3ksdzxdybsgui92.png" alt="Many clients connect to server on SSE"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My client is &lt;code&gt;curl -H Accept:text/event-stream http://localhost:8080/stream&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can see clients connect to server using SSE then our server sends back &lt;code&gt;i++&lt;/code&gt; every 1 second&lt;/p&gt;

&lt;p&gt;That's cool isn't it, and I just covered part 2, that was quick 😉 &lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Part 3 Pros/Cons&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;during message pushing from server, no header ! (you just have to follow patter &lt;code&gt;data: &amp;lt;text&amp;gt;\n\n&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;HTTP/2 compatible &lt;/li&gt;
&lt;li&gt;Firewall friendly, because it's http! not any fancy new protocol&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Proxy could be hard&lt;/li&gt;
&lt;li&gt;Layer 7 Load balancing could be hard&lt;/li&gt;
&lt;li&gt;It is hard to scale (less than WebSocket)&lt;/li&gt;
&lt;li&gt;Feature is limited, if the requirement changed, we might have to re-implement with WebSocket&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;h3&gt;
  
  
  Part 4 learn
&lt;/h3&gt;

&lt;p&gt;I really like this part, because this part is purely from my deduction and reasoning skill on why Ian comes up with this idea, there will be a lot of discussion on it. And by understanding why it has been invented also try to make sense with it. It is super beneficial.&lt;/p&gt;

&lt;p&gt;Because SSE comes after WebSocket, but still, Ian had pushed SSE to become a standard in HTML5 (see &lt;a href="https://html.spec.whatwg.org/multipage/server-sent-events.html" rel="noopener noreferrer"&gt;https://html.spec.whatwg.org/multipage/server-sent-events.html&lt;/a&gt;), so, I think we should compare SSE with WebSocket just to see, in what case we should use SSE over WebSocket. Because both SSE and WebSocket, server can send data to client without the need to client asking every single time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WebSocket&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is a full-duplex which means, client and server can talk to each other simultaneously&lt;/li&gt;
&lt;li&gt;The protocol starts with TCP 3 way handshake, then client sends &lt;code&gt;GET / UPGRADE&lt;/code&gt; Http request, if server can support websocket, server will ack back, after that the communication talks in a different protocol, not HTTP anymore and it requires WebSocket headers&lt;/li&gt;
&lt;li&gt;Client has to implement reconnection logic by themself, where SSE on the other hand, client is support by &lt;code&gt;EventSource&lt;/code&gt; interface (supported 95% of the browsers, see &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/EventSource" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/EventSource&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Horizontal scaling is hard, because it is stateful&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By knowing that, I think, Ian introduces this SSE because he wants to have an alternative way of server sending data to client (without client asking every single time), and he wants to make it compatible on existing Networking device, for example, router, firewall. And also it is faster! (than WebSocket) because WebSocket has it's own header but SSE does not! (I mean, only the data format &lt;code&gt;data: &amp;lt;text&amp;gt;\n\n&lt;/code&gt; we have to follow)&lt;/p&gt;

&lt;p&gt;That's it! I cannot think of any other reasons why Ian had invented this SSE. If you can think of other good reason, please let me know.&lt;/p&gt;

&lt;p&gt;End of part 4&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Next chapter let's deep dive into TCP&lt;br&gt;
And WebSocket afterward&lt;/p&gt;

&lt;p&gt;Cheers !&lt;/p&gt;

&lt;p&gt;....&lt;/p&gt;

&lt;h2&gt;
  
  
  references
&lt;/h2&gt;

&lt;p&gt;QUIC: &lt;a href="https://http3-explained.haxx.se/en/quic-v2" rel="noopener noreferrer"&gt;https://http3-explained.haxx.se/en/quic-v2&lt;/a&gt;&lt;br&gt;
Pitfalls of EventSource on HTTP/1.1: &lt;a href="https://textslashplain.com/2019/12/04/the-pitfalls-of-eventsource-over-http-1-1/" rel="noopener noreferrer"&gt;https://textslashplain.com/2019/12/04/the-pitfalls-of-eventsource-over-http-1-1/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sse</category>
      <category>http</category>
      <category>tcp</category>
    </item>
    <item>
      <title>What is business domain?</title>
      <dc:creator>Thanapon Jindapitak</dc:creator>
      <pubDate>Wed, 30 Jun 2021 13:51:49 +0000</pubDate>
      <link>https://dev.to/tjindapitak/what-is-business-domain-5gk8</link>
      <guid>https://dev.to/tjindapitak/what-is-business-domain-5gk8</guid>
      <description>&lt;p&gt;So I've read books about "Domain Driven Design" architecture,&lt;br&gt;
most of the thoughts coming to my mind during reading process are where should I put these fraction of code to? And wonder that everyone is also having this problem.&lt;/p&gt;

&lt;p&gt;Then, I concluded that you will put everything related to "Business logic" into "Domain layer"&lt;/p&gt;

&lt;p&gt;Next question comes to my mind, "Are these fraction of code business logic ?"&lt;/p&gt;

&lt;p&gt;FU.. it's a hard question, I even let my friends look at the code and ask them is it the "Business logic" or not, not to be surprised, their answers diverted and with different reasons at that.&lt;/p&gt;

&lt;p&gt;Confused, really confused.&lt;/p&gt;

&lt;p&gt;Then I found this "Vertical Slice Architecture" (ref. &lt;a href="https://jimmybogard.com/vertical-slice-architecture/"&gt;https://jimmybogard.com/vertical-slice-architecture/&lt;/a&gt;) invented by Jimmy Bogard, and went through some of his talks.&lt;/p&gt;

&lt;p&gt;Finally, I got the idea that every thing is "Business logic", don't spend a lot of time arguing where should we put the code to, put it in plainly first then the important step is later "Refactor" it.&lt;/p&gt;

&lt;p&gt;That's it !! &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Better way of storing Per-Request data across middlewares in ASP.NET Core</title>
      <dc:creator>Thanapon Jindapitak</dc:creator>
      <pubDate>Thu, 11 Jun 2020 14:39:02 +0000</pubDate>
      <link>https://dev.to/tjindapitak/better-way-of-storing-per-request-data-across-middlewares-in-asp-net-core-1m9k</link>
      <guid>https://dev.to/tjindapitak/better-way-of-storing-per-request-data-across-middlewares-in-asp-net-core-1m9k</guid>
      <description>&lt;p&gt;Hi developers who loves to work from home like me, for those who doesn't enjoy working from home, please skip this post. warning!&lt;/p&gt;

&lt;p&gt;As you can guess from title, this post is about "How to not use HttpContext.Items"&lt;/p&gt;

&lt;p&gt;To be honest, I'm the one who is still using &lt;code&gt;HttpContext.Items&lt;/code&gt; because why not, right?--It's static! easy to write.&lt;/p&gt;

&lt;p&gt;But no, collectively, it gives me tremendous amount of issues.&lt;/p&gt;

&lt;p&gt;First, it's static! forget about mocking it.&lt;br&gt;
Second, it's static! forget about testing it.&lt;br&gt;
Third, it allows me to modify any variables I want!, brilliant !!, why is that a bad thing? because I can't control them. I can perform any add/delete/modify operations to any objects.&lt;/p&gt;

&lt;p&gt;aight, anyways, I will show you how to use HttpContext.Items first, I know it's boring, just bear with me once. coz I don't have many things to make my blog longer. haha.. oops.&lt;/p&gt;

&lt;p&gt;aight, let's go&lt;br&gt;
&lt;code&gt;HttpContext.Current.Items&lt;/code&gt; is a Key-Value-Pair, key and value both are type object.&lt;br&gt;
You can access anywhere in your middlewares.&lt;/p&gt;

&lt;p&gt;to store&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"myobject"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MyObject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to read&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;myObject&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IMyObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"myobject"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to modify&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;myObject&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IMyObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"myobject"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Hime&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hime Hime~"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;easy peasy!&lt;/p&gt;

&lt;p&gt;now let's have a look into this code one bit&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"DogSays"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Henlo hooman"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;sexyMailer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"DogSays"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"DogSays"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Kwakkkkk!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now you can imagine if you work with 1500+ developers, how troublesome to maintain your calm, when you're assigned to find who changes our dog to says "Kwakkkkk!".&lt;/p&gt;

&lt;p&gt;enough playing with time.&lt;/p&gt;

&lt;p&gt;I found 2 elegant ways&lt;/p&gt;

&lt;p&gt;[1.] use IHttpContextAccessor this interface is to read HttpContext through DI, simply you can register it by &lt;br&gt;
&lt;code&gt;services.AddSingleton&amp;lt;IHttpContextAccessor, HttpContextAccessor&amp;gt;();&lt;/code&gt;&lt;br&gt;
// this has to be registerd as singleton as per this announcement &lt;a href="https://github.com/aspnet/Hosting/issues/793"&gt;ref.&lt;/a&gt;&lt;br&gt;
or in ASP.NET Core 2.1 or above, you can use extension method &lt;code&gt;services.AddHttpContextAccessor();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;now in your class, you can access context via DI &lt;br&gt;
for example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeService&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IHttpContextAccessor&lt;/span&gt; &lt;span class="n"&gt;_httpContextAccessor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;HomeSerivce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IHttpContextAccessor&lt;/span&gt; &lt;span class="n"&gt;httpContextAccessor&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;_httpContextAccessor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;httpContextAccessor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;DoWork&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;says&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;_httpContextAccessor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"DogSays"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Rock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;says&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now you can mock your IHttpContextAccessor quite easily! and live a happy life.&lt;/p&gt;

&lt;p&gt;[2.] Create a Class to handle the modification of this shared objects, and register it as Scoped (created once per client request (connection) &lt;a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1#scoped"&gt;ref.&lt;/a&gt;) then you can use it through DI, also it's a class! you can do a typed properties&lt;/p&gt;

&lt;p&gt;so do speak, here is the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IVaultAccessor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AddToVault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;GetFromVault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VaultAccessor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyDog&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IVaultAccessor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyDog&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;MyDog&lt;/span&gt; &lt;span class="n"&gt;_myDog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AddToVault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MyDog&lt;/span&gt; &lt;span class="n"&gt;myDog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_myDog&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myDog&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;MyDog&lt;/span&gt; &lt;span class="nf"&gt;GetFromVault&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_myDog&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyDog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;WhatDoesTheDogSay&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// register as Scoped&lt;/span&gt;
&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddScoped&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IVaultAccessor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyDog&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;VaultAccessor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyDog&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;());&lt;/span&gt;


&lt;span class="c1"&gt;// Usage in any random class&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tea&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ITea&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IVaultAccessor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyDog&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_myDogAccessor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Tea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IVaultAccessor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyDog&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myDogAccessor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_myDogAccessor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myDogAccessor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;RandomName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;myDog&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_myDogAccessor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetFromVault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Rock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"What a relief you are still my dog, &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;myDog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WhatDoesTheDogSay&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can make IVaultAccessor fancy to the extent. And live a happy life.&lt;/p&gt;

&lt;p&gt;Whoaaa! that's it for today.&lt;br&gt;
How was it guys, hope you enjoy being chubbier.&lt;/p&gt;

&lt;p&gt;See you around.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Monadic null checking in ES6</title>
      <dc:creator>Thanapon Jindapitak</dc:creator>
      <pubDate>Thu, 14 Nov 2019 16:38:58 +0000</pubDate>
      <link>https://dev.to/tjindapitak/monadic-null-checking-in-es6-1obl</link>
      <guid>https://dev.to/tjindapitak/monadic-null-checking-in-es6-1obl</guid>
      <description>&lt;h2&gt;
  
  
  Hola, readers
&lt;/h2&gt;

&lt;p&gt;I'd like share you guys my journey of moving from C# world to JavaScript.&lt;/p&gt;

&lt;p&gt;First and last, it was fun.&lt;/p&gt;

&lt;p&gt;Of course, there're some features I really miss,&lt;br&gt;
One of them is the beautiful &lt;code&gt;Monadic null checking&lt;/code&gt; in C#.&lt;/p&gt;

&lt;p&gt;Some of you might have no idea.&lt;br&gt;
Let me tell, you can do something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Duck&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;duck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;ToLowerCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="s"&gt;"again? duck has no name"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;duck.Name&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; it won't throw exception, instead it ignores the rest of statement replacing with &lt;code&gt;null&lt;/code&gt;.&lt;br&gt;
Then we can simply use &lt;code&gt;??&lt;/code&gt; operator to check weather it's &lt;code&gt;null&lt;/code&gt; or not&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Basically, we can say, if &lt;code&gt;duck.Name&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; use default value&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it! brilliant, loved it !.&lt;br&gt;
This feature is amazingly powerful and save up lots of null checking line of code.&lt;/p&gt;

&lt;p&gt;But in JavaScript ... THERE IS NO SUCH THING! WHY?&lt;/p&gt;

&lt;p&gt;No worry, it's time to use Google to search good things, my friend.&lt;/p&gt;

&lt;p&gt;Apparently, there're soooo many questions like this in StackOverflow,&lt;br&gt;
now I know that people are struggling, upsetting, drowning in his/her own null checking code&lt;/p&gt;

&lt;p&gt;And there are helpful valid answers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;try&lt;/code&gt; &lt;code&gt;catch&lt;/code&gt;
=&amp;gt; Nah (dunno why, but, nah)&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Lodash.get&lt;/code&gt;
=&amp;gt; Nah (hate when I'm forced to use string!)&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;ES6&lt;/code&gt;'s &lt;code&gt;Proxy&lt;/code&gt;
=&amp;gt; Nah! ... oh! this is new, this is new.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see, what it can do.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Proxy object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy"&gt;developer.mozilla&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whoaaa !.. sounds good but I don't understand&lt;/p&gt;

&lt;p&gt;After years of researching &lt;code&gt;ES6&lt;/code&gt;'s &lt;code&gt;Proxy&lt;/code&gt;&lt;br&gt;
Finally, I did it!&lt;/p&gt;

&lt;p&gt;Here it is. (I'm not gonna explain anything about this code, since I have to do dishes and clean my house, sry, duty calls)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// proxyLib&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;proxies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;WeakSet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;proxies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getEmptyProxy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Proxy&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getEmptyProxy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="nx"&gt;proxies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getProxyObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Proxy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;propertyName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;propertyName&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

          &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;getEmptyProxy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;




&lt;span class="c1"&gt;// main&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;proxiedObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getProxyObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;def&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;proxiedObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;def&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;proxiedObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// world&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it everyone, thanks for reading, hope you guys enjoy.&lt;br&gt;
Feel free to comment, I rather not reply back, since I already have too many haters. Anyways, see you.&lt;/p&gt;

&lt;p&gt;Peach...&lt;/p&gt;

&lt;p&gt;dang auto correct&lt;/p&gt;

&lt;p&gt;Peace&lt;/p&gt;

</description>
      <category>es6</category>
      <category>proxy</category>
      <category>monadic</category>
    </item>
  </channel>
</rss>
