starpc 0.10.3 → 0.10.6
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/rpcstream/rpcstream.d.ts +3 -3
- package/dist/rpcstream/rpcstream.js +1 -2
- package/dist/srpc/conn.d.ts +2 -1
- package/dist/srpc/conn.js +12 -2
- package/dist/srpc/server.d.ts +2 -4
- package/dist/srpc/server.js +1 -6
- package/dist/srpc/stream.d.ts +1 -2
- package/package.json +1 -1
- package/srpc/conn.ts +14 -3
- package/srpc/server.ts +3 -20
- package/srpc/stream.ts +1 -2
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { RpcStreamPacket } from './rpcstream.pb.js';
|
|
2
2
|
import { OpenStreamFunc, Stream } from '../srpc/stream.js';
|
|
3
3
|
import { Pushable } from 'it-pushable';
|
|
4
|
-
import {
|
|
4
|
+
import { Source, Sink } from 'it-stream-types';
|
|
5
5
|
import { Uint8ArrayList } from 'uint8arraylist';
|
|
6
6
|
export declare type RpcStreamCaller = (request: AsyncIterable<RpcStreamPacket>) => AsyncIterable<RpcStreamPacket>;
|
|
7
7
|
export declare function openRpcStream(componentId: string, caller: RpcStreamCaller): Promise<Stream>;
|
|
8
8
|
export declare function buildRpcStreamOpenStream(componentId: string, caller: RpcStreamCaller): OpenStreamFunc;
|
|
9
|
-
export declare type RpcStreamHandler = (stream:
|
|
9
|
+
export declare type RpcStreamHandler = (stream: Stream) => void;
|
|
10
10
|
export declare type RpcStreamGetter = (componentId: string) => Promise<RpcStreamHandler | null>;
|
|
11
11
|
export declare function handleRpcStream(packetStream: AsyncIterator<RpcStreamPacket>, getter: RpcStreamGetter): AsyncIterable<RpcStreamPacket>;
|
|
12
12
|
export declare class RpcStream implements Stream {
|
|
13
|
-
readonly source: Source<
|
|
13
|
+
readonly source: Source<Uint8Array>;
|
|
14
14
|
readonly sink: Sink<Uint8ArrayList | Uint8Array>;
|
|
15
15
|
private readonly _packetStream;
|
|
16
16
|
private readonly _packetSink;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { pushable } from 'it-pushable';
|
|
2
|
-
import { Uint8ArrayList } from 'uint8arraylist';
|
|
3
2
|
// openRpcStream attempts to open a stream over a RPC call.
|
|
4
3
|
// waits for the remote to ack the stream before returning.
|
|
5
4
|
export async function openRpcStream(componentId, caller) {
|
|
@@ -142,7 +141,7 @@ export class RpcStream {
|
|
|
142
141
|
if (!body || body.$case !== 'data') {
|
|
143
142
|
continue;
|
|
144
143
|
}
|
|
145
|
-
yield* [
|
|
144
|
+
yield* [body.data];
|
|
146
145
|
}
|
|
147
146
|
})();
|
|
148
147
|
}
|
package/dist/srpc/conn.d.ts
CHANGED
|
@@ -9,8 +9,9 @@ export interface ConnParams {
|
|
|
9
9
|
direction?: Direction;
|
|
10
10
|
}
|
|
11
11
|
export interface StreamHandler {
|
|
12
|
-
handleStream(strm:
|
|
12
|
+
handleStream(strm: SRPCStream): void;
|
|
13
13
|
}
|
|
14
|
+
export declare function streamToSRPCStream(stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>): SRPCStream;
|
|
14
15
|
export declare class Conn implements Duplex<Uint8Array> {
|
|
15
16
|
private muxer;
|
|
16
17
|
private server?;
|
package/dist/srpc/conn.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
import { pipe } from 'it-pipe';
|
|
1
2
|
import { Mplex } from '@libp2p/mplex';
|
|
2
3
|
import { Client } from './client.js';
|
|
4
|
+
import { combineUint8ArrayListTransform } from './array-list.js';
|
|
5
|
+
// streamToSRPCStream converts a Stream to a SRPCStream.
|
|
6
|
+
export function streamToSRPCStream(stream) {
|
|
7
|
+
return {
|
|
8
|
+
source: pipe(stream, combineUint8ArrayListTransform()),
|
|
9
|
+
sink: stream.sink,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
3
12
|
// Conn implements a generic connection with a two-way stream.
|
|
4
13
|
// Implements the client by opening streams with the remote.
|
|
5
14
|
// Implements the server by handling incoming streams.
|
|
@@ -36,7 +45,8 @@ export class Conn {
|
|
|
36
45
|
}
|
|
37
46
|
// openStream implements the client open stream function.
|
|
38
47
|
async openStream() {
|
|
39
|
-
|
|
48
|
+
const stream = this.muxer.newStream();
|
|
49
|
+
return streamToSRPCStream(stream);
|
|
40
50
|
}
|
|
41
51
|
// buildOpenStreamFunc returns openStream bound to this conn.
|
|
42
52
|
buildOpenStreamFunc() {
|
|
@@ -48,6 +58,6 @@ export class Conn {
|
|
|
48
58
|
if (!server) {
|
|
49
59
|
return strm.abort(new Error('server not implemented'));
|
|
50
60
|
}
|
|
51
|
-
server.handleStream(strm);
|
|
61
|
+
server.handleStream(streamToSRPCStream(strm));
|
|
52
62
|
}
|
|
53
63
|
}
|
package/dist/srpc/server.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { Stream } from '@libp2p/interface-connection';
|
|
2
1
|
import { Duplex } from 'it-stream-types';
|
|
3
|
-
import { Uint8ArrayList } from 'uint8arraylist';
|
|
4
2
|
import { LookupMethod } from './mux.js';
|
|
5
3
|
import { ServerRPC } from './server-rpc.js';
|
|
6
4
|
import { Packet } from './rpcproto.pb.js';
|
|
7
5
|
import { StreamHandler } from './conn.js';
|
|
6
|
+
import { Stream } from './stream.js';
|
|
8
7
|
import { RpcStreamHandler } from '../rpcstream/rpcstream.js';
|
|
9
8
|
export declare class Server implements StreamHandler {
|
|
10
9
|
private lookupMethod;
|
|
@@ -12,8 +11,7 @@ export declare class Server implements StreamHandler {
|
|
|
12
11
|
get rpcStreamHandler(): RpcStreamHandler;
|
|
13
12
|
startRpc(): ServerRPC;
|
|
14
13
|
handleStream(stream: Stream): ServerRPC;
|
|
15
|
-
handleDuplex(stream: Duplex<
|
|
16
|
-
handleUint8ArrayDuplex(stream: Duplex<Uint8Array>): ServerRPC;
|
|
14
|
+
handleDuplex(stream: Duplex<Uint8Array, Uint8Array>): ServerRPC;
|
|
17
15
|
handlePacketDuplex(stream: Duplex<Uint8Array>): ServerRPC;
|
|
18
16
|
handlePacketStream(stream: Duplex<Packet>): ServerRPC;
|
|
19
17
|
}
|
package/dist/srpc/server.js
CHANGED
|
@@ -9,7 +9,7 @@ export class Server {
|
|
|
9
9
|
}
|
|
10
10
|
// rpcStreamHandler implements the RpcStreamHandler interface.
|
|
11
11
|
get rpcStreamHandler() {
|
|
12
|
-
return this.
|
|
12
|
+
return this.handleStream.bind(this);
|
|
13
13
|
}
|
|
14
14
|
// startRpc starts a new server-side RPC.
|
|
15
15
|
// the returned RPC handles incoming Packets.
|
|
@@ -22,11 +22,6 @@ export class Server {
|
|
|
22
22
|
}
|
|
23
23
|
// handleDuplex handles an incoming message duplex.
|
|
24
24
|
handleDuplex(stream) {
|
|
25
|
-
const rpc = this.startRpc();
|
|
26
|
-
pipe(stream, parseLengthPrefixTransform(), combineUint8ArrayListTransform(), decodePacketSource, rpc, encodePacketSource, prependLengthPrefixTransform(), stream);
|
|
27
|
-
return rpc;
|
|
28
|
-
}
|
|
29
|
-
handleUint8ArrayDuplex(stream) {
|
|
30
25
|
const rpc = this.startRpc();
|
|
31
26
|
pipe(stream, parseLengthPrefixTransform(), combineUint8ArrayListTransform(), decodePacketSource, rpc, encodePacketSource, prependLengthPrefixTransform(), combineUint8ArrayListTransform(), stream);
|
|
32
27
|
return rpc;
|
package/dist/srpc/stream.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Packet } from './rpcproto.pb.js';
|
|
2
2
|
import type { Duplex } from 'it-stream-types';
|
|
3
|
-
import { Uint8ArrayList } from 'uint8arraylist';
|
|
4
3
|
export declare type PacketHandler = (packet: Packet) => Promise<void>;
|
|
5
|
-
export declare type Stream = Duplex<
|
|
4
|
+
export declare type Stream = Duplex<Uint8Array>;
|
|
6
5
|
export declare type OpenStreamFunc = () => Promise<Stream>;
|
package/package.json
CHANGED
package/srpc/conn.ts
CHANGED
|
@@ -3,12 +3,14 @@ import type {
|
|
|
3
3
|
StreamMuxer,
|
|
4
4
|
StreamMuxerFactory,
|
|
5
5
|
} from '@libp2p/interface-stream-muxer'
|
|
6
|
+
import { pipe } from 'it-pipe'
|
|
6
7
|
import type { Duplex } from 'it-stream-types'
|
|
7
8
|
import { Mplex } from '@libp2p/mplex'
|
|
8
9
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
import type { OpenStreamFunc, Stream as SRPCStream } from './stream.js'
|
|
11
12
|
import { Client } from './client.js'
|
|
13
|
+
import { combineUint8ArrayListTransform } from './array-list.js'
|
|
12
14
|
|
|
13
15
|
// ConnParams are parameters that can be passed to the Conn constructor.
|
|
14
16
|
export interface ConnParams {
|
|
@@ -23,7 +25,15 @@ export interface ConnParams {
|
|
|
23
25
|
// Implemented by Server.
|
|
24
26
|
export interface StreamHandler {
|
|
25
27
|
// handleStream handles an incoming stream.
|
|
26
|
-
handleStream(strm:
|
|
28
|
+
handleStream(strm: SRPCStream): void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// streamToSRPCStream converts a Stream to a SRPCStream.
|
|
32
|
+
export function streamToSRPCStream(stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>): SRPCStream {
|
|
33
|
+
return {
|
|
34
|
+
source: pipe(stream, combineUint8ArrayListTransform()),
|
|
35
|
+
sink: stream.sink,
|
|
36
|
+
}
|
|
27
37
|
}
|
|
28
38
|
|
|
29
39
|
// Conn implements a generic connection with a two-way stream.
|
|
@@ -72,7 +82,8 @@ export class Conn implements Duplex<Uint8Array> {
|
|
|
72
82
|
|
|
73
83
|
// openStream implements the client open stream function.
|
|
74
84
|
public async openStream(): Promise<SRPCStream> {
|
|
75
|
-
|
|
85
|
+
const stream = this.muxer.newStream()
|
|
86
|
+
return streamToSRPCStream(stream)
|
|
76
87
|
}
|
|
77
88
|
|
|
78
89
|
// buildOpenStreamFunc returns openStream bound to this conn.
|
|
@@ -86,6 +97,6 @@ export class Conn implements Duplex<Uint8Array> {
|
|
|
86
97
|
if (!server) {
|
|
87
98
|
return strm.abort(new Error('server not implemented'))
|
|
88
99
|
}
|
|
89
|
-
server.handleStream(strm)
|
|
100
|
+
server.handleStream(streamToSRPCStream(strm))
|
|
90
101
|
}
|
|
91
102
|
}
|
package/srpc/server.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Stream } from '@libp2p/interface-connection'
|
|
2
1
|
import { Duplex } from 'it-stream-types'
|
|
3
2
|
import { pipe } from 'it-pipe'
|
|
4
3
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
@@ -13,6 +12,7 @@ import {
|
|
|
13
12
|
encodePacketSource,
|
|
14
13
|
} from './packet.js'
|
|
15
14
|
import { StreamHandler } from './conn.js'
|
|
15
|
+
import { Stream } from './stream.js'
|
|
16
16
|
import { RpcStreamHandler } from '../rpcstream/rpcstream.js'
|
|
17
17
|
import { combineUint8ArrayListTransform } from './array-list.js'
|
|
18
18
|
|
|
@@ -27,7 +27,7 @@ export class Server implements StreamHandler {
|
|
|
27
27
|
|
|
28
28
|
// rpcStreamHandler implements the RpcStreamHandler interface.
|
|
29
29
|
public get rpcStreamHandler(): RpcStreamHandler {
|
|
30
|
-
return this.
|
|
30
|
+
return this.handleStream.bind(this)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
// startRpc starts a new server-side RPC.
|
|
@@ -43,24 +43,7 @@ export class Server implements StreamHandler {
|
|
|
43
43
|
|
|
44
44
|
// handleDuplex handles an incoming message duplex.
|
|
45
45
|
public handleDuplex(
|
|
46
|
-
stream: Duplex<
|
|
47
|
-
): ServerRPC {
|
|
48
|
-
const rpc = this.startRpc()
|
|
49
|
-
pipe(
|
|
50
|
-
stream,
|
|
51
|
-
parseLengthPrefixTransform(),
|
|
52
|
-
combineUint8ArrayListTransform(),
|
|
53
|
-
decodePacketSource,
|
|
54
|
-
rpc,
|
|
55
|
-
encodePacketSource,
|
|
56
|
-
prependLengthPrefixTransform(),
|
|
57
|
-
stream
|
|
58
|
-
)
|
|
59
|
-
return rpc
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public handleUint8ArrayDuplex(
|
|
63
|
-
stream: Duplex<Uint8Array>
|
|
46
|
+
stream: Duplex<Uint8Array, Uint8Array>
|
|
64
47
|
): ServerRPC {
|
|
65
48
|
const rpc = this.startRpc()
|
|
66
49
|
pipe(
|
package/srpc/stream.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type { Packet } from './rpcproto.pb.js'
|
|
2
2
|
import type { Duplex } from 'it-stream-types'
|
|
3
|
-
import { Uint8ArrayList } from 'uint8arraylist'
|
|
4
3
|
|
|
5
4
|
// PacketHandler handles incoming packets.
|
|
6
5
|
export type PacketHandler = (packet: Packet) => Promise<void>
|
|
7
6
|
|
|
8
7
|
// Stream is an open connection.
|
|
9
|
-
export type Stream = Duplex<
|
|
8
|
+
export type Stream = Duplex<Uint8Array>
|
|
10
9
|
|
|
11
10
|
// OpenStreamFunc is a function to start a new RPC by opening a Stream.
|
|
12
11
|
export type OpenStreamFunc = () => Promise<Stream>
|