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 - [email protected]

Published by Brooooooklyn almost 3 years ago

Features

⚠️ There is no official Node.js build for Windows ARM64 for now: https://github.com/nodejs/build/issues/2450

So if you want build library for Windows ARM64, you can not use experimental features for now.

napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

What's Changed

#[napi]
fn convert_u32_array(input: Uint32Array) -> Vec<u32> {
  // AsRef<[u32]>, AsMut<[u32]>, Deref<[u32]>, DerefMut<u32>
  input.to_vec()
}

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

export function convertU32Array(input: Uint32Array): Array<number>

New Contributors

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

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

Published by Brooooooklyn almost 3 years ago

2.0.0-beta.0 (2021-12-02)

Features

  • cli: fail the pipeline if artifacts not been built (5f22203)
  • cli: support android armv7 target (68b0483)
  • napi: support TypedArray input and output (d9c53d7)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn almost 3 years ago

1.1.0 (2021-12-02)

Features

  • triples: support android armv7 (809350b)
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Features

  • Output Rust doc comments in definitions as jsdoc comments #885 @timfish
/// `constructor` option for `struct` requires all fields to be public,
/// otherwise tag impl fn as constructor
/// #[napi(constructor)]
#[napi]
pub struct Animal {
  #[napi(readonly)]
  /// Kind of animal
  pub kind: Kind,

  name: String,
}

#[napi]
impl Animal {
  /// This is the constructor
  #[napi(constructor)]
  pub fn new(kind: Kind, name: String) -> Self {
    Animal { kind, name }
  }

  /// This is a factory method
  #[napi(factory)]
  pub fn with_kind(kind: Kind) -> Self {
    Animal {
      kind,
      name: "Default".to_owned(),
    }
  }

  #[napi(getter)]
  pub fn get_name(&self) -> &str {
    self.name.as_str()
  }

  #[napi(setter)]
  pub fn set_name(&mut self, name: String) {
    self.name = name;
  }

  /// This is a
  /// multi-line comment
  /// with an emoji 🚀
  #[napi]
  pub fn whoami(&self) -> String {
    match self.kind {
      Kind::Dog => {
        format!("Dog: {}", self.name)
      }
      Kind::Cat => format!("Cat: {}", self.name),
      Kind::Duck => format!("Duck: {}", self.name),
    }
  }

  #[napi]
  /// This is static...
  pub fn get_dog_kind() -> Kind {
    Kind::Dog
  }
}

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

/**
 * `constructor` option for `struct` requires all fields to be public,
 * otherwise tag impl fn as constructor
 * #[napi(constructor)]
 */
export class Animal {
  /** Kind of animal */
  readonly kind: Kind
  /** This is the constructor */
  constructor(kind: Kind, name: string)
  /** This is a factory method */
  static withKind(kind: Kind): Animal
  get name(): string
  set name(name: string)
  /**
   * This is a
   * multi-line comment
   * with an emoji 🚀
   */
  whoami(): string
  /** This is static... */
  static getDogKind(): Kind
}
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Breaking Changes

  • Remove rust enum to avoid compatible issue #887 @Brooooooklyn
  • threadsafe_function related types now is under the napi4 feature flag @Brooooooklyn
  • Add experimental flag and node_api_get_module_file_name function @Brooooooklyn
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Features

  • Support rename function args and return type #884 @Brooooooklyn
use napi::bindgen_prelude::*;
use napi_derive::napi;

#[napi(ts_args_type = "a: { foo: number }", ts_return_type = "string[]")]
fn ts_rename(a: Object) -> Result<Object> {
  a.get_property_names()
}

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

export function tsRename(a: { foo: number }): string[]
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Bugfix

  • Fix cargo:rerun-if commands #871 @sam3d
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

What's Changed

Support export rust mod as JavaScript Object

#[napi]
mod xxh3 {
  use napi::bindgen_prelude::{BigInt, Buffer};

  #[napi]
  pub const ALIGNMENT: u32 = 16;

  #[napi(js_name = "xxh3_64")]
  pub fn xxh64(input: Buffer) -> u64 {
    let mut h: u64 = 0;
    for i in input.as_ref() {
      h = h.wrapping_add(*i as u64);
    }
    h
  }
}

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

export namespace xxh3 {
  export const ALIGNMENT: number
  export function xxh3_64(input: Buffer): BigInt
  export function xxh128(input: Buffer): BigInt
}

Support return &Self for method chain

#[napi]
pub struct ClassWithFactory {
  pub name: String,
}

#[napi]
impl ClassWithFactory {
  #[napi(factory)]
  pub fn with_name(name: String) -> Self {
    Self { name }
  }

  #[napi]
  pub fn set_name(&mut self, name: String) -> &Self {
    self.name = name;
    self
  }
}

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

export class ClassWithFactory {
  name: string
  static withName(name: string): ClassWithFactory
  setName(name: string): this
}

New Contributors

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

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

Published by Brooooooklyn almost 3 years ago

2.0.0-alpha.11 (2021-11-25)

Features

  • napi: support export rust mod as ts namespace (1fe39ff)
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

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

Published by Brooooooklyn almost 3 years ago

2.0.0-alpha.10 (2021-11-21)

Features

  • cli: create pre-release if tag includes alpha/beta/rc (7b797d3)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn almost 3 years ago

2.0.0-alpha.8 (2021-11-21)

Features

  • cli: export android toolchains to PATH before build (dca5ada)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn almost 3 years ago

2.0.0-alpha.7 (2021-11-21)

Bug Fixes

  • cli: ExternalObject type decalare (1f64f9f)
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Features 🎉

feat(napi-derive): support const export by @Brooooooklyn in https://github.com/napi-rs/napi-rs/pull/863

#[napi]
pub const DEFAULT_COST: u32 = 12;

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

export const DEFAULT_COST: number

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

import { DEFAULT_COST } from './index.js'

console.log(DEFAULT_COST) // 12

feat(napi): implement external value by @Brooooooklyn in https://github.com/napi-rs/napi-rs/pull/868

#[napi]
pub fn create_external(size: u32) -> External<u32> {
  External::new(size)
}

#[napi]
pub fn get_external(external: External<u32>) -> u32 {
  *external.as_ref()
}

#[napi]
pub fn mutate_external(mut external: External<u32>, new_val: u32) {
  *external.as_mut() = new_val;
}

#[napi]
pub fn create_external_string(content: String) -> External<String> {
  External::new(content)
}

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

export class ExternalObject<T> {
  private readonly __type: unique symbol;
  [val: unique symbol]: T
}

export function createExternal(size: number): ExternalObject<number>
export function getExternal(external: ExternalObject<number>): number
export function mutateExternal(external: ExternalObject<number>, newVal: number): void
export function createExternalString(content: string): ExternalObject<string>

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

import test from 'ava'

import { createExternal, getExternal, mutateExternal } from './index.js'

test('external', (t) => {
  const FX = 42
  const ext = createExternal(FX)
  t.is(getExternal(ext), FX)
  mutateExternal(ext, FX + 1)
  t.is(getExternal(ext), FX + 1)
  // Can not pass non `ExternalObject` type to functions accept `ExternalObject`
  // @ts-expect-error
  t.throws(() => getExternal({}))
  const ext2 = createExternalString('wtf')
  // Can not assign `ExternalObject<string>` to `ExternalObject<number>`
  // @ts-expect-error
  const e = t.throws(() => getExternal(ext2))
  t.is(e.message, 'T on `get_value_external` is not the type of wrapped object')
})

Bugfix

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

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

Published by Brooooooklyn almost 3 years ago

2.0.0-alpha.6 (2021-11-21)

Features

  • cli: refactor cli build (4c3fe26)
  • napi: implement external value (bdfb150)
napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

Bugfix

napi-rs - [email protected]

Published by Brooooooklyn almost 3 years ago

What's Changed

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