Socket
Socket
Sign inDemoInstall

node-addon-api

Package Overview
Dependencies
0
Maintainers
5
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-addon-api

Node.js API (N-API)


Version published
Maintainers
5
Install size
673 kB
Created

Package description

What is node-addon-api?

The node-addon-api package is a helper library for building Node.js Addons in C++. It provides a C++ API that abstracts away the complexities of working with the low-level V8 and N-API provided by Node.js, making it easier to develop native addons.

What are node-addon-api's main functionalities?

Object Wrapping

Object wrapping allows C++ classes to be represented as JavaScript objects. It provides an easy way to create and manage objects that have a one-to-one relationship with C++ objects.

Napi::ObjectReference MyClass::constructor;

MyClass::MyClass(const Napi::CallbackInfo& info) : Napi::ObjectWrap<MyClass>(info) {
  // constructor implementation
}

Napi::Object MyClass::Init(Napi::Env env, Napi::Object exports) {
  Napi::HandleScope scope(env);

  Napi::Function func = DefineClass(env, "MyClass", {
    InstanceMethod("myMethod", &MyClass::MyMethod)
  });

  constructor = Napi::Persistent(func);
  constructor.SuppressDestruct();

  exports.Set("MyClass", func);
  return exports;
}

Function Arguments

The package provides a way to handle function arguments passed from JavaScript to C++ with type checking and conversion utilities.

Napi::Value MyFunction(const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();
  if (info.Length() < 2) {
    Napi::TypeError::New(env, "Expected at least two arguments").ThrowAsJavaScriptException();
  }

  Napi::String arg0 = info[0].As<Napi::String>();
  double arg1 = info[1].As<Napi::Number>().DoubleValue();

  // function implementation

  return Napi::String::New(env, "result");
}

Asynchronous Operations

node-addon-api provides utilities for performing asynchronous operations, allowing long-running tasks to be executed without blocking the Node.js event loop.

class MyAsyncWorker : public Napi::AsyncWorker {
public:
  MyAsyncWorker(Napi::Function& callback) : AsyncWorker(callback) {}

  void Execute() override {
    // Do work in another thread
  }

  void OnOK() override {
    Napi::HandleScope scope(Env());
    Callback().Call({Env().Null(), Napi::String::New(Env(), "Success")});
  }
};

Napi::Value RunAsyncWork(const Napi::CallbackInfo& info) {
  Napi::Function callback = info[0].As<Napi::Function>();
  MyAsyncWorker* worker = new MyAsyncWorker(callback);
  worker->Queue();
  return info.Env().Undefined();
}

Other packages similar to node-addon-api

Changelog

Source

2018-10-03 Version 1.5.0, @NickNaso

Notable changes:

Documentation
  • Completed the documentation to cover all the API surface.
  • Numerous fixes to make documentation more consistent in all of its parts.
API
  • Add Napi::AsyncContext class to handle asynchronous operation.
  • Add Napi::BigInt class to work with BigInt type.
  • Add Napi::VersionManagement class to retrieve the versions of Node.js and N-API.
  • Fix potential memory leaks.
  • DataView feature is enabled by default
  • Add descriptor for Symbols
  • Add new methods on Napi::FunctionReference.
  • Add the possibility to retrieve the environment on Napi::Promise::Deferred
TOOL
  • Add tool to check if a native add-on is built using N-API
TEST
  • Start to increase the test coverage
  • Fix in the test suite to better handle the experimental features that are not yet backported in the previous Node.js version.

