starpc 0.4.8 → 0.5.1
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/Makefile +1 -0
- package/README.md +20 -10
- package/dist/echo/client-test.d.ts +1 -0
- package/dist/echo/client-test.js +20 -18
- package/dist/echo/echo.pb.d.ts +165 -12
- package/dist/echo/echo.pb.js +61 -17
- package/dist/echo/server.d.ts +8 -4
- package/dist/echo/server.js +29 -37
- package/dist/rpcstream/rpcstream.d.ts +6 -6
- package/dist/rpcstream/rpcstream.js +92 -51
- package/dist/rpcstream/rpcstream.pb.d.ts +46 -1
- package/dist/rpcstream/rpcstream.pb.js +157 -9
- package/dist/srpc/broadcast-channel.d.ts +2 -2
- package/dist/srpc/broadcast-channel.js +6 -6
- package/dist/srpc/client.d.ts +3 -4
- package/dist/srpc/client.js +12 -46
- package/dist/srpc/common-rpc.d.ts +3 -2
- package/dist/srpc/common-rpc.js +12 -0
- package/dist/srpc/definition.d.ts +3 -3
- package/dist/srpc/handler.d.ts +2 -3
- package/dist/srpc/handler.js +5 -22
- package/dist/srpc/index.d.ts +2 -2
- package/dist/srpc/index.js +2 -2
- package/dist/srpc/packet.js +0 -32
- package/dist/srpc/pushable.d.ts +2 -0
- package/dist/srpc/pushable.js +13 -0
- package/dist/srpc/rpcproto.pb.d.ts +13 -6
- package/dist/srpc/rpcproto.pb.js +95 -10
- package/dist/srpc/server.d.ts +1 -0
- package/dist/srpc/server.js +7 -0
- package/dist/srpc/ts-proto-rpc.d.ts +3 -4
- package/e2e/e2e.ts +4 -3
- package/e2e/e2e_test.go +24 -1
- package/echo/client-test.ts +23 -18
- package/echo/echo.pb.go +33 -20
- package/echo/echo.pb.ts +90 -34
- package/echo/echo.proto +4 -0
- package/echo/echo_srpc.pb.go +77 -0
- package/echo/server.go +18 -0
- package/echo/server.ts +47 -41
- package/integration/integration.go +1 -2
- package/integration/integration.ts +5 -1
- package/integration/integration_srpc.pb.go +139 -0
- package/package.json +13 -9
- package/patches/ts-proto+1.116.0.patch +14 -0
- package/srpc/broadcast-channel.ts +8 -8
- package/srpc/client.ts +16 -50
- package/srpc/common-rpc.ts +14 -2
- package/srpc/definition.ts +3 -3
- package/srpc/handler.ts +17 -34
- package/srpc/index.ts +3 -3
- package/srpc/muxed-conn.go +2 -2
- package/srpc/packet-rw.go +4 -6
- package/srpc/packet.ts +0 -33
- package/srpc/pushable.ts +17 -0
- package/srpc/rpcproto.pb.ts +122 -12
- package/srpc/server-pipe.go +2 -2
- package/srpc/server.go +2 -2
- package/srpc/server.ts +8 -0
- package/srpc/ts-proto-rpc.ts +4 -6
- package/srpc/websocket.go +2 -2
- package/dist/echo/sever.d.ts +0 -0
- package/dist/echo/sever.js +0 -1
- package/dist/srpc/observable-source.d.ts +0 -9
- package/dist/srpc/observable-source.js +0 -25
- package/echo/sever.ts +0 -0
- package/srpc/observable-source.ts +0 -40
package/srpc/rpcproto.pb.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
/* eslint-disable */
|
|
2
2
|
import Long from 'long'
|
|
3
3
|
import * as _m0 from 'protobufjs/minimal'
|
|
4
|
+
import {
|
|
5
|
+
CallStart as CallStart1,
|
|
6
|
+
CallData as CallData2,
|
|
7
|
+
} from './rpcproto.pb.js'
|
|
4
8
|
|
|
5
9
|
export const protobufPackage = 'srpc'
|
|
6
10
|
|
|
7
11
|
/** Packet is a message sent over a srpc packet connection. */
|
|
8
12
|
export interface Packet {
|
|
9
13
|
body?:
|
|
10
|
-
| { $case: 'callStart'; callStart:
|
|
11
|
-
| { $case: 'callData'; callData:
|
|
14
|
+
| { $case: 'callStart'; callStart: CallStart1 }
|
|
15
|
+
| { $case: 'callData'; callData: CallData2 }
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
/** CallStart requests starting a new RPC call. */
|
|
@@ -57,13 +61,13 @@ export const Packet = {
|
|
|
57
61
|
writer: _m0.Writer = _m0.Writer.create()
|
|
58
62
|
): _m0.Writer {
|
|
59
63
|
if (message.body?.$case === 'callStart') {
|
|
60
|
-
|
|
64
|
+
CallStart1.encode(
|
|
61
65
|
message.body.callStart,
|
|
62
66
|
writer.uint32(10).fork()
|
|
63
67
|
).ldelim()
|
|
64
68
|
}
|
|
65
69
|
if (message.body?.$case === 'callData') {
|
|
66
|
-
|
|
70
|
+
CallData2.encode(message.body.callData, writer.uint32(18).fork()).ldelim()
|
|
67
71
|
}
|
|
68
72
|
return writer
|
|
69
73
|
},
|
|
@@ -78,13 +82,13 @@ export const Packet = {
|
|
|
78
82
|
case 1:
|
|
79
83
|
message.body = {
|
|
80
84
|
$case: 'callStart',
|
|
81
|
-
callStart:
|
|
85
|
+
callStart: CallStart1.decode(reader, reader.uint32()),
|
|
82
86
|
}
|
|
83
87
|
break
|
|
84
88
|
case 2:
|
|
85
89
|
message.body = {
|
|
86
90
|
$case: 'callData',
|
|
87
|
-
callData:
|
|
91
|
+
callData: CallData2.decode(reader, reader.uint32()),
|
|
88
92
|
}
|
|
89
93
|
break
|
|
90
94
|
default:
|
|
@@ -95,15 +99,49 @@ export const Packet = {
|
|
|
95
99
|
return message
|
|
96
100
|
},
|
|
97
101
|
|
|
102
|
+
// encodeTransform encodes a source of message objects.
|
|
103
|
+
// Transform<Packet, Uint8Array>
|
|
104
|
+
async *encodeTransform(
|
|
105
|
+
source: AsyncIterable<Packet | Packet[]> | Iterable<Packet | Packet[]>
|
|
106
|
+
): AsyncIterable<Uint8Array> {
|
|
107
|
+
for await (const pkt of source) {
|
|
108
|
+
if (Array.isArray(pkt)) {
|
|
109
|
+
for (const p of pkt) {
|
|
110
|
+
yield* [Packet.encode(p).finish()]
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
yield* [Packet.encode(pkt).finish()]
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
// decodeTransform decodes a source of encoded messages.
|
|
119
|
+
// Transform<Uint8Array, Packet>
|
|
120
|
+
async *decodeTransform(
|
|
121
|
+
source:
|
|
122
|
+
| AsyncIterable<Uint8Array | Uint8Array[]>
|
|
123
|
+
| Iterable<Uint8Array | Uint8Array[]>
|
|
124
|
+
): AsyncIterable<Packet> {
|
|
125
|
+
for await (const pkt of source) {
|
|
126
|
+
if (Array.isArray(pkt)) {
|
|
127
|
+
for (const p of pkt) {
|
|
128
|
+
yield* [Packet.decode(p)]
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
yield* [Packet.decode(pkt)]
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
|
|
98
136
|
fromJSON(object: any): Packet {
|
|
99
137
|
return {
|
|
100
138
|
body: isSet(object.callStart)
|
|
101
139
|
? {
|
|
102
140
|
$case: 'callStart',
|
|
103
|
-
callStart:
|
|
141
|
+
callStart: CallStart1.fromJSON(object.callStart),
|
|
104
142
|
}
|
|
105
143
|
: isSet(object.callData)
|
|
106
|
-
? { $case: 'callData', callData:
|
|
144
|
+
? { $case: 'callData', callData: CallData2.fromJSON(object.callData) }
|
|
107
145
|
: undefined,
|
|
108
146
|
}
|
|
109
147
|
},
|
|
@@ -112,11 +150,11 @@ export const Packet = {
|
|
|
112
150
|
const obj: any = {}
|
|
113
151
|
message.body?.$case === 'callStart' &&
|
|
114
152
|
(obj.callStart = message.body?.callStart
|
|
115
|
-
?
|
|
153
|
+
? CallStart1.toJSON(message.body?.callStart)
|
|
116
154
|
: undefined)
|
|
117
155
|
message.body?.$case === 'callData' &&
|
|
118
156
|
(obj.callData = message.body?.callData
|
|
119
|
-
?
|
|
157
|
+
? CallData2.toJSON(message.body?.callData)
|
|
120
158
|
: undefined)
|
|
121
159
|
return obj
|
|
122
160
|
},
|
|
@@ -130,7 +168,7 @@ export const Packet = {
|
|
|
130
168
|
) {
|
|
131
169
|
message.body = {
|
|
132
170
|
$case: 'callStart',
|
|
133
|
-
callStart:
|
|
171
|
+
callStart: CallStart1.fromPartial(object.body.callStart),
|
|
134
172
|
}
|
|
135
173
|
}
|
|
136
174
|
if (
|
|
@@ -140,7 +178,7 @@ export const Packet = {
|
|
|
140
178
|
) {
|
|
141
179
|
message.body = {
|
|
142
180
|
$case: 'callData',
|
|
143
|
-
callData:
|
|
181
|
+
callData: CallData2.fromPartial(object.body.callData),
|
|
144
182
|
}
|
|
145
183
|
}
|
|
146
184
|
return message
|
|
@@ -203,6 +241,42 @@ export const CallStart = {
|
|
|
203
241
|
return message
|
|
204
242
|
},
|
|
205
243
|
|
|
244
|
+
// encodeTransform encodes a source of message objects.
|
|
245
|
+
// Transform<CallStart, Uint8Array>
|
|
246
|
+
async *encodeTransform(
|
|
247
|
+
source:
|
|
248
|
+
| AsyncIterable<CallStart | CallStart[]>
|
|
249
|
+
| Iterable<CallStart | CallStart[]>
|
|
250
|
+
): AsyncIterable<Uint8Array> {
|
|
251
|
+
for await (const pkt of source) {
|
|
252
|
+
if (Array.isArray(pkt)) {
|
|
253
|
+
for (const p of pkt) {
|
|
254
|
+
yield* [CallStart.encode(p).finish()]
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
yield* [CallStart.encode(pkt).finish()]
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
// decodeTransform decodes a source of encoded messages.
|
|
263
|
+
// Transform<Uint8Array, CallStart>
|
|
264
|
+
async *decodeTransform(
|
|
265
|
+
source:
|
|
266
|
+
| AsyncIterable<Uint8Array | Uint8Array[]>
|
|
267
|
+
| Iterable<Uint8Array | Uint8Array[]>
|
|
268
|
+
): AsyncIterable<CallStart> {
|
|
269
|
+
for await (const pkt of source) {
|
|
270
|
+
if (Array.isArray(pkt)) {
|
|
271
|
+
for (const p of pkt) {
|
|
272
|
+
yield* [CallStart.decode(p)]
|
|
273
|
+
}
|
|
274
|
+
} else {
|
|
275
|
+
yield* [CallStart.decode(pkt)]
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
|
|
206
280
|
fromJSON(object: any): CallStart {
|
|
207
281
|
return {
|
|
208
282
|
rpcService: isSet(object.rpcService) ? String(object.rpcService) : '',
|
|
@@ -294,6 +368,42 @@ export const CallData = {
|
|
|
294
368
|
return message
|
|
295
369
|
},
|
|
296
370
|
|
|
371
|
+
// encodeTransform encodes a source of message objects.
|
|
372
|
+
// Transform<CallData, Uint8Array>
|
|
373
|
+
async *encodeTransform(
|
|
374
|
+
source:
|
|
375
|
+
| AsyncIterable<CallData | CallData[]>
|
|
376
|
+
| Iterable<CallData | CallData[]>
|
|
377
|
+
): AsyncIterable<Uint8Array> {
|
|
378
|
+
for await (const pkt of source) {
|
|
379
|
+
if (Array.isArray(pkt)) {
|
|
380
|
+
for (const p of pkt) {
|
|
381
|
+
yield* [CallData.encode(p).finish()]
|
|
382
|
+
}
|
|
383
|
+
} else {
|
|
384
|
+
yield* [CallData.encode(pkt).finish()]
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
|
|
389
|
+
// decodeTransform decodes a source of encoded messages.
|
|
390
|
+
// Transform<Uint8Array, CallData>
|
|
391
|
+
async *decodeTransform(
|
|
392
|
+
source:
|
|
393
|
+
| AsyncIterable<Uint8Array | Uint8Array[]>
|
|
394
|
+
| Iterable<Uint8Array | Uint8Array[]>
|
|
395
|
+
): AsyncIterable<CallData> {
|
|
396
|
+
for await (const pkt of source) {
|
|
397
|
+
if (Array.isArray(pkt)) {
|
|
398
|
+
for (const p of pkt) {
|
|
399
|
+
yield* [CallData.decode(p)]
|
|
400
|
+
}
|
|
401
|
+
} else {
|
|
402
|
+
yield* [CallData.decode(pkt)]
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
|
|
297
407
|
fromJSON(object: any): CallData {
|
|
298
408
|
return {
|
|
299
409
|
data: isSet(object.data)
|
package/srpc/server-pipe.go
CHANGED
|
@@ -14,9 +14,9 @@ func NewServerPipe(server *Server) OpenStreamFunc {
|
|
|
14
14
|
go func() {
|
|
15
15
|
_ = server.HandleStream(ctx, srvPipe)
|
|
16
16
|
}()
|
|
17
|
-
clientPrw := NewPacketReadWriter(clientPipe
|
|
17
|
+
clientPrw := NewPacketReadWriter(clientPipe)
|
|
18
18
|
go func() {
|
|
19
|
-
err := clientPrw.ReadPump()
|
|
19
|
+
err := clientPrw.ReadPump(msgHandler)
|
|
20
20
|
if err != nil {
|
|
21
21
|
_ = clientPrw.Close()
|
|
22
22
|
}
|
package/srpc/server.go
CHANGED
|
@@ -30,9 +30,9 @@ func (s *Server) HandleStream(ctx context.Context, rwc io.ReadWriteCloser) error
|
|
|
30
30
|
subCtx, subCtxCancel := context.WithCancel(ctx)
|
|
31
31
|
defer subCtxCancel()
|
|
32
32
|
serverRPC := NewServerRPC(subCtx, s.mux)
|
|
33
|
-
prw := NewPacketReadWriter(rwc
|
|
33
|
+
prw := NewPacketReadWriter(rwc)
|
|
34
34
|
serverRPC.SetWriter(prw)
|
|
35
|
-
err := prw.ReadPump()
|
|
35
|
+
err := prw.ReadPump(serverRPC.HandlePacket)
|
|
36
36
|
_ = rwc.Close()
|
|
37
37
|
return err
|
|
38
38
|
}
|
package/srpc/server.ts
CHANGED
|
@@ -48,6 +48,14 @@ export class Server implements StreamHandler {
|
|
|
48
48
|
return rpc
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
// handlePacketDuplex handles an incoming Uint8Array duplex.
|
|
52
|
+
// skips the packet length prefix transform.
|
|
53
|
+
public handlePacketDuplex(stream: Duplex<Uint8Array>): ServerRPC {
|
|
54
|
+
const rpc = this.startRpc()
|
|
55
|
+
pipe(stream, decodePacketSource, rpc, encodePacketSource, stream)
|
|
56
|
+
return rpc
|
|
57
|
+
}
|
|
58
|
+
|
|
51
59
|
// handlePacketStream handles an incoming Packet duplex.
|
|
52
60
|
public handlePacketStream(stream: Duplex<Packet>): ServerRPC {
|
|
53
61
|
const rpc = this.startRpc()
|
package/srpc/ts-proto-rpc.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { Observable } from 'rxjs'
|
|
2
|
-
|
|
3
1
|
// TsProtoRpc matches the Rpc interface generated by ts-proto.
|
|
4
2
|
export interface TsProtoRpc {
|
|
5
3
|
// request fires a one-off unary RPC request.
|
|
@@ -12,18 +10,18 @@ export interface TsProtoRpc {
|
|
|
12
10
|
clientStreamingRequest(
|
|
13
11
|
service: string,
|
|
14
12
|
method: string,
|
|
15
|
-
data:
|
|
13
|
+
data: AsyncIterable<Uint8Array>
|
|
16
14
|
): Promise<Uint8Array>
|
|
17
15
|
// serverStreamingRequest fires a one-way server->client streaming request.
|
|
18
16
|
serverStreamingRequest(
|
|
19
17
|
service: string,
|
|
20
18
|
method: string,
|
|
21
19
|
data: Uint8Array
|
|
22
|
-
):
|
|
20
|
+
): AsyncIterable<Uint8Array>
|
|
23
21
|
// bidirectionalStreamingRequest implements a two-way streaming request.
|
|
24
22
|
bidirectionalStreamingRequest(
|
|
25
23
|
service: string,
|
|
26
24
|
method: string,
|
|
27
|
-
data:
|
|
28
|
-
):
|
|
25
|
+
data: AsyncIterable<Uint8Array>
|
|
26
|
+
): AsyncIterable<Uint8Array>
|
|
29
27
|
}
|
package/srpc/websocket.go
CHANGED
|
@@ -49,9 +49,9 @@ func (w *WebSocketConn) OpenStream(ctx context.Context, msgHandler PacketHandler
|
|
|
49
49
|
return nil, err
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
rw := NewPacketReadWriter(muxedStream
|
|
52
|
+
rw := NewPacketReadWriter(muxedStream)
|
|
53
53
|
go func() {
|
|
54
|
-
err := rw.ReadPump()
|
|
54
|
+
err := rw.ReadPump(msgHandler)
|
|
55
55
|
if err != nil {
|
|
56
56
|
_ = rw.Close()
|
|
57
57
|
}
|
package/dist/echo/sever.d.ts
DELETED
|
File without changes
|
package/dist/echo/sever.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Source } from 'it-stream-types';
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
3
|
-
export declare class ObservableSource<T> {
|
|
4
|
-
readonly source: Source<T>;
|
|
5
|
-
private readonly _source;
|
|
6
|
-
private readonly subscription;
|
|
7
|
-
constructor(observable: Observable<T>);
|
|
8
|
-
close(err?: Error): void;
|
|
9
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { pushable } from 'it-pushable';
|
|
2
|
-
// ObservableSource wraps an Observable into a Source.
|
|
3
|
-
export class ObservableSource {
|
|
4
|
-
constructor(observable) {
|
|
5
|
-
const source = pushable({ objectMode: true });
|
|
6
|
-
this.source = source;
|
|
7
|
-
this._source = source;
|
|
8
|
-
this.subscription = observable.subscribe({
|
|
9
|
-
next: (value) => {
|
|
10
|
-
this._source.push(value);
|
|
11
|
-
},
|
|
12
|
-
error: (err) => {
|
|
13
|
-
this._source.end(err);
|
|
14
|
-
},
|
|
15
|
-
complete: () => {
|
|
16
|
-
this._source.end();
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
// close closes the subscription.
|
|
21
|
-
close(err) {
|
|
22
|
-
this._source.end(err);
|
|
23
|
-
this.subscription.unsubscribe();
|
|
24
|
-
}
|
|
25
|
-
}
|
package/echo/sever.ts
DELETED
|
File without changes
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Source } from 'it-stream-types'
|
|
2
|
-
import { pushable, Pushable } from 'it-pushable'
|
|
3
|
-
import { Observable, Subscription } from 'rxjs'
|
|
4
|
-
|
|
5
|
-
// ObservableSource wraps an Observable into a Source.
|
|
6
|
-
export class ObservableSource<T> {
|
|
7
|
-
// source is the source for observable objects.
|
|
8
|
-
public readonly source: Source<T>
|
|
9
|
-
// _source emits incoming data to the source.
|
|
10
|
-
private readonly _source: {
|
|
11
|
-
push: (val: T) => void
|
|
12
|
-
end: (err?: Error) => void
|
|
13
|
-
}
|
|
14
|
-
// subscription is the observable subscription
|
|
15
|
-
private readonly subscription: Subscription
|
|
16
|
-
|
|
17
|
-
constructor(observable: Observable<T>) {
|
|
18
|
-
const source: Pushable<T> = pushable({ objectMode: true })
|
|
19
|
-
this.source = source
|
|
20
|
-
this._source = source
|
|
21
|
-
|
|
22
|
-
this.subscription = observable.subscribe({
|
|
23
|
-
next: (value: T) => {
|
|
24
|
-
this._source.push(value)
|
|
25
|
-
},
|
|
26
|
-
error: (err) => {
|
|
27
|
-
this._source.end(err)
|
|
28
|
-
},
|
|
29
|
-
complete: () => {
|
|
30
|
-
this._source.end()
|
|
31
|
-
},
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// close closes the subscription.
|
|
36
|
-
public close(err?: Error) {
|
|
37
|
-
this._source.end(err)
|
|
38
|
-
this.subscription.unsubscribe()
|
|
39
|
-
}
|
|
40
|
-
}
|