You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

node-addon-api

Package Overview
Dependencies
Maintainers
5
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-addon-api

Node.js API (N-API)


Version published
Weekly downloads
18M
increased by1.62%
Maintainers
5
Install size
564 kB
Created
Weekly downloads
 

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-07-19 Version 1.4.0, @NickNaso

Notable changes:

Documentation
  • Numerous additions to the documentation, filling out coverage of API surface
API
  • Add resource parameters to AsyncWorker constructor
  • Add memory management feature

Commits

  • [7dc5ac8bc3] - doc: update metadata for release (Nicola Del Gobbo)
  • [d68e86adb4] - doc: Added documentation for PropertyDescriptor (Anisha Rohra) #309
  • [968a5f2000] - doc: Add documentation for ObjectReference.md (Anisha Rohra) #307
  • [908cdc314c] - doc: add TypedArray and TypedArrayOf (Kyle Farnung) #305
  • [2ff776ffe3] - backport node::Persistent (Gabriel Schulhof) #300
  • [98161970c9] - Backport perf, crash and exception handling fixes (Gabriel Schulhof) #295
  • [dd1191e086] - test: fix asyncworker test so it runs on 6.x (Michael Dawson) #298
  • [11697fcecd] - doc: ArrayBuffer and Buffer documentation (Kyle Farnung) #256
  • [605aa2babf] - Add memory management feature (NickNaso) #286
  • [86be13a611] - doc: Fix HandleScope docs (Ben Berman) #287
  • [90f92c4dc0] - doc: Update broken links in README.md (Hitesh Kanwathirtha) #290
  • [c2a620dc11] - doc: Clarify positioning versus N-API (Michael Dawson) #288
  • [6cff890ee5] - doc: Fix typo in docs (Ben Berman) #284
  • [7394bfd154] - doc: Fix typo in docs (Ben Berman) #285
  • [12b2cdeed3] - fix test files (Kyle Farnung) #257
  • [9ab6607242] - doc: Update Doc Version Number (joshgarde) #277
  • [e029a076c6] - doc: First pass at basic Node Addon API docs (Hitesh Kanwathirtha) #268
  • [74ff79717e] - doc: fix link to async_worker.md (Michael Dawson)
  • [5a63f45eda] - doc: First step of error and async doc (NickNaso) #272
  • [9d38f61afb] - doc: New Promise and Reference docs (Jim Schlight) #243
  • [43ff9fa836] - doc: Updated Object documentation (Anisha Rohra) #254
  • [b197f7cc8b] - doc: minor typos (Nick Soggin) #248
  • [4b8918b352] - Add resource parameters to AsyncWorker constructor (Jinho Bang) #253
  • [1ecf7c19b6] - doc: fix wrong link in readme (miloas) #255
  • [a750ed1932] - release: updates to metadata for next release (Michael Dawson)

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.

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.4

(See CHANGELOG.md for complete Changelog)

NPM NPM

Setup

API Documentation

The following is the documentation for node-addon-api (NOTE: still a work in progress as its not yet complete).

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

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

Package last updated on 19 Jul 2018

Did you know?

Socket

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
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc