@dxos/rpc 0.5.1-main.ff7d242 → 0.5.1-next.0ef769d

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/rpc",
3
- "version": "0.5.1-main.ff7d242",
3
+ "version": "0.5.1-next.0ef769d",
4
4
  "description": "A lightweight, transport-agnostic RPC implementation",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -16,14 +16,14 @@
16
16
  "src"
17
17
  ],
18
18
  "dependencies": {
19
- "@dxos/debug": "0.5.1-main.ff7d242",
20
- "@dxos/async": "0.5.1-main.ff7d242",
21
- "@dxos/invariant": "0.5.1-main.ff7d242",
22
- "@dxos/log": "0.5.1-main.ff7d242",
23
- "@dxos/node-std": "0.5.1-main.ff7d242",
24
- "@dxos/codec-protobuf": "0.5.1-main.ff7d242",
25
- "@dxos/protocols": "0.5.1-main.ff7d242",
26
- "@dxos/util": "0.5.1-main.ff7d242"
19
+ "@dxos/async": "0.5.1-next.0ef769d",
20
+ "@dxos/codec-protobuf": "0.5.1-next.0ef769d",
21
+ "@dxos/invariant": "0.5.1-next.0ef769d",
22
+ "@dxos/debug": "0.5.1-next.0ef769d",
23
+ "@dxos/node-std": "0.5.1-next.0ef769d",
24
+ "@dxos/log": "0.5.1-next.0ef769d",
25
+ "@dxos/protocols": "0.5.1-next.0ef769d",
26
+ "@dxos/util": "0.5.1-next.0ef769d"
27
27
  },
28
28
  "devDependencies": {
29
29
  "earljs": "~0.1.10",
package/src/rpc.ts CHANGED
@@ -16,6 +16,8 @@ import { decodeRpcError } from './errors';
16
16
  const DEFAULT_TIMEOUT = 3_000;
17
17
  const BYE_SEND_TIMEOUT = 2_000;
18
18
 
19
+ const DEBUG_CALLS = true;
20
+
19
21
  type MaybePromise<T> = Promise<T> | T;
20
22
 
21
23
  export interface RpcPeerOptions {
@@ -115,7 +117,7 @@ export class RpcPeer {
115
117
  private readonly _byeTrigger = new Trigger();
116
118
 
117
119
  private _nextId = 0;
118
- private _state = RpcState.INITIAL;
120
+ private _state: RpcState = RpcState.INITIAL;
119
121
  private _unsubscribeFromPort: (() => void) | undefined = undefined;
120
122
  private _clearOpenInterval: (() => void) | undefined = undefined;
121
123
 
@@ -155,7 +157,7 @@ export class RpcPeer {
155
157
  return;
156
158
  }
157
159
 
158
- log('sending open message');
160
+ log('sending open message', { state: this._state });
159
161
  await this._sendMessage({ open: true });
160
162
 
161
163
  if (this._state !== RpcState.OPENING) {
@@ -170,16 +172,15 @@ export class RpcPeer {
170
172
  await Promise.race([this._remoteOpenTrigger.wait(), this._closingTrigger.wait()]);
171
173
 
172
174
  this._clearOpenInterval?.();
173
- this._state = RpcState.OPENED;
174
175
 
175
- if (this._state !== RpcState.OPENED) {
176
+ if ((this._state as RpcState) !== RpcState.OPENED) {
176
177
  // Closed while opening.
177
178
  return; // TODO(dmaretskyi): Throw error?
178
179
  }
179
180
 
180
181
  // TODO(burdon): This seems error prone.
181
182
  // Send an "open" message in case the other peer has missed our first "open" message and is still waiting.
182
- log('sending second open message');
183
+ log('sending second open message', { state: this._state });
183
184
  await this._sendMessage({ openAck: true });
184
185
  }
185
186
 
@@ -251,7 +252,7 @@ export class RpcPeer {
251
252
  */
252
253
  private async _receive(msg: Uint8Array): Promise<void> {
253
254
  const decoded = RpcMessage.decode(msg, { preserveAny: true });
254
- log('received message', { type: Object.keys(decoded)[0] });
255
+ DEBUG_CALLS && log('received message', { type: Object.keys(decoded)[0] });
255
256
 
256
257
  if (decoded.request) {
257
258
  if (this._state !== RpcState.OPENED && this._state !== RpcState.OPENING) {
@@ -281,14 +282,14 @@ export class RpcPeer {
281
282
  });
282
283
  });
283
284
  } else {
284
- log('request', { method: req.method });
285
+ DEBUG_CALLS && log('request', { method: req.method });
285
286
  const response = await this._callHandler(req);
286
-
287
- log('sending response', {
288
- method: req.method,
289
- response: response.payload?.type_url,
290
- error: response.error,
291
- });
287
+ DEBUG_CALLS &&
288
+ log('sending response', {
289
+ method: req.method,
290
+ response: response.payload?.type_url,
291
+ error: response.error,
292
+ });
292
293
  await this._sendMessage({ response });
293
294
  }
294
295
  } else if (decoded.response) {
@@ -310,21 +311,22 @@ export class RpcPeer {
310
311
  this._outgoingRequests.delete(responseId);
311
312
  }
312
313
 
313
- log('response', { type_url: decoded.response.payload?.type_url });
314
+ DEBUG_CALLS && log('response', { type_url: decoded.response.payload?.type_url });
314
315
  item.resolve(decoded.response);
315
316
  } else if (decoded.open) {
316
- log('received open message');
317
+ log('received open message', { state: this._state });
317
318
  if (this._params.noHandshake) {
318
319
  return;
319
320
  }
320
321
 
321
322
  await this._sendMessage({ openAck: true });
322
323
  } else if (decoded.openAck) {
323
- log('received openAck message');
324
+ log('received openAck message', { state: this._state });
324
325
  if (this._params.noHandshake) {
325
326
  return;
326
327
  }
327
328
 
329
+ this._state = RpcState.OPENED;
328
330
  this._remoteOpenTrigger.wake();
329
331
  } else if (decoded.streamClose) {
330
332
  if (this._state !== RpcState.OPENED) {
@@ -364,7 +366,7 @@ export class RpcPeer {
364
366
  * Peer should be open before making this call.
365
367
  */
366
368
  async call(method: string, request: Any, options?: RequestOptions): Promise<Any> {
367
- log('calling', { method });
369
+ DEBUG_CALLS && log('calling', { method });
368
370
  throwIfNotOpen(this._state);
369
371
 
370
372
  let response: Response;
@@ -469,7 +471,7 @@ export class RpcPeer {
469
471
  }
470
472
 
471
473
  private async _sendMessage(message: RpcMessage, timeout?: number) {
472
- log('sending message', { type: Object.keys(message)[0] });
474
+ DEBUG_CALLS && log('sending message', { type: Object.keys(message)[0] });
473
475
  await this._params.port.send(RpcMessage.encode(message, { preserveAny: true }), timeout);
474
476
  }
475
477