starpc 0.4.7 → 0.5.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.
Files changed (68) hide show
  1. package/Makefile +1 -0
  2. package/README.md +20 -10
  3. package/dist/echo/client-test.d.ts +1 -0
  4. package/dist/echo/client-test.js +20 -18
  5. package/dist/echo/echo.pb.d.ts +165 -12
  6. package/dist/echo/echo.pb.js +61 -17
  7. package/dist/echo/server.d.ts +8 -4
  8. package/dist/echo/server.js +29 -37
  9. package/dist/rpcstream/rpcstream.d.ts +6 -6
  10. package/dist/rpcstream/rpcstream.js +92 -51
  11. package/dist/rpcstream/rpcstream.pb.d.ts +46 -1
  12. package/dist/rpcstream/rpcstream.pb.js +157 -9
  13. package/dist/srpc/broadcast-channel.d.ts +2 -2
  14. package/dist/srpc/broadcast-channel.js +6 -6
  15. package/dist/srpc/client.d.ts +3 -4
  16. package/dist/srpc/client.js +12 -46
  17. package/dist/srpc/common-rpc.d.ts +3 -2
  18. package/dist/srpc/common-rpc.js +12 -0
  19. package/dist/srpc/definition.d.ts +3 -3
  20. package/dist/srpc/handler.d.ts +2 -3
  21. package/dist/srpc/handler.js +5 -22
  22. package/dist/srpc/index.d.ts +2 -2
  23. package/dist/srpc/index.js +2 -2
  24. package/dist/srpc/packet.js +0 -32
  25. package/dist/srpc/pushable.d.ts +2 -0
  26. package/dist/srpc/pushable.js +13 -0
  27. package/dist/srpc/rpcproto.pb.d.ts +13 -6
  28. package/dist/srpc/rpcproto.pb.js +95 -10
  29. package/dist/srpc/server.d.ts +1 -0
  30. package/dist/srpc/server.js +7 -0
  31. package/dist/srpc/ts-proto-rpc.d.ts +3 -4
  32. package/e2e/e2e.ts +4 -3
  33. package/e2e/e2e_test.go +35 -7
  34. package/echo/client-test.ts +23 -18
  35. package/echo/echo.pb.go +33 -20
  36. package/echo/echo.pb.ts +90 -34
  37. package/echo/echo.proto +4 -0
  38. package/echo/echo_srpc.pb.go +77 -0
  39. package/echo/server.go +18 -0
  40. package/echo/server.ts +47 -41
  41. package/integration/integration.go +1 -2
  42. package/integration/integration.ts +5 -1
  43. package/integration/integration_srpc.pb.go +139 -0
  44. package/package.json +13 -11
  45. package/patches/{ts-poet+4.13.0.patch → ts-poet+4.14.0.patch} +1 -1
  46. package/patches/ts-proto+1.115.5.patch +1339 -0
  47. package/srpc/broadcast-channel.ts +8 -8
  48. package/srpc/client.ts +16 -50
  49. package/srpc/common-rpc.ts +14 -2
  50. package/srpc/definition.ts +3 -3
  51. package/srpc/handler.ts +17 -34
  52. package/srpc/index.ts +3 -3
  53. package/srpc/muxed-conn.go +2 -2
  54. package/srpc/packet-rw.go +4 -6
  55. package/srpc/packet.ts +0 -33
  56. package/srpc/pushable.ts +17 -0
  57. package/srpc/rpcproto.pb.ts +122 -12
  58. package/srpc/server-pipe.go +2 -2
  59. package/srpc/server.go +2 -2
  60. package/srpc/server.ts +8 -0
  61. package/srpc/ts-proto-rpc.ts +4 -6
  62. package/srpc/websocket.go +2 -2
  63. package/dist/echo/sever.d.ts +0 -0
  64. package/dist/echo/sever.js +0 -1
  65. package/dist/srpc/observable-source.d.ts +0 -9
  66. package/dist/srpc/observable-source.js +0 -25
  67. package/echo/sever.ts +0 -0
  68. package/srpc/observable-source.ts +0 -40
package/echo/server.ts CHANGED
@@ -1,57 +1,63 @@
1
- import { Observable, from as observableFrom } from 'rxjs'
2
1
  import { Echoer, EchoMsg } from './echo.pb.js'
3
2
  import { pushable, Pushable } from 'it-pushable'
3
+ import first from 'it-first'
4
+ import { Server } from '../srpc/server.js'
5
+ import { writeToPushable } from '../srpc/pushable.js'
6
+ import { RpcStreamPacket } from '../rpcstream/rpcstream.pb.js'
7
+ import { handleRpcStream } from '../rpcstream/rpcstream.js'
4
8
 
5
9
  // EchoServer implements the Echoer server.
6
10
  export class EchoerServer implements Echoer {
11
+ // proxyServer is the server used for RpcStream requests.
12
+ private proxyServer?: Server
13
+
14
+ constructor(proxyServer?: Server) {
15
+ this.proxyServer = proxyServer
16
+ }
17
+
7
18
  public async Echo(request: EchoMsg): Promise<EchoMsg> {
8
19
  return request
9
20
  }
10
21
 
11
- public EchoServerStream(request: EchoMsg): Observable<EchoMsg> {
12
- // send 5 responses, with a 200ms delay for each
13
- return observableFrom(
14
- (async function* response(): AsyncIterable<EchoMsg> {
15
- for (let i = 0; i < 5; i++) {
16
- yield request
17
- await new Promise((resolve) => setTimeout(resolve, 200))
18
- }
19
- })()
20
- )
22
+ public async *EchoServerStream(request: EchoMsg): AsyncIterable<EchoMsg> {
23
+ for (let i = 0; i < 5; i++) {
24
+ yield request
25
+ await new Promise((resolve) => setTimeout(resolve, 200))
26
+ }
21
27
  }
22
28
 
23
- public EchoClientStream(request: Observable<EchoMsg>): Promise<EchoMsg> {
24
- return new Promise<EchoMsg>((resolve, reject) => {
25
- request.subscribe({
26
- next(msg) {
27
- resolve(msg)
28
- },
29
- error(err: any) {
30
- reject(err)
31
- },
32
- complete() {
33
- reject(new Error('none received'))
34
- },
35
- })
36
- })
29
+ public async EchoClientStream(
30
+ request: AsyncIterable<EchoMsg>
31
+ ): Promise<EchoMsg> {
32
+ // return the first message sent by the client.
33
+ const message = await first(request)
34
+ if (!message) {
35
+ throw new Error('received no messages')
36
+ }
37
+ return message
37
38
  }
38
39
 
39
- public EchoBidiStream(request: Observable<EchoMsg>): Observable<EchoMsg> {
40
+ public EchoBidiStream(
41
+ request: AsyncIterable<EchoMsg>
42
+ ): AsyncIterable<EchoMsg> {
40
43
  // build result observable
41
- const pushResponse: Pushable<EchoMsg> = pushable({ objectMode: true })
42
- const response = observableFrom(pushResponse)
43
- pushResponse.push({ body: 'hello from server' })
44
- request.subscribe({
45
- next(msg) {
46
- pushResponse.push(msg)
47
- },
48
- error(err) {
49
- pushResponse.throw(err)
50
- },
51
- complete() {
52
- pushResponse.end()
53
- },
54
- })
55
- return response
44
+ const result: Pushable<EchoMsg> = pushable({ objectMode: true })
45
+ result.push({ body: 'hello from server' })
46
+ writeToPushable(request, result)
47
+ return result
48
+ }
49
+
50
+ public RpcStream(
51
+ request: AsyncIterable<RpcStreamPacket>
52
+ ): AsyncIterable<RpcStreamPacket> {
53
+ return handleRpcStream(
54
+ request[Symbol.asyncIterator](),
55
+ async (_componentId: string): Promise<Server> => {
56
+ if (!this.proxyServer) {
57
+ throw new Error('rpc stream proxy server not set')
58
+ }
59
+ return this.proxyServer
60
+ }
61
+ )
56
62
  }
57
63
  }
@@ -11,8 +11,7 @@ import (
11
11
 
12
12
  func main() {
13
13
  mux := srpc.NewMux()
14
-
15
- echoServer := &echo.EchoServer{}
14
+ echoServer := echo.NewEchoServer(mux)
16
15
  if err := echo.SRPCRegisterEchoer(mux, echoServer); err != nil {
17
16
  logrus.Fatal(err.Error())
18
17
  }
@@ -1,5 +1,5 @@
1
1
  import { WebSocketConn } from '../srpc/websocket.js'
2
- import { runClientTest } from '../echo/client-test.js'
2
+ import { runClientTest, runRpcStreamTest } from '../echo/client-test.js'
3
3
  import WebSocket from 'isomorphic-ws'
4
4
 
5
5
  async function runRPC() {
@@ -9,7 +9,11 @@ async function runRPC() {
9
9
  const channel = new WebSocketConn(ws)
10
10
  const client = channel.buildClient()
11
11
 
12
+ console.log('Running client test via WebSocket..')
12
13
  await runClientTest(client)
14
+
15
+ console.log('Running RpcStream test via WebSocket..')
16
+ await runRpcStreamTest(client)
13
17
  }
14
18
 
15
19
  runRPC()
@@ -0,0 +1,139 @@
1
+ // Code generated by protoc-gen-srpc. DO NOT EDIT.
2
+ // protoc-gen-srpc version: v0.0.0-20220611014014-aa9dc5523865
3
+ // source: github.com/aperturerobotics/starpc/integration/integration.proto
4
+
5
+ package main
6
+
7
+ import (
8
+ context "context"
9
+
10
+ rpcstream "github.com/aperturerobotics/starpc/rpcstream"
11
+ srpc "github.com/aperturerobotics/starpc/srpc"
12
+ )
13
+
14
+ type SRPCIntegrationServiceClient interface {
15
+ SRPCClient() srpc.Client
16
+
17
+ RpcStream(ctx context.Context) (SRPCIntegrationService_RpcStreamClient, error)
18
+ }
19
+
20
+ type srpcIntegrationServiceClient struct {
21
+ cc srpc.Client
22
+ }
23
+
24
+ func NewSRPCIntegrationServiceClient(cc srpc.Client) SRPCIntegrationServiceClient {
25
+ return &srpcIntegrationServiceClient{cc}
26
+ }
27
+
28
+ func (c *srpcIntegrationServiceClient) SRPCClient() srpc.Client { return c.cc }
29
+
30
+ func (c *srpcIntegrationServiceClient) RpcStream(ctx context.Context) (SRPCIntegrationService_RpcStreamClient, error) {
31
+ stream, err := c.cc.NewStream(ctx, "main.IntegrationService", "RpcStream", nil)
32
+ if err != nil {
33
+ return nil, err
34
+ }
35
+ strm := &srpcIntegrationService_RpcStreamClient{stream}
36
+ return strm, nil
37
+ }
38
+
39
+ type SRPCIntegrationService_RpcStreamClient interface {
40
+ srpc.Stream
41
+ Send(*rpcstream.RpcStreamPacket) error
42
+ Recv() (*rpcstream.RpcStreamPacket, error)
43
+ RecvTo(*rpcstream.RpcStreamPacket) error
44
+ }
45
+
46
+ type srpcIntegrationService_RpcStreamClient struct {
47
+ srpc.Stream
48
+ }
49
+
50
+ func (x *srpcIntegrationService_RpcStreamClient) Send(m *rpcstream.RpcStreamPacket) error {
51
+ return x.MsgSend(m)
52
+ }
53
+
54
+ func (x *srpcIntegrationService_RpcStreamClient) Recv() (*rpcstream.RpcStreamPacket, error) {
55
+ m := new(rpcstream.RpcStreamPacket)
56
+ if err := x.MsgRecv(m); err != nil {
57
+ return nil, err
58
+ }
59
+ return m, nil
60
+ }
61
+
62
+ func (x *srpcIntegrationService_RpcStreamClient) RecvTo(m *rpcstream.RpcStreamPacket) error {
63
+ return x.MsgRecv(m)
64
+ }
65
+
66
+ type SRPCIntegrationServiceServer interface {
67
+ RpcStream(SRPCIntegrationService_RpcStreamStream) error
68
+ }
69
+
70
+ type SRPCIntegrationServiceUnimplementedServer struct{}
71
+
72
+ func (s *SRPCIntegrationServiceUnimplementedServer) RpcStream(SRPCIntegrationService_RpcStreamStream) error {
73
+ return srpc.ErrUnimplemented
74
+ }
75
+
76
+ const SRPCIntegrationServiceServiceID = "main.IntegrationService"
77
+
78
+ type SRPCIntegrationServiceHandler struct {
79
+ impl SRPCIntegrationServiceServer
80
+ }
81
+
82
+ func (SRPCIntegrationServiceHandler) GetServiceID() string { return SRPCIntegrationServiceServiceID }
83
+
84
+ func (SRPCIntegrationServiceHandler) GetMethodIDs() []string {
85
+ return []string{
86
+ "RpcStream",
87
+ }
88
+ }
89
+
90
+ func (d *SRPCIntegrationServiceHandler) InvokeMethod(
91
+ serviceID, methodID string,
92
+ strm srpc.Stream,
93
+ ) (bool, error) {
94
+ if serviceID != "" && serviceID != d.GetServiceID() {
95
+ return false, nil
96
+ }
97
+
98
+ switch methodID {
99
+ case "RpcStream":
100
+ return true, d.InvokeMethod_RpcStream(d.impl, strm)
101
+ default:
102
+ return false, nil
103
+ }
104
+ }
105
+
106
+ func (SRPCIntegrationServiceHandler) InvokeMethod_RpcStream(impl SRPCIntegrationServiceServer, strm srpc.Stream) error {
107
+ clientStrm := &srpcIntegrationService_RpcStreamStream{strm}
108
+ return impl.RpcStream(clientStrm)
109
+ }
110
+
111
+ func SRPCRegisterIntegrationService(mux srpc.Mux, impl SRPCIntegrationServiceServer) error {
112
+ return mux.Register(&SRPCIntegrationServiceHandler{impl: impl})
113
+ }
114
+
115
+ type SRPCIntegrationService_RpcStreamStream interface {
116
+ srpc.Stream
117
+ Send(*rpcstream.RpcStreamPacket) error
118
+ Recv() (*rpcstream.RpcStreamPacket, error)
119
+ }
120
+
121
+ type srpcIntegrationService_RpcStreamStream struct {
122
+ srpc.Stream
123
+ }
124
+
125
+ func (x *srpcIntegrationService_RpcStreamStream) Send(m *rpcstream.RpcStreamPacket) error {
126
+ return x.MsgSend(m)
127
+ }
128
+
129
+ func (x *srpcIntegrationService_RpcStreamStream) Recv() (*rpcstream.RpcStreamPacket, error) {
130
+ m := new(rpcstream.RpcStreamPacket)
131
+ if err := x.MsgRecv(m); err != nil {
132
+ return nil, err
133
+ }
134
+ return m, nil
135
+ }
136
+
137
+ func (x *srpcIntegrationService_RpcStreamStream) RecvTo(m *rpcstream.RpcStreamPacket) error {
138
+ return x.MsgRecv(m)
139
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.4.7",
3
+ "version": "0.5.0",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -42,7 +42,9 @@
42
42
  "gen": "make genproto",
43
43
  "test": "npm run test:js && npm run test:go",
44
44
  "test:go": "make test",
45
- "test:js": "npm run build && cd e2e && esbuild e2e.ts --sourcemap --outfile=e2e.js --bundle --platform=node && node ./e2e.js",
45
+ "build:e2e": "npm run build && cd e2e && esbuild e2e.ts --sourcemap --outfile=e2e.js --bundle --platform=node",
46
+ "test:js": "npm run build:e2e && cd e2e && node ./e2e.js",
47
+ "debug:js": "npm run build:e2e && cd e2e && node --inspect --inspect-brk ./e2e.js",
46
48
  "test:integration": "make integration",
47
49
  "integration": "npm run test:integration",
48
50
  "lint": "npm run lint:go && npm run lint:js",
@@ -56,23 +58,24 @@
56
58
  "singleQuote": true
57
59
  },
58
60
  "devDependencies": {
59
- "@typescript-eslint/eslint-plugin": "^5.27.1",
60
- "@typescript-eslint/parser": "^5.27.1",
61
+ "@typescript-eslint/eslint-plugin": "^5.30.0",
62
+ "@typescript-eslint/parser": "^5.30.0",
61
63
  "depcheck": "^1.4.3",
62
- "esbuild": "^0.14.46",
64
+ "esbuild": "^0.14.47",
63
65
  "eslint": "^8.18.0",
64
66
  "eslint-config-prettier": "^8.5.0",
65
67
  "prettier": "^2.7.1",
66
68
  "rimraf": "^3.0.2",
67
- "ts-proto": "^1.115.4",
69
+ "ts-proto": "^1.115.5",
68
70
  "typescript": "^4.7.4"
69
71
  },
70
72
  "dependencies": {
71
- "@libp2p/interface-connection": "^2.0.0",
72
- "@libp2p/interface-stream-muxer": "^1.0.2",
73
- "@libp2p/mplex": "^3.0.0",
73
+ "@libp2p/interface-connection": "^2.1.1",
74
+ "@libp2p/interface-stream-muxer": "^2.0.1",
75
+ "@libp2p/mplex": "^4.0.0",
74
76
  "event-iterator": "^2.0.0",
75
- "isomorphic-ws": "^4.0.1",
77
+ "isomorphic-ws": "^5.0.0",
78
+ "it-first": "^1.0.7",
76
79
  "it-length-prefixed": "^7.0.1",
77
80
  "it-pipe": "^2.0.3",
78
81
  "it-pushable": "^3.0.0",
@@ -81,7 +84,6 @@
81
84
  "long": "^5.2.0",
82
85
  "patch-package": "^6.4.7",
83
86
  "protobufjs": "^6.11.3",
84
- "rxjs": "^7.5.5",
85
87
  "ws": "^8.8.0"
86
88
  }
87
89
  }
@@ -1,5 +1,5 @@
1
1
  diff --git a/node_modules/ts-poet/build/Import.js b/node_modules/ts-poet/build/Import.js
2
- index c43569e..a77a1b1 100644
2
+ index fe1f6d2..280df8f 100644
3
3
  --- a/node_modules/ts-poet/build/Import.js
4
4
  +++ b/node_modules/ts-poet/build/Import.js
5
5
  @@ -293,6 +293,14 @@ function emitImports(imports, ourModulePath, importMappings) {