Expand description

Node Workers

This crate lets you call node binaries from Rust code using a pool of workers. This is useful for project that are mostly coded in Rust but need to leverage Node packages for some tasks, like using Typescript’s API or performing SSR with a JS framework.
Using a pool of workers is fundamental to avoid the cost of booting node binaries on multiple calls. Medium to big Node binaries can take about a second to bot (or more) depending on its imports, and using a pool of long-lived processes save you time on subsequent runs. If throughout the usage of your program you need to interact with a node binary multiple times, a reusable and long-lived process will save you a LOT of time.

Usage example

use node_workers::WorkerPool;
use serde::Deserialize;
use std::path::Path;

#[derive(Deserialize, Debug)]
struct Property {
  pub key: String,
  #[serde(alias = "type")]
  pub propType: String,
}
#[derive(Deserialize, Debug)]
struct Interface {
  pub name: String,
  pub props: Vec<Property>,
}

// Create a pool of 4 node workers
let mut pool = WorkerPool::setup("examples/worker", 4);
pool.with_debug(true);

// Payloads
let files = vec![
  Path::new("examples/user-files/user.ts").canonicalize()?,
  Path::new("examples/user-files/pet.ts").canonicalize()?,
];

// execute the command "getInterfaces" on every file
// each executed worker will return an array of interfaces (Vec<Interface>)
let interfaces = pool.perform::<Vec<Interface>, _>("getInterfaces", files)?;
let interfaces: Vec<Interface> = interfaces
  .into_iter()
  .map(|x| x.unwrap())
  .flatten()
  .collect();
println!("interfaces: {:#?}", interfaces);

Macros

A macro to quickly create an array of payload. This is usefull for running a task with payloads of different types.

Structs

Represents an empty payload that can be sent to a node worker

A pool of nodejs workers. Wraps a inner struct inside Arc<Mutex<T>> to be able to invoke it’s method within a spawned thread. This is important so that indefinitely blocking methods such as get_available_workers can be offloaded.

Traits

Represent a data that can be sent to a node worker. Under the hood, node worker can only receive and transfer back serde_json::Value. This trait is mainly for convenience as it is already implemented for all primitive types, and lets you send all kinds of data to a node worker without boilerplate.