How I ported bcrypt to new N-API

In a very simple way Native Addons could be considered as C/C++ code called from JavaScript.

They are a bridge between our application programming language JavaScript and native environment that is completely written in C/C++.

They allow us to call C/C++ functions and methods directly from JavaScript.

Node.js Native Addons

One of the most important experimental feature announced with Node.js 8 was N-API which is aimed at reducing maintenance cost for Node.js Native Addons.

It is independent from the underlying JavaScript runtime (ex V8) 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.
Addons are built/packaged with the same approach/tools outlined in the section titled C++ Addons. The only difference is the set of APIs that are used by the native code. Instead of using the V8 or Native Abstractions for Node.js APIs, the functions available in the N-API are used.

As you can understand from the N-API documentation it’s completely JavaScript engine agnostic and it guarentees the API and ABI compatibility between different version of Node.js. So if you switch to a different version of Node.js you must not reinstall and recompile the addon.

I was really enthusiastic about N-API specially after I followed the talk reported above at Node Interactive 2017

N-API — Next Generation Node API for Native Modules

I discovered that there is a C++ Wrapper of N-API called node-addon-api so I just started on experiment using it and in 4 or 5 days of my spare time I ported bcrypt to N-API.

I started to modify the package.json file where I added the right dependencies as reported on setup documentation

package.json

The next step was to modify the binding.gyp file that contains all the building configurations for the bcrypt addon.

binding.gyp

At the end bcrypt addon is composed by two parts one written in C++ and another written in JavaScript and as usually the JavaScript part use the native code to expose its features. I didn’t want change the JavaScript API exposed by bcrypt so I focused on the C++ part and changed the code following the node-addon-api documentation.

I started to rewrite the parts that has the responsibility to create the codes that will register a module named “bcrypt_napi” and in addition it will ensure that a function init will be called when the module is required.

Initialization code

bcrypt exposes synchronous and asynchronous API, so method after method I completed the refactor and now the code is as reported below

The last step was to execute the entire test suite and with my pleasure all tests passed successfully and they were executed more fast than the addon created using NAN. In the next days I will will perform most effective performance tests after that I will deploy my first Node.js application that will use this version of bcrypt, so stay tuned I will update you very soon.

For most of you that want start to write their Node.js native addons using N-API I just repost some useful resources that was very useful to me:

C++ Addons

C++ Addons N-API

Node Addon API

N-API the next API for Native Addons

Nicola Del Gobbo

Like what you read? Give Nicola Del Gobbo a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.