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.
Files changed (55) hide show
  1. package/dist/e2e/mock/mock.pb.js +10 -6
  2. package/dist/echo/echo.pb.js +11 -7
  3. package/dist/rpcstream/pushable-sink.d.ts +0 -0
  4. package/dist/rpcstream/pushable-sink.js +1 -0
  5. package/dist/rpcstream/rpcstream.js +1 -1
  6. package/dist/rpcstream/rpcstream.pb.js +45 -25
  7. package/dist/srpc/client.js +3 -4
  8. package/dist/srpc/common-rpc.js +1 -1
  9. package/dist/srpc/conn.d.ts +1 -1
  10. package/dist/srpc/conn.js +16 -6
  11. package/dist/srpc/errors.d.ts +1 -0
  12. package/dist/srpc/errors.js +23 -0
  13. package/dist/srpc/index.d.ts +4 -2
  14. package/dist/srpc/index.js +4 -2
  15. package/dist/srpc/pushable.d.ts +2 -0
  16. package/dist/srpc/pushable.js +23 -0
  17. package/dist/srpc/rpcproto.pb.js +69 -31
  18. package/dist/srpc/server.d.ts +3 -4
  19. package/dist/srpc/server.js +11 -12
  20. package/e2e/mock/mock.pb.go +1 -1
  21. package/e2e/mock/mock.pb.ts +12 -6
  22. package/e2e/mock/mock_srpc.pb.go +1 -1
  23. package/e2e/mock/mock_vtproto.pb.go +13 -5
  24. package/echo/echo.pb.go +1 -1
  25. package/echo/echo.pb.ts +13 -7
  26. package/echo/echo_srpc.pb.go +1 -1
  27. package/echo/echo_vtproto.pb.go +13 -5
  28. package/go.mod +3 -3
  29. package/go.sum +6 -6
  30. package/package.json +18 -18
  31. package/srpc/client-prefix.go +7 -0
  32. package/srpc/client-set.go +18 -3
  33. package/srpc/client.go +19 -3
  34. package/srpc/client.ts +3 -9
  35. package/srpc/common-rpc.ts +1 -1
  36. package/srpc/conn.ts +22 -8
  37. package/srpc/errors.go +2 -0
  38. package/srpc/errors.ts +24 -0
  39. package/srpc/index.ts +9 -2
  40. package/srpc/message.go +12 -0
  41. package/srpc/muxed-conn.go +1 -1
  42. package/srpc/packet-rw.go +18 -15
  43. package/srpc/packet.go +19 -0
  44. package/srpc/pushable.ts +23 -0
  45. package/srpc/raw-stream-rwc.go +153 -0
  46. package/srpc/rpcproto.pb.go +1 -1
  47. package/srpc/rpcproto.pb.ts +89 -34
  48. package/srpc/rpcproto_vtproto.pb.go +35 -13
  49. package/srpc/server-pipe.go +1 -1
  50. package/srpc/server-rpc.go +9 -0
  51. package/srpc/server.go +2 -1
  52. package/srpc/server.ts +11 -13
  53. package/srpc/stream-rwc.go +85 -0
  54. package/srpc/stream.ts +1 -0
  55. 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.handleStream.bind(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
- // handleStream handles an incoming Uint8Array message duplex.
39
- public handleStream(stream: Stream): ServerRPC {
40
- return this.handleDuplex(stream)
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
- // handlePacketDuplex handles an incoming Uint8Array duplex.
61
- // skips the packet length prefix transform.
62
- public handlePacketDuplex(stream: Duplex<Uint8Array>): ServerRPC {
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
- // handlePacketStream handles an incoming Packet duplex.
69
- public handlePacketStream(stream: Duplex<Packet>): ServerRPC {
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.