starpc 0.17.1 → 0.18.0

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.
@@ -4,7 +4,7 @@ import { Pushable } from 'it-pushable';
4
4
  import { Source, Sink } from 'it-stream-types';
5
5
  import { Uint8ArrayList } from 'uint8arraylist';
6
6
  export type RpcStreamCaller = (request: AsyncIterable<RpcStreamPacket>) => AsyncIterable<RpcStreamPacket>;
7
- export declare function openRpcStream(componentId: string, caller: RpcStreamCaller): Promise<Stream>;
7
+ export declare function openRpcStream(componentId: string, caller: RpcStreamCaller, waitAck?: boolean): Promise<Stream>;
8
8
  export declare function buildRpcStreamOpenStream(componentId: string, caller: RpcStreamCaller): OpenStreamFunc;
9
9
  export type RpcStreamHandler = (stream: Stream) => void;
10
10
  export type RpcStreamGetter = (componentId: string) => Promise<RpcStreamHandler | null>;
@@ -1,7 +1,7 @@
1
1
  import { pushable } from 'it-pushable';
2
2
  // openRpcStream attempts to open a stream over a RPC call.
3
- // waits for the remote to ack the stream before returning.
4
- export async function openRpcStream(componentId, caller) {
3
+ // if waitAck is set, waits for the remote to ack the stream before returning.
4
+ export async function openRpcStream(componentId, caller, waitAck) {
5
5
  const packetSink = pushable({ objectMode: true });
6
6
  const packetSource = caller(packetSink);
7
7
  // write the component id
@@ -11,21 +11,24 @@ export async function openRpcStream(componentId, caller) {
11
11
  init: { componentId },
12
12
  },
13
13
  });
14
- // wait for ack
14
+ // construct packet stream
15
15
  const packetIt = packetSource[Symbol.asyncIterator]();
16
- const ackPacketIt = await packetIt.next();
17
- if (ackPacketIt.done) {
18
- throw new Error(`rpcstream: closed before ack packet`);
19
- }
20
- const ackPacket = ackPacketIt.value;
21
- const ackBody = ackPacket?.body;
22
- if (!ackBody || ackBody.$case !== 'ack') {
23
- const msgType = ackBody?.$case || 'none';
24
- throw new Error(`rpcstream: expected ack packet but got ${msgType}`);
25
- }
26
- const errStr = ackBody.ack?.error;
27
- if (errStr) {
28
- throw new Error(`rpcstream: remote: ${errStr}`);
16
+ // wait for ack, if set.
17
+ if (waitAck) {
18
+ const ackPacketIt = await packetIt.next();
19
+ if (ackPacketIt.done) {
20
+ throw new Error(`rpcstream: closed before ack packet`);
21
+ }
22
+ const ackPacket = ackPacketIt.value;
23
+ const ackBody = ackPacket?.body;
24
+ if (!ackBody || ackBody.$case !== 'ack') {
25
+ const msgType = ackBody?.$case || 'none';
26
+ throw new Error(`rpcstream: expected ack packet but got ${msgType}`);
27
+ }
28
+ const errStr = ackBody.ack?.error;
29
+ if (errStr) {
30
+ throw new Error(`rpcstream: remote: ${errStr}`);
31
+ }
29
32
  }
30
33
  // build & return the data stream
31
34
  return new RpcStream(packetSink, packetIt); // packetSource)
@@ -136,10 +139,19 @@ export class RpcStream {
136
139
  }
137
140
  const value = msgIt.value;
138
141
  const body = value?.body;
139
- if (!body || body.$case !== 'data') {
142
+ if (!body) {
140
143
  continue;
141
144
  }
142
- yield* [body.data];
145
+ switch (body.$case) {
146
+ case 'ack':
147
+ if (body.ack?.error?.length) {
148
+ throw new Error(body.ack.error);
149
+ }
150
+ break;
151
+ case 'data':
152
+ yield* [body.data];
153
+ break;
154
+ }
143
155
  }
144
156
  })();
145
157
  }
package/e2e/e2e_test.go CHANGED
@@ -227,7 +227,7 @@ func TestE2E_RpcStream(t *testing.T) {
227
227
  RunE2E(t, func(client echo.SRPCEchoerClient) error {
228
228
  openStreamFn := rpcstream.NewRpcStreamOpenStream(func(ctx context.Context) (rpcstream.RpcStream, error) {
229
229
  return client.RpcStream(ctx)
230
- }, "test")
230
+ }, "test", false)
231
231
  proxiedClient := srpc.NewClient(openStreamFn)
232
232
  proxiedSvc := echo.NewSRPCEchoerClient(proxiedClient)
233
233
 
package/go.mod CHANGED
@@ -9,7 +9,7 @@ require (
9
9
  )
10
10
 
11
11
  require (
12
- github.com/aperturerobotics/util v0.0.0-20230105015752-3e6dd02b20c6
12
+ github.com/aperturerobotics/util v1.0.3-0.20230130061818-dab10b56858b
13
13
  github.com/libp2p/go-libp2p v0.24.2
14
14
  github.com/libp2p/go-yamux/v4 v4.0.1-0.20220919134236-1c09f2ab3ec1
15
15
  github.com/sirupsen/logrus v1.9.0
package/go.sum CHANGED
@@ -1,5 +1,5 @@
1
- github.com/aperturerobotics/util v0.0.0-20230105015752-3e6dd02b20c6 h1:YCg1Eyh5M5SvWCpJmdM62iInd0yfZXSGFld10GQIIPA=
2
- github.com/aperturerobotics/util v0.0.0-20230105015752-3e6dd02b20c6/go.mod h1:up2AYcp62UgmFVTm7QhM4USXAKGv73gpb5dHraKmzxQ=
1
+ github.com/aperturerobotics/util v1.0.3-0.20230130061818-dab10b56858b h1:E+riBVCoYAZL2ATHTlDZYZRZj/6FPb+UGHVTQdU03UU=
2
+ github.com/aperturerobotics/util v1.0.3-0.20230130061818-dab10b56858b/go.mod h1:gFLdglkzXYEWP/iYrOGeJEKzfjarlMDPrLNLgwDHC1o=
3
3
  github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4
4
  github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5
5
  github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.17.1",
3
+ "version": "0.18.0",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -71,7 +71,7 @@
71
71
  "prettier": "^2.8.3",
72
72
  "rimraf": "^4.1.0",
73
73
  "ts-proto": "^1.138.0",
74
- "typescript": "^4.9.4",
74
+ "typescript": "^4.9.5",
75
75
  "utf-8-validate": "^6.0.0"
76
76
  },
77
77
  "dependencies": {