starpc 0.3.0 → 0.3.3

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.
@@ -8,6 +8,7 @@ export declare class BroadcastChannelIterable<T> implements Duplex<T> {
8
8
  sink: Sink<T>;
9
9
  source: AsyncIterable<T>;
10
10
  constructor(readChannel: BroadcastChannel, writeChannel: BroadcastChannel);
11
+ close(): void;
11
12
  private _createSink;
12
13
  private _createSource;
13
14
  }
@@ -17,4 +18,5 @@ export declare class BroadcastChannelConn extends DuplexConn {
17
18
  constructor(readChannel: BroadcastChannel, writeChannel: BroadcastChannel, server?: Server, connParams?: ConnParams);
18
19
  getReadChannel(): BroadcastChannel;
19
20
  getWriteChannel(): BroadcastChannel;
21
+ close(): void;
20
22
  }
@@ -16,6 +16,11 @@ export class BroadcastChannelIterable {
16
16
  this.sink = this._createSink();
17
17
  this.source = this._createSource();
18
18
  }
19
+ // close closes the broadcast channels.
20
+ close() {
21
+ this.readChannel.close();
22
+ this.writeChannel.close();
23
+ }
19
24
  // _createSink initializes the sink field.
20
25
  _createSink() {
21
26
  return async (source) => {
@@ -62,4 +67,8 @@ export class BroadcastChannelConn extends DuplexConn {
62
67
  getWriteChannel() {
63
68
  return this.broadcastChannel.writeChannel;
64
69
  }
70
+ // close closes the read and write channels.
71
+ close() {
72
+ this.broadcastChannel.close();
73
+ }
65
74
  }
@@ -3,7 +3,10 @@ import type { TsProtoRpc } from './ts-proto-rpc.js';
3
3
  import type { OpenStreamFunc } from './stream.js';
4
4
  export declare class Client implements TsProtoRpc {
5
5
  private openConnFn;
6
- constructor(openConnFn: OpenStreamFunc);
6
+ private _openConnFn?;
7
+ constructor(openConnFn?: OpenStreamFunc);
8
+ setOpenConnFn(openConnFn?: OpenStreamFunc): Promise<OpenStreamFunc>;
9
+ private initOpenConnFn;
7
10
  request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
8
11
  clientStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Promise<Uint8Array>;
9
12
  serverStreamingRequest(service: string, method: string, data: Uint8Array): Observable<Uint8Array>;
@@ -19,11 +19,45 @@ function writeClientStream(call, data) {
19
19
  }
20
20
  // Client implements the ts-proto Rpc interface with the drpcproto protocol.
21
21
  export class Client {
22
- // openConnFn is the open connection function.
23
- // called when starting RPC.
22
+ // openConnFn is a promise which contains the OpenStreamFunc.
24
23
  openConnFn;
24
+ // _openConnFn resolves openConnFn.
25
+ _openConnFn;
25
26
  constructor(openConnFn) {
26
- this.openConnFn = openConnFn;
27
+ this.openConnFn = this.setOpenConnFn(openConnFn);
28
+ }
29
+ // setOpenConnFn updates the openConnFn for the Client.
30
+ setOpenConnFn(openConnFn) {
31
+ if (this._openConnFn) {
32
+ if (openConnFn) {
33
+ this._openConnFn(openConnFn);
34
+ this._openConnFn = undefined;
35
+ }
36
+ }
37
+ else {
38
+ if (openConnFn) {
39
+ this.openConnFn = Promise.resolve(openConnFn);
40
+ }
41
+ else {
42
+ this.initOpenConnFn();
43
+ }
44
+ }
45
+ return this.openConnFn;
46
+ }
47
+ // initOpenConnFn creates the empty Promise for openConnFn.
48
+ initOpenConnFn() {
49
+ const openPromise = new Promise((resolve, reject) => {
50
+ this._openConnFn = (conn, err) => {
51
+ if (err) {
52
+ reject(err);
53
+ }
54
+ else if (conn) {
55
+ resolve(conn);
56
+ }
57
+ };
58
+ });
59
+ this.openConnFn = openPromise;
60
+ return this.openConnFn;
27
61
  }
28
62
  // request starts a non-streaming request.
29
63
  async request(service, method, data) {
@@ -101,7 +135,8 @@ export class Client {
101
135
  // throws any error starting the rpc call
102
136
  // if data == null and data.length == 0, sends a separate data packet.
103
137
  async startRpc(rpcService, rpcMethod, data) {
104
- const conn = await this.openConnFn();
138
+ const openConnFn = await this.openConnFn;
139
+ const conn = await openConnFn();
105
140
  const call = new ClientRPC(rpcService, rpcMethod);
106
141
  pipe(conn, parseLengthPrefixTransform(), decodePacketSource, call, encodePacketSource, prependLengthPrefixTransform(), conn);
107
142
  await call.writeCallStart(data || undefined);
@@ -8,6 +8,7 @@ export declare class MessagePortIterable<T> implements Duplex<T> {
8
8
  source: AsyncIterable<T>;
9
9
  private _source;
10
10
  constructor(port: MessagePort);
11
+ close(): void;
11
12
  private _createSink;
12
13
  private _createSource;
13
14
  }
@@ -16,4 +17,5 @@ export declare class MessagePortConn extends DuplexConn {
16
17
  private messagePort;
17
18
  constructor(port: MessagePort, server?: Server, connParams?: ConnParams);
18
19
  getMessagePort(): MessagePort;
20
+ close(): void;
19
21
  }
@@ -16,6 +16,10 @@ export class MessagePortIterable {
16
16
  this._source = this._createSource();
17
17
  this.source = this._source;
18
18
  }
19
+ // close closes the message port.
20
+ close() {
21
+ this.port.close();
22
+ }
19
23
  // _createSink initializes the sink field.
20
24
  _createSink() {
21
25
  return async (source) => {
@@ -58,4 +62,8 @@ export class MessagePortConn extends DuplexConn {
58
62
  getMessagePort() {
59
63
  return this.messagePort.port;
60
64
  }
65
+ // close closes the message port.
66
+ close() {
67
+ this.messagePort.close();
68
+ }
61
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.3.0",
3
+ "version": "0.3.3",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -23,6 +23,12 @@ export class BroadcastChannelIterable<T> implements Duplex<T> {
23
23
  this.source = this._createSource()
24
24
  }
25
25
 
26
+ // close closes the broadcast channels.
27
+ public close() {
28
+ this.readChannel.close()
29
+ this.writeChannel.close()
30
+ }
31
+
26
32
  // _createSink initializes the sink field.
27
33
  private _createSink(): Sink<T> {
28
34
  return async (source) => {
@@ -90,4 +96,9 @@ export class BroadcastChannelConn extends DuplexConn {
90
96
  public getWriteChannel(): BroadcastChannel {
91
97
  return this.broadcastChannel.writeChannel
92
98
  }
99
+
100
+ // close closes the read and write channels.
101
+ public close() {
102
+ this.broadcastChannel.close()
103
+ }
93
104
  }
package/srpc/client.ts CHANGED
@@ -29,12 +29,45 @@ function writeClientStream(call: ClientRPC, data: Observable<Uint8Array>) {
29
29
 
30
30
  // Client implements the ts-proto Rpc interface with the drpcproto protocol.
31
31
  export class Client implements TsProtoRpc {
32
- // openConnFn is the open connection function.
33
- // called when starting RPC.
34
- private openConnFn: OpenStreamFunc
32
+ // openConnFn is a promise which contains the OpenStreamFunc.
33
+ private openConnFn: Promise<OpenStreamFunc>
34
+ // _openConnFn resolves openConnFn.
35
+ private _openConnFn?: (conn?: OpenStreamFunc, err?: Error) => void
35
36
 
36
- constructor(openConnFn: OpenStreamFunc) {
37
- this.openConnFn = openConnFn
37
+ constructor(openConnFn?: OpenStreamFunc) {
38
+ this.openConnFn = this.setOpenConnFn(openConnFn)
39
+ }
40
+
41
+ // setOpenConnFn updates the openConnFn for the Client.
42
+ public setOpenConnFn(openConnFn?: OpenStreamFunc): Promise<OpenStreamFunc> {
43
+ if (this._openConnFn) {
44
+ if (openConnFn) {
45
+ this._openConnFn(openConnFn)
46
+ this._openConnFn = undefined
47
+ }
48
+ } else {
49
+ if (openConnFn) {
50
+ this.openConnFn = Promise.resolve(openConnFn)
51
+ } else {
52
+ this.initOpenConnFn()
53
+ }
54
+ }
55
+ return this.openConnFn
56
+ }
57
+
58
+ // initOpenConnFn creates the empty Promise for openConnFn.
59
+ private initOpenConnFn(): Promise<OpenStreamFunc> {
60
+ const openPromise = new Promise<OpenStreamFunc>((resolve, reject) => {
61
+ this._openConnFn = (conn?: OpenStreamFunc, err?: Error) => {
62
+ if (err) {
63
+ reject(err)
64
+ } else if (conn) {
65
+ resolve(conn)
66
+ }
67
+ }
68
+ })
69
+ this.openConnFn = openPromise
70
+ return this.openConnFn
38
71
  }
39
72
 
40
73
  // request starts a non-streaming request.
@@ -135,7 +168,8 @@ export class Client implements TsProtoRpc {
135
168
  rpcMethod: string,
136
169
  data: Uint8Array | null
137
170
  ): Promise<ClientRPC> {
138
- const conn = await this.openConnFn()
171
+ const openConnFn = await this.openConnFn
172
+ const conn = await openConnFn()
139
173
  const call = new ClientRPC(rpcService, rpcMethod)
140
174
  pipe(
141
175
  conn,
@@ -23,6 +23,11 @@ export class MessagePortIterable<T> implements Duplex<T> {
23
23
  this.source = this._source
24
24
  }
25
25
 
26
+ // close closes the message port.
27
+ public close() {
28
+ this.port.close()
29
+ }
30
+
26
31
  // _createSink initializes the sink field.
27
32
  private _createSink(): Sink<T> {
28
33
  return async (source) => {
@@ -73,4 +78,9 @@ export class MessagePortConn extends DuplexConn {
73
78
  public getMessagePort(): MessagePort {
74
79
  return this.messagePort.port
75
80
  }
81
+
82
+ // close closes the message port.
83
+ public close() {
84
+ this.messagePort.close()
85
+ }
76
86
  }