starpc 0.1.7 → 0.2.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 (73) hide show
  1. package/Makefile +1 -0
  2. package/README.md +24 -5
  3. package/dist/echo/client-test.d.ts +2 -0
  4. package/dist/echo/client-test.js +35 -0
  5. package/dist/echo/echo.d.ts +124 -0
  6. package/dist/echo/echo.js +42 -0
  7. package/dist/echo/index.d.ts +3 -0
  8. package/dist/echo/index.js +3 -0
  9. package/dist/echo/server.d.ts +8 -0
  10. package/dist/echo/server.js +50 -0
  11. package/dist/echo/sever.d.ts +0 -0
  12. package/dist/echo/sever.js +1 -0
  13. package/dist/srpc/broadcast-channel.d.ts +3 -2
  14. package/dist/srpc/broadcast-channel.js +2 -2
  15. package/dist/srpc/client-rpc.d.ts +4 -28
  16. package/dist/srpc/client-rpc.js +9 -152
  17. package/dist/srpc/client.d.ts +2 -2
  18. package/dist/srpc/client.js +56 -84
  19. package/dist/srpc/common-rpc.d.ts +24 -0
  20. package/dist/srpc/common-rpc.js +140 -0
  21. package/dist/srpc/conn.d.ts +8 -3
  22. package/dist/srpc/conn.js +19 -3
  23. package/dist/srpc/definition.d.ts +15 -0
  24. package/dist/srpc/definition.js +1 -0
  25. package/dist/srpc/handler.d.ts +24 -0
  26. package/dist/srpc/handler.js +123 -0
  27. package/dist/srpc/index.d.ts +7 -3
  28. package/dist/srpc/index.js +6 -2
  29. package/dist/srpc/message.d.ts +11 -0
  30. package/dist/srpc/message.js +32 -0
  31. package/dist/srpc/mux.d.ts +11 -0
  32. package/dist/srpc/mux.js +36 -0
  33. package/dist/srpc/packet.d.ts +4 -4
  34. package/dist/srpc/packet.js +38 -27
  35. package/dist/srpc/rpcproto.d.ts +18 -43
  36. package/dist/srpc/rpcproto.js +37 -78
  37. package/dist/srpc/server-rpc.d.ts +11 -0
  38. package/dist/srpc/server-rpc.js +55 -0
  39. package/dist/srpc/server.d.ts +14 -0
  40. package/dist/srpc/server.js +31 -0
  41. package/dist/srpc/websocket.d.ts +3 -2
  42. package/dist/srpc/websocket.js +4 -4
  43. package/e2e/README.md +10 -0
  44. package/e2e/e2e.ts +27 -0
  45. package/echo/client-test.ts +41 -0
  46. package/echo/echo.ts +45 -0
  47. package/echo/index.ts +3 -0
  48. package/echo/server.ts +57 -0
  49. package/echo/sever.ts +0 -0
  50. package/integration/integration.bash +1 -1
  51. package/integration/integration.ts +10 -42
  52. package/package.json +18 -17
  53. package/srpc/broadcast-channel.ts +8 -3
  54. package/srpc/client-rpc.go +1 -9
  55. package/srpc/client-rpc.ts +11 -175
  56. package/srpc/client.ts +58 -99
  57. package/srpc/common-rpc.ts +171 -0
  58. package/srpc/conn.ts +33 -5
  59. package/srpc/definition.ts +30 -0
  60. package/srpc/handler.ts +174 -0
  61. package/srpc/index.ts +7 -3
  62. package/srpc/message.ts +60 -0
  63. package/srpc/mux.ts +56 -0
  64. package/srpc/packet.go +4 -11
  65. package/srpc/packet.ts +44 -30
  66. package/srpc/rpcproto.pb.go +54 -118
  67. package/srpc/rpcproto.proto +7 -12
  68. package/srpc/rpcproto.ts +38 -101
  69. package/srpc/rpcproto_vtproto.pb.go +58 -210
  70. package/srpc/server-rpc.go +5 -10
  71. package/srpc/server-rpc.ts +65 -0
  72. package/srpc/server.ts +56 -0
  73. package/srpc/websocket.ts +6 -4
