a blog for those who code

Wednesday, 7 June 2017

What is N-API in Nodejs 8

In this post we will be discussing about N-API in Node.js 8. N-API is an experimental feature which was added in Node.js Version 8. The advantage of using N-API is that now native module developer have to compile their module only once per platform and it will be available for any Node.js version that implements N-API so that developer do not have to worry about any future releases.

In our previous post we have discussed about What's New in Node Version 8 where we have discussed what all the new features have been added in Node.js Version 8. Let us further discuss about N-API and what's its usage.

According to Node.js Doc, N-API is an API for building native Addons. It is independent from the underlying JavaScript run-time and is maintained as part of Node.js itself. This API will be Application Binary Interface (ABI) stable across versions of Node.js. It is intended to insulate Addons from changes in the underlying JavaScript engine and allow modules compiled for one version to run on later versions of Node.js without recompilation.

Why this is important because existing native modules which are written in C or C++ are directly dependent on JavaScript V8 engine thus all the addons have to be recompiled or to be updated with every new releases of Node.js. This is the major reason why most of the module developers hesitate to update to the newer Node.js version in the fear that it might break some functionality.

N-API came to solve this problem by providing an Application Binary Interface (ABI) which will help module developers to compile only once and not to worry about future releases as ABI will take care of that part. The N-API is a C API that ensures ABI stability across Node.js version and different compiler levels.

Usage

In order to use N-API functions we need to include node_api.h file as shown below :

#include <node.api.h>

Basic N-API Data Types 

napi_status - Indicates the success or failure of a N-API call.
napi_env - Is to represent a context to persist VM-specific state.
napi_value - To represent JavaScript value.
napi_extended_error_info - To represent all the error occurred.
napi_handle_scope - Control and Modify the lifetime of objects created within a particular scope.
napi_callback_info - Get the information about the context of callback.

Simple Hello World Example in N-API

#include <node_api.h>
#include <assert.h>

napi_value Method(napi_env env, napi_callback_info info) {
  napi_status status;
  napi_value world;
  status = napi_create_string_utf8(env, "world", 5, &world);
  assert(status == napi_ok);
  return world;
}

#define DECLARE_NAPI_METHOD(name, func)                          \
  { name, 0, func, 0, 0, 0, napi_default, 0 }

void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
  napi_status status;
  napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
  status = napi_define_properties(env, exports, 1, &desc);
  assert(status == napi_ok);
}

NAPI_MODULE(hello, Init)


Old Node.js Versions Support

Node-addon-api module helps you to add a backward compatibility to be used with older versions of Node.js that do not have N-API built in.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment