@dxos/rpc 0.8.4-main.40e3dcdf1b → 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.
@@ -12,6 +12,7 @@ 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";
@@ -25,7 +26,7 @@ function _ts_decorate(decorators, target, key, desc) {
25
26
  return c > 3 && r && Object.defineProperty(target, key, r), r;
26
27
  }
27
28
  var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/rpc/src/rpc.ts";
28
- var DEFAULT_TIMEOUT = 3e3;
29
+ var DEFAULT_TIMEOUT = 3e4;
29
30
  var BYE_SEND_TIMEOUT = 2e3;
30
31
  var DEBUG_CALLS = true;
31
32
  var CLOSE_TIMEOUT = 3e3;
@@ -81,7 +82,7 @@ var RpcPeer = class {
81
82
  } catch (err) {
82
83
  log.catch(err, void 0, {
83
84
  F: __dxlog_file,
84
- L: 156,
85
+ L: 157,
85
86
  S: this,
86
87
  C: (f, a) => f(...a)
87
88
  });
@@ -97,7 +98,7 @@ var RpcPeer = class {
97
98
  state: this._state
98
99
  }, {
99
100
  F: __dxlog_file,
100
- L: 168,
101
+ L: 169,
101
102
  S: this,
102
103
  C: (f, a) => f(...a)
103
104
  });
@@ -112,7 +113,7 @@ var RpcPeer = class {
112
113
  open: true
113
114
  }).catch((err) => log.warn(err, void 0, {
114
115
  F: __dxlog_file,
115
- L: 177,
116
+ L: 178,
116
117
  S: this,
117
118
  C: (f, a) => f(...a)
118
119
  }));
@@ -129,7 +130,7 @@ var RpcPeer = class {
129
130
  state: this._state
130
131
  }, {
131
132
  F: __dxlog_file,
132
- L: 191,
133
+ L: 192,
133
134
  S: this,
134
135
  C: (f, a) => f(...a)
135
136
  });
@@ -159,7 +160,7 @@ var RpcPeer = class {
159
160
  err
160
161
  }, {
161
162
  F: __dxlog_file,
162
- L: 213,
163
+ L: 214,
163
164
  S: this,
164
165
  C: (f, a) => f(...a)
165
166
  });
@@ -167,7 +168,7 @@ var RpcPeer = class {
167
168
  try {
168
169
  log("closing waiting on bye", void 0, {
169
170
  F: __dxlog_file,
170
- L: 216,
171
+ L: 217,
171
172
  S: this,
172
173
  C: (f, a) => f(...a)
173
174
  });
@@ -179,7 +180,7 @@ var RpcPeer = class {
179
180
  err
180
181
  }, {
181
182
  F: __dxlog_file,
182
- L: 219,
183
+ L: 220,
183
184
  S: this,
184
185
  C: (f, a) => f(...a)
185
186
  });
@@ -223,7 +224,7 @@ var RpcPeer = class {
223
224
  type: Object.keys(decoded)[0]
224
225
  }, {
225
226
  F: __dxlog_file,
226
- L: 263,
227
+ L: 264,
227
228
  S: this,
228
229
  C: (f, a) => f(...a)
229
230
  });
@@ -231,7 +232,7 @@ var RpcPeer = class {
231
232
  if (this._state !== "OPENED" && this._state !== "OPENING") {
232
233
  log("received request while closed", void 0, {
233
234
  F: __dxlog_file,
234
- L: 267,
235
+ L: 268,
235
236
  S: this,
236
237
  C: (f, a) => f(...a)
237
238
  });
@@ -249,7 +250,7 @@ var RpcPeer = class {
249
250
  method: req.method
250
251
  }, {
251
252
  F: __dxlog_file,
252
- L: 279,
253
+ L: 280,
253
254
  S: this,
254
255
  C: (f, a) => f(...a)
255
256
  });
@@ -261,7 +262,7 @@ var RpcPeer = class {
261
262
  close: response.close
262
263
  }, {
263
264
  F: __dxlog_file,
264
- L: 281,
265
+ L: 282,
265
266
  S: this,
266
267
  C: (f, a) => f(...a)
267
268
  });
@@ -270,7 +271,7 @@ var RpcPeer = class {
270
271
  }).catch((err) => {
271
272
  log.warn("failed during close", err, {
272
273
  F: __dxlog_file,
273
- L: 289,
274
+ L: 290,
274
275
  S: this,
275
276
  C: (f, a) => f(...a)
276
277
  });
@@ -281,7 +282,7 @@ var RpcPeer = class {
281
282
  method: req.method
282
283
  }, {
283
284
  F: __dxlog_file,
284
- L: 293,
285
+ L: 294,
285
286
  S: this,
286
287
  C: (f, a) => f(...a)
287
288
  });
@@ -292,7 +293,7 @@ var RpcPeer = class {
292
293
  error: response.error
293
294
  }, {
294
295
  F: __dxlog_file,
295
- L: 296,
296
+ L: 297,
296
297
  S: this,
297
298
  C: (f, a) => f(...a)
298
299
  });
