From c496181f57bcc9f9e375ab6eae7252713adaf971 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 17 Aug 2025 16:43:07 +0800 Subject: [PATCH 1/2] doc: add NAPI-RS reference to the Node-API doc --- doc/api/n-api.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 77257623157bdd..c763d44f80805d 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -78,6 +78,51 @@ it still gets the benefits of the ABI stability provided by the C API. When using `node-addon-api` instead of the C APIs, start with the API [docs][] for `node-addon-api`. +## Rust + +Node-API is also accessible from Rust through the [`NAPI-RS`][] project. +`NAPI-RS` provides safe Rust bindings to Node-API, allowing developers to write +native Node.js modules in Rust while maintaining ABI stability across Node.js +versions. The project offers both low-level bindings that closely mirror the +Node-API C interface and high-level, idiomatic Rust APIs that leverage Rust's +type system and memory safety guarantees. + +Similar to the C++ wrapper, `NAPI-RS` simplifies the development process. +For example, creating an object with a property in `NAPI-RS`: + +```rust +#[napi(object)] +pub struct MyObject { + foo: String, +} + +#[napi] +pub fn create_object() -> MyObject { + MyObject { + foo: "bar".to_string(), + } +} +``` + +This will automatically generate the following TypeScript definition: + +```typescript +export interface MyObject { + foo: string; +} + +export declare function createObject(): MyObject; +``` + +The `NAPI-RS` ecosystem includes: +- Build tooling that simplifies the compilation and packaging process +- TypeScript type generation for better IDE support +- Cross-platform compilation support, including `WebAssembly` + +When using `NAPI-RS` for Rust-based Node.js addons, refer to the +[NAPI-RS documentation](https://napi.rs) for comprehensive +guides and examples. + The [Node-API Resource](https://nodejs.github.io/node-addon-examples/) offers an excellent orientation and tips for developers just getting started with Node-API and `node-addon-api`. Additional media resources can be found on the @@ -6843,6 +6888,7 @@ the add-on's file name during loading. [`napi_unwrap`]: #napi_unwrap [`napi_wrap`]: #napi_wrap [`node-addon-api`]: https://github.com/nodejs/node-addon-api +[`NAPI-RS`]: https://github.com/napi-rs/napi-rs [`node_api.h`]: https://github.com/nodejs/node/blob/HEAD/src/node_api.h [`node_api_basic_finalize`]: #node_api_basic_finalize [`node_api_create_external_string_latin1`]: #node_api_create_external_string_latin1 From 47b4ef1beb0f48eb8c0cff309888a8d6ace920a8 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 18 Aug 2025 13:54:52 +0800 Subject: [PATCH 2/2] Fix lint issue --- doc/api/n-api.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index c763d44f80805d..5a1ccd3c46389b 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -80,7 +80,7 @@ for `node-addon-api`. ## Rust -Node-API is also accessible from Rust through the [`NAPI-RS`][] project. +Node-API is also accessible from Rust through the [NAPI-RS][] project. `NAPI-RS` provides safe Rust bindings to Node-API, allowing developers to write native Node.js modules in Rust while maintaining ABI stability across Node.js versions. The project offers both low-level bindings that closely mirror the @@ -106,7 +106,7 @@ pub fn create_object() -> MyObject { This will automatically generate the following TypeScript definition: -```typescript +```ts export interface MyObject { foo: string; } @@ -115,9 +115,10 @@ export declare function createObject(): MyObject; ``` The `NAPI-RS` ecosystem includes: -- Build tooling that simplifies the compilation and packaging process -- TypeScript type generation for better IDE support -- Cross-platform compilation support, including `WebAssembly` + +* Build tooling that simplifies the compilation and packaging process +* TypeScript type generation for better IDE support +* Cross-platform compilation support, including `WebAssembly` When using `NAPI-RS` for Rust-based Node.js addons, refer to the [NAPI-RS documentation](https://napi.rs) for comprehensive @@ -6789,6 +6790,7 @@ the add-on's file name during loading. [GYP]: https://gyp.gsrc.io [GitHub releases]: https://help.github.com/en/github/administering-a-repository/about-releases [LLVM]: https://llvm.org +[NAPI-RS]: https://github.com/napi-rs/napi-rs [Native Abstractions for Node.js]: https://github.com/nodejs/nan [Node-API Media]: https://github.com/nodejs/abi-stable-node/blob/HEAD/node-api-media.md [Object lifetime management]: #object-lifetime-management @@ -6888,7 +6890,6 @@ the add-on's file name during loading. [`napi_unwrap`]: #napi_unwrap [`napi_wrap`]: #napi_wrap [`node-addon-api`]: https://github.com/nodejs/node-addon-api -[`NAPI-RS`]: https://github.com/napi-rs/napi-rs [`node_api.h`]: https://github.com/nodejs/node/blob/HEAD/src/node_api.h [`node_api_basic_finalize`]: #node_api_basic_finalize [`node_api_create_external_string_latin1`]: #node_api_create_external_string_latin1