<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Node.JS &#8211; MeghRaj TechnoSoft &#8211; Web, App, E-commerce Development  Agency In India.</title>
	<atom:link href="https://www.meghrajtechnosoft.com/node-js/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.meghrajtechnosoft.com</link>
	<description></description>
	<lastBuildDate>Tue, 08 Dec 2020 13:30:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.5.15</generator>

<image>
	<url>https://www.meghrajtechnosoft.com/wp-content/uploads/2020/10/fevecon.png</url>
	<title>Node.JS &#8211; MeghRaj TechnoSoft &#8211; Web, App, E-commerce Development  Agency In India.</title>
	<link>https://www.meghrajtechnosoft.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Angular — Fallback for Broken Images</title>
		<link>https://www.meghrajtechnosoft.com/angular-fallback-for-broken-images/</link>
					<comments>https://www.meghrajtechnosoft.com/angular-fallback-for-broken-images/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 08 Dec 2020 13:19:17 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Node.JS]]></category>
		<guid isPermaLink="false">https://www.meghrajtechnosoft.com/?p=17550</guid>

					<description><![CDATA[<p>If you have worked with images on websites then you must have run into the common problem where the image doesn’t load for some reason. Maybe the link is broken, or the link might be an empty string/null/undefined. The image [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.meghrajtechnosoft.com/angular-fallback-for-broken-images/">Angular — Fallback for Broken Images</a> appeared first on <a rel="nofollow" href="https://www.meghrajtechnosoft.com">MeghRaj TechnoSoft - Web, App, E-commerce Development  Agency In India.</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you have worked with images on websites then you must have run into the common problem where the image doesn’t load for some reason. Maybe the link is broken, or the link might be an empty string/null/undefined. The image fails to load and you see something like this:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="700" height="250" src="https://www.meghrajtechnosoft.com/wp-content/uploads/2020/12/1_pOvzhe0JaYBd6yLooq2fzA.png" alt="" class="wp-image-17535" srcset="https://www.meghrajtechnosoft.com/wp-content/uploads/2020/12/1_pOvzhe0JaYBd6yLooq2fzA.png 700w, https://www.meghrajtechnosoft.com/wp-content/uploads/2020/12/1_pOvzhe0JaYBd6yLooq2fzA-300x107.png 300w" sizes="(max-width: 700px) 100vw, 700px" /></figure>



<p id="5735">This is a very unpleasant sight for the user. In order to work around this in Angular 2+, you have a variety of solutions. The solution I prefer is to create a directive for the&nbsp;<code>&lt;img&gt;</code>&nbsp;elements on the page. If the image fails to load I show a default image that i pass in the element as a parameter. So i can show different default images in different parts of my website if I need to.</p>



<p id="587c">Note that this is for Angular 2+. Directives in Angular 1.x.x are entirely different. In general, directives are used to change the default behavior of an element in Angular 2+.</p>



<p id="5e21">So coming back to the subject, I created an&nbsp;<code>ImagePreload</code>&nbsp;Directive:</p>



<pre><code>import {Directive, Input, HostBinding} from '@angular/core'
@Directive({
    selector: 'img[default]',
    host: {
      '(error)':'updateUrl()',
      '(load)': 'load()',
      '[src]':'src'
     }
  })
  
 export class ImagePreloadDirective {
    @Input() src:string;
    @Input() default:string;
    @HostBinding('class') className
  
    updateUrl() {
      this.src = this.default;
    }
    load(){
      this.className = 'image-loaded';
    }
  }</code></pre>



<p id="ca7b">In the decorator, I have a selector property and a host property. In the selector property I tell the Directive to apply on every element that matches the css selector&nbsp;<code>img[default]</code>. I write&nbsp;<code>[default]</code>&nbsp;here because I will give an attribute&nbsp;<strong>default</strong>&nbsp;to the&nbsp;<code>&lt;img&gt;</code>&nbsp;element next.</p>



<p id="2a4d">The host property gives me access to the default attributes on this element such as&nbsp;<code>error</code>,&nbsp;<code>load</code>&nbsp;and&nbsp;<code>src</code>. I bind them to my methods and properties on my&nbsp;<code>ImagePreloadDirective</code>&nbsp;class. So it means that when the default error event on the&nbsp;<code>&lt;img&gt;</code>&nbsp;element is invoked, my&nbsp;<code>updateUrl()</code>&nbsp;method will run too. Same goes for the&nbsp;<code>load</code>&nbsp;event. The&nbsp;<code>[src]</code>&nbsp;is not an event. Its an attribute on every&nbsp;<code>&lt;img&gt;</code>&nbsp;element. Here I’m just saying that my&nbsp;<code>src</code>&nbsp;property in my class should be bound to the<code>&nbsp;&lt;img&gt;</code>&nbsp;element’s src property. So if I change my property, it should change the element’s property. It might sound confusing but it’s extremely simple when you get the hang of it.</p>



<p id="7620">Moving on into the class, we have an 2 inputs to the directive&nbsp;<code>src</code>&nbsp;and&nbsp;<code>default</code>&nbsp;. The third one is a HostBinding. It lets you set properties on the element or component that hosts the directive, in this case&nbsp;<code>img[default]</code>&nbsp;.</p>



<p id="07cf">Our &lt;img&gt; element in the html will now look like this:</p>



<pre><code><img src="test.com/img-1" default="test.com/default-img" class="img"></code></pre>



<p id="c3b4">So let’s say that the url&nbsp;<code>test.com/img-1</code>&nbsp;is broken for some reason, the error event on the&nbsp;<code>&lt;img&gt;</code>&nbsp;element will fire. This in turn will fire our&nbsp;<code>updateURL()</code>&nbsp;method that will set the src attribute of the&nbsp;<code>&lt;img&gt;</code>&nbsp;element to the default url that we provided to the element.<br>In the event that the url&nbsp;<code>test.com/img-1&nbsp;</code>loads fine, the load event on the&nbsp;<code>&lt;img&gt;</code>&nbsp;element will fire. It will fire our&nbsp;<code>load()</code>&nbsp;event and it will add a class ‘image-loaded’ to the<code>&nbsp;&lt;img&gt;</code>&nbsp;element. You can use this class to show or hide loading-spinners/image-loading-overlays.</p>



<p id="7df1">So after you use this method, your website will look like something like this:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="700" height="339" src="https://www.meghrajtechnosoft.com/wp-content/uploads/2020/12/1_VMTmehKylNGyKKpY6Nqu4w.png" alt="" class="wp-image-17536" srcset="https://www.meghrajtechnosoft.com/wp-content/uploads/2020/12/1_VMTmehKylNGyKKpY6Nqu4w.png 700w, https://www.meghrajtechnosoft.com/wp-content/uploads/2020/12/1_VMTmehKylNGyKKpY6Nqu4w-300x145.png 300w" sizes="(max-width: 700px) 100vw, 700px" /></figure>



<p id="6dbe">This is all you need to setup default image loading on your images. I like this approach because its short and simple. You can extend the functionality in the directive if you need. It only affects the elements you want and not all the image elements. It also allows you to show different fallback images for different&nbsp;<code>&lt;img&gt;&nbsp;</code>elements.</p>



<p id="38ec">Hope this short article helped you improving the look of your website. Cheers!</p>



<p></p>
<p>The post <a rel="nofollow" href="https://www.meghrajtechnosoft.com/angular-fallback-for-broken-images/">Angular — Fallback for Broken Images</a> appeared first on <a rel="nofollow" href="https://www.meghrajtechnosoft.com">MeghRaj TechnoSoft - Web, App, E-commerce Development  Agency In India.</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.meghrajtechnosoft.com/angular-fallback-for-broken-images/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>What does unsafe-perm in npm actually do?</title>
		<link>https://www.meghrajtechnosoft.com/what-does-unsafe-perm-in-npm-actually-do/</link>
					<comments>https://www.meghrajtechnosoft.com/what-does-unsafe-perm-in-npm-actually-do/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 07 Dec 2020 08:20:04 +0000</pubDate>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[Node.JS]]></category>
		<category><![CDATA[NPM]]></category>
		<guid isPermaLink="false">https://www.meghrajtechnosoft.com/?p=17487</guid>

					<description><![CDATA[<p>I’ve routinely been directed to what feels like a magic fix to issues when installing items over NPM. The issue that I’ve most recently encountered occurs in two different OS’s; Windows and Elementary. When attempting to install&#160;Sindre Sorhus’ Pure ZSH [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.meghrajtechnosoft.com/what-does-unsafe-perm-in-npm-actually-do/">What does unsafe-perm in npm actually do?</a> appeared first on <a rel="nofollow" href="https://www.meghrajtechnosoft.com">MeghRaj TechnoSoft - Web, App, E-commerce Development  Agency In India.</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>I’ve routinely been directed to what feels like a magic fix to issues when installing items over NPM. The issue that I’ve most recently encountered occurs in two different OS’s; Windows and Elementary. When attempting to install&nbsp;<a href="https://github.com/sindresorhus/pure" target="_blank" rel="noreferrer noopener">Sindre Sorhus’ Pure ZSH theme</a>&nbsp;both in Ubuntu WSL and Elementary Loki, I have issues with not having permissions.</p>



<p>The solution seems to be almost the same in both cases.</p>



<pre><code>npm config set unsafe-perm true</code></pre>



<p>What exactly is this doing? Is it ‘unsafe’?</p>



<p>Let’s start with&nbsp;<a href="https://duckduckgo.com/?q=npm+config+set+unsafe-perm+true&amp;t=canonical&amp;ia=qa" target="_blank" rel="noreferrer noopener">a search</a>. So there is a config setting allowed in the&nbsp;<code>package.json</code>&nbsp;that actually can set this per package. But wait,&nbsp;<a href="https://docs.npmjs.com/misc/config" target="_blank" rel="noreferrer noopener">it’s always true unless using root</a>. I never run as&nbsp;<code>root</code>, or with&nbsp;<code>sudo</code>&nbsp;privileges unless I am forced to. Let’s take a look through the&nbsp;<a href="https://github.com/npm/cli" target="_blank" rel="noreferrer noopener">source code</a></p>



<blockquote class="wp-block-quote"><p>Set to true to suppress the UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.</p></blockquote>



<blockquote class="wp-block-quote"><p>If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.</p></blockquote>



<pre><code>function loadUid(cb) {
  // if we're not in unsafe-perm mode, then figure out who
  // to run stuff as.  Do this first, to support `npm update npm -g`
  if (!this.get('unsafe-perm')) {
    getUid(this.get('user'), this.get('group'), cb);
  } else {
    process.nextTick(cb);
  }
}</code></pre>



<p>In other words, if we set&nbsp;<code>unsafe-perm</code>&nbsp;to always true, we will stop getting the user and the group the command was ran as and we will over-load the defaults.</p>



<pre><code>'unsafe-perm': process.platform === 'win32' ||
                     process.platform === 'cygwin' ||
                     !(process.getuid &amp;&amp; process.setuid &amp;&amp;
                       process.getgid &amp;&amp; process.setgid) ||
process.getuid() !== 0,</code></pre>



<p>It’s starting to make a bit more sense. This issue that I’m facing is that the permissions my current user has are unable to create the symlinks that are being asked by the program. By setting&nbsp;<code>unsafe-perm</code>&nbsp;to&nbsp;<code>true</code>&nbsp;will force&nbsp;<code>NPM</code>&nbsp;to attempt to always run within the context of the running script.</p>



<p>I suppose that this isn’t ‘unsafe’ but it does force the package installer to never drop into user and group switching when installing apps. It’s possible then you may end up having the code run as ‘root’ when installing (which could then be considered ‘unsafe’);</p>



<p>This goes all the way back to&nbsp;<a href="https://github.com/npm/cli/commit/021750a5bf0c1024916c0a497763cb2d2da78e46" target="_blank" rel="noreferrer noopener">February 7th, 2011</a>!<br>Initially this would cause the app to either Error out when&nbsp;<code>false</code>&nbsp;or to&nbsp;<a href="https://github.com/npm/cli/commit/52e3e88feb75c1e4c5e05771e2f4e67be27015ab" target="_blank" rel="noreferrer noopener">toggle the user and group</a>.</p>



<p>But finally,&nbsp;<a href="https://github.com/npm/cli/commit/dae3259fd17866c189c5e8b3a7968f515b1af4ca" target="_blank" rel="noreferrer noopener">we reach the reasoning for this code</a>&nbsp;(and still it’s not what many may expect).</p>



<blockquote class="wp-block-quote"><p>You can’t please everyone.</p></blockquote>



<blockquote class="wp-block-quote"><p>Don’t try to be secure if it’ll fail.&nbsp;</p><p>If someone really wants to encourage this all the time, then they can do<br><code>npm config set unsafe-perm false</code>&nbsp;explicitly.&nbsp;</p><p>In the npm 1.0 future, it will probably require sudo for global<br>installing no matter what, and use this behavior for local installing.<br>Since so many people have npm installing in their home dir, requiring<br>sudo is causing more trouble than it’s worth.</p></blockquote>



<p><em>Boom</em></p>



<p>This was added to help installing files into ‘safe’ locations without&nbsp;<code>sudo</code>.<br>So I think that I may have the answer?</p>



<blockquote class="wp-block-quote"><p>Use this sparingly; probably bad to set globally for all running scripts. If the user is running as&nbsp;<code>root</code>&nbsp;it may cause the app to force changes that could extend beyond what a running script should have. Never run as&nbsp;<code>root</code>&nbsp;and it will be&nbsp;<code>true</code>&nbsp;by default. Use&nbsp;<code>sudo</code>&nbsp;otherwise. It’s not a magic bullet.</p></blockquote>
<p>The post <a rel="nofollow" href="https://www.meghrajtechnosoft.com/what-does-unsafe-perm-in-npm-actually-do/">What does unsafe-perm in npm actually do?</a> appeared first on <a rel="nofollow" href="https://www.meghrajtechnosoft.com">MeghRaj TechnoSoft - Web, App, E-commerce Development  Agency In India.</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.meghrajtechnosoft.com/what-does-unsafe-perm-in-npm-actually-do/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
