<?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: ishar19</title>
    <description>The latest articles on DEV Community by ishar19 (@ishar19).</description>
    <link>https://dev.to/ishar19</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%2F784388%2Fd4168ac4-e27d-46b3-b3d1-b06cd33a4571.jpeg</url>
      <title>DEV Community: ishar19</title>
      <link>https://dev.to/ishar19</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ishar19"/>
    <language>en</language>
    <item>
      <title>It’s free real (e)state</title>
      <dc:creator>ishar19</dc:creator>
      <pubDate>Sun, 09 Jun 2024 14:55:44 +0000</pubDate>
      <link>https://dev.to/ishar19/its-free-real-estate-3jip</link>
      <guid>https://dev.to/ishar19/its-free-real-estate-3jip</guid>
      <description>&lt;p&gt;Let’s talk about States, in context of building user interfaces be it a web application, desktop or mobile applications.&lt;/p&gt;

&lt;p&gt;What is a state? Why do we use it, need it? How should we use it? And why/ where  should we not use it?&lt;/p&gt;

&lt;h3&gt;
  
  
  Act 1: What
&lt;/h3&gt;

&lt;p&gt;A state can be representative of your UI at any given point of time, whatever is on the screen can be considered as a state of your application. It’s like talking a snapshot of your app and keeping the information about colours, fonts, data, interactions everything.&lt;/p&gt;

&lt;p&gt;It tells us how and what is an app right now and what could happen next depending on the choices user makes.&lt;/p&gt;

&lt;p&gt;It holds your data coming from backend, user generated actions and interactions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffx83pd2xmpknjs9op0h6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffx83pd2xmpknjs9op0h6.png" alt="State as a function" width="603" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Act 2: Why
&lt;/h3&gt;

&lt;p&gt;Why do need a state? Why do we use states in our app? Do we even need a state? How many types of state can be there?&lt;/p&gt;

&lt;p&gt;All these questions are very subjective and opinionated, and here are my 2cents on it.&lt;/p&gt;

&lt;p&gt;Need: To hold information which might change depending on certain situations like user interaction or loading new set of data. To “do something, when something happens”.&lt;/p&gt;

&lt;p&gt;Use: To change only those parts of UI which needs to be changed when something changes or happens. That can be a click, a hover, a long press, refreshing the loading bar every 3 seconds. It could be anything. imagine reloading the whole application when a button is pressed.&lt;/p&gt;

&lt;p&gt;Do we need: Yes and No&lt;/p&gt;

&lt;p&gt;If your application contains some part which will be updated in future depending on the change of a variable, you might want to use a state for that.&lt;/p&gt;

&lt;p&gt;Do not keep constants which might as well be hardcoded and will never be changed through any user interaction/ to provide an user interaction or doesn’t affect the interface into a “state” in your app.&lt;/p&gt;

&lt;p&gt;Examples can be loading something from local storage of your device, or keeping a set of constants to map out your username’s initials with colours.&lt;/p&gt;

&lt;p&gt;Anything that is just loaded once and never re-loaded can just be a variable. &lt;/p&gt;

&lt;p&gt;Technically it is a state but not in the context of “state” when we use “setState” or “getState”. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl855fjjj6g1e7p0ilvqv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl855fjjj6g1e7p0ilvqv.png" alt="UI as a function" width="800" height="306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice, how it’s a two way function, depicting your UI depends on state and your state depends on UI.&lt;/p&gt;

&lt;p&gt;A change in state, loading a data, can change your UI and change in UI, selection of a filter, can change your state.&lt;/p&gt;

&lt;p&gt;I only keep a piece of info in “state” if the change in it should immediately reflect in UI and I only map a piece of UI to “state” if I need to carry that piece of information to next interaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Act 3: How
&lt;/h3&gt;

&lt;p&gt;Depending on the tech you use, you will have different options to use states in your application.&lt;/p&gt;

&lt;p&gt;But there are two major types which needs to be differentiated &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;App wide state - This piece of info is going to be used at multiple places and, emphasis on and, can be changed from multiple places. If it’s only going to be read, you might as well be okay with reading it directly from source rather than going through the pain of setting up an app level state. &lt;/p&gt;

&lt;p&gt;App level info like theme of your UI, user authentication info, cart info if it’s an e-commerce thing, omnibox etc&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnl6m9klu8qz1jhyci78.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnl6m9klu8qz1jhyci78.png" alt="App state" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Component/ widget level state - This piece of info is going to be used in this and maximum of two level deep component/ widget from this. You will be fine with just using a component/ widget level solution.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyrxef1r6r9d48r46vh3q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyrxef1r6r9d48r46vh3q.png" alt="Component State" width="528" height="885"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Component level info, like selected filters in a product section, preferred font for typing in a input box etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tip: Never use your state for holding and calculating business logic or app logic, it should only contain UI logic.
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F975741sqe5s4uyb60qqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F975741sqe5s4uyb60qqu.png" alt="Venn Diagram of states" width="800" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And in the end, would like to quote, Dan Abramov &lt;br&gt;
"Do what is less awkward"&lt;br&gt;
&lt;a href="https://github.com/reduxjs/redux/issues/1287#issuecomment-175351978"&gt;Chatting in a github issue&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>flutter</category>
      <category>webdev</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Lights, Camera, Encode</title>
      <dc:creator>ishar19</dc:creator>
      <pubDate>Sun, 19 May 2024 07:52:23 +0000</pubDate>
      <link>https://dev.to/ishar19/lights-camera-encode-3mc3</link>
      <guid>https://dev.to/ishar19/lights-camera-encode-3mc3</guid>
      <description>&lt;p&gt;Storing images have always been a trivial and omnipresent task while building out any application. What if i tell you there is a way you can store images without any storage services like AWS, Cloudinary or anything else like firebase buckets.&lt;/p&gt;

&lt;p&gt;Get back to the only truth in computer science, everything is 0,1 i.e, binary data in the end, leveraging this we can store images as text format directly. Base64 is a way of encoding binary data (like an image file) into a text format using only printable ASCII characters. It is used to transmit or store binary data over mediums that can only handle text content.&lt;/p&gt;

