<?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>Forem: Muhammad Bin Zafar</title>
    <description>The latest articles on Forem by Muhammad Bin Zafar (@midnqp).</description>
    <link>https://forem.com/midnqp</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%2F699002%2Fcd31dc23-fcba-41ee-9152-8beda37a593c.png</url>
      <title>Forem: Muhammad Bin Zafar</title>
      <link>https://forem.com/midnqp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/midnqp"/>
    <language>en</language>
    <item>
      <title>The fuss with CRLF and LF in Git 😕</title>
      <dc:creator>Muhammad Bin Zafar</dc:creator>
      <pubDate>Thu, 04 Jan 2024 14:08:24 +0000</pubDate>
      <link>https://forem.com/midnqp/the-fuss-with-crlf-and-lf-in-git-4nnf</link>
      <guid>https://forem.com/midnqp/the-fuss-with-crlf-and-lf-in-git-4nnf</guid>
      <description>&lt;p&gt;This problem arises with Git when team members are using different operating systems, e.g., Windows vs Mac/Linux.&lt;/p&gt;

&lt;p&gt;A non-AI-generated summary of the solution:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Decide whether to save changes in the Git index (the Git commit database) in CRLF or LF. Git wants to save in LF. The world wants to save in LF. Linux and Mac have the default of LF. Windows is alone in having CRLF. So the decision is made: LF.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; In git-config, &lt;code&gt;core.autocrlf=input&lt;/code&gt; means when generating files from Git index for the user, Git will do CRLF for Windows and LF for Mac/Unix. Also, while writing to Git index from files, it will use LF. Having &lt;code&gt;core.autocrlf=false&lt;/code&gt; means Git won't do anything. Just save stuff as is. So, make sure you don't change the default setting.&lt;/p&gt;

&lt;p&gt;Another case to be aware of is that code formatters like Prettier may format the code and save the file with LF as line-endings in Windows, although the file had CRLF. In this case, Git will notice it and show a warning. Safely ignore this warning, not important at all, forget it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;warning: LF will be replaced by CRLF in &amp;lt;filename&amp;gt;.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Usually, that's all. But some teammates might add an ESLint (or similar code formatter) rule to have all line-endings as LF.&lt;/p&gt;

&lt;p&gt;This is tricky because tools like ESLint check the current file buffer and informs you of warnings and errors on the fly. And, in Windows, Visual Studio Code defaults to saving files with CRLF. &lt;/p&gt;

&lt;p&gt;Now, if you have an ESLint rule that enforces line-endings to be LF, then your entire file buffer in Visual Studio Code will be underlined due to warnings/errors! Overwhelming as hell!!!&lt;/p&gt;

&lt;p&gt;This is pain, and that's what happened to me once. Untangling that tuxedo situation is how I started investigating the case of CRLF vs LF in Git and finally wrote a LinkedIn post about it!&lt;/p&gt;

&lt;p&gt;To fix:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Try not having such an ESLint rule. One that strictly checks line-endings to be LF or CRLF. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create an &lt;code&gt;.editorconfig&lt;/code&gt; file in project root with:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="py"&gt;root&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="nn"&gt;[*]&lt;/span&gt;
&lt;span class="py"&gt;end_of_line&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;lf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then install the EditorConfig extension. This way, Visual Studio Code will not save the file with CRLF (the default in Windows), rather will follow the &lt;code&gt;end_of_line&lt;/code&gt; option.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;And that's it. Another wrap on another fussy issue. If you have a question, let me know. I'd love to answer and try helping you out!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading and have a great one 👋&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>github</category>
      <category>git</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The fuss with ESM vs CJS in Node.js</title>
      <dc:creator>Muhammad Bin Zafar</dc:creator>
      <pubDate>Thu, 26 Oct 2023 06:13:10 +0000</pubDate>
      <link>https://forem.com/midnqp/esm-vs-cjs-the-prelude-the-clash-and-the-solution-46ia</link>
      <guid>https://forem.com/midnqp/esm-vs-cjs-the-prelude-the-clash-and-the-solution-46ia</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;If you are learning to code in Node.js or have learnt to code in Node.js, then you are familiar with &lt;em&gt;callback hell&lt;/em&gt;. However, that's a case solved and closed. The one at hand is the dilemma of whether to write our code in the ECMAScript Module format or the CommonJS one. So, let's talk about this!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Prelude
&lt;/h2&gt;

&lt;p&gt;Node.js 🐢 started in 2009 with the standard of its time, the CommonJS format. Later the &lt;a href="https://www.ecma-international.org/publications-and-standards/standards/ecma-262/" rel="noopener noreferrer"&gt;ECMAScript specification&lt;/a&gt; picked up pace and garnered more stability. For this, ECMAScript modules were first introduced to Node.js in v8.5.0 in 2017 and it continued to receive improvements over the years.&lt;/p&gt;

&lt;p&gt;The default format in Node.js is CommonJS, and it is usually not explicitly mentioned in package.json such as &lt;code&gt;{ "type": "commonjs" }&lt;/code&gt;. However, the standard format for code reuse in JavaScript is ESM, which needs to be explicitly mentioned in the package.json like so &lt;code&gt;{ "type": "module" }&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Clash
&lt;/h2&gt;

&lt;p&gt;Many articles and docs including the &lt;a href="https://nodejs.org/api/esm.html" rel="noopener noreferrer"&gt;Node.js docs&lt;/a&gt; talk in length about this. The summary, in my opinion, is the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can use CommonJS code from ESM.&lt;/li&gt;
&lt;li&gt;You can use ESM code from CommonJS.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Firstly, CommonJS code from ESM.&lt;/strong&gt; It is dead simple from ESM to import CommonJS code that has a &lt;em&gt;default export&lt;/em&gt; or &lt;em&gt;named exports&lt;/em&gt;.&lt;br&gt;
&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;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// importing the default export&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// importing named exports&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, it gets tricky when there's no named exports from the CommonJS package, and the default-exported variable is an object. This is tricky because in CJS-to-CJS code reuse, we are accustomed to this convenience. But for CJS-ESM interoperability, this is an obvious bug.&lt;/p&gt;

&lt;p&gt;The following is perfectly fine between CJS files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// common.cjs&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// main.cjs&lt;/span&gt;
&lt;span class="kd"&gt;const&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="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./common.js&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;However, while importing &lt;code&gt;common.cjs&lt;/code&gt; from ESM, we cannot do &lt;em&gt;named imports&lt;/em&gt; of the properties &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;.&lt;br&gt;
&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="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="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./common.cjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;   &lt;span class="c1"&gt;// SyntaxError: Named export 'a' not found. &lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;common&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./common.cjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;  &lt;span class="c1"&gt;// Rather, first import the default&lt;/span&gt;
&lt;span class="kd"&gt;const&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="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;common&lt;/span&gt;               &lt;span class="c1"&gt;// Then, access object properties!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Secondly, ESM code from CommonJS.&lt;/strong&gt; This is not convenient. Importing ESM code from CommonJS is only done through the async &lt;code&gt;import()&lt;/code&gt; function. Since the ESM package is loaded asynchronously, coding pattern and linear reason-ability is hurt.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;importFileType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;file-type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isGzip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&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;fileType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;importFileType&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fileType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fileTypeFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&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;ext&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gz&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isPng&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&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;fileType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;importFileType&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fileType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fileTypeFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&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;ext&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;png&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;In the code above, the gist is, only to import an ESM module all dependent functions are now being converted to async. CommonJS does not support &lt;em&gt;top-level awaits&lt;/em&gt; for us to do &lt;code&gt;const importFileType = await import('file-type')&lt;/code&gt;. For this inconvenience, you should not write library code in ESM that will be reused. &lt;/p&gt;

&lt;p&gt;I disagree with &lt;a href="https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c" rel="noopener noreferrer"&gt;Sindre Sorhus&lt;/a&gt; on this in terms of writing library code. However, for writing application code, ESM is quite nice and convenient.&lt;/p&gt;

&lt;p&gt;Note that, in the code example above, the &lt;code&gt;file-type&lt;/code&gt; package will be imported only once and the promise is also resolved only once - causing no performance issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;This dilemma has multiple frontiers to solve for. For me, the summary is as follows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Case&lt;/th&gt;
&lt;th&gt;Suggestion&lt;/th&gt;
&lt;th&gt;Reason&lt;/th&gt;
&lt;th&gt;Example project&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Node.js app&lt;/td&gt;
&lt;td&gt;ESM&lt;/td&gt;
&lt;td&gt;App code are never reused&lt;/td&gt;
&lt;td&gt;An express.js backend server &lt;a href="https://github.com/midnqp/nodejs-backend" rel="noopener noreferrer"&gt;e.g.&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Node.js app that will bundled, minified, and &lt;a href="https://dev.to/midnqp/bundling-nodejs-into-single-executable-binary-l3g"&gt;packaged&lt;/a&gt;
&lt;/td&gt;
&lt;td&gt;CJS&lt;/td&gt;
&lt;td&gt;Better support for CJS in these tools&lt;/td&gt;
&lt;td&gt;A command-line app &lt;a href="https://github.com/midnqp/postman-cli" rel="noopener noreferrer"&gt;e.g.&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Library package&lt;/td&gt;
&lt;td&gt;CJS&lt;/td&gt;
&lt;td&gt;Best for the most swift reuse by both&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/expressjs/express" rel="noopener noreferrer"&gt;Express.js&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TypeScript&lt;/td&gt;
&lt;td&gt;Same rules&lt;/td&gt;
&lt;td&gt;Same cases&lt;/td&gt;
&lt;td&gt;Read &lt;a href="https://www.typescriptlang.org/docs/handbook/modules/guides/choosing-compiler-options.html" rel="noopener noreferrer"&gt;this.&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Babel/Transpilers&lt;/td&gt;
&lt;td&gt;Try not using them.&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;Thanks for reading! Found it interesting? Give it a ❤️ or share your thoughts/questions in 💭! Did I miss anything? Let me know in the comments.&lt;/p&gt;

&lt;p&gt;Have a great day!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>Ryan Dahl, and the story of his Wizardry in Opensource</title>
      <dc:creator>Muhammad Bin Zafar</dc:creator>
      <pubDate>Thu, 07 Sep 2023 10:07:00 +0000</pubDate>
      <link>https://forem.com/midnqp/wizards-of-opensource-ep-1-ryan-dahl-4f60</link>
      <guid>https://forem.com/midnqp/wizards-of-opensource-ep-1-ryan-dahl-4f60</guid>
      <description>&lt;p&gt;On 16 February 2009, &lt;a href="https://github.com/nodejs/node-v0.x-archive/commit/9d7895c567e8f38abfff35da1b6d6d6a0a06f9aa" rel="noopener noreferrer"&gt;a GitHub commit&lt;/a&gt; was made. By a person who would later turn out to be the creator of two of the most successful, impactful, and standard tech platforms the world has ever seen.&lt;/p&gt;




&lt;p&gt;It's the story of &lt;a href="https://tinyclouds.org" rel="noopener noreferrer"&gt;Ryan Dahl&lt;/a&gt;, the person behind Node.js and Deno. The mastermind behind the non-blocking asynchronous I/O model of Node.js - a radical approach at that time to process thousands of requests per second. A runtime that made JavaScript successful and gave it legs (i.e. through file system access) to pave the path for modern frontend development frameworks e.g. React from Facebook and Angular from Google.&lt;/p&gt;

&lt;p&gt;The struggle, however, was real. First, he had to give up his addiction towards Abstract Mathematics - some people apparently loved doing maths, a rare breed! Second, he had to find a company which will be willing to fund &lt;a href="https://github.com/midnqp/learning-to-code-in-nodejs/wiki/Event-Loop" rel="noopener noreferrer"&gt;this research and development&lt;/a&gt; for the first few years. With these two figured out after much efforts, came the big third one. Fork!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fup3i8jd7xd586vf925xa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fup3i8jd7xd586vf925xa.png" alt="Node.js code back in 2012" width="506" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Forks in opensource are often good things, but not so much for an emerging technology that attempts to be a standard in handling web requests and developing server-side applications. Difference of opinions in certain aspects may cause a project to be forked and have the dedicated pool of contributors be divided into forks, competing against each other with a more limited and divided group of people. This isn't good news for Ryan.&lt;/p&gt;