@@ -304,7 +305,7 @@ var RpcPeer = class {
304
305
  if (this._state !== "OPENED") {
305
306
  log("received response while closed", void 0, {
306
307
  F: __dxlog_file,
307
- L: 305,
308
+ L: 306,
308
309
  S: this,
309
310
  C: (f, a) => f(...a)
310
311
  });
@@ -313,7 +314,7 @@ var RpcPeer = class {
313
314
  const responseId = decoded.response.id;
314
315
  invariant(typeof responseId === "number", void 0, {
315
316
  F: __dxlog_file,
316
- L: 310,
317
+ L: 311,
317
318
  S: this,
318
319
  A: [
319
320
  "typeof responseId === 'number'",
@@ -325,7 +326,7 @@ var RpcPeer = class {
325
326
  responseId
326
327
  }, {
327
328
  F: __dxlog_file,
328
- L: 312,
329
+ L: 313,
329
330
  S: this,
330
331
  C: (f, a) => f(...a)
331
332
  });
@@ -339,7 +340,7 @@ var RpcPeer = class {
339
340
  type_url: decoded.response.payload?.type_url
340
341
  }, {
341
342
  F: __dxlog_file,
342
- L: 322,
343
+ L: 323,
343
344
  S: this,
344
345
  C: (f, a) => f(...a)
345
346
  });
@@ -349,7 +350,7 @@ var RpcPeer = class {
349
350
  state: this._state
350
351
  }, {
351
352
  F: __dxlog_file,
352
- L: 325,
353
+ L: 326,
353
354
  S: this,
354
355
  C: (f, a) => f(...a)
355
356
  });
@@ -364,7 +365,7 @@ var RpcPeer = class {
364
365
  state: this._state
365
366
  }, {
366
367
  F: __dxlog_file,
367
- L: 332,
368
+ L: 333,
368
369
  S: this,
369
370
  C: (f, a) => f(...a)
370
371
  });
@@ -377,7 +378,7 @@ var RpcPeer = class {
377
378
  if (this._state !== "OPENED") {
378
379
  log("received stream close while closed", void 0, {
379
380
  F: __dxlog_file,
380
- L: 341,
381
+ L: 342,
381
382
  S: this,
382
383
  C: (f, a) => f(...a)
383
384
  });
@@ -387,13 +388,13 @@ var RpcPeer = class {
387
388
  id: decoded.streamClose.id
388
389
  }, {
389
390
  F: __dxlog_file,
390
- L: 345,
391
+ L: 346,
391
392
  S: this,
392
393
  C: (f, a) => f(...a)
393
394
  });
394
395
  invariant(typeof decoded.streamClose.id === "number", void 0, {
395
396
  F: __dxlog_file,
396
- L: 346,
397
+ L: 347,
397
398
  S: this,
398
399
  A: [
399
400
  "typeof decoded.streamClose.id === 'number'",
@@ -406,7 +407,7 @@ var RpcPeer = class {
406
407
  id: decoded.streamClose.id
407
408
  }, {
408
409
  F: __dxlog_file,
409
- L: 349,
410
+ L: 350,
410
411
  S: this,
411
412
  C: (f, a) => f(...a)
412
413
  });
@@ -419,7 +420,7 @@ var RpcPeer = class {
419
420
  if (this._state !== "CLOSING" && this._state !== "CLOSED") {
420
421
  log("replying to bye", void 0, {
421
422
  F: __dxlog_file,
422
- L: 359,
423
+ L: 360,
423
424
  S: this,
424
425
  C: (f, a) => f(...a)
425
426
  });
@@ -435,7 +436,7 @@ var RpcPeer = class {
435
436
  msg
436
437
  }, {
437
438
  F: __dxlog_file,
438
- L: 367,
439
+ L: 368,
439
440
  S: this,
440
441
  C: (f, a) => f(...a)
441
442
  });
@@ -451,7 +452,7 @@ var RpcPeer = class {
451
452
  method
452
453
  }, {
453
454
  F: __dxlog_file,
454
- L: 377,
455
+ L: 378,
455
456
  S: this,
456
457
  C: (f, a) => f(...a)
457
458
  });
@@ -462,12 +463,28 @@ var RpcPeer = class {
462
463
  const responseReceived = new Promise((resolve, reject) => {
463
464
  this._outgoingRequests.set(id, new PendingRpcRequest(resolve, reject, false));
464
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
+ }
465
479
  const sending = this._sendMessage({
466
480
  request: {
467
481
  id,
468
482
  method,
469
483
  payload: request,
470
- stream: false
484
+ stream: false,
485
+ ...traceContext ? {
486
+ traceContext
487
+ } : {}
471
488
  }
472
489
  });
473
490
  const timeout = options?.timeout ?? this._params.timeout;
@@ -479,7 +496,7 @@ var RpcPeer = class {
479
496
  response = await waiting;
480
497
  invariant(response.id === id, void 0, {
481
498
  F: __dxlog_file,
482
- L: 405,
499
+ L: 414,
483
500
  S: this,
484
501
  A: [
485
502
  "response.id === id",
@@ -540,16 +557,38 @@ ${stack.getStack()}`;
540
557
  }
541
558
  };
542
559
  this._outgoingRequests.set(id, new PendingRpcRequest(onResponse, closeStream, true));
543
- this._sendMessage({
544
- request: {
545
- id,
546
- method,
547
- payload: request,
548
- stream: true
549
- }
550
- }).catch((err) => {
551
- close(err);
552
- });
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
+ }
553
592
  return () => {
554
593
  this._sendMessage({
555
594
  streamClose: {
@@ -558,7 +597,7 @@ ${stack.getStack()}`;
558
597
  }).catch((err) => {
559
598
  log.catch(err, void 0, {
560
599
  F: __dxlog_file,
561
- L: 478,
600
+ L: 501,
562
601
  S: this,
563
602
  C: (f, a) => f(...a)
564
603
  });
@@ -572,7 +611,7 @@ ${stack.getStack()}`;
572
611
  type: Object.keys(message)[0]
573
612
  }, {
574
613
  F: __dxlog_file,
575
- L: 486,
614
+ L: 509,
576
615
  S: this,
577
616
  C: (f, a) => f(...a)
578
617
  });
@@ -580,11 +619,38 @@ ${stack.getStack()}`;
580
619
  preserveAny: true
581
620
  }), timeout);
582
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
+ }
583
649
  async _callHandler(req) {
584
650
  try {
585
651
  invariant(typeof req.id === "number", void 0, {
586
652
  F: __dxlog_file,
587
- L: 492,
653
+ L: 530,
588
654
  S: this,
589
655
  A: [
590
656
  "typeof req.id === 'number'",
@@ -593,7 +659,7 @@ ${stack.getStack()}`;
593
659
  });
594
660
  invariant(req.payload, void 0, {
595
661
  F: __dxlog_file,
596
- L: 493,
662
+ L: 531,
597
663
  S: this,
598
664
  A: [
599
665
  "req.payload",
@@ -602,14 +668,14 @@ ${stack.getStack()}`;
602
668
  });
603
669
  invariant(req.method, void 0, {
604
670
  F: __dxlog_file,
605
- L: 494,
671
+ L: 532,
606
672
  S: this,
607
673
  A: [
608
674
  "req.method",
609
675
  ""
610
676
  ]
611
677
  });
612
- 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));
613
679
  return {
614
680
  id: req.id,
615
681
  payload: response
@@ -625,7 +691,7 @@ ${stack.getStack()}`;
625
691
  try {
626
692
  invariant(this._params.streamHandler, "Requests with streaming responses are not supported.", {
627
693
  F: __dxlog_file,
628
- L: 511,
694
+ L: 549,
629
695
  S: this,
630
696
  A: [
631
697
  "this._params.streamHandler",
@@ -634,7 +700,7 @@ ${stack.getStack()}`;
634
700
  });
635
701
  invariant(typeof req.id === "number", void 0, {
636
702
  F: __dxlog_file,
637
- L: 512,
703
+ L: 550,
638
704
  S: this,
639
705
  A: [
640
706
  "typeof req.id === 'number'",
@@ -643,7 +709,7 @@ ${stack.getStack()}`;
643
709
  });
644
710
  invariant(req.payload, void 0, {
645
711
  F: __dxlog_file,
646
- L: 513,
712
+ L: 551,
647
713
  S: this,
648
714
  A: [
649
715
  "req.payload",
@@ -652,14 +718,14 @@ ${stack.getStack()}`;
652
718
  });
653
719
  invariant(req.method, void 0, {
654
720
  F: __dxlog_file,
655
- L: 514,
721
+ L: 552,
656
722
  S: this,
657
723
  A: [
658
724
  "req.method",
659
725
  ""
660
726
  ]
661
727
  });
662
- 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));
663
729
  responseStream.onReady(() => {
664
730
  callback({
665
731
  id: req.id,