starpc 0.49.8 → 0.49.9
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 +5 -5
- package/dist/srpc/packet.d.ts +4 -4
- package/go.mod +1 -1
- package/go.sum +2 -2
- package/package.json +2 -2
- package/srpc/build.rs +593 -0
- package/srpc/lib.rs +4 -1
- package/srpc/packet-rw.go +3 -6
package/README.md
CHANGED
|
@@ -151,20 +151,20 @@ Add the dependencies to your `Cargo.toml`:
|
|
|
151
151
|
|
|
152
152
|
```toml
|
|
153
153
|
[dependencies]
|
|
154
|
-
starpc = "0.
|
|
155
|
-
prost = "0.
|
|
154
|
+
starpc = "0.49"
|
|
155
|
+
prost = "0.14"
|
|
156
156
|
tokio = { version = "1", features = ["rt", "macros"] }
|
|
157
157
|
|
|
158
158
|
[build-dependencies]
|
|
159
|
-
starpc
|
|
160
|
-
prost-build = "0.
|
|
159
|
+
starpc = { version = "0.49", features = ["build"] }
|
|
160
|
+
prost-build = "0.14"
|
|
161
161
|
```
|
|
162
162
|
|
|
163
163
|
Create a `build.rs` to generate code from your proto files:
|
|
164
164
|
|
|
165
165
|
```rust
|
|
166
166
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
167
|
-
|
|
167
|
+
starpc::build::configure()
|
|
168
168
|
.compile_protos(&["proto/echo.proto"], &["proto"])?;
|
|
169
169
|
Ok(())
|
|
170
170
|
}
|
package/dist/srpc/packet.d.ts
CHANGED
|
@@ -7,14 +7,14 @@ export declare function uint32LEDecode(data: Uint8ArrayList): number;
|
|
|
7
7
|
export declare namespace uint32LEDecode {
|
|
8
8
|
var bytes: number;
|
|
9
9
|
}
|
|
10
|
-
export declare function uint32LEEncode(value: number): Uint8ArrayList
|
|
10
|
+
export declare function uint32LEEncode(value: number): Uint8ArrayList<ArrayBuffer>;
|
|
11
11
|
export declare namespace uint32LEEncode {
|
|
12
12
|
var bytes: number;
|
|
13
13
|
}
|
|
14
|
-
export declare function lengthPrefixEncode(source: Source<Uint8Array | Uint8ArrayList>, lengthEncoder: typeof uint32LEEncode): AsyncGenerator<Uint8ArrayList
|
|
15
|
-
export declare function lengthPrefixDecode(source: Source<Uint8Array | Uint8ArrayList>, lengthDecoder: typeof uint32LEDecode): AsyncGenerator<Uint8ArrayList
|
|
14
|
+
export declare function lengthPrefixEncode(source: Source<Uint8Array | Uint8ArrayList>, lengthEncoder: typeof uint32LEEncode): AsyncGenerator<Uint8ArrayList<ArrayBufferLike>, void, unknown>;
|
|
15
|
+
export declare function lengthPrefixDecode(source: Source<Uint8Array | Uint8ArrayList>, lengthDecoder: typeof uint32LEDecode): AsyncGenerator<Uint8ArrayList<ArrayBufferLike>, void, unknown>;
|
|
16
16
|
export declare function prependLengthPrefixTransform(lengthEncoder?: {
|
|
17
|
-
(value: number): Uint8ArrayList
|
|
17
|
+
(value: number): Uint8ArrayList<ArrayBuffer>;
|
|
18
18
|
bytes: number;
|
|
19
19
|
}): Transform<Source<Uint8Array | Uint8ArrayList>, AsyncGenerator<Uint8ArrayList, void, undefined> | Generator<Uint8ArrayList, void, undefined>>;
|
|
20
20
|
export declare function parseLengthPrefixTransform(lengthDecoder?: {
|
package/go.mod
CHANGED
|
@@ -21,7 +21,7 @@ require (
|
|
|
21
21
|
require (
|
|
22
22
|
github.com/libp2p/go-yamux/v4 v4.0.2 // latest
|
|
23
23
|
github.com/pkg/errors v0.9.1 // latest
|
|
24
|
-
github.com/sirupsen/logrus v1.9.5-0.
|
|
24
|
+
github.com/sirupsen/logrus v1.9.5-0.20260508084601-d4a50659cfd6 // latest
|
|
25
25
|
)
|
|
26
26
|
|
|
27
27
|
require (
|
package/go.sum
CHANGED
|
@@ -30,8 +30,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
|
|
30
30
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
|
31
31
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
32
32
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
33
|
-
github.com/sirupsen/logrus v1.9.5-0.
|
|
34
|
-
github.com/sirupsen/logrus v1.9.5-0.
|
|
33
|
+
github.com/sirupsen/logrus v1.9.5-0.20260508084601-d4a50659cfd6 h1:D6qewLO/pJ+JvUwuri1dmCbc32d/eO+xyqg+p3N+9kA=
|
|
34
|
+
github.com/sirupsen/logrus v1.9.5-0.20260508084601-d4a50659cfd6/go.mod h1:FXZFonkDAnFozmO+5hGAFvB0Yg9/j2SIhA/QuIkP180=
|
|
35
35
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
|
36
36
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
|
37
37
|
github.com/tetratelabs/wazero v1.11.0 h1:+gKemEuKCTevU4d7ZTzlsvgd1uaToIDtlQlmNbwqYhA=
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starpc",
|
|
3
|
-
"version": "0.49.
|
|
3
|
+
"version": "0.49.9",
|
|
4
4
|
"description": "Streaming protobuf RPC service protocol over any two-way channel.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -137,7 +137,7 @@
|
|
|
137
137
|
"it-pipe": "^3.0.1",
|
|
138
138
|
"it-pushable": "^3.2.3",
|
|
139
139
|
"it-stream-types": "^2.0.4",
|
|
140
|
-
"uint8arraylist": "^
|
|
140
|
+
"uint8arraylist": "^3.0.0",
|
|
141
141
|
"ws": "^8.20.0"
|
|
142
142
|
}
|
|
143
143
|
}
|
package/srpc/build.rs
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
//! Build-script helpers for generating Starpc Rust service bindings.
|
|
2
|
+
//!
|
|
3
|
+
//! Enable the `build` feature and call [`configure`] from a downstream
|
|
4
|
+
//! `build.rs` to install the Starpc service generator into `prost-build`.
|
|
5
|
+
|
|
6
|
+
/// Configures `prost-build` to generate Starpc service bindings.
|
|
7
|
+
///
|
|
8
|
+
/// The returned config generates normal Prost message types and appends Starpc
|
|
9
|
+
/// client/server traits, client implementations, stream wrappers, and handler
|
|
10
|
+
/// glue for every protobuf service.
|
|
11
|
+
pub fn configure() -> prost_build::Config {
|
|
12
|
+
let mut config = prost_build::Config::new();
|
|
13
|
+
if let Ok(protoc) = protoc_bin_vendored::protoc_bin_path() {
|
|
14
|
+
config.protoc_executable(protoc);
|
|
15
|
+
}
|
|
16
|
+
if let Ok(include) = protoc_bin_vendored::include_path() {
|
|
17
|
+
config.protoc_arg(format!("--proto_path={}", include.display()));
|
|
18
|
+
}
|
|
19
|
+
config.extern_path(".rpcstream", "::starpc::rpcstream");
|
|
20
|
+
config.service_generator(Box::new(StarpcServiceGenerator::default()));
|
|
21
|
+
config
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// StarpcServiceGenerator emits Starpc client and server glue for Prost services.
|
|
25
|
+
#[derive(Debug, Default)]
|
|
26
|
+
pub struct StarpcServiceGenerator;
|
|
27
|
+
|
|
28
|
+
impl prost_build::ServiceGenerator for StarpcServiceGenerator {
|
|
29
|
+
fn generate(&mut self, service: prost_build::Service, buf: &mut String) {
|
|
30
|
+
let mut gen = Generator::new(service, buf);
|
|
31
|
+
gen.generate();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
struct Generator<'a> {
|
|
36
|
+
service: prost_build::Service,
|
|
37
|
+
buf: &'a mut String,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
impl<'a> Generator<'a> {
|
|
41
|
+
fn new(service: prost_build::Service, buf: &'a mut String) -> Self {
|
|
42
|
+
Self { service, buf }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fn generate(&mut self) {
|
|
46
|
+
let service_id = self.service_id();
|
|
47
|
+
let service_name = self.service.name.clone();
|
|
48
|
+
|
|
49
|
+
self.line("");
|
|
50
|
+
self.line("#[allow(unused_imports)]");
|
|
51
|
+
self.line("use starpc::StreamExt;");
|
|
52
|
+
self.line("");
|
|
53
|
+
self.line(&format!("/// Service ID for {}.", service_name));
|
|
54
|
+
self.line(&format!(
|
|
55
|
+
"pub const {}: &str = {:?};",
|
|
56
|
+
service_id_const(&service_name),
|
|
57
|
+
service_id
|
|
58
|
+
));
|
|
59
|
+
self.line("");
|
|
60
|
+
|
|
61
|
+
self.generate_stream_traits();
|
|
62
|
+
self.generate_client_trait();
|
|
63
|
+
self.generate_client_impl();
|
|
64
|
+
self.generate_stream_impls();
|
|
65
|
+
self.generate_server_trait();
|
|
66
|
+
self.generate_handler();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fn generate_stream_traits(&mut self) {
|
|
70
|
+
let service_name = self.service.name.clone();
|
|
71
|
+
for method in self.service.methods.clone() {
|
|
72
|
+
if !method.client_streaming && !method.server_streaming {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let stream_trait = stream_trait_name(&service_name, &method);
|
|
77
|
+
|
|
78
|
+
self.line(&format!(
|
|
79
|
+
"/// Stream trait for {}.{}.",
|
|
80
|
+
service_name, method.proto_name
|
|
81
|
+
));
|
|
82
|
+
self.line("#[starpc::async_trait]");
|
|
83
|
+
self.line(&format!("pub trait {}: Send + Sync {{", stream_trait));
|
|
84
|
+
self.line(" /// Returns the context for this stream.");
|
|
85
|
+
self.line(" fn context(&self) -> &starpc::Context;");
|
|
86
|
+
|
|
87
|
+
if method.client_streaming {
|
|
88
|
+
self.line(" /// Sends a message on the stream.");
|
|
89
|
+
self.line(&format!(
|
|
90
|
+
" async fn send(&self, msg: &{}) -> starpc::Result<()>;",
|
|
91
|
+
method.input_type
|
|
92
|
+
));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if method.server_streaming {
|
|
96
|
+
self.line(" /// Receives a message from the stream.");
|
|
97
|
+
self.line(&format!(
|
|
98
|
+
" async fn recv(&self) -> starpc::Result<{}>;",
|
|
99
|
+
method.output_type
|
|
100
|
+
));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if method.client_streaming && !method.server_streaming {
|
|
104
|
+
self.line(" /// Closes the send side and receives the response.");
|
|
105
|
+
self.line(&format!(
|
|
106
|
+
" async fn close_and_recv(&self) -> starpc::Result<{}>;",
|
|
107
|
+
method.output_type
|
|
108
|
+
));
|
|
109
|
+
} else {
|
|
110
|
+
self.line(" /// Closes the stream.");
|
|
111
|
+
self.line(" async fn close(&self) -> starpc::Result<()>;");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
self.line("}");
|
|
115
|
+
self.line("");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fn generate_client_trait(&mut self) {
|
|
120
|
+
let service_name = self.service.name.clone();
|
|
121
|
+
self.line(&format!("/// Client trait for {}.", service_name));
|
|
122
|
+
self.line("#[starpc::async_trait]");
|
|
123
|
+
self.line(&format!("pub trait {}Client: Send + Sync {{", service_name));
|
|
124
|
+
|
|
125
|
+
for method in self.service.methods.clone() {
|
|
126
|
+
self.line(&format!(" /// {}.", method.proto_name));
|
|
127
|
+
self.line(&format!(
|
|
128
|
+
" {};",
|
|
129
|
+
client_trait_method(&service_name, &method)
|
|
130
|
+
));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
self.line("}");
|
|
134
|
+
self.line("");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
fn generate_client_impl(&mut self) {
|
|
138
|
+
let service_id = self.service_id();
|
|
139
|
+
let service_name = self.service.name.clone();
|
|
140
|
+
|
|
141
|
+
self.line(&format!("/// Client implementation for {}.", service_name));
|
|
142
|
+
self.line(&format!("pub struct {}ClientImpl<C> {{", service_name));
|
|
143
|
+
self.line(" client: C,");
|
|
144
|
+
self.line("}");
|
|
145
|
+
self.line("");
|
|
146
|
+
self.line(&format!(
|
|
147
|
+
"impl<C: starpc::Client> {}ClientImpl<C> {{",
|
|
148
|
+
service_name
|
|
149
|
+
));
|
|
150
|
+
self.line(" /// Creates a new client.");
|
|
151
|
+
self.line(" pub fn new(client: C) -> Self {");
|
|
152
|
+
self.line(" Self { client }");
|
|
153
|
+
self.line(" }");
|
|
154
|
+
self.line("}");
|
|
155
|
+
self.line("");
|
|
156
|
+
self.line("#[starpc::async_trait]");
|
|
157
|
+
self.line(&format!(
|
|
158
|
+
"impl<C: starpc::Client + 'static> {}Client for {}ClientImpl<C> {{",
|
|
159
|
+
service_name, service_name
|
|
160
|
+
));
|
|
161
|
+
|
|
162
|
+
for method in self.service.methods.clone() {
|
|
163
|
+
let method_name = &method.name;
|
|
164
|
+
if method.client_streaming && method.server_streaming {
|
|
165
|
+
self.line(&format!(
|
|
166
|
+
" async fn {}(&self) -> starpc::Result<Box<dyn {}>> {{",
|
|
167
|
+
method_name,
|
|
168
|
+
stream_trait_name(&service_name, &method)
|
|
169
|
+
));
|
|
170
|
+
self.line(&format!(
|
|
171
|
+
" let stream = self.client.new_stream({:?}, {:?}, None).await?;",
|
|
172
|
+
service_id, method.proto_name
|
|
173
|
+
));
|
|
174
|
+
self.line(&format!(
|
|
175
|
+
" Ok(Box::new({}Impl {{ stream }}))",
|
|
176
|
+
stream_trait_name(&service_name, &method)
|
|
177
|
+
));
|
|
178
|
+
self.line(" }");
|
|
179
|
+
} else if method.server_streaming {
|
|
180
|
+
self.line(&format!(
|
|
181
|
+
" async fn {}(&self, request: &{}) -> starpc::Result<Box<dyn {}>> {{",
|
|
182
|
+
method_name,
|
|
183
|
+
method.input_type,
|
|
184
|
+
stream_trait_name(&service_name, &method)
|
|
185
|
+
));
|
|
186
|
+
self.line(" use starpc::ProstMessage;");
|
|
187
|
+
self.line(" let data = request.encode_to_vec();");
|
|
188
|
+
self.line(&format!(
|
|
189
|
+
" let stream = self.client.new_stream({:?}, {:?}, Some(&data)).await?;",
|
|
190
|
+
service_id, method.proto_name
|
|
191
|
+
));
|
|
192
|
+
self.line(" stream.close_send().await?;");
|
|
193
|
+
self.line(&format!(
|
|
194
|
+
" Ok(Box::new({}Impl {{ stream }}))",
|
|
195
|
+
stream_trait_name(&service_name, &method)
|
|
196
|
+
));
|
|
197
|
+
self.line(" }");
|
|
198
|
+
} else if method.client_streaming {
|
|
199
|
+
self.line(&format!(
|
|
200
|
+
" async fn {}(&self) -> starpc::Result<Box<dyn {}>> {{",
|
|
201
|
+
method_name,
|
|
202
|
+
stream_trait_name(&service_name, &method)
|
|
203
|
+
));
|
|
204
|
+
self.line(&format!(
|
|
205
|
+
" let stream = self.client.new_stream({:?}, {:?}, None).await?;",
|
|
206
|
+
service_id, method.proto_name
|
|
207
|
+
));
|
|
208
|
+
self.line(&format!(
|
|
209
|
+
" Ok(Box::new({}Impl {{ stream }}))",
|
|
210
|
+
stream_trait_name(&service_name, &method)
|
|
211
|
+
));
|
|
212
|
+
self.line(" }");
|
|
213
|
+
} else {
|
|
214
|
+
self.line(&format!(
|
|
215
|
+
" async fn {}(&self, request: &{}) -> starpc::Result<{}> {{",
|
|
216
|
+
method_name, method.input_type, method.output_type
|
|
217
|
+
));
|
|
218
|
+
self.line(&format!(
|
|
219
|
+
" self.client.exec_call({:?}, {:?}, request).await",
|
|
220
|
+
service_id, method.proto_name
|
|
221
|
+
));
|
|
222
|
+
self.line(" }");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
self.line("}");
|
|
227
|
+
self.line("");
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
fn generate_stream_impls(&mut self) {
|
|
231
|
+
let service_name = self.service.name.clone();
|
|
232
|
+
for method in self.service.methods.clone() {
|
|
233
|
+
if !method.client_streaming && !method.server_streaming {
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let stream_trait = stream_trait_name(&service_name, &method);
|
|
238
|
+
|
|
239
|
+
self.line(&format!("struct {}Impl {{", stream_trait));
|
|
240
|
+
self.line(" stream: Box<dyn starpc::Stream>,");
|
|
241
|
+
self.line("}");
|
|
242
|
+
self.line("");
|
|
243
|
+
self.line("#[starpc::async_trait]");
|
|
244
|
+
self.line(&format!(
|
|
245
|
+
"impl {} for {}Impl {{",
|
|
246
|
+
stream_trait, stream_trait
|
|
247
|
+
));
|
|
248
|
+
self.line(" fn context(&self) -> &starpc::Context {");
|
|
249
|
+
self.line(" self.stream.context()");
|
|
250
|
+
self.line(" }");
|
|
251
|
+
|
|
252
|
+
if method.client_streaming {
|
|
253
|
+
self.line(&format!(
|
|
254
|
+
" async fn send(&self, msg: &{}) -> starpc::Result<()> {{",
|
|
255
|
+
method.input_type
|
|
256
|
+
));
|
|
257
|
+
self.line(" self.stream.msg_send(msg).await");
|
|
258
|
+
self.line(" }");
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if method.server_streaming {
|
|
262
|
+
self.line(&format!(
|
|
263
|
+
" async fn recv(&self) -> starpc::Result<{}> {{",
|
|
264
|
+
method.output_type
|
|
265
|
+
));
|
|
266
|
+
self.line(" self.stream.msg_recv().await");
|
|
267
|
+
self.line(" }");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if method.client_streaming && !method.server_streaming {
|
|
271
|
+
self.line(&format!(
|
|
272
|
+
" async fn close_and_recv(&self) -> starpc::Result<{}> {{",
|
|
273
|
+
method.output_type
|
|
274
|
+
));
|
|
275
|
+
self.line(" self.stream.close_send().await?;");
|
|
276
|
+
self.line(" self.stream.msg_recv().await");
|
|
277
|
+
self.line(" }");
|
|
278
|
+
} else {
|
|
279
|
+
self.line(" async fn close(&self) -> starpc::Result<()> {");
|
|
280
|
+
self.line(" self.stream.close().await");
|
|
281
|
+
self.line(" }");
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
self.line("}");
|
|
285
|
+
self.line("");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
fn generate_server_trait(&mut self) {
|
|
290
|
+
let service_name = self.service.name.clone();
|
|
291
|
+
|
|
292
|
+
self.line(&format!("/// Server trait for {}.", service_name));
|
|
293
|
+
self.line("#[starpc::async_trait]");
|
|
294
|
+
self.line(&format!("pub trait {}Server: Send + Sync {{", service_name));
|
|
295
|
+
|
|
296
|
+
for method in self.service.methods.clone() {
|
|
297
|
+
self.line(&format!(" /// {}.", method.proto_name));
|
|
298
|
+
if method.client_streaming && method.server_streaming {
|
|
299
|
+
self.line(&format!(
|
|
300
|
+
" async fn {}(&self, stream: Box<dyn starpc::Stream>) -> starpc::Result<()>;",
|
|
301
|
+
method.name
|
|
302
|
+
));
|
|
303
|
+
} else if method.server_streaming {
|
|
304
|
+
self.line(&format!(
|
|
305
|
+
" async fn {}(&self, request: {}, stream: Box<dyn starpc::Stream>) -> starpc::Result<()>;",
|
|
306
|
+
method.name, method.input_type
|
|
307
|
+
));
|
|
308
|
+
} else if method.client_streaming {
|
|
309
|
+
self.line(&format!(
|
|
310
|
+
" async fn {}(&self, stream: &dyn starpc::Stream) -> starpc::Result<{}>;",
|
|
311
|
+
method.name, method.output_type
|
|
312
|
+
));
|
|
313
|
+
} else {
|
|
314
|
+
self.line(&format!(
|
|
315
|
+
" async fn {}(&self, request: {}) -> starpc::Result<{}>;",
|
|
316
|
+
method.name, method.input_type, method.output_type
|
|
317
|
+
));
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
self.line("}");
|
|
322
|
+
self.line("");
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
fn generate_handler(&mut self) {
|
|
326
|
+
let service_id = self.service_id();
|
|
327
|
+
let service_name = self.service.name.clone();
|
|
328
|
+
let methods = self.service.methods.clone();
|
|
329
|
+
let method_ids = method_ids_const(&service_name);
|
|
330
|
+
|
|
331
|
+
self.line(&format!("const {}: &[&str] = &[", method_ids));
|
|
332
|
+
for method in &methods {
|
|
333
|
+
self.line(&format!(" {:?},", method.proto_name));
|
|
334
|
+
}
|
|
335
|
+
self.line("];");
|
|
336
|
+
self.line("");
|
|
337
|
+
self.line(&format!("/// Handler for {}.", service_name));
|
|
338
|
+
self.line(&format!(
|
|
339
|
+
"pub struct {}Handler<S: {}Server> {{",
|
|
340
|
+
service_name, service_name
|
|
341
|
+
));
|
|
342
|
+
self.line(" server: std::sync::Arc<S>,");
|
|
343
|
+
self.line("}");
|
|
344
|
+
self.line("");
|
|
345
|
+
self.line(&format!(
|
|
346
|
+
"impl<S: {}Server + 'static> {}Handler<S> {{",
|
|
347
|
+
service_name, service_name
|
|
348
|
+
));
|
|
349
|
+
self.line(" /// Creates a new handler wrapping the server implementation.");
|
|
350
|
+
self.line(" pub fn new(server: S) -> Self {");
|
|
351
|
+
self.line(" Self { server: std::sync::Arc::new(server) }");
|
|
352
|
+
self.line(" }");
|
|
353
|
+
self.line("");
|
|
354
|
+
self.line(" /// Creates a new handler with a shared server.");
|
|
355
|
+
self.line(" pub fn with_arc(server: std::sync::Arc<S>) -> Self {");
|
|
356
|
+
self.line(" Self { server }");
|
|
357
|
+
self.line(" }");
|
|
358
|
+
self.line("}");
|
|
359
|
+
self.line("");
|
|
360
|
+
self.line("#[starpc::async_trait]");
|
|
361
|
+
self.line(&format!(
|
|
362
|
+
"impl<S: {}Server + 'static> starpc::Invoker for {}Handler<S> {{",
|
|
363
|
+
service_name, service_name
|
|
364
|
+
));
|
|
365
|
+
self.line(" async fn invoke_method(");
|
|
366
|
+
self.line(" &self,");
|
|
367
|
+
self.line(" _service_id: &str,");
|
|
368
|
+
self.line(" method_id: &str,");
|
|
369
|
+
self.line(" stream: Box<dyn starpc::Stream>,");
|
|
370
|
+
self.line(" ) -> (bool, starpc::Result<()>) {");
|
|
371
|
+
self.line(" match method_id {");
|
|
372
|
+
|
|
373
|
+
for method in &methods {
|
|
374
|
+
self.line(&format!(" {:?} => {{", method.proto_name));
|
|
375
|
+
if method.client_streaming && method.server_streaming {
|
|
376
|
+
self.line(&format!(
|
|
377
|
+
" (true, self.server.{}(stream).await)",
|
|
378
|
+
method.name
|
|
379
|
+
));
|
|
380
|
+
} else if method.server_streaming {
|
|
381
|
+
self.line(&format!(
|
|
382
|
+
" let request: {} = match stream.msg_recv().await {{",
|
|
383
|
+
method.input_type
|
|
384
|
+
));
|
|
385
|
+
self.line(" Ok(r) => r,");
|
|
386
|
+
self.line(" Err(e) => return (true, Err(e)),");
|
|
387
|
+
self.line(" };");
|
|
388
|
+
self.line(&format!(
|
|
389
|
+
" (true, self.server.{}(request, stream).await)",
|
|
390
|
+
method.name
|
|
391
|
+
));
|
|
392
|
+
} else if method.client_streaming {
|
|
393
|
+
self.line(&format!(
|
|
394
|
+
" match self.server.{}(stream.as_ref()).await {{",
|
|
395
|
+
method.name
|
|
396
|
+
));
|
|
397
|
+
self.line(" Ok(response) => {");
|
|
398
|
+
self.line(
|
|
399
|
+
" if let Err(e) = stream.msg_send(&response).await {",
|
|
400
|
+
);
|
|
401
|
+
self.line(" return (true, Err(e));");
|
|
402
|
+
self.line(" }");
|
|
403
|
+
self.line(" (true, Ok(()))");
|
|
404
|
+
self.line(" }");
|
|
405
|
+
self.line(" Err(e) => (true, Err(e)),");
|
|
406
|
+
self.line(" }");
|
|
407
|
+
} else {
|
|
408
|
+
self.line(&format!(
|
|
409
|
+
" let request: {} = match stream.msg_recv().await {{",
|
|
410
|
+
method.input_type
|
|
411
|
+
));
|
|
412
|
+
self.line(" Ok(r) => r,");
|
|
413
|
+
self.line(" Err(e) => return (true, Err(e)),");
|
|
414
|
+
self.line(" };");
|
|
415
|
+
self.line(&format!(
|
|
416
|
+
" match self.server.{}(request).await {{",
|
|
417
|
+
method.name
|
|
418
|
+
));
|
|
419
|
+
self.line(" Ok(response) => {");
|
|
420
|
+
self.line(
|
|
421
|
+
" if let Err(e) = stream.msg_send(&response).await {",
|
|
422
|
+
);
|
|
423
|
+
self.line(" return (true, Err(e));");
|
|
424
|
+
self.line(" }");
|
|
425
|
+
self.line(" (true, Ok(()))");
|
|
426
|
+
self.line(" }");
|
|
427
|
+
self.line(" Err(e) => (true, Err(e)),");
|
|
428
|
+
self.line(" }");
|
|
429
|
+
}
|
|
430
|
+
self.line(" }");
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
self.line(" _ => (false, Err(starpc::Error::Unimplemented)),");
|
|
434
|
+
self.line(" }");
|
|
435
|
+
self.line(" }");
|
|
436
|
+
self.line("}");
|
|
437
|
+
self.line("");
|
|
438
|
+
self.line(&format!(
|
|
439
|
+
"impl<S: {}Server + 'static> starpc::Handler for {}Handler<S> {{",
|
|
440
|
+
service_name, service_name
|
|
441
|
+
));
|
|
442
|
+
self.line(" fn service_id(&self) -> &'static str {");
|
|
443
|
+
self.line(&format!(" {:?}", service_id));
|
|
444
|
+
self.line(" }");
|
|
445
|
+
self.line("");
|
|
446
|
+
self.line(" fn method_ids(&self) -> &'static [&'static str] {");
|
|
447
|
+
self.line(&format!(" {}", method_ids));
|
|
448
|
+
self.line(" }");
|
|
449
|
+
self.line("}");
|
|
450
|
+
self.line("");
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
fn service_id(&self) -> String {
|
|
454
|
+
if self.service.package.is_empty() {
|
|
455
|
+
self.service.proto_name.clone()
|
|
456
|
+
} else {
|
|
457
|
+
format!("{}.{}", self.service.package, self.service.proto_name)
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
fn line(&mut self, line: &str) {
|
|
462
|
+
self.buf.push_str(line);
|
|
463
|
+
self.buf.push('\n');
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
fn client_trait_method(service_name: &str, method: &prost_build::Method) -> String {
|
|
468
|
+
let stream_trait = stream_trait_name(service_name, method);
|
|
469
|
+
if method.client_streaming && method.server_streaming {
|
|
470
|
+
format!(
|
|
471
|
+
"async fn {}(&self) -> starpc::Result<Box<dyn {}>>",
|
|
472
|
+
method.name, stream_trait
|
|
473
|
+
)
|
|
474
|
+
} else if method.server_streaming {
|
|
475
|
+
format!(
|
|
476
|
+
"async fn {}(&self, request: &{}) -> starpc::Result<Box<dyn {}>>",
|
|
477
|
+
method.name, method.input_type, stream_trait
|
|
478
|
+
)
|
|
479
|
+
} else if method.client_streaming {
|
|
480
|
+
format!(
|
|
481
|
+
"async fn {}(&self) -> starpc::Result<Box<dyn {}>>",
|
|
482
|
+
method.name, stream_trait
|
|
483
|
+
)
|
|
484
|
+
} else {
|
|
485
|
+
format!(
|
|
486
|
+
"async fn {}(&self, request: &{}) -> starpc::Result<{}>",
|
|
487
|
+
method.name, method.input_type, method.output_type
|
|
488
|
+
)
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
fn stream_trait_name(service_name: &str, method: &prost_build::Method) -> String {
|
|
493
|
+
format!(
|
|
494
|
+
"{}{}Stream",
|
|
495
|
+
service_name,
|
|
496
|
+
upper_camel_from_snake(&method.name)
|
|
497
|
+
)
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
fn service_id_const(service_name: &str) -> String {
|
|
501
|
+
format!("{}_SERVICE_ID", screaming_snake(service_name))
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
fn method_ids_const(service_name: &str) -> String {
|
|
505
|
+
format!("{}_METHOD_IDS", screaming_snake(service_name))
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
fn screaming_snake(name: &str) -> String {
|
|
509
|
+
let mut out = String::new();
|
|
510
|
+
for (idx, ch) in name.chars().enumerate() {
|
|
511
|
+
if ch.is_uppercase() && idx != 0 {
|
|
512
|
+
out.push('_');
|
|
513
|
+
}
|
|
514
|
+
out.extend(ch.to_uppercase());
|
|
515
|
+
}
|
|
516
|
+
out
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
fn upper_camel_from_snake(name: &str) -> String {
|
|
520
|
+
let mut out = String::new();
|
|
521
|
+
let mut upper = true;
|
|
522
|
+
for ch in name.chars() {
|
|
523
|
+
if ch == '_' {
|
|
524
|
+
upper = true;
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
if upper {
|
|
528
|
+
out.extend(ch.to_uppercase());
|
|
529
|
+
upper = false;
|
|
530
|
+
} else {
|
|
531
|
+
out.push(ch);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
out
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
#[cfg(test)]
|
|
538
|
+
mod tests {
|
|
539
|
+
use std::fs;
|
|
540
|
+
|
|
541
|
+
#[test]
|
|
542
|
+
fn configure_generates_starpc_service_glue() {
|
|
543
|
+
let root = std::env::temp_dir().join(format!(
|
|
544
|
+
"starpc-codegen-test-{}-{}",
|
|
545
|
+
std::process::id(),
|
|
546
|
+
std::time::SystemTime::now()
|
|
547
|
+
.duration_since(std::time::UNIX_EPOCH)
|
|
548
|
+
.unwrap()
|
|
549
|
+
.as_nanos()
|
|
550
|
+
));
|
|
551
|
+
let proto_dir = root.join("proto");
|
|
552
|
+
let out_dir = root.join("out");
|
|
553
|
+
let _ = fs::remove_dir_all(&root);
|
|
554
|
+
fs::create_dir_all(&proto_dir).unwrap();
|
|
555
|
+
fs::create_dir_all(&out_dir).unwrap();
|
|
556
|
+
|
|
557
|
+
let proto = proto_dir.join("test.proto");
|
|
558
|
+
fs::write(
|
|
559
|
+
&proto,
|
|
560
|
+
r#"syntax = "proto3";
|
|
561
|
+
package fixture;
|
|
562
|
+
|
|
563
|
+
service TestService {
|
|
564
|
+
rpc Unary(TestMsg) returns (TestMsg);
|
|
565
|
+
rpc ServerStream(TestMsg) returns (stream TestMsg);
|
|
566
|
+
rpc ClientStream(stream TestMsg) returns (TestMsg);
|
|
567
|
+
rpc Bidi(stream TestMsg) returns (stream TestMsg);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
message TestMsg {
|
|
571
|
+
string body = 1;
|
|
572
|
+
}
|
|
573
|
+
"#,
|
|
574
|
+
)
|
|
575
|
+
.unwrap();
|
|
576
|
+
|
|
577
|
+
let mut config = super::configure();
|
|
578
|
+
config.out_dir(&out_dir);
|
|
579
|
+
config.compile_protos(&[proto], &[proto_dir]).unwrap();
|
|
580
|
+
|
|
581
|
+
let generated = fs::read_to_string(out_dir.join("fixture.rs")).unwrap();
|
|
582
|
+
assert!(generated.contains("pub const TEST_SERVICE_SERVICE_ID"));
|
|
583
|
+
assert!(generated.contains("pub trait TestServiceClient"));
|
|
584
|
+
assert!(generated.contains("pub struct TestServiceClientImpl"));
|
|
585
|
+
assert!(generated.contains("pub trait TestServiceServer"));
|
|
586
|
+
assert!(generated.contains("pub struct TestServiceHandler"));
|
|
587
|
+
assert!(generated.contains("TestServiceServerStreamStream"));
|
|
588
|
+
assert!(generated.contains("TestServiceClientStreamStream"));
|
|
589
|
+
assert!(generated.contains("TestServiceBidiStream"));
|
|
590
|
+
|
|
591
|
+
let _ = fs::remove_dir_all(&root);
|
|
592
|
+
}
|
|
593
|
+
}
|
package/srpc/lib.rs
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
//! - **Wire-compatible** with the Go and TypeScript implementations
|
|
10
10
|
//! - **Streaming support** for all RPC patterns
|
|
11
11
|
//! - **Transport agnostic** - works with TCP, WebSocket, or any AsyncRead/AsyncWrite
|
|
12
|
-
//! - **Code generation** via
|
|
12
|
+
//! - **Code generation** via the optional `build` feature
|
|
13
13
|
//!
|
|
14
14
|
//! # Quick Start
|
|
15
15
|
//!
|
|
@@ -73,6 +73,9 @@ pub mod stream;
|
|
|
73
73
|
pub mod testing;
|
|
74
74
|
pub mod transport;
|
|
75
75
|
|
|
76
|
+
#[cfg(feature = "build")]
|
|
77
|
+
pub mod build;
|
|
78
|
+
|
|
76
79
|
// Re-exports for convenience.
|
|
77
80
|
pub use client::{BoxClient, Client, OpenStream, SrpcClient};
|
|
78
81
|
pub use codec::{PacketCodec, MAX_MESSAGE_SIZE};
|
package/srpc/packet-rw.go
CHANGED
|
@@ -5,14 +5,13 @@ import (
|
|
|
5
5
|
"context"
|
|
6
6
|
"encoding/binary"
|
|
7
7
|
"io"
|
|
8
|
-
"math"
|
|
9
8
|
"sync"
|
|
10
9
|
|
|
11
10
|
"github.com/pkg/errors"
|
|
12
11
|
)
|
|
13
12
|
|
|
14
13
|
// maxMessageSize is the max message size in bytes
|
|
15
|
-
|
|
14
|
+
const maxMessageSize = 10_000_000
|
|
16
15
|
|
|
17
16
|
// PacketReadWriter reads and writes packets from a io.ReadWriter.
|
|
18
17
|
// Uses a LittleEndian uint32 length prefix.
|
|
@@ -43,10 +42,8 @@ func (r *PacketReadWriter) WritePacket(p *Packet) error {
|
|
|
43
42
|
defer r.writeMtx.Unlock()
|
|
44
43
|
|
|
45
44
|
msgSize := p.SizeVT()
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if uint64(msgSize) > uint64(math.MaxUint32) {
|
|
49
|
-
return errors.New("message size exceeds maximum uint32 value")
|
|
45
|
+
if msgSize < 0 || msgSize > maxMessageSize {
|
|
46
|
+
return errors.Errorf("message size %v greater than maximum %v", msgSize, maxMessageSize)
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
data := make([]byte, 4+msgSize)
|