starpc 0.8.4 → 0.9.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.
- package/dist/e2e/e2e.js +1 -1
- package/dist/echo/echo.pb.d.ts +1 -1
- package/dist/echo/echo.pb.js +1 -1
- package/dist/integration/integration.js +1 -1
- package/dist/rpcstream/rpcstream.pb.d.ts +1 -1
- package/dist/rpcstream/rpcstream.pb.js +1 -1
- package/dist/srpc/array-list.d.ts +3 -0
- package/dist/srpc/array-list.js +15 -0
- package/dist/srpc/client.js +2 -1
- package/dist/srpc/conn.d.ts +2 -1
- package/dist/srpc/conn.js +1 -0
- package/dist/srpc/packet.d.ts +4 -3
- package/dist/srpc/packet.js +6 -7
- package/dist/srpc/rpcproto.pb.d.ts +1 -1
- package/dist/srpc/rpcproto.pb.js +1 -1
- package/dist/srpc/server.js +2 -1
- package/dist/srpc/websocket.d.ts +2 -1
- package/dist/srpc/websocket.js +2 -1
- package/e2e/e2e.ts +1 -1
- package/e2e/e2e_test.go +4 -2
- package/echo/echo.pb.ts +1 -1
- package/echo/echo_srpc.pb.go +1 -1
- package/go.mod +8 -10
- package/go.sum +13 -17
- package/integration/integration.ts +1 -1
- package/package.json +11 -10
- package/srpc/accept.go +1 -1
- package/srpc/array-list.ts +19 -0
- package/srpc/client-rpc.go +19 -0
- package/srpc/client.go +2 -8
- package/srpc/client.ts +3 -0
- package/srpc/conn.ts +5 -1
- package/srpc/muxed-conn.go +6 -5
- package/srpc/packet-rw.go +9 -1
- package/srpc/packet.ts +9 -12
- package/srpc/rpcproto.pb.ts +1 -1
- package/srpc/server-rpc.go +0 -10
- package/srpc/server.ts +3 -0
- package/srpc/websocket.ts +3 -1
package/dist/e2e/e2e.js
CHANGED
|
@@ -8,7 +8,7 @@ async function runRPC() {
|
|
|
8
8
|
const echoer = new EchoerServer(server);
|
|
9
9
|
mux.register(createHandler(EchoerDefinition, echoer));
|
|
10
10
|
const clientConn = new Conn();
|
|
11
|
-
const serverConn = new Conn(server);
|
|
11
|
+
const serverConn = new Conn(server, { direction: 'inbound' });
|
|
12
12
|
pipe(clientConn, serverConn, clientConn);
|
|
13
13
|
const client = new Client(clientConn.buildOpenStreamFunc());
|
|
14
14
|
await runRpcStreamTest(client);
|
package/dist/echo/echo.pb.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Long from 'long';
|
|
2
2
|
import { RpcStreamPacket } from '../rpcstream/rpcstream.pb.js';
|
|
3
|
-
import
|
|
3
|
+
import _m0 from 'protobufjs/minimal.js';
|
|
4
4
|
export declare const protobufPackage = "echo";
|
|
5
5
|
/** EchoMsg is the message body for Echo. */
|
|
6
6
|
export interface EchoMsg {
|
package/dist/echo/echo.pb.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable */
|
|
2
2
|
import Long from 'long';
|
|
3
3
|
import { RpcStreamPacket } from '../rpcstream/rpcstream.pb.js';
|
|
4
|
-
import
|
|
4
|
+
import _m0 from 'protobufjs/minimal.js';
|
|
5
5
|
export const protobufPackage = 'echo';
|
|
6
6
|
function createBaseEchoMsg() {
|
|
7
7
|
return { body: '' };
|
|
@@ -5,7 +5,7 @@ async function runRPC() {
|
|
|
5
5
|
const addr = 'ws://localhost:5000/demo';
|
|
6
6
|
console.log(`Connecting to ${addr}`);
|
|
7
7
|
const ws = new WebSocket(addr);
|
|
8
|
-
const channel = new WebSocketConn(ws);
|
|
8
|
+
const channel = new WebSocketConn(ws, 'outbound');
|
|
9
9
|
const client = channel.buildClient();
|
|
10
10
|
console.log('Running client test via WebSocket..');
|
|
11
11
|
await runClientTest(client);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Long from 'long';
|
|
2
|
-
import
|
|
2
|
+
import _m0 from 'protobufjs/minimal.js';
|
|
3
3
|
export declare const protobufPackage = "rpcstream";
|
|
4
4
|
/** RpcStreamPacket is a packet encapsulating data for a RPC stream. */
|
|
5
5
|
export interface RpcStreamPacket {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isUint8ArrayList } from 'uint8arraylist';
|
|
2
|
+
// combineUint8ArrayListTransform combines a Uint8ArrayList into a Uint8Array.
|
|
3
|
+
export function combineUint8ArrayListTransform() {
|
|
4
|
+
// decodeMessageSource unmarshals and async yields encoded Messages.
|
|
5
|
+
return async function* decodeMessageSource(source) {
|
|
6
|
+
for await (const obj of source) {
|
|
7
|
+
if (isUint8ArrayList(obj)) {
|
|
8
|
+
yield* [obj.subarray()];
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
yield* [obj];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
package/dist/srpc/client.js
CHANGED
|
@@ -3,6 +3,7 @@ import { pushable } from 'it-pushable';
|
|
|
3
3
|
import { ClientRPC } from './client-rpc.js';
|
|
4
4
|
import { writeToPushable } from './pushable.js';
|
|
5
5
|
import { decodePacketSource, encodePacketSource, parseLengthPrefixTransform, prependLengthPrefixTransform, } from './packet.js';
|
|
6
|
+
import { combineUint8ArrayListTransform } from './array-list.js';
|
|
6
7
|
// Client implements the ts-proto Rpc interface with the drpcproto protocol.
|
|
7
8
|
export class Client {
|
|
8
9
|
constructor(openStreamFn) {
|
|
@@ -100,7 +101,7 @@ export class Client {
|
|
|
100
101
|
const openStreamFn = await this.openStreamFn;
|
|
101
102
|
const conn = await openStreamFn();
|
|
102
103
|
const call = new ClientRPC(rpcService, rpcMethod);
|
|
103
|
-
pipe(conn, parseLengthPrefixTransform(), decodePacketSource, call, encodePacketSource, prependLengthPrefixTransform(), conn);
|
|
104
|
+
pipe(conn, parseLengthPrefixTransform(), combineUint8ArrayListTransform(), decodePacketSource, call, encodePacketSource, prependLengthPrefixTransform(), combineUint8ArrayListTransform(), conn);
|
|
104
105
|
await call.writeCallStart(data || undefined);
|
|
105
106
|
return call;
|
|
106
107
|
}
|
package/dist/srpc/conn.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { Stream } from '@libp2p/interface-connection';
|
|
1
|
+
import type { Direction, Stream } from '@libp2p/interface-connection';
|
|
2
2
|
import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer';
|
|
3
3
|
import type { Duplex } from 'it-stream-types';
|
|
4
4
|
import type { OpenStreamFunc, Stream as SRPCStream } from './stream.js';
|
|
5
5
|
import { Client } from './client.js';
|
|
6
6
|
export interface ConnParams {
|
|
7
7
|
muxerFactory?: StreamMuxerFactory;
|
|
8
|
+
direction?: Direction;
|
|
8
9
|
}
|
|
9
10
|
export interface StreamHandler {
|
|
10
11
|
handleStream(strm: Duplex<Uint8Array>): void;
|
package/dist/srpc/conn.js
CHANGED
package/dist/srpc/packet.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { Transform } from 'it-stream-types';
|
|
2
1
|
import { Packet } from './rpcproto.pb.js';
|
|
2
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
3
|
+
import { Transform } from 'it-pipe';
|
|
3
4
|
export declare const decodePacketSource: (source: import("it-stream-types").Source<Uint8Array | Uint8Array[]>) => AsyncIterable<Packet>;
|
|
4
5
|
export declare const encodePacketSource: (source: import("it-stream-types").Source<Packet | Packet[]>) => AsyncIterable<Uint8Array>;
|
|
5
|
-
export declare function prependLengthPrefixTransform(): Transform<Uint8Array>;
|
|
6
|
-
export declare function parseLengthPrefixTransform(): Transform<Uint8Array>;
|
|
6
|
+
export declare function prependLengthPrefixTransform(): Transform<Uint8Array | Uint8ArrayList, Uint8ArrayList>;
|
|
7
|
+
export declare function parseLengthPrefixTransform(): Transform<Uint8Array | Uint8ArrayList, Uint8ArrayList>;
|
|
7
8
|
export declare function encodeUint32Le(value: number): Uint8Array;
|
|
8
9
|
export declare function decodeUint32Le(data: Uint8Array): number;
|
|
9
10
|
export declare function prependPacketLen(msgData: Uint8Array): Uint8Array;
|
package/dist/srpc/packet.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { encode as lengthPrefixEncode, decode as lengthPrefixDecode, } from 'it-length-prefixed';
|
|
2
2
|
import { Packet } from './rpcproto.pb.js';
|
|
3
3
|
import { buildDecodeMessageTransform, buildEncodeMessageTransform, } from './message.js';
|
|
4
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
4
5
|
// decodePacketSource decodes packets from a binary data stream.
|
|
5
6
|
export const decodePacketSource = buildDecodeMessageTransform(Packet);
|
|
6
7
|
// encodePacketSource encodes packets from a packet object stream.
|
|
@@ -10,16 +11,14 @@ const uint32LEDecode = (data) => {
|
|
|
10
11
|
if (data.length < 4) {
|
|
11
12
|
throw RangeError('Could not decode int32BE');
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
-
return view.getUint32(0, true);
|
|
14
|
+
return data.getUint32(0, true);
|
|
15
15
|
};
|
|
16
16
|
uint32LEDecode.bytes = 4;
|
|
17
17
|
// uint32LEEncode adds the length prefix.
|
|
18
|
-
const uint32LEEncode = (value
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return target;
|
|
18
|
+
const uint32LEEncode = (value) => {
|
|
19
|
+
const data = new Uint8ArrayList(new Uint8Array(4));
|
|
20
|
+
data.setUint32(0, value, true);
|
|
21
|
+
return data;
|
|
23
22
|
};
|
|
24
23
|
uint32LEEncode.bytes = 4;
|
|
25
24
|
// prependLengthPrefixTransform adds a length prefix to a message source.
|
package/dist/srpc/rpcproto.pb.js
CHANGED
package/dist/srpc/server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { pipe } from 'it-pipe';
|
|
2
2
|
import { ServerRPC } from './server-rpc.js';
|
|
3
3
|
import { parseLengthPrefixTransform, prependLengthPrefixTransform, decodePacketSource, encodePacketSource, } from './packet.js';
|
|
4
|
+
import { combineUint8ArrayListTransform } from './array-list.js';
|
|
4
5
|
// Server implements the SRPC server in TypeScript with a Mux.
|
|
5
6
|
export class Server {
|
|
6
7
|
constructor(lookupMethod) {
|
|
@@ -22,7 +23,7 @@ export class Server {
|
|
|
22
23
|
// handleDuplex handles an incoming message duplex.
|
|
23
24
|
handleDuplex(stream) {
|
|
24
25
|
const rpc = this.startRpc();
|
|
25
|
-
pipe(stream, parseLengthPrefixTransform(), decodePacketSource, rpc, encodePacketSource, prependLengthPrefixTransform(), stream);
|
|
26
|
+
pipe(stream, parseLengthPrefixTransform(), combineUint8ArrayListTransform(), decodePacketSource, rpc, encodePacketSource, prependLengthPrefixTransform(), combineUint8ArrayListTransform(), stream);
|
|
26
27
|
return rpc;
|
|
27
28
|
}
|
|
28
29
|
// handlePacketDuplex handles an incoming Uint8Array duplex.
|
package/dist/srpc/websocket.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { Direction } from '@libp2p/interface-connection';
|
|
1
2
|
import type WebSocket from 'isomorphic-ws';
|
|
2
3
|
import { Conn } from './conn.js';
|
|
3
4
|
import { Server } from './server.js';
|
|
4
5
|
export declare class WebSocketConn extends Conn {
|
|
5
6
|
private socket;
|
|
6
|
-
constructor(socket: WebSocket, server?: Server);
|
|
7
|
+
constructor(socket: WebSocket, direction: Direction, server?: Server);
|
|
7
8
|
getSocket(): WebSocket;
|
|
8
9
|
}
|
package/dist/srpc/websocket.js
CHANGED
|
@@ -4,8 +4,9 @@ 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
|
-
constructor(socket, server) {
|
|
7
|
+
constructor(socket, direction, server) {
|
|
8
8
|
super(server, {
|
|
9
|
+
direction,
|
|
9
10
|
muxerFactory: new Mplex(),
|
|
10
11
|
});
|
|
11
12
|
this.socket = socket;
|
package/e2e/e2e.ts
CHANGED
|
@@ -10,7 +10,7 @@ async function runRPC() {
|
|
|
10
10
|
mux.register(createHandler(EchoerDefinition, echoer))
|
|
11
11
|
|
|
12
12
|
const clientConn = new Conn()
|
|
13
|
-
const serverConn = new Conn(server)
|
|
13
|
+
const serverConn = new Conn(server, {direction: 'inbound'})
|
|
14
14
|
pipe(clientConn, serverConn, clientConn)
|
|
15
15
|
const client = new Client(clientConn.buildOpenStreamFunc())
|
|
16
16
|
|
package/e2e/e2e_test.go
CHANGED
|
@@ -32,14 +32,16 @@ func RunE2E(t *testing.T, cb func(client echo.SRPCEchoerClient) error) {
|
|
|
32
32
|
// construct the client
|
|
33
33
|
clientPipe, serverPipe := net.Pipe()
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
// outbound=true
|
|
36
|
+
clientMp, err := mp.NewMultiplex(clientPipe, true, nil)
|
|
36
37
|
if err != nil {
|
|
37
38
|
t.Fatal(err.Error())
|
|
38
39
|
}
|
|
39
40
|
client := srpc.NewClientWithMuxedConn(mplex.NewMuxedConn(clientMp))
|
|
40
41
|
|
|
41
42
|
ctx := context.Background()
|
|
42
|
-
|
|
43
|
+
// outbound=false
|
|
44
|
+
serverMp, _ := mp.NewMultiplex(serverPipe, false, nil)
|
|
43
45
|
go func() {
|
|
44
46
|
_ = server.AcceptMuxedConn(ctx, mplex.NewMuxedConn(serverMp))
|
|
45
47
|
}()
|
package/echo/echo.pb.ts
CHANGED
package/echo/echo_srpc.pb.go
CHANGED
package/go.mod
CHANGED
|
@@ -6,27 +6,25 @@ replace github.com/libp2p/go-libp2p => github.com/paralin/go-libp2p v0.20.1-0.20
|
|
|
6
6
|
|
|
7
7
|
require (
|
|
8
8
|
github.com/pkg/errors v0.9.1
|
|
9
|
-
google.golang.org/protobuf v1.
|
|
9
|
+
google.golang.org/protobuf v1.28.1
|
|
10
10
|
nhooyr.io/websocket v1.8.8-0.20210410000328-8dee580a7f74
|
|
11
11
|
)
|
|
12
12
|
|
|
13
13
|
require (
|
|
14
|
-
github.com/libp2p/go-libp2p v0.
|
|
15
|
-
github.com/libp2p/go-libp2p-core v0.19.
|
|
16
|
-
github.com/libp2p/go-mplex v0.7.0
|
|
17
|
-
github.com/sirupsen/logrus v1.
|
|
14
|
+
github.com/libp2p/go-libp2p v0.21.0
|
|
15
|
+
github.com/libp2p/go-libp2p-core v0.19.1
|
|
16
|
+
github.com/libp2p/go-mplex v0.7.1-0.20220702225122-8cbdf39b21f5
|
|
17
|
+
github.com/sirupsen/logrus v1.9.0
|
|
18
18
|
)
|
|
19
19
|
|
|
20
20
|
require (
|
|
21
|
-
github.com/btcsuite/btcd v0.22.1 // indirect
|
|
22
21
|
github.com/btcsuite/btcd/btcec/v2 v2.1.3 // indirect
|
|
23
|
-
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
|
|
24
22
|
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
|
|
25
23
|
github.com/gogo/protobuf v1.3.2 // indirect
|
|
26
24
|
github.com/ipfs/go-cid v0.2.0 // indirect
|
|
27
25
|
github.com/ipfs/go-log/v2 v2.5.1 // indirect
|
|
28
26
|
github.com/klauspost/compress v1.15.1 // indirect
|
|
29
|
-
github.com/klauspost/cpuid/v2 v2.0.
|
|
27
|
+
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
|
|
30
28
|
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
|
|
31
29
|
github.com/libp2p/go-openssl v0.0.7 // indirect
|
|
32
30
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
|
@@ -45,7 +43,7 @@ require (
|
|
|
45
43
|
go.uber.org/atomic v1.7.0 // indirect
|
|
46
44
|
go.uber.org/multierr v1.6.0 // indirect
|
|
47
45
|
go.uber.org/zap v1.19.1 // indirect
|
|
48
|
-
golang.org/x/crypto v0.0.0-
|
|
49
|
-
golang.org/x/sys v0.0.0-
|
|
46
|
+
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
|
|
47
|
+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
|
50
48
|
lukechampine.com/blake3 v1.1.6 // indirect
|
|
51
49
|
)
|
package/go.sum
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
|
2
2
|
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
|
|
3
3
|
github.com/btcsuite/btcd v0.22.1 h1:CnwP9LM/M9xuRrGSCGeMVs9iv09uMqwsVX7EeIpgV2c=
|
|
4
|
-
github.com/btcsuite/btcd v0.22.1/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y=
|
|
5
4
|
github.com/btcsuite/btcd/btcec/v2 v2.1.3 h1:xM/n3yIhHAhHy04z4i43C8p4ehixJZMsnrVJkgl+MTE=
|
|
6
5
|
github.com/btcsuite/btcd/btcec/v2 v2.1.3/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
|
|
7
6
|
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
|
|
8
|
-
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
|
|
9
7
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
10
8
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
11
9
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
@@ -54,9 +52,8 @@ github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYs
|
|
|
54
52
|
github.com/klauspost/compress v1.15.1 h1:y9FcTHGyrebwfP0ZZqFiaxTaiDnUrGkJkI+f583BL1A=
|
|
55
53
|
github.com/klauspost/compress v1.15.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
|
56
54
|
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
|
55
|
+
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
|
57
56
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
|
58
|
-
github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE=
|
|
59
|
-
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
|
|
60
57
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
|
61
58
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|
62
59
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|
@@ -64,11 +61,11 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
|
|
64
61
|
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
|
65
62
|
github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
|
|
66
63
|
github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
|
|
67
|
-
github.com/libp2p/go-libp2p-core v0.19.
|
|
68
|
-
github.com/libp2p/go-libp2p-core v0.19.
|
|
64
|
+
github.com/libp2p/go-libp2p-core v0.19.1 h1:zaZQQCeCrFMtxFa1wHy6AhsVynyNmZAvwgWqSSPT3WE=
|
|
65
|
+
github.com/libp2p/go-libp2p-core v0.19.1/go.mod h1:2uLhmmqDiFY+dw+70KkBLeKvvsJHGWUINRDdeV1ip7k=
|
|
69
66
|
github.com/libp2p/go-libp2p-testing v0.10.0 h1:LO7wuUPPNAe1D1s0HZ+9WoROaGIn/MEl1wtugXuTRzg=
|
|
70
|
-
github.com/libp2p/go-mplex v0.7.0 h1:
|
|
71
|
-
github.com/libp2p/go-mplex v0.7.0/go.mod h1:rW8ThnRcYWft/Jb2jeORBmPd6xuG3dGxWN/W168L9EU=
|
|
67
|
+
github.com/libp2p/go-mplex v0.7.1-0.20220702225122-8cbdf39b21f5 h1:ik8jmF64ZDxAn5K9zJ74ZHed6SBoomwcrP+p6QT7OOQ=
|
|
68
|
+
github.com/libp2p/go-mplex v0.7.1-0.20220702225122-8cbdf39b21f5/go.mod h1:rW8ThnRcYWft/Jb2jeORBmPd6xuG3dGxWN/W168L9EU=
|
|
72
69
|
github.com/libp2p/go-openssl v0.0.7 h1:eCAzdLejcNVBzP/iZM9vqHnQm+XyCEbSSIheIPRGNsw=
|
|
73
70
|
github.com/libp2p/go-openssl v0.0.7/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc=
|
|
74
71
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
|
@@ -106,8 +103,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
|
|
106
103
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
|
107
104
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
108
105
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
109
|
-
github.com/sirupsen/logrus v1.
|
|
110
|
-
github.com/sirupsen/logrus v1.
|
|
106
|
+
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
|
107
|
+
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
|
111
108
|
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU=
|
|
112
109
|
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc=
|
|
113
110
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
|
@@ -136,8 +133,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
|
|
|
136
133
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
|
137
134
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
|
138
135
|
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
|
139
|
-
golang.org/x/crypto v0.0.0-
|
|
140
|
-
golang.org/x/crypto v0.0.0-
|
|
136
|
+
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA=
|
|
137
|
+
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
|
141
138
|
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
|
142
139
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
|
143
140
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
|
@@ -163,9 +160,8 @@ golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7w
|
|
|
163
160
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
164
161
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
165
162
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
166
|
-
golang.org/x/sys v0.0.0-
|
|
167
|
-
golang.org/x/sys v0.0.0-
|
|
168
|
-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
163
|
+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
|
164
|
+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
169
165
|
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
|
170
166
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
171
167
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
@@ -184,8 +180,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
|
|
|
184
180
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
|
185
181
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
186
182
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
|
187
|
-
google.golang.org/protobuf v1.
|
|
188
|
-
google.golang.org/protobuf v1.
|
|
183
|
+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
|
184
|
+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
|
189
185
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
190
186
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
191
187
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
@@ -6,7 +6,7 @@ async function runRPC() {
|
|
|
6
6
|
const addr = 'ws://localhost:5000/demo'
|
|
7
7
|
console.log(`Connecting to ${addr}`)
|
|
8
8
|
const ws = new WebSocket(addr)
|
|
9
|
-
const channel = new WebSocketConn(ws)
|
|
9
|
+
const channel = new WebSocketConn(ws, 'outbound')
|
|
10
10
|
const client = channel.buildClient()
|
|
11
11
|
|
|
12
12
|
console.log('Running client test via WebSocket..')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Streaming protobuf RPC service protocol over any two-way channel.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -59,34 +59,35 @@
|
|
|
59
59
|
"singleQuote": true
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
63
|
-
"@typescript-eslint/parser": "^5.
|
|
62
|
+
"@typescript-eslint/eslint-plugin": "^5.31.0",
|
|
63
|
+
"@typescript-eslint/parser": "^5.31.0",
|
|
64
64
|
"bufferutil": "^4.0.6",
|
|
65
65
|
"depcheck": "^1.4.3",
|
|
66
|
-
"esbuild": "^0.14.
|
|
67
|
-
"eslint": "^8.
|
|
66
|
+
"esbuild": "^0.14.51",
|
|
67
|
+
"eslint": "^8.20.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.121.1",
|
|
72
72
|
"typescript": "^4.7.4",
|
|
73
73
|
"utf-8-validate": "^5.0.9"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
76
|
"@libp2p/interface-connection": "^2.1.1",
|
|
77
77
|
"@libp2p/interface-stream-muxer": "^2.0.1",
|
|
78
|
-
"@libp2p/mplex": "^4.0.
|
|
78
|
+
"@libp2p/mplex": "^4.0.2",
|
|
79
79
|
"event-iterator": "^2.0.0",
|
|
80
80
|
"isomorphic-ws": "^5.0.0",
|
|
81
81
|
"it-first": "^1.0.7",
|
|
82
|
-
"it-length-prefixed": "^
|
|
82
|
+
"it-length-prefixed": "^8.0.0",
|
|
83
83
|
"it-pipe": "^2.0.3",
|
|
84
84
|
"it-pushable": "^3.0.0",
|
|
85
85
|
"it-stream-types": "^1.0.4",
|
|
86
86
|
"it-ws": "^5.0.2",
|
|
87
87
|
"long": "^5.2.0",
|
|
88
88
|
"patch-package": "^6.4.7",
|
|
89
|
-
"protobufjs": "^
|
|
90
|
-
"
|
|
89
|
+
"protobufjs": "^7.0.0",
|
|
90
|
+
"uint8arraylist": "^2.0.0",
|
|
91
|
+
"ws": "^8.8.1"
|
|
91
92
|
}
|
|
92
93
|
}
|
package/srpc/accept.go
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Source } from 'it-stream-types'
|
|
2
|
+
import { isUint8ArrayList, Uint8ArrayList } from 'uint8arraylist'
|
|
3
|
+
import type { Transform } from 'it-stream-types'
|
|
4
|
+
|
|
5
|
+
// combineUint8ArrayListTransform combines a Uint8ArrayList into a Uint8Array.
|
|
6
|
+
export function combineUint8ArrayListTransform(): Transform<Uint8Array | Uint8ArrayList, Uint8Array> {
|
|
7
|
+
// decodeMessageSource unmarshals and async yields encoded Messages.
|
|
8
|
+
return async function* decodeMessageSource(
|
|
9
|
+
source: Source<Uint8Array | Uint8ArrayList>
|
|
10
|
+
): AsyncIterable<Uint8Array> {
|
|
11
|
+
for await (const obj of source) {
|
|
12
|
+
if (isUint8ArrayList(obj)) {
|
|
13
|
+
yield* [obj.subarray()]
|
|
14
|
+
} else {
|
|
15
|
+
yield* [obj]
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
package/srpc/client-rpc.go
CHANGED
|
@@ -2,6 +2,7 @@ package srpc
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"context"
|
|
5
|
+
"io"
|
|
5
6
|
|
|
6
7
|
"github.com/pkg/errors"
|
|
7
8
|
)
|
|
@@ -86,6 +87,24 @@ func (r *ClientRPC) ReadAll() ([][]byte, error) {
|
|
|
86
87
|
}
|
|
87
88
|
}
|
|
88
89
|
|
|
90
|
+
// ReadOne reads a single message and returns.
|
|
91
|
+
//
|
|
92
|
+
// returns io.EOF if the stream ended.
|
|
93
|
+
func (r *ClientRPC) ReadOne() ([]byte, error) {
|
|
94
|
+
select {
|
|
95
|
+
case <-r.ctx.Done():
|
|
96
|
+
return nil, context.Canceled
|
|
97
|
+
case data, ok := <-r.dataCh:
|
|
98
|
+
if !ok {
|
|
99
|
+
if err := r.serverErr; err != nil {
|
|
100
|
+
return nil, err
|
|
101
|
+
}
|
|
102
|
+
return nil, io.EOF
|
|
103
|
+
}
|
|
104
|
+
return data, nil
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
89
108
|
// Context is canceled when the ClientRPC is no longer valid.
|
|
90
109
|
func (r *ClientRPC) Context() context.Context {
|
|
91
110
|
return r.ctx
|
package/srpc/client.go
CHANGED
|
@@ -2,7 +2,6 @@ package srpc
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"context"
|
|
5
|
-
"io"
|
|
6
5
|
|
|
7
6
|
"github.com/pkg/errors"
|
|
8
7
|
)
|
|
@@ -55,17 +54,12 @@ func (c *client) Invoke(rctx context.Context, service, method string, in, out Me
|
|
|
55
54
|
if err := clientRPC.Start(writer, true, firstMsg); err != nil {
|
|
56
55
|
return err
|
|
57
56
|
}
|
|
58
|
-
|
|
57
|
+
msg, err := clientRPC.ReadOne()
|
|
59
58
|
if err != nil {
|
|
60
59
|
// this includes any server returned error.
|
|
61
60
|
return err
|
|
62
61
|
}
|
|
63
|
-
if
|
|
64
|
-
// no reply? return eof.
|
|
65
|
-
return io.EOF
|
|
66
|
-
}
|
|
67
|
-
// parse first message to out
|
|
68
|
-
if err := out.UnmarshalVT(msgs[0]); err != nil {
|
|
62
|
+
if err := out.UnmarshalVT(msg); err != nil {
|
|
69
63
|
return errors.Wrap(ErrInvalidMessage, err.Error())
|
|
70
64
|
}
|
|
71
65
|
// done
|
package/srpc/client.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
parseLengthPrefixTransform,
|
|
12
12
|
prependLengthPrefixTransform,
|
|
13
13
|
} from './packet.js'
|
|
14
|
+
import { combineUint8ArrayListTransform } from './array-list.js'
|
|
14
15
|
|
|
15
16
|
// Client implements the ts-proto Rpc interface with the drpcproto protocol.
|
|
16
17
|
export class Client implements TsProtoRpc {
|
|
@@ -142,10 +143,12 @@ export class Client implements TsProtoRpc {
|
|
|
142
143
|
pipe(
|
|
143
144
|
conn,
|
|
144
145
|
parseLengthPrefixTransform(),
|
|
146
|
+
combineUint8ArrayListTransform(),
|
|
145
147
|
decodePacketSource,
|
|
146
148
|
call,
|
|
147
149
|
encodePacketSource,
|
|
148
150
|
prependLengthPrefixTransform(),
|
|
151
|
+
combineUint8ArrayListTransform(),
|
|
149
152
|
conn
|
|
150
153
|
)
|
|
151
154
|
await call.writeCallStart(data || undefined)
|
package/srpc/conn.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Stream } from '@libp2p/interface-connection'
|
|
1
|
+
import type { Direction, Stream } from '@libp2p/interface-connection'
|
|
2
2
|
import type {
|
|
3
3
|
StreamMuxer,
|
|
4
4
|
StreamMuxerFactory,
|
|
@@ -13,6 +13,9 @@ import { Client } from './client.js'
|
|
|
13
13
|
export interface ConnParams {
|
|
14
14
|
// muxerFactory overrides using the default factory (@libp2p/mplex).
|
|
15
15
|
muxerFactory?: StreamMuxerFactory
|
|
16
|
+
// direction is the muxer connection direction.
|
|
17
|
+
// defaults to outbound.
|
|
18
|
+
direction?: Direction
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
// StreamHandler handles incoming streams.
|
|
@@ -42,6 +45,7 @@ export class Conn implements Duplex<Uint8Array> {
|
|
|
42
45
|
}
|
|
43
46
|
this.muxer = muxerFactory.createStreamMuxer({
|
|
44
47
|
onIncomingStream: this.handleIncomingStream.bind(this),
|
|
48
|
+
direction: connParams?.direction || 'outbound',
|
|
45
49
|
})
|
|
46
50
|
}
|
|
47
51
|
|
package/srpc/muxed-conn.go
CHANGED
|
@@ -2,6 +2,7 @@ package srpc
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"context"
|
|
5
|
+
"io"
|
|
5
6
|
"net"
|
|
6
7
|
|
|
7
8
|
"github.com/libp2p/go-libp2p-core/network"
|
|
@@ -9,9 +10,9 @@ import (
|
|
|
9
10
|
mp "github.com/libp2p/go-mplex"
|
|
10
11
|
)
|
|
11
12
|
|
|
12
|
-
// NewMuxedConn constructs a new MuxedConn from a
|
|
13
|
-
func NewMuxedConn(conn
|
|
14
|
-
m, err := mp.NewMultiplex(conn,
|
|
13
|
+
// NewMuxedConn constructs a new MuxedConn from a ReadWriteCloser.
|
|
14
|
+
func NewMuxedConn(conn io.ReadWriteCloser, outbound bool) (network.MuxedConn, error) {
|
|
15
|
+
m, err := mp.NewMultiplex(conn, outbound, nil)
|
|
15
16
|
if err != nil {
|
|
16
17
|
return nil, err
|
|
17
18
|
}
|
|
@@ -21,8 +22,8 @@ func NewMuxedConn(conn net.Conn, isServer bool) (network.MuxedConn, error) {
|
|
|
21
22
|
// NewClientWithConn constructs the muxer and the client.
|
|
22
23
|
//
|
|
23
24
|
// uses libp2p mplex
|
|
24
|
-
func NewClientWithConn(conn net.Conn,
|
|
25
|
-
mconn, err := NewMuxedConn(conn,
|
|
25
|
+
func NewClientWithConn(conn net.Conn, outbound bool) (Client, error) {
|
|
26
|
+
mconn, err := NewMuxedConn(conn, outbound)
|
|
26
27
|
if err != nil {
|
|
27
28
|
return nil, err
|
|
28
29
|
}
|
package/srpc/packet-rw.go
CHANGED
|
@@ -35,7 +35,15 @@ func (r *PacketReaderWriter) WritePacket(p *Packet) error {
|
|
|
35
35
|
if err != nil {
|
|
36
36
|
return err
|
|
37
37
|
}
|
|
38
|
-
|
|
38
|
+
var n int
|
|
39
|
+
written := 0
|
|
40
|
+
for written < len(data) {
|
|
41
|
+
n, err = r.rw.Write(data)
|
|
42
|
+
if err != nil {
|
|
43
|
+
return err
|
|
44
|
+
}
|
|
45
|
+
written += n
|
|
46
|
+
}
|
|
39
47
|
return err
|
|
40
48
|
}
|
|
41
49
|
|
package/srpc/packet.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Transform } from 'it-stream-types'
|
|
2
1
|
import {
|
|
3
2
|
encode as lengthPrefixEncode,
|
|
4
3
|
decode as lengthPrefixDecode,
|
|
@@ -9,6 +8,8 @@ import {
|
|
|
9
8
|
buildDecodeMessageTransform,
|
|
10
9
|
buildEncodeMessageTransform,
|
|
11
10
|
} from './message.js'
|
|
11
|
+
import { Uint8ArrayList } from 'uint8arraylist'
|
|
12
|
+
import { Transform } from 'it-pipe'
|
|
12
13
|
|
|
13
14
|
// decodePacketSource decodes packets from a binary data stream.
|
|
14
15
|
export const decodePacketSource = buildDecodeMessageTransform<Packet>(Packet)
|
|
@@ -17,38 +18,34 @@ export const decodePacketSource = buildDecodeMessageTransform<Packet>(Packet)
|
|
|
17
18
|
export const encodePacketSource = buildEncodeMessageTransform<Packet>(Packet)
|
|
18
19
|
|
|
19
20
|
// uint32LEDecode removes the length prefix.
|
|
20
|
-
const uint32LEDecode = (data:
|
|
21
|
+
const uint32LEDecode = (data: Uint8ArrayList) => {
|
|
21
22
|
if (data.length < 4) {
|
|
22
23
|
throw RangeError('Could not decode int32BE')
|
|
23
24
|
}
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
return view.getUint32(0, true)
|
|
26
|
+
return data.getUint32(0, true)
|
|
27
27
|
}
|
|
28
28
|
uint32LEDecode.bytes = 4
|
|
29
29
|
|
|
30
30
|
// uint32LEEncode adds the length prefix.
|
|
31
31
|
const uint32LEEncode = (
|
|
32
32
|
value: number,
|
|
33
|
-
target?: Uint8Array,
|
|
34
|
-
offset?: number
|
|
35
33
|
) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return target
|
|
34
|
+
const data = new Uint8ArrayList(new Uint8Array(4))
|
|
35
|
+
data.setUint32(0, value, true)
|
|
36
|
+
return data
|
|
40
37
|
}
|
|
41
38
|
uint32LEEncode.bytes = 4
|
|
42
39
|
|
|
43
40
|
// prependLengthPrefixTransform adds a length prefix to a message source.
|
|
44
41
|
// little-endian uint32
|
|
45
|
-
export function prependLengthPrefixTransform(): Transform<Uint8Array> {
|
|
42
|
+
export function prependLengthPrefixTransform(): Transform<Uint8Array | Uint8ArrayList, Uint8ArrayList> {
|
|
46
43
|
return lengthPrefixEncode({ lengthEncoder: uint32LEEncode })
|
|
47
44
|
}
|
|
48
45
|
|
|
49
46
|
// parseLengthPrefixTransform parses the length prefix from a message source.
|
|
50
47
|
// little-endian uint32
|
|
51
|
-
export function parseLengthPrefixTransform(): Transform<Uint8Array> {
|
|
48
|
+
export function parseLengthPrefixTransform(): Transform<Uint8Array | Uint8ArrayList, Uint8ArrayList> {
|
|
52
49
|
return lengthPrefixDecode({ lengthDecoder: uint32LEDecode })
|
|
53
50
|
}
|
|
54
51
|
|
package/srpc/rpcproto.pb.ts
CHANGED
package/srpc/server-rpc.go
CHANGED
|
@@ -3,8 +3,6 @@ package srpc
|
|
|
3
3
|
import (
|
|
4
4
|
"context"
|
|
5
5
|
"io"
|
|
6
|
-
"runtime"
|
|
7
|
-
"time"
|
|
8
6
|
|
|
9
7
|
"github.com/pkg/errors"
|
|
10
8
|
)
|
|
@@ -173,14 +171,6 @@ func (r *ServerRPC) invokeRPC() {
|
|
|
173
171
|
}
|
|
174
172
|
outPkt := NewCallDataPacket(nil, false, true, err)
|
|
175
173
|
_ = r.writer.WritePacket(outPkt)
|
|
176
|
-
|
|
177
|
-
// HACK: some multiplexers will drop the last packet if Close is called too
|
|
178
|
-
// soon after Write. Add a brief delay to ensure the packet is written.
|
|
179
|
-
//
|
|
180
|
-
// See: https://github.com/lucas-clemente/quic-go/issues/3472
|
|
181
|
-
runtime.Gosched()
|
|
182
|
-
time.Sleep(time.Millisecond)
|
|
183
|
-
|
|
184
174
|
_ = r.writer.Close()
|
|
185
175
|
r.ctxCancel()
|
|
186
176
|
}
|
package/srpc/server.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
} from './packet.js'
|
|
14
14
|
import { StreamHandler } from './conn.js'
|
|
15
15
|
import { RpcStreamHandler } from '../rpcstream/rpcstream.js'
|
|
16
|
+
import { combineUint8ArrayListTransform } from './array-list.js'
|
|
16
17
|
|
|
17
18
|
// Server implements the SRPC server in TypeScript with a Mux.
|
|
18
19
|
export class Server implements StreamHandler {
|
|
@@ -45,10 +46,12 @@ export class Server implements StreamHandler {
|
|
|
45
46
|
pipe(
|
|
46
47
|
stream,
|
|
47
48
|
parseLengthPrefixTransform(),
|
|
49
|
+
combineUint8ArrayListTransform(),
|
|
48
50
|
decodePacketSource,
|
|
49
51
|
rpc,
|
|
50
52
|
encodePacketSource,
|
|
51
53
|
prependLengthPrefixTransform(),
|
|
54
|
+
combineUint8ArrayListTransform(),
|
|
52
55
|
stream
|
|
53
56
|
)
|
|
54
57
|
return rpc
|
package/srpc/websocket.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { duplex } from 'it-ws'
|
|
2
2
|
import { pipe } from 'it-pipe'
|
|
3
3
|
import { Mplex } from '@libp2p/mplex'
|
|
4
|
+
import { Direction } from '@libp2p/interface-connection'
|
|
4
5
|
import type WebSocket from 'isomorphic-ws'
|
|
5
6
|
|
|
6
7
|
import { Conn } from './conn.js'
|
|
@@ -11,8 +12,9 @@ export class WebSocketConn extends Conn {
|
|
|
11
12
|
// socket is the web socket
|
|
12
13
|
private socket: WebSocket
|
|
13
14
|
|
|
14
|
-
constructor(socket: WebSocket, server?: Server) {
|
|
15
|
+
constructor(socket: WebSocket, direction: Direction, server?: Server) {
|
|
15
16
|
super(server, {
|
|
17
|
+
direction,
|
|
16
18
|
muxerFactory: new Mplex(),
|
|
17
19
|
})
|
|
18
20
|
this.socket = socket
|