# rustlang-bridge

> Simple node.js script to achieve a bridge between rust and node.js. Use with the cargo crate node_bridge.

Latest version **1.0.2** (published 2023-04-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install rustlang-bridge
pnpm add rustlang-bridge
yarn add rustlang-bridge
bun add rustlang-bridge
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-04-22 |
| First published | 2023-04-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 6 |
| Author | PureSci |
| Maintainers | purechen |
| Keywords | rust, rustlang, bridge |

## Links

- npm: https://www.npmjs.com/package/rustlang-bridge
- Repository: https://github.com/PureSci/node-rust-bridge
- Homepage: https://github.com/PureSci/node-rust-bridge#readme
- Issues: https://github.com/PureSci/node-rust-bridge/issues
- npm.io page: https://npm.io/package/rustlang-bridge

## Dependencies (1)

- [uuid](https://npm.io/package/uuid.md) ^9.0.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-04-22
- 1.0.0 — 2023-04-22
- 0.1.2 — 2023-04-08

## README

# Rustlang Bridge

Simple node.js script to achieve a bridge between rust and node.js. Use with the cargo crate node_bridge.
Only 1 bridge can be initialized per rust program. But node.js can have many bridges.

## Installation

```bash
$ npm install rustlang-bridge
```
    
Rust installation:
Add the following line to your Cargo.toml under [dependencies]
```toml
node_bridge = "1.0.2"
```

## Usage/Examples

```rust
use node_bridge::NodeBridge;

#[tokio::main]
async fn main() {
    let bridge = NodeBridge::new();
    bridge.register("addition", add, None);
    bridge.register_async("find_longer", find_longer, Some("Variable to pass to the function"));
    assert_eq!(bridge.receive("channel_a").await.unwrap(), "Sent this from node!");
    bridge.send("channel_foo", "bar").ok();
    bridge.wait_until_closes().await;
}
 
fn add(params: Vec<i32>, _: Option<()>) -> i32 {
    params[0] + params[1]
}
 
async fn find_longer(params: Vec<String>, pass_data: Option<&str>) -> String {
    assert_eq!(pass_data, Some("Variable to pass to the function"));
    if params[0].len() > params[1].len() {
        return params[0].to_string();
    }
    params[1].to_string()
}
```

Handling in node.js:

```javascript
import RustBridge from "rustlang-bridge";
 
const bridge = new RustBridge("/path/to/rust_executable");
await new Promise(resolve => setTimeout(resolve, 500)); // wait for the functions to initialize
console.log(await bridge.addition(10, 20)); // "30"
console.log(await bridge.find_longer("foo", "longer_foo")); // "longer_foo"
bridge.on("channel_foo", data => {
    console.log(data); // "bar"
});
bridge.send("channel_a", "Sent this from node!");
```
## Contributing

Contributions are always welcome!

---
_Source: https://npm.io/package/rustlang-bridge · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
