$ show neon-bindings/neon
Neon is a Rust framework for building safe and fast native Node.js addons via the Node-API (N-API). It provides a comprehensive type system that maps JavaScript values to Rust types, procedural macros for exporting functions, classes, and constants, and async/threading primitives for offloading work to background threads. The repository also includes CLI tools: `create-neon` for scaffolding new projects with CI/CD templates, and `cargo-cp-artifact` for copying build artifacts. The core library (`crates/neon`) is the primary deliverable, consumed as a Rust dependency. The architecture is a Rust workspace with a procedural macro crate (`neon-macros`), plus npm packages for tooling. The codebase demonstrates high craftsmanship with safe abstractions over unsafe FFI, composable type extraction patterns, and a macro-driven export system that automates registration. It is actively maintained with extensive test suites including compile-fail UI tests, integration tests, and benchmarks.
The LLM identified 5 files worth highlighting for reuse:
Implements `Channel` for scheduling closures on the main Node.js thread from worker threads. Uses N-API thread-safe functions (TSFN) to enqueue work and settle promises. This is a reusable pattern for any Rust library that needs to bridge async work back to a JavaScript event loop.
Provides `Root<T>` for creating thread-safe, context-independent references to JavaScript objects. Uses N-API reference counting to keep objects alive across threads. This pattern is essential for any FFI library that needs to hold JS values beyond a single function call.
Defines the `TryFromJs` and `TryIntoJs` traits and composable extractors like `Json<T>`, `Either<L,R>`, `Array<T>`, and `Buffer<B>`. This pattern allows ergonomic, type-safe conversion between Rust and JavaScript values with automatic error handling. The trait design is reusable in any Rust-to-JS binding project.
Wraps N-API's `napi_threadsafe_function` with a safe Rust API. Manages callbacks, queuing, and cleanup. This is a low-level but critical component for async operations, demonstrating safe encapsulation of a complex C API.
Implements the `#[neon::export]` procedural macro that automatically generates wrapper functions for Rust functions, constants, statics, and classes. Handles context detection, argument extraction, async/task variants, and JSON serialization. The macro pattern is reusable for any framework that needs to generate FFI bindings from annotated Rust items.