starpc 0.18.2 → 0.19.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/mock/mock.pb.js +10 -6
- package/dist/echo/echo.pb.js +11 -7
- package/dist/rpcstream/pushable-sink.d.ts +0 -0
- package/dist/rpcstream/pushable-sink.js +1 -0
- package/dist/rpcstream/rpcstream.js +1 -1
- package/dist/rpcstream/rpcstream.pb.js +45 -25
- package/dist/srpc/client.js +3 -4
- package/dist/srpc/common-rpc.js +1 -1
- package/dist/srpc/conn.d.ts +1 -1
- package/dist/srpc/conn.js +16 -6
- package/dist/srpc/errors.d.ts +1 -0
- package/dist/srpc/errors.js +23 -0
- package/dist/srpc/index.d.ts +4 -2
- package/dist/srpc/index.js +4 -2
- package/dist/srpc/pushable.d.ts +2 -0
- package/dist/srpc/pushable.js +23 -0
- package/dist/srpc/rpcproto.pb.js +69 -31
- package/dist/srpc/server.d.ts +3 -4
- package/dist/srpc/server.js +11 -12
- package/e2e/mock/mock.pb.go +1 -1
- package/e2e/mock/mock.pb.ts +12 -6
- package/e2e/mock/mock_srpc.pb.go +1 -1
- package/e2e/mock/mock_vtproto.pb.go +13 -5
- package/echo/echo.pb.go +1 -1
- package/echo/echo.pb.ts +13 -7
- package/echo/echo_srpc.pb.go +1 -1
- package/echo/echo_vtproto.pb.go +13 -5
- package/go.mod +3 -3
- package/go.sum +6 -6
- package/package.json +18 -18
- package/srpc/client-prefix.go +7 -0
- package/srpc/client-set.go +18 -3
- package/srpc/client.go +19 -3
- package/srpc/client.ts +3 -9
- package/srpc/common-rpc.ts +1 -1
- package/srpc/conn.ts +22 -8
- package/srpc/errors.go +2 -0
- package/srpc/errors.ts +24 -0
- package/srpc/index.ts +9 -2
- package/srpc/message.go +12 -0
- package/srpc/muxed-conn.go +1 -1
- package/srpc/packet-rw.go +18 -15
- package/srpc/packet.go +19 -0
- package/srpc/pushable.ts +23 -0
- package/srpc/raw-stream-rwc.go +153 -0
- package/srpc/rpcproto.pb.go +1 -1
- package/srpc/rpcproto.pb.ts +89 -34
- package/srpc/rpcproto_vtproto.pb.go +35 -13
- package/srpc/server-pipe.go +1 -1
- package/srpc/server-rpc.go +9 -0
- package/srpc/server.go +2 -1
- package/srpc/server.ts +11 -13
- package/srpc/stream-rwc.go +85 -0
- package/srpc/stream.ts +1 -0
- package/srpc/writer.go +2 -0
package/srpc/server.ts
CHANGED
|
@@ -25,8 +25,9 @@ export class Server implements StreamHandler {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
// rpcStreamHandler implements the RpcStreamHandler interface.
|
|
28
|
+
// uses handlePacketDuplex (expects 1 buf = 1 Packet)
|
|
28
29
|
public get rpcStreamHandler(): RpcStreamHandler {
|
|
29
|
-
return this.
|
|
30
|
+
return this.handlePacketStream.bind(this)
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
// startRpc starts a new server-side RPC.
|
|
@@ -35,13 +36,10 @@ export class Server implements StreamHandler {
|
|
|
35
36
|
return new ServerRPC(this.lookupMethod)
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
// handleDuplex handles an incoming message duplex.
|
|
44
|
-
public handleDuplex(stream: Duplex<Uint8Array, Uint8Array>): ServerRPC {
|
|
39
|
+
// handleFragmentStream handles an incoming stream.
|
|
40
|
+
// assumes that stream does not maintain packet framing.
|
|
41
|
+
// uses length-prefixed packets for packet framing.
|
|
42
|
+
public handleFragmentStream(stream: Stream): ServerRPC {
|
|
45
43
|
const rpc = this.startRpc()
|
|
46
44
|
pipe(
|
|
47
45
|
stream,
|
|
@@ -57,16 +55,16 @@ export class Server implements StreamHandler {
|
|
|
57
55
|
return rpc
|
|
58
56
|
}
|
|
59
57
|
|
|
60
|
-
//
|
|
61
|
-
//
|
|
62
|
-
public
|
|
58
|
+
// handlePacketStream handles an incoming Uint8Array duplex.
|
|
59
|
+
// the stream has one Uint8Array per packet w/o length prefix.
|
|
60
|
+
public handlePacketStream(stream: Stream): ServerRPC {
|
|
63
61
|
const rpc = this.startRpc()
|
|
64
62
|
pipe(stream, decodePacketSource, rpc, encodePacketSource, stream)
|
|
65
63
|
return rpc
|
|
66
64
|
}
|
|
67
65
|
|
|
68
|
-
//
|
|
69
|
-
public
|
|
66
|
+
// handlePacketDuplex handles an incoming Packet duplex.
|
|
67
|
+
public handlePacketDuplex(stream: Duplex<Packet>): ServerRPC {
|
|
70
68
|
const rpc = this.startRpc()
|
|
71
69
|
pipe(stream, rpc, stream)
|
|
72
70
|
return rpc
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
package srpc
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"io"
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
// StreamRwc implements an io.ReadWriteCloser with a srpc.Stream.
|
|
9
|
+
type StreamRwc struct {
|
|
10
|
+
// Stream is the base stream interface.
|
|
11
|
+
Stream
|
|
12
|
+
|
|
13
|
+
// buf is the incoming data buffer
|
|
14
|
+
buf bytes.Buffer
|
|
15
|
+
// readMsg is the raw read message
|
|
16
|
+
readMsg RawMessage
|
|
17
|
+
// writeMsg is the raw write message
|
|
18
|
+
writeMsg RawMessage
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// NewStreamRwc constructs a new stream read write closer.
|
|
22
|
+
func NewStreamRwc(strm Stream) *StreamRwc {
|
|
23
|
+
rwc := &StreamRwc{Stream: strm}
|
|
24
|
+
rwc.readMsg.copy = true
|
|
25
|
+
rwc.writeMsg.copy = true
|
|
26
|
+
return rwc
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Read reads data from the stream to p.
|
|
30
|
+
// Implements io.Reader.
|
|
31
|
+
func (s *StreamRwc) Read(p []byte) (n int, err error) {
|
|
32
|
+
readBuf := p
|
|
33
|
+
for len(readBuf) != 0 && err == nil {
|
|
34
|
+
var rn int
|
|
35
|
+
|
|
36
|
+
// if the buffer has data, read from it.
|
|
37
|
+
if s.buf.Len() != 0 {
|
|
38
|
+
rn, err = s.buf.Read(readBuf)
|
|
39
|
+
} else {
|
|
40
|
+
if n != 0 {
|
|
41
|
+
// if we read data to p already, return now.
|
|
42
|
+
break
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
s.readMsg.Clear()
|
|
46
|
+
if err := s.Stream.MsgRecv(&s.readMsg); err != nil {
|
|
47
|
+
return n, err
|
|
48
|
+
}
|
|
49
|
+
data := s.readMsg.GetData()
|
|
50
|
+
if len(data) == 0 {
|
|
51
|
+
continue
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// read as much as possible directly to the output
|
|
55
|
+
copy(readBuf, data)
|
|
56
|
+
if len(data) > len(readBuf) {
|
|
57
|
+
// we read some of the data, buffer the rest.
|
|
58
|
+
rn = len(readBuf)
|
|
59
|
+
_, _ = s.buf.Write(data[rn:]) // never returns an error
|
|
60
|
+
} else {
|
|
61
|
+
// we read all of data
|
|
62
|
+
rn = len(data)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// advance readBuf by rn
|
|
67
|
+
n += rn
|
|
68
|
+
readBuf = readBuf[rn:]
|
|
69
|
+
}
|
|
70
|
+
return n, err
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Write writes data to the stream.
|
|
74
|
+
func (s *StreamRwc) Write(p []byte) (n int, err error) {
|
|
75
|
+
s.writeMsg.SetData(p)
|
|
76
|
+
err = s.Stream.MsgSend(&s.writeMsg)
|
|
77
|
+
s.writeMsg.Clear()
|
|
78
|
+
if err != nil {
|
|
79
|
+
return 0, err
|
|
80
|
+
}
|
|
81
|
+
return len(p), nil
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// _ is a type assertion
|
|
85
|
+
var _ io.ReadWriteCloser = ((*StreamRwc)(nil))
|
package/srpc/stream.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { Duplex } from 'it-stream-types'
|
|
|
5
5
|
export type PacketHandler = (packet: Packet) => Promise<void>
|
|
6
6
|
|
|
7
7
|
// Stream is an open connection.
|
|
8
|
+
// This stream type generally assumes that each Uint8Array corresponds to a Packet.
|
|
8
9
|
export type Stream = Duplex<Uint8Array>
|
|
9
10
|
|
|
10
11
|
// OpenStreamFunc is a function to start a new RPC by opening a Stream.
|
package/srpc/writer.go
CHANGED
|
@@ -2,6 +2,8 @@ package srpc
|
|
|
2
2
|
|
|
3
3
|
// Writer is the interface used to write messages to the remote.
|
|
4
4
|
type Writer interface {
|
|
5
|
+
// Write writes raw data to the remote.
|
|
6
|
+
Write(p []byte) (n int, err error)
|
|
5
7
|
// WritePacket writes a packet to the remote.
|
|
6
8
|
WritePacket(p *Packet) error
|
|
7
9
|
// Close closes the writer.
|