docs.rs failed to build napi-0.4.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: napi-3.0.0-alpha.2

High level NodeJS N-API binding

napi-rs provides minimal overhead to write N-API modules in Rust.

Feature flags

libuv

With libuv feature, you can execute a rust future in libuv in NodeJS, and return a promise object.

use std::thread;
use std::fs;

use futures::prelude::*;
use futures::channel::oneshot;
use napi::{CallContext, Result, JsString, JsObject, Status, Error};

#[js_function(1)]
pub fn uv_read_file(ctx: CallContext) -> Result<JsObject> {
let path = ctx.get::<JsString>(0)?;
let (sender, receiver) = oneshot::channel();
let p = path.as_str()?.to_owned();
thread::spawn(|| {
let res = fs::read(p).map_err(|e| Error::new(Status::Unknown, format!("{}", e)));
sender.send(res).expect("Send data failed");
});
ctx.env.execute(receiver.map_err(|e| Error::new(Status::Unknown, format!("{}", e))).map(|x| x.and_then(|x| x)), |&mut env, data| {
env.create_buffer_with_data(data)
})
}

tokio_rt

With tokio_rt feature, napi-rs provides a tokio runtime in an additional thread. And you can easily run tokio future in it and return promise.

use futures::prelude::*;
use napi::{CallContext, Error, JsObject, JsString, Result, Status};
use tokio;

#[js_function(1)]
pub fn tokio_readfile(ctx: CallContext) -> Result<JsObject> {
let js_filepath = ctx.get::<JsString>(0)?;
let path_str = js_filepath.as_str()?;
ctx.env.execute_tokio_future(
tokio::fs::read(path_str.to_owned())
.map(|v| v.map_err(|e| Error::new(Status::Unknown, format!("failed to read file, {}", e)))),
|&mut env, data| env.create_buffer_with_data(data),
)
}

Tokio channel in napi-rs buffer size is default 100.

You can adjust it via NAPI_RS_TOKIO_CHANNEL_BUFFER_SIZE environment variable

NAPI_RS_TOKIO_CHANNEL_BUFFER_SIZE=1000 node ./app.js