starpc 0.12.1 → 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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import { pipe } from 'it-pipe';
2
- import { Mplex } from '@libp2p/mplex';
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 = new Mplex();
24
+ muxerFactory = mplex()();
25
25
  }
26
26
  this.muxer = muxerFactory.createStreamMuxer({
27
27
  onIncomingStream: this.handleIncomingStream.bind(this),
@@ -1,13 +1,13 @@
1
1
  import { duplex } from 'it-ws';
2
2
  import { pipe } from 'it-pipe';
3
- import { Mplex } from '@libp2p/mplex';
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: new Mplex(),
10
+ muxerFactory: mplex()(),
11
11
  });
12
12
  this.socket = socket;
13
13
  const socketDuplex = duplex(socket);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.12.1",
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.10",
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.126.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": "^6.0.2",
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.2",
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.2",
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.2",
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 { Mplex } from '@libp2p/mplex'
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 = new Mplex()
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 { Mplex } from '@libp2p/mplex'
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: new Mplex(),
18
+ muxerFactory: mplex()(),
19
19
  })
20
20
  this.socket = socket
21
21
  const socketDuplex = duplex(socket)