starpc 0.40.0 → 0.40.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 +76 -88
- package/dist/srpc/duplex-message-stream.d.ts +1 -0
- package/dist/srpc/duplex-message-stream.js +12 -9
- package/dist/srpc/stream.js +1 -1
- package/package.json +1 -1
- package/srpc/duplex-message-stream.ts +16 -11
- package/srpc/stream.ts +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# starpc
|
|
2
2
|
|
|
3
3
|
[![GoDoc Widget]][GoDoc] [![Go Report Card Widget]][Go Report Card]
|
|
4
4
|
|
|
5
|
-
>
|
|
5
|
+
> Streaming Protobuf RPC with bidirectional streaming over any multiplexed transport.
|
|
6
6
|
|
|
7
7
|
[GoDoc]: https://godoc.org/github.com/aperturerobotics/starpc
|
|
8
8
|
[GoDoc Widget]: https://godoc.org/github.com/aperturerobotics/starpc?status.svg
|
|
@@ -18,20 +18,22 @@
|
|
|
18
18
|
- [Protobuf Definition](#protobuf)
|
|
19
19
|
- [Go Implementation](#go)
|
|
20
20
|
- [TypeScript Implementation](#typescript)
|
|
21
|
+
- [Debugging](#debugging)
|
|
21
22
|
- [Development Setup](#development-setup)
|
|
22
23
|
- [Support](#support)
|
|
23
24
|
|
|
24
25
|
## Features
|
|
25
26
|
|
|
26
|
-
- Full [Proto3 services]
|
|
27
|
-
- Bidirectional streaming
|
|
28
|
-
- Built on libp2p streams with
|
|
29
|
-
- Efficient
|
|
30
|
-
- Zero-reflection Go
|
|
31
|
-
- TypeScript
|
|
32
|
-
- Sub-
|
|
27
|
+
- Full [Proto3 services] support for TypeScript and Go
|
|
28
|
+
- Bidirectional streaming in browsers via WebSocket or WebRTC
|
|
29
|
+
- Built on libp2p streams with [@chainsafe/libp2p-yamux]
|
|
30
|
+
- Efficient multiplexing of RPCs over single connections
|
|
31
|
+
- Zero-reflection Go via [protobuf-go-lite]
|
|
32
|
+
- Lightweight TypeScript via [protobuf-es-lite]
|
|
33
|
+
- Sub-stream support via [rpcstream]
|
|
33
34
|
|
|
34
35
|
[Proto3 services]: https://developers.google.com/protocol-buffers/docs/proto3#services
|
|
36
|
+
[@chainsafe/libp2p-yamux]: https://github.com/ChainSafe/js-libp2p-yamux
|
|
35
37
|
[protobuf-go-lite]: https://github.com/aperturerobotics/protobuf-go-lite
|
|
36
38
|
[protobuf-es-lite]: https://github.com/aperturerobotics/protobuf-es-lite
|
|
37
39
|
[rpcstream]: ./rpcstream
|
|
@@ -119,118 +121,115 @@ if out.GetBody() != bodyTxt {
|
|
|
119
121
|
}
|
|
120
122
|
```
|
|
121
123
|
|
|
122
|
-
[e2e test]: ./e2e/e2e_test.go
|
|
123
|
-
|
|
124
124
|
### TypeScript
|
|
125
125
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
For an example of Go <-> TypeScript interop, see the [integration] test. For an
|
|
129
|
-
example of TypeScript <-> TypeScript interop, see the [e2e] test.
|
|
126
|
+
For Go <-> TypeScript interop, see the [integration] test.
|
|
127
|
+
For TypeScript <-> TypeScript, see the [e2e] test.
|
|
130
128
|
|
|
131
129
|
[e2e]: ./e2e/e2e.ts
|
|
132
130
|
[integration]: ./integration/integration.ts
|
|
133
131
|
|
|
134
|
-
Supports any AsyncIterable communication channel.
|
|
135
|
-
|
|
136
132
|
#### WebSocket Example
|
|
137
133
|
|
|
138
|
-
|
|
134
|
+
Connect to a WebSocket server:
|
|
139
135
|
|
|
140
136
|
```typescript
|
|
141
|
-
import { WebSocketConn } from '
|
|
142
|
-
import { EchoerClient } from '
|
|
137
|
+
import { WebSocketConn } from 'starpc'
|
|
138
|
+
import { EchoerClient } from './echo/index.js'
|
|
143
139
|
|
|
144
|
-
const ws = new WebSocket('ws://localhost:
|
|
145
|
-
const
|
|
146
|
-
const client =
|
|
147
|
-
const
|
|
140
|
+
const ws = new WebSocket('ws://localhost:8080/api')
|
|
141
|
+
const conn = new WebSocketConn(ws)
|
|
142
|
+
const client = conn.buildClient()
|
|
143
|
+
const echoer = new EchoerClient(client)
|
|
148
144
|
|
|
149
|
-
const result = await
|
|
150
|
-
|
|
151
|
-
})
|
|
152
|
-
console.log('output', result.body)
|
|
145
|
+
const result = await echoer.Echo({ body: 'Hello world!' })
|
|
146
|
+
console.log('result:', result.body)
|
|
153
147
|
```
|
|
154
148
|
|
|
155
|
-
#### In-
|
|
149
|
+
#### In-Memory Example
|
|
156
150
|
|
|
157
|
-
|
|
151
|
+
Server and client with an in-memory pipe:
|
|
158
152
|
|
|
159
153
|
```typescript
|
|
160
154
|
import { pipe } from 'it-pipe'
|
|
161
|
-
import { createHandler, createMux, Server,
|
|
162
|
-
import { EchoerDefinition, EchoerServer
|
|
163
|
-
import { pushable } from 'it-pushable'
|
|
155
|
+
import { createHandler, createMux, Server, StreamConn } from 'starpc'
|
|
156
|
+
import { EchoerDefinition, EchoerServer } from './echo/index.js'
|
|
164
157
|
|
|
165
|
-
// Create
|
|
158
|
+
// Create server with registered handlers
|
|
166
159
|
const mux = createMux()
|
|
167
160
|
const echoer = new EchoerServer()
|
|
168
161
|
mux.register(createHandler(EchoerDefinition, echoer))
|
|
169
162
|
const server = new Server(mux.lookupMethod)
|
|
170
163
|
|
|
171
|
-
// Create
|
|
172
|
-
const clientConn = new
|
|
173
|
-
const serverConn = new
|
|
164
|
+
// Create client and server connections, pipe together
|
|
165
|
+
const clientConn = new StreamConn()
|
|
166
|
+
const serverConn = new StreamConn(server)
|
|
174
167
|
pipe(clientConn, serverConn, clientConn)
|
|
175
|
-
const client = new Client(clientConn.buildOpenStreamFunc())
|
|
176
168
|
|
|
177
|
-
//
|
|
169
|
+
// Build client and make RPC calls
|
|
170
|
+
const client = clientConn.buildClient()
|
|
171
|
+
const echoerClient = new EchoerClient(client)
|
|
178
172
|
|
|
179
|
-
//
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
console.log('
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
console.log('Calling EchoServerStream: server -> client...')
|
|
196
|
-
const serverStream = demoServiceClient.EchoServerStream({
|
|
197
|
-
body: 'Hello world from server to client streaming request.',
|
|
198
|
-
})
|
|
199
|
-
for await (const msg of serverStream) {
|
|
200
|
-
console.log('server: output', msg.body)
|
|
173
|
+
// Unary call
|
|
174
|
+
const result = await echoerClient.Echo({ body: 'Hello world!' })
|
|
175
|
+
console.log('result:', result.body)
|
|
176
|
+
|
|
177
|
+
// Client streaming
|
|
178
|
+
import { pushable } from 'it-pushable'
|
|
179
|
+
const stream = pushable({ objectMode: true })
|
|
180
|
+
stream.push({ body: 'Message 1' })
|
|
181
|
+
stream.push({ body: 'Message 2' })
|
|
182
|
+
stream.end()
|
|
183
|
+
const response = await echoerClient.EchoClientStream(stream)
|
|
184
|
+
console.log('response:', response.body)
|
|
185
|
+
|
|
186
|
+
// Server streaming
|
|
187
|
+
for await (const msg of echoerClient.EchoServerStream({ body: 'Hello' })) {
|
|
188
|
+
console.log('server msg:', msg.body)
|
|
201
189
|
}
|
|
202
190
|
```
|
|
203
191
|
|
|
192
|
+
## Debugging
|
|
193
|
+
|
|
194
|
+
Enable debug logging in TypeScript using the `DEBUG` environment variable:
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
# Enable all starpc logs
|
|
198
|
+
DEBUG=starpc:* node app.js
|
|
199
|
+
|
|
200
|
+
# Enable specific component logs
|
|
201
|
+
DEBUG=starpc:stream-conn node app.js
|
|
202
|
+
```
|
|
203
|
+
|
|
204
204
|
## Attribution
|
|
205
205
|
|
|
206
206
|
`protoc-gen-go-starpc` is a heavily modified version of `protoc-gen-go-drpc`.
|
|
207
|
-
|
|
208
|
-
Be sure to check out [drpc] as well: it's compatible with grpc, twirp, and more.
|
|
207
|
+
Check out [drpc] as well - it's compatible with grpc, twirp, and more.
|
|
209
208
|
|
|
210
209
|
[drpc]: https://github.com/storj/drpc
|
|
211
210
|
|
|
212
|
-
Uses [vtprotobuf] to generate
|
|
211
|
+
Uses [vtprotobuf] to generate Protobuf marshal/unmarshal code for Go.
|
|
213
212
|
|
|
214
213
|
[vtprotobuf]: https://github.com/planetscale/vtprotobuf
|
|
215
214
|
|
|
216
|
-
|
|
215
|
+
`protoc-gen-es-starpc` is a modified version of `protoc-gen-connect-es`.
|
|
216
|
+
Uses [protobuf-es-lite] (fork of [protobuf-es]) for TypeScript.
|
|
217
217
|
|
|
218
218
|
[protobuf-es]: https://github.com/bufbuild/protobuf-es
|
|
219
|
-
[protobuf-es-lite]: https://github.com/aperturerobotics/protobuf-es-lite
|
|
220
|
-
|
|
221
|
-
`protoc-gen-es-starpc` is a heavily modified version of `protoc-gen-connect-es`.
|
|
222
219
|
|
|
223
220
|
## Development Setup
|
|
224
221
|
|
|
225
|
-
### MacOS
|
|
222
|
+
### MacOS
|
|
223
|
+
|
|
224
|
+
Install required packages:
|
|
226
225
|
|
|
227
|
-
1. Install required packages:
|
|
228
226
|
```bash
|
|
229
227
|
brew install bash make coreutils gnu-sed findutils protobuf
|
|
230
228
|
brew link --overwrite protobuf
|
|
231
229
|
```
|
|
232
230
|
|
|
233
|
-
|
|
231
|
+
Add to your shell rc file (.bashrc, .zshrc):
|
|
232
|
+
|
|
234
233
|
```bash
|
|
235
234
|
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
|
|
236
235
|
export PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"
|
|
@@ -238,23 +237,12 @@ export PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
|
|
|
238
237
|
export PATH="/opt/homebrew/opt/make/libexec/gnubin:$PATH"
|
|
239
238
|
```
|
|
240
239
|
|
|
241
|
-
## Attribution
|
|
242
|
-
|
|
243
|
-
- `protoc-gen-go-starpc`: Modified version of `protoc-gen-go-drpc`
|
|
244
|
-
- `protoc-gen-es-starpc`: Modified version of `protoc-gen-connect-es`
|
|
245
|
-
- Uses [vtprotobuf] for Go Protobuf marshaling
|
|
246
|
-
- Uses [protobuf-es-lite] for TypeScript Protobuf interfaces
|
|
247
|
-
|
|
248
|
-
[vtprotobuf]: https://github.com/planetscale/vtprotobuf
|
|
249
|
-
|
|
250
240
|
## Support
|
|
251
241
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
- [
|
|
255
|
-
- [Join our Discord][Join Discord]
|
|
256
|
-
- [Matrix Chat][Matrix Chat]
|
|
242
|
+
- [GitHub Issues][issues]
|
|
243
|
+
- [Discord][discord]
|
|
244
|
+
- [Matrix][matrix]
|
|
257
245
|
|
|
258
|
-
[
|
|
259
|
-
[
|
|
260
|
-
[
|
|
246
|
+
[issues]: https://github.com/aperturerobotics/starpc/issues/new
|
|
247
|
+
[discord]: https://discord.gg/KJutMESRsT
|
|
248
|
+
[matrix]: https://matrix.to/#/#aperturerobotics:matrix.org
|
|
@@ -10,6 +10,7 @@ export interface DuplexMessageStreamInit {
|
|
|
10
10
|
}
|
|
11
11
|
export declare class DuplexMessageStream extends AbstractMessageStream {
|
|
12
12
|
private readonly _outgoing;
|
|
13
|
+
private readonly _sink;
|
|
13
14
|
constructor(init?: DuplexMessageStreamInit);
|
|
14
15
|
get source(): AsyncIterable<Uint8Array | Uint8ArrayList>;
|
|
15
16
|
get sink(): (source: Source<Uint8Array | Uint8ArrayList>) => Promise<void>;
|
|
@@ -9,6 +9,8 @@ import { pushable } from 'it-pushable';
|
|
|
9
9
|
export class DuplexMessageStream extends AbstractMessageStream {
|
|
10
10
|
// _outgoing is a pushable that collects data to be sent out.
|
|
11
11
|
_outgoing;
|
|
12
|
+
// _sink is the bound sink function
|
|
13
|
+
_sink;
|
|
12
14
|
constructor(init) {
|
|
13
15
|
// Create the MessageStreamInit required by AbstractMessageStream
|
|
14
16
|
const streamInit = {
|
|
@@ -19,15 +21,7 @@ export class DuplexMessageStream extends AbstractMessageStream {
|
|
|
19
21
|
};
|
|
20
22
|
super(streamInit);
|
|
21
23
|
this._outgoing = pushable();
|
|
22
|
-
|
|
23
|
-
// source returns an async iterable that yields data to be sent to the remote.
|
|
24
|
-
get source() {
|
|
25
|
-
return this._outgoing;
|
|
26
|
-
}
|
|
27
|
-
// sink consumes data from the remote and feeds it into the stream.
|
|
28
|
-
// This is the receiving end of the duplex.
|
|
29
|
-
get sink() {
|
|
30
|
-
return async (source) => {
|
|
24
|
+
this._sink = async (source) => {
|
|
31
25
|
try {
|
|
32
26
|
for await (const data of source) {
|
|
33
27
|
if (this.status === 'closed' ||
|
|
@@ -46,6 +40,15 @@ export class DuplexMessageStream extends AbstractMessageStream {
|
|
|
46
40
|
}
|
|
47
41
|
};
|
|
48
42
|
}
|
|
43
|
+
// source returns an async iterable that yields data to be sent to the remote.
|
|
44
|
+
get source() {
|
|
45
|
+
return this._outgoing;
|
|
46
|
+
}
|
|
47
|
+
// sink consumes data from the remote and feeds it into the stream.
|
|
48
|
+
// This is the receiving end of the duplex.
|
|
49
|
+
get sink() {
|
|
50
|
+
return this._sink;
|
|
51
|
+
}
|
|
49
52
|
// sendData implements AbstractMessageStream.sendData
|
|
50
53
|
// Called by the parent class when processing the write queue.
|
|
51
54
|
sendData(data) {
|
package/dist/srpc/stream.js
CHANGED
package/package.json
CHANGED
|
@@ -29,6 +29,10 @@ export interface DuplexMessageStreamInit {
|
|
|
29
29
|
export class DuplexMessageStream extends AbstractMessageStream {
|
|
30
30
|
// _outgoing is a pushable that collects data to be sent out.
|
|
31
31
|
private readonly _outgoing: Pushable<Uint8Array | Uint8ArrayList>
|
|
32
|
+
// _sink is the bound sink function
|
|
33
|
+
private readonly _sink: (
|
|
34
|
+
source: Source<Uint8Array | Uint8ArrayList>,
|
|
35
|
+
) => Promise<void>
|
|
32
36
|
|
|
33
37
|
constructor(init?: DuplexMessageStreamInit) {
|
|
34
38
|
// Create the MessageStreamInit required by AbstractMessageStream
|
|
@@ -40,17 +44,7 @@ export class DuplexMessageStream extends AbstractMessageStream {
|
|
|
40
44
|
}
|
|
41
45
|
super(streamInit)
|
|
42
46
|
this._outgoing = pushable<Uint8Array | Uint8ArrayList>()
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
// source returns an async iterable that yields data to be sent to the remote.
|
|
46
|
-
get source(): AsyncIterable<Uint8Array | Uint8ArrayList> {
|
|
47
|
-
return this._outgoing
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// sink consumes data from the remote and feeds it into the stream.
|
|
51
|
-
// This is the receiving end of the duplex.
|
|
52
|
-
get sink(): (source: Source<Uint8Array | Uint8ArrayList>) => Promise<void> {
|
|
53
|
-
return async (
|
|
47
|
+
this._sink = async (
|
|
54
48
|
source: Source<Uint8Array | Uint8ArrayList>,
|
|
55
49
|
): Promise<void> => {
|
|
56
50
|
try {
|
|
@@ -73,6 +67,17 @@ export class DuplexMessageStream extends AbstractMessageStream {
|
|
|
73
67
|
}
|
|
74
68
|
}
|
|
75
69
|
|
|
70
|
+
// source returns an async iterable that yields data to be sent to the remote.
|
|
71
|
+
get source(): AsyncIterable<Uint8Array | Uint8ArrayList> {
|
|
72
|
+
return this._outgoing
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// sink consumes data from the remote and feeds it into the stream.
|
|
76
|
+
// This is the receiving end of the duplex.
|
|
77
|
+
get sink(): (source: Source<Uint8Array | Uint8ArrayList>) => Promise<void> {
|
|
78
|
+
return this._sink
|
|
79
|
+
}
|
|
80
|
+
|
|
76
81
|
// sendData implements AbstractMessageStream.sendData
|
|
77
82
|
// Called by the parent class when processing the write queue.
|
|
78
83
|
sendData(data: Uint8ArrayList): SendResult {
|
package/srpc/stream.ts
CHANGED