node_api

Crates.ionode_api
lib.rsnode_api
version0.5.0
sourcesrc
created_at2021-06-16 16:39:27.278727
updated_at2022-02-27 17:15:55.801789
descriptionWrite Node.js native addons with Node-API in Rust.
homepagehttps://github.com/tangramdotdev/node_api
repositoryhttps://github.com/tangramdotdev/node_api
max_upload_size
id410970
size62,521
David Yamnitsky (nitsky)

documentation

https://docs.rs/node_api

README

node_api

This crate provides bindings to the Node-API C API, making it easy to write Node native addons in Rust.

Example

Write your native addon in Rust:

node_api::init!(init);

fn init(env: node_api::Env, exports: node_api::Value) -> Result<node_api:Value, String> {
  let exports = export.as_object();
  let key = node_api::String::new(env, "add")?;
  let value = node_api::Function::new(env, "add", add)?;
  exports.set(key, value);
  Ok(exports.value())
}

#[node_api::function]
fn add(a: u64, b: u64) -> Result<u64, String> {
  Ok(a + b)
}

Then load it in Node.js.

let native = require("./add.node");
assert(native.add(3, 4) === 7);

See the examples folder for a complete example.

Serde integration

To make it easy to move data structures between Rust and Node.js, the node_api crate supports integration with serde.

#[derive(serde::Serialize, serde::Deserialize)]
struct Contact {
  name: String,
  email: String,
}

#[node_api::function]
fn contact(env: node_api::Env) -> node_api::Result<Contact> {
  Ok(Contact {
    name: "John Doe".to_owned(),
    email: "john.doe@example.com".to_owned(),
  })
}
let contact = native.contact();
console.log("Contact name: " + contact.name);
console.log("Contact email: " + contact.email);
Commit count: 19

cargo fmt