2020-02-18 22:09:17 +09:00
# napi-rs
2018-04-28 17:26:38 +09:00
2021-07-05 14:28:10 +09:00
< a href = "https://stakes.social/0x2C9F5c3ebC01A45D34198229E60eE186eCDc5C5E" > < img src = "https://badge.devprotocol.xyz/0x2C9F5c3ebC01A45D34198229E60eE186eCDc5C5E/descriptive" alt = "Stake to support us" > < / img > < / a >
2021-08-09 23:12:10 +09:00
< a href = "https://discord.gg/SpWzYHsKHs" >
< img src = "https://img.shields.io/discord/874290842444111882.svg?logo=discord&style=flat-square"
alt="chat" />
< / a >
2021-07-05 14:28:10 +09:00
2018-04-28 17:26:38 +09:00
> This project was initialized from [xray](https://github.com/atom/xray)
2021-04-21 18:19:45 +09:00
A minimal library for building compiled `Node.js` add-ons in `Rust` .
2020-09-17 20:04:05 +09:00
2021-10-31 22:51:53 +09:00
_Main branch is now under napi@next developing. Checkout [v1 docs ](https://napi.rs ) for napi@1.x._
2020-08-03 17:23:15 +09:00
< p >
2020-08-03 17:26:20 +09:00
< a href = "https://docs.rs/crate/napi" > < img src = "https://docs.rs/napi/badge.svg" > < / img > < / a >
2020-08-03 17:23:15 +09:00
< a href = "https://crates.io/crates/napi" > < img src = "https://img.shields.io/crates/v/napi.svg" > < / img > < / a >
2020-12-03 00:45:27 +09:00
< a href = "https://www.npmjs.com/package/@napi-rs/cli" > < img src = "https://img.shields.io/npm/v/@napi-rs/cli.svg" > < / img > < / a >
2020-08-03 17:23:15 +09:00
< / p >
2020-08-03 13:21:22 +09:00
2021-06-07 23:39:26 +09:00
## Ecosystem
< p align = "center" >
< a href = "https://www.prisma.io/" target = "_blank" >
2021-07-30 13:59:38 +09:00
< img alt = "Prisma" src = "./images/prisma.svg" height = "50px" >
2021-06-07 23:39:26 +09:00
< / a >
< a href = "https://swc.rs/" target = "_blank" >
2021-07-30 13:59:38 +09:00
< img alt = "swc" src = "https://raw.githubusercontent.com/swc-project/logo/master/swc.png" height = "50px" >
2021-06-07 23:39:26 +09:00
< / a >
< a href = "https://parceljs.org/" target = "_blank" >
2021-07-30 13:59:38 +09:00
< img alt = "Parcel" src = "https://user-images.githubusercontent.com/19409/31321658-f6aed0f2-ac3d-11e7-8100-1587e676e0ec.png" height = "50px" >
< / a >
< a href = "https://nextjs.org/" >
< img alt = "next.js" src = "https://assets.vercel.com/image/upload/v1607554385/repositories/next-js/next-logo.png" height = "50px" >
< img alt = "nextjs.svg" src = "./images/nextjs.svg" height = "50px" >
2021-06-07 23:39:26 +09:00
< / a >
< / p >
## Platform Support
2020-02-18 22:09:17 +09:00
2020-10-11 23:19:38 +09:00
![Lint ](https://github.com/napi-rs/napi-rs/workflows/Lint/badge.svg )
2020-10-15 17:44:23 +09:00
![Linux N-API@3 ](https://github.com/napi-rs/napi-rs/workflows/Linux%20N-API@3/badge.svg )
![Linux musl ](https://github.com/napi-rs/napi-rs/workflows/Linux%20musl/badge.svg )
![macOS/Windows/Linux x64 ](https://github.com/napi-rs/napi-rs/workflows/macOS/Windows/Linux%20x64/badge.svg )
![Linux-aarch64 ](https://github.com/napi-rs/napi-rs/workflows/Linux-aarch64/badge.svg )
2020-12-08 12:23:26 +09:00
![Linux-armv7 ](https://github.com/napi-rs/napi-rs/workflows/Linux-armv7/badge.svg )
2020-12-09 16:50:34 +09:00
![macOS-Android ](https://github.com/napi-rs/napi-rs/workflows/macOS-Android/badge.svg )
2020-11-04 23:52:13 +09:00
![Windows i686 ](https://github.com/napi-rs/napi-rs/workflows/Windows%20i686/badge.svg )
2021-06-01 00:21:56 +09:00
[![Windows arm64 ](https://github.com/napi-rs/napi-rs/actions/workflows/windows-arm.yml/badge.svg )](https://github.com/napi-rs/napi-rs/actions/workflows/windows-arm.yml)
2020-12-03 17:30:14 +09:00
[![FreeBSD ](https://api.cirrus-ci.com/github/napi-rs/napi-rs.svg )](https://cirrus-ci.com/github/napi-rs/napi-rs?branch=main)
2020-02-18 22:09:17 +09:00
2021-04-21 18:19:45 +09:00
| | node12 | node14 | node16 |
| --------------------- | ------ | ------ | ------ |
| Windows x64 | ✓ | ✓ | ✓ |
| Windows x86 | ✓ | ✓ | ✓ |
2021-06-01 00:21:56 +09:00
| Windows arm64 | ✓ | ✓ | ✓ |
2021-04-21 18:19:45 +09:00
| macOS x64 | ✓ | ✓ | ✓ |
| macOS aarch64 | ✓ | ✓ | ✓ |
| Linux x64 gnu | ✓ | ✓ | ✓ |
| Linux x64 musl | ✓ | ✓ | ✓ |
| Linux aarch64 gnu | ✓ | ✓ | ✓ |
2021-06-01 00:21:56 +09:00
| Linux aarch64 musl | ✓ | ✓ | ✓ |
2021-04-21 18:19:45 +09:00
| Linux arm gnueabihf | ✓ | ✓ | ✓ |
| Linux aarch64 android | ✓ | ✓ | ✓ |
| FreeBSD x64 | ✓ | ✓ | ✓ |
2020-02-18 22:09:17 +09:00
2021-05-20 05:22:40 +09:00
This library depends on Node-API and requires `Node@10.0.0` or later.
2020-06-21 20:10:06 +09:00
We already have some packages written by `napi-rs` : [node-rs ](https://github.com/napi-rs/node-rs )
2018-04-28 17:26:38 +09:00
2020-12-03 18:17:40 +09:00
One nice feature is that this crate allows you to build add-ons purely with the `Rust/JavaScript` toolchain and without involving `node-gyp` .
2018-04-28 17:26:38 +09:00
2020-04-30 19:57:04 +09:00
## Taste
2020-05-06 00:13:23 +09:00
2020-09-07 11:46:51 +09:00
> You can start from [package-template](https://github.com/napi-rs/package-template) to play with `napi-rs`
### Define JavaScript functions
2020-04-30 19:58:29 +09:00
```rust
2021-09-23 02:29:09 +09:00
#[macro_use]
extern crate napi;
2021-10-31 22:51:53 +09:00
/// import the preludes
2021-09-23 02:29:09 +09:00
use napi::bindgen_prelude::*;
2020-04-30 19:57:04 +09:00
2021-11-10 14:15:54 +09:00
/// module registration is done by the runtime, no need to explicitly do it now.
2021-09-23 02:29:09 +09:00
#[napi]
fn fibonacci(n: u32) -> u32 {
2020-04-30 19:57:04 +09:00
match n {
1 | 2 => 1,
2021-10-31 22:51:53 +09:00
_ => fibonacci(n - 1) + fibonacci(n - 2),
2020-04-30 19:57:04 +09:00
}
}
2020-09-07 11:46:51 +09:00
2021-09-23 02:29:09 +09:00
/// use `Fn` , `FnMut` or `FnOnce` traits to defined JavaScript callbacks
/// the return type of callbacks can only be `Result` .
#[napi]
fn get_cwd< T: Fn ( String ) - > Result< ()>>(callback: T) {
callback(env::current_dir().unwrap().to_string_lossy().to_string()).unwrap();
2020-12-03 18:17:40 +09:00
}
2020-12-17 19:59:29 +09:00
2021-09-23 02:29:09 +09:00
/// or, define the callback signature in where clause
#[napi]
fn test_callback< T > (callback: T)
where T: Fn(String) -> Result< ()>
{}
2021-10-31 22:51:53 +09:00
/// async fn, require `async` feature enabled.
/// [dependencies]
/// napi = {version="2", features=["async"]}
#[napi]
async fn read_file_async(path: String) -> Result< Buffer > {
tokio::fs::read(path)
.map(|r| match r {
Ok(content) => Ok(content.into()),
Err(e) => Err(Error::new(
Status::GenericFailure,
format!("failed to read file, {}", e),
)),
})
.await
}
2020-09-07 11:46:51 +09:00
```
2021-10-31 22:51:53 +09:00
more examples at [examples ](./examples/napi )
2021-09-23 02:29:09 +09:00
2018-04-28 17:26:38 +09:00
## Building
2020-12-03 18:17:40 +09:00
This repository is a `Cargo` crate. Any napi-based add-on should contain `Cargo.toml` to make it a Cargo crate.
2018-04-28 17:26:38 +09:00
2020-02-19 00:05:13 +09:00
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.
2018-04-28 17:26:38 +09:00
2020-02-19 00:05:13 +09:00
```toml
2020-12-03 18:17:40 +09:00
[package]
name = "awesome"
2018-04-28 17:26:38 +09:00
[lib]
crate-type = ["cdylib"]
2020-02-19 00:05:13 +09:00
[dependencies]
2021-09-23 02:29:09 +09:00
napi = "2"
napi-derive = "2"
2020-02-19 00:05:13 +09:00
[build-dependencies]
2021-04-21 18:19:45 +09:00
napi-build = "1"
2018-04-28 17:26:38 +09:00
```
2020-02-19 00:05:13 +09:00
And create `build.rs` in your own project:
2020-05-06 00:13:23 +09:00
```rust
2020-02-19 00:05:13 +09:00
// build.rs
extern crate napi_build;
fn main() {
napi_build::setup();
}
```
2018-04-28 17:26:38 +09:00
2020-12-03 18:17:40 +09:00
So far, the `napi` build script has only been tested on `macOS` `Linux` `Windows x64 MSVC` and `FreeBSD` .
2018-04-28 17:26:38 +09:00
2020-12-03 18:17:40 +09:00
Install the `@napi-rs/cli` to help you build your `Rust` codes and copy `Dynamic lib` file to `.node` file in case you can `require` it in your program.
2018-04-28 17:26:38 +09:00
2020-12-03 18:19:43 +09:00
```js
2018-04-28 17:26:38 +09:00
{
2020-12-03 18:17:40 +09:00
"package": "awesome-package",
2020-06-21 11:57:04 +09:00
"devDependencies": {
2020-12-03 18:17:40 +09:00
"@napi-rs/cli": "^1.0.0"
},
"napi": {
"name": "jarvis" // < ----------- Config the name of native addon , or the napi command will use the name of `Cargo.toml` for the binary file name .
2020-02-19 00:05:13 +09:00
},
"scripts": {
2020-12-03 18:17:40 +09:00
"build": "napi build --release",
"build:debug": "napi build"
2018-04-28 17:26:38 +09:00
}
}
```
2020-02-19 00:05:13 +09:00
Then you can require your native binding:
```js
2020-12-03 18:17:40 +09:00
require('./jarvis.node')
2020-02-19 00:05:13 +09:00
```
The `module_name` would be your `package` name in your `Cargo.toml` .
2020-12-03 18:17:40 +09:00
`xxx => ./xxx.node`
2020-02-19 00:05:13 +09:00
2020-12-03 18:17:40 +09:00
`xxx-yyy => ./xxx_yyy.node`
2020-02-19 00:05:13 +09:00
You can also copy `Dynamic lib` file to an appointed location:
```bash
2020-12-03 18:17:40 +09:00
napi build [--release] ./dll
napi build [--release] ./artifacts
2020-02-19 00:05:13 +09:00
```
2018-04-28 17:26:38 +09:00
2020-12-03 18:17:40 +09:00
There are [documents ](./cli ) which contains more details about the `@napi-rs/cli` usage.
2018-04-28 17:26:38 +09:00
## 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:
```sh
2020-10-11 23:16:33 +09:00
yarn build:test
2020-06-21 18:00:40 +09:00
yarn test
2018-04-28 17:26:38 +09:00
```
2020-03-16 15:26:30 +09:00
2021-06-07 23:39:26 +09:00
## Related projects
- [neon ](https://www.neon-bindings.com )
- [node-bindgen ](https://github.com/infinyon/node-bindgen )
2020-03-16 15:26:30 +09:00
## Features table
2021-10-31 22:51:53 +09:00
| Rust Type | Node Type | [NAPI Version ](https://nodejs.org/api/n-api.html#n_api_node_api_version_matrix ) | Minimal Node version | Enable by `napi` feature |
| ----------------------- | ---------------------- | ------------------------------------------------------------------------------- | -------------------- | ------------------------ |
2021-09-23 02:29:09 +09:00
| u32 | Number | 1 | v8.0.0 |
| i32/i64 | Number | 1 | v8.0.0 |
| f64 | Number | 1 | v8.0.0 |
| bool | Boolean | 1 | v8.0.0 |
| String/& 'a str | String | 1 | v8.0.0 |
2021-10-31 22:51:53 +09:00
| Latin1String | String | 1 | v8.0.0 | latin1 |
2021-09-23 02:29:09 +09:00
| UTF16String | String | 1 | v8.0.0 |
| Object | Object | 1 | v8.0.0 |
2021-10-31 22:51:53 +09:00
| serde_json::Map | Object | 1 | v8.0.0 | serde-json |
| serde_json::Value | any | 1 | v8.0.0 | serde-json |
2021-09-23 02:29:09 +09:00
| Array | Array< any > | 1 | v8.0.0 |
| Vec< T > | Array< T > | 1 | v8.0.0 |
| Buffer | Buffer | 1 | v8.0.0 |
| Null | null | 1 | v8.0.0 |
| Undefined/() | undefined | 1 | v8.0.0 |
| Result< ()> | Error | 1 | v8.0.0 |
2021-10-31 22:51:53 +09:00
| T: Fn(...) -> Result< T > | Function | 1 | v8.0.0 |
| Async/Future | Promise< T > | 4 | v10.6.0 | async |
2021-11-11 01:33:51 +09:00
| AsyncTask | Promise< T > | 1 | v8.5.0 |
2021-09-23 02:29:09 +09:00
| (NOT YET) | global | 1 | v8.0.0 |
2021-11-11 01:33:51 +09:00
| JsSymbol | Symbol | 1 | v8.0.0 |
2021-09-23 02:29:09 +09:00
| (NOT YET) | ArrayBuffer/TypedArray | 1 | v8.0.0 |
2021-11-12 18:22:57 +09:00
| JsFunction | threadsafe function | 4 | v10.6.0 | napi4 |
2021-11-11 01:33:51 +09:00
| BigInt | BigInt | 6 | v10.7.0 | napi6 |