&lt;p&gt;With much negotiations, he was able to bring teams back together to focus on the main project. The remnants, however, of the fork still is found in some historical corners of GitHub. Its name was &lt;em&gt;io.js&lt;/em&gt;.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/nodejs" rel="noopener noreferrer"&gt;
        nodejs
      &lt;/a&gt; / &lt;a href="https://github.com/nodejs/node-convergence-archive" rel="noopener noreferrer"&gt;
        node-convergence-archive
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Archive for node/io.js convergence work pre-3.0.0
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Node.js Foundation - Node.js&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;This repository is the working repository for the proposed convergence of the
&lt;a href="http://github.com/iojs/io.js" rel="noopener noreferrer"&gt;http://github.com/iojs/io.js&lt;/a&gt; and &lt;a href="http://github.com/joyent/node" rel="noopener noreferrer"&gt;http://github.com/joyent/node&lt;/a&gt; projects under
the Node.js Foundation.&lt;/p&gt;
&lt;p&gt;Contributions, releases, and contributorship are under the proposed
Governance and Developer Policy for the soon-to-be-launched Node.js
Foundation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/joyent/nodejs-advisory-board/tree/master/governance-proposal" rel="noopener noreferrer"&gt;https://github.com/joyent/nodejs-advisory-board/tree/master/governance-proposal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://github.com/jasnell/dev-policy" rel="noopener noreferrer"&gt;http://github.com/jasnell/dev-policy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This project is operating with the oversight of the joint Node.js and io.js
core technical teams.&lt;/p&gt;
&lt;p&gt;Note: The original io.js README.md is temporarily renamed to iojs_README.md.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/nodejs/node-convergence-archive" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;The days were going swift with Node.js and Ryan, until... one day. &lt;a href="https://go.dev/" rel="noopener noreferrer"&gt;Golang&lt;/a&gt; from Google came across Ryan, and its newer concurrency model to process web requests through &lt;em&gt;goroutines&lt;/em&gt; and &lt;em&gt;channels&lt;/em&gt;. It made a realization for Ryan: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Node.js is &lt;strong&gt;not&lt;/strong&gt; the most effective approach to handling HTTP requests.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For more distributed applications or DNS servers, Node.js is no match for Golang. Despite having garbage collection built-in, the performance of Golang is unmatched and it compiles code into a easily-distributable single binary executable file. &lt;/p&gt;

&lt;p&gt;Soon after this realization, Ryan left the Node.js opensource project 😕&lt;/p&gt;

&lt;p&gt;Since then, Node.js is being governed by the &lt;a href="https://openjsf.org/" rel="noopener noreferrer"&gt;OpenJS Foundation&lt;/a&gt;. Now, the project is in good hands having people like &lt;a href="https://github.com/jasnell" rel="noopener noreferrer"&gt;James Snell&lt;/a&gt; from &lt;a href="https://snyk.io" rel="noopener noreferrer"&gt;Snyk&lt;/a&gt;, &lt;a href="https://github.com/mhdawson" rel="noopener noreferrer"&gt;Michael Dawson&lt;/a&gt; from Red Hat, and &lt;a href="https://github.com/mcollina" rel="noopener noreferrer"&gt;Matteo Collina&lt;/a&gt; formerly from NearForm in its Technical Steering Committee - tirelessly driving success, assessing latest developments in the field, and keeping the legend of Ryan alive!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>opensource</category>
      <category>programming</category>
    </item>
    <item>
      <title>Notes: Advanced Node.js Concepts by Stephen Grider</title>
      <dc:creator>Muhammad Bin Zafar</dc:creator>
      <pubDate>Sat, 19 Aug 2023 15:17:14 +0000</pubDate>
      <link>https://forem.com/midnqp/notes-advanced-nodejs-concepts-by-stephen-grider-4pp7</link>
      <guid>https://forem.com/midnqp/notes-advanced-nodejs-concepts-by-stephen-grider-4pp7</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Here's one of the most common interview questions you'll face when looking for a Node.js job: "Can you explain Node's Event Loop?" There are two types of engineers: those who can describe the Event Loop and those who cannot!  This course will ensure that you are incredibly well prepared to answer that most important question.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the preface to an advanced course from &lt;a href="https://www.udemy.com/user/sgslo" rel="noopener noreferrer"&gt;Stephen Grider&lt;/a&gt;, an Engineering Architect and a distinguished Udemy Instructor Partner based in San Francisco Bay Area, authoring engineering courses including the 3rd highest-rated course on React.js in all of Udemy, among 1000s of courses. Stephen has received over 400,000 reviews, and over a million students studied his published materials.&lt;/p&gt;

&lt;p&gt;The Advanced Node.js Concepts course contains 16 aggregate hours of lectures divided into several sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Internals of Node&lt;/li&gt;
&lt;li&gt;Enhancing Node performance&lt;/li&gt;
&lt;li&gt;Project Setup&lt;/li&gt;
&lt;li&gt;Data caching with Redis&lt;/li&gt;
&lt;li&gt;Automated Headless Browser Testing&lt;/li&gt;
&lt;li&gt;Wiring Up Continuous Integration&lt;/li&gt;
&lt;li&gt;Scalable Image/File Upload&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first two explores solely the Node.js anatomy in considerable depth, while the rest address several challenges using standard libraries and approaches in a suspensive manner. While benefitting from the course materials, I have greatly enjoyed the discoveries and solidified what I already knew. &lt;/p&gt;

&lt;p&gt;Without taking notes information flies away, which is why I made sure to do that maintaining a quite high signal-to-noise ratio as Chuck Zerby mentions in his book &lt;em&gt;The Devil's Details: A History of Footnotes&lt;/em&gt;. The notes may be the most helpful, if you already have an introductory idea on Node.js and willing to further it. It may also be advisable to follow Stephen alongside. However, I have included addendum wherever seemed relevent.&lt;/p&gt;

&lt;p&gt;So, here we go, the notes, enjoy!&lt;/p&gt;

&lt;h1&gt;
  
  
  The Internals of Node.js
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Node.js&lt;/strong&gt; is not a new programming language, rather a &lt;em&gt;runtime&lt;/em&gt; built on top of JavaScript. A programming language is primarily distinguished through its syntax. Node.js does not introduce any new syntax. In such a manner, TypeScript is a new programming language since it introduces new syntax.&lt;/p&gt;

&lt;p&gt;In computing, a &lt;strong&gt;program&lt;/strong&gt; can be defined by an executable file stored on disk, containing code or binary instructions for the processor. A &lt;strong&gt;process&lt;/strong&gt; can be defined as an instance of a program, when it is loaded into memory and executed by the processor. The operating system allocates and manages resources, e.g. heap,  stack pointers, registers, for the process in a secure isolated manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Threads&lt;/strong&gt; are units of instructions to be executed from a process. &lt;strong&gt;Scheduling&lt;/strong&gt; is the order of execution, and it refers to the ability of an operating system to decide which thread to process/execute in any given time. Some threads are more important than others. However, note the &lt;strong&gt;distinctions&lt;/strong&gt; that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In Node.js, threads are best suited for CPU-intensive tasks, not I/O-intensive tasks,&lt;sup&gt;[1]&lt;/sup&gt; for example any cryptographic computation, e.g. &lt;em&gt;crypto.pbkdf2()&lt;/em&gt; will run fast and &lt;em&gt;fs.writeFile()&lt;/em&gt; will not.&lt;/li&gt;
&lt;li&gt;Also note that threads are not used in Node.js for network I/O, rather network requests are managed by libuv using underlying functions provided by the operating system, e.g. &lt;em&gt;epoll&lt;/em&gt; for Linux, &lt;em&gt;kqueue&lt;/em&gt; for Mac. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;Further note that, having more number of threads does not necessarily improve the file I/O performance. File read-write performance depends on completely different set of factors, such as bus speed, bus bandwidth, storage device, etc. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inspite of that, using a thread from the thread pool to perform file I/O is necessary, in order to enable asynchronicity. Having more threads does in fact mean that we can read more files simultaneously, but it doesn't mean the act of reading an individual file becomes faster 🤯&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thread pool&lt;/strong&gt; (also known as the Worker pool) is a collection of worker threads that are managed by the libuv library. By default 4 threads are deployed by Node.js, and the &lt;em&gt;long-running operations&lt;/em&gt; as referenced in the event loop, in truth, refer to thread pool tasks. Note that the event loop itself does not assign tasks to the thread pool, but only checks and runs pending callbacks when the thread pool tasks are complete. Thread pool tasks are assigned by the standard libraries e.g. &lt;em&gt;fs&lt;/em&gt; module, themselves. Custom thread pools tasks can be assigned from a Node.js C++ addon through &lt;a href="https://nodejs.org/api/n-api.html#:~:text=The%20given%20function%20is%20called%20from%20a%20worker%20pool%20thread%20and%20can%20execute%20in%20parallel%20with%20the%20main%20event%20loop%20thread." rel="noopener noreferrer"&gt;N-API&lt;/a&gt; and &lt;a href="https://www.npmjs.com/package/webworker-threads" rel="noopener noreferrer"&gt;WebWorker Threads&lt;/a&gt;. Despite its relative feasibility, it may be suggested to use the builtin thread pool instead of creating new workers using the &lt;em&gt;worker_threads&lt;/em&gt; module.&lt;sup&gt;[2]&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In the source code&lt;/strong&gt; of the &lt;a href="https://github.com/nodejs/node" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; opensource project, &lt;em&gt;lib&lt;/em&gt; folder contains JavaScript code, mostly wrappers over C++ and function definitions. On the contrary, &lt;em&gt;src&lt;/em&gt; folder contains C++ implementations of the functions, which pulls dependencies from the &lt;a href="https://chromium.googlesource.com/v8/v8.git" rel="noopener noreferrer"&gt;V8&lt;/a&gt; project, the &lt;a href="https://github.com/libuv/libuv" rel="noopener noreferrer"&gt;libuv&lt;/a&gt; project, the &lt;a href="https://github.com/madler/zlib" rel="noopener noreferrer"&gt;zlib&lt;/a&gt; project, the &lt;a href="https://github.com/nodejs/llhttp" rel="noopener noreferrer"&gt;llhttp&lt;/a&gt; project, and many more - which are all placed at the &lt;em&gt;deps&lt;/em&gt; folder.&lt;/p&gt;

&lt;p&gt;For example, the latest implementation of the exported function named &lt;em&gt;pbkdf2&lt;/em&gt; in the file lib/internal/crypto/pbkdf2.js contains a reference to a class named &lt;em&gt;PBKDF2Job&lt;/em&gt;. However that class is never found among the JavaScript source code, rather found at the file src/crypto/crypto_pbkdf2.h - which is made available to internal JavaScript code through &lt;code&gt;const {PBKDF2Job} = internalBinding('crypto');&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When does libuv and V8 come into play?&lt;/strong&gt; The purpose of V8 is to translate C++ values into their V8 JavaScript equivalence. The libuv project provides file system access, some aspects of concurrency, and a lot of processing constructs on the C++ side. &lt;/p&gt;

&lt;p&gt;To process threads well, multiple cores are introduced. A single core can process multiple threads, which is called multi-threading or hyperthreading. For example, let's say Thread 1 is reading from a file, and Thread 2 is multiplying 3x3. Reading a file always takes a non-zero amount time, which the OS can detect and put the thread on pause, to allow running Thread 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Threads inside the event loop.&lt;/strong&gt; Node.js creates 1 thread by default, and executes all code in that one thread. There is exactly one Event Loop per Node.js process, however that Event Loop may initiate other smaller event loops within itself. Each iteration in an Event Loop is called a &lt;em&gt;tick&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Stephen Grider mentions 5 major phases, checked by the event loop:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pending callbacks of timers to run? &lt;em&gt;e.g. setTimeout, setInterval&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Pending callbacks of &lt;em&gt;system operations&lt;/em&gt; and &lt;em&gt;long-running operations&lt;/em&gt; to run? &lt;em&gt;e.g. epoll(), inotify_init1(), kevent() are system operations, and Thread Pool tasks are long-running operations&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Pause execution, continue only when:

&lt;ul&gt;
&lt;li&gt;a new event done for system operations&lt;/li&gt;
&lt;li&gt;a new event done for long-running operations&lt;/li&gt;
&lt;li&gt;a timer is about to complete&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Pending callbacks for &lt;em&gt;setImmediate&lt;/em&gt;?&lt;/li&gt;
&lt;li&gt;Pending &lt;em&gt;close&lt;/em&gt; events to cleanup?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, the Event Loop makes one more iteration if it detects any pending timers, or system operations, or long-running operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is Node.js single-threaded?&lt;/strong&gt; The answer isn't straightforward. The Event loop of Node.js is single-threaded, however file I/O operations, cryptographic operations, and some network operations run in multiple threads through the Thread pool.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I can't say I don't yearn for another deeper more advanced series of materials from Stephen Grider or someone alike. Nevertheless, I do think I have been well-packed enough to pursue the next steps in this learning journey by myself.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;[1] Node.js API Docs: &lt;a href="https://nodejs.org/api/worker_threads.html#worker_threads_best_practices:~:text=Workers%20(threads)%20are%20useful%20for%20performing%20CPU%2Dintensive%20JavaScript%20operations.%20They%20do%20not%20help%20much%20with%20I/O%2Dintensive%20work.%20The%20Node.js%20built%2Din%20asynchronous%20I/O%20operations%20are%20more%20efficient%20than%20Workers%20can%20be." rel="noopener noreferrer"&gt;Worker threads&lt;/a&gt;&lt;/sup&gt;&lt;br&gt;
&lt;sup&gt;[2] Node.js Guide Docs: &lt;a href="https://nodejs.org/en/docs/guides/dont-block-the-event-loop#:~:text=The%20secret%20to,memory%2C%20context%2Dswitching)." rel="noopener noreferrer"&gt;Don't Block the Event Loop&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>My journey from Vim/Neovim to VS code 🥺</title>
      <dc:creator>Muhammad Bin Zafar</dc:creator>
      <pubDate>Mon, 12 Jun 2023 18:36:36 +0000</pubDate>
      <link>https://forem.com/midnqp/my-journey-into-vimneovim-23n5</link>
      <guid>https://forem.com/midnqp/my-journey-into-vimneovim-23n5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Disclaimer: potential standard-inclined content, reader discretion advised.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following anti-monkeypatch standard-inclined mindset originated from the frustrations from prolonged exposure to and usage of semi-standard technologies, e.g. code editors, operating systems, desktop workspace, etc.&lt;/p&gt;

&lt;p&gt;Opensource is good. As a movement to empowering developers, enabling innovations, and receiving critique to gradually agree on an industry standard - there is &lt;em&gt;simply&lt;/em&gt; no alternative of opensource. Our world runs on opensource! However the aspect of opensource regarding its very nature, that is, most opensource projects do not receive the funding they deserve and may lack a strong leadership to govern the project. &lt;strong&gt;This may yield a project deficit in supporting its ever-evolving consumer base.&lt;/strong&gt; Opensource software projects witnessed tremendous success in the early ages of commercial computer software - this is beyond doubts. However in the contemporary era, in which the complexity, diversity, and expectations of software is vital - &lt;strong&gt;a distinction is to be made&lt;/strong&gt; between FAANG-backed or Fortune 500-backed opensource initiatives and other individual/communitarian ones.&lt;/p&gt;




&lt;p&gt;This was a regular day in the life of mine as a Software Engineer - however, yet again, I caught myself searching for decentralized extensions individually developed on an average of 9 years ago for Vim/Neovim. Similar to Visual Studio Code, however... &lt;em&gt;(checking vscode vs vim user count worldwide...)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(In a survey of 86,544 responses...)&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Code editor&lt;/th&gt;
&lt;th&gt;% Users&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Visual Studio Code&lt;/td&gt;
&lt;td&gt;73.71%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Visual Studio&lt;/td&gt;
&lt;td&gt;28.43%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IntelliJ IDEA&lt;/td&gt;
&lt;td&gt;26.82%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vim&lt;/td&gt;
&lt;td&gt;22.29%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Neovim&lt;/td&gt;
&lt;td&gt;11.86%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;... more likely that, Visual Studio Code extensions will be better 🙃. Plus, centralized "verified" extension support provide an upper hand in some cases.&lt;/p&gt;

&lt;p&gt;In reddit, a question was asked, why shouldn't I use Vim? Arguments against Vim. The first reply received 92 upvotes. Feb 2019.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Every once in a while you'll go through the rabbit hole of addng something cool, configuring another plugin, or changing colorscheme/syntax - &lt;strong&gt;that you forget doing actual productive work.&lt;/strong&gt;"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(This is too true! May I be forgiven! Unbelievable! Feels sad!)&lt;/p&gt;

&lt;p&gt;One of the reply to the first reply was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"That's why I started relearning vanilla Vim. Native Vim. Without plugins. For Java, Go, Swift - I use JetBrains products with ideavim [a plugin that enables Vim keybindings]."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I almost feel like taking one step further,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"I may just use Visual Studio Code, instead of Vim. Maybe Windows 11, instead of a Linux distribution that was created by a guy over a decade ago. Maybe the magic developed by JetBrains. Maybe Slack, instead of Matrix. Maybe Microsoft 365 w/ Copilot, instead of LibreOffice. These are products well-built and polished and proven through rigorous testing and extensive real-world usage.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Maybe, just maybe, I need to learn to appreciate professionals investing their time and life and company to build these incredible, impossible products."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>The Fuss of Cross-Compiling C/C++ 😒</title>
      <dc:creator>Muhammad Bin Zafar</dc:creator>
      <pubDate>Sun, 20 Nov 2022 04:04:30 +0000</pubDate>
      <link>https://forem.com/midnqp/compiling-cc-on-both-windows-and-linux-with-address-sanitizer-3ikn</link>
      <guid>https://forem.com/midnqp/compiling-cc-on-both-windows-and-linux-with-address-sanitizer-3ikn</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was put together from my findings when I tried to compile &lt;a href="//www.github.com/midnqp/cpy"&gt;libcpy&lt;/a&gt; for Windows 11.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Let's say we have some C/C++ code already written for gcc in linux. We want to compile them for Windows 11 as well. To have greater portability. How can we achieve this? Which toolchain to select? What are the obstacles? In this article we'll explore.&lt;/p&gt;
&lt;h3&gt;
  
  
  🧩 Les problèmes
&lt;/h3&gt;

&lt;p&gt;How everything other than VS2022 fails? In CMake for VS Code, you can have some preset CMake configurations in &lt;code&gt;CMakeSettings.json&lt;/code&gt; file. Also known as toolchain or "kits" for clang, mingw, cl, etc. However:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;mingw doesn't have address sanitizers.&lt;/li&gt;
&lt;li&gt;clang throws syntax errors for multi-line macro, same as cl.&lt;/li&gt;
&lt;li&gt;cl doesn't fully support C99, so &lt;code&gt;unsigned stack[va_arg_count]&lt;/code&gt; is an error because it "expected a constant expression" of &lt;code&gt;int&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;all projects may not be portable in visual studio; if you clone a project using cmake from github and &lt;em&gt;Open&lt;/em&gt; that folder in VS2022, then you're almost on your own with &lt;code&gt;CMakeSettings.json&lt;/code&gt; or CMake presets&lt;/li&gt;
&lt;li&gt;in VS code, you may have quite a hard time in larger projects, e.g. linking external shared libraries (i.e. &lt;code&gt;vld_x64&lt;/code&gt;) with the compiled executable using CMake&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  🌟 La lumière de la solution
&lt;/h3&gt;

&lt;p&gt;Enter Microsoft Visual Studio 2022. The pinnacle of excellency from Microsoft. An under-appreciated humble big-friendly-giant. A champ. A software specimen.&lt;/p&gt;

&lt;p&gt;If you ask any airline "is this flight safe?", they reply something to the effect of "oh, your trip to the airport was the most risky part".&lt;/p&gt;

&lt;p&gt;If you ask me, "how does it feel coding in VS2022?", I'd brighten up my charm and uplift my morale to say "double-clicking this app was the hardest part". T is liketh wat'r.&lt;/p&gt;
&lt;h4&gt;
  
  
  L'appréciation
&lt;/h4&gt;

&lt;p&gt;So, a major reason behind migrating to Microsoft VS2022 was to have great support, great tooling, and ease in development. VS2022 has a ton of features/tooling/extensions/options/configurations. So much so that, in the beginning it may feel like an overkill or a bloat. However, each feature has a purpose of life, and each of them are helpful. Each of them are built over years of demand from low-level developers from around the global over more than a decade of feedback.&lt;/p&gt;
&lt;h4&gt;
  
  
  La confusion?
&lt;/h4&gt;

&lt;p&gt;Let's say I have a &lt;em&gt;Project&lt;/em&gt; for dll in C/C++ in a &lt;em&gt;Solution&lt;/em&gt;. In the same solution, I have another Console App &lt;em&gt;Project&lt;/em&gt; to use the library. &lt;/p&gt;

