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 CHANGED
@@ -1,8 +1,8 @@
1
- # Stream RPC (starpc)
1
+ # starpc
2
2
 
3
3
  [![GoDoc Widget]][GoDoc] [![Go Report Card Widget]][Go Report Card]
4
4
 
5
- > A high-performance Protobuf 3 RPC framework supporting bidirectional streaming over any multiplexer.
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] implementation for both TypeScript and Go
27
- - Bidirectional streaming support in web browsers
28
- - Built on libp2p streams with `@chainsafe/libp2p-yamux`
29
- - Efficient RPC multiplexing over single connections
30
- - Zero-reflection Go code via [protobuf-go-lite]
31
- - TypeScript interfaces via [protobuf-es-lite]
32
- - Sub-streams support through [rpcstream]
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
- See the ts-proto README to generate the TypeScript for your protobufs.
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
- This examples demonstrates connecting to a WebSocket server:
134
+ Connect to a WebSocket server:
139
135
 
140
136
  ```typescript
141
- import { WebSocketConn } from 'srpc'
142
- import { EchoerClient } from 'srpc/echo'
137
+ import { WebSocketConn } from 'starpc'
138
+ import { EchoerClient } from './echo/index.js'
143
139
 
144
- const ws = new WebSocket('ws://localhost:1347/demo')
145
- const channel = new WebSocketConn(ws)
146
- const client = channel.buildClient()
147
- const demoServiceClient = new EchoerClient(client)
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 demoServiceClient.Echo({
150
- body: "Hello world!"
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-memory Demo with TypeScript Server and Client
149
+ #### In-Memory Example
156
150
 
157
- This example demonstrates both the server and client with an in-memory pipe:
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, Client, Conn } from 'srpc'
162
- import { EchoerDefinition, EchoerServer, runClientTest } from 'srpc/echo'
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 the server and register the handlers.
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 the client connection to the server with an in-memory pipe.
172
- const clientConn = new Conn()
173
- const serverConn = new Conn(server)
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
- // Examples of different types of RPC calls:
169
+ // Build client and make RPC calls
170
+ const client = clientConn.buildClient()
171
+ const echoerClient = new EchoerClient(client)
178
172
 
179
- // One-shot request/response (unary):
180
- console.log('Calling Echo: unary call...')
181
- let result = await demoServiceClient.Echo({
182
- body: 'Hello world!',
183
- })
184
- console.log('success: output', result.body)
185
-
186
- // Streaming from client->server with a single server response:
187
- const clientRequestStream = pushable<EchoMsg>({objectMode: true})
188
- clientRequestStream.push({body: 'Hello world from streaming request.'})
189
- clientRequestStream.end()
190
- console.log('Calling EchoClientStream: client -> server...')
191
- result = await demoServiceClient.EchoClientStream(clientRequestStream)
192
- console.log('success: output', result.body)
193
-
194
- // Streaming from server -> client with a single client message.
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 Go Protobuf marshal / unmarshal code.
211
+ Uses [vtprotobuf] to generate Protobuf marshal/unmarshal code for Go.
213
212
 
214
213
  [vtprotobuf]: https://github.com/planetscale/vtprotobuf
215
214
 
216
- Uses [protobuf-es-lite] (fork of [protobuf-es]) to generate TypeScript Protobuf marshal / unmarshal code.
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 Requirements
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
- 2. Add to your .bashrc or .zshrc:
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
- Need help? We're here:
253
-
254
- - [File a GitHub Issue][GitHub issue]
255
- - [Join our Discord][Join Discord]
256
- - [Matrix Chat][Matrix Chat]
242
+ - [GitHub Issues][issues]
243
+ - [Discord][discord]
244
+ - [Matrix][matrix]
257
245
 
258
- [GitHub issue]: https://github.com/aperturerobotics/starpc/issues/new
259
- [Join Discord]: https://discord.gg/KJutMESRsT
260
- [Matrix Chat]: https://matrix.to/#/#aperturerobotics:matrix.org
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) {
@@ -14,7 +14,7 @@ export function streamToPacketStream(stream) {
14
14
  }
15
15
  await stream.close();
16
16
  }
17
- catch (err) {
17
+ catch {
18
18
  await stream
19
19
  .close({ signal: AbortSignal.timeout(1000) })
20
20
  .catch(() => { });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.40.0",
3
+ "version": "0.40.1",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -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
@@ -43,7 +43,7 @@ export function streamToPacketStream(stream: Stream): PacketStream {
43
43
  stream.send(data)
44
44
  }
45
45
  await stream.close()
46
- } catch (err: unknown) {
46
+ } catch {
47
47
  await stream
48
48
  .close({ signal: AbortSignal.timeout(1000) })
49
49
  .catch(() => {})