0.3.10 • Published 3 years ago

napi-rs v0.3.10

Weekly downloads
156
License
MIT
Repository
github
Last release
3 years ago

napi-rs

This project was initialized from xray

A minimal library for building compiled NodeJS add-ons in Rust.

Platform Support

Lint Linux N-API@3 Linux musl macOS/Windows/Linux x64 Linux-aarch64 Windows i686 FreeBSD

Operating Systems

LinuxmacOSWindows MSVCFreeBSD

NodeJS

Node10Node 12Node14

This library depends on N-API and requires Node@8.9 or later.

We already have some packages written by napi-rs: node-rs

One nice feature is that this crate allows you to build add-ons purely with the Rust toolchain and without involving node-gyp.

Taste

You can start from package-template to play with napi-rs

Define JavaScript functions

#[js_function(1)] // ------> arguments length, omit for zero
fn fibonacci(ctx: CallContext) -> Result<JsNumber> {
  let n = ctx.get::<JsNumber>(0)?.try_into()?;
  ctx.env.create_int64(fibonacci_native(n))
}

#[inline]
fn fibonacci_native(n: i64) -> i64 {
  match n {
    1 | 2 => 1,
    _ => fibonacci_native(n - 1) + fibonacci_native(n - 2),
  }
}

Register module

/// test_module is Module name
register_module!(test_module, init);

/// Module is `module` object in NodeJS
fn init(module: &mut Module) -> Result<()> {
  module.create_named_method("fibonacci", fibonacci)?;
}

Building

This repository is a Cargo crate. Any napi-based add-on should contain Cargo.toml to make it a Cargo crate.

In your Cargo.toml you need to set the crate-type to "cdylib" so that cargo builds a C-style shared library that can be dynamically loaded by the Node executable. You'll also need to add this crate as a dependency.

[lib]
crate-type = ["cdylib"]

[dependencies]
napi = "0.5"
napi-derive = "0.5"

[build-dependencies]
napi-build = "0.2"

And create build.rs in your own project:

// build.rs
extern crate napi_build;

fn main() {
  napi_build::setup();
}

So far, the napi build script has only been tested on macOS Linux and Windows x64 MSVC.

See the included test_module for an example add-on.

Run cargo build to produce the Dynamic lib file. And install the napi-rs to help you copy Dynamic lib file to .node file in case you can require it in your program.

{
  "package": "your pkg",
  "devDependencies": {
    "napi-rs": "latest"
  },
  "scripts": {
    "build": "cargo build && napi build",
    "build-release": "cargo build --release && napi build --release"
  }
}

Then you can require your native binding:

require('./target/debug|release/[module_name].node')

The module_name would be your package name in your Cargo.toml.

xxx => ./target/debug|release/xxx.node

xxx-yyy => ./target/debug|release/xxx_yyy.node

You can also copy Dynamic lib file to an appointed location:

napi build [--release] .
napi build [--release] ./mylib

Testing

Because libraries that depend on this crate must be loaded into a Node executable in order to resolve symbols, all tests are written in JavaScript in the test_module subdirectory.

To run tests:

yarn build:test
yarn test

Features table

Create JavaScript values

NAPINAPI VersionMinimal Node versionStatus
napi_create_array1v8.0.0
napi_create_array_with_length1v8.0.0
napi_create_arraybuffer1v8.0.0
napi_create_buffer1v8.0.0
napi_create_buffer_copy1v8.0.0
napi_create_date5v11.11.0
napi_create_external1v8.0.0
napi_create_external_arraybuffer1v8.0.0
napi_create_external_buffer1v8.0.0
napi_create_object1v8.0.0
napi_create_symbol1v8.0.0
napi_create_typedarray1v8.0.0
napi_create_dataview1v8.3.0
napi_create_int321v8.4.0
napi_create_uint321v8.4.0
napi_create_int641v8.4.0
napi_create_double1v8.4.0
napi_create_bigint_int646v10.7.0
napi_create_bigint_uint646v10.7.0
napi_create_bigint_words6v10.7.0
napi_create_string_latin11v8.0.0
napi_create_string_utf161v8.0.0
napi_create_string_utf81v8.0.0

Functions to convert from N-API to C types

NAPINAPI VersionMinimal Node VersionStatus
napi_get_array_length1v8.0.0
napi_get_arraybuffer_info1v8.0.0
napi_get_buffer_info1v8.0.0
napi_get_prototype1v8.0.0
napi_get_typedarray_info1v8.0.0
napi_get_dataview_info1v8.3.0
napi_get_date_value5v11.11.0
napi_get_value_bool1v8.0.0
napi_get_value_double1v8.0.0
napi_get_value_bigint_int646v10.7.0
napi_get_value_bigint_uint646v10.7.0
napi_get_value_bigint_words6v10.7.0
napi_get_value_external1v8.0.0
napi_get_value_int321v8.0.0
napi_get_value_int641v8.0.0
napi_get_value_string_latin11v8.0.0
napi_get_value_string_utf81v8.0.0
napi_get_value_string_utf161v8.0.0
napi_get_value_uint321v8.0.0
napi_get_boolean1v8.0.0
napi_get_global1v8.0.0
napi_get_null1v8.0.0
napi_get_undefined1v8.0.0

Working with JavaScript Values and Abstract Operations

NAPINAPI VersionMinimal Node VersionStatus
napi_coerce_to_bool1v8.0.0
napi_coerce_to_number1v8.0.0
napi_coerce_to_object1v8.0.0
napi_coerce_to_string1v8.0.0
napi_typeof1v8.0.0
napi_instanceof1v8.0.0
napi_is_array1v8.0.0
napi_is_arraybuffer1v8.0.0
napi_is_buffer1v8.0.0
napi_is_date1v8.0.0
napi_is_error1v8.0.0
napi_is_typedarray1v8.0.0
napi_is_dataview1v8.3.0
napi_strict_equals1v8.0.0
napi_detach_arraybuffer7v13.3.0
napi_is_detached_arraybuffer7v13.3.0
0.3.10

3 years ago

0.4.0-alpha.0

3 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.1.1

5 years ago

0.1.0

6 years ago