&lt;p&gt;A bit more of detail before we see how to do it if you’re a programmer or even if not one.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Binary Data&lt;/strong&gt;: Digital files like images, videos, audio, etc. are stored as binary data, which are sequences of 0s and 1s. Binary data cannot be directly transmitted over text-based protocols like HTTP, email, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ASCII Characters&lt;/strong&gt;: ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns numbers to printable and non-printable characters. ASCII uses 7 bits, allowing for 128 different characters, including letters, digits, punctuation marks, and control characters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base64 Encoding&lt;/strong&gt;: Base64 is a way to represent binary data using only the printable ASCII characters. It works by taking the binary data and dividing it into groups of 6 bits (since 6 bits can represent 64 different values), then mapping each 6-bit group to a printable ASCII character from the Base64 character set.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Base64 character set consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;26 uppercase letters (A-Z)&lt;/li&gt;
&lt;li&gt;26 lowercase letters (a-z)&lt;/li&gt;
&lt;li&gt;10 digits (0-9)&lt;/li&gt;
&lt;li&gt;2 additional characters (+ and /)&lt;/li&gt;
&lt;li&gt;An equal sign (=) is used for padding at the end if needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s see in action, an image like &lt;a href="https://unsplash.com/photos/a-white-and-red-robot-standing-on-top-of-a-black-background-7Bm7Lhets9Q"&gt;Cool Robot&lt;/a&gt; will convert into base64 and look like &lt;a href="https://gist.github.com/ishar19/032819af64f7175c7ce9626fd3bfff7e"&gt;String&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But why would I ever do this, what are the advantages?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fewer HTTP Requests&lt;/strong&gt;: When images are embedded as base64 data directly into HTML/CSS/JS files, it eliminates the need for separate HTTP requests to fetch those image files. This can improve page load times, especially for websites with many small images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity&lt;/strong&gt;: Having the image data encoded and inlined within the same file can make development and deployment simpler, as there are fewer external dependencies to manage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching&lt;/strong&gt;: Since the base64 data is part of the document itself, it can benefit from browser caching for that document, reducing the need to re-download the image data on subsequent page loads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Source of Truth&lt;/strong&gt;: Embedding images as base64 data ensures that the image data is tightly coupled with the code that uses it, reducing the chances of broken image links or missing files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data URI Support&lt;/strong&gt;: Base64 encoding allows images to be represented as Data URIs, which can be useful in certain scenarios, such as generating dynamic images on the fly or inlining small images directly into CSS styles.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s cool, but how to do this, how do I even convert an image into a string; converting is very simple and it doesn’t even need anything extra, no packages or software&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For Windows: You can use the &lt;code&gt;certutil&lt;/code&gt; command. Open the Command Prompt and run
&lt;code&gt;certutil -encodehex -f "input.jpg" "output.txt" 0x40000001 1&amp;gt;nul&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For macOS and Linux: You can use the &lt;code&gt;base64&lt;/code&gt; command. Open the Terminal and run &lt;code&gt;openssl base64 -in [input-file] -out [output-file]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, but let’s say I want to do it in my web-app, I am finally convinced to store my images as a string, how do I do it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readFileSync&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="s2"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;proc&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="s2"&gt;child_process&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pbcopy&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;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path-to-image&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;base64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;base64&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;convertedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`data:image/jpeg;base64,&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This all seems too good to be true, what’s the catch?&lt;br&gt;
There are some like, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increased File Size&lt;/strong&gt;: Base64 encoding increases the file size by about 33% compared to the original binary data, which can impact initial page load times for larger images or sites with many images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited Browser Caching&lt;/strong&gt;: While the base64 data itself benefits from document caching, it doesn't take advantage of more advanced browser caching mechanisms for individual image files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging Complexity&lt;/strong&gt;: Having images embedded as base64 data can make debugging and maintaining the code more complex, as the image data is mixed with the code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource Limits&lt;/strong&gt;: Some older browsers or environments may have limits on the maximum size of data URIs or inlined base64 data, which could cause issues with larger images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessibility Concerns&lt;/strong&gt;: Inlining images as base64 data can make it more difficult for screen readers and other assistive technologies to interpret the image content properly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, when do we use base64 or when do we store images, well, that’s an open ended question and upto you to decide :)     &lt;/p&gt;

&lt;p&gt;But for the sake of providing an answer, i’ll be brave enough and say &lt;br&gt;
”Storing images as base64 can be beneficial for small images or websites with a limited number of images, where the reduction in HTTP requests outweighs the increase in file size. However, for larger images or image-heavy websites, it's often better to serve images as separate files and leverage browser caching and compression mechanisms for improved performance.”&lt;/p&gt;

&lt;p&gt;See you in the next one.&lt;/p&gt;

</description>
      <category>node</category>
      <category>macos</category>
      <category>windows</category>
      <category>development</category>
    </item>
    <item>
      <title>Journey to the centre of CPU: Building an ALU</title>
      <dc:creator>ishar19</dc:creator>
      <pubDate>Sun, 05 May 2024 19:48:10 +0000</pubDate>
      <link>https://dev.to/ishar19/journey-to-the-centre-of-cpu-building-an-alu-1dd7</link>
      <guid>https://dev.to/ishar19/journey-to-the-centre-of-cpu-building-an-alu-1dd7</guid>
      <description>&lt;p&gt;Ever wondered how your cpu performs an operation as simple as multiplication or comparing two numbers and deciding which is greater.&lt;br&gt;
We all know everything deep inside is binary and anything that happens in cpu is done with binary gates such as AND, OR, NOT, XOR etc.&lt;/p&gt;

&lt;p&gt;Today we will discuss and see how a cpu can build an ALU to compare two numbers.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is an ALU?
&lt;/h3&gt;

&lt;p&gt;Think of an ALU as a combination of several primitive logic gates combined to perform one specific action.&lt;/p&gt;

&lt;p&gt;It’s like assembling simple pieces in IKEA to build the furniture you want or assembling whatever sauces or veggies you like to create the subway you like.&lt;/p&gt;
&lt;h3&gt;
  
  
  let’s start with basics
&lt;/h3&gt;

&lt;p&gt;AND - an operation which decides whether both the input are 1 or one of them is 0&lt;/p&gt;

&lt;p&gt;OR - an operation which decides if either of the input is 1&lt;/p&gt;

&lt;p&gt;NOT - an operation which negates the input (convert 0 to 1 and convert 1 to 0)&lt;/p&gt;

&lt;p&gt;NAND - an operation which negates the output after performing AND operation &lt;/p&gt;

&lt;p&gt;NOR - an operation which negates the output after performing OR operation &lt;/p&gt;

&lt;p&gt;XOR - an operation which decides whether both the inputs are equal&lt;/p&gt;

&lt;p&gt;Using these gates, one can build any combination which can perform almost any digital or arithmetic logic, in fact all the gates or operation can just be achieved with combination of NAND gates.&lt;/p&gt;
&lt;h3&gt;
  
  
  Let’s try building a simple operation unit which adds two bits and return us carry and sum
&lt;/h3&gt;

&lt;p&gt;We have two numbers a and b both representing one single bit either 0 and 1.&lt;/p&gt;

&lt;p&gt;In order to add two binary bits, we can’t expect cpu to just “add” like human, we have to explicitly define the operations that we need, for this let us list down all the possible cases and think of an operation which can be used to achieve the result.&lt;/p&gt;

&lt;p&gt;Possible outcomes are&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;a&lt;/th&gt;
&lt;th&gt;b&lt;/th&gt;
&lt;th&gt;carry&lt;/th&gt;
&lt;th&gt;sum&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;looking closely, we see sum is 1 when inputs are different and 0 when input it same, going back to basics, XOR gate emulates this behaviour, okay, so sum can be achieved by using XOR over both the inputs.&lt;/p&gt;

&lt;p&gt;What about carry?&lt;br&gt;
Notice how it is 1 only when both the inputs are 1, that means, drumroll, 🥁 🥁, AND gate.&lt;/p&gt;

&lt;p&gt;YES, now we have a gate which can add two inputs and give us output, and this is how you build an ALU.&lt;br&gt;
Combining different gates to achieve one specific function can be an example of a small ALU unit under large complex ALUs.&lt;br&gt;
Typically standard ALUs contains more complex and bunch of different operations based on the inputs and several flags used to manipulate the inputs and outputs.&lt;/p&gt;
&lt;h3&gt;
  
  
  An ALU which compares two numbers are tells us which is bigger
&lt;/h3&gt;

&lt;p&gt;Defining the input and output flags  &lt;/p&gt;

&lt;p&gt;Inputs - a, b&lt;/p&gt;

&lt;p&gt;Possible flags - we will have three flags, x, y, z and depending on their values we will decide whether they are equal or which is greater.&lt;/p&gt;

&lt;p&gt;x is 1 when a=b&lt;/p&gt;

&lt;p&gt;y is 1when a&amp;lt;b&lt;/p&gt;

&lt;p&gt;z is 1 when a&amp;gt;b&lt;/p&gt;

&lt;p&gt;(NOT (a XOR b)) will return 1 when a is equal b&lt;/p&gt;

&lt;p&gt;(a AND (NOT b)) will return 1 when 1 a is greater than b&lt;/p&gt;

&lt;p&gt;((NOT a) AND b) will return 1 when b is greater than a&lt;/p&gt;

&lt;p&gt;We will combine these gates and put into one unit, and map the output to different lines, x, y, z and whichever lights the bulb will tell us the about the comparison.&lt;/p&gt;

&lt;p&gt;A more elaborative diagram of to compare 4 bit inputs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;                     &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="err"&gt;──────┐&lt;/span&gt;
                                &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="err"&gt;──────┘──&lt;/span&gt;&lt;span class="nx"&gt;XOR&lt;/span&gt;&lt;span class="err"&gt;──┐&lt;/span&gt;
                                        &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="err"&gt;──────┐&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;
                                &lt;span class="err"&gt;│&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="err"&gt;──────┘──&lt;/span&gt;&lt;span class="nx"&gt;XOR&lt;/span&gt;&lt;span class="err"&gt;──┴──&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="err"&gt;──┐&lt;/span&gt;
                                        &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&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="err"&gt;──────┐&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                                &lt;span class="err"&gt;│&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;B&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="err"&gt;──────┘──&lt;/span&gt;&lt;span class="nx"&gt;XOR&lt;/span&gt;&lt;span class="err"&gt;──┴──&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="err"&gt;──┴──&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="err"&gt;──&lt;/span&gt;&lt;span class="nx"&gt;A_EQ_B&lt;/span&gt;
                                        &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;A&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="err"&gt;──────┐&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                                &lt;span class="err"&gt;│&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;B&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="err"&gt;──────┘──&lt;/span&gt;&lt;span class="nx"&gt;XOR&lt;/span&gt;&lt;span class="err"&gt;──┴──&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="err"&gt;──┘&lt;/span&gt;

                     &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="err"&gt;──────┐&lt;/span&gt;
                                &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="err"&gt;──────┘──&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="err"&gt;──┐&lt;/span&gt;
                                        &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="err"&gt;──────┐&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;
                                &lt;span class="err"&gt;│&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="err"&gt;──────┘──&lt;/span&gt;&lt;span class="nx"&gt;XOR&lt;/span&gt;&lt;span class="err"&gt;──┼──&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="err"&gt;──┐&lt;/span&gt;
                                        &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&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="err"&gt;──────┐&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                                &lt;span class="err"&gt;│&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;B&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="err"&gt;──────┘──&lt;/span&gt;&lt;span class="nx"&gt;XOR&lt;/span&gt;&lt;span class="err"&gt;──┼──&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="err"&gt;──┼──&lt;/span&gt;&lt;span class="nx"&gt;OR&lt;/span&gt;&lt;span class="err"&gt;──&lt;/span&gt;&lt;span class="nx"&gt;A_GT_B&lt;/span&gt;
                                        &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;A&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="err"&gt;──────┐&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                                &lt;span class="err"&gt;│&lt;/span&gt;       &lt;span class="err"&gt;│&lt;/span&gt;      &lt;span class="err"&gt;│&lt;/span&gt;
                     &lt;span class="nx"&gt;B&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="err"&gt;──────┘──&lt;/span&gt;&lt;span class="nx"&gt;XOR&lt;/span&gt;&lt;span class="err"&gt;──┼──&lt;/span&gt;&lt;span class="nx"&gt;AND&lt;/span&gt;&lt;span class="err"&gt;──┘&lt;/span&gt;
                                        &lt;span class="err"&gt;│&lt;/span&gt;
                                &lt;span class="nx"&gt;A_EQ_B&lt;/span&gt;&lt;span class="err"&gt;──┘──&lt;/span&gt;&lt;span class="nx"&gt;NOT&lt;/span&gt;&lt;span class="err"&gt;──&lt;/span&gt;&lt;span class="nx"&gt;A_LT_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we observe, it is just smaller 1 bit comparators combined to build a bigger one and that’s how an ALU is made.&lt;/p&gt;

&lt;p&gt;Literally like lego pieces.&lt;/p&gt;

&lt;p&gt;In real world, ALUs are more complex but fundamentals remain the same.&lt;/p&gt;

&lt;p&gt;CPUs are fascinating piece of tech and in later parts of these posts, we will uncover how RAMs are made.&lt;/p&gt;

&lt;p&gt;Until then, keep processing.&lt;/p&gt;

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

</description>
      <category>cpu</category>
      <category>hardware</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Need for Speed: nextjs drift</title>
      <dc:creator>ishar19</dc:creator>
      <pubDate>Sat, 27 Apr 2024 22:53:30 +0000</pubDate>
      <link>https://dev.to/ishar19/need-for-speed-nextjs-drift-4c57</link>
      <guid>https://dev.to/ishar19/need-for-speed-nextjs-drift-4c57</guid>
      <description>&lt;h3&gt;
  
  
  From
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgw509tb0qxf2nanabcub.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgw509tb0qxf2nanabcub.png" alt="Before" width="800" height="1724"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  To
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7yu8ps42wj1yyyaiajc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7yu8ps42wj1yyyaiajc3.png" alt="After" width="800" height="1503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How did we come here? What were we using earlier and what we switched to?&lt;/p&gt;

&lt;p&gt;Both the screenshots are from the same website &lt;a href="https://www.fourcore.io"&gt;https://www.fourcore.io&lt;/a&gt; before and after the rewrite.&lt;/p&gt;

&lt;p&gt;Earlier we were using nextjs 12 and after switching to 14 we made the page load time 3x faster and improved almost every statistics of speed, performance and accessibilty.&lt;/p&gt;

&lt;p&gt;How did we do it?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reducing the number of dependencies, we removed almost every unnecessary dependency and uses only framer. tailwind and next-mdx.&lt;/li&gt;
&lt;li&gt;Rendering everything on server as either static content or static HTML which means it’s pre-rendered and client doesn’t have to load any data.&lt;/li&gt;
&lt;li&gt;Use client component in composition instead of making the whole page as a client component so that only that part that needs to be hydrated stays as client component and everything else renders on server.&lt;/li&gt;
&lt;li&gt;If data is not be mutated or revalidated, use SSG; even if there are mutations to be done, using server actions will make it faster instead of whole page reload.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have more than 30 mdx pages, rendering on server on build, which makes the loading faster and adding SEO is much easier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Speeding up a page load and first contentful paint improves the retention and interaction on the page driving better user experience and more click on CTA. ;)&lt;/p&gt;

&lt;p&gt;Spend time and remove whatever extra js bundle you have and make use of new nextjs feature to make your website&lt;br&gt;
BLAZINGLY, BLEEDING EDGE FAST /s.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Fullstack alchemy with nextjs and ffmpeg: convert video to audio version</title>
      <dc:creator>ishar19</dc:creator>
      <pubDate>Sun, 21 Apr 2024 08:04:53 +0000</pubDate>
      <link>https://dev.to/ishar19/fullstack-alchemy-with-nextjs-and-ffmpeg-convert-video-to-audio-version-2n23</link>
      <guid>https://dev.to/ishar19/fullstack-alchemy-with-nextjs-and-ffmpeg-convert-video-to-audio-version-2n23</guid>
      <description>&lt;p&gt;Let’s build a simple nextjs app which will convert video to audio files and downloads it.&lt;br&gt;
No extra backend, VMs or server, everything running on nextjs and can be easily deployed on vercel.&lt;/p&gt;
&lt;h3&gt;
  
  
  Background Story of our main elements
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Nextjs - A backend (yes, backend) framework developed by vercel which ships react (kinda not exactly) to the browser with so many rendering strategies, some good, some more confusing than F1’s ferrari team strategy. Summing up, really cool tool if you want to move fast and ship production grade full-stack apps. It comes with in-built support for creating apis which runs on server environment for your client without the extra effort for set backend deployment or those docker containers or lambdas; they somehow mimic ec2 behaviour though.&lt;/li&gt;
&lt;li&gt;FFMPEG - Beast tool, probably one of the biggest gigachad stuff available on the internet. Officiaily they go with - &lt;strong&gt;A complete, cross-platform solution to record, convert and stream audio and video.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Let’s begin; Try to follow along
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a new nextjs app by
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;npx&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;latest&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;you can opt for customisation
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;What&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;your&lt;/span&gt; &lt;span class="nx"&gt;project&lt;/span&gt; &lt;span class="nx"&gt;named&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;
&lt;span class="nx"&gt;Would&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;TypeScript&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;No&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Yes&lt;/span&gt;
&lt;span class="nx"&gt;Would&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;ESLint&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;No&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Yes&lt;/span&gt;
&lt;span class="nx"&gt;Would&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;Tailwind&lt;/span&gt; &lt;span class="nx"&gt;CSS&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;No&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Yes&lt;/span&gt;
&lt;span class="nx"&gt;Would&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="s2"&gt;`src/`&lt;/span&gt; &lt;span class="nx"&gt;directory&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;No&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Yes&lt;/span&gt;
&lt;span class="nx"&gt;Would&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recommended&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;No&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Yes&lt;/span&gt;
&lt;span class="nx"&gt;Would&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;customize&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nf"&gt;alias &lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="cm"&gt;/*)? No / Yes
What import alias would you like configured? @/*
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;For using ffmpeg inside our app, we will use fluent-ffmpeg which makes it easier to create a ffmpeg command in a nodejs like module. They say this on their page 
This library abstracts the complex command-line usage of ffmpeg into a fluent, easy to use node.js module. 
&lt;a href="https://www.npmjs.com/package/fluent-ffmpeg"&gt;https://www.npmjs.com/package/fluent-ffmpeg&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;We will also be using multer for this for uploading a file. You can download multer from 
&lt;a href="https://www.npmjs.com/package/multer"&gt;https://www.npmjs.com/package/multer&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Create an api folder inside app directory and create a folder named convertAudio.ts. Paste the following code there
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;multer&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;multer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&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;exec&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="s2"&gt;child_process&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&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;promises&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;fs&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="s2"&gt;fs&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;upload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;multer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;dest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/tmp&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; 

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async &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="k"&gt;if &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;method&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;convertAudio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;outputFormat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&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="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;outputPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`/tmp/output.&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;outputFormat&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`ffmpeg -i &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;error&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outputPath&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="p"&gt;};&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fileHandlingMiddleware&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;upload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;single&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;audioFile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;fileHandlingMiddleware&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="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error uploading file.&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="k"&gt;try&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;convertedFilePath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;convertAudio&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;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mp3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Convert to mp3 for this example&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fileBuffer&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;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;convertedFilePath&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-Disposition&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;attachment; filename=converted.mp3&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;audio/mp3&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;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fileBuffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Cleanup temp files&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unlink&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;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unlink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;convertedFilePath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&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;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error converting audio. Some problem occured&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="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;405&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Only POST requests are allowed.&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="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;


&lt;p&gt;Let’s understand this code briefly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`ffmpeg -i &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;error&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outputPath&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;This will create a ffmpeg command taking original file path and an output path to store the audio file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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-Disposition&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;attachment; filename=converted.mp3&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;audio/mp3&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;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fileBuffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This set necessary headers ("Content-Disposition",) to the response object to tell the browser to directly download the file with the name ("attachment; filename={file_name}").&lt;/p&gt;

&lt;p&gt;That’s it, running your nextjs app will make this api available at /api/convertAudio for your nextjs app which you can directly use with a post request.&lt;/p&gt;

&lt;p&gt;Note: This will only work for form-type multipart/form-data since we are using multer.&lt;/p&gt;

&lt;p&gt;No need to setup an environment, directly deploy it to vercel and it will work fine.&lt;/p&gt;

&lt;p&gt;That’s one the best part of nextjs is to remove the line or atleast blur between frontend and backend.&lt;/p&gt;

&lt;p&gt;Thanks for reading 😄&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Tech I am learning in 2024</title>
      <dc:creator>ishar19</dc:creator>
      <pubDate>Sat, 13 Apr 2024 05:27:53 +0000</pubDate>
      <link>https://dev.to/ishar19/tech-i-am-learning-in-2024-3pi9</link>
      <guid>https://dev.to/ishar19/tech-i-am-learning-in-2024-3pi9</guid>
      <description>&lt;p&gt;Starting of 2024, I decided to learn some other technologies which is not JS, as I am particularly comfortable with JS, I wanted to explore and learn more.&lt;/p&gt;

&lt;p&gt;Here are 5 pieces of technology I am learning this year&lt;/p&gt;

&lt;h2&gt;
  
  
  Golang
&lt;/h2&gt;

&lt;p&gt;Coming from JS land, Go feels like a breeze of air with it’s error handling and particulary neat syntax and well crafted conventions about structuring code.&lt;/p&gt;

&lt;p&gt;My curiosity of discovering about what goes on scalable backend system push me towards backend, moreover I wanna tap into that concurrency shaolin chamber everyone’s been talking about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operating System
&lt;/h2&gt;

&lt;p&gt;Thanks to &lt;a href="http://cpu.land"&gt;cpu.land&lt;/a&gt; , I grew an uncanny interest in how cpu works and got me deep into operating systems from there, it’s fascinating to see the deep down of your computer to the levels of registers and ALUs(arithmetic logical units). Nand2tetris and OSTEP is the greatest resource for starting with operating systems.&lt;/p&gt;

&lt;p&gt;During this, it will force me to tackle C and Assembly in the process, hope i survive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Typescript
&lt;/h2&gt;

&lt;p&gt;Been deep into JS land for more than an year and still not using types extensively, being fed up of my dart and golang friends teasing me about types, so here is me finding my “type” ;&lt;/p&gt;

&lt;p&gt;I will finally be type complete, the holy grail of javascript ecosystem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Networking and Devops
&lt;/h2&gt;

&lt;p&gt;Enough of mugging up 7 OSI layers and not understanding what any of this means, here is me trying to become a bash king and terminal wizard. Gotta deploy those docker containers so that it doesn’t work on only my computer but everywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning my IDE
&lt;/h2&gt;