&lt;p&gt;You turn on "Enable Address Sanitizer" from &lt;em&gt;Project Properties&lt;/em&gt; on the console app - and the whole app crashes for some people. Apparently Visual Studio developers &lt;a href="https://developercommunity.visualstudio.com/t/address-sanitizer-asan-causes-f10-stepping-to-appe/1142590#T-N1251253" rel="noopener noreferrer"&gt;tried fixing&lt;/a&gt; this flaw. However this is not fixed &lt;a href="https://developercommunity.visualstudio.com/t/asan-x64-causes-unhandled-exception-at-0x00007ffac/1365655#T-N10173820" rel="noopener noreferrer"&gt;yet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The error output will almost spam your debug console with the following message in enormous quantity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Exception thrown at 0x00007FFB12B64F69 (KernelBase.dll) in
main.exe: 0xC0000005: Access violation 
writing location 0x0000001F3966FCCE.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please do note that this major problem is for x64 only. However, a dev from the Visual Studio team &lt;a href="https://developercommunity.visualstudio.com/t/access-violation-writing-location-in-output-window/1383196#T-N1386053" rel="noopener noreferrer"&gt;said&lt;/a&gt; that the silent violations are normal, and not a bug. This is incorrect because, these "silent normal access violations" cause:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;all memory leaks to be undetected! (when compiled with Visual Studio's lovely library to detect memory leaks, &lt;code&gt;&amp;lt;crtdbg.h&amp;gt;&lt;/code&gt;, which we'll discuss later)&lt;/li&gt;
&lt;li&gt;causes deadlier, more messy, unfixable Stack overflow 😎 errors. (when compiled with Visual Leak Detector, &lt;code&gt;&amp;lt;vld.h&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;crashes the application immediately for some people&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  La good news :D
&lt;/h4&gt;

&lt;p&gt;Just don't turn on "Enable Address Sanitizer" for the console app. That's it. That's how all of the problems in the previous section can be completely mitigated!&lt;/p&gt;

&lt;h4&gt;
  
  
  Les armes 🛠️
&lt;/h4&gt;

&lt;p&gt;There are quite a number of libraries and tools to detect memory leaks, but the following two worked great for me. Both of the library masterpieces work without enabling &lt;code&gt;--fsantize=address&lt;/code&gt;, hence no access violation errors. Let's say we have a piece of code, and let's see how they turn out.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;code&gt;#include &amp;lt;crtdbg.h&amp;gt;&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;This is a system library from the C Runtime library of Microsoft Visual Studio. Therefore this library is available is no other platform except Windows. The library works exceptionally well, and it automatically binds itself to all app exit points, and outputs memory leak info.&lt;/p&gt;

&lt;p&gt;Let's say the code we have is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;crtdbg.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;_CrtSetDbgFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_CRTDBG_ALLOC_MEM_DF&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;_CRTDBG_LEAK_CHECK_DF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// free(str);&lt;/span&gt;
  &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Detected memory leaks!
Dumping objects -&amp;gt;
main.c(8) : normal block at 0x0000020C7981FB50, 6 bytes long.
 Data: &amp;lt;hello&amp;gt; .... some hex values .... 
Object dump complete.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  &lt;code&gt;#include &amp;lt;vld.h&amp;gt;&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;This &lt;a href="https://github.com/kinddragon/vld#:~:text=as%20a%20service%20to%20the%20Windows%20developer%20community." rel="noopener noreferrer"&gt;library&lt;/a&gt; is developed by KindDragon. Another acme of human intelligence. However the &lt;a href="https://github.com/Azure/vld" rel="noopener noreferrer"&gt;fork&lt;/a&gt; from Azure seems to fix an issue.&lt;/p&gt;

&lt;p&gt;Let's say the code we have is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// free(str);&lt;/span&gt;
  &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00000000C7C0D970: 6 bytes ----------
Leak Hash: 0x9C8CAD7D, Count: 1, Total 6 bytes
Call Stack (TID 13088):
ucrtbased.dll!malloc()
.... more call stack ....                                                                                             
.... more call stack ....
C:\Users\USER\main.c
.... more call stack ....
.... more call stack ....

Data: 
.... The readable value of the pointer: "hello" ....
.... outputs the surrounding heap ....
.... outputs the surrounding heap ....
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  💭 La final thoughts
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.msys2.org/" rel="noopener noreferrer"&gt;Msys2&lt;/a&gt; may help while coding in Visual Studio. And as we discussed, you may get VS Code to work correctly on a large project, however Microsoft Visual Studio 2022 would be the best practice! Apparantly you can also write code for &lt;a href="https://visualstudio.microsoft.com/vs/features/cplusplus/#:~:text=conformance%20from%20Microsoft-,Linux%20Development,-Write%20Linux%20apps" rel="noopener noreferrer"&gt;linux&lt;/a&gt; from VS 2022, however &lt;code&gt;gcc&lt;/code&gt; and &lt;code&gt;gdb&lt;/code&gt; has complete support for address sanitizers.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>c</category>
      <category>linux</category>
      <category>programming</category>
    </item>
    <item>
      <title>📦 Bundle Node.js code into single executable binary</title>
      <dc:creator>Muhammad Bin Zafar</dc:creator>
      <pubDate>Sat, 20 Aug 2022 12:13:00 +0000</pubDate>
      <link>https://forem.com/midnqp/bundling-nodejs-into-single-executable-binary-l3g</link>
      <guid>https://forem.com/midnqp/bundling-nodejs-into-single-executable-binary-l3g</guid>
      <description>&lt;p&gt;Node.js 🐢, the asynchronous event-driven JavaScript runtime, has unparalleled support for file-system access, among other things - opening up the door to endless possiblities! However, Node.js often loses out to other runtimes/languages in cases where being able to package a single, executable application simplifies distribution and management of what needs to be delivered. &lt;/p&gt;

&lt;p&gt;While there are components/approaches for doing this, they need to be better documented and evangelized so that this is not seen as a barrier for using Node.js in these situations. This is important to support the expansion of where/when Node.js is used in building solutions.&lt;/p&gt;

&lt;p&gt;This article addresses 2 major concerns in the Node.js ecosystem: bundling and packaging. Let's talk about them briefly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bundling&lt;/strong&gt; is the concept of merging the code, and all its dependencies into a single file. This is commonly seen for frontend development. &lt;/p&gt;

&lt;p&gt;However, using the ESM packaging format has one advantage than CJS: tree-shaking. Tree-shaking is the concept of removing unused code from a dependency. Tools: &lt;a href="https://esbuild.github.io" rel="noopener noreferrer"&gt;esbuild&lt;/a&gt;, &lt;a href="https://parceljs.org" rel="noopener noreferrer"&gt;parcel&lt;/a&gt;, &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;webpack&lt;/a&gt;, &lt;a href="https://rollupjs.org/" rel="noopener noreferrer"&gt;rollup&lt;/a&gt;, &lt;a href="https://terser.org/" rel="noopener noreferrer"&gt;terser&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Packaging&lt;/strong&gt; in Node.js is concept of creating a single executable binary, which includes the source code and the Node.js runtime. This way, Node.js will not be needed to be installed on end-user's machine. &lt;/p&gt;

&lt;p&gt;During the process, the tool parses the source code, detects calls to &lt;code&gt;require()&lt;/code&gt;, traverses the dependencies, and includes them into executable. Usually the source code is compiled into bytecode using the &lt;code&gt;V8&lt;/code&gt; engine. Tools: &lt;a href="https://www.npmjs.com/package/pkg" rel="noopener noreferrer"&gt;pkg&lt;/a&gt;, &lt;a href="https://www.npmjs.com/package/ncc" rel="noopener noreferrer"&gt;ncc&lt;/a&gt;, &lt;a href="https://www.npmjs.com/package/nexe" rel="noopener noreferrer"&gt;nexe&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;esbuild&lt;/code&gt; to bundle
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An extremely fast JavaScript and CSS bundler and minifier&lt;/li&gt;
&lt;li&gt;Most convenient&lt;/li&gt;
&lt;li&gt;Fastest in comparison&lt;/li&gt;
&lt;li&gt;Support for TypeScript syntax, ESM, and CJS&lt;/li&gt;
&lt;li&gt;Supports tree-shaking for ESM&lt;/li&gt;
&lt;li&gt;Supports minification and source maps
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Output CommonJS bundle&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;npx esbuild index.js  &lt;span class="nt"&gt;--bundle&lt;/span&gt; &lt;span class="nt"&gt;--outfile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;build.cjs &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;cjs &lt;span class="nt"&gt;--platform&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Output ESM bundle&lt;/span&gt;
&lt;span class="c"&gt;# Note that, you may not need the --banner flag.&lt;/span&gt;
&lt;span class="c"&gt;# But, in some cases, require() and __dirname are needed.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;npx esbuild index.js  &lt;span class="nt"&gt;--bundle&lt;/span&gt; &lt;span class="nt"&gt;--outfile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;build.mjs &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;esm &lt;span class="nt"&gt;--platform&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;node &lt;span class="nt"&gt;--banner&lt;/span&gt;:js&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"
import {createRequire} from 'module';
const require = createRequire(import.meta.url);
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;pkg&lt;/code&gt; to package
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Package your Node.js project into an executable&lt;/li&gt;
&lt;li&gt;Instantly make executables for Windows, Mac, Linux, etc&lt;/li&gt;
&lt;li&gt;No need to install Node.js, or hundreds of dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Packaging tools work best with CJS. &lt;/span&gt;
&lt;span class="c"&gt;# These tools don't go well with ESM.&lt;/span&gt;

&lt;span class="c"&gt;# To package into executable, just take the file outputted&lt;/span&gt;
&lt;span class="c"&gt;# by `esbuild`, and pass it to `pkg`, and we're done!&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;npx pkg build.cjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will output 3 binary exectuable files &lt;code&gt;build-linux&lt;/code&gt;, &lt;code&gt;build-macos&lt;/code&gt;, and &lt;code&gt;build-win.exe&lt;/code&gt;. You might want to run the executable file for your platform. Now you can simply distribute these files to your end-users or deploy in production - without installing Node.js or any dependencies or anything - just this one file!&lt;/p&gt;




&lt;p&gt;Thanks for reading! Found it interesting? Give it a ❤️ or 🦄! Any topic you'd want to me cover? Let me know in the comments. &lt;/p&gt;

&lt;p&gt;Have a great day!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>node</category>
    </item>
    <item>
      <title>How to correctly design REST APIs</title>
      <dc:creator>Muhammad Bin Zafar</dc:creator>
      <pubDate>Thu, 18 Aug 2022 17:45:00 +0000</pubDate>
      <link>https://forem.com/midnqp/rest-api-a-quickread-for-backend-dev-3i70</link>
      <guid>https://forem.com/midnqp/rest-api-a-quickread-for-backend-dev-3i70</guid>
      <description>&lt;p&gt;REST stands for "Representational State Transfer". This is an architectural style. A web API conforming to this style is a REST API.&lt;/p&gt;

&lt;p&gt;REST stands on &lt;strong&gt;6 guiding principles&lt;/strong&gt;, important among them are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Client-server: separate backend and frontend.&lt;/li&gt;
&lt;li&gt;Stateless: each http request contains enough data (e.g. auth, session, user data) to understand it without knowing what the previous request was.&lt;/li&gt;
&lt;li&gt;Cacheable

&lt;ul&gt;
&lt;li&gt;GET: always &lt;/li&gt;
&lt;li&gt;POST: using http headers &lt;code&gt;expire&lt;/code&gt;, &lt;code&gt;cache-control&lt;/code&gt;, &lt;code&gt;etag&lt;/code&gt;, &lt;code&gt;last-modified&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;PUT, DELETE: never&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  🧠 Concepts
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F79n67b6tyw7nss50ow6z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F79n67b6tyw7nss50ow6z.png" width="571" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following are some important, concise, and simplified REST concepts a good backend dev must be familiarized with.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resource
&lt;/h3&gt;

&lt;p&gt;REST APIs are modeled as a resource hierarchy - where each node is either a collection, or a single resource. A single resource has some state, or sub-resources. &lt;/p&gt;

&lt;p&gt;Example of a stateful resource is the following, where a user resource has data/states:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="nx"&gt;e7d418a70f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;muhammad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;earth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;verified&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example of a resource with sub-resources is the following, where a user has multiple projects, each of which are a resource.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;projects&lt;/span&gt;
&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;postman cli&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;postman implemented in command-line&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;github.com/midnqp/postman-cli&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;typescript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;typescript is a superset of javascript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;github.com/microsoft/typescript&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Idempotency
&lt;/h3&gt;

&lt;p&gt;API Idempotency means "a client can make a request multiple times, returning same response - without having any side-effect".&lt;/p&gt;

&lt;p&gt;Making the following requests multiple times produces the same result, and has no further side-effect - thus are "idempotent" APIs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET /users/:id&lt;/code&gt; - just retrieving same user data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PUT /users&lt;/code&gt; - just updating same json data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE /users/:id&lt;/code&gt; - just deleteing the same user&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A non-idempotent API:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;POST /users/:id&lt;/code&gt; - because a new user will be created everytime this request is made&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  🎨 Designs
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fluuhhldx2mxb4u14wm8r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fluuhhldx2mxb4u14wm8r.jpg" width="506" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some constraints in REST API naming ensures a design of scalable API endpoints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;em&gt;plural&lt;/em&gt; nouns, e.g. users, orders, categories.&lt;/li&gt;
&lt;li&gt;Use &lt;em&gt;hyphen&lt;/em&gt;, not underscores. Use &lt;em&gt;lowercase&lt;/em&gt;, never camel-case, e.g. &lt;code&gt;GET /food-categories&lt;/code&gt;, not &lt;code&gt;GET /foodCategories&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Never&lt;/em&gt; use CRUD function names, e.g. &lt;code&gt;GET /users/list&lt;/code&gt; or &lt;code&gt;POST /users/create&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some perfect API endpoints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET /users&lt;/code&gt; - get a collection/list of users.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GET /users/:id&lt;/code&gt; - get a single user.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /users/:id&lt;/code&gt; - create a single user.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PUT /users/:id&lt;/code&gt; - update a single user.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE /users/:id&lt;/code&gt; - delete a single user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For sub-resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET /users/:id/projects&lt;/code&gt; - get projects of given user&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE /users/:id/projects/:id&lt;/code&gt; - remove a project of given user&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adding some features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET /users?country=earth&amp;amp;verified=true&lt;/code&gt; - search users by &lt;code&gt;country&lt;/code&gt; and &lt;code&gt;verified&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GET /users?$fields=name,country&lt;/code&gt; - list users, but return only 2 data attributes: &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;country&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GET /users?$sort=name&amp;amp;$order=asc&lt;/code&gt; - list users and sort by &lt;code&gt;name&lt;/code&gt; in ascending order&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GET /users/$page=1&amp;amp;$limit=10&lt;/code&gt; - list users and paginate with &lt;code&gt;page&lt;/code&gt; and &lt;code&gt;limit&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  🚫 Errors
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbpwe00cap7q5v0pbb7h5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbpwe00cap7q5v0pbb7h5.jpg" width="509" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most Google APIs use resource-oriented API design. Instead of defining different &lt;code&gt;NOT_FOUND&lt;/code&gt; errors, the server uses one standard &lt;code&gt;NOT_FOUND&lt;/code&gt; status code, and tells the client which specific resource was not found.&lt;/p&gt;

&lt;p&gt;The smaller error space has advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reduces the complexity of documentation&lt;/li&gt;
&lt;li&gt;better mapping&lt;/li&gt;
&lt;li&gt;reduces client logic complexity&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Model
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
  &lt;span class="na"&gt;details&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&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;ul&gt;
&lt;li&gt;
&lt;code&gt;Error.code&lt;/code&gt;: simple code, easily handled by client&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Error.message&lt;/code&gt;: Developer-facing human-readable error&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Error.details&lt;/code&gt;: Additional error information for client, such as retry info, help link, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Individual APIs &lt;strong&gt;must&lt;/strong&gt; avoid defining additional error codes.&lt;/li&gt;
&lt;li&gt;Developers must use canonical error codes.&lt;/li&gt;
&lt;li&gt;Standard error codes for Google APIs:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;200 OK&lt;/code&gt;, not an error; returned on success.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;500 UNKNOWN&lt;/code&gt;, internal server error; unexpected and insufficient info&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;400 INVALID_ARGUMENT&lt;/code&gt;, invalid/problematic user data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;404 NOT_FOUND&lt;/code&gt;, something doesn't exist globally, for everyone&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;409 ALREADY_EXISTS&lt;/code&gt;, something already exists&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;403 PERMISSION_DENIED&lt;/code&gt;, access denied for a user; relevant people have access&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;401 UNAUTHENTICATED&lt;/code&gt;, no bearer token; no valid auth creds&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;429 RESOURCE_EXHAUSTED&lt;/code&gt;, too many requests; rate-limit exceeded&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;400 FAILED_PRECONDITION&lt;/code&gt;, the operation was rejected; business logic unmet&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;400 UNAVAILABLE&lt;/code&gt;, the operation was rejected; user should retry&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Message
&lt;/h3&gt;

&lt;p&gt;Error messages should help users understand &amp;amp; resolve API errors easily.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not assume the user to an expert user of the API.&lt;/li&gt;
&lt;li&gt;Do not assume user knows anything about service implementation.&lt;/li&gt;
&lt;li&gt;Should be constructed such that technical users can respond, and correct it.&lt;/li&gt;
&lt;li&gt;Keep the message brief. If needed, provide a link to more info, questions, feedback. Otherwise, use "details" field.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>api</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
