@push.rocks/smartrust 1.1.0 → 1.1.2
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/changelog.md +14 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/classes.rustbinarylocator.js +3 -2
- package/dist_ts/plugins.d.ts +2 -1
- package/dist_ts/plugins.js +3 -2
- package/package.json +1 -1
- package/readme.md +298 -3
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/classes.rustbinarylocator.ts +2 -1
- package/ts/plugins.ts +2 -1
package/changelog.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 2026-02-10 - 1.1.2 - fix(rust-binary-locator)
|
|
4
|
+
use import.meta.resolve and url.fileURLToPath to locate bundled Rust binary in ESM environments
|
|
5
|
+
|
|
6
|
+
- Replace require.resolve with import.meta.resolve to support ESM module resolution
|
|
7
|
+
- Convert resolved file URL to a filesystem path using url.fileURLToPath
|
|
8
|
+
- Export the url module from ts/plugins to provide fileURLToPath
|
|
9
|
+
|
|
10
|
+
## 2026-02-10 - 1.1.1 - fix(readme)
|
|
11
|
+
update README with comprehensive documentation, usage examples, API reference, installation instructions, and legal/company information
|
|
12
|
+
|
|
13
|
+
- Rewrote readme.md (≈ +298 −3 lines) to add detailed install, overview, IPC protocol, command definition examples, usage, API reference, issue reporting & security guidance, and legal/trademark/company information.
|
|
14
|
+
- Documentation-only change — no source code modified.
|
|
15
|
+
- Current package version is 1.1.0; recommend a patch release
|
|
16
|
+
|
|
3
17
|
## 2026-02-10 - 1.1.0 - feat(rustbridge)
|
|
4
18
|
add RustBridge and RustBinaryLocator with typed IPC interfaces, plugins, tests and mock runner; export from index; add npm registries
|
|
5
19
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const commitinfo = {
|
|
5
5
|
name: '@push.rocks/smartrust',
|
|
6
|
-
version: '1.1.
|
|
6
|
+
version: '1.1.2',
|
|
7
7
|
description: 'a bridge between JS engines and rust'
|
|
8
8
|
};
|
|
9
9
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSx1QkFBdUI7SUFDN0IsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLHNDQUFzQztDQUNwRCxDQUFBIn0=
|
|
@@ -93,7 +93,8 @@ export class RustBinaryLocator {
|
|
|
93
93
|
const arch = process.arch;
|
|
94
94
|
const packageName = `${platformPackagePrefix}-${platform}-${arch}`;
|
|
95
95
|
try {
|
|
96
|
-
const
|
|
96
|
+
const resolved = import.meta.resolve(`${packageName}/${binaryName}`);
|
|
97
|
+
const packagePath = plugins.url.fileURLToPath(resolved);
|
|
97
98
|
if (await this.isExecutable(packagePath)) {
|
|
98
99
|
return packagePath;
|
|
99
100
|
}
|
|
@@ -123,4 +124,4 @@ export class RustBinaryLocator {
|
|
|
123
124
|
return null;
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
127
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xhc3Nlcy5ydXN0YmluYXJ5bG9jYXRvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL2NsYXNzZXMucnVzdGJpbmFyeWxvY2F0b3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLE9BQU8sTUFBTSxjQUFjLENBQUM7Q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UM7SUFDZCxDQUFDO0lBRU8sS0FBSyxDQUFDLFlBQVksQ0FBQyxRQUFnQjtRQUN6QyxJQUFJLENBQUM7WUFDSCxNQUFNLE9BQU8sQ0FBQyxFQUFFLENBQUMsUUFBUSxDQUFDLE1BQU0sQ0FBQyxRQUFRLEVBQUUsT0FBTyxDQUFDLEVBQUUsQ0FBQyxTQUFTLENBQUMsSUFBSSxDQUFDLENBQUM7WUFDdEUsT0FBTyxJQUFJLENBQUM7UUFDZCxDQUFDO1FBQUMsTUFBTSxDQUFDO1lBQ1AsT0FBTyxLQUFLLENBQUM7UUFDZixDQUFDO0lBQ0gsQ0FBQztJQUVPLEtBQUssQ0FBQyxVQUFVLENBQUMsVUFBa0I7UUFDekMsTUFBTSxRQUFRLEdBQUcsQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksSUFBSSxFQUFFLENBQUMsQ0FBQyxLQUFLLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxTQUFTLENBQUMsQ0FBQztRQUN4RSxLQUFLLE1BQU0sR0FBRyxJQUFJLFFBQVEsRUFBRSxDQUFDO1lBQzNCLE1BQU0sUUFBUSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLEdBQUcsRUFBRSxVQUFVLENBQUMsQ0FBQztZQUNwRCxJQUFJLE1BQU0sSUFBSSxDQUFDLFlBQVksQ0FBQyxRQUFRLENBQUMsRUFBRSxDQUFDO2dCQUN0QyxPQUFPLFFBQVEsQ0FBQztZQUNsQixDQUFDO1FBQ0gsQ0FBQztRQUNELE9BQU8sSUFBSSxDQUFDO0lBQ2QsQ0FBQztDQUNGIn0=
|
package/dist_ts/plugins.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as fs from 'fs';
|
|
|
3
3
|
import * as childProcess from 'child_process';
|
|
4
4
|
import * as readline from 'readline';
|
|
5
5
|
import * as events from 'events';
|
|
6
|
-
|
|
6
|
+
import * as url from 'url';
|
|
7
|
+
export { path, fs, childProcess, readline, events, url };
|
|
7
8
|
import * as smartpath from '@push.rocks/smartpath';
|
|
8
9
|
export { smartpath };
|
package/dist_ts/plugins.js
CHANGED
|
@@ -4,8 +4,9 @@ import * as fs from 'fs';
|
|
|
4
4
|
import * as childProcess from 'child_process';
|
|
5
5
|
import * as readline from 'readline';
|
|
6
6
|
import * as events from 'events';
|
|
7
|
-
|
|
7
|
+
import * as url from 'url';
|
|
8
|
+
export { path, fs, childProcess, readline, events, url };
|
|
8
9
|
// @push.rocks scope
|
|
9
10
|
import * as smartpath from '@push.rocks/smartpath';
|
|
10
11
|
export { smartpath };
|
|
11
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
12
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3BsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsZUFBZTtBQUNmLE9BQU8sS0FBSyxJQUFJLE1BQU0sTUFBTSxDQUFDO0FBQzdCLE9BQU8sS0FBSyxFQUFFLE1BQU0sSUFBSSxDQUFDO0FBQ3pCLE9BQU8sS0FBSyxZQUFZLE1BQU0sZUFBZSxDQUFDO0FBQzlDLE9BQU8sS0FBSyxRQUFRLE1BQU0sVUFBVSxDQUFDO0FBQ3JDLE9BQU8sS0FBSyxNQUFNLE1BQU0sUUFBUSxDQUFDO0FBQ2pDLE9BQU8sS0FBSyxHQUFHLE1BQU0sS0FBSyxDQUFDO0FBRTNCLE9BQU8sRUFBRSxJQUFJLEVBQUUsRUFBRSxFQUFFLFlBQVksRUFBRSxRQUFRLEVBQUUsTUFBTSxFQUFFLEdBQUcsRUFBRSxDQUFDO0FBRXpELG9CQUFvQjtBQUNwQixPQUFPLEtBQUssU0FBUyxNQUFNLHVCQUF1QixDQUFDO0FBRW5ELE9BQU8sRUFBRSxTQUFTLEVBQUUsQ0FBQyJ9
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,5 +1,300 @@
|
|
|
1
1
|
# @push.rocks/smartrust
|
|
2
|
-
a bridge between JS engines and rust
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
A type-safe, standardized bridge between TypeScript and Rust binaries via JSON-over-stdin/stdout IPC.
|
|
4
|
+
|
|
5
|
+
## Issue Reporting and Security
|
|
6
|
+
|
|
7
|
+
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
|
8
|
+
|
|
9
|
+
## Install 📦
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @push.rocks/smartrust
|
|
13
|
+
# or
|
|
14
|
+
pnpm install @push.rocks/smartrust
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Overview 🔭
|
|
18
|
+
|
|
19
|
+
`@push.rocks/smartrust` provides a production-ready bridge for TypeScript applications that need to communicate with Rust binaries. It handles the entire lifecycle — binary discovery, process spawning, request/response correlation with timeouts, event streaming, and graceful shutdown — so you can focus on your command definitions instead of IPC plumbing.
|
|
20
|
+
|
|
21
|
+
### Why?
|
|
22
|
+
|
|
23
|
+
If you're integrating Rust into a Node.js project, you'll inevitably need:
|
|
24
|
+
- A way to **find** the compiled Rust binary across different environments (dev, CI, production, platform packages)
|
|
25
|
+
- A way to **spawn** it and establish reliable two-way communication
|
|
26
|
+
- **Type-safe** request/response patterns with proper error handling
|
|
27
|
+
- **Event streaming** from Rust to TypeScript
|
|
28
|
+
- **Graceful lifecycle management** (ready detection, clean shutdown, force kill)
|
|
29
|
+
|
|
30
|
+
`smartrust` wraps all of this into two classes: `RustBridge` and `RustBinaryLocator`.
|
|
31
|
+
|
|
32
|
+
## Usage 🚀
|
|
33
|
+
|
|
34
|
+
### The IPC Protocol
|
|
35
|
+
|
|
36
|
+
`smartrust` uses a simple, newline-delimited JSON protocol over stdin/stdout:
|
|
37
|
+
|
|
38
|
+
| Direction | Format | Description |
|
|
39
|
+
|-----------|--------|-------------|
|
|
40
|
+
| **TS → Rust** (Request) | `{"id": "req_1", "method": "start", "params": {...}}` | Command with unique ID |
|
|
41
|
+
| **Rust → TS** (Response) | `{"id": "req_1", "success": true, "result": {...}}` | Response correlated by ID |
|
|
42
|
+
| **Rust → TS** (Error) | `{"id": "req_1", "success": false, "error": "msg"}` | Error correlated by ID |
|
|
43
|
+
| **Rust → TS** (Event) | `{"event": "ready", "data": {...}}` | Unsolicited event (no ID) |
|
|
44
|
+
|
|
45
|
+
Your Rust binary reads JSON lines from stdin and writes JSON lines to stdout. That's it. Stderr is free for logging.
|
|
46
|
+
|
|
47
|
+
### Defining Your Commands
|
|
48
|
+
|
|
49
|
+
Start by defining a type map of commands your Rust binary supports:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { RustBridge, type ICommandDefinition } from '@push.rocks/smartrust';
|
|
53
|
+
|
|
54
|
+
// Define your command types
|
|
55
|
+
type TMyCommands = {
|
|
56
|
+
start: { params: { port: number; host: string }; result: { pid: number } };
|
|
57
|
+
stop: { params: {}; result: void };
|
|
58
|
+
getMetrics: { params: {}; result: { connections: number; uptime: number } };
|
|
59
|
+
reload: { params: { configPath: string }; result: void };
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Creating and Using the Bridge
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
const bridge = new RustBridge<TMyCommands>({
|
|
67
|
+
binaryName: 'my-rust-server',
|
|
68
|
+
envVarName: 'MY_SERVER_BINARY', // optional: env var override
|
|
69
|
+
platformPackagePrefix: '@myorg/my-server', // optional: platform npm packages
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Spawn the binary and wait for it to signal readiness
|
|
73
|
+
const ok = await bridge.spawn();
|
|
74
|
+
if (!ok) {
|
|
75
|
+
console.error('Failed to start Rust binary');
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Send type-safe commands — params and return types are inferred!
|
|
80
|
+
const { pid } = await bridge.sendCommand('start', { port: 8080, host: '0.0.0.0' });
|
|
81
|
+
console.log(`Server started with PID ${pid}`);
|
|
82
|
+
|
|
83
|
+
const metrics = await bridge.sendCommand('getMetrics', {});
|
|
84
|
+
console.log(`Active connections: ${metrics.connections}`);
|
|
85
|
+
|
|
86
|
+
// Listen for events from Rust
|
|
87
|
+
bridge.on('management:configChanged', (data) => {
|
|
88
|
+
console.log('Config was changed:', data);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Clean shutdown
|
|
92
|
+
bridge.kill();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Binary Locator
|
|
96
|
+
|
|
97
|
+
The `RustBinaryLocator` searches for your binary using a priority-ordered strategy:
|
|
98
|
+
|
|
99
|
+
| Priority | Source | Description |
|
|
100
|
+
|----------|--------|-------------|
|
|
101
|
+
| 1 | `binaryPath` option | Explicit path — skips all other search |
|
|
102
|
+
| 2 | Environment variable | e.g. `MY_SERVER_BINARY=/usr/local/bin/server` |
|
|
103
|
+
| 3 | Platform npm package | e.g. `@myorg/my-server-linux-x64/my-rust-server` |
|
|
104
|
+
| 4 | Local dev paths | `./rust/target/release/<name>` and `./rust/target/debug/<name>` |
|
|
105
|
+
| 5 | System PATH | Standard `$PATH` lookup |
|
|
106
|
+
|
|
107
|
+
You can also use the locator standalone:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { RustBinaryLocator } from '@push.rocks/smartrust';
|
|
111
|
+
|
|
112
|
+
const locator = new RustBinaryLocator({
|
|
113
|
+
binaryName: 'my-rust-server',
|
|
114
|
+
envVarName: 'MY_SERVER_BINARY',
|
|
115
|
+
localPaths: ['/opt/myapp/bin/server'], // custom search paths
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const binaryPath = await locator.findBinary();
|
|
119
|
+
// Result is cached — call clearCache() to force re-search
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Configuration Reference
|
|
123
|
+
|
|
124
|
+
The `RustBridge` constructor accepts an `IRustBridgeOptions` object:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
const bridge = new RustBridge<TMyCommands>({
|
|
128
|
+
// --- Binary Locator Options ---
|
|
129
|
+
binaryName: 'my-server', // required: name of the binary
|
|
130
|
+
binaryPath: '/explicit/path/to/binary', // optional: skip search entirely
|
|
131
|
+
envVarName: 'MY_SERVER_BINARY', // optional: env var for path override
|
|
132
|
+
platformPackagePrefix: '@myorg/my-server', // optional: platform npm package prefix
|
|
133
|
+
localPaths: ['./build/server'], // optional: custom local search paths
|
|
134
|
+
searchSystemPath: true, // optional: search $PATH (default: true)
|
|
135
|
+
|
|
136
|
+
// --- Bridge Options ---
|
|
137
|
+
cliArgs: ['--management'], // optional: args passed to binary (default: ['--management'])
|
|
138
|
+
requestTimeoutMs: 30000, // optional: per-request timeout (default: 30000)
|
|
139
|
+
readyTimeoutMs: 10000, // optional: ready event timeout (default: 10000)
|
|
140
|
+
env: { RUST_LOG: 'debug' }, // optional: extra env vars for the child process
|
|
141
|
+
readyEventName: 'ready', // optional: name of the ready event (default: 'ready')
|
|
142
|
+
logger: myLogger, // optional: logger implementing IRustBridgeLogger
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Events
|
|
147
|
+
|
|
148
|
+
`RustBridge` extends `EventEmitter` and emits the following events:
|
|
149
|
+
|
|
150
|
+
| Event | Payload | Description |
|
|
151
|
+
|-------|---------|-------------|
|
|
152
|
+
| `ready` | — | Bridge connected and binary reported ready |
|
|
153
|
+
| `exit` | `(code, signal)` | Rust process exited |
|
|
154
|
+
| `stderr` | `string` | A line from the binary's stderr |
|
|
155
|
+
| `management:<name>` | `any` | Custom event from Rust (e.g. `management:configChanged`) |
|
|
156
|
+
|
|
157
|
+
### Custom Logger
|
|
158
|
+
|
|
159
|
+
Plug in your own logger by implementing the `IRustBridgeLogger` interface:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import type { IRustBridgeLogger } from '@push.rocks/smartrust';
|
|
163
|
+
|
|
164
|
+
const logger: IRustBridgeLogger = {
|
|
165
|
+
log(level: string, message: string, data?: Record<string, any>) {
|
|
166
|
+
console.log(`[${level}] ${message}`, data || '');
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const bridge = new RustBridge<TMyCommands>({
|
|
171
|
+
binaryName: 'my-server',
|
|
172
|
+
logger,
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Writing the Rust Side
|
|
177
|
+
|
|
178
|
+
Your Rust binary needs to implement a simple protocol:
|
|
179
|
+
|
|
180
|
+
1. **On startup**, write a ready event to stdout:
|
|
181
|
+
```
|
|
182
|
+
{"event":"ready","data":{"version":"1.0.0"}}\n
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
2. **Read JSON lines from stdin**, parse each as `{"id": "...", "method": "...", "params": {...}}`
|
|
186
|
+
|
|
187
|
+
3. **Write JSON responses to stdout**, each as `{"id": "...", "success": true, "result": {...}}\n`
|
|
188
|
+
|
|
189
|
+
4. **Emit events** anytime by writing `{"event": "name", "data": {...}}\n` to stdout
|
|
190
|
+
|
|
191
|
+
5. **Use stderr** for logging — it won't interfere with the IPC protocol
|
|
192
|
+
|
|
193
|
+
Here's a minimal Rust skeleton:
|
|
194
|
+
|
|
195
|
+
```rust
|
|
196
|
+
use serde::{Deserialize, Serialize};
|
|
197
|
+
use std::io::{self, BufRead, Write};
|
|
198
|
+
|
|
199
|
+
#[derive(Deserialize)]
|
|
200
|
+
struct Request {
|
|
201
|
+
id: String,
|
|
202
|
+
method: String,
|
|
203
|
+
params: serde_json::Value,
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
#[derive(Serialize)]
|
|
207
|
+
struct Response {
|
|
208
|
+
id: String,
|
|
209
|
+
success: bool,
|
|
210
|
+
#[serde(skip_serializing_if = "Option::is_none")]
|
|
211
|
+
result: Option<serde_json::Value>,
|
|
212
|
+
#[serde(skip_serializing_if = "Option::is_none")]
|
|
213
|
+
error: Option<String>,
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
fn main() {
|
|
217
|
+
// Signal ready
|
|
218
|
+
println!(r#"{{"event":"ready","data":{{"version":"1.0.0"}}}}"#);
|
|
219
|
+
io::stdout().flush().unwrap();
|
|
220
|
+
|
|
221
|
+
let stdin = io::stdin();
|
|
222
|
+
for line in stdin.lock().lines() {
|
|
223
|
+
let line = line.unwrap();
|
|
224
|
+
let req: Request = serde_json::from_str(&line).unwrap();
|
|
225
|
+
|
|
226
|
+
let response = match req.method.as_str() {
|
|
227
|
+
"ping" => Response {
|
|
228
|
+
id: req.id,
|
|
229
|
+
success: true,
|
|
230
|
+
result: Some(serde_json::json!({"pong": true})),
|
|
231
|
+
error: None,
|
|
232
|
+
},
|
|
233
|
+
_ => Response {
|
|
234
|
+
id: req.id,
|
|
235
|
+
success: false,
|
|
236
|
+
result: None,
|
|
237
|
+
error: Some(format!("Unknown method: {}", req.method)),
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
let json = serde_json::to_string(&response).unwrap();
|
|
242
|
+
println!("{json}");
|
|
243
|
+
io::stdout().flush().unwrap();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## API Reference 📖
|
|
249
|
+
|
|
250
|
+
### `RustBridge<TCommands>`
|
|
251
|
+
|
|
252
|
+
| Method / Property | Signature | Description |
|
|
253
|
+
|---|---|---|
|
|
254
|
+
| `constructor` | `new RustBridge<T>(options: IRustBridgeOptions)` | Create a new bridge instance |
|
|
255
|
+
| `spawn()` | `Promise<boolean>` | Spawn the binary and wait for ready; returns `false` on failure |
|
|
256
|
+
| `sendCommand(method, params)` | `Promise<TCommands[K]['result']>` | Send a typed command and await the response |
|
|
257
|
+
| `kill()` | `void` | SIGTERM the process, reject pending requests, force SIGKILL after 5s |
|
|
258
|
+
| `running` | `boolean` | Whether the bridge is currently connected |
|
|
259
|
+
|
|
260
|
+
### `RustBinaryLocator`
|
|
261
|
+
|
|
262
|
+
| Method / Property | Signature | Description |
|
|
263
|
+
|---|---|---|
|
|
264
|
+
| `constructor` | `new RustBinaryLocator(options: IBinaryLocatorOptions, logger?)` | Create a locator instance |
|
|
265
|
+
| `findBinary()` | `Promise<string \| null>` | Find the binary using the priority search; result is cached |
|
|
266
|
+
| `clearCache()` | `void` | Clear the cached path to force a fresh search |
|
|
267
|
+
|
|
268
|
+
### Exported Interfaces
|
|
269
|
+
|
|
270
|
+
| Interface | Description |
|
|
271
|
+
|---|---|
|
|
272
|
+
| `IRustBridgeOptions` | Full configuration for `RustBridge` |
|
|
273
|
+
| `IBinaryLocatorOptions` | Configuration for `RustBinaryLocator` |
|
|
274
|
+
| `IRustBridgeLogger` | Logger interface: `{ log(level, message, data?) }` |
|
|
275
|
+
| `IManagementRequest` | IPC request shape: `{ id, method, params }` |
|
|
276
|
+
| `IManagementResponse` | IPC response shape: `{ id, success, result?, error? }` |
|
|
277
|
+
| `IManagementEvent` | IPC event shape: `{ event, data }` |
|
|
278
|
+
| `ICommandDefinition` | Single command definition: `{ params, result }` |
|
|
279
|
+
| `TCommandMap` | `Record<string, ICommandDefinition>` |
|
|
280
|
+
|
|
281
|
+
## License and Legal Information
|
|
282
|
+
|
|
283
|
+
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
|
284
|
+
|
|
285
|
+
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
|
286
|
+
|
|
287
|
+
### Trademarks
|
|
288
|
+
|
|
289
|
+
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
|
290
|
+
|
|
291
|
+
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
|
292
|
+
|
|
293
|
+
### Company Information
|
|
294
|
+
|
|
295
|
+
Task Venture Capital GmbH
|
|
296
|
+
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
297
|
+
|
|
298
|
+
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
|
299
|
+
|
|
300
|
+
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
package/ts/00_commitinfo_data.ts
CHANGED
|
@@ -108,7 +108,8 @@ export class RustBinaryLocator {
|
|
|
108
108
|
const packageName = `${platformPackagePrefix}-${platform}-${arch}`;
|
|
109
109
|
|
|
110
110
|
try {
|
|
111
|
-
const
|
|
111
|
+
const resolved = import.meta.resolve(`${packageName}/${binaryName}`);
|
|
112
|
+
const packagePath = plugins.url.fileURLToPath(resolved);
|
|
112
113
|
if (await this.isExecutable(packagePath)) {
|
|
113
114
|
return packagePath;
|
|
114
115
|
}
|
package/ts/plugins.ts
CHANGED
|
@@ -4,8 +4,9 @@ import * as fs from 'fs';
|
|
|
4
4
|
import * as childProcess from 'child_process';
|
|
5
5
|
import * as readline from 'readline';
|
|
6
6
|
import * as events from 'events';
|
|
7
|
+
import * as url from 'url';
|
|
7
8
|
|
|
8
|
-
export { path, fs, childProcess, readline, events };
|
|
9
|
+
export { path, fs, childProcess, readline, events, url };
|
|
9
10
|
|
|
10
11
|
// @push.rocks scope
|
|
11
12
|
import * as smartpath from '@push.rocks/smartpath';
|