### Install cargo-cp-artifact Source: https://github.com/neon-bindings/neon/blob/main/pkgs/cargo-cp-artifact/README.md Install the cargo-cp-artifact command-line utility globally using npm. ```sh npm install -g cargo-cp-artifact ``` -------------------------------- ### Run the Neon Benchmarks Source: https://github.com/neon-bindings/neon/blob/main/bench/README.md Execute this command to run the performance benchmarks after the suite has been built. This will start the performance regression tests. ```sh $ npm run benchmark ``` -------------------------------- ### Initialize a New Neon Project Source: https://github.com/neon-bindings/neon/blob/main/README.md Use this command to create a new Node.js project with Neon bindings. Follow the Hello World guide to write your first addon. ```bash npm init neon@latest my-project ``` -------------------------------- ### Build the Neon Benchmark Suite Source: https://github.com/neon-bindings/neon/blob/main/bench/README.md Run this command to build the performance regression suite. Ensure you have completed any necessary project setup before building. ```sh $ npm run build ``` -------------------------------- ### Run All Neon Tests Source: https://github.com/neon-bindings/neon/blob/main/README.md Install all project dependencies and run the full test suite for the Neon project. This command is used for testing the entire Neon workspace. ```bash npm install npm test ``` -------------------------------- ### Explore Node Addon Exports Source: https://github.com/neon-bindings/neon/blob/main/test/rust-2024/README.md After installation and building, this demonstrates how to require and use the addon's functions in the Node console. ```sh $ npm i $ npm run build $ node > require('.').hello() 'hello node' ``` -------------------------------- ### Parse Cargo Artifacts from a File Source: https://github.com/neon-bindings/neon/blob/main/pkgs/cargo-cp-artifact/README.md Example of using cargo-cp-artifact to copy a cdylib artifact named 'my-crate' to 'lib/index.node' by parsing a pre-existing build output file. ```sh cargo-cp-artifact -a cdylib my-crate lib/index.node -- cat build-output.txt ``` -------------------------------- ### Create a Portable Neon Library Source: https://github.com/neon-bindings/neon/blob/main/pkgs/create-neon/README.md Generate a Neon project designed for portable, cross-platform libraries with pre-built binaries. This allows consumers to use Rust-based modules without needing a Rust installation. ```sh $ npm init neon[@latest] -- [ ...] --lib [ ...] my-project ``` -------------------------------- ### Configure npm Script for Cargo Build Source: https://github.com/neon-bindings/neon/blob/main/pkgs/cargo-cp-artifact/README.md Example package.json script that uses cargo-cp-artifact to build a cdylib artifact and place it at 'lib/index.node'. The crate name is inferred from the npm package name. ```json { "name": "my-crate", "scripts": { "build": "cargo-cp-artifact -nc lib/index.node -- cargo build --message-format=json-render-diagnostics" } } ``` -------------------------------- ### Wrap Cargo Build for Artifact Copying Source: https://github.com/neon-bindings/neon/blob/main/pkgs/cargo-cp-artifact/README.md Example of wrapping a `cargo build` command to copy a cdylib artifact named 'my-crate' to 'lib/index.node'. Ensure the cargo command uses JSON message format. ```sh cargo-cp-artifact -a cdylib my-crate lib/index.node -- cargo build --message-format=json-render-diagnostics ``` -------------------------------- ### JavaScript: Idiomatic User class interface (After N-API) Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md Provides an example of how to create a JavaScript class that wraps the Rust-created `JsBox` type, offering a clean interface for JavaScript consumers. ```javascript class User { constructor(firstName, lastName) { this.boxed = addon.createUser(firstName, lastName); } fullName() { return addon.userFullName(this.boxed); } } ``` -------------------------------- ### Create a Simple Neon Project Source: https://github.com/neon-bindings/neon/blob/main/pkgs/create-neon/README.md Use this command to create a basic Neon project consisting solely of Rust code. The `--` is required to pass options to Neon. ```sh $ npm init neon[@latest] -- [ ...] my-project ``` -------------------------------- ### Publish Release Source: https://github.com/neon-bindings/neon/wiki/Release-checklist Run this script to publish the new release of Neon. ```bash ./dev/publish.sh ``` -------------------------------- ### Basic Usage of cargo-cp-artifact Source: https://github.com/neon-bindings/neon/blob/main/pkgs/cargo-cp-artifact/README.md Demonstrates the general syntax for using cargo-cp-artifact to map artifact kinds and crate names to output files, followed by the command to wrap. ```sh cargo-cp-artifact --artifact artifact-kind crate-name output-file -- wrapped-command ``` -------------------------------- ### Execute npm Build Script with Additional Arguments Source: https://github.com/neon-bindings/neon/blob/main/pkgs/cargo-cp-artifact/README.md Demonstrates passing additional arguments (e.g., --feature=serde) to the wrapped cargo build command via the npm script. ```sh npm run build -- --feature=serde ``` -------------------------------- ### Publish N-API Pre-release with Unstaged Files Source: https://github.com/neon-bindings/neon/wiki/Release-checklist Manually publish an N-API pre-release using Cargo. This command allows publishing even with unstaged files. ```bash cargo publish --no-verify --allow-dirty ``` -------------------------------- ### Rust: Create and access User with JsBox (After N-API) Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md Demonstrates how to wrap a Rust `User` struct in a `JsBox` using `cx.boxed` and how to access it in another function. This replaces the `declare_types!` macro. ```rust fn create_user(mut cx: FunctionContext) -> JsResult> { let first_name = cx.argument::(0)?.value(&mut cx); let last_name = cx.argument::(1)?.value(&mut cx); Ok(cx.boxed(User { first_name, last_name })) } fn user_full_name(mut cx: FunctionContext) -> JsResult { let user = cx.argument::>(0)?; let full_name = user.full_name(); Ok(cx.string(full_name)) } ``` -------------------------------- ### Build with Additional Arguments Source: https://github.com/neon-bindings/neon/blob/main/RELEASES.md Use this command to build a Neon project with additional arguments passed to cargo build, such as features. ```sh neon build --release -- --features awesome ``` -------------------------------- ### Commit, Push, and Release on GitHub Source: https://github.com/neon-bindings/neon/wiki/Release-checklist This script handles committing changes, pushing to the remote repository, and initiating the release on GitHub. You will be prompted to input your personal access token. ```bash ./dev/commit.sh dherman ``` -------------------------------- ### Execute npm Build Script Source: https://github.com/neon-bindings/neon/blob/main/pkgs/cargo-cp-artifact/README.md Command to run the 'build' script defined in package.json, which utilizes cargo-cp-artifact. ```sh npm run build ``` -------------------------------- ### Select Minimum N-API Version Source: https://github.com/neon-bindings/neon/blob/main/RELEASES.md Specify the minimum required N-API version for a Neon module. This ensures compatibility with specific Node.js versions. ```toml neon = { version = "0.7", default-features = false, features = ["napi-4"] } ``` -------------------------------- ### Validate Repository Source: https://github.com/neon-bindings/neon/wiki/Release-checklist Execute this script to ensure the repository is in a valid state before proceeding with the release. Confirm that the validation passes. ```bash ./dev/validate.sh ``` -------------------------------- ### Enable N-API Backend in Cargo.toml Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md Configure your Cargo.toml to use the N-API backend by disabling default features and enabling a specific N-API version feature. ```toml [dependencies.neon] version = "0.9.1" default-features = false features = ["napi-4"] ``` -------------------------------- ### Build with Cargo Feature Source: https://github.com/neon-bindings/neon/blob/main/test/rust-2024/README.md Pass additional arguments to `cargo build` via npm, such as enabling a specific cargo feature. ```sh npm run build -- --feature=beetle ``` -------------------------------- ### Test a Specific Rust Crate in Neon Source: https://github.com/neon-bindings/neon/blob/main/README.md Execute tests for an individual Rust crate within the Neon Cargo workspace. This command targets the 'neon-build' crate. ```bash cargo test -p neon-build ``` -------------------------------- ### Create and Export a JavaScript Array from Rust Source: https://github.com/neon-bindings/neon/blob/main/README.md This Rust code demonstrates how to create a JavaScript array with different data types (number, string, boolean) and export it as a function to Node.js. It requires the 'neon' crate. ```rust fn make_an_array(mut cx: FunctionContext) -> JsResult { // Create some values: let n = cx.number(9000); let s = cx.string("hello"); let b = cx.boolean(true); // Create a new array: let array = cx.empty_array(); // Push the values into the array: array.set(&mut cx, 0, n)?; array.set(&mut cx, 1, s)?; array.set(&mut cx, 2, b)?; // Return the array: Ok(array) } #[neon::main] fn main(mut cx: ModuleContext) -> NeonResult<()> { cx.export_function("make_an_array", make_an_array)?; Ok(()) } ``` -------------------------------- ### Rust: Define User struct and full_name method (Before N-API) Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md This snippet shows the structure of a Rust `User` type and its `full_name` method before the N-API migration, used with the `declare_types!` macro. ```rust struct User { first_name: String, last_name: String, } impl User { fn full_name(&self) -> String { format!("{} {}", self.first_name, self.last_name) } } ``` -------------------------------- ### Bump Version Number Source: https://github.com/neon-bindings/neon/wiki/Release-checklist Run this script to increment the patch version of the release. Visually verify the printed diff afterwards. ```bash ./dev/bump.sh patch ``` -------------------------------- ### Migrate Event Handler API to Event Queue API Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md This snippet demonstrates replacing the deprecated Event Handler API with the Event Queue API. The new approach offers improved safety and ergonomics for handling asynchronous events. ```rust pub fn start_task(mut cx: FunctionContext) -> JsResult { let callback = cx.argument::(0)?; let handler = EventHandler::new(callback); thread::spawn(move || { let result = // compute the result... handler.schedule(move |cx| { vec![cx.number(result).upcast()] }); }); Ok(cx.undefined()) } ``` ```rust pub fn start_task(mut cx: FunctionContext) -> JsResult { let callback = cx.argument::(0)?.root(&mut cx); let queue = cx.queue(); std::thread::spawn(move || { let result = // compute the result... queue.send(move |mut cx| { let callback = callback.into_inner(&mut cx); let this = cx.undefined(); let args = vec![ cx.null().upcast::(), cx.number(result).upcast() ]; callback.call(&mut cx, this, args)?; Ok(()) }); }); Ok(cx.undefined()) } ``` -------------------------------- ### Handle Downcast Before Context Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md Illustrates the syntax for downcasting a Handle to a specific JavaScript type before context was required. ```rust value.downcast::() ``` -------------------------------- ### Migrate Task API to Event Queue API Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md This snippet shows the migration from the deprecated Task API to the Event Queue API for background computations. The new approach uses native threads and avoids competing with Node.js system internals. ```rust impl Task for MyTask { type Output = i32; type Error = String; type JsEvent = JsNumber; fn perform(&self) -> Result { // compute the result... } fn complete(self, mut cx: TaskContext, result: Result) -> JsResult { match result { Ok(n) => { Ok(cx.number(n)) } Err(s) => { cx.throw_error(s) } } } } pub fn start_task(mut cx: FunctionContext) -> JsResult { let callback = cx.argument::(0)?; MyTask.schedule(callback); Ok(cx.undefined()) } ``` ```rust pub fn start_task(mut cx: FunctionContext) -> JsResult { let callback = cx.argument::(0)?.root(&mut cx); let queue = cx.queue(); std::thread::spawn(move || { let result = // compute the result... queue.send(move |mut cx| { let callback = callback.into_inner(&mut cx); let this = cx.undefined(); let args = match result { Ok(n) => vec![ cx.null().upcast::(), cx.number(result).upcast() ], Err(msg) => vec![ cx.error(msg).upcast() ] }; callback.call(&mut cx, this, args)?; Ok(()) }); }); Ok(cx.undefined()) } ``` -------------------------------- ### Enable Channel API Feature Flag Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md To use the Channel API for concurrency, you must enable the 'channel-api' feature flag in your Cargo.toml. ```toml [dependencies.neon] version = "0.9.1" default-features = false features = ["napi-6", "channel-api"] ``` -------------------------------- ### Standardize `usize` for array indexing Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_1.0.0.md Update explicit types for array indices and lengths from `u32` to `usize` for consistency with Rust. ```rust fn example(mut cx: FunctionContext) -> JsResult { let arr = cx.empty_array(); let msg = cx.string("Hello!"); arr.set(&mut cx, 0u32, msg)?; Ok(cx.undefined()) } ``` ```rust fn example(mut cx: FunctionContext) -> JsResult { let arr = cx.empty_array(); let msg = cx.string("Hello!"); arr.set(&mut cx, 0usize, msg)?; Ok(cx.undefined()) } ``` -------------------------------- ### Update `CallContext` usage in `JsFunction` Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_1.0.0.md Replace `CallContext` with `FunctionContext` and use `cx.this::()?.` for accessing `this`. ```rust // `CallContext` is equivalent to `FunctionContext` fn example(mut cx: CallContext) -> JsResult { let a = cx.this().get::(&mut cx, "a")?; Ok(cx.undefined()) } ``` ```rust fn example(mut cx: FunctionContext) -> JsResult { let a = cx.this::()?.get::(&mut cx, "a")?; Ok(cx.undefined()) } ``` -------------------------------- ### Rust: Implement Finalize trait for User (After N-API) Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md Shows the minimal implementation required for a Rust type to be embedded using `JsBox` in the N-API backend. The `Finalize` trait has a default implementation. ```rust struct User { first_name: String, last_name: String, } impl Finalize for User { } ``` -------------------------------- ### Fix Soundness Hole in JsArrayBuffer/JsBuffer External Source: https://github.com/neon-bindings/neon/blob/main/RELEASES.md This Rust code demonstrates a soundness hole related to creating external JsArrayBuffer or JsBuffer from mutable slices without proper lifetime management. It shows how dropping the original data can lead to issues. ```rust pub fn soundness_hole(mut cx: FunctionContext) -> JsResult { let mut data = vec![0u8, 1, 2, 3]; // Creating an external from `&mut [u8]` instead of `Vec` since there is a blanket impl // of `AsMut for &mut T` let buf = JsArrayBuffer::external(&mut cx, data.as_mut_slice()); // `buf` is still holding a reference to `data`! drop(data); Ok(buf) } ``` -------------------------------- ### Handle Downcast After Context Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md Shows the updated syntax for downcasting a Handle, now requiring a mutable context reference. ```rust value.downcast::(&mut cx) ``` -------------------------------- ### Rust: Define JsUser class with declare_types! (Before N-API) Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_NAPI.md Illustrates the use of the deprecated `declare_types!` macro to define a JavaScript class `JsUser` from a Rust `User` struct. ```rust declare_types! { class JsUser for User { init(mut cx) { let first_name = cx.argument::(0)?; let last_name = cx.argument::(1)?; Ok(User { first_name, last_name }) } method full_name(mut cx) { let this = cx.this(); let guard = cx.lock(); let user = this.borrow(&guard); let full_name = user.full_name(); Ok(cx.string(full_name).upcast()) } } } ``` -------------------------------- ### Replace `JsResultExt` with `ResultExt` Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_1.0.0.md Replace the `JsResultExt` trait with `ResultExt` for converting Rust errors to JavaScript exceptions. ```rust use neon::result::JsResultExt; ``` ```rust use neon::result::ResultExt; ``` -------------------------------- ### Replace `Managed` trait with `Value` trait Source: https://github.com/neon-bindings/neon/blob/main/doc/MIGRATION_GUIDE_1.0.0.md Replace trait bounds referencing `Managed` with `Value` as `Managed` has been removed. ```rust fn example(h: Handle) where V: Managed, { } ``` ```rust fn example(h: Handle) where V: Value, { } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.