starpc 0.22.2 → 0.22.4
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 +4 -4
- package/dist/rpcstream/rpcstream.js +21 -22
- package/dist/srpc/client.js +4 -4
- package/dist/srpc/common-rpc.js +1 -1
- package/package.json +1 -1
- package/srpc/client.ts +4 -4
- package/srpc/common-rpc.ts +1 -1
|
@@ -7,13 +7,13 @@ export declare function openRpcStream(componentId: string, caller: RpcStreamCall
|
|
|
7
7
|
export declare function buildRpcStreamOpenStream(componentId: string, caller: RpcStreamCaller): OpenStreamFunc;
|
|
8
8
|
export type RpcStreamHandler = (stream: Stream) => void;
|
|
9
9
|
export type RpcStreamGetter = (componentId: string) => Promise<RpcStreamHandler | null>;
|
|
10
|
-
export declare function handleRpcStream(
|
|
10
|
+
export declare function handleRpcStream(packetRx: AsyncIterator<RpcStreamPacket>, getter: RpcStreamGetter): AsyncIterable<RpcStreamPacket>;
|
|
11
11
|
export declare class RpcStream implements Stream {
|
|
12
12
|
readonly source: AsyncGenerator<Uint8Array>;
|
|
13
13
|
readonly sink: Sink<Source<Uint8Array>, Promise<void>>;
|
|
14
|
-
private readonly
|
|
15
|
-
private readonly
|
|
16
|
-
constructor(
|
|
14
|
+
private readonly _packetRx;
|
|
15
|
+
private readonly _packetTx;
|
|
16
|
+
constructor(packetTx: Pushable<RpcStreamPacket>, packetRx: AsyncIterator<RpcStreamPacket>);
|
|
17
17
|
private _createSink;
|
|
18
18
|
private _createSource;
|
|
19
19
|
}
|
|
@@ -2,17 +2,17 @@ import { pushable } from 'it-pushable';
|
|
|
2
2
|
// openRpcStream attempts to open a stream over a RPC call.
|
|
3
3
|
// if waitAck is set, waits for the remote to ack the stream before returning.
|
|
4
4
|
export async function openRpcStream(componentId, caller, waitAck) {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const packetTx = pushable({ objectMode: true });
|
|
6
|
+
const packetRx = caller(packetTx);
|
|
7
7
|
// write the component id
|
|
8
|
-
|
|
8
|
+
packetTx.push({
|
|
9
9
|
body: {
|
|
10
10
|
$case: 'init',
|
|
11
11
|
init: { componentId },
|
|
12
12
|
},
|
|
13
13
|
});
|
|
14
14
|
// construct packet stream
|
|
15
|
-
const packetIt =
|
|
15
|
+
const packetIt = packetRx[Symbol.asyncIterator]();
|
|
16
16
|
// wait for ack, if set.
|
|
17
17
|
if (waitAck) {
|
|
18
18
|
const ackPacketIt = await packetIt.next();
|
|
@@ -31,7 +31,7 @@ export async function openRpcStream(componentId, caller, waitAck) {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
// build & return the data stream
|
|
34
|
-
return new RpcStream(
|
|
34
|
+
return new RpcStream(packetTx, packetIt);
|
|
35
35
|
}
|
|
36
36
|
// buildRpcStreamOpenStream builds a OpenStream func with a RpcStream.
|
|
37
37
|
export function buildRpcStreamOpenStream(componentId, caller) {
|
|
@@ -40,9 +40,9 @@ export function buildRpcStreamOpenStream(componentId, caller) {
|
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
// handleRpcStream handles an incoming RPC stream (remote is the initiator).
|
|
43
|
-
export async function* handleRpcStream(
|
|
43
|
+
export async function* handleRpcStream(packetRx, getter) {
|
|
44
44
|
// read the component id
|
|
45
|
-
const initRpcStreamIt = await
|
|
45
|
+
const initRpcStreamIt = await packetRx.next();
|
|
46
46
|
if (initRpcStreamIt.done) {
|
|
47
47
|
throw new Error('closed before init received');
|
|
48
48
|
}
|
|
@@ -83,23 +83,23 @@ export async function* handleRpcStream(packetStream, getter) {
|
|
|
83
83
|
throw err;
|
|
84
84
|
}
|
|
85
85
|
// build the outgoing packet sink & the packet source
|
|
86
|
-
const
|
|
86
|
+
const packetTx = pushable({ objectMode: true });
|
|
87
87
|
// start the handler
|
|
88
|
-
const rpcStream = new RpcStream(
|
|
88
|
+
const rpcStream = new RpcStream(packetTx, packetRx);
|
|
89
89
|
handler(rpcStream);
|
|
90
90
|
// process packets
|
|
91
|
-
for await (const packet of
|
|
91
|
+
for await (const packet of packetTx) {
|
|
92
92
|
yield* [packet];
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
// RpcStream implements the Stream on top of a RPC call.
|
|
96
96
|
// Note: expects the stream to already have been negotiated.
|
|
97
97
|
export class RpcStream {
|
|
98
|
-
//
|
|
99
|
-
//
|
|
100
|
-
constructor(
|
|
101
|
-
this.
|
|
102
|
-
this.
|
|
98
|
+
// packetTx writes packets to the remote.
|
|
99
|
+
// packetRx receives packets from the remote.
|
|
100
|
+
constructor(packetTx, packetRx) {
|
|
101
|
+
this._packetTx = packetTx;
|
|
102
|
+
this._packetRx = packetRx;
|
|
103
103
|
this.sink = this._createSink();
|
|
104
104
|
this.source = this._createSource();
|
|
105
105
|
}
|
|
@@ -108,23 +108,22 @@ export class RpcStream {
|
|
|
108
108
|
return async (source) => {
|
|
109
109
|
try {
|
|
110
110
|
for await (const arr of source) {
|
|
111
|
-
this.
|
|
111
|
+
this._packetTx.push({
|
|
112
112
|
body: { $case: 'data', data: arr },
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
|
-
this.
|
|
115
|
+
this._packetTx.end();
|
|
116
116
|
}
|
|
117
117
|
catch (err) {
|
|
118
|
-
this.
|
|
118
|
+
this._packetTx.end(err);
|
|
119
119
|
}
|
|
120
120
|
};
|
|
121
121
|
}
|
|
122
122
|
// _createSource initializes the source field.
|
|
123
123
|
_createSource() {
|
|
124
|
-
|
|
125
|
-
return (async function* () {
|
|
124
|
+
return (async function* (packetRx) {
|
|
126
125
|
while (true) {
|
|
127
|
-
const msgIt = await
|
|
126
|
+
const msgIt = await packetRx.next();
|
|
128
127
|
if (msgIt.done) {
|
|
129
128
|
return;
|
|
130
129
|
}
|
|
@@ -144,6 +143,6 @@ export class RpcStream {
|
|
|
144
143
|
break;
|
|
145
144
|
}
|
|
146
145
|
}
|
|
147
|
-
})();
|
|
146
|
+
})(this._packetRx);
|
|
148
147
|
}
|
|
149
148
|
}
|
package/dist/srpc/client.js
CHANGED
|
@@ -44,7 +44,7 @@ export class Client {
|
|
|
44
44
|
.then(async (call) => {
|
|
45
45
|
return writeToPushable(call.rpcDataSource, serverData);
|
|
46
46
|
})
|
|
47
|
-
.catch(serverData.
|
|
47
|
+
.catch(err => serverData.end(err));
|
|
48
48
|
return serverData;
|
|
49
49
|
}
|
|
50
50
|
// bidirectionalStreamingRequest starts a two-way streaming request.
|
|
@@ -57,13 +57,13 @@ export class Client {
|
|
|
57
57
|
for await (const message of call.rpcDataSource) {
|
|
58
58
|
serverData.push(message);
|
|
59
59
|
}
|
|
60
|
+
serverData.end();
|
|
60
61
|
}
|
|
61
62
|
catch (err) {
|
|
62
|
-
serverData.
|
|
63
|
+
serverData.end(err);
|
|
63
64
|
}
|
|
64
|
-
serverData.end();
|
|
65
65
|
})
|
|
66
|
-
.catch(serverData.
|
|
66
|
+
.catch(err => serverData.end(err));
|
|
67
67
|
return serverData;
|
|
68
68
|
}
|
|
69
69
|
// startRpc is a common utility function to begin a rpc call.
|
package/dist/srpc/common-rpc.js
CHANGED
|
@@ -120,11 +120,11 @@ export class CommonRPC {
|
|
|
120
120
|
return;
|
|
121
121
|
}
|
|
122
122
|
this.closed = true;
|
|
123
|
+
this._rpcDataSource.end(err);
|
|
123
124
|
try {
|
|
124
125
|
await this.writeCallCancel();
|
|
125
126
|
}
|
|
126
127
|
finally {
|
|
127
|
-
this._rpcDataSource.end(err);
|
|
128
128
|
// note: don't pass error to _source here.
|
|
129
129
|
this._source.end();
|
|
130
130
|
}
|
package/package.json
CHANGED
package/srpc/client.ts
CHANGED
|
@@ -70,7 +70,7 @@ export class Client implements TsProtoRpc {
|
|
|
70
70
|
.then(async (call) => {
|
|
71
71
|
return writeToPushable(call.rpcDataSource, serverData)
|
|
72
72
|
})
|
|
73
|
-
.catch(serverData.
|
|
73
|
+
.catch(err => serverData.end(err))
|
|
74
74
|
return serverData
|
|
75
75
|
}
|
|
76
76
|
|
|
@@ -89,12 +89,12 @@ export class Client implements TsProtoRpc {
|
|
|
89
89
|
for await (const message of call.rpcDataSource) {
|
|
90
90
|
serverData.push(message)
|
|
91
91
|
}
|
|
92
|
+
serverData.end()
|
|
92
93
|
} catch (err) {
|
|
93
|
-
serverData.
|
|
94
|
+
serverData.end(err as Error)
|
|
94
95
|
}
|
|
95
|
-
serverData.end()
|
|
96
96
|
})
|
|
97
|
-
.catch(serverData.
|
|
97
|
+
.catch(err => serverData.end(err))
|
|
98
98
|
return serverData
|
|
99
99
|
}
|
|
100
100
|
|
package/srpc/common-rpc.ts
CHANGED
|
@@ -157,10 +157,10 @@ export class CommonRPC {
|
|
|
157
157
|
return
|
|
158
158
|
}
|
|
159
159
|
this.closed = true
|
|
160
|
+
this._rpcDataSource.end(err)
|
|
160
161
|
try {
|
|
161
162
|
await this.writeCallCancel()
|
|
162
163
|
} finally {
|
|
163
|
-
this._rpcDataSource.end(err)
|
|
164
164
|
// note: don't pass error to _source here.
|
|
165
165
|
this._source.end()
|
|
166
166
|
}
|