Web

PowerShell: Check a parameter/variable value is null

June 8, 2018 PowerShell, Scripting No comments

While you are writing PowerShell modules, with lot with parameters and you might want to verify these parameters are not ‘null’ to validate some business cases. In normal powershell inline scripting context, $variablename -eq ā€œ$nullā€ would work :

if ($varibalename -eq $null) 
{ 
Write-Host "variable is null.Please supply the values for variablename." 
}

RECOMMENDED APPROACH:
Efficient way of checking this inside a module is to use:

if (!$variablename) 
{ 
Write-Host "variable is null.Please supply the values for variablename." 
}

If you would want to verify $variablename has any value except $null:

if ($variablename) 
{ 
Write-Host "variablename is not null. do something here." 
}

Node.js 9.x.x and npm 6.x.x – “npm audit” to identify and fix security vulnerabilities in dependencies

June 3, 2018 JavaScript, Javascript Development, Modern Web Development, Node.js, NPM, OpenSource, Package Manager, Tech Newz, TypeScript, Web No comments

nodejs-npm

It has been a while I have been reading about the major changes that areintroduced in Node.js 9.x.x / NPM 6.x.x and myself faced by Node.js application going to a toss after I upgraded to Node.js 9.x.x, as I always keep Node.js up to date in my development environment.

I use NVM(Node Virtual Manager) to switch between different version of Node.js and I love the flexiblity NVM provides. So I was able to quickly switch back to 8.x.x version, when I figured out this change.

But npm packgage downgrade did not work using ā€œnpm install –g npm@5.x.xā€ due to old traces of 6.x.x   I had to clean up my npm cache and do npm install again.

Introduction – The ā€œnpm auditā€ command:

Recently with 6.0.0 NPM team has introduced many improvements such as :

a.) Provide protection against insecure code into the workflow during your npm install . When a user downloads code from the npm Registry, npm will review the request against the Node Security Platform database and return a warning if the code contains a vulnerability.

b.) Package signing for publishers.   npm-signature field will allow users of npm packages to verify the integrity of the package regardless of the tools they use to retrieve it or the registry from which they download it.

c.) Security auditing capability (which I am covering in this article).

The audit capability, which provides an ability to perform a security audit  on your project and dependency components.  To simplify it provides a moment-in-time security review of your project’s dependency tree.

  • It will scan your project for any vulnerabilities. 
  • You can choose the option to automatically install the compatible updates vulnerable dependencies.
  • Audit reports contain information about security vulnerabilities in your dependencies.
  • This report also contains necessary steps to be taken to fixe these vulnerability. For example, by running an npm install <package>@new-version.
  • It would work very well with your private/enterprise registries such as artifactory etc. 
  • It  will allow the developer to recursively analyze trees of dependent code to identify specifically what’s insecure.

The audit command submits a description of the dependencies configured in your project to your default registry and asks for a report of known vulnerabilities.

Quick Insight on the new commands:

  • npm audit      – Scan your project for vulnerabilities and just show the details, without fixing anything.
  • npm audit [—json]      – To provide report in Json format.
  • npm audit fix   – to scan and fix all vulnerabilities
  • npm audit fix –only=prod     – to skip updating devDependecies
  • npm audit fix –force  – will install semver-major updates to all top level dependencies.
  • npm audit fix –dry-run –json   – to do a dry run on the fixes and provide you a report.

NB: Npm audit fix runs a full  npm install under the hood, all configs that apply to the ā€œnpm audit fixā€  will also apply to npm install.

References:

[NPM Tip] Error: self signed certificate in certificate chain

May 10, 2018 JavaScript, Javascript Development, OpenSource, TypeScript, Web, Web Development No comments

As a developer, if you are behind a corporate proxy that assigns an intermediatory self signed SSL certificate to every request to provide secure content filtering as part of cybersecurity measures, I am sure you might have gone through the pain to get it working when working with NodeJS.

if you have Admin access to your windows machine, you could simply try the following fix:

    • Simply Add an Environment Variable
Environment Variable Name: NODE_TLS_REJECT_UNAUTHORIZED, Value: 0

image

image

Hope that solves your problem.

[NPM Tip] Rewriting the default protocol for GitHub package references

May 9, 2018 JavaScript, Javascript Development, OpenSource, TypeScript, Web, Web Development No comments

Some times as a Modern Web Developer you will face some ā€œnpm installā€ as some of the packages would be referring to git/ssh protocol to reference private packages from Git Hub.

This would fail when you are behind a corporate proxy.

Rewriting the default protocol for GitHub, run the following snippet in your command line snippets:

git config --global url."https://github.com/".insteadOf git@github.com
git config --global url."https://".insteadOf git://

Setting up Local NPM repository to Speedup Dev/CI Builds

April 29, 2018 Emerging Technologies, JavaScript, JavaScript, Modern Web Development, TypeScript, Web No comments , , ,

As a modern day JavaScript developer working with Node.js and NPM, it has been always any developer’s case to clean up local node modules sometimes when local build is broken. It is a tedious tasks to cleanup %appData%\npm-cacheĀ  to do a fresh install of all the modules again. Depending on the number of modules your project have, you will get stuck up for few minutes to hours to complete npm module installation depending on your Internet bandwidth.

Another scenario we can think of it on a build server or CI server, where we need to cleanup the entire modules during each build process, and ā€˜npm install’ would be like a fresh start, would take longer time to have your build complete.

What if we have a simple way of caching these packages locally, so that we do not have to download again from Internet every-time.Ā  I will help you with a simple solution, that once setup will resolve some of these problems effectively.

Introducing Local-NPM


local-npm is a Node server that acts as a local npm registry. It serves modules, caches them, and updates them whenever they change. Basically it’s a local mirror, but without having to replicate the entire npm registry.

This allows your npm install commands to (mostly) work off-line. Also, your NPM modulesĀ  get faster and faster over time, as commonly-installed modules are aggressively cached.

local-npm acts as a proxy between you and the main npm registry. You run npm install commands like normal, but under the hood, all requests are sent through the local server.

 

Getting Started with Local-NPM:

Step 1: Install the module ‘local-npm’

$ npm install –g local-npm

Step 2: launch local-npmĀ  and this will start the local npm server

$ local-npm

This will start the local npm server at localhost:5080.

http://127.0.0.1:5080

PS: Please note that, this step would take some time as this module tried to replicate the entire NPM repository remote skimdb to the local couchdb instance for efficient caching. But it will not eat up your disk space, as it caches modules based on usage only, it will not replicate the entire NPM repository.

Step 3: Validate the local-NPM registry

There is a basic NPMJS like UI to browse through local packages which can be accessed through.

http://localhost:5080/_browse.

Step 4: Then set npm to point to the local server:

$ npm set registry http://127.0.0.1:5080

Step 5: runĀ  ā€œnpm installā€ of your modules and you can see that local-NPM caches these modules that you regularly use.

Incase, to switch back to default NPMJS registry, you can do:

$ npm set registry https://registry.npmjs.org

How it works?

npm is built on top of Apache CouchDB (a No-SQL db), soĀ local-npmĀ works by replicating the full “skimdb” database to a localĀ PouchDB Server.

You can inspect the running database atĀ http://127.0.0.1:16984/_utils.

References

To understand more on local-NPM and documentation visit the module repository in github@https://github.com/local-npm/local-npm

Introduction to HTTP/2

May 23, 2015 .NET, Communication, CSS, HTML, HTTP, HTTP2, IIS, KnowledgeBase, Microsoft, Protocols, Visual Studio 2015, VisualStudio, VS2015, Web, Windows, Windows 10 No comments

The reason I got started with topic is that, there  were some buzz around Visual Studio 2015 RC support for HTTP/2 and Windows 8 – IIS support for HTTP/2. I was curious to learn further about the HTTP/2 and sharing my findings in this article.

About HTTP/2.

HTTP/2 is the first new version of HTTP since HTTP 1.1, which was standardized in RFC 2068 in 1997.

  • HTTP/2 enables a more efficient use of network resources and a reduced perception of latency by introducing header field compression and allowing multiple concurrent exchanges on the same connection. 
  • It also introduces unsolicited push of representations from servers to clients.
  • This specification is an alternative to, but does not obsolete, the HTTP/1.1 message syntax. 
  • HTTP’s existing semantics remain unchanged.

ā€œ HTTP/2 allows the server to “push” content, that is, to respond with data for more queries than the client requested. This allows the server to supply data it knows a web browser will need to render a web page, without waiting for the browser to examine the first response, and without the overhead of an additional request cycle. ā€œ

Quoting from MSDN:

HTTP/2 is a new version of the HTTP protocol that provides much better connection utilization (fewer round-trips between client and server), resulting in lower latency web page loading for users.  Web pages (as opposed to services) benefit the most from HTTP/2, since the protocol optimizes for multiple artifacts being requested as part of a single experience.

The browser and the web server (IIS on Windows) do all the work. You don’t have to do any heavy-lifting for your users.

[Source: MSDN]

HTTP v1.1 vs HTTPv2

  • HTTP/2 leaves most of HTTP 1.1’s high level syntax, such as methods, status codes, header fields, and URIs, the same. The element that is modified is how the data is framed and transported between the client and the server.

At a high level, HTTP/2:

  • is binary, instead of textual  ( the reason being is – ā€œBinary protocols are more efficient to parse, more compact ā€œon the wireā€, and most importantly, they are much less error-prone, compared to textual protocols like HTTP/1.x, because they often have a number of affordances to ā€œhelpā€ with things like whitespace handling, capitalization, line endings, blank links and so on. ā€œ)
  • is fully multiplexed, instead of ordered and blocking
  • can therefore use one connection for parallelism
  • uses header compression to reduce overhead
  • allows servers to ā€œpushā€ responses proactively into client caches

Taking help of an image visualization

http-timing-diagram

Major Milestones:

  • December 2014: The HTTP Working Group presented HTTP/2 to IESG for consideration as a Proposed Standard.
  • Feb 17, 2015: IESG approved it to publish as Proposed Standard
  • May 2015: The HTTP/2 specification was published as RFC 7540

Browser Support:

  • Chrome supports HTTP/2 by default.  (from version 41)
  • Google Chrome Canary supports HTTP/2 by default. (from version 43)
  • Chrome for iOS supports HTTP/2 by default.  (from version 41)
  • Firefox supports HTTP/2 which has been enabled by default since version 34.
  • Internet Explorer supports HTTP/2 in version 11, but only for Windows 10 beta, and is enabled by default. Currently only HTTP/2 over TLS is implemented.
  • Opera supports HTTP/2 by default (from v 28 onwards)

Reference Links: