starpc 0.23.2 → 0.24.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 (47) hide show
  1. package/dist/e2e/e2e.js +6 -5
  2. package/dist/rpcstream/rpcstream.d.ts +5 -5
  3. package/dist/rpcstream/rpcstream.js +1 -1
  4. package/dist/srpc/broadcast-channel.d.ts +9 -13
  5. package/dist/srpc/broadcast-channel.js +64 -30
  6. package/dist/srpc/client.js +11 -4
  7. package/dist/srpc/common-rpc.d.ts +4 -2
  8. package/dist/srpc/common-rpc.js +19 -7
  9. package/dist/srpc/conn.d.ts +16 -12
  10. package/dist/srpc/conn.js +35 -34
  11. package/dist/srpc/handler.d.ts +1 -4
  12. package/dist/srpc/handler.js +1 -67
  13. package/dist/srpc/index.d.ts +7 -7
  14. package/dist/srpc/index.js +5 -5
  15. package/dist/srpc/invoker.d.ts +4 -0
  16. package/dist/srpc/invoker.js +66 -0
  17. package/dist/srpc/message-port.d.ts +7 -9
  18. package/dist/srpc/message-port.js +56 -13
  19. package/dist/srpc/pushable.d.ts +1 -1
  20. package/dist/srpc/pushable.js +2 -14
  21. package/dist/srpc/server-rpc.js +1 -0
  22. package/dist/srpc/server.d.ts +2 -6
  23. package/dist/srpc/server.js +4 -17
  24. package/dist/srpc/stream.d.ts +5 -3
  25. package/dist/srpc/stream.js +16 -1
  26. package/dist/srpc/websocket.d.ts +2 -2
  27. package/dist/srpc/websocket.js +2 -2
  28. package/e2e/e2e.ts +8 -5
  29. package/package.json +1 -1
  30. package/srpc/broadcast-channel.ts +78 -47
  31. package/srpc/client.ts +10 -3
  32. package/srpc/common-rpc.go +1 -10
  33. package/srpc/common-rpc.ts +23 -8
  34. package/srpc/conn.ts +54 -58
  35. package/srpc/handler.ts +2 -92
  36. package/srpc/index.ts +18 -7
  37. package/srpc/invoker.ts +92 -0
  38. package/srpc/message-port.ts +71 -21
  39. package/srpc/open-stream-ctr.ts +2 -2
  40. package/srpc/pushable.ts +5 -17
  41. package/srpc/server-rpc.ts +1 -0
  42. package/srpc/server.ts +5 -37
  43. package/srpc/stream.ts +32 -7
  44. package/srpc/websocket.ts +2 -2
  45. package/dist/srpc/conn-stream.d.ts +0 -8
  46. package/dist/srpc/conn-stream.js +0 -16
  47. package/srpc/conn-stream.ts +0 -24
package/srpc/pushable.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Pushable } from 'it-pushable'
2
- import { Source, Sink } from 'it-stream-types'
2
+ import { Sink, Source } from 'it-stream-types'
3
3
 
4
4
  // writeToPushable writes the incoming server data to the pushable.
5
5
  export async function writeToPushable<T>(
@@ -23,14 +23,12 @@ export function buildPushableSink<T>(
23
23
  return async (source: Source<T>): Promise<void> => {
24
24
  try {
25
25
  if (Symbol.asyncIterator in source) {
26
- // Handle AsyncIterable
27
- for await (const pkt of source as AsyncIterable<any>) {
28
- processPacket(pkt, target)
26
+ for await (const pkt of source) {
27
+ target.push(pkt)
29
28
  }
30
29
  } else {
31
- // Handle Iterable
32
- for (const pkt of source as Iterable<any>) {
33
- processPacket(pkt, target)
30
+ for (const pkt of source) {
31
+ target.push(pkt)
34
32
  }
35
33
  }
36
34
  target.end()
@@ -39,13 +37,3 @@ export function buildPushableSink<T>(
39
37
  }
40
38
  }
41
39
  }
42
-
43
- function processPacket<T>(pkt: T, target: Pushable<T>): void {
44
- if (Array.isArray(pkt)) {
45
- for (const p of pkt) {
46
- target.push(p)
47
- }
48
- } else {
49
- target.push(pkt)
50
- }
51
- }
@@ -62,6 +62,7 @@ export class ServerRPC extends CommonRPC {
62
62
  await this.writeCallData(msg)
63
63
  }
64
64
  await this.writeCallData(undefined, true)
65
+ this.close()
65
66
  } catch (err) {
66
67
  this.close(err as Error)
67
68
  }
package/srpc/server.ts CHANGED
@@ -1,19 +1,11 @@
1
- import { Duplex, Source } from 'it-stream-types'
2
1
  import { pipe } from 'it-pipe'
3
2
 
4
3
  import { LookupMethod } from './mux.js'
5
4
  import { ServerRPC } from './server-rpc.js'
6
- import { Packet } from './rpcproto.pb.js'
7
- import {
8
- parseLengthPrefixTransform,
9
- prependLengthPrefixTransform,
10
- decodePacketSource,
11
- encodePacketSource,
12
- } from './packet.js'
5
+ import { decodePacketSource, encodePacketSource } from './packet.js'
13
6
  import { StreamHandler } from './conn.js'
14
- import { Stream } from './stream.js'
7
+ import { PacketStream } from './stream.js'
15
8
  import { RpcStreamHandler } from '../rpcstream/rpcstream.js'
16
- import { combineUint8ArrayListTransform } from './array-list.js'
17
9
 
18
10
  // Server implements the SRPC server in TypeScript with a Mux.
19
11
  export class Server implements StreamHandler {
@@ -36,37 +28,13 @@ export class Server implements StreamHandler {
36
28
  return new ServerRPC(this.lookupMethod)
37
29
  }
38
30
 
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 {
43
- const rpc = this.startRpc()
44
- pipe(
45
- stream,
46
- parseLengthPrefixTransform(),
47
- combineUint8ArrayListTransform(),
48
- decodePacketSource,
49
- rpc,
50
- encodePacketSource,
51
- prependLengthPrefixTransform(),
52
- combineUint8ArrayListTransform(),
53
- stream,
54
- )
55
- return rpc
56
- }
57
-
58
31
  // handlePacketStream handles an incoming Uint8Array duplex.
59
32
  // the stream has one Uint8Array per packet w/o length prefix.
60
- public handlePacketStream(stream: Stream): ServerRPC {
33
+ public handlePacketStream(stream: PacketStream): ServerRPC {
61
34
  const rpc = this.startRpc()
62
35
  pipe(stream, decodePacketSource, rpc, encodePacketSource, stream)
63
- return rpc
64
- }
65
-
66
- // handlePacketDuplex handles an incoming Packet duplex.
67
- public handlePacketDuplex(stream: Duplex<Source<Packet>>): ServerRPC {
68
- const rpc = this.startRpc()
69
- pipe(stream, rpc, stream)
36
+ .catch((err: Error) => rpc.close(err))
37
+ .then(() => rpc.close())
70
38
  return rpc
71
39
  }
72
40
  }
package/srpc/stream.ts CHANGED
@@ -1,16 +1,41 @@
1
- import type { Packet } from './rpcproto.pb.js'
2
1
  import type { Duplex, Source } from 'it-stream-types'
2
+ import { pipe } from 'it-pipe'
3
+ import { Stream } from '@libp2p/interface'
4
+
5
+ import type { Packet } from './rpcproto.pb.js'
6
+ import { combineUint8ArrayListTransform } from './array-list.js'
7
+ import {
8
+ parseLengthPrefixTransform,
9
+ prependLengthPrefixTransform,
10
+ } from './packet.js'
3
11
 
4
12
  // PacketHandler handles incoming packets.
5
13
  export type PacketHandler = (packet: Packet) => Promise<void>
6
14
 
7
- // Stream is an open connection.
8
- // This stream type assumes that each Uint8Array corresponds to a Packet.
9
- export type Stream<T = Uint8Array> = Duplex<
10
- AsyncGenerator<T>,
11
- Source<T>,
15
+ // PacketStream represents a stream of packets where each Uint8Array represents one packet.
16
+ export type PacketStream = Duplex<
17
+ AsyncGenerator<Uint8Array>,
18
+ Source<Uint8Array>,
12
19
  Promise<void>
13
20
  >
14
21
 
15
22
  // OpenStreamFunc is a function to start a new RPC by opening a Stream.
16
- export type OpenStreamFunc = () => Promise<Stream>
23
+ export type OpenStreamFunc = () => Promise<PacketStream>
24
+
25
+ // streamToPacketStream converts a Stream into a PacketStream using length-prefix framing.
26
+ //
27
+ // The stream is closed when the source writing to the sink ends.
28
+ export function streamToPacketStream(stream: Stream): PacketStream {
29
+ return {
30
+ source: pipe(
31
+ stream,
32
+ parseLengthPrefixTransform(),
33
+ combineUint8ArrayListTransform(),
34
+ ),
35
+ sink: async (source: Source<Uint8Array>): Promise<void> => {
36
+ await pipe(source, prependLengthPrefixTransform(), stream)
37
+ .catch((err) => stream.close(err))
38
+ .then(() => stream.close())
39
+ },
40
+ }
41
+ }
package/srpc/websocket.ts CHANGED
@@ -4,12 +4,12 @@ import { Direction } from '@libp2p/interface'
4
4
  import duplex from '@aptre/it-ws/duplex'
5
5
  import type WebSocket from '@aptre/it-ws/web-socket'
6
6
 
7
- import { Conn } from './conn.js'
7
+ import { StreamConn } from './conn.js'
8
8
  import { Server } from './server.js'
9
9
  import { combineUint8ArrayListTransform } from './array-list.js'
10
10
 
11
11
  // WebSocketConn implements a connection with a WebSocket and optional Server.
12
- export class WebSocketConn extends Conn {
12
+ export class WebSocketConn extends StreamConn {
13
13
  // socket is the web socket
14
14
  private socket: WebSocket
15
15
 
@@ -1,8 +0,0 @@
1
- import { Conn, ConnParams } from './conn.js';
2
- import { Server } from './server';
3
- import { Stream } from './stream.js';
4
- export declare class StreamConn extends Conn {
5
- private channel;
6
- constructor(duplex: Stream, server?: Server, connParams?: ConnParams);
7
- getChannelStream(): Stream;
8
- }
@@ -1,16 +0,0 @@
1
- import { pipe } from 'it-pipe';
2
- import { Conn } from './conn.js';
3
- // StreamConn wraps any Stream into a Conn.
4
- //
5
- // expects Uint8Array objects
6
- export class StreamConn extends Conn {
7
- constructor(duplex, server, connParams) {
8
- super(server, connParams);
9
- this.channel = duplex;
10
- pipe(this.channel, this, this.channel);
11
- }
12
- // getChannelStream returns the Duplex channel.
13
- getChannelStream() {
14
- return this.channel;
15
- }
16
- }
@@ -1,24 +0,0 @@
1
- import { pipe } from 'it-pipe'
2
-
3
- import { Conn, ConnParams } from './conn.js'
4
- import { Server } from './server'
5
- import { Stream } from './stream.js'
6
-
7
- // StreamConn wraps any Stream into a Conn.
8
- //
9
- // expects Uint8Array objects
10
- export class StreamConn extends Conn {
11
- // channel is the iterable
12
- private channel: Stream
13
-
14
- constructor(duplex: Stream, server?: Server, connParams?: ConnParams) {
15
- super(server, connParams)
16
- this.channel = duplex
17
- pipe(this.channel, this, this.channel)
18
- }
19
-
20
- // getChannelStream returns the Duplex channel.
21
- public getChannelStream(): Stream {
22
- return this.channel
23
- }
24
- }