napi-rs

A framework for building compiled Node.js add-ons in Rust via Node-API

OTHER License

Downloads
6.9M
Stars
5.4K
Committers
119

Bot releases are hidden (Show)

napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn almost 3 years ago

2.0.0-alpha.5 (2021-11-16)

Bug Fixes

  • always add a newline at the end of the file when generating js-binding.js (753bb1e)

Features

  • napi: add pipe flag to pipe the generated files into custom command (e37c3fd)
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

What's Changed

  • Generate TypeScript 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

napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

What's Changed

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.0.0-alpha.5

napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Breaking changes

  • Bigint is renamed into BigInt
  • ValueType::Bigint is renamed into ValueType::BigInt
  • Deprecate creating Error from fields, Error must be created from Error::new/Error::from, Error::from_reason

Highlight features 🚀

await JavaScript Promise in Rust

This feature requires napi4 and tokio_rt features.

#[napi]
pub async fn async_plus_100(p: Promise<u32>) -> Result<u32> {
  let v = p.await?;
  Ok(v + 100)
}

Resolve value

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)
})

Reject error

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)
})

What's Changed

New Contributors

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.0.0-alpha.3

napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Bugfix

  • Wrong CString usage #854 @Brooooooklyn
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Bugfix

  • fix(napi-derive-backend): wrong restrict on Result return type 413a555 @Brooooooklyn
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn almost 3 years ago

Generate JavaScript binding file for native addons

Compatible with both 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.

No more need @node-rs/helper dependency

The @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:

napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

🚧 Breaking Changes

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(())
+  }
}


Features

Either type #823 @Brooooooklyn

Either 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 @Brooooooklyn

carbon(5)

carbon(3)

Static Factory function for Class #834 @Brooooooklyn

Env::throw to throw any JavaScript Value e0671fe @Brooooooklyn

Throw 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()
}
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Features

  • feat(napi): implement Env::throw to throw any JsValue #839 @Brooooooklyn

Throw 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()
}
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

[email protected]

Async with Tokio @forehalo

⬇️⬇️⬇️⬇️⬇️⬇️

Throw JavaScript Error @forehalo

⬇️⬇️⬇️⬇️⬇️⬇️

Better serde integrate @forehalo

Better TypeScript .d.ts generation @forehalo

  • The return type of Vec<u8> now will be generated as Buffer
  • Utf16String and Latin1String now will be generated as string correctly
  • Option<T> now will be generated as T | null instead of T | undefined

Misc

  • perf(napi): ignore callback info while no arguments #769 @Brooooooklyn
  • feat(napi): impl from/to NapiValue for JsUnkown #817 @Brooooooklyn
  • feat(cli): refactor workflow generated by cli #814 @Brooooooklyn
  • ci: make sure CI fails if yarn test fails #818 @joaomoreno
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn almost 3 years ago

1.3.5 (2021-10-29)

Make sure CI fails if yarn test fails (#818) @joaomoreno

napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn almost 3 years ago

1.3.4 (2021-10-27)

Bug Fixes

  • cli: workflow file generated by new command (b60b6de)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn almost 3 years ago

2.0.0-alpha.3 (2021-10-27)

Bug Fixes

  • cli: workflow file generated by new command (cbb71a9)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 3 years ago

2.0.0-alpha.2 (2021-10-01)

Features

  • cli: strip android binary in CI (1c9a307)
napi-rs - [email protected]

Published by Brooooooklyn about 3 years ago

Perf

  • ignore callback info while no arguments e69a763
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 3 years ago

2.0.0-alpha.1 (2021-10-01)

Bug Fixes

  • cli: missing main and types field in created package.json (860a02a)

Features

  • cli: dts flag for build command (0e8de17)
napi-rs - 2.0.0-alpha.0

Published by Brooooooklyn about 3 years ago

Features

Introduce #[napi] procedural macro to automation development boilerplate https://github.com/napi-rs/napi-rs/commit/2467b7139b1e96a315651254046bb2e1788be6c7

  • napi procedural macro for basic rust/JavaScript types
  • introduce the compat-mode for napi and napi-derive crates for backward compatible
  • remove #[inline] and let compiler to decide the inline behavior
  • cli now can produce the .d.ts file for native binding
  • many tests and example for the new procedural macro

napi-rs

And 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

napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 3 years ago

1.3.3 (2021-09-19)

Bug Fixes

  • cli: version of binary optional dependencies should be pinned (27dbca8)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 3 years ago

1.3.2 (2021-09-14)

Bug Fixes

  • cli: cargo config path and ci template in new command (c385254)
napi-rs - [email protected]

Published by Brooooooklyn about 3 years ago