@dxos/rpc 0.8.4-main.406dc2a → 0.8.4-main.422d1c7879

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/README.md CHANGED
@@ -4,8 +4,8 @@ A lightweight, transport-agnostic RPC implementation.
4
4
 
5
5
  ## Features
6
6
 
7
- * Performs a handshake to wait for the other side to be open.
8
- * Errors are serialized and transported with their stack intact.
9
- * Requests are terminated on close.
10
- * TODO: Request timeouts.
11
- * TODO: Data streams.
7
+ - Performs a handshake to wait for the other side to be open.
8
+ - Errors are serialized and transported with their stack intact.
9
+ - Requests are terminated on close.
10
+ - TODO: Request timeouts.
11
+ - TODO: Data streams.
@@ -12,25 +12,13 @@ var decodeRpcError = (err, rpcMethod) => decodeError(err, {
12
12
  // src/rpc.ts
13
13
  import { Trigger, asyncTimeout, synchronized } from "@dxos/async";
14
14
  import { Stream } from "@dxos/codec-protobuf";
15
+ import { ContextRpcCodec } from "@dxos/context";
15
16
  import { StackTrace as StackTrace2 } from "@dxos/debug";
16
17
  import { invariant } from "@dxos/invariant";
17
18
  import { log } from "@dxos/log";
18
19
  import { RpcClosedError, RpcNotOpenError, encodeError } from "@dxos/protocols";
19
20
  import { schema } from "@dxos/protocols/proto";
20
21
  import { exponentialBackoffInterval } from "@dxos/util";
21
- function _define_property(obj, key, value) {
22
- if (key in obj) {
23
- Object.defineProperty(obj, key, {
24
- value,
25
- enumerable: true,
26
- configurable: true,
27
- writable: true
28
- });
29
- } else {
30
- obj[key] = value;
31
- }
32
- return obj;
33
- }
34
22
  function _ts_decorate(decorators, target, key, desc) {
35
23
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
36
24
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -38,23 +26,47 @@ function _ts_decorate(decorators, target, key, desc) {
38
26
  return c > 3 && r && Object.defineProperty(target, key, r), r;
39
27
  }
40
28
  var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/rpc/src/rpc.ts";
41
- var DEFAULT_TIMEOUT = 3e3;
29
+ var DEFAULT_TIMEOUT = 3e4;
42
30
  var BYE_SEND_TIMEOUT = 2e3;
43
31
  var DEBUG_CALLS = true;
44
32
  var CLOSE_TIMEOUT = 3e3;
45
33
  var PendingRpcRequest = class {
34
+ resolve;
35
+ reject;
36
+ stream;
46
37
  constructor(resolve, reject, stream) {
47
- _define_property(this, "resolve", void 0);
48
- _define_property(this, "reject", void 0);
49
- _define_property(this, "stream", void 0);
50
38
  this.resolve = resolve;
51
39
  this.reject = reject;
52
40
  this.stream = stream;
53
41
  }
54
42
  };
55
43
  var RpcMessageCodec;
56
- var getRpcMessageCodec = () => RpcMessageCodec ?? (RpcMessageCodec = schema.getCodecForType("dxos.rpc.RpcMessage"));
44
+ var getRpcMessageCodec = () => RpcMessageCodec ??= schema.getCodecForType("dxos.rpc.RpcMessage");
57
45
  var RpcPeer = class {
46
+ _params;
47
+ _outgoingRequests = /* @__PURE__ */ new Map();
48
+ _localStreams = /* @__PURE__ */ new Map();
49
+ _remoteOpenTrigger = new Trigger();
50
+ /**
51
+ * Triggered when the peer starts closing.
52
+ */
53
+ _closingTrigger = new Trigger();
54
+ /**
55
+ * Triggered when peer receives a bye message.
56
+ */
57
+ _byeTrigger = new Trigger();
58
+ _nextId = 0;
59
+ _state = "INITIAL";
60
+ _unsubscribeFromPort = void 0;
61
+ _clearOpenInterval = void 0;
62
+ constructor(params) {
63
+ this._params = {
64
+ timeout: void 0,
65
+ streamHandler: void 0,
66
+ noHandshake: false,
67
+ ...params
68
+ };
69
+ }
58
70
  /**
59
71
  * Open the peer. Required before making any calls.
60
72
  *
@@ -70,7 +82,7 @@ var RpcPeer = class {
70
82
  } catch (err) {
71
83
  log.catch(err, void 0, {
72
84
  F: __dxlog_file,
73
- L: 156,
85
+ L: 157,
74
86
  S: this,
75
87
  C: (f, a) => f(...a)
76
88
  });
@@ -86,7 +98,7 @@ var RpcPeer = class {
86
98
  state: this._state
87
99
  }, {
88
100
  F: __dxlog_file,
89
- L: 168,
101
+ L: 169,
90
102
  S: this,
91
103
  C: (f, a) => f(...a)
92
104
  });
@@ -101,7 +113,7 @@ var RpcPeer = class {
101
113
  open: true
102
114
  }).catch((err) => log.warn(err, void 0, {
103
115
  F: __dxlog_file,
104
- L: 177,
116
+ L: 178,
105
117
  S: this,
106
118
  C: (f, a) => f(...a)
107
119
  }));
@@ -118,7 +130,7 @@ var RpcPeer = class {
118
130
  state: this._state
119
131
  }, {
120
132
  F: __dxlog_file,
121
- L: 191,
133
+ L: 192,
122
134
  S: this,
123
135
  C: (f, a) => f(...a)
124
136
  });
@@ -148,7 +160,7 @@ var RpcPeer = class {
148
160
  err
149
161
  }, {
150
162
  F: __dxlog_file,
151
- L: 213,
163
+ L: 214,
152
164
  S: this,
153
165
  C: (f, a) => f(...a)
154
166
  });
@@ -156,7 +168,7 @@ var RpcPeer = class {
156
168
  try {
157
169
  log("closing waiting on bye", void 0, {
158
170
  F: __dxlog_file,
159
- L: 216,
171
+ L: 217,
160
172
  S: this,
161
173
  C: (f, a) => f(...a)
162
174
  });
@@ -168,7 +180,7 @@ var RpcPeer = class {
168
180
  err
169
181
  }, {
170
182
  F: __dxlog_file,
171
- L: 219,
183
+ L: 220,
172
184
  S: this,
173
185
  C: (f, a) => f(...a)
174
186
  });
@@ -212,7 +224,7 @@ var RpcPeer = class {
212
224
  type: Object.keys(decoded)[0]
213
225
  }, {
214
226
  F: __dxlog_file,
215
- L: 263,
227
+ L: 264,
216
228
  S: this,
217
229
  C: (f, a) => f(...a)
218
230
  });
@@ -220,7 +232,7 @@ var RpcPeer = class {
220
232
  if (this._state !== "OPENED" && this._state !== "OPENING") {
221
233
  log("received request while closed", void 0, {
222
234
  F: __dxlog_file,
223
- L: 267,
235
+ L: 268,
224
236
  S: this,
225
237
  C: (f, a) => f(...a)
226
238
  });
@@ -238,7 +250,7 @@ var RpcPeer = class {
238
250
  method: req.method
239
251
  }, {
240
252
  F: __dxlog_file,
241
- L: 279,
253
+ L: 280,
242
254
  S: this,
243
255
  C: (f, a) => f(...a)
244
256
  });
@@ -250,7 +262,7 @@ var RpcPeer = class {
250
262
  close: response.close
251
263
  }, {
252
264
  F: __dxlog_file,
253
- L: 281,
265
+ L: 282,
254
266
  S: this,
255
267
  C: (f, a) => f(...a)
256
268
  });
@@ -259,7 +271,7 @@ var RpcPeer = class {
259
271
  }).catch((err) => {
260
272
  log.warn("failed during close", err, {
261
273
  F: __dxlog_file,
262
- L: 289,
274
+ L: 290,
263
275
  S: this,
264
276
  C: (f, a) => f(...a)
265
277
  });
@@ -270,7 +282,7 @@ var RpcPeer = class {
270
282
  method: req.method
271
283
  }, {
272
284
  F: __dxlog_file,
273
- L: 293,
285
+ L: 294,
274
286
  S: this,
275
287
  C: (f, a) => f(...a)
276
288
  });
@@ -281,7 +293,7 @@ var RpcPeer = class {
281
293
  error: response.error
282
294
  }, {
283
295
  F: __dxlog_file,
284
- L: 296,
296
+ L: 297,
285
297
  S: this,
286
298
  C: (f, a) => f(...a)
287
299
  });
@@ -293,7 +305,7 @@ var RpcPeer = class {
293
305
  if (this._state !== "OPENED") {
294
306
  log("received response while closed", void 0, {
295
307
  F: __dxlog_file,
296
- L: 305,
308
+ L: 306,
297
309
  S: this,
298
310
  C: (f, a) => f(...a)
299
311
  });
@@ -302,7 +314,7 @@ var RpcPeer = class {
302
314
  const responseId = decoded.response.id;
303
315
  invariant(typeof responseId === "number", void 0, {
304
316
  F: __dxlog_file,
305
- L: 310,
317
+ L: 311,
306
318
  S: this,
307
319
  A: [
308
320
  "typeof responseId === 'number'",
@@ -314,7 +326,7 @@ var RpcPeer = class {
314
326
  responseId
315
327
  }, {
316
328
  F: __dxlog_file,
317
- L: 312,
329
+ L: 313,
318
330
  S: this,
319
331
  C: (f, a) => f(...a)
320
332
  });
@@ -328,7 +340,7 @@ var RpcPeer = class {
328
340
  type_url: decoded.response.payload?.type_url
329
341
  }, {
330
342
  F: __dxlog_file,
331
- L: 322,
343
+ L: 323,
332
344
  S: this,
333
345
  C: (f, a) => f(...a)
334
346
  });
@@ -338,7 +350,7 @@ var RpcPeer = class {
338
350
  state: this._state
339
351
  }, {
340
352
  F: __dxlog_file,
341
- L: 325,
353
+ L: 326,
342
354
  S: this,
343
355
  C: (f, a) => f(...a)
344
356
  });
@@ -353,7 +365,7 @@ var RpcPeer = class {
353
365
  state: this._state
354
366
  }, {
355
367
  F: __dxlog_file,
356
- L: 332,
368
+ L: 333,
357
369
  S: this,
358
370
  C: (f, a) => f(...a)
359
371
  });
@@ -366,7 +378,7 @@ var RpcPeer = class {
366
378
  if (this._state !== "OPENED") {
367
379
  log("received stream close while closed", void 0, {
368
380
  F: __dxlog_file,
369
- L: 341,
381
+ L: 342,
370
382
  S: this,
371
383
  C: (f, a) => f(...a)
372
384
  });
@@ -376,13 +388,13 @@ var RpcPeer = class {
376
388
  id: decoded.streamClose.id
377
389
  }, {
378
390
  F: __dxlog_file,
379
- L: 345,
391
+ L: 346,
380
392
  S: this,
381
393
  C: (f, a) => f(...a)
382
394
  });
383
395
  invariant(typeof decoded.streamClose.id === "number", void 0, {
384
396
  F: __dxlog_file,
385
- L: 346,
397
+ L: 347,
386
398
  S: this,
387
399
  A: [
388
400
  "typeof decoded.streamClose.id === 'number'",
@@ -395,7 +407,7 @@ var RpcPeer = class {
395
407
  id: decoded.streamClose.id
396
408
  }, {
397
409
  F: __dxlog_file,
398
- L: 349,
410
+ L: 350,
399
411
  S: this,
400
412
  C: (f, a) => f(...a)
401
413
  });
@@ -408,7 +420,7 @@ var RpcPeer = class {
408
420
  if (this._state !== "CLOSING" && this._state !== "CLOSED") {
409
421
  log("replying to bye", void 0, {
410
422
  F: __dxlog_file,
411
- L: 359,
423
+ L: 360,
412
424
  S: this,
413
425
  C: (f, a) => f(...a)
414
426
  });
@@ -424,7 +436,7 @@ var RpcPeer = class {
424
436
  msg
425
437
  }, {
426
438
  F: __dxlog_file,
427
- L: 367,
439
+ L: 368,
428
440
  S: this,
429
441
  C: (f, a) => f(...a)
430
442
  });
@@ -440,7 +452,7 @@ var RpcPeer = class {
440
452
  method
441
453
  }, {
442
454
  F: __dxlog_file,
443
- L: 377,
455
+ L: 378,
444
456
  S: this,
445
457
  C: (f, a) => f(...a)
446
458
  });
@@ -451,12 +463,28 @@ var RpcPeer = class {
451
463
  const responseReceived = new Promise((resolve, reject) => {
452
464
  this._outgoingRequests.set(id, new PendingRpcRequest(resolve, reject, false));
453
465
  });
466
+ let traceContext;
467
+ try {
468
+ traceContext = options?.ctx ? ContextRpcCodec.encode(options.ctx) : void 0;
469
+ } catch (err) {
470
+ log.warn("failed to encode trace context", {
471
+ err
472
+ }, {
473
+ F: __dxlog_file,
474
+ L: 393,
475
+ S: this,
476
+ C: (f, a) => f(...a)
477
+ });
478
+ }
454
479
  const sending = this._sendMessage({
455
480
  request: {
456
481
  id,
457
482
  method,
458
483
  payload: request,
459
- stream: false
484
+ stream: false,
485
+ ...traceContext ? {
486
+ traceContext
487
+ } : {}
460
488
  }
461
489
  });
462
490
  const timeout = options?.timeout ?? this._params.timeout;
@@ -468,7 +496,7 @@ var RpcPeer = class {
468
496
  response = await waiting;
469
497
  invariant(response.id === id, void 0, {
470
498
  F: __dxlog_file,
471
- L: 405,
499
+ L: 414,
472
500
  S: this,
473
501
  A: [
474
502
  "response.id === id",
@@ -529,16 +557,38 @@ ${stack.getStack()}`;
529
557
  }
530
558
  };
531
559
  this._outgoingRequests.set(id, new PendingRpcRequest(onResponse, closeStream, true));
532
- this._sendMessage({
533
- request: {
534
- id,
535
- method,
536
- payload: request,
537
- stream: true
538
- }
539
- }).catch((err) => {
540
- close(err);
541
- });
560
+ let traceContext;
561
+ try {
562
+ traceContext = options?.ctx ? ContextRpcCodec.encode(options.ctx) : void 0;
563
+ } catch (err) {
564
+ log.warn("failed to encode trace context", {
565
+ err
566
+ }, {
567
+ F: __dxlog_file,
568
+ L: 476,
569
+ S: this,
570
+ C: (f, a) => f(...a)
571
+ });
572
+ }
573
+ try {
574
+ this._sendMessage({
575
+ request: {
576
+ id,
577
+ method,
578
+ payload: request,
579
+ stream: true,
580
+ ...traceContext ? {
581
+ traceContext
582
+ } : {}
583
+ }
584
+ }).catch((err) => {
585
+ this._outgoingRequests.delete(id);
586
+ close(err);
587
+ });
588
+ } catch (err) {
589
+ this._outgoingRequests.delete(id);
590
+ throw err;
591
+ }
542
592
  return () => {
543
593
  this._sendMessage({
544
594
  streamClose: {
@@ -547,7 +597,7 @@ ${stack.getStack()}`;
547
597
  }).catch((err) => {
548
598
  log.catch(err, void 0, {
549
599
  F: __dxlog_file,
550
- L: 478,
600
+ L: 501,
551
601
  S: this,
552
602
  C: (f, a) => f(...a)
553
603
  });
@@ -561,7 +611,7 @@ ${stack.getStack()}`;
561
611
  type: Object.keys(message)[0]
562
612
  }, {
563
613
  F: __dxlog_file,
564
- L: 486,
614
+ L: 509,
565
615
  S: this,
566
616
  C: (f, a) => f(...a)
567
617
  });
@@ -569,11 +619,38 @@ ${stack.getStack()}`;
569
619
  preserveAny: true
570
620
  }), timeout);
571
621
  }
622
+ _getHandlerRpcOptions(req) {
623
+ let traceCtx;
624
+ if (req.traceContext) {
625
+ try {
626
+ traceCtx = ContextRpcCodec.decode(req.traceContext);
627
+ } catch (err) {
628
+ log.warn("failed to decode trace context", {
629
+ traceContext: req.traceContext,
630
+ err
631
+ }, {
632
+ F: __dxlog_file,
633
+ L: 519,
634
+ S: this,
635
+ C: (f, a) => f(...a)
636
+ });
637
+ }
638
+ }
639
+ if (!traceCtx && !this._params.handlerRpcOptions) {
640
+ return void 0;
641
+ }
642
+ return {
643
+ ...this._params.handlerRpcOptions,
644
+ ...traceCtx ? {
645
+ ctx: traceCtx
646
+ } : {}
647
+ };
648
+ }
572
649
  async _callHandler(req) {
573
650
  try {
574
651
  invariant(typeof req.id === "number", void 0, {
575
652
  F: __dxlog_file,
576
- L: 492,
653
+ L: 530,
577
654
  S: this,
578
655
  A: [
579
656
  "typeof req.id === 'number'",
@@ -582,7 +659,7 @@ ${stack.getStack()}`;
582
659
  });
583
660
  invariant(req.payload, void 0, {
584
661
  F: __dxlog_file,
585
- L: 493,
662
+ L: 531,
586
663
  S: this,
587
664
  A: [
588
665
  "req.payload",
@@ -591,14 +668,14 @@ ${stack.getStack()}`;
591
668
  });
592
669
  invariant(req.method, void 0, {
593
670
  F: __dxlog_file,
594
- L: 494,
671
+ L: 532,
595
672
  S: this,
596
673
  A: [
597
674
  "req.method",
598
675
  ""
599
676
  ]
600
677
  });
601
- const response = await this._params.callHandler(req.method, req.payload, this._params.handlerRpcOptions);
678
+ const response = await this._params.callHandler(req.method, req.payload, this._getHandlerRpcOptions(req));
602
679
  return {
603
680
  id: req.id,
604
681
  payload: response
@@ -614,7 +691,7 @@ ${stack.getStack()}`;
614
691
  try {
615
692
  invariant(this._params.streamHandler, "Requests with streaming responses are not supported.", {
616
693
  F: __dxlog_file,
617
- L: 511,
694
+ L: 549,
618
695
  S: this,
619
696
  A: [
620
697
  "this._params.streamHandler",
@@ -623,7 +700,7 @@ ${stack.getStack()}`;
623
700
  });
624
701
  invariant(typeof req.id === "number", void 0, {
625
702
  F: __dxlog_file,
626
- L: 512,
703
+ L: 550,
627
704
  S: this,
628
705
  A: [
629
706
  "typeof req.id === 'number'",
@@ -632,7 +709,7 @@ ${stack.getStack()}`;
632
709
  });
633
710
  invariant(req.payload, void 0, {
634
711
  F: __dxlog_file,
635
- L: 513,
712
+ L: 551,
636
713
  S: this,
637
714
  A: [
638
715
  "req.payload",
@@ -641,14 +718,14 @@ ${stack.getStack()}`;
641
718
  });
642
719
  invariant(req.method, void 0, {
643
720
  F: __dxlog_file,
644
- L: 514,
721
+ L: 552,
645
722
  S: this,
646
723
  A: [
647
724
  "req.method",
648
725
  ""
649
726
  ]
650
727
  });
651
- const responseStream = this._params.streamHandler(req.method, req.payload, this._params.handlerRpcOptions);
728
+ const responseStream = this._params.streamHandler(req.method, req.payload, this._getHandlerRpcOptions(req));
652
729
  responseStream.onReady(() => {
653
730
  callback({
654
731
  id: req.id,
@@ -681,24 +758,6 @@ ${stack.getStack()}`;
681
758
  });
682
759
  }
683
760
  }
684
- constructor(params) {
685
- _define_property(this, "_params", void 0);
686
- _define_property(this, "_outgoingRequests", /* @__PURE__ */ new Map());
687
- _define_property(this, "_localStreams", /* @__PURE__ */ new Map());
688
- _define_property(this, "_remoteOpenTrigger", new Trigger());
689
- _define_property(this, "_closingTrigger", new Trigger());
690
- _define_property(this, "_byeTrigger", new Trigger());
691
- _define_property(this, "_nextId", 0);
692
- _define_property(this, "_state", "INITIAL");
693
- _define_property(this, "_unsubscribeFromPort", void 0);
694
- _define_property(this, "_clearOpenInterval", void 0);
695
- this._params = {
696
- timeout: void 0,
697
- streamHandler: void 0,
698
- noHandshake: false,
699
- ...params
700
- };
701
- }
702
761
  };
703
762
  _ts_decorate([
704
763
  synchronized
@@ -719,22 +778,15 @@ var throwIfNotOpen = (state) => {
719
778
 
720
779
  // src/service.ts
721
780
  import { invariant as invariant2 } from "@dxos/invariant";
722
- function _define_property2(obj, key, value) {
723
- if (key in obj) {
724
- Object.defineProperty(obj, key, {
725
- value,
726
- enumerable: true,
727
- configurable: true,
728
- writable: true
729
- });
730
- } else {
731
- obj[key] = value;
732
- }
733
- return obj;
734
- }
735
781
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/rpc/src/service.ts";
736
782
  var createServiceBundle = (services) => services;
737
783
  var ProtoRpcPeer = class {
784
+ rpc;
785
+ _peer;
786
+ constructor(rpc, _peer) {
787
+ this.rpc = rpc;
788
+ this._peer = _peer;
789
+ }
738
790
  async open() {
739
791
  await this._peer.open();
740
792
  }
@@ -744,12 +796,6 @@ var ProtoRpcPeer = class {
744
796
  async abort() {
745
797
  await this._peer.abort();
746
798
  }
747
- constructor(rpc, _peer) {
748
- _define_property2(this, "rpc", void 0);
749
- _define_property2(this, "_peer", void 0);
750
- this.rpc = rpc;
751
- this._peer = _peer;
752
- }
753
799
  };
754
800
  var createProtoRpcPeer = ({ requested, exposed, handlers, encodingOptions, ...rest }) => {
755
801
  const exposedRpcs = {};
@@ -896,29 +942,12 @@ var encodeMessage = (msg) => isNode() ? Buffer.from(msg) : new TextEncoder().enc
896
942
  // src/trace.ts
897
943
  import { Event } from "@dxos/async";
898
944
  import { MessageTrace } from "@dxos/protocols/proto/dxos/rpc";
899
- function _define_property3(obj, key, value) {
900
- if (key in obj) {
901
- Object.defineProperty(obj, key, {
902
- value,
903
- enumerable: true,
904
- configurable: true,
905
- writable: true
906
- });
907
- } else {
908
- obj[key] = value;
909
- }
910
- return obj;
911
- }
912
945
  var PortTracer = class {
913
- get port() {
914
- return this._port;
915
- }
946
+ _wrappedPort;
947
+ message = new Event();
948
+ _port;
916
949
  constructor(_wrappedPort) {
917
- _define_property3(this, "_wrappedPort", void 0);
918
- _define_property3(this, "message", void 0);
919
- _define_property3(this, "_port", void 0);
920
950
  this._wrappedPort = _wrappedPort;
921
- this.message = new Event();
922
951
  this._port = {
923
952
  send: (msg) => {
924
953
  this.message.emit({
@@ -938,6 +967,9 @@ var PortTracer = class {
938
967
  }
939
968
  };
940
969
  }
970
+ get port() {
971
+ return this._port;
972
+ }
941
973
  };
942
974
  export {
943
975
  PortTracer,