@@ -0,0 +1,65 @@
1
+ import type { Sink } from 'it-stream-types'
2
+
3
+ import type { CallData, CallStart } from './rpcproto.js'
4
+ import { CommonRPC } from './common-rpc.js'
5
+ import { InvokeFn } from './handler.js'
6
+ import { Mux } from './mux.js'
7
+
8
+ // ServerRPC is an ongoing RPC from the server side.
9
+ export class ServerRPC extends CommonRPC {
10
+ // mux is used to handle incoming rpcs.
11
+ private mux: Mux
12
+
13
+ constructor(mux: Mux) {
14
+ super()
15
+ this.mux = mux
16
+ }
17
+
18
+ // handleCallStart handles a CallStart cket.
19
+ public override async handleCallStart(packet: Partial<CallStart>) {
20
+ if (this.service || this.method) {
21
+ throw new Error('call start must be sent only once')
22
+ }
23
+ this.service = packet.rpcService
24
+ this.method = packet.rpcMethod
25
+ if (!this.service || !this.method) {
26
+ throw new Error('rpcService and rpcMethod cannot be empty')
27
+ }
28
+ const methodDef = await this.mux.lookupMethod(this.service, this.method)
29
+ if (!methodDef) {
30
+ throw new Error(`not found: ${this.service}/${this.method}`)
31
+ }
32
+ this.pushRpcData(packet.data, packet.dataIsZero)
33
+ this.invokeRPC(methodDef)
34
+ }
35
+
36
+ // handleCallData handles a CallData packet.
37
+ public override async handleCallData(packet: Partial<CallData>) {
38
+ if (!this.service || !this.method) {
39
+ throw new Error('call start must be sent before call data')
40
+ }
41
+ return super.handleCallData(packet)
42
+ }
43
+
44
+ // invokeRPC starts invoking the RPC handler.
45
+ private invokeRPC(invokeFn: InvokeFn) {
46
+ const dataSink = this._createDataSink()
47
+ invokeFn(this.rpcDataSource, dataSink).catch((err) => {
48
+ this.close(err)
49
+ })
50
+ }
51
+
52
+ // _createDataSink creates a sink for outgoing data packets.
53
+ private _createDataSink(): Sink<Uint8Array> {
54
+ return async (source) => {
55
+ try {
56
+ for await (const msg of source) {
57
+ await this.writeCallData(msg)
58
+ }
59
+ await this.writeCallData(undefined, true)
60
+ } catch (err) {
61
+ this.close(err as Error)
62
+ }
63
+ }
64
+ }
65
+ }
package/srpc/server.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { Stream } from '@libp2p/interface-connection'
2
+ import { Duplex } from 'it-stream-types'
3
+ import { pipe } from 'it-pipe'
4
+
5
+ import { Mux } from './mux.js'
6
+ import { ServerRPC } from './server-rpc.js'
7
+ import { Packet } from './rpcproto.js'
8
+ import {
9
+ parseLengthPrefixTransform,
10
+ prependLengthPrefixTransform,
11
+ decodePacketSource,
12
+ encodePacketSource,
13
+ } from './packet.js'
14
+ import { StreamHandler } from './conn.js'
15
+
16
+ // Server implements the SRPC server in TypeScript with a Mux.
17
+ export class Server implements StreamHandler {
18
+ // mux is the mux used to handle requests.
19
+ private mux: Mux
20
+
21
+ constructor(mux: Mux) {
22
+ this.mux = mux
23
+ }
24
+
25
+ // startRpc starts a new server-side RPC.
26
+ // the returned RPC handles incoming Packets.
27
+ public startRpc(): ServerRPC {
28
+ return new ServerRPC(this.mux)
29
+ }
30
+
31
+ // handleStream handles an incoming Uint8Array message duplex.
32
+ // closes the stream when the rpc completes.
33
+ public handleStream(stream: Stream): Promise<void> {
34
+ return this.handleDuplex(stream)
35
+ }
36
+
37
+ // handleDuplex handles an incoming message duplex.
38
+ public async handleDuplex(stream: Duplex<Uint8Array>): Promise<void> {
39
+ const rpc = this.startRpc()
40
+ await pipe(
41
+ stream,
42
+ parseLengthPrefixTransform(),
43
+ decodePacketSource,
44
+ rpc,
45
+ encodePacketSource,
46
+ prependLengthPrefixTransform(),
47
+ stream
48
+ )
49
+ }
50
+
51
+ // handlePacketStream handles an incoming Packet duplex.
52
+ public async handlePacketStream(stream: Duplex<Packet>): Promise<void> {
53
+ const rpc = this.startRpc()
54
+ await pipe(stream, rpc, stream)
55
+ }
56
+ }
package/srpc/websocket.ts CHANGED
@@ -1,16 +1,18 @@
1
- import { Conn } from './conn.js'
2
1
  import { duplex } from 'it-ws'
3
2
  import { pipe } from 'it-pipe'
4
3
  import { Mplex } from '@libp2p/mplex'
5
4
  import type WebSocket from 'isomorphic-ws'
6
5
 
7
- // WebSocketConn implements a connection with a WebSocket.
6
+ import { Conn } from './conn.js'
7
+ import { Server } from './server.js'
8
+
9
+ // WebSocketConn implements a connection with a WebSocket and optional Server.
8
10
  export class WebSocketConn extends Conn {
9
11
  // socket is the web socket
10
12
  private socket: WebSocket
11
13
 
12
- constructor(socket: WebSocket) {
13
- super({
14
+ constructor(socket: WebSocket, server?: Server) {
15
+ super(server, {
14
16
  muxerFactory: new Mplex(),
15
17
  })
16
18
  this.socket = socket