starpc 0.0.1 → 0.1.2

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 (73) hide show
  1. package/LICENSE +19 -0
  2. package/Makefile +140 -0
  3. package/README.md +128 -26
  4. package/dist/echo/echo.d.ts +59 -0
  5. package/dist/echo/echo.js +85 -0
  6. package/dist/srpc/broadcast-channel.d.ts +16 -0
  7. package/dist/srpc/broadcast-channel.js +60 -0
  8. package/dist/srpc/client-rpc.d.ts +31 -0
  9. package/dist/srpc/client-rpc.js +176 -0
  10. package/dist/srpc/client.d.ts +12 -0
  11. package/dist/srpc/client.js +129 -0
  12. package/dist/srpc/conn.d.ts +14 -0
  13. package/dist/srpc/conn.js +38 -0
  14. package/dist/srpc/index.d.ts +3 -0
  15. package/dist/srpc/index.js +2 -0
  16. package/dist/srpc/packet.d.ts +9 -0
  17. package/dist/srpc/packet.js +89 -0
  18. package/dist/srpc/rpcproto.d.ts +194 -0
  19. package/dist/srpc/rpcproto.js +322 -0
  20. package/dist/srpc/stream.d.ts +5 -0
  21. package/dist/srpc/stream.js +1 -0
  22. package/dist/srpc/ts-proto-rpc.d.ts +7 -0
  23. package/dist/srpc/ts-proto-rpc.js +1 -0
  24. package/dist/srpc/websocket.d.ts +7 -0
  25. package/dist/srpc/websocket.js +18 -0
  26. package/e2e/e2e.go +1 -0
  27. package/e2e/e2e_test.go +158 -0
  28. package/echo/echo.go +1 -0
  29. package/echo/echo.pb.go +165 -0
  30. package/echo/echo.proto +19 -0
  31. package/echo/echo.ts +191 -0
  32. package/echo/echo_srpc.pb.go +333 -0
  33. package/echo/echo_vtproto.pb.go +271 -0
  34. package/echo/server.go +73 -0
  35. package/go.mod +50 -0
  36. package/go.sum +210 -0
  37. package/integration/integration.bash +25 -0
  38. package/integration/integration.go +30 -0
  39. package/integration/integration.ts +54 -0
  40. package/integration/tsconfig.json +11 -0
  41. package/package.json +77 -9
  42. package/patches/@libp2p+mplex+1.2.1.patch +22 -0
  43. package/srpc/broadcast-channel.ts +72 -0
  44. package/srpc/client-rpc.go +163 -0
  45. package/srpc/client-rpc.ts +197 -0
  46. package/srpc/client.go +96 -0
  47. package/srpc/client.ts +182 -0
  48. package/srpc/conn.go +7 -0
  49. package/srpc/conn.ts +49 -0
  50. package/srpc/errors.go +20 -0
  51. package/srpc/handler.go +13 -0
  52. package/srpc/index.ts +3 -0
  53. package/srpc/message.go +7 -0
  54. package/srpc/mux.go +76 -0
  55. package/srpc/packet-rw.go +102 -0
  56. package/srpc/packet.go +71 -0
  57. package/srpc/packet.ts +105 -0
  58. package/srpc/rpc-stream.go +76 -0
  59. package/srpc/rpcproto.pb.go +455 -0
  60. package/srpc/rpcproto.proto +46 -0
  61. package/srpc/rpcproto.ts +467 -0
  62. package/srpc/rpcproto_vtproto.pb.go +1094 -0
  63. package/srpc/server-http.go +66 -0
  64. package/srpc/server-pipe.go +26 -0
  65. package/srpc/server-rpc.go +160 -0
  66. package/srpc/server.go +29 -0
  67. package/srpc/stream-pipe.go +86 -0
  68. package/srpc/stream.go +24 -0
  69. package/srpc/stream.ts +11 -0
  70. package/srpc/ts-proto-rpc.ts +29 -0
  71. package/srpc/websocket.go +68 -0
  72. package/srpc/websocket.ts +22 -0
  73. package/srpc/writer.go +9 -0
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2022 Aperture Robotics, LLC.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/Makefile ADDED
@@ -0,0 +1,140 @@
1
+ PROTOWRAP=hack/bin/protowrap
2
+ PROTOC_GEN_GO=hack/bin/protoc-gen-go
3
+ PROTOC_GEN_VTPROTO=hack/bin/protoc-gen-go-vtproto
4
+ PROTOC_GEN_STARPC=hack/bin/protoc-gen-go-starpc
5
+ GOIMPORTS=hack/bin/goimports
6
+ GOLANGCI_LINT=hack/bin/golangci-lint
7
+ GO_MOD_OUTDATED=hack/bin/go-mod-outdated
8
+ export GO111MODULE=on
9
+ GOLIST=go list -f "{{ .Dir }}" -m
10
+
11
+ all:
12
+
13
+ vendor:
14
+ go mod vendor
15
+
16
+ $(PROTOC_GEN_GO):
17
+ cd ./hack; \
18
+ go build -v \
19
+ -o ./bin/protoc-gen-go \
20
+ github.com/golang/protobuf/protoc-gen-go
21
+
22
+ $(PROTOC_GEN_VTPROTO):
23
+ cd ./hack; \
24
+ go build -v \
25
+ -o ./bin/protoc-gen-go-vtproto \
26
+ github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto
27
+
28
+ $(PROTOC_GEN_STARPC):
29
+ cd ./hack; \
30
+ go build -v \
31
+ -o ./bin/protoc-gen-go-starpc \
32
+ github.com/aperturerobotics/starpc/cmd/protoc-gen-go-starpc
33
+
34
+ $(GOIMPORTS):
35
+ cd ./hack; \
36
+ go build -v \
37
+ -o ./bin/goimports \
38
+ golang.org/x/tools/cmd/goimports
39
+
40
+ $(PROTOWRAP):
41
+ cd ./hack; \
42
+ go build -v \
43
+ -o ./bin/protowrap \
44
+ github.com/square/goprotowrap/cmd/protowrap
45
+
46
+ $(GOLANGCI_LINT):
47
+ cd ./hack; \
48
+ go build -v \
49
+ -o ./bin/golangci-lint \
50
+ github.com/golangci/golangci-lint/cmd/golangci-lint
51
+
52
+ $(GO_MOD_OUTDATED):
53
+ cd ./hack; \
54
+ go build -v \
55
+ -o ./bin/go-mod-outdated \
56
+ github.com/psampaz/go-mod-outdated
57
+
58
+ .PHONY: gengo
59
+ gengo: $(GOIMPORTS) $(PROTOWRAP) $(PROTOC_GEN_GO) $(PROTOC_GEN_VTPROTO) $(PROTOC_GEN_STARPC)
60
+ go mod vendor
61
+ shopt -s globstar; \
62
+ set -eo pipefail; \
63
+ export PROJECT=$$(go list -m); \
64
+ export PATH=$$(pwd)/hack/bin:$${PATH}; \
65
+ mkdir -p $$(pwd)/vendor/$$(dirname $${PROJECT}); \
66
+ rm $$(pwd)/vendor/$${PROJECT} || true; \
67
+ ln -s $$(pwd) $$(pwd)/vendor/$${PROJECT} ; \
68
+ $(PROTOWRAP) \
69
+ -I $$(pwd)/vendor \
70
+ --go_out=$$(pwd)/vendor \
71
+ --go-starpc_out=$$(pwd)/vendor \
72
+ --go-vtproto_out=$$(pwd)/vendor \
73
+ --go-vtproto_opt=features=marshal+unmarshal+size+equal \
74
+ --proto_path $$(pwd)/vendor \
75
+ --print_structure \
76
+ --only_specified_files \
77
+ $$(\
78
+ git \
79
+ ls-files "*.proto" |\
80
+ xargs printf -- \
81
+ "$$(pwd)/vendor/$${PROJECT}/%s "); \
82
+ rm $$(pwd)/vendor/$${PROJECT} || true
83
+ $(GOIMPORTS) -w ./
84
+
85
+ node_modules:
86
+ yarn install
87
+
88
+ .PHONY: gents
89
+ gents: $(PROTOWRAP) node_modules
90
+ go mod vendor
91
+ shopt -s globstar; \
92
+ set -eo pipefail; \
93
+ export PROJECT=$$(go list -m); \
94
+ export PATH=$$(pwd)/hack/bin:$${PATH}; \
95
+ mkdir -p $$(pwd)/vendor/$$(dirname $${PROJECT}); \
96
+ rm $$(pwd)/vendor/$${PROJECT} || true; \
97
+ ln -s $$(pwd) $$(pwd)/vendor/$${PROJECT} ; \
98
+ $(PROTOWRAP) \
99
+ -I $$(pwd)/vendor \
100
+ --plugin=./node_modules/.bin/protoc-gen-ts_proto \
101
+ --ts_proto_out=$$(pwd)/vendor \
102
+ --ts_proto_opt=forceLong=long \
103
+ --ts_proto_opt=oneof=unions \
104
+ --ts_proto_opt=esModuleInterop=true \
105
+ --proto_path $$(pwd)/vendor \
106
+ --print_structure \
107
+ --only_specified_files \
108
+ $$(\
109
+ git \
110
+ ls-files "*.proto" |\
111
+ xargs printf -- \
112
+ "$$(pwd)/vendor/$${PROJECT}/%s "); \
113
+ go mod vendor
114
+
115
+ .PHONY: genproto
116
+ genproto: gengo gents
117
+
118
+ .PHONY: gen
119
+ gen: genproto
120
+
121
+ outdated: $(GO_MOD_OUTDATED)
122
+ go list -mod=mod -u -m -json all | $(GO_MOD_OUTDATED) -update -direct
123
+
124
+ list: $(GO_MOD_OUTDATED)
125
+ go list -mod=mod -u -m -json all | $(GO_MOD_OUTDATED)
126
+
127
+ lint: $(GOLANGCI_LINT)
128
+ $(GOLANGCI_LINT) run
129
+
130
+ fix: $(GOLANGCI_LINT)
131
+ $(GOLANGCI_LINT) run --fix
132
+
133
+ .PHONY: test
134
+ test:
135
+ go test -v ./...
136
+
137
+ .PHONY: integration
138
+ integration: node_modules vendor
139
+ cd ./integration && \
140
+ bash ./integration.bash
package/README.md CHANGED
@@ -1,56 +1,158 @@
1
- # Stream RPC (starpc)
1
+ # Stream RPC
2
2
 
