Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Node.js 8 - New Features & Fixes Released with NPM 5

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 2.61k
    Comment on it

    Node.js is an open source cross-platform built on Chrome runtime environment for server-side and networking applications which offer a fast network application which can be built very easily.

    What's New Released:

    Node.js 8.x has been finally announced on May30, 2017. Node.js will go into Long Term Support (LTS) and will be maintained till December 31st, 2019.

     


     

    Features of Node.js 8.x

     

    1: N-API

    N-API stands for Native API. It is independent of Javascript V8 engine (a Javascript run time). N-API is based on `the Application binary interface (ABI)`. When you write source code, you access the library through the API. Once the source code is compiled (Converted to binary code), your application accesses the library through ABI. N-API make it possible for modules or libraries (compiled for one version) to run on any version of node without having to go for recompilation. Node.js API for native modules is an experimental feature in Node.js v8.x.

     

    For more on N-API and ABI: 

     

     

    2: async_hooks

    Developers make use of async Hooks (formerly known as `AsyncWrap`) to attach listeners to the event-loop life cycle. `async_hook` is an experimental feature in Node.js v8.x.

    It is a high level abstraction of undocumented node API called `AsyncWrap`. It patches some issues and makes the APIs more uniform.

     

    How to use async_hooks?

    const asyncHook = require('async_hooks');

     

    Hooks :

    2.1: init

    init() is called during object construction. The resource may not have completed construction when this callback was run. So, all fields of the resource referenced by "id" might not have been populated. 

    Code snippet:

    function init(id, type, triggerId, resource) { }

     

    2.2: before

    before() is called just before the resource's callback is called.

    function before(id) { }

     

    2.3: after

    after() is called just after the resource's callback has finished, and will always fire. In the case of an uncaught exception after() will fire after the 'uncaughtException' handler, or domain has handled the exception.

    function after(id) { }

     

    2.4: destroy

    destroy() is called when an AsyncWrap instance is destroyed. For example, in HTTP Parser, the resource is reused, or timers where the resource is only a JS object, destroy() will be triggered manually to fire asynchronously after the after() hook is completed.

    function destroy(id) { }

     

     

    3: util.promisify

    Good news for Node.js developers, Node.js is adding a new utility (`util.promisify`) to convert callback based internals to returning promises. If you have already worked with promisify NPM module, it won't surprise you much; they work almost in the very same way. 

    `util.promisify` takes a callback function as a parameter: `(err, value) => ...`.

    Let’s say, I want to read the content of a file. fs.readFile is a method of "fs" NPM module. The implementation is as follows:

    const fs = require('fs');
    
    fs.readFile('./readme.txt', (err, content) => {
      if (err) {
        throw new Error(err.message);
      }
      console.log(content);
    });

    A promise based `fs.readFile` is also possible. You have two options: Pick a NPM module, or manually code using the Promises. Here, I am going to use the second option:

    const fs = require('fs');
    exports = module.exports = 
      (file, options) => 
        new Promise((resolve, reject) => {
          fs.readFile(file, options, (err, content) => {
            if (err) {
              return reject(err.message);
            }
            resolve(content);
          });
        });


    That's pretty straightforward, but it doesn't scale very well. I don't follow the above approach if I need to do something complex. I would love to use `util.promisify`:

    const fs = require('fs');
    const util = require('util');
    
    const readFile = util.promisify(fs.readFile);
    
    readFile('./readme.txt')
      .then(content => console.log(content));

    For more on util.promisify: https://nodejs.org/api/util.html#util_util_promisify_original

     

     

    4: Debugging

    There is a big emphasis on debugging. A new `inspector` core module for developers to leverage the debug protocol (used by the Chrome inspector) in order to debug the Node.js code. `Inspector`  is an experimental feature in Node.JS v8.x.

    For more on Debugging: https://nodejs.org/api/debugger.html

     

     

    5: zero-filling Buffers

    The `new Buffer(number)` constructor initializes memory spaces with zeros by default and makes Node.js safer from security issues. In earlier versions, `new Buffer(number)` constructor didn't initialize the memory space with zero by default which allowed sensitive information to leak in.

    For more on buffers: https://nodejs.org/api/buffer.html

     

     

    6: NPM 5

    In a bid to improve the performance of NPM, the popular JavaScript package manager, an upgrade has been within Node.js 8.0. Now, the speed has increased by six folds of what it was in NPM 4. Although NPM's cache has been rewritten for speed, there are chances for existing applications to break.

    For more on NPM-5: http://blog.npmjs.org/post/161081169345/v500

     

     

    There are many more features which are introduced in the new release, you can read from Node.js v8.0.0 Documentation: https://nodejs.org/api/v8.html

     

     

    What you think about the new features of Node.js 8.0 will the changes be saluted by the developers? Feel free to share your thoughts and views in the comment box below.

    Node.js 8 - New Features & Fixes Released with NPM 5

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: