fbpx

How to Setup Puppeteer In CentOS 7

How to Setup Puppeteer in CentOS 7

Setup Puppeteer In CentOS 7 is easy but gets tricky in Linux distributions. Few weeks back we have Completed a project using puppeteer . Now the Question was Where Should we deploy ?

Our code is written in Windows and we wanted to move code to Production . Now we have CentOS 7 in prod . We were struggling to run Puppeteer on Prod machine .

Have a look at Environment details we have used to deploy a puppeteer .

Environment Details:

Have a look at Steps we Have Followed to Deploy A puppeteer backed application in CentOS 7

Step 1:- Install Node JS In CentOS 7


curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash -

yum install nodejs


Step 2 :- Install Puppeteer In CentOS 7

npm i pupeteer

Step 3 :- Install Chromium In CentOS 7


yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc

Step 4 :- Install puppeteer in CentOs7

npm install --save puppeteer

Now Let’s Write a Sample Code to run Puppeteer in headless mode and name that file as test.js

'use strict';
const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {
  console.info("Starting browser");
  let browser;
  try {
    browser = await puppeteer.launch({});
  } catch (e) {
    console.info("Unable to launch browser mode in sandbox mode. Lauching Chrome without sandbox.");
    browser = await puppeteer.launch({args:['--no-sandbox']});
  }
  console.info("Browser successfully started");
  console.info("Closing browser");
  await browser.close();
  console.info("Done");
})();

Step 5 :- Run the Code , you will see the following output

Setup Puppeteer In CentOS 7
running puppeteer centos7 without sandbox mode

Real problem starts here , We are not able to launch browser without --no-sandbox flag.

I Challenge most of you will face this problem while you setup puppeteer in Centos 7. SO Follow down this simple hack to solve this . I struggled a lot , I think you may or may not . But if you have reached this section of article then i firmly believe you have faced this as well.

For running Puppeteer in sandbox mode we need to do few changes as of now .

If you navigate to node_modules/puppeteer/.local-chromium/linux-549031, you notice that for puppeteer there is a file named chrome_sandbox (with an underscore).

Renaming this file to chrome-sandbox and perform following steps below


sudo mv chrome_sandbox chrome-sandbox
sudo chown root chrome-sandbox
sudo chmod 4755 chrome-sandbox

So what are we doing here ,

We are renaming the chrome_sandbox folder to chrome-sandbox as standard chromium searches for file with chrome-sandbox. Now run the test file again.


$ node test.js

Starting browser
Browser successfully started
Closing browser
Done

Conclusion :-

While running puppeteer we were facing issues Unable to connect to chrome . While searching on internet about the problem i got this solution . Hence sharing this with you all . Let me know if this helps .

References :-

https://github.com/puppeteer/puppeteer/issues/560

https://github.com/puppeteer/puppeteer/issues/497

https://github.com/puppeteer/puppeteer/issues/2857

Bluetooth Connectivity in iOS – Part 1

Bluetooth Connectivity in iOS 

Bluetooth is a technology that is used to exchange data packets between two devices. Hence Bluetooth Connectivity in iOS has become one of the important aspect in modern day applications with IOT.

In the last decade we have witnessed some incredible developments have come in the way we use our mobile phones and other technologies specially Bluetooth.

So like other mobile operating systems iOS too comes with an option to connect to the Bluetooth via the phone’s settings menu. It is seen that due to lack of correct information  .

So in this blog we have tried to explain you the different profiles available in the Bluetooth and its connectivity with iOS.

iOS supports and connects to two different type of Bluetooth devices – one that is termed as BLE – Bluetooth Low Energy device and the other is a Bluetooth classic device.

Well, you did not get it right ?????????????? Let’s explain it a bit.

First let’s cover the most common form of Bluetooth Device which is a classic Bluetooth device.

Bluetooth Classic is designed for continuous two-way data transfer with high Application throughput (up to 2.1 Mbps); highly effective, but only for short distances.

So, it’s a perfect solution in the case of streaming audio and video, or mice and other devices that need a continuous, broadband link. There are different Bluetooth profiles which comes under Bluetooth classic :

Serial Port Profile:

If you’re replacing a serial communication interface (like RS-232 or a UART) with Bluetooth, SPP is the profile for you. SPP is great for sending bursts of data between two devices. It’s is one of the more fundamental Bluetooth profiles (Bluetooth’s original purpose was to replace RS-232 cables after all).

Human Interface Device (HID):

HID is the go-to profile for Bluetooth-enabled user-input devices like mice, keyboards, and joysticks. It’s also used for a lot of modern video game controllers, for PS3 controllers.

HSP – HeadSet Profile & HFP – Hands Free Profile

HSP is a Bluetooth profile that enables wireless connection between a phone and a Bluetooth headset. It supports simultaneous two-way audio but not stereo.

HFP is used in the hands-free audio systems built into cars. It implements a few features on top of those in HSP to allow for common phone interactions (accepting/rejecting calls, hanging up, etc.) to occur while the phone remains in your pocket.

Advanced Audio Distribution Profile (A2DP)

Advanced audio distribution profile (A2DP) defines how audio can be transmitted from one Bluetooth device to another.

Where HFP and HSP send audio to and from both devices, A2DP is a one-way street, but the audio quality has the potential to be much higher.

A2DP is well-suited to wireless audio transmissions between an MP3 player and a Bluetooth-enabled stereo.

A/V Remote Control Profile (AVRCP)

The audio/video remote control profile (AVRCP) allows for remote controlling of a Bluetooth device.

It’s usually implemented alongside A2DP to allow the remote speaker to tell the audio-sending device to fast-forward, rewind, etc.

Another mode of communicating with Bluetooth is BLE ( Bluetooth Low Energy):

BLE is a wireless personal area network technology designed and marketed by the Bluetooth Special Interest Group (Bluetooth SIG) aimed at novel applications in the healthcare, fitness, beacons, security, and home entertainment industries.

Compared to Classic Bluetooth, Bluetooth Low Energy is intended to provide considerably reduced power consumption and cost while maintaining a similar communication range.

BLE finds application largely in IoT and also other utility applications. BLE uses GATT profile (General Attribute Profile). General Attribute Profile is built on top of Attribute Protocol (ATT) and establishes common operations and a framework for the data transported and stored by the Attribute Protocol.

Please refer to the below mentioned diagram to understand the Bluetooth stack.

 

Bluetooth Connectivity IOS

After WWDC 2019,  docs on apple developer portal claimed to connect to classic Bluetooth devices using one of its frameworks : Core Bluetooth. However this is pretty misleading, as in practice this is not quite feasible till iOS 13.3. Lets take a look

Apple allows Bluetooth connectivity in iOS using two of its frameworks

Core Bluetooth communicates with Bluetooth Low Energy devices. The official apple documentation claims that using Core Bluetooth you can connect to the BR/EDR devices.

However which is not the case. If you go through the video of WWDC 2019 and look at what their team has to say about it, you would know that apple allows you to scan and connect the BR/EDR “CLASSIC” devices via a BLE service ID.

This means that the BR/EDR device which you would want to connect via Core Bluetooth should be having GATT profiles. For the Bluetooth devices with dual- mode apple uses Cross Transport Delivery Key to connect to the BR/EDR device if connected to it via the GATT profile.

To summarise apple still do not have any apis or any method to connect to a non- GATT BR/EDR (classic) device using Core Bluetooth.

The second way of connecting the classic devices is using External Accessory framework. But a big caveat with this is that External accessory only recognises MFI compliant devices.

What is MFI :-

MFI is a licensing program for developers of hardware and software peripherals that work with Apple’s iPod, iPad and iPhone. The name is a shortened version of the long-form Made for iPod, the original program that ultimately became MFI.

When you are enroll with a MFi you get a protocol string or rather a unique string for the device you enrolled which you can pass in the delegate methods of EAAccessoryDelegate.

The steps and code to connect to the BLE using Core Bluetooth and MFi device using External Accessory framework is available in the second  part. You can refer to the below WWDC 2019 .

Article Written By :- Kumar Gaurav

 

Categories IOS

Simplify your callback or Promise with async await in Nodejs application

Async Await Nodejs

With the release of Node.js version 8, the long-awaited async functions have landed in Node.js as well. Before we understand the asyn await in Node.js, let us dive into what and why we use async await in the first place.

If you really want to get rid of callback hell OR chained promises, then it’s time to remove those code snippets from your codebase. We will see below step by step.

What is Async Await in Nodejs ?

  • It is the newest way to write asynchronous code in JavaScript.
  • It is non-blocking (just like callbacks and promises).
  • Async/Await is created to simplify the process of working with and writing chained promises.
  • An async function returns the Promise. If the function throws an error, the Promise will be automatically rejected, and if the function returns the value that means the Promise will be resolved.

Syntax of Async Function

We need to add the async keyword before a function.

// Normal Function
function add(a,b){
  return a + b;
}
// Async Function
async function add(a,b){
  return a + b;
}

Async function declarations return an async object. These are similar to Generator in the sense that their execution can be paused. The only fundamental difference is that they always create a Promise instead of an object.

Now, let us take a simple example of Promise.

But before that, please check your Node.js version. If your Node.js has not the latest version, then please update to the newest version.

Now, create a project folder.

mkdir node-examples

Go into the project folder.

cd node-examples

Now, create a package.json file using the following command.

npm init -y

Install the nodemon server using the following command.

npm install nodemon --save

Create a new file called server.js inside the root. Write the following code inside it.

// server.js

function square(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(Math.pow(x, 2));
    }, 2000);
  });
}

square(10).then(data => {
  console.log(data);
});

Next step is to start the nodemon server using the following command.

 

nodemon server.js

So, hereafter two seconds, we can see the square of 10. Function square returns the promise, and after resolving that promise, we get our squared data.

Switching from Promises to Async/Await.

Okay, now turn above code into async/await function.

// server.js

function square(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(Math.pow(x, 2));
    }, 2000);
  });
}

async function layer(x)
{
  const value = await square(x);
  console.log(value);
}

layer(10);

Output:

The great thing about having our function in Promise form is that we don’t actually need to “make it an async/await version” if we don’t want to. When we call/execute the function, we can simply use the async/await keywords.

Please Comment if You Want some More examples or You are stuck at any place .We will be happy to help you ! Thanks !

FilterChainProxy and DelegatingFilterProxy Uses

Filter Chain Proxy and Delegating Filter Proxy

FilterChainProxy and DelegatingFilterProxy  is a good topic to know . We will discuss this topic as most of you were asking me earlier lot of times. In Order to understand FilterChainProxy and DeligatingFilterproxy one must understand the servlet filters first .

DelegatingFilterProxy :-

When a request reaches the server ,it is intercepted by SpringSecurityFilterChain(ex: DelegatingFilterProxy) , then it delegates the request to Spring security framework where the security tasks defined will be handled by security filters defined in application context.

Note:( DeligatingFilterProxy provides the link between application context and web.xml. What DelegatingFilterProxy does Here ?  It is just delegating the request  to a Spring-managed bean that implements the Servlet Filter interface , that is the reason probably the name is DeligatingFilterProxy .

This enables the bean to get benefit from the Spring web application context lifecycle support and configuration flexibility. The bean must  have the same name as that in the filter-name element as below.

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

SpringSecurityFilterChain and FilterChainProxy :-

Now Once a Servlet request reaches the Filter , DelegatingFilterProxy gets initialized , as a part of its Initialization it looks for the filter name , in above example the filter name is springSecurityFilterChain .

This  is basically a Spring Bean , Now If you see there are no bean with name springSecurityFilterChain is present in Spring Context . So what is this springSecurityFilterChain represents . So SpringSecurityFilterChain is an alias for bean FilterChainProxy . It returns a FilterChainProxy object actually .

if You want to know how this Proxy gets called and initialized , please go through the stackoverflow link present here which explains SpringSecurityFilterChain  and FilterChainProxy in detail.

FilterChainProxy: –

FilterChainProxy lets us add a single entry to web.xml and deal entirely with the application context file for managing our web security beans. It is wired using a DelegatingFilterProxy, just like in the example above, but with the filter-name set to the bean name “filterChainProxy”.

At runtime the FilterChainProxy will locate the first URI pattern that matches the current web request and the list of filter beans specified by the filters attribute will be applied to that request. The filters will be invoked in the order they are defined, so you have complete control over the filter chain which is applied to a particular URL.

FilterChainProxy will always delegate init(FilterConfig) and destroy() methods through to the underlaying Filters if such methods are called against FilterChainProxy itself. In this case, FilterChainProxy guarantees to only initialize and destroy each Filter bean once, no matter how many times it is declared in the filter chain(s).

After that this filter will invoke to the next possible set of filter chains in the order. filterChainProxy consists of an ordered list of security filters that are defined in the spring application context which is alias of springSecurityFilterChain bean.

By default when we add ‹http› element  SecurityContextPersistenceFilter,ExceptionTranslationFilter and FilterSecurityInterceptor will be added, And as we have set auto-config to true, BasicAuthenticationFilter, LogoutFilter and UsernamePasswordAuthenticationFilter also gets added to the filter chain.Filter chain keeps skipping until the right filter is reached.

There are Different Filters that Works behind the Scene , once different modes of authentications are uses. Please have a look at the diagram below .

Spring SecurityFiltersAndUses

This is a basic Comparison of FilterChainProxy and DelegatingFilterProxy.

Written by – Subrat Padhi.

Demystifying How Authentication Works In Spring Security

Demystifying How Authentication Works In Spring Security

Spring Security Authentication

In this Post of How Authentication Works In Spring Security , we will cover Authentication Flow in Spring Security . This is going to be a long article as we will be discussing Spring Security Authentication Full Flow .

Spring Security provides comprehensive security services for J2EE-based enterprise software applications. People use Spring Security for many reasons, but most are drawn to the project after finding the security features of J2EE’s Servlet Specification or EJB Specification lack the depth required for typical enterprise application scenarios.

Spring Security Works on Three Core Principles or Steps :-

  1. Authentication
  2. Authorization
  3. Exception handling

We Will See How Spring Initiates and Starts the Whole Authentication Process , but lets discuss Spring Authentication Flow , then we Will deep dive in upcoming Posts.

 

How Authentication Works In Spring Security :-

So Spring  Security basically is a chain of Servlet filters written for various purposes. Each of those filters are meant to do some specific task .

So there are different authentication mechanisms provided in Spring such as Http Basic , Form based login , Ouath1 etc . All these mechanisms are handled by Specific Filters Which extends AbstractAuthenticationProcessingFilter  .  So all requests are handle by this Filter and in the Whole Post we will name it as Authentication Filter. Have a look at different Filters and its Use cases .

How HTTPBasic Authentication Works In Spring Security :-

Once a Servlet request for HttpBasic Authentication reaches Spring Security , Authentication Filter Picks it up and tries to  decode Http Basic Headers . It then checks if Authentication is required or not . If authentication is required It Creates an  instance of UserNamepasswordAuthenticationToken  with username and token  and sets isAuthenticated(false)   This token is  then passed to AuthenticationManager  as shown in diagram below . 

SpringSecurityAuthenticationFlow

 How AuthenticationManager authenticates that token we will see in a while .Now the Authenticated value in this step is Still False till UserNamePassWordAuthenticationToken is passed to AuthenticationManager . So Once AuthenticationManager Processes the Request  with that Token Object  , the status of Authentication value is Changed to true in case of Successful authentication  as shown below else AuthenticationException is thrown.




authentication works in spring security

UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
						username, tokens[1]);				
Authentication authResult = authenticationManager.authenticate(authRequest);

How Authentication Manager handles Authentication In Spring Security:-

Authentication manager is Responsible for Handling Authentication , using different Authentication providers . We can also use a custom authentication Provider like Databse , LDAP etc. So the Sole responsibilty of AuthenticationProvider is to return an Authentication instance with Principal Object and Granted Authorities Populated .

AuthenticationManagerSpringSecurity

Have a look at AuthenticationManager Source Code :-

package org.springframework.security.authentication;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

public interface AuthenticationManager {
    Authentication authenticate(Authentication authentication) throwsAuthenticationException;
}

Once a user sends a request it  reaches the right authentication filter based on the authentication mechanism used.Now it extract the given credentials from the request and then using the supplied values UsernamePasswordAuthenticationToken is  created( the authentication object).Then  it calls ‘authenticate’ method of the Authentication Manager.

The request then passes to Authentication Provider and the Authentication Provider contains an implementation of UserDetailsService. Spring loads the user information in UserDetailsService  by calling loadbyuserName and compares the username/password combination with the credentials supplied at login.

If the user is valid then Authentication Manger creates UsernamePasswordAuthenticationToken  instance with follwing Constructor  , setting authenticated =true .

public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
Collection<? extends GrantedAuthority> authorities) {
		super(authorities);
		this.principal = principal;
		this.credentials = credentials;
		super.setAuthenticated(true); // must use super, as we override
	}

UserNamePasswordAuthenticationToken is the Authentication object which is passed to authenticate method of Authentication Manager. Have a look how Authentication interface in Spring looks like

package org.springframework.security.core;

import java.io.Serializable;
import java.security.Principal;
import java.util.Collection;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.context.SecurityContextHolder;
public interface Authentication extends Principal, Serializable {
	Collection<? extends GrantedAuthority> getAuthorities();
	Object getCredentials();
	Object getDetails();
	Object getPrincipal();
	boolean isAuthenticated();
	void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}

Now this Authentication object  is then passed to Authentication Filter . Authentication Filter Sets the Authentication Object Containing Principal and Granted Authorities in Security Context  as Show below extending the above diagram.

How AuthenticationFilter and SecurityContext

How to Configure Multiple Authentication Providers In Spring Security :-

We can configure multiple AuthenticationProviders in Spring Security , that will return Authentication Object if any of them is sucessful .