Commits

  • [2009c019af] - Merge pull request #292 from devsnek/feature/bigint (Gus Caplan)
  • [e44aca985e] - add bigint class (Gus Caplan)
  • [a3951ab973] - Add documentation for Env(). (Rolf Timmermans) #318
  • [a6f7a6ad51] - Add Env() to Promise::Deferred. (Rolf Timmermans)
  • [0097e96b92] - Fixed broken links for Symbol and String (NickNaso)
  • [b0ecd38d76] - Fix Code of conduct link properly (#323) (Jake Yoon) #323
  • [223474900f] - doc: update Version management (Dongjin Na) #360
  • [4f76262a10] - doc: some fix on Napi::Boolean documentation (NickNaso) #354
  • [78374f72d2] - doc: number documentation (NickNaso) #356
  • [51ffe453f8] - doc: doc cleanup (NickNaso) #353
  • [fc11c944b2] - doc: major doc cleanup (NickNaso) #335
  • [100d0a7cb2] - doc: first pass on objectwrap documentation (NickNaso) #321
  • [c7d54180ff] - doc: the Napi::ObjectWrap example does not compile (Arnaud Botella) #339
  • [7cdd78726a] - doc: added cpp highlight for string.md (Jaeseok Yoon) #329
  • [8ed29f547c] - doc: add blurb about ABI stability (Gabriel Schulhof) #326
  • [757eb1f5a3] - doc: add function and function reference doc (NickNaso) #299
  • [2885c18591] - doc: Create changelog for release 1.4.0 (Nicola Del Gobbo)
  • [917bd60baa] - src: remove TODOs by fixing memory leaks (Gabriel Schulhof) #343
  • [dfcb93945f] - src: implement AsyncContext class (Jinho Bang) #252
  • [211ed38d0d] - src: make 'nothing' target a static library (Gabriel Schulhof) #348
  • [97c4ab5cf2] - src: add Call and MakeCallback that accept cargs (NickNaso) #344
  • [b6e2d92c09] - src: enable DataView feature by default (Jinho) #331
  • [0a00e7c97b] - src: implement missing descriptor defs for symbols (Philipp Renoth) #280
  • [38e01b7e3b] - src: first pass on adding version management apis (NickNaso) #325
  • [79ee8381d2] - src: fix compile failure in test (Michael Dawson) #345
  • [4d92a6066f] - src: Add ObjectReference test case (Anisha Rohra) #212
  • [779560f397] - test: add operator overloading tests in Number (Your Name) #355
  • [73fed84ceb] - test: add ability to control experimental tests (Michael Dawson) #350
  • [14c69abd46] - test: write tests for Boolean class (Jaeseok Yoon) #328
  • [2ad47a83b1] - test: explicitly cast to uint32_t in test (Gabriel Schulhof) #341
  • [622ffaea76] - test: Tighten up compiler warnings (Mikhail Cheshkov) #315
  • [fd3c37b0f2] - tools: add tool to check for N-API modules (Gabriel Schulhof) #346

Readme

Source

node-addon-api module

This module contains header-only C++ wrapper classes which simplify the use of the C based N-API provided by Node.js when using C++. It provides a C++ object model and exception handling semantics with low overhead.

N-API is an ABI stable C interface provided by Node.js for building native addons. It is independent from the underlying JavaScript runtime (e.g. V8 or ChakraCore) and is maintained as part of Node.js itself. It is intended to insulate native 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.

The node-addon-api module, which is not part of Node.js, preserves the benefits of the N-API as it consists only of inline code that depends only on the stable API provided by N-API. As such, modules built against one version of Node.js using node-addon-api should run without having to be rebuilt with newer versions of Node.js.

It is important to remember that other Node.js interfaces such as libuv (included in a project via #include <uv.h>) are not ABI-stable across Node.js major versions. Thus, and addon must use N-API and/or node-addon-api exclusively and build against a version of Node.js that includes an implementation of N-API (meaning a version of Node.js newer than 6.14.2) in order to benefit from ABI stability across Node.js major versions.

As new APIs are added to N-API, node-addon-api must be updated to provide wrappers for those new APIs. For this reason node-addon-api provides methods that allow callers to obtain the underlying N-API handles so direct calls to N-API and the use of the objects/methods provided by node-addon-api can be used together. For example, in order to be able to use an API for which the node-add-api does not yet provide a wrapper.

APIs exposed by node-addon-api are generally used to create and manipulate JavaScript values. Concepts and operations generally map to ideas specified in the ECMA262 Language Specification.

Current version: 1.5

(See CHANGELOG.md for complete Changelog)

NPM NPM

Setup

API Documentation

The following is the documentation for node-addon-api.

Examples

Are you new to node-addon-api? Take a look at our examples

Tests

To run the node-addon-api tests do:

npm install
npm test

To avoid testing the deprecated portions of the API run

npm install
npm test --disable-deprecated

Take a look and get inspired by our test suite

More resource and info about native Addons

WG Members / Collaborators

NameGitHub link
Anna Henningsenaddaleax
Arunesh Chandraaruneshchandra
Benjamin Byholmkkoopa
Gabriel Schulhofgabrielschulhof
Hitesh Kanwathirthadigitalinfinity
Jason Ginchereaujasongin
Michael Dawsonmhdawson
Nicola Del GobboNickNaso
Sampson Gaosampsongao
Taylor Wollboingoing

Licensed under MIT

FAQs

Last updated on 03 Oct 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc