starpc 0.41.2 → 0.43.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/README.md +101 -20
- package/dist/mock/mock.pb.d.ts +1 -1
- package/dist/mock/mock.pb.js +4 -5
- package/dist/mock/mock_srpc.pb.d.ts +3 -3
- package/dist/mock/mock_srpc.pb.js +5 -5
- package/echo/Cargo.toml +21 -0
- package/echo/build.rs +15 -0
- package/echo/echo.pb.cc +405 -0
- package/echo/echo.pb.go +9 -27
- package/echo/echo.pb.h +364 -0
- package/echo/echo_srpc.pb.go +1 -1
- package/echo/gen/mod.rs +3 -0
- package/echo/main.rs +162 -0
- package/go.mod +18 -10
- package/go.sum +28 -18
- package/mock/mock.pb.cc +394 -0
- package/mock/mock.pb.go +9 -27
- package/mock/mock.pb.h +366 -0
- package/mock/mock.pb.ts +11 -13
- package/mock/mock_srpc.pb.go +1 -1
- package/mock/mock_srpc.pb.ts +12 -9
- package/package.json +27 -25
- package/srpc/Cargo.toml +26 -0
- package/srpc/build.rs +15 -0
- package/srpc/client.rs +356 -0
- package/srpc/codec.rs +225 -0
- package/srpc/error.rs +177 -0
- package/srpc/handler.rs +163 -0
- package/srpc/invoker.rs +192 -0
- package/srpc/lib.rs +107 -0
- package/srpc/message.rs +9 -0
- package/srpc/mux.rs +353 -0
- package/srpc/packet.rs +334 -0
- package/srpc/proto/mod.rs +10 -0
- package/srpc/rpc.rs +777 -0
- package/srpc/rpcproto.pb.cc +1381 -0
- package/srpc/rpcproto.pb.go +75 -183
- package/srpc/rpcproto.pb.h +1451 -0
- package/srpc/server.rs +337 -0
- package/srpc/stream.rs +304 -0
- package/srpc/testing.rs +290 -0
- package/srpc/tests/integration_test.rs +495 -0
- package/srpc/transport.rs +218 -0
- package/Makefile +0 -154
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# starpc
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/starpc)
|
|
4
|
+
[](https://crates.io/crates/starpc)
|
|
5
|
+
[](https://github.com/aperturerobotics/starpc/actions)
|
|
3
6
|
[![GoDoc Widget]][GoDoc] [![Go Report Card Widget]][Go Report Card]
|
|
4
7
|
|
|
5
8
|
> Streaming Protobuf RPC with bidirectional streaming over any multiplexed transport.
|
|
@@ -12,32 +15,43 @@
|
|
|
12
15
|
## Table of Contents
|
|
13
16
|
|
|
14
17
|
- [Features](#features)
|
|
18
|
+
- [Documentation](#documentation)
|
|
15
19
|
- [Installation](#installation)
|
|
16
20
|
- [Quick Start](#quick-start)
|
|
17
21
|
- [Examples](#examples)
|
|
18
22
|
- [Protobuf Definition](#protobuf)
|
|
19
23
|
- [Go Implementation](#go)
|
|
20
24
|
- [TypeScript Implementation](#typescript)
|
|
25
|
+
- [Rust Implementation](#rust)
|
|
21
26
|
- [Debugging](#debugging)
|
|
22
27
|
- [Development Setup](#development-setup)
|
|
23
28
|
- [Support](#support)
|
|
24
29
|
|
|
25
30
|
## Features
|
|
26
31
|
|
|
27
|
-
- Full [Proto3 services] support for TypeScript and
|
|
32
|
+
- Full [Proto3 services] support for TypeScript, Go, and Rust
|
|
28
33
|
- Bidirectional streaming in browsers via WebSocket or WebRTC
|
|
29
34
|
- Built on libp2p streams with [@chainsafe/libp2p-yamux]
|
|
30
35
|
- Efficient multiplexing of RPCs over single connections
|
|
31
36
|
- Zero-reflection Go via [protobuf-go-lite]
|
|
32
37
|
- Lightweight TypeScript via [protobuf-es-lite]
|
|
38
|
+
- Async Rust via [prost] and [tokio]
|
|
33
39
|
- Sub-stream support via [rpcstream]
|
|
34
40
|
|
|
35
41
|
[Proto3 services]: https://developers.google.com/protocol-buffers/docs/proto3#services
|
|
36
42
|
[@chainsafe/libp2p-yamux]: https://github.com/ChainSafe/js-libp2p-yamux
|
|
37
43
|
[protobuf-go-lite]: https://github.com/aperturerobotics/protobuf-go-lite
|
|
38
44
|
[protobuf-es-lite]: https://github.com/aperturerobotics/protobuf-es-lite
|
|
45
|
+
[prost]: https://github.com/tokio-rs/prost
|
|
46
|
+
[tokio]: https://github.com/tokio-rs/tokio
|
|
39
47
|
[rpcstream]: ./rpcstream
|
|
40
48
|
|
|
49
|
+
## Documentation
|
|
50
|
+
|
|
51
|
+
- [Getting Started with TypeScript](./GET_STARTED_TYPESCRIPT.md)
|
|
52
|
+
- [Getting Started with Go](./GET_STARTED_GO.md)
|
|
53
|
+
- [Getting Started with Rust](./GET_STARTED_RUST.md)
|
|
54
|
+
|
|
41
55
|
## Installation
|
|
42
56
|
|
|
43
57
|
```bash
|
|
@@ -45,18 +59,20 @@
|
|
|
45
59
|
git clone -b starpc https://github.com/aperturerobotics/protobuf-project
|
|
46
60
|
cd protobuf-project
|
|
47
61
|
|
|
48
|
-
# Install dependencies
|
|
49
|
-
|
|
62
|
+
# Install dependencies (any of these work)
|
|
63
|
+
bun install
|
|
64
|
+
npm install
|
|
65
|
+
pnpm install
|
|
50
66
|
|
|
51
67
|
# Generate TypeScript and Go code
|
|
52
|
-
|
|
68
|
+
bun run gen
|
|
53
69
|
```
|
|
54
70
|
|
|
55
71
|
## Quick Start
|
|
56
72
|
|
|
57
73
|
1. Start with the [protobuf-project] template repository (starpc branch)
|
|
58
74
|
2. Add your .proto files to the project
|
|
59
|
-
3. Run `
|
|
75
|
+
3. Run `bun run gen` to generate TypeScript and Go code
|
|
60
76
|
4. Implement your services using the examples below
|
|
61
77
|
|
|
62
78
|
[protobuf-project]: https://github.com/aperturerobotics/protobuf-project/tree/starpc
|
|
@@ -129,6 +145,84 @@ For TypeScript <-> TypeScript, see the [e2e] test.
|
|
|
129
145
|
[e2e]: ./e2e/e2e.ts
|
|
130
146
|
[integration]: ./integration/integration.ts
|
|
131
147
|
|
|
148
|
+
### Rust
|
|
149
|
+
|
|
150
|
+
Add the dependencies to your `Cargo.toml`:
|
|
151
|
+
|
|
152
|
+
```toml
|
|
153
|
+
[dependencies]
|
|
154
|
+
starpc = "0.1"
|
|
155
|
+
prost = "0.13"
|
|
156
|
+
tokio = { version = "1", features = ["rt", "macros"] }
|
|
157
|
+
|
|
158
|
+
[build-dependencies]
|
|
159
|
+
starpc-build = "0.1"
|
|
160
|
+
prost-build = "0.13"
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Create a `build.rs` to generate code from your proto files:
|
|
164
|
+
|
|
165
|
+
```rust
|
|
166
|
+
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
167
|
+
starpc_build::configure()
|
|
168
|
+
.compile_protos(&["proto/echo.proto"], &["proto"])?;
|
|
169
|
+
Ok(())
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Implement and use your service:
|
|
174
|
+
|
|
175
|
+
```rust
|
|
176
|
+
use starpc::{Client, Mux, Server, SrpcClient};
|
|
177
|
+
use std::sync::Arc;
|
|
178
|
+
|
|
179
|
+
// Include generated code
|
|
180
|
+
mod proto {
|
|
181
|
+
include!(concat!(env!("OUT_DIR"), "/echo.rs"));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
use proto::*;
|
|
185
|
+
|
|
186
|
+
// Implement the server trait
|
|
187
|
+
struct EchoServer;
|
|
188
|
+
|
|
189
|
+
#[starpc::async_trait]
|
|
190
|
+
impl EchoerServer for EchoServer {
|
|
191
|
+
async fn echo(&self, request: EchoMsg) -> starpc::Result<EchoMsg> {
|
|
192
|
+
Ok(request) // Echo back the message
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
#[tokio::main]
|
|
197
|
+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
198
|
+
// Create server
|
|
199
|
+
let mux = Arc::new(Mux::new());
|
|
200
|
+
mux.register(Arc::new(EchoerHandler::new(EchoServer)))?;
|
|
201
|
+
let server = Server::with_arc(mux);
|
|
202
|
+
|
|
203
|
+
// Create an in-memory connection for demonstration
|
|
204
|
+
let (client_stream, server_stream) = tokio::io::duplex(64 * 1024);
|
|
205
|
+
|
|
206
|
+
// Spawn server handler
|
|
207
|
+
tokio::spawn(async move {
|
|
208
|
+
let _ = server.handle_stream(server_stream).await;
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// Create client
|
|
212
|
+
let opener = starpc::client::transport::SingleStreamOpener::new(client_stream);
|
|
213
|
+
let client = SrpcClient::new(opener);
|
|
214
|
+
let echoer = EchoerClientImpl::new(client);
|
|
215
|
+
|
|
216
|
+
// Make RPC call
|
|
217
|
+
let response = echoer.echo(&EchoMsg { body: "Hello!".into() }).await?;
|
|
218
|
+
println!("Response: {}", response.body);
|
|
219
|
+
|
|
220
|
+
Ok(())
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
See the [echo example](./echo/main.rs) for a complete working example.
|
|
225
|
+
|
|
132
226
|
#### WebSocket Example
|
|
133
227
|
|
|
134
228
|
Connect to a WebSocket server:
|
|
@@ -221,21 +315,8 @@ Uses [protobuf-es-lite] (fork of [protobuf-es]) for TypeScript.
|
|
|
221
315
|
|
|
222
316
|
### MacOS
|
|
223
317
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
```bash
|
|
227
|
-
brew install bash make coreutils gnu-sed findutils protobuf
|
|
228
|
-
brew link --overwrite protobuf
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
Add to your shell rc file (.bashrc, .zshrc):
|
|
232
|
-
|
|
233
|
-
```bash
|
|
234
|
-
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
|
|
235
|
-
export PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"
|
|
236
|
-
export PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
|
|
237
|
-
export PATH="/opt/homebrew/opt/make/libexec/gnubin:$PATH"
|
|
238
|
-
```
|
|
318
|
+
The `aptre` CLI handles all protobuf generation without requiring additional
|
|
319
|
+
homebrew packages. Just run `bun run gen` after installing bun.
|
|
239
320
|
|
|
240
321
|
## Support
|
|
241
322
|
|
package/dist/mock/mock.pb.d.ts
CHANGED
package/dist/mock/mock.pb.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
// @generated by protoc-gen-es-lite unknown with parameter "target=ts,ts_nocheck=false"
|
|
2
2
|
// @generated from file github.com/aperturerobotics/starpc/mock/mock.proto (package e2e.mock, syntax proto3)
|
|
3
3
|
/* eslint-disable */
|
|
4
|
-
import { createMessageType, ScalarType } from
|
|
5
|
-
export const protobufPackage =
|
|
6
|
-
;
|
|
4
|
+
import { createMessageType, ScalarType } from '@aptre/protobuf-es-lite';
|
|
5
|
+
export const protobufPackage = 'e2e.mock';
|
|
7
6
|
// MockMsg contains the message type declaration for MockMsg.
|
|
8
7
|
export const MockMsg = createMessageType({
|
|
9
|
-
typeName:
|
|
8
|
+
typeName: 'e2e.mock.MockMsg',
|
|
10
9
|
fields: [
|
|
11
|
-
{ no: 1, name:
|
|
10
|
+
{ no: 1, name: 'body', kind: 'scalar', T: ScalarType.STRING },
|
|
12
11
|
],
|
|
13
12
|
packedByDefault: true,
|
|
14
13
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { MockMsg } from
|
|
2
|
-
import { MethodKind } from
|
|
3
|
-
import { ProtoRpc } from
|
|
1
|
+
import { MockMsg } from './mock.pb.js';
|
|
2
|
+
import { MethodKind } from '@aptre/protobuf-es-lite';
|
|
3
|
+
import { ProtoRpc } from 'starpc';
|
|
4
4
|
/**
|
|
5
5
|
* Mock service mocks some RPCs for the e2e tests.
|
|
6
6
|
*
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
// @generated by protoc-gen-es-starpc none with parameter "target=ts,ts_nocheck=false"
|
|
2
2
|
// @generated from file github.com/aperturerobotics/starpc/mock/mock.proto (package e2e.mock, syntax proto3)
|
|
3
3
|
/* eslint-disable */
|
|
4
|
-
import { MockMsg } from
|
|
5
|
-
import { MethodKind } from
|
|
4
|
+
import { MockMsg } from './mock.pb.js';
|
|
5
|
+
import { MethodKind } from '@aptre/protobuf-es-lite';
|
|
6
6
|
/**
|
|
7
7
|
* Mock service mocks some RPCs for the e2e tests.
|
|
8
8
|
*
|
|
9
9
|
* @generated from service e2e.mock.Mock
|
|
10
10
|
*/
|
|
11
11
|
export const MockDefinition = {
|
|
12
|
-
typeName:
|
|
12
|
+
typeName: 'e2e.mock.Mock',
|
|
13
13
|
methods: {
|
|
14
14
|
/**
|
|
15
15
|
* MockRequest runs a mock unary request.
|
|
@@ -17,12 +17,12 @@ export const MockDefinition = {
|
|
|
17
17
|
* @generated from rpc e2e.mock.Mock.MockRequest
|
|
18
18
|
*/
|
|
19
19
|
MockRequest: {
|
|
20
|
-
name:
|
|
20
|
+
name: 'MockRequest',
|
|
21
21
|
I: MockMsg,
|
|
22
22
|
O: MockMsg,
|
|
23
23
|
kind: MethodKind.Unary,
|
|
24
24
|
},
|
|
25
|
-
}
|
|
25
|
+
},
|
|
26
26
|
};
|
|
27
27
|
export const MockServiceName = MockDefinition.typeName;
|
|
28
28
|
export class MockClient {
|
package/echo/Cargo.toml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "echo-example"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
rust-version.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
publish = false
|
|
8
|
+
|
|
9
|
+
[[bin]]
|
|
10
|
+
name = "echo-example"
|
|
11
|
+
path = "main.rs"
|
|
12
|
+
|
|
13
|
+
[dependencies]
|
|
14
|
+
starpc = { workspace = true }
|
|
15
|
+
prost = { workspace = true }
|
|
16
|
+
tokio = { version = "1", features = ["full"] }
|
|
17
|
+
async-trait = { workspace = true }
|
|
18
|
+
|
|
19
|
+
[build-dependencies]
|
|
20
|
+
starpc-build = { workspace = true }
|
|
21
|
+
prost-build = { workspace = true }
|
package/echo/build.rs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
use std::io::Result;
|
|
2
|
+
use std::path::PathBuf;
|
|
3
|
+
|
|
4
|
+
fn main() -> Result<()> {
|
|
5
|
+
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
6
|
+
// Use simplified proto without rpcstream dependency
|
|
7
|
+
let proto_path = manifest_dir.join("echo_rust.proto");
|
|
8
|
+
|
|
9
|
+
println!("cargo:rerun-if-changed={}", proto_path.display());
|
|
10
|
+
|
|
11
|
+
starpc_build::configure()
|
|
12
|
+
.compile_protos(&[proto_path], &[&manifest_dir])?;
|
|
13
|
+
|
|
14
|
+
Ok(())
|
|
15
|
+
}
|