1.0.1 • Published 3 years ago

rust-ipc v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

rust-ipc

rust-ipc is a library that enables inter-process duplex JSON-RPC communications with executables generated by rustc.

rust-ipc is not opinionated about the data layout and allows scalar types to be passed across the process.

rust-ipc extends Node's EventEmitter API to enable bi-directional communication with the executable's process.

Warning: Due to API mismatches, bi-directional communications are not possible. Only Rust binaries can emit data.

Motivation

Maintenance of Rust bindings to Node via N-API or the Node Addon API is challenging due to the internal usage of node-gyp for the latter.

FFI bindings via node-ffi, which uses the libffi C++ library, also has its own set of maintenance issues, although considerably more maintainable than node-gyp, albeit a substantial performance overhead for FFI calls.

With both the solutions above, I/O is not free. Creating and keeping alive HTTP servers or file watchers is complicated or impossible (std::ffi::c_void cannot be passed across threads). Given that many server implementations use threads, it becomes difficult to write I/O Node applications with native bindings, effectively rendering both solutions (node-gyp and node-ffi) just as featureless as WebAssembly in Node.

My presumption is that rust-ipc does not rank against both the above solutions in terms of performance, which is not part of the motivations. Maintenance is the primary goal. A separate process is created to execute the binary and run bi-directional operations.

Optionally, a user can ship a platform specific binary which is then fed to rust-ipc's API. Such logic is left to the user and rust-ipc handles communications only.

Installation

$ npm install rust-ipc

Usage

Before proceeding to examples, it is important to understand that the Rust binary is virtually sandboxed and hence expected to use println!() calls to the stdout in order for rust-ipc to catch any incoming data. Ensure that no included crates print any trace messages to the console.

Additionally, the spawn function from child_process is used, so the process is non-blocking and can live as long as the Rust executable alive. For instance, if a watcher is used, the

In this example, we'll send some data from Node, modify it in Rust, then send it back.

import ipc from "rust-ipc";

interface Person {
  name: string;
  age: number;
  hobby: string;
}

ipc("./path/to/executable.exe")
  .on("data", (data: string) => {
    const person: Person = JSON.parse(data);
    console.table(...p);
  })
  .end();

License

MIT License © Saddam M.