Skip to content

Commit

Permalink
Merge pull request #24 from Meta-A/feat/js-bindings
Browse files Browse the repository at this point in the history
feat(js-bindings): integrate js bindings
  • Loading branch information
sxiii authored Dec 22, 2024
2 parents bf21161 + b33933a commit e7689a0
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# will have compiled files and executables
debug/
target/
cargo.log

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[workspace]
members = [
"packages/cli"
, "packages/core"]
"packages/cli",
"packages/core",
"packages/js-bindings"

]

[workspace.dependencies]
bpm_core = { version = "0.1.0", path = "packages/core" }
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use bpm_core::{
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
init_logger(log::LevelFilter::Info);

const VERSION: &str = env!("CARGO_PKG_VERSION");

info!("BPM v{}", VERSION);
Expand Down
6 changes: 6 additions & 0 deletions packages/js-bindings/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target
index.node
**/node_modules
**/.DS_Store
npm-debug.log*cargo.log
cross.log
19 changes: 19 additions & 0 deletions packages/js-bindings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "js-bindings"
version = "0.1.0"
license = "ISC"
edition = "2021"
exclude = ["index.node"]

[lib]
crate-type = ["cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bpm_core = { workspace = true }
hex = "0.4.3"
home = "0.5.9"
neon = "1"
once_cell = "1"
tokio = { version = "1", features = ["rt-multi-thread"] }
90 changes: 90 additions & 0 deletions packages/js-bindings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# js-bindings

This project was bootstrapped by [create-neon](https://www.npmjs.com/package/create-neon).

## Building js-bindings

Building js-bindings requires a [supported version of Node and Rust](https://github.com/neon-bindings/neon#platform-support).

To run the build, run:

```sh
$ npm run build
```

This command uses the [@neon-rs/cli](https://www.npmjs.com/package/@neon-rs/cli) utility to assemble the binary Node addon from the output of `cargo`.

## Exploring js-bindings

After building js-bindings, you can explore its exports at the Node console:

```sh
$ npm i
$ npm run build
$ node
> require('.').hello()
'hello node'
```

## Available Scripts

In the project directory, you can run:

#### `npm install`

Installs the project, including running `npm run build`.

#### `npm run build`

Builds the Node addon (`index.node`) from source, generating a release build with `cargo --release`.

Additional [`cargo build`](https://doc.rust-lang.org/cargo/commands/cargo-build.html) arguments may be passed to `npm run build` and similar commands. For example, to enable a [cargo feature](https://doc.rust-lang.org/cargo/reference/features.html):

```
npm run build -- --feature=beetle
```

#### `npm run debug`

Similar to `npm run build` but generates a debug build with `cargo`.

#### `npm run cross`

Similar to `npm run build` but uses [cross-rs](https://github.com/cross-rs/cross) to cross-compile for another platform. Use the [`CARGO_BUILD_TARGET`](https://doc.rust-lang.org/cargo/reference/config.html#buildtarget) environment variable to select the build target.

#### `npm test`

Runs the unit tests by calling `cargo test`. You can learn more about [adding tests to your Rust code](https://doc.rust-lang.org/book/ch11-01-writing-tests.html) from the [Rust book](https://doc.rust-lang.org/book/).

## Project Layout

The directory structure of this project is:

```
js-bindings/
├── Cargo.toml
├── README.md
├── src/
| └── lib.rs
├── index.node
├── package.json
└── target/
```

| Entry | Purpose |
|----------------|------------------------------------------------------------------------------------------------------------------------------------------|
| `Cargo.toml` | The Cargo [manifest file](https://doc.rust-lang.org/cargo/reference/manifest.html), which informs the `cargo` command. |
| `README.md` | This file. |
| `src/` | The directory tree containing the Rust source code for the project. |
| `lib.rs` | Entry point for the Rust source code. |
| `index.node` | The main module, a [Node addon](https://nodejs.org/api/addons.html) generated by the build and pointed to by `"main"` in `package.json`. |
| `package.json` | The npm [manifest file](https://docs.npmjs.com/cli/v7/configuring-npm/package-json), which informs the `npm` command. |
| `target/` | Binary artifacts generated by the Rust build. |

## Learn More

Learn more about:

- [Neon](https://neon-bindings.com).
- [Rust](https://www.rust-lang.org).
- [Node](https://nodejs.org).
178 changes: 178 additions & 0 deletions packages/js-bindings/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions packages/js-bindings/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "js-bindings",
"version": "0.1.0",
"description": "",
"main": "index.node",
"scripts": {
"test": "cargo test",
"cargo-build": "cargo build --message-format=json-render-diagnostics > cargo.log",
"cross-build": "cross build --message-format=json-render-diagnostics > cross.log",
"postcargo-build": "neon dist < cargo.log",
"postcross-build": "neon dist -m /target < cross.log",
"debug": "npm run cargo-build --",
"build": "npm run cargo-build -- --release",
"cross": "npm run cross-build -- --release"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@neon-rs/cli": "0.1.82"
}
}
Loading

0 comments on commit e7689a0

Please sign in to comment.