&lt;p&gt;It’s never bad time to brush-up some hotkeys and be one with your ide become neo of the matrix.&lt;/p&gt;

&lt;p&gt;I use VSCode and have no intentions of switching to anything in near future.&lt;/p&gt;

&lt;p&gt;That’s all, that’s how i am spending my 2024. Beating the FOMO of AI stuff was tough in this but let me take a chance on what i find interesting.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The curious case of HTML forms</title>
      <dc:creator>ishar19</dc:creator>
      <pubDate>Mon, 05 Sep 2022 21:11:14 +0000</pubDate>
      <link>https://dev.to/ishar19/the-curious-case-of-html-forms-3iak</link>
      <guid>https://dev.to/ishar19/the-curious-case-of-html-forms-3iak</guid>
      <description>&lt;p&gt;&lt;strong&gt;Forms are an inevitable part of any application if we are making an application that’s interactive and relies on user inputs.&lt;br&gt;
And as the sensitivity of data grows, handling forms becomes critical.&lt;br&gt;
So let’s talk about HTML forms.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"https://www.something.com"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"get"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;A basic structure of HTML form&lt;/strong&gt;&lt;br&gt;
The action attribute of the FORM element defines where to send the form data, and the method attribute specifies the HTTP method(i.e, GET, POST) for sending the form data.&lt;br&gt;
The URL inside the action would probably be an API endpoint but if it is an URL and method is get, chances are you would be redirected to the page. Go on, try running above code and you will be redirected to &lt;a href="https://www.something.com"&gt;https://www.something.com&lt;/a&gt; which (funny enough) is an actual website with “something”, quite literally.&lt;/p&gt;

&lt;p&gt;One less harmful use of this could be instead of using links, usage of form buttons for redirection so user wont know where they are going to be redirected by just hovering.(PS:- this is not best a11y(accessibility) practice but there could be some use cases)&lt;/p&gt;

&lt;p&gt;Major risk of doing forms like this inside HTML could make your website vulnerable to an attack called XSS (Cross Site Scripting) which are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user.&lt;/p&gt;

&lt;p&gt;One of the biggest e-commerce website E-bay fell prey to this kind of attack numerous of times in the past and to this day XSS attacks are quite common.&lt;/p&gt;

&lt;p&gt;(Explanation of XSS is quite outside of the scope of this article, for further reading, you can visit &lt;/p&gt;

&lt;p&gt;&lt;a href="https://brightsec.com/blog/xss-attack/"&gt;https://brightsec.com/blog/xss-attack/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Tip: Always verify the URL of the page if you are putting in any personal information.&lt;/p&gt;

&lt;p&gt;Above Example was provided to point out the importance of validation of any type of user input taken in a website.&lt;/p&gt;
&lt;h2&gt;
  
  
  Validation
&lt;/h2&gt;

&lt;p&gt;HTML provides a way to validate the data provided to it’s input elements&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"fname"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;First name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"fname"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"fname"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Age:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above code will validate that first name provided is in text format and age should be a number and both are required if user wants to submit the form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"uname"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"uname"&lt;/span&gt; &lt;span class="na"&gt;pattern=&lt;/span&gt;&lt;span class="s"&gt;"[a-zA-Z0-9]+"&lt;/span&gt; &lt;span class="na"&gt;minlength=&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt; &lt;span class="na"&gt;maxlength=&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, above code will validate that text only contains letters from a-z A-Z and from 0-9, hence invalidating other inputs. This may seem like a valid solution and to an extent it is, but writing strong reg-ex could be very tiresome and another issue could be posting data directly to backend using methods like &lt;em&gt;curl&lt;/em&gt; or &lt;em&gt;wget&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Hence validating data sent on backend is also important and there are tools like joi and strip-js for that.&lt;/p&gt;

&lt;p&gt;Okay, this was all about validating but how do we submit?&lt;/p&gt;

&lt;p&gt;Well you can submit your form using a submit type button and execute some code using onsubmit event provided with forms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;onsubmit&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"https://api.server.com"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt; &lt;span class="na"&gt;onsubmit=&lt;/span&gt;&lt;span class="s"&gt;"someFunction()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When users submit the form using the submit button, first the function provided to onsubmit executes and if everything goes well, the form is submitted.&lt;br&gt;
But one very irritating part you would notice is that page will reload everytime user tries to submit and hence creating a very bad user experience.&lt;/p&gt;

&lt;p&gt;So how do we solve it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the event object,&lt;/strong&gt;&lt;br&gt;
onsubmit gives us access to an event object, which can be used to used to do various things like preventing default action, accessing data on which the event was occurred and much more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;someFunction&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"https://api.server.com"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt; &lt;span class="na"&gt;onsubmit=&lt;/span&gt;&lt;span class="s"&gt;"someFunction(e)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will prevent the page from reloading but here’s the interesting thing, it also prevents the form from submitting, because contrary to a beginner misconception, what preventDefault does is that it prevents the default behaviour of an event rather than just preventing the reload and submitting is also a default behaviour of onsubmit.&lt;/p&gt;

&lt;p&gt;So what do we do,&lt;/p&gt;

&lt;p&gt;use the best programming language for your rescue, JavaScript&lt;/p&gt;

&lt;p&gt;Take the formData and the action URL and post it to your server using &lt;strong&gt;The Fetch API&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleFormSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&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;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTarget&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;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;try&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;formData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FormData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;form&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;responseData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;postFormDataAsJson&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;formData&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="nx"&gt;responseData&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;(According to MDN, The Fetch API provides an interface for fetching resources (including across the network), more about Fetch and APIs later someday)&lt;/p&gt;

&lt;p&gt;Above code might look unfamiliar if you are new to Javascript, you can go read about Fetch, try-catch and every other thing on The MDN.&lt;/p&gt;

&lt;p&gt;By no means this article can act like a comprehensive guide to forms and submission, this was just to show some pitfalls and ways to avoid it. Why validation is important and why learning about fetch and ways to validate data is important.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion: Developers should really pay attention about forms and user inputs and it’s validation.&lt;br&gt;
How javascript solves an essential problem by letting us access the Fetch API (which by the way again, contrary to mass-misconception is not a part of javascript rather a browser API.)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But thats all functionality, what about styling, after all it also has to look good&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Well, CSS provides a lot of ways to style a form and in some amazing ways which we will look in some another article, till then Ciao.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>forms</category>
      <category>javascript</category>
      <category>xss</category>
    </item>
  </channel>
</rss>