Spring Security provides a number of AuthenticationProvider implementations,

  1. Dao AuthenticationProvider
  2. Cas AuthenticationProvider
  3. X509  AuthenticationProvider
  4. Jaas AuthenticationProvider
  5. Ldap AuthenticationProvider

In next Post We will See How We Can Create Custom Authentication Providers in Spring Security  and Explain the Authorization Flow .

Now that You have understood the Http-Basic Authentication mechanism in Spring , its same in case of other Authentication Mechanisms in Spring Security . Please let me know in frugalisminds@gmail.com  with Post title in Subject if you have any Questions or clarifications .

Summarizing Authentication Flow Spring Security :-

  • Authentication Filter creates an Authentication Request and Passes to Authentication Manager.
  • Authentication Manager delegates the request to Authentication Provider
  • Authentication Provider calls User Details service loads the User Details and returns the Authenticated Principal.
  • Authentication Manager returns the Authenticated Object to Authentication Filter and Authentication Filter sets the Authentication object in Security Context .

Note :- If you think closely we don’t need an User Details Service as well , the only things that is needed to Authentication Provider is an Authentication object with Principal as any Object and Granted Authorities .

This is the beauty of Spring Framework , where you always have an option to customize stuff.

More Posts On Spring Security :-

FilterChainProxy and DelegatingFilterProxy Uses

Using Custom Authentication Provider Spring Security

 

Article Written and Published By :- Subrat Padhi .

 

Redefining JavaScript loops – filter map and reduce

Redefining JavaScript loops – filter map and reduce

In JavaScript we have been writing loops , like we do in any programming language . Today we are going to check JavaScript  filter , map and reduce . These are a part of JavaScript Functional Programming .

We are not going to deep dive into the concepts of functional programming in java script . Lets get started and have  a look at these concepts.

Have a look what MDN Says about these JavaScript filter map and reduce :-

  1. Filter In Javascript  :- As the name suggests, it returns an array with filtered  items from the original array
  2. Map In Javascript :- Used to create a new array by iterating existing array
  3. Reduce  In Javascript  :- Returns a single object .

We are going to take following example where we have an array of employees and we are going to do some operations on this array to demonstrate JavaScript filter map and reduce.




How to use Filter In JavaScript  :-

How Would you Find Out Employees Whose age is less than 50 here in above employee array ?

In a Normal JavaScript we would do something below as shown ,

let seniorCitizen = [];
for(let i = 0; i < employee.length; i++) {
  if(employee[i].age <= 50) {
    seniorCitizen.push(employee[i]);
  }
}

Now We have a different Way of Doing this using Javascript Filter .

const seniorCitizen= employee.filter(function(emp,index) {
    console.log(index);
      if(emp.age <= 50) {
        return true;
      }
    });

JavaScript Filter we can pass two arguments inside a callback function , actual object and index .

We can also Write same thing using ES6 syntax as below .

const seniorCitizen= employee.filter((emp,index) => {emp.age <= 50});

Output:-

How to use JavaScript Map  :-

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

const usernames = employee.map(function(emp) {
  return emp.name;
});

const test1 = employee.map(function(emp) {
  return `${emp.name} [${emp.age} - ${emp.salary}]`;
});

const testMap = employee.map(emp => `${emp.name} [${emp.age} - ${emp.salary}]`);

const ageMap = employee
  .map(emp => emp.age * 2)
  .map(age => age+2).filter(age=>age<88);

console.log(ageMap);
console.log(test1);
console.log(testMap);

Output :-

 Array(1) [48]
 Array(3) ["Sanjay [23 - 34545]", "John [43 - 3444]", "Sean [53 - 344445]"]
 Array(3) ["Sanjay [23 - 34545]", "John [43 - 3444]", "Sean [53 - 344445]"]

How to Use JavaScript Reduce :- reduce()  works on an array and returns a single object .We are going to write a code works on an array and reduce to compute the total value of age of users  array .

Lets See How We can achieve this :-

const ageSum = employee.reduce(function(total, emp) {
  return total + emp.age;
}, 0);

In the above example we are passing two parameters to the callback function , total is known as accumulator. The Job of accumulator is it accumulates the callback’s values . It is the accumulated value previously returned . In this case we have initialized our accumulator with zero value .

So once the first element of array is traversed accumulator total will have 0+age of First employee. Now this value is passed to the next callback function for second array item and so on.

Now We Can simplify the above JavaScript reduce() code using ES6 syntax as :

const totalYears = employee.reduce((total, emp) => total + (emp.age), 0);

 

Please Comment if You Want some More examples or You are stuck at any place .We will be happy to help you ! Thanks !