3
- **starpc** is a Protobuf RPC implementation leveraging an external Stream
4
- multiplexer and Message framing system like HTTP/2 or Yamux.
3
+ **starpc** is a fully-featured client and server for [Proto3 services] in both
4
+ TypeScript and Go.
5
5
 
6
- Uses any two-way ordered message channel like WebSocket or BroadcastChannel.
6
+ [Proto3 services]: https://developers.google.com/protocol-buffers/docs/proto3#services
7
7
 
8
- Implementation features:
8
+ One of the first libraries to support **client-to-server streaming** RPCs.
9
9
 
10
- - Generates a Go server for proto3 services that does not use reflection.
11
- - Uses the **ts-proto** RPC interface to implement TypeScript clients.
12
- - Each RPC call is mapped to a Stream preventing head-of-line blocking.
10
+ The [rpcproto](./srpc/rpcproto.proto) file contains the entire protocol.
13
11
 
14
- The Go backend provides an interface for forwarding the incoming RPC calls to
15
- the appropriate backend service.
12
+ Leverages the Stream multiplexing of the underlying transport; for example:
13
+ HTTP/2 or [libp2p-mplex] over a WebSocket.
14
+
15
+ [libp2p-mplex]: https://github.com/libp2p/js-libp2p-mplex
16
+
17
+ The server side has not yet been implemented in TypeScript.
16
18
 
17
19
  # Usage
18
20
 
19
- ## Go
21
+ Starting with the [protobuf-project] repository on the "starpc" branch.
22
+
23
+ Use "git add" to add your new .proto files, then `yarn gen` to generate the
24
+ TypeScript and Go code for them.
25
+
26
+ # Examples
27
+
28
+ See the [protobuf-project] template on the "starpc" branch.
29
+
30
+ [protobuf-project]: https://github.com/aperturerobotics/protobuf-project
31
+
32
+ ## Protobuf
20
33
 
21
- TODO
34
+ The following examples use the [echo](./echo/echo.proto) protobuf sample.
22
35
 
23
- Copy the Go project boilerplate files:
36
+ ```protobuf
37
+ syntax = "proto3";
38
+ package echo;
24
39
 
25
- - Makefile: implements "make gengo" to generate Go code with protoc.
26
- - hack/: downloads & builds the necessary protoc-gen-go tools.
40
+ // Echoer service returns the given message.
41
+ service Echoer {
42
+ // Echo returns the given message.
43
+ rpc Echo(EchoMsg) returns (EchoMsg);
44
+ // EchoServerStream is an example of a server -> client one-way stream.
45
+ rpc EchoServerStream(EchoMsg) returns (stream EchoMsg);
46
+ // EchoClientStream is an example of client->server one-way stream.
47
+ rpc EchoClientStream(stream EchoMsg) returns (EchoMsg);
48
+ // EchoBidiStream is an example of a two-way stream.
49
+ rpc EchoBidiStream(stream EchoMsg) returns (stream EchoMsg);
50
+ }
27
51
 
28
- To generate code for .proto files in your repository:
52
+ // EchoMsg is the message body for Echo.
53
+ message EchoMsg {
54
+ string body = 1;
55
+ }
56
+ ```
57
+
58
+ ## Go
29
59
 
