Skip to content

Commit

Permalink
feat: bindings for node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed Aug 26, 2024
1 parent 12ae781 commit f6c57b2
Show file tree
Hide file tree
Showing 11 changed files with 752 additions and 69 deletions.
243 changes: 175 additions & 68 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[workspace]
resolver = "2"
members = ["crates/delta", "crates/bonfire", "crates/core/*"]
members = [
"crates/delta",
"crates/bonfire",
"crates/core/*",
"crates/bindings/node",
]

[patch.crates-io]
# mobc-redis = { git = "https://github.com/insertish/mobc", rev = "8b880bb59f2ba80b4c7bc40c649c113d8857a186" }
Expand Down
7 changes: 7 additions & 0 deletions crates/bindings/node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
target
index.node
**/node_modules
**/.DS_Store
npm-debug.log*
cargo.log
cross.log
24 changes: 24 additions & 0 deletions crates/bindings/node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "revolt-nodejs-bindings"
version = "0.7.15"
description = "Node.js bindings for the Revolt software"
authors = ["Paul Makles <[email protected]>"]
license = "MIT"
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]
neon = "1.0.0"
neon-serde4 = "1.0.0"

serde = { version = "1", features = ["derive"] }

async-std = "1.12.0"

revolt-result = { version = "0.7.15", path = "../../core/result" }
revolt-database = { version = "0.7.15", path = "../../core/database" }
92 changes: 92 additions & 0 deletions crates/bindings/node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# revolt.node

**revolt.node:** Node.js bindings for the Revolt software

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

## Building revolt.node

Building revolt.node 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 revolt.node

After building revolt.node, 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:

```
revolt.node/
├── 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).
66 changes: 66 additions & 0 deletions crates/bindings/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Channel, User } from "revolt-api";

/**
* Opaque type for Revolt database
*/
export declare interface Database {}

/**
* Opaque type for Revolt database
*/
export declare interface OpaqueUser {}

/**
* Error type from Revolt backend
*/
export declare interface Err {
type: string;
location: string;
}

/**
* Gets a new handle to the Revolt database
* @returns {Database} Handle
*/
export declare function database(): Database;

/**
* Fetch user from database
* @param {string} userId User's ID
* @this {Database}
*/
export declare function database_fetch_user(userId: string): OpaqueUser;

/**
* Fetch user from database
* @param {string} username Username
* @param {string} discriminator Discriminator
* @this {Database}
*/
export declare function database_fetch_user_by_username(
username: string,
discriminator: string
): OpaqueUser;

/**
* Gets model data as JSON
* @this {OpaqueUser}
*/
export declare function model_data(): User;

/**
* Gets error if the model failed to fetch
* @this {OpaqueUser}
*/
export declare function model_error(): Err;

/**
* Open a direct message channel between two users
* @param {string} userA User A ID
* @param {string} userB User B ID
* @returns Existing or newly created channel
*/
export declare function proc_channels_create_dm(
userA: string,
userB: string
): Promise<Channel & { error: Err }>;
36 changes: 36 additions & 0 deletions crates/bindings/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "revolt-nodejs-bindings",
"version": "0.7.15",
"description": "Node.js bindings for the Revolt software",
"main": "index.node",
"scripts": {
"test": "cargo test",
"cargo-build": "cargo build --message-format=json > cargo.log",
"cross-build": "cross build --message-format=json > 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": "Paul Makles",
"license": "AGPL-3.0",
"devDependencies": {
"@neon-rs/cli": "0.1.73"
},
"repository": {
"type": "git",
"url": "git+https://github.com/revoltchat/backend"
},
"keywords": [
"revolt",
"chat"
],
"bugs": {
"url": "https://github.com/revoltchat/backend/issues"
},
"homepage": "https://github.com/revoltchat/backend#readme",
"dependencies": {
"revolt-api": "^0.7.15"
}
}
117 changes: 117 additions & 0 deletions crates/bindings/node/pnpm-lock.yaml

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

Loading

0 comments on commit f6c57b2

Please sign in to comment.