starpc 0.3.1 → 0.3.4

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.
@@ -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);
@@ -4,6 +4,5 @@ export { Server } from './server.js';
4
4
  export { Conn, ConnParams } from './conn.js';
5
5
  export { Handler, InvokeFn, createHandler, createInvokeFn } from './handler.js';
6
6
  export { Mux, createMux } from './mux.js';
7
- export { WebSocketConn } from './websocket.js';
8
7
  export { BroadcastChannelIterable, newBroadcastChannelIterable, BroadcastChannelConn, } from './broadcast-channel.js';
9
8
  export { MessagePortIterable, newMessagePortIterable, MessagePortConn, } from './message-port.js';
@@ -3,6 +3,5 @@ export { Server } from './server.js';
3
3
  export { Conn } from './conn.js';
4
4
  export { createHandler, createInvokeFn } from './handler.js';
5
5
  export { createMux } from './mux.js';
6
- export { WebSocketConn } from './websocket.js';
7
6
  export { BroadcastChannelIterable, newBroadcastChannelIterable, BroadcastChannelConn, } from './broadcast-channel.js';
8
7
  export { MessagePortIterable, newMessagePortIterable, MessagePortConn, } from './message-port.js';
@@ -1,6 +1,9 @@
1
1
  #!/bin/bash
2
2
  set -eo pipefail
3
3
 
4
+ unset GOOS
5
+ unset GOARCH
6
+
4
7
  echo "Compiling ts..."
5
8
  # ../node_modules/.bin/tsc --out integration.js --project tsconfig.json
6
9
  ../node_modules/.bin/esbuild integration.ts --bundle --platform=node --outfile=integration.js
@@ -1,4 +1,4 @@
1
- import { WebSocketConn } from '../srpc'
1
+ import { WebSocketConn } from '../srpc/websocket'
2
2
  import { runClientTest } from '../echo'
3
3
  import WebSocket from 'isomorphic-ws'
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.3.1",
3
+ "version": "0.3.4",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
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,
package/srpc/index.ts CHANGED
@@ -4,7 +4,6 @@ export { Server } from './server.js'
4
4
  export { Conn, ConnParams } from './conn.js'
5
5
  export { Handler, InvokeFn, createHandler, createInvokeFn } from './handler.js'
6
6
  export { Mux, createMux } from './mux.js'
7
- export { WebSocketConn } from './websocket.js'
8
7
  export {
9
8
  BroadcastChannelIterable,
10
9
  newBroadcastChannelIterable,