@flowscripter/template-bun-rust-library 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/check-bun-dependencies.yml +9 -0
- package/.github/workflows/check-rust-dependencies.yml +9 -0
- package/.github/workflows/lint-pr-message.yml +11 -0
- package/.github/workflows/release-bun-rust-library.yml +8 -0
- package/.github/workflows/validiate-bun-rust-library-pr.yml +8 -0
- package/Cargo.lock +16 -0
- package/Cargo.toml +15 -0
- package/LICENSE +21 -0
- package/README.md +74 -0
- package/bun.lock +27 -0
- package/index.ts +1 -0
- package/package.json +16 -0
- package/src/lib-path.ts +61 -0
- package/src/lib.rs +30 -0
- package/src/lib.ts +26 -0
- package/tests/integration_test.rs +6 -0
- package/tests/world_test.ts +9 -0
- package/tsconfig.json +27 -0
package/Cargo.lock
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "flowscripter_template_bun_rust_library"
|
|
7
|
+
version = "1.0.1"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"flowscripter_template_rust_library",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
[[package]]
|
|
13
|
+
name = "flowscripter_template_rust_library"
|
|
14
|
+
version = "1.1.10"
|
|
15
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
16
|
+
checksum = "976ac733ec768cafc5771dd98b6a08a0551dc0f7358ac0df2d40eb091f3ee977"
|
package/Cargo.toml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "flowscripter_template_bun_rust_library"
|
|
3
|
+
version ="1.0.1"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
description = "Project template for a Rust library with Bun FFI bindings."
|
|
6
|
+
homepage = "https://github.com/flowscripter"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
repository = "https://github.com/flowscripter/template-bun-rust-library"
|
|
9
|
+
exclude = [".github/"]
|
|
10
|
+
|
|
11
|
+
[lib]
|
|
12
|
+
crate-type = ["cdylib","lib"]
|
|
13
|
+
|
|
14
|
+
[dependencies]
|
|
15
|
+
flowscripter_template_rust_library = "1.1.10"
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Flowscripter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# template-bun-rust-library
|
|
2
|
+
|
|
3
|
+
[](https://github.com/flowscripter/template-bun-rust-library/releases)
|
|
4
|
+
[](https://github.com/flowscripter/template-bun-rust-library/actions/workflows/release-bun-rust-library.yml)
|
|
5
|
+
[](https://codecov.io/gh/flowscripter/template-bun-rust-library)
|
|
6
|
+
[](https://github.com/flowscripter/template-bun-rust-library/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
> Project template for a Rust library with Bun FFI bindings.
|
|
9
|
+
|
|
10
|
+
## Template Usage
|
|
11
|
+
|
|
12
|
+
Create a new Bun project using this as a template:
|
|
13
|
+
|
|
14
|
+
`bun create @flowscripter/template-bun-rust-library`
|
|
15
|
+
|
|
16
|
+
## Bun Module Usage
|
|
17
|
+
|
|
18
|
+
Add the module:
|
|
19
|
+
|
|
20
|
+
`bun add @flowscripter/template-bun-rust-library`
|
|
21
|
+
|
|
22
|
+
Use the module:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { world } from "@flowscripter/template-bun-rust-library";
|
|
26
|
+
|
|
27
|
+
world();
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Development
|
|
31
|
+
|
|
32
|
+
Build:
|
|
33
|
+
|
|
34
|
+
Test:
|
|
35
|
+
|
|
36
|
+
`cargo test`
|
|
37
|
+
|
|
38
|
+
`cargo build --release && bun test`
|
|
39
|
+
|
|
40
|
+
**NOTE**: The following tasks use Deno as it excels at these and Bun does not
|
|
41
|
+
currently provide such functionality:
|
|
42
|
+
|
|
43
|
+
Format:
|
|
44
|
+
|
|
45
|
+
`deno fmt`
|
|
46
|
+
|
|
47
|
+
Lint:
|
|
48
|
+
|
|
49
|
+
`cargo fmt && deno lint index.ts src/ tests/`
|
|
50
|
+
|
|
51
|
+
Generate HTML API Documentation:
|
|
52
|
+
|
|
53
|
+
`deno doc --html --name=template-bun-library index.ts`
|
|
54
|
+
|
|
55
|
+
## Documentation
|
|
56
|
+
|
|
57
|
+
### Overview
|
|
58
|
+
|
|
59
|
+
Sample mermaid diagram to test rendering in markdown:
|
|
60
|
+
|
|
61
|
+
```mermaid
|
|
62
|
+
classDiagram
|
|
63
|
+
Foo <|-- Bar
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### API
|
|
67
|
+
|
|
68
|
+
Link to auto-generated API docs:
|
|
69
|
+
|
|
70
|
+
[API Documentation](https://flowscripter.github.io/template-bun-library/index.html)
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT © Flowscripter
|
package/bun.lock
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"lockfileVersion": 1,
|
|
3
|
+
"workspaces": {
|
|
4
|
+
"": {
|
|
5
|
+
"name": "@flowscripter/template-bun-rust-library",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@types/bun": "latest",
|
|
8
|
+
},
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"typescript": "^5.0.0",
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
"packages": {
|
|
15
|
+
"@types/bun": ["@types/bun@1.2.2", "", { "dependencies": { "bun-types": "1.2.2" } }, "sha512-tr74gdku+AEDN5ergNiBnplr7hpDp3V1h7fqI2GcR/rsUaM39jpSeKH0TFibRvU0KwniRx5POgaYnaXbk0hU+w=="],
|
|
16
|
+
|
|
17
|
+
"@types/node": ["@types/node@22.13.4", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-ywP2X0DYtX3y08eFVx5fNIw7/uIv8hYUKgXoK8oayJlLnKcRfEYCxWMVE1XagUdVtCJlZT1AU4LXEABW+L1Peg=="],
|
|
18
|
+
|
|
19
|
+
"@types/ws": ["@types/ws@8.5.14", "", { "dependencies": { "@types/node": "*" } }, "sha512-bd/YFLW+URhBzMXurx7lWByOu+xzU9+kb3RboOteXYDfW+tr+JZa99OyNmPINEGB/ahzKrEuc8rcv4gnpJmxTw=="],
|
|
20
|
+
|
|
21
|
+
"bun-types": ["bun-types@1.2.2", "", { "dependencies": { "@types/node": "*", "@types/ws": "~8.5.10" } }, "sha512-RCbMH5elr9gjgDGDhkTTugA21XtJAy/9jkKe/G3WR2q17VPGhcquf9Sir6uay9iW+7P/BV0CAHA1XlHXMAVKHg=="],
|
|
22
|
+
|
|
23
|
+
"typescript": ["typescript@5.7.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw=="],
|
|
24
|
+
|
|
25
|
+
"undici-types": ["undici-types@6.20.0", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="],
|
|
26
|
+
}
|
|
27
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/lib.ts";
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flowscripter/template-bun-rust-library",
|
|
3
|
+
"module": "index.ts",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "1.0.1",
|
|
6
|
+
"ffiLibBaseUri": "https://github.com/flowscripter/template-bun-rust-library/releases/download/v1.0.1",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@types/bun": "latest"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"typescript": "^5.0.0"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/lib-path.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { suffix } from "bun:ffi";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { mkdir } from "node:fs/promises";
|
|
5
|
+
import packageJson from "../package.json";
|
|
6
|
+
|
|
7
|
+
export async function getLibPath(libName: string) {
|
|
8
|
+
let fullLibName = libName + "." + suffix;
|
|
9
|
+
|
|
10
|
+
if (suffix !== "dll") {
|
|
11
|
+
fullLibName = "lib" + fullLibName;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// look for release build location in preference
|
|
15
|
+
const builtLibPath = path.join("target", "release", fullLibName);
|
|
16
|
+
|
|
17
|
+
console.debug(`builtLibPath: ${builtLibPath}`);
|
|
18
|
+
|
|
19
|
+
const builtLibFile = Bun.file(builtLibPath);
|
|
20
|
+
let exists = await builtLibFile.exists();
|
|
21
|
+
|
|
22
|
+
console.debug(`${builtLibPath} exists: ${exists}`);
|
|
23
|
+
|
|
24
|
+
if (exists) {
|
|
25
|
+
return builtLibPath;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// look for release installed location
|
|
29
|
+
const installedLibFolder = path.join(os.homedir(), ".flowscripter", "lib");
|
|
30
|
+
const installedLibPath = path.join(installedLibFolder, fullLibName);
|
|
31
|
+
const installedLibFile = Bun.file(installedLibPath);
|
|
32
|
+
|
|
33
|
+
exists = await installedLibFile.exists();
|
|
34
|
+
|
|
35
|
+
console.debug(`${installedLibPath} exists: ${exists}`);
|
|
36
|
+
|
|
37
|
+
if (exists) {
|
|
38
|
+
return installedLibPath;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
console.debug(`packageJson.ffiLibBaseUri: ${packageJson.ffiLibBaseUri}`);
|
|
42
|
+
|
|
43
|
+
// look for release download location
|
|
44
|
+
const remotePath = path.join(packageJson.ffiLibBaseUri, fullLibName);
|
|
45
|
+
|
|
46
|
+
console.debug(`remotePath: ${remotePath}`);
|
|
47
|
+
|
|
48
|
+
await mkdir(installedLibFolder, { recursive: true });
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const result = await fetch(remotePath);
|
|
52
|
+
|
|
53
|
+
await Bun.write(installedLibPath, result);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
console.error(
|
|
56
|
+
`Failed to download ${remotePath} to ${installedLibPath}: ${e}`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return installedLibPath;
|
|
61
|
+
}
|
package/src/lib.rs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//! # Template Bun Rust Library
|
|
2
|
+
//!
|
|
3
|
+
//! `flowscripter_template_bun_rust_library` provides a sample function to be called from Bun FFI.
|
|
4
|
+
|
|
5
|
+
use flowscripter_template_rust_library::adder;
|
|
6
|
+
|
|
7
|
+
/// Adds two numbers together.
|
|
8
|
+
///
|
|
9
|
+
/// # Examples
|
|
10
|
+
/// ```
|
|
11
|
+
/// let arg1 = 2;
|
|
12
|
+
/// let arg2 = 2;
|
|
13
|
+
/// let answer = flowscripter_template_bun_rust_library::add(arg1, arg2);
|
|
14
|
+
///
|
|
15
|
+
/// assert_eq!(4, answer);
|
|
16
|
+
/// ```
|
|
17
|
+
#[no_mangle]
|
|
18
|
+
pub extern "C" fn add(a: i32, b: i32) -> i32 {
|
|
19
|
+
adder(a, b)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
#[cfg(test)]
|
|
23
|
+
mod tests {
|
|
24
|
+
use super::*;
|
|
25
|
+
|
|
26
|
+
#[test]
|
|
27
|
+
fn adder_works() {
|
|
28
|
+
assert_eq!(4, add(2, 2));
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/lib.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { dlopen, FFIType } from "bun:ffi";
|
|
2
|
+
import { getLibPath } from "./lib-path.ts";
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
symbols: {
|
|
6
|
+
add,
|
|
7
|
+
},
|
|
8
|
+
} = dlopen(
|
|
9
|
+
await getLibPath("flowscripter_template_bun_rust_library"),
|
|
10
|
+
{
|
|
11
|
+
add: {
|
|
12
|
+
args: [
|
|
13
|
+
FFIType.i32,
|
|
14
|
+
FFIType.i32,
|
|
15
|
+
],
|
|
16
|
+
returns: FFIType.i32,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Adds 2 and 2 and logs the result as "World 4"
|
|
23
|
+
*/
|
|
24
|
+
export function world(): void {
|
|
25
|
+
console.info(`World ${add(2, 2)}`);
|
|
26
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Enable latest features
|
|
4
|
+
"lib": ["ESNext", "DOM"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
|
|
11
|
+
// Bundler mode
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"allowImportingTsExtensions": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
|
|
17
|
+
// Best practices
|
|
18
|
+
"strict": true,
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
|
21
|
+
|
|
22
|
+
// Some stricter flags (disabled by default)
|
|
23
|
+
"noUnusedLocals": false,
|
|
24
|
+
"noUnusedParameters": false,
|
|
25
|
+
"noPropertyAccessFromIndexSignature": false
|
|
26
|
+
}
|
|
27
|
+
}
|