starpc 0.12.0 → 0.12.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.
- package/dist/srpc/conn.js +2 -2
- package/dist/srpc/websocket.js +2 -2
- package/echo/server.go +3 -3
- package/package.json +7 -7
- package/srpc/client-set.go +79 -0
- package/srpc/conn.ts +2 -2
- package/srpc/websocket.ts +2 -2
- package/patches/ts-proto+1.126.1.patch +0 -25
package/dist/srpc/conn.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { pipe } from 'it-pipe';
|
|
2
|
-
import {
|
|
2
|
+
import { mplex } from '@libp2p/mplex';
|
|
3
3
|
import isPromise from 'is-promise';
|
|
4
4
|
import { Client } from './client.js';
|
|
5
5
|
import { combineUint8ArrayListTransform } from './array-list.js';
|
|
@@ -21,7 +21,7 @@ export class Conn {
|
|
|
21
21
|
}
|
|
22
22
|
let muxerFactory = connParams?.muxerFactory;
|
|
23
23
|
if (!muxerFactory) {
|
|
24
|
-
muxerFactory =
|
|
24
|
+
muxerFactory = mplex()();
|
|
25
25
|
}
|
|
26
26
|
this.muxer = muxerFactory.createStreamMuxer({
|
|
27
27
|
onIncomingStream: this.handleIncomingStream.bind(this),
|
package/dist/srpc/websocket.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { duplex } from 'it-ws';
|
|
2
2
|
import { pipe } from 'it-pipe';
|
|
3
|
-
import {
|
|
3
|
+
import { mplex } from '@libp2p/mplex';
|
|
4
4
|
import { Conn } from './conn.js';
|
|
5
5
|
// WebSocketConn implements a connection with a WebSocket and optional Server.
|
|
6
6
|
export class WebSocketConn extends Conn {
|
|
7
7
|
constructor(socket, direction, server) {
|
|
8
8
|
super(server, {
|
|
9
9
|
direction,
|
|
10
|
-
muxerFactory:
|
|
10
|
+
muxerFactory: mplex()(),
|
|
11
11
|
});
|
|
12
12
|
this.socket = socket;
|
|
13
13
|
const socketDuplex = duplex(socket);
|
package/echo/server.go
CHANGED
|
@@ -84,11 +84,11 @@ func (s *EchoServer) EchoBidiStream(strm SRPCEchoer_EchoBidiStreamStream) error
|
|
|
84
84
|
|
|
85
85
|
// RpcStream runs a rpc stream
|
|
86
86
|
func (r *EchoServer) RpcStream(stream SRPCEchoer_RpcStreamStream) error {
|
|
87
|
-
return rpcstream.HandleRpcStream(stream, func(ctx context.Context, componentID string) (srpc.Mux, error) {
|
|
87
|
+
return rpcstream.HandleRpcStream(stream, func(ctx context.Context, componentID string) (srpc.Mux, func(), error) {
|
|
88
88
|
if r.rpcStreamMux == nil {
|
|
89
|
-
return nil, errors.New("not implemented")
|
|
89
|
+
return nil, nil, errors.New("not implemented")
|
|
90
90
|
}
|
|
91
|
-
return r.rpcStreamMux, nil
|
|
91
|
+
return r.rpcStreamMux, nil, nil
|
|
92
92
|
})
|
|
93
93
|
}
|
|
94
94
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starpc",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.2",
|
|
4
4
|
"description": "Streaming protobuf RPC service protocol over any two-way channel.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -63,32 +63,32 @@
|
|
|
63
63
|
"@typescript-eslint/parser": "^5.40.0",
|
|
64
64
|
"bufferutil": "^4.0.6",
|
|
65
65
|
"depcheck": "^1.4.3",
|
|
66
|
-
"esbuild": "^0.15.
|
|
66
|
+
"esbuild": "^0.15.11",
|
|
67
67
|
"eslint": "^8.25.0",
|
|
68
68
|
"eslint-config-prettier": "^8.5.0",
|
|
69
69
|
"prettier": "^2.7.1",
|
|
70
70
|
"rimraf": "^3.0.2",
|
|
71
|
-
"ts-proto": "^1.
|
|
71
|
+
"ts-proto": "^1.128.0",
|
|
72
72
|
"typescript": "^4.8.4",
|
|
73
73
|
"utf-8-validate": "^5.0.9"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
76
|
"@libp2p/interface-connection": "^3.0.2",
|
|
77
77
|
"@libp2p/interface-stream-muxer": "^3.0.0",
|
|
78
|
-
"@libp2p/mplex": "^
|
|
78
|
+
"@libp2p/mplex": "^7.0.0",
|
|
79
79
|
"event-iterator": "^2.0.0",
|
|
80
80
|
"is-promise": "^4.0.0",
|
|
81
81
|
"isomorphic-ws": "^5.0.0",
|
|
82
82
|
"it-first": "^1.0.7",
|
|
83
|
-
"it-length-prefixed": "^8.0.
|
|
83
|
+
"it-length-prefixed": "^8.0.3",
|
|
84
84
|
"it-pipe": "^2.0.4",
|
|
85
85
|
"it-pushable": "^3.1.0",
|
|
86
86
|
"it-stream-types": "^1.0.4",
|
|
87
|
-
"it-ws": "^5.0.
|
|
87
|
+
"it-ws": "^5.0.3",
|
|
88
88
|
"long": "^5.2.0",
|
|
89
89
|
"patch-package": "^6.4.7",
|
|
90
90
|
"protobufjs": "^7.1.2",
|
|
91
|
-
"uint8arraylist": "^2.3.
|
|
91
|
+
"uint8arraylist": "^2.3.3",
|
|
92
92
|
"ws": "^8.9.0"
|
|
93
93
|
}
|
|
94
94
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
package srpc
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"errors"
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
// ClientSet wraps a list of clients into one Client.
|
|
9
|
+
type ClientSet struct {
|
|
10
|
+
clients []Client
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// NewClientSet constructs a new client set.
|
|
14
|
+
func NewClientSet(clients []Client) *ClientSet {
|
|
15
|
+
return &ClientSet{clients: clients}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ExecCall executes a request/reply RPC with the remote.
|
|
19
|
+
func (c *ClientSet) ExecCall(
|
|
20
|
+
ctx context.Context,
|
|
21
|
+
service, method string,
|
|
22
|
+
in, out Message,
|
|
23
|
+
) error {
|
|
24
|
+
return c.execCall(ctx, func(client Client) error {
|
|
25
|
+
return client.ExecCall(ctx, service, method, in, out)
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// NewStream starts a streaming RPC with the remote & returns the stream.
|
|
30
|
+
// firstMsg is optional.
|
|
31
|
+
func (c *ClientSet) NewStream(
|
|
32
|
+
ctx context.Context,
|
|
33
|
+
service, method string,
|
|
34
|
+
firstMsg Message,
|
|
35
|
+
) (Stream, error) {
|
|
36
|
+
var strm Stream
|
|
37
|
+
err := c.execCall(ctx, func(client Client) error {
|
|
38
|
+
var err error
|
|
39
|
+
strm, err = client.NewStream(ctx, service, method, firstMsg)
|
|
40
|
+
return err
|
|
41
|
+
})
|
|
42
|
+
return strm, err
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// execCall executes the call conditionally retrying against subsequent client handles.
|
|
46
|
+
func (c *ClientSet) execCall(ctx context.Context, doCall func(client Client) error) error {
|
|
47
|
+
var any bool
|
|
48
|
+
for _, client := range c.clients {
|
|
49
|
+
if client == nil {
|
|
50
|
+
continue
|
|
51
|
+
}
|
|
52
|
+
err := doCall(client)
|
|
53
|
+
any = true
|
|
54
|
+
if err == nil {
|
|
55
|
+
return nil
|
|
56
|
+
}
|
|
57
|
+
if err == context.Canceled {
|
|
58
|
+
select {
|
|
59
|
+
case <-ctx.Done():
|
|
60
|
+
return context.Canceled
|
|
61
|
+
default:
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if err.Error() == ErrUnimplemented.Error() {
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
return err
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if !any {
|
|
72
|
+
return errors.New("no available rpc clients")
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return ErrUnimplemented
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// _ is a type assertion
|
|
79
|
+
var _ Client = ((*ClientSet)(nil))
|
package/srpc/conn.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
} from '@libp2p/interface-stream-muxer'
|
|
6
6
|
import { pipe } from 'it-pipe'
|
|
7
7
|
import type { Duplex } from 'it-stream-types'
|
|
8
|
-
import {
|
|
8
|
+
import { mplex } from '@libp2p/mplex'
|
|
9
9
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
10
10
|
import isPromise from 'is-promise'
|
|
11
11
|
|
|
@@ -55,7 +55,7 @@ export class Conn implements Duplex<Uint8Array> {
|
|
|
55
55
|
}
|
|
56
56
|
let muxerFactory = connParams?.muxerFactory
|
|
57
57
|
if (!muxerFactory) {
|
|
58
|
-
muxerFactory =
|
|
58
|
+
muxerFactory = mplex()()
|
|
59
59
|
}
|
|
60
60
|
this.muxer = muxerFactory.createStreamMuxer({
|
|
61
61
|
onIncomingStream: this.handleIncomingStream.bind(this),
|
package/srpc/websocket.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { duplex } from 'it-ws'
|
|
2
2
|
import { pipe } from 'it-pipe'
|
|
3
|
-
import {
|
|
3
|
+
import { mplex } from '@libp2p/mplex'
|
|
4
4
|
import { Direction } from '@libp2p/interface-connection'
|
|
5
5
|
import type WebSocket from 'isomorphic-ws'
|
|
6
6
|
|
|
@@ -15,7 +15,7 @@ export class WebSocketConn extends Conn {
|
|
|
15
15
|
constructor(socket: WebSocket, direction: Direction, server?: Server) {
|
|
16
16
|
super(server, {
|
|
17
17
|
direction,
|
|
18
|
-
muxerFactory:
|
|
18
|
+
muxerFactory: mplex()(),
|
|
19
19
|
})
|
|
20
20
|
this.socket = socket
|
|
21
21
|
const socketDuplex = duplex(socket)
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
diff --git a/node_modules/ts-proto/build/generate-services.js b/node_modules/ts-proto/build/generate-services.js
|
|
2
|
-
index 8ae7981..5ab6bf6 100644
|
|
3
|
-
--- a/node_modules/ts-proto/build/generate-services.js
|
|
4
|
-
+++ b/node_modules/ts-proto/build/generate-services.js
|
|
5
|
-
@@ -129,7 +129,7 @@ function generateRegularRpcMethod(ctx, fileDesc, serviceDesc, methodDesc) {
|
|
6
|
-
const data = ${encode};
|
|
7
|
-
const ${returnVariable} = this.rpc.${rpcMethod}(
|
|
8
|
-
${maybeCtx}
|
|
9
|
-
- "${(0, utils_1.maybePrefixPackage)(fileDesc, serviceDesc.name)}",
|
|
10
|
-
+ this.service,
|
|
11
|
-
"${methodDesc.name}",
|
|
12
|
-
data
|
|
13
|
-
);
|
|
14
|
-
@@ -148,7 +148,10 @@ function generateServiceClientImpl(ctx, fileDesc, serviceDesc) {
|
|
15
|
-
// Create the constructor(rpc: Rpc)
|
|
16
|
-
const rpcType = options.context ? "Rpc<Context>" : "Rpc";
|
|
17
|
-
chunks.push((0, ts_poet_1.code) `private readonly rpc: ${rpcType};`);
|
|
18
|
-
- chunks.push((0, ts_poet_1.code) `constructor(rpc: ${rpcType}) {`);
|
|
19
|
-
+ chunks.push((0, ts_poet_1.code) `private readonly service: string;`);
|
|
20
|
-
+ chunks.push((0, ts_poet_1.code) `constructor(rpc: ${rpcType}, service?: string) {`);
|
|
21
|
-
+ const serviceID = (0, utils_1.maybePrefixPackage)(fileDesc, serviceDesc.name);
|
|
22
|
-
+ chunks.push((0, ts_poet_1.code) `this.service = service || "${serviceID}";`);
|
|
23
|
-
chunks.push((0, ts_poet_1.code) `this.rpc = rpc;`);
|
|
24
|
-
// Bind each FooService method to the FooServiceImpl class
|
|
25
|
-
for (const methodDesc of serviceDesc.method) {
|