30
- - `git add .` - stage all changes first.
31
- - `make gengo` - generate the protobuf files.
60
+ A basic example can be found in the [e2e test]:
61
+
62
+ ```go
63
+ // construct the server
64
+ echoServer := &echo.EchoServer{}
65
+ mux := srpc.NewMux()
66
+ if err := echo.SRPCRegisterEchoer(mux, echoServer); err != nil {
67
+ t.Fatal(err.Error())
68
+ }
69
+ server := srpc.NewServer(mux)
70
+
71
+ // create an in-memory connection to the server
72
+ openStream := srpc.NewServerPipe(server)
73
+
74
+ // construct the client
75
+ client := srpc.NewClient(openStream)
76
+
77
+ // construct the client rpc interface
78
+ clientEcho := echo.NewSRPCEchoerClient(client)
79
+ ctx := context.Background()
80
+ bodyTxt := "hello world"
81
+ out, err := clientEcho.Echo(ctx, &echo.EchoMsg{
82
+ Body: bodyTxt,
83
+ })
84
+ if err != nil {
85
+ t.Fatal(err.Error())
86
+ }
87
+ if out.GetBody() != bodyTxt {
88
+ t.Fatalf("expected %q got %q", bodyTxt, out.GetBody())
89
+ }
90
+ ```
32
91
 
33
- Example of creating & serving a service in Go: (TODO)
92
+ [e2e test]: ./e2e/e2e_test.go
34
93
 
35
94
  ## TypeScript
36
95
 
37
96
  See the ts-proto README to generate the TypeScript for your protobufs.
38
97
 
98
+ Also check out the [integration](./integration/integration.ts) test.
99
+
100
+ Supports any AsyncIterable communication channel with an included implementation
101
+ for WebSockets.
102
+
103
+ This repository uses protowrap, see the [Makefile](./Makefile).
104
+
39
105
  ```typescript
40
- import { RpcClient, WebSocketConn } from 'starpc'
41
- import { DemoServiceClientImpl } from './demo'
106
+ import { WebSocketConn } from '../srpc'
107
+ import { EchoerClientImpl } from '../echo/echo'
42
108
 
43
109
  const ws = new WebSocket('ws://localhost:5000/demo')
44
110
  const channel = new WebSocketConn(ws)
45
- const rpc = new RpcClient(channel)
46
- const demoServiceClient = new DemoServiceClientImpl(rpc)
111
+ const client = channel.buildClient()
112
+ const demoServiceClient = new EchoerClientImpl(client)
47
113
 
48
- const result = await demoServiceClient.DemoEcho({
49
- msg: "Hello world!"
114
+ const result = await demoServiceClient.Echo({
115
+ body: "Hello world!"
50
116
  })
51
- console.log('output', result.msg)
117
+ console.log('output', result.body)
118
+
119
+ const clientRequestStream = new Observable<EchoMsg>(subscriber => {
120
+ subscriber.next({body: 'Hello world from streaming request.'})
121
+ subscriber.complete()
122
+ })
123
+
124
+ console.log('Calling EchoClientStream: client -> server...')
125
+ result = await demoServiceClient.EchoClientStream(clientRequestStream)
126
+ console.log('success: output', result.body)
52
127
  ```
53
128
 
