Posts

Async/await functions in JavaScript

Async/await functions in JavaScript ES2017 (ES8) introduced the concept of  async/await functions , and it’s the most important change introduced in this ECMAScript edition. Async functions are a combination of promises and generators to reduce the boilerplate around promises, and the “don’t break the chain” limitation of chaining promises.  It’s a higher level abstraction over promises. When Promises were introduced in ES6, they were meant to solve a problem with asynchronous code, and they did, but over the 2 years that separated ES6 and ES8, it was clear that  promises could not be the final solution . Promises were introduced to solve the famous  callback hell  problem, but they introduced complexity on their own, and syntax complexity. They were good primitives around which a better syntax could be exposed to the developers: enter  async functions . Code making use of asynchronous functions can be written as function doSomethingAsync ( ) {...

Node Js - Blocking and Non-Blocking Concept.

Look at these two blocks of code and find the difference. var fs = require ( "fs" ); var data = fs . readFileSync ( 'input.txt' ); console . log ( data . toString ()); console . log ( "Program Ended" ); var fs = require ( "fs" ); fs . readFile ( 'input.txt' , function ( err , data ) { if ( err ) return console . error ( err ); console . log ( data . toString ()); }); console . log ( "Program Ended" ); These two examples explain the concept of blocking and non-blocking calls. The first example shows that the program blocks until it reads the file and then only it proceeds to end the program. The second example shows that the program does not wait for file reading and proceeds to print "Program Ended" and at the same time, the program without blocking continues reading the file. Thus, a blocking program executes very much in sequence. From the programming point of view, it i...

Node.js for I/O bound, Not for CPU bound.

Image
What do these terms ' I/O bound ' and  ' CPU bound '  mean? I/O bound This is the case for typical Node.js web server application. Majority of the time is spent waiting for network, filesystem and perhaps database I/O to complete. Increasing hard disk speed or network connection improves the overall performance. In its most basic form Node.js is best suited for this type of computing. All I/O in Node.js is non-blocking and it allows other requests to be served while waiting for a particular read or write to complete. CPU bound An example of CPU bound application would be a service that calculates SHA-1 checksums. Majority of the time is spent crunching the hash - doing large amount of bitwise xors and shifts for the input string. This kind of application leads to trouble in Node.js. If the application spends too much time performing CPU intensive task all other requests are being held up. Node.js runs a single threaded event loop to concurrently advan...

OAuth vs JWT

The question is an common one, but it isn't quite sensible. JWT is a type of Token, and OAuth is a Framework that describes how to dispense tokens. JWT can absolutely be used as an OAuth Bearer token. In fact, this is the most common practice. In light of that "JWT vs OAuth" is a comparison of apples and apple carts. Often people think "OAuth token"  always  implies an opaque token that is granted by a OAuth token dispensary, that can then be validated only by that same OAuth dispensary system. But this is not the only kind of OAuth token. JWT is just a different kind of OAuth token. Today, the OAuthV2/GenerateAccessToken policy in Apigee Edge generates opaque tokens. It returns a token of 32 seemingly random characters, and the holder has no idea what the token signifies. Therefore, we call it "opaque". To USE the token, the holder must present it back to the token dispensary, because the original dispensary is the only party that can relate the...

@EnableResourceServer Vs (@EnableOAuth2Sso and @EnableOAuth2Client)

OAuth defines four roles: resource owner An entity capable of granting access to a protected resource. When the resource owner is a person, it is referred to as an end-user. resource server The server hosting the protected resources, capable of accepting and responding to protected resource requests using access tokens. client An application making protected resource requests on behalf of the resource owner and with its authorization. The term "client" does not imply any particular implementation characteristics (e.g., whether the application executes on a server, a desktop, or other devices). authorization server The server issuing access tokens to the client after successfully authenticating the resource owner and obtaining authorization. @EnableResourceServer  annotation means that your service (in terms of OAuth 2.0 - Resource Server) expects an access token in order to process...

H2-Console is not showing in browser

Image
If you have override configure method of Spring Security and you want to use console of h2 database in browser, and after connect h2-console showing loading error as shown in below image. Then you have to add following line in your overridden configure method. @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {    Logger logger = LoggerFactory.getLogger(SecurityConfig. class );       //......            @Override    protected void configure(HttpSecurity http) throws Exception {      http.authorizeRequests().antMatchers( "/" ).permitAll();      http.authorizeRequests().antMatchers( "/imgs/**" ).permitAll();      http.authorizeRequests().antMatchers( "/admin/**" ).hasRole( "ADMIN" );      http.authorizeRequests().antMatchers( "/**" ).hasRole( "USER" ).and().formLogin(); ...