Posts

Showing posts with the label nodejs

when to use prototypes

* For object creation optimization Built-in prototype object is if you'll be duplicating an object multiple times that will share common functionality. By attaching methods to the prototype, you can save on duplicating methods being created per each new instance. But when you attach a method to the prototype, all instances will have access to those methods * use prototypes if we need to declare a "non-static" method of the object. var myObject = function () { }; myObject.prototype.getA = function (){   alert("A"); }; myObject.getB = function (){   alert("B"); }; myObject.getB();  // This works fine myObject.getA();  // Error!

compare password and sensitive data's in nodejs

previously I did like this to compare the sensitive data's function validatePassword(fromDbPassword, inputPassword){          if( fromDbPassword === inputPassword) return true;           return false; } There is an attack security attack called timing attack . Hackers try to crack the encryption algorithm using the way.  So How to compare password hackers cant use the timing attack V8,  JavaScript engine used by Node.js, tries to optimize the code you run from a performance point of view. It starts comparing the strings character by character, and once a mismatch is found, it stops the comparison operation. So the longer the attacker has right from the password, the more time it takes. function checkApiKey (apiKeyFromDb, apiKeyReceived) {  return cryptiles.fixedTimeComparison(apiKeyFromDb, apiKeyReceived)  } To solve this issue, you can use the npm module c...

Wrap external http request in node js

Wrap external http request in node js var http = require('http'); var url = require('url').URL; var shimmer = require('shimmer') var api = {} api.wrapEmitter = require('emitter-listener') var Origional = http.get; shimmer.wrap(http, 'get', function getWrap(get) {     return makeRequestTrace(get); }) function makeRequestTrace(request) {     return function trace(options, callback) {         var req = request.call(this, options, function (res) {             console.log('Response===================' , res)             shimmer.wrap(res, 'on', function onWrap(on) {                 return function on_trace(eventName, cb) {                     if (eventName === 'data') {                         on.call(this, 'data', func...

Application monitoring tool essentials and links

basic APM tool should monitor 1. Down time of the applications 2. Capture  the slow responses 3. Compare the response time metrics for before commit  vs after commit  4. Show distributed transactions (Call trace) 5. Alerting the security issues (Ex: If external library has the security issue then it should indicate the alert )  Good Nodejs  APM  tools https://www.appdynamics.com/ https://newrelic.com/ https://www.dynatrace.com/ https://opbeat.com/ https://www.instana.com/ https://traceview.solarwinds.com/ https://risingstack.com/

__proto__ vs prototype

The  __proto__  property of  Object.prototype  is an accessor property (a getter function and a setter function) that exposes the internal  [[Prototype]]  (either an object or  null ) of the object through which it is accessed. __proto__  is the actual object that is used in the lookup chain to resolve methods, etc.  prototype  is the object that is used to build  __proto__  when you create an object with  new : _proto_ var parent = {   color : 'white' } var child = {} child  = Object.create(parent) child.color //White parent.color = 'black' child.color  // 'black' because of _proto_ chain Prototype function   Soldier ( )   { } Soldier . prototype  =   {   weapons :   [ 'rifle' ,   'grenade' ,   'bayonet' ] ,   weaponOfChoice :   'rifle' } ; var  GIJoe  =   new   Soldier ( ) ; var  Ram...

Inheritance in nodejs

To inherit the properties we can use util.inherits method var util  = require('util') util.inherits(child, parent) /**  * Inherit the prototype methods from one constructor into another.  *  * The Function.prototype.inherits from lang.js rewritten as a standalone  * function (not on Function.prototype). NOTE: If this file is to be loaded  * during bootstrapping this function needs to be rewritten using some native  * functions as prototype setup using normal JavaScript does not work as  * expected during bootstrapping (see mirror.js in r114903).  *  * @param {function} ctor Constructor function which needs to inherit the  * prototype.  * @param {function} superCtor Constructor function to inherit prototype from.  */ exports.inherits = function(ctor, superCtor) {     ctor.super_ = superCtor;     ctor.prototype = Object.create(superCtor.prototype, {         const...

Javascript tutorials

Today I learned few things in javascript  check for null, undefined, or blank variables in JavaScript? if ( value ) { } will evaluate to  true  if  value  is not: null undefined NaN empty string ("") 0 false !! operator in node js This converts a value to a boolean and  ensures a boolean type . "foo" = "foo" ! "foo" = false !! "foo" = true Truth Table for javascript '' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true " \t\r\n" == 0 // true NaN === NaN //false !! NaN === !! NaN //true

Node js tutorials

Today I learned Total response time To calculate the overall response time of the method we basically use new Date() function but in these functions we cannot able to find the exact time.   if we use the process.hrtime() function we can find the difference accurately till nano seconds Process.hrtime() const NS_PER_SEC = 1e9 ; const time = process . hrtime (); // [ 1800216, 25 ] setTimeout (() => { const diff = process . hrtime ( time ); // [ 1, 552 ] console . log ( `Benchmark took $ { diff [ 0 ] * NS_PER_SEC + diff [ 1 ] } nanoseconds` ); // benchmark took 1000000552 nanoseconds } , 1000 ); ref :  https://nodejs.org/api/process.html#process_process_hrtime_time HTTP & HTTPS request in nodejs There are two modules in nodejs to send the request http, https (for secure request) There is another module called url  in  nodejs  its used for parse, Create request object, find path , resolve path ...