54
129
  `WebSocketConn` uses [js-libp2p-mplex] to multiplex streams over the WebSocket.
55
130
 
56
131
  [js-libp2p-mplex]: https://github.com/libp2p/js-libp2p-mplex
132
+
133
+ # Attribution
134
+
135
+ `protoc-gen-go-starpc` is a heavily modified version of `protoc-gen-go-drpc`.
136
+
137
+ Be sure to check out [drpc] as well: it's compatible with grpc, twirp, and more.
138
+
139
+ [drpc]: https://github.com/storj/drpc
140
+
141
+ Uses [vtprotobuf] to generate Protobuf marshal / unmarshal code.
142
+
143
+ [vtprotobuf]: https://github.com/planetscale/vtprotobuf
144
+
145
+ # Support
146
+
147
+ Starpc is built & supported by Aperture Robotics, LLC.
148
+
149
+ Community contributions and discussion are welcomed!
150
+
151
+ Please open a [GitHub issue] with any questions / issues.
152
+
153
+ [GitHub issue]: https://github.com/aperturerobotics/bifrost/issues/new
154
+
155
+ ... or feel free to reach out on [Matrix Chat] or [Discord].
156
+
157
+ [Discord]: https://discord.gg/KJutMESRsT
158
+ [Matrix Chat]: https://matrix.to/#/#aperturerobotics:matrix.org
@@ -0,0 +1,59 @@
1
+ import Long from 'long';
2
+ import * as _m0 from 'protobufjs/minimal';
3
+ import { Observable } from 'rxjs';
4
+ export declare const protobufPackage = "echo";
5
+ /** EchoMsg is the message body for Echo. */
6
+ export interface EchoMsg {
7
+ body: string;
8
+ }
9
+ export declare const EchoMsg: {
10
+ encode(message: EchoMsg, writer?: _m0.Writer): _m0.Writer;
11
+ decode(input: _m0.Reader | Uint8Array, length?: number): EchoMsg;
12
+ fromJSON(object: any): EchoMsg;
13
+ toJSON(message: EchoMsg): unknown;
14
+ fromPartial<I extends {
15
+ body?: string | undefined;
16
+ } & {
17
+ body?: string | undefined;
18
+ } & Record<Exclude<keyof I, "body">, never>>(object: I): EchoMsg;
19
+ };
20
+ /** Echoer service returns the given message. */
21
+ export interface Echoer {
22
+ /** Echo returns the given message. */
23
+ Echo(request: EchoMsg): Promise<EchoMsg>;
24
+ /** EchoServerStream is an example of a server -> client one-way stream. */
25
+ EchoServerStream(request: EchoMsg): Observable<EchoMsg>;
26
+ /** EchoClientStream is an example of client->server one-way stream. */
27
+ EchoClientStream(request: Observable<EchoMsg>): Promise<EchoMsg>;
28
+ /** EchoBidiStream is an example of a two-way stream. */
29
+ EchoBidiStream(request: Observable<EchoMsg>): Observable<EchoMsg>;
30
+ }
31
+ export declare class EchoerClientImpl implements Echoer {
32
+ private readonly rpc;
33
+ constructor(rpc: Rpc);
34
+ Echo(request: EchoMsg): Promise<EchoMsg>;
35
+ EchoServerStream(request: EchoMsg): Observable<EchoMsg>;
36
+ EchoClientStream(request: Observable<EchoMsg>): Promise<EchoMsg>;
37
+ EchoBidiStream(request: Observable<EchoMsg>): Observable<EchoMsg>;
38
+ }
39
+ interface Rpc {
40
+ request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
41
+ clientStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Promise<Uint8Array>;
42
+ serverStreamingRequest(service: string, method: string, data: Uint8Array): Observable<Uint8Array>;
43
+ bidirectionalStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Observable<Uint8Array>;
44
+ }
45
+ declare type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
46
+ export declare type DeepPartial<T> = T extends Builtin ? T : T extends Long ? string | number | Long : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {
47
+ $case: string;
48
+ } ? {
49
+ [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]>;
50
+ } & {
51
+ $case: T['$case'];
52
+ } : T extends {} ? {
53
+ [K in keyof T]?: DeepPartial<T[K]>;
54
+ } : Partial<T>;
55
+ declare type KeysOfUnion<T> = T extends T ? keyof T : never;
56
+ export declare type Exact<P, I extends P> = P extends Builtin ? P : P & {
57
+ [K in keyof P]: Exact<P[K], I[K]>;
58
+ } & Record<Exclude<keyof I, KeysOfUnion<P>>, never>;
59
+ export {};
@@ -0,0 +1,85 @@
1
+ /* eslint-disable */
2
+ import Long from 'long';
3
+ import * as _m0 from 'protobufjs/minimal';
4
+ import { map } from 'rxjs/operators';
5
+ export const protobufPackage = 'echo';
6
+ function createBaseEchoMsg() {
7
+ return { body: '' };
8
+ }
9
+ export const EchoMsg = {
10
+ encode(message, writer = _m0.Writer.create()) {
11
+ if (message.body !== '') {
12
+ writer.uint32(10).string(message.body);
13
+ }
14
+ return writer;
15
+ },
16
+ decode(input, length) {
17
+ const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
18
+ let end = length === undefined ? reader.len : reader.pos + length;
19
+ const message = createBaseEchoMsg();
20
+ while (reader.pos < end) {
21
+ const tag = reader.uint32();
22
+ switch (tag >>> 3) {
23
+ case 1:
24
+ message.body = reader.string();
25
+ break;
26
+ default:
27
+ reader.skipType(tag & 7);
28
+ break;
29
+ }
30
+ }
31
+ return message;
32
+ },
33
+ fromJSON(object) {
34
+ return {
35
+ body: isSet(object.body) ? String(object.body) : '',
36
+ };
37
+ },
38
+ toJSON(message) {
39
+ const obj = {};
40
+ message.body !== undefined && (obj.body = message.body);
41
+ return obj;
42
+ },
43
+ fromPartial(object) {
44
+ const message = createBaseEchoMsg();
45
+ message.body = object.body ?? '';
46
+ return message;
47
+ },
48
+ };
49
+ export class EchoerClientImpl {
50
+ rpc;
51
+ constructor(rpc) {
52
+ this.rpc = rpc;
53
+ this.Echo = this.Echo.bind(this);
54
+ this.EchoServerStream = this.EchoServerStream.bind(this);
55
+ this.EchoClientStream = this.EchoClientStream.bind(this);
56
+ this.EchoBidiStream = this.EchoBidiStream.bind(this);
57
+ }
58
+ Echo(request) {
59
+ const data = EchoMsg.encode(request).finish();
60
+ const promise = this.rpc.request('echo.Echoer', 'Echo', data);
61
+ return promise.then((data) => EchoMsg.decode(new _m0.Reader(data)));
62
+ }
63
+ EchoServerStream(request) {
64
+ const data = EchoMsg.encode(request).finish();
65
+ const result = this.rpc.serverStreamingRequest('echo.Echoer', 'EchoServerStream', data);
66
+ return result.pipe(map((data) => EchoMsg.decode(new _m0.Reader(data))));
67
+ }
68
+ EchoClientStream(request) {
69
+ const data = request.pipe(map((request) => EchoMsg.encode(request).finish()));
70
+ const promise = this.rpc.clientStreamingRequest('echo.Echoer', 'EchoClientStream', data);
71
+ return promise.then((data) => EchoMsg.decode(new _m0.Reader(data)));
72
+ }
73
+ EchoBidiStream(request) {
74
+ const data = request.pipe(map((request) => EchoMsg.encode(request).finish()));
75
+ const result = this.rpc.bidirectionalStreamingRequest('echo.Echoer', 'EchoBidiStream', data);
76
+ return result.pipe(map((data) => EchoMsg.decode(new _m0.Reader(data))));
77
+ }
78
+ }
79
+ if (_m0.util.Long !== Long) {
80
+ _m0.util.Long = Long;
81
+ _m0.configure();
82
+ }
83
+ function isSet(value) {
84
+ return value !== null && value !== undefined;
85
+ }
@@ -0,0 +1,16 @@
1
+ import type { Duplex, Sink } from 'it-stream-types';
2
+ import { Conn } from './conn';
3
+ export declare class BroadcastChannelIterable<T> implements Duplex<T> {
4
+ readonly channel: BroadcastChannel;
5
+ sink: Sink<T>;
6
+ source: AsyncIterable<T>;
7
+ constructor(channel: BroadcastChannel);
8
+ private _createSink;
9
+ private _createSource;
10
+ }
11
+ export declare function newBroadcastChannelIterable<T>(name: string): BroadcastChannelIterable<T>;
12
+ export declare class BroadcastChannelConn extends Conn {
13
+ private channel;
14
+ constructor(channel: BroadcastChannel);
15
+ getBroadcastChannel(): BroadcastChannel;
16
+ }
@@ -0,0 +1,60 @@
1
+ import { Conn } from './conn';
2
+ import { EventIterator } from 'event-iterator';
3
+ import { pipe } from 'it-pipe';
4
+ // BroadcastChannelIterable is a AsyncIterable wrapper for BroadcastChannel.
5
+ export class BroadcastChannelIterable {
6
+ // channel is the broadcast channel
7
+ channel;
8
+ // sink is the sink for incoming messages.
9
+ sink;
10
+ // source is the source for outgoing messages.
11
+ source;
12
+ constructor(channel) {
13
+ this.channel = channel;
14
+ this.sink = this._createSink();
15
+ this.source = this._createSource();
16
+ }
17
+ // _createSink initializes the sink field.
18
+ _createSink() {
19
+ return async (source) => {
20
+ for await (const msg of source) {
21
+ this.channel.postMessage(msg);
22
+ }
23
+ };
24
+ }
25
+ // _createSource initializes the source field.
26
+ _createSource() {
27
+ return new EventIterator((queue) => {
28
+ const messageListener = (ev) => {
29
+ if (ev.data) {
30
+ queue.push(ev.data);
31
+ }
32
+ };
33
+ this.channel.addEventListener('message', messageListener);
34
+ return () => {
35
+ this.channel.removeEventListener('message', messageListener);
36
+ };
37
+ });
38
+ }
39
+ }
40
+ // newBroadcastChannelIterable constructs a BroadcastChannelIterable with a channel name.
41
+ export function newBroadcastChannelIterable(name) {
42
+ const channel = new BroadcastChannel(name);
43
+ return new BroadcastChannelIterable(channel);
44
+ }
45
+ // BroadcastChannelConn implements a connection with a BroadcastChannel.
46
+ //
47
+ // expects Uint8Array objects over the BroadcastChannel.
48
+ export class BroadcastChannelConn extends Conn {
49
+ // channel is the broadcast channel iterable
50
+ channel;
51
+ constructor(channel) {
52
+ super();
53
+ this.channel = new BroadcastChannelIterable(channel);
54
+ pipe(this, this.channel, this);
55
+ }
56
+ // getBroadcastChannel returns the BroadcastChannel.
57
+ getBroadcastChannel() {
58
+ return this.channel.channel;
59
+ }
60
+ }
@@ -0,0 +1,31 @@
1
+ import type { CallData, CallStart, CallStartResp } from './rpcproto';
2
+ import { Packet } from './rpcproto';
3
+ import type { Sink } from 'it-stream-types';
4
+ export declare type DataCb = (data: Uint8Array) => Promise<boolean | void>;
5
+ export declare class ClientRPC {
6
+ sink: Sink<Packet>;
7
+ source: AsyncIterable<Packet>;
8
+ private readonly _source;
9
+ private service;
10
+ private method;
11
+ private dataCb?;
12
+ private started;
13
+ private onStarted?;
14
+ private complete;
15
+ private onComplete?;
16
+ private closed;
17
+ constructor(service: string, method: string, dataCb: DataCb | null);
18
+ waitStarted(): Promise<void>;
19
+ waitComplete(): Promise<void>;
20
+ writeCallStart(data?: Uint8Array): Promise<void>;
21
+ writeCallData(data: Uint8Array, complete?: boolean, error?: string): Promise<void>;
22
+ private writePacket;
23
+ handleMessage(message: Uint8Array): Promise<void>;
24
+ handlePacket(packet: Partial<Packet>): Promise<void>;
25
+ handleCallStart(packet: Partial<CallStart>): Promise<void>;
26
+ handleCallStartResp(packet: Partial<CallStartResp>): Promise<void>;
27
+ handleCallData(packet: Partial<CallData>): Promise<void>;
28
+ close(err?: Error): Promise<void>;
29
+ private _createSink;
30
+ private _createSource;
31
+ }