A framework for building compiled Node.js add-ons in Rust via Node-API
OTHER License
Bot releases are hidden (Show)
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
optional types
from Rust Option
#[napi]
fn map_option(val: Option<u32>) -> Option<u32> {
val.map(|v| v + 1)
}
export function mapOption(val?: number | undefined | null): number | undefined | null
#[napi]
pub struct Context {
data: String,
pub maybe_need: Option<bool>,
}
export class Context {
maybeNeed?: boolean | undefined | null
}
#[napi]
fn read_file<T: Fn(Result<()>, Option<String>) -> Result<()>>(callback: T) {
match read_file_content() {
Ok(s) => callback(Ok(()), Some(s)),
Err(e) => callback(Err(e), None),
}
.unwrap();
}
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.0.0-alpha.6
Published by Brooooooklyn almost 3 years ago
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.0.0-alpha.5
Published by Brooooooklyn almost 3 years ago
Bigint
is renamed into BigInt
ValueType::Bigint
is renamed into ValueType::BigInt
Error
from fields, Error
must be created from Error::new
/Error::from
, Error::from_reason
await
JavaScript Promise
in Rust
This feature requires
napi4
andtokio_rt
features.
#[napi]
pub async fn async_plus_100(p: Promise<u32>) -> Result<u32> {
let v = p.await?;
Ok(v + 100)
}
test('await Promise in rust', async (t) => {
const fx = 20
const result = await asyncPlus100(
new Promise((resolve) => {
setTimeout(() => resolve(fx), 50)
}),
)
t.is(result, fx + 100)
})
test('Promise should reject raw error in rust', async (t) => {
const fxError = new Error('What is Happy Planet')
const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError)))
t.is(err, fxError)
})
Env
by @Brooooooklyn in https://github.com/napi-rs/napi-rs/pull/852
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.0.0-alpha.3
Published by Brooooooklyn almost 3 years ago
CString
usage #854 @BrooooooklynPublished by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
CommonJS
and ESM
ESM and CommonJS are both supported by the generated JavaScript binding file. Using const { fastFunction } = require('@awesome/addon')
or import { fastFunction } from '@awesome/addon'
as you need.
@node-rs/helper
dependencyThe @node-rs/helper
is convenient to load native binary cross platform and cpu arch. But it's not friendly to webpack
, vercel/nft
and vercel/ncc
because the logic is too dynamic.
It will close:
Published by Brooooooklyn almost 3 years ago
Task
resolve
and reject
now take &mut self
instead of self
. Add new fn finally
to excute after resolve
or reject
:
Ref
unref
now takes &mut self
instead of self
.
struct BufferLength(Ref<JsBufferValue>);
impl Task for BufferLength {
type Output = usize;
type JsValue = JsNumber;
fn compute(&mut self) -> Result<Self::Output> {
Ok(self.0.len() + 1)
}
- fn resolve(self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
- self.0.unref(env)?;
+ fn resolve(&mut self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
env.create_uint32(output as u32)
}
- fn reject(self, err: Error) -> Result<Self::JsValue> {
- self.0.unref(env)?;
- Err(err)
- }
+ fn finally(&mut self, env: Env) -> Result<()> {
+ self.0.unref(env)?;
+ Ok(())
+ }
}
Either
type #823 @BrooooooklynEither
types could be passed in as arguments or as return types:
#[napi]
fn either4(input: Either4<String, u32, bool, Obj>) -> u32 {
match input {
Either4::A(s) => s.len() as u32,
Either4::B(n) => n,
Either4::C(b) => {
if b {
1
} else {
0
}
}
Either4::D(f) => match f.v {
Either::A(s) => s.len() as u32,
Either::B(n) => n,
},
}
}
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
interface Obj {
v: string | number
}
export function either4(input: string | number | boolean | Obj): number
AsyncTask
with AbortSignal
support #831 @BrooooooklynStatic Factory
function for Class
#834 @BrooooooklynEnv::throw
to throw any JavaScript Value e0671fe @BrooooooklynSyntaxError
:#[js_function(1)]
pub fn throw_syntax_error(ctx: CallContext) -> Result<JsUndefined> {
let message: JsString = ctx.get(0)?;
let syntax_error = ctx
.env
.get_global()?
.get_named_property::<JsFunction>("SyntaxError")?;
ctx.env.throw(syntax_error.new(&[message])?)?;
ctx.env.get_undefined()
}
Published by Brooooooklyn almost 3 years ago
SyntaxError
:#[js_function(1)]
pub fn throw_syntax_error(ctx: CallContext) -> Result<JsUndefined> {
let message: JsString = ctx.get(0)?;
let syntax_error = ctx
.env
.get_global()?
.get_named_property::<JsFunction>("SyntaxError")?;
ctx.env.throw(syntax_error.new(&[message])?)?;
ctx.env.get_undefined()
}
Published by Brooooooklyn almost 3 years ago
⬇️⬇️⬇️⬇️⬇️⬇️
⬇️⬇️⬇️⬇️⬇️⬇️
serde
integrate @forehalo.d.ts
generation @forehaloVec<u8>
now will be generated as Buffer
Utf16String
and Latin1String
now will be generated as string
correctlyOption<T>
now will be generated as T | null
instead of T | undefined
Published by Brooooooklyn almost 3 years ago
Make sure CI fails if yarn test fails (#818) @joaomoreno
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn about 3 years ago
Published by Brooooooklyn about 3 years ago
Published by Brooooooklyn about 3 years ago
Published by Brooooooklyn about 3 years ago
Introduce #[napi] procedural macro to automation development boilerplate https://github.com/napi-rs/napi-rs/commit/2467b7139b1e96a315651254046bb2e1788be6c7
compat-mode
for napi
and napi-derive
crates for backward compatible.d.ts
file for native bindingAnd we got better performance because of the well-tuned code generation logic:
napi-rs | napi-rs-compat | neon | node-bindgen | |
---|---|---|---|---|
Sum (a + b) | 36 121 906 ops/s 100% | 36 021 114 ops/s 99.72% | 9 438 807 ops/s 26.13% | 7 522 062 ops/s 20.82% |
Concat(Hello + " world") | 4 526 668 ops/s 100% | 4 336 416 ops/s 95.95% | 3 973 988 ops/s 85.09% | 2 747 707 ops/s 60.93% |
The benchmark code: https://github.com/Brooooooklyn/rust-to-nodejs-overhead-benchmark
Published by Brooooooklyn about 3 years ago
Published by Brooooooklyn about 3 years ago
Published by Brooooooklyn about 3 years ago
drop_wrapped
implementation https://github.com/napi-rs/napi-rs/commit/5fe8003f9c9cc2d80f40a1af0364b41335a0495b