How I ported bcrypt to new N-API
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.
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.
I was really enthusiastic about N-API specially after I followed the talk reported above at Node Interactive 2017
I started to modify the package.json file where I added the right dependencies as reported on setup documentation
The next step was to modify the binding.gyp file that contains all the building configurations for the bcrypt addon.
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.
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: