@dxos/rpc 0.6.11 → 0.6.12-main.5cc132e

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.
@@ -0,0 +1,923 @@
1
+ // packages/core/mesh/rpc/src/rpc.ts
2
+ import { asyncTimeout, synchronized, Trigger } from "@dxos/async";
3
+ import { Stream } from "@dxos/codec-protobuf";
4
+ import { StackTrace as StackTrace2 } from "@dxos/debug";
5
+ import { invariant } from "@dxos/invariant";
6
+ import { log } from "@dxos/log";
7
+ import { encodeError, RpcClosedError, RpcNotOpenError } from "@dxos/protocols";
8
+ import { schema } from "@dxos/protocols/proto";
9
+ import { exponentialBackoffInterval } from "@dxos/util";
10
+
11
+ // packages/core/mesh/rpc/src/errors.ts
12
+ import { StackTrace } from "@dxos/debug";
13
+ import { decodeError } from "@dxos/protocols";
14
+ var decodeRpcError = (err, rpcMethod) => decodeError(err, {
15
+ appendStack: `
16
+ at RPC ${rpcMethod}
17
+ ` + new StackTrace().getStack(1)
18
+ });
19
+
20
+ // packages/core/mesh/rpc/src/rpc.ts
21
+ function _ts_decorate(decorators, target, key, desc) {
22
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
23
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
24
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
25
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
26
+ }
27
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/rpc/src/rpc.ts";
28
+ var DEFAULT_TIMEOUT = 3e3;
29
+ var BYE_SEND_TIMEOUT = 2e3;
30
+ var DEBUG_CALLS = true;
31
+ var CLOSE_TIMEOUT = 3e3;
32
+ var PendingRpcRequest = class {
33
+ constructor(resolve, reject, stream) {
34
+ this.resolve = resolve;
35
+ this.reject = reject;
36
+ this.stream = stream;
37
+ }
38
+ };
39
+ var RpcMessageCodec;
40
+ var getRpcMessageCodec = () => RpcMessageCodec ??= schema.getCodecForType("dxos.rpc.RpcMessage");
41
+ var RpcState;
42
+ (function(RpcState2) {
43
+ RpcState2["INITIAL"] = "INITIAL";
44
+ RpcState2["OPENING"] = "OPENING";
45
+ RpcState2["OPENED"] = "OPENED";
46
+ RpcState2["CLOSING"] = "CLOSING";
47
+ RpcState2["CLOSED"] = "CLOSED";
48
+ })(RpcState || (RpcState = {}));
49
+ var RpcPeer = class {
50
+ constructor(params) {
51
+ this._outgoingRequests = /* @__PURE__ */ new Map();
52
+ this._localStreams = /* @__PURE__ */ new Map();
53
+ this._remoteOpenTrigger = new Trigger();
54
+ /**
55
+ * Triggered when the peer starts closing.
56
+ */
57
+ this._closingTrigger = new Trigger();
58
+ /**
59
+ * Triggered when peer receives a bye message.
60
+ */
61
+ this._byeTrigger = new Trigger();
62
+ this._nextId = 0;
63
+ this._state = "INITIAL";
64
+ this._unsubscribeFromPort = void 0;
65
+ this._clearOpenInterval = void 0;
66
+ this._params = {
67
+ timeout: void 0,
68
+ streamHandler: void 0,
69
+ noHandshake: false,
70
+ ...params
71
+ };
72
+ }
73
+ /**
74
+ * Open the peer. Required before making any calls.
75
+ *
76
+ * Will block before the other peer calls `open`.
77
+ */
78
+ async open() {
79
+ if (this._state !== "INITIAL") {
80
+ return;
81
+ }
82
+ this._unsubscribeFromPort = this._params.port.subscribe(async (msg) => {
83
+ try {
84
+ await this._receive(msg);
85
+ } catch (err) {
86
+ log.catch(err, void 0, {
87
+ F: __dxlog_file,
88
+ L: 156,
89
+ S: this,
90
+ C: (f, a) => f(...a)
91
+ });
92
+ }
93
+ });
94
+ this._state = "OPENING";
95
+ if (this._params.noHandshake) {
96
+ this._state = "OPENED";
97
+ this._remoteOpenTrigger.wake();
98
+ return;
99
+ }
100
+ log("sending open message", {
101
+ state: this._state
102
+ }, {
103
+ F: __dxlog_file,
104
+ L: 168,
105
+ S: this,
106
+ C: (f, a) => f(...a)
107
+ });
108
+ await this._sendMessage({
109
+ open: true
110
+ });
111
+ if (this._state !== "OPENING") {
112
+ return;
113
+ }
114
+ this._clearOpenInterval = exponentialBackoffInterval(() => {
115
+ void this._sendMessage({
116
+ open: true
117
+ }).catch((err) => log.warn(err, void 0, {
118
+ F: __dxlog_file,
119
+ L: 177,
120
+ S: this,
121
+ C: (f, a) => f(...a)
122
+ }));
123
+ }, 50);
124
+ await Promise.race([
125
+ this._remoteOpenTrigger.wait(),
126
+ this._closingTrigger.wait()
127
+ ]);
128
+ this._clearOpenInterval?.();
129
+ if (this._state !== "OPENED") {
130
+ return;
131
+ }
132
+ log("sending second open message", {
133
+ state: this._state
134
+ }, {
135
+ F: __dxlog_file,
136
+ L: 191,
137
+ S: this,
138
+ C: (f, a) => f(...a)
139
+ });
140
+ await this._sendMessage({
141
+ openAck: true
142
+ });
143
+ }
144
+ /**
145
+ * Close the peer.
146
+ * Stop taking or making requests.
147
+ * Will wait for confirmation from the other side.
148
+ * Any responses for RPC calls made before close will be delivered.
149
+ */
150
+ async close({ timeout = CLOSE_TIMEOUT } = {}) {
151
+ if (this._state === "CLOSED") {
152
+ return;
153
+ }
154
+ this._abortRequests();
155
+ if (this._state === "OPENED" && !this._params.noHandshake) {
156
+ try {
157
+ this._state = "CLOSING";
158
+ await this._sendMessage({
159
+ bye: {}
160
+ }, BYE_SEND_TIMEOUT);
161
+ } catch (err) {
162
+ log("error closing peer, sending bye", {
163
+ err
164
+ }, {
165
+ F: __dxlog_file,
166
+ L: 213,
167
+ S: this,
168
+ C: (f, a) => f(...a)
169
+ });
170
+ }
171
+ try {
172
+ log("closing waiting on bye", void 0, {
173
+ F: __dxlog_file,
174
+ L: 216,
175
+ S: this,
176
+ C: (f, a) => f(...a)
177
+ });
178
+ await this._byeTrigger.wait({
179
+ timeout
180
+ });
181
+ } catch (err) {
182
+ log("error closing peer", {
183
+ err
184
+ }, {
185
+ F: __dxlog_file,
186
+ L: 219,
187
+ S: this,
188
+ C: (f, a) => f(...a)
189
+ });
190
+ return;
191
+ }
192
+ }
193
+ this._disposeAndClose();
194
+ }
195
+ /**
196
+ * Dispose the connection without waiting for the other side.
197
+ */
198
+ async abort() {
199
+ if (this._state === "CLOSED") {
200
+ return;
201
+ }
202
+ this._abortRequests();
203
+ this._disposeAndClose();
204
+ }
205
+ _abortRequests() {
206
+ this._clearOpenInterval?.();
207
+ this._closingTrigger.wake();
208
+ for (const req of this._outgoingRequests.values()) {
209
+ req.reject(new RpcClosedError());
210
+ }
211
+ this._outgoingRequests.clear();
212
+ }
213
+ _disposeAndClose() {
214
+ this._unsubscribeFromPort?.();
215
+ this._unsubscribeFromPort = void 0;
216
+ this._clearOpenInterval?.();
217
+ this._state = "CLOSED";
218
+ }
219
+ /**
220
+ * Handle incoming message. Should be called as the result of other peer's `send` callback.
221
+ */
222
+ async _receive(msg) {
223
+ const decoded = getRpcMessageCodec().decode(msg, {
224
+ preserveAny: true
225
+ });
226
+ DEBUG_CALLS && log("received message", {
227
+ type: Object.keys(decoded)[0]
228
+ }, {
229
+ F: __dxlog_file,
230
+ L: 263,
231
+ S: this,
232
+ C: (f, a) => f(...a)
233
+ });
234
+ if (decoded.request) {
235
+ if (this._state !== "OPENED" && this._state !== "OPENING") {
236
+ log("received request while closed", void 0, {
237
+ F: __dxlog_file,
238
+ L: 267,
239
+ S: this,
240
+ C: (f, a) => f(...a)
241
+ });
242
+ await this._sendMessage({
243
+ response: {
244
+ id: decoded.request.id,
245
+ error: encodeError(new RpcClosedError())
246
+ }
247
+ });
248
+ return;
249
+ }
250
+ const req = decoded.request;
251
+ if (req.stream) {
252
+ log("stream request", {
253
+ method: req.method
254
+ }, {
255
+ F: __dxlog_file,
256
+ L: 279,
257
+ S: this,
258
+ C: (f, a) => f(...a)
259
+ });
260
+ this._callStreamHandler(req, (response) => {
261
+ log("sending stream response", {
262
+ method: req.method,
263
+ response: response.payload?.type_url,
264
+ error: response.error,
265
+ close: response.close
266
+ }, {
267
+ F: __dxlog_file,
268
+ L: 281,
269
+ S: this,
270
+ C: (f, a) => f(...a)
271
+ });
272
+ void this._sendMessage({
273
+ response
274
+ }).catch((err) => {
275
+ log.warn("failed during close", err, {
276
+ F: __dxlog_file,
277
+ L: 289,
278
+ S: this,
279
+ C: (f, a) => f(...a)
280
+ });
281
+ });
282
+ });
283
+ } else {
284
+ DEBUG_CALLS && log("request", {
285
+ method: req.method
286
+ }, {
287
+ F: __dxlog_file,
288
+ L: 293,
289
+ S: this,
290
+ C: (f, a) => f(...a)
291
+ });
292
+ const response = await this._callHandler(req);
293
+ DEBUG_CALLS && log("sending response", {
294
+ method: req.method,
295
+ response: response.payload?.type_url,
296
+ error: response.error
297
+ }, {
298
+ F: __dxlog_file,
299
+ L: 296,
300
+ S: this,
301
+ C: (f, a) => f(...a)
302
+ });
303
+ await this._sendMessage({
304
+ response
305
+ });
306
+ }
307
+ } else if (decoded.response) {
308
+ if (this._state !== "OPENED") {
309
+ log("received response while closed", void 0, {
310
+ F: __dxlog_file,
311
+ L: 305,
312
+ S: this,
313
+ C: (f, a) => f(...a)
314
+ });
315
+ return;
316
+ }
317
+ const responseId = decoded.response.id;
318
+ invariant(typeof responseId === "number", void 0, {
319
+ F: __dxlog_file,
320
+ L: 310,
321
+ S: this,
322
+ A: [
323
+ "typeof responseId === 'number'",
324
+ ""
325
+ ]
326
+ });
327
+ if (!this._outgoingRequests.has(responseId)) {
328
+ log("received response with invalid id", {
329
+ responseId
330
+ }, {
331
+ F: __dxlog_file,
332
+ L: 312,
333
+ S: this,
334
+ C: (f, a) => f(...a)
335
+ });
336
+ return;
337
+ }
338
+ const item = this._outgoingRequests.get(responseId);
339
+ if (!item.stream) {
340
+ this._outgoingRequests.delete(responseId);
341
+ }
342
+ DEBUG_CALLS && log("response", {
343
+ type_url: decoded.response.payload?.type_url
344
+ }, {
345
+ F: __dxlog_file,
346
+ L: 322,
347
+ S: this,
348
+ C: (f, a) => f(...a)
349
+ });
350
+ item.resolve(decoded.response);
351
+ } else if (decoded.open) {
352
+ log("received open message", {
353
+ state: this._state
354
+ }, {
355
+ F: __dxlog_file,
356
+ L: 325,
357
+ S: this,
358
+ C: (f, a) => f(...a)
359
+ });
360
+ if (this._params.noHandshake) {
361
+ return;
362
+ }
363
+ await this._sendMessage({
364
+ openAck: true
365
+ });
366
+ } else if (decoded.openAck) {
367
+ log("received openAck message", {
368
+ state: this._state
369
+ }, {
370
+ F: __dxlog_file,
371
+ L: 332,
372
+ S: this,
373
+ C: (f, a) => f(...a)
374
+ });
375
+ if (this._params.noHandshake) {
376
+ return;
377
+ }
378
+ this._state = "OPENED";
379
+ this._remoteOpenTrigger.wake();
380
+ } else if (decoded.streamClose) {
381
+ if (this._state !== "OPENED") {
382
+ log("received stream close while closed", void 0, {
383
+ F: __dxlog_file,
384
+ L: 341,
385
+ S: this,
386
+ C: (f, a) => f(...a)
387
+ });
388
+ return;
389
+ }
390
+ log("received stream close", {
391
+ id: decoded.streamClose.id
392
+ }, {
393
+ F: __dxlog_file,
394
+ L: 345,
395
+ S: this,
396
+ C: (f, a) => f(...a)
397
+ });
398
+ invariant(typeof decoded.streamClose.id === "number", void 0, {
399
+ F: __dxlog_file,
400
+ L: 346,
401
+ S: this,
402
+ A: [
403
+ "typeof decoded.streamClose.id === 'number'",
404
+ ""
405
+ ]
406
+ });
407
+ const stream = this._localStreams.get(decoded.streamClose.id);
408
+ if (!stream) {
409
+ log("no local stream", {
410
+ id: decoded.streamClose.id
411
+ }, {
412
+ F: __dxlog_file,
413
+ L: 349,
414
+ S: this,
415
+ C: (f, a) => f(...a)
416
+ });
417
+ return;
418
+ }
419
+ this._localStreams.delete(decoded.streamClose.id);
420
+ await stream.close();
421
+ } else if (decoded.bye) {
422
+ this._byeTrigger.wake();
423
+ if (this._state !== "CLOSING" && this._state !== "CLOSED") {
424
+ log("replying to bye", void 0, {
425
+ F: __dxlog_file,
426
+ L: 359,
427
+ S: this,
428
+ C: (f, a) => f(...a)
429
+ });
430
+ this._state = "CLOSING";
431
+ await this._sendMessage({
432
+ bye: {}
433
+ });
434
+ this._abortRequests();
435
+ this._disposeAndClose();
436
+ }
437
+ } else {
438
+ log.error("received malformed message", {
439
+ msg
440
+ }, {
441
+ F: __dxlog_file,
442
+ L: 367,
443
+ S: this,
444
+ C: (f, a) => f(...a)
445
+ });
446
+ throw new Error("Malformed message.");
447
+ }
448
+ }
449
+ /**
450
+ * Make RPC call. Will trigger a handler on the other side.
451
+ * Peer should be open before making this call.
452
+ */
453
+ async call(method, request, options) {
454
+ DEBUG_CALLS && log("calling", {
455
+ method
456
+ }, {
457
+ F: __dxlog_file,
458
+ L: 377,
459
+ S: this,
460
+ C: (f, a) => f(...a)
461
+ });
462
+ throwIfNotOpen(this._state);
463
+ let response;
464
+ try {
465
+ const id = this._nextId++;
466
+ const responseReceived = new Promise((resolve, reject) => {
467
+ this._outgoingRequests.set(id, new PendingRpcRequest(resolve, reject, false));
468
+ });
469
+ const sending = this._sendMessage({
470
+ request: {
471
+ id,
472
+ method,
473
+ payload: request,
474
+ stream: false
475
+ }
476
+ });
477
+ const timeout = options?.timeout ?? this._params.timeout;
478
+ const waiting = timeout === 0 ? responseReceived : asyncTimeout(responseReceived, timeout ?? DEFAULT_TIMEOUT);
479
+ await Promise.race([
480
+ sending,
481
+ waiting
482
+ ]);
483
+ response = await waiting;
484
+ invariant(response.id === id, void 0, {
485
+ F: __dxlog_file,
486
+ L: 405,
487
+ S: this,
488
+ A: [
489
+ "response.id === id",
490
+ ""
491
+ ]
492
+ });
493
+ } catch (err) {
494
+ if (err instanceof RpcClosedError) {
495
+ const error = new RpcClosedError();
496
+ error.stack += `
497
+
498
+ info: RPC client was closed at:
499
+ ${err.stack?.split("\n").slice(1).join("\n")}`;
500
+ throw error;
501
+ }
502
+ throw err;
503
+ }
504
+ if (response.payload) {
505
+ return response.payload;
506
+ } else if (response.error) {
507
+ throw decodeRpcError(response.error, method);
508
+ } else {
509
+ throw new Error("Malformed response.");
510
+ }
511
+ }
512
+ /**
513
+ * Make RPC call with a streaming response.
514
+ * Will trigger a handler on the other side.
515
+ * Peer should be open before making this call.
516
+ */
517
+ callStream(method, request, options) {
518
+ throwIfNotOpen(this._state);
519
+ const id = this._nextId++;
520
+ return new Stream(({ ready, next, close }) => {
521
+ const onResponse = (response) => {
522
+ if (response.streamReady) {
523
+ ready();
524
+ } else if (response.close) {
525
+ close();
526
+ } else if (response.error) {
527
+ close(decodeRpcError(response.error, method));
528
+ } else if (response.payload) {
529
+ next(response.payload);
530
+ } else {
531
+ throw new Error("Malformed response.");
532
+ }
533
+ };
534
+ const stack = new StackTrace2();
535
+ const closeStream = (err) => {
536
+ if (!err) {
537
+ close();
538
+ } else {
539
+ err.stack += `
540
+
541
+ Error happened in the stream at:
542
+ ${stack.getStack()}`;
543
+ close(err);
544
+ }
545
+ };
546
+ this._outgoingRequests.set(id, new PendingRpcRequest(onResponse, closeStream, true));
547
+ this._sendMessage({
548
+ request: {
549
+ id,
550
+ method,
551
+ payload: request,
552
+ stream: true
553
+ }
554
+ }).catch((err) => {
555
+ close(err);
556
+ });
557
+ return () => {
558
+ this._sendMessage({
559
+ streamClose: {
560
+ id
561
+ }
562
+ }).catch((err) => {
563
+ log.catch(err, void 0, {
564
+ F: __dxlog_file,
565
+ L: 478,
566
+ S: this,
567
+ C: (f, a) => f(...a)
568
+ });
569
+ });
570
+ this._outgoingRequests.delete(id);
571
+ };
572
+ });
573
+ }
574
+ async _sendMessage(message, timeout) {
575
+ DEBUG_CALLS && log("sending message", {
576
+ type: Object.keys(message)[0]
577
+ }, {
578
+ F: __dxlog_file,
579
+ L: 486,
580
+ S: this,
581
+ C: (f, a) => f(...a)
582
+ });
583
+ await this._params.port.send(getRpcMessageCodec().encode(message, {
584
+ preserveAny: true
585
+ }), timeout);
586
+ }
587
+ async _callHandler(req) {
588
+ try {
589
+ invariant(typeof req.id === "number", void 0, {
590
+ F: __dxlog_file,
591
+ L: 492,
592
+ S: this,
593
+ A: [
594
+ "typeof req.id === 'number'",
595
+ ""
596
+ ]
597
+ });
598
+ invariant(req.payload, void 0, {
599
+ F: __dxlog_file,
600
+ L: 493,
601
+ S: this,
602
+ A: [
603
+ "req.payload",
604
+ ""
605
+ ]
606
+ });
607
+ invariant(req.method, void 0, {
608
+ F: __dxlog_file,
609
+ L: 494,
610
+ S: this,
611
+ A: [
612
+ "req.method",
613
+ ""
614
+ ]
615
+ });
616
+ const response = await this._params.callHandler(req.method, req.payload, this._params.handlerRpcOptions);
617
+ return {
618
+ id: req.id,
619
+ payload: response
620
+ };
621
+ } catch (err) {
622
+ return {
623
+ id: req.id,
624
+ error: encodeError(err)
625
+ };
626
+ }
627
+ }
628
+ _callStreamHandler(req, callback) {
629
+ try {
630
+ invariant(this._params.streamHandler, "Requests with streaming responses are not supported.", {
631
+ F: __dxlog_file,
632
+ L: 511,
633
+ S: this,
634
+ A: [
635
+ "this._params.streamHandler",
636
+ "'Requests with streaming responses are not supported.'"
637
+ ]
638
+ });
639
+ invariant(typeof req.id === "number", void 0, {
640
+ F: __dxlog_file,
641
+ L: 512,
642
+ S: this,
643
+ A: [
644
+ "typeof req.id === 'number'",
645
+ ""
646
+ ]
647
+ });
648
+ invariant(req.payload, void 0, {
649
+ F: __dxlog_file,
650
+ L: 513,
651
+ S: this,
652
+ A: [
653
+ "req.payload",
654
+ ""
655
+ ]
656
+ });
657
+ invariant(req.method, void 0, {
658
+ F: __dxlog_file,
659
+ L: 514,
660
+ S: this,
661
+ A: [
662
+ "req.method",
663
+ ""
664
+ ]
665
+ });
666
+ const responseStream = this._params.streamHandler(req.method, req.payload, this._params.handlerRpcOptions);
667
+ responseStream.onReady(() => {
668
+ callback({
669
+ id: req.id,
670
+ streamReady: true
671
+ });
672
+ });
673
+ responseStream.subscribe((msg) => {
674
+ callback({
675
+ id: req.id,
676
+ payload: msg
677
+ });
678
+ }, (error) => {
679
+ if (error) {
680
+ callback({
681
+ id: req.id,
682
+ error: encodeError(error)
683
+ });
684
+ } else {
685
+ callback({
686
+ id: req.id,
687
+ close: true
688
+ });
689
+ }
690
+ });
691
+ this._localStreams.set(req.id, responseStream);
692
+ } catch (err) {
693
+ callback({
694
+ id: req.id,
695
+ error: encodeError(err)
696
+ });
697
+ }
698
+ }
699
+ };
700
+ _ts_decorate([
701
+ synchronized
702
+ ], RpcPeer.prototype, "open", null);
703
+ var throwIfNotOpen = (state) => {
704
+ switch (state) {
705
+ case "OPENED": {
706
+ return;
707
+ }
708
+ case "INITIAL": {
709
+ throw new RpcNotOpenError();
710
+ }
711
+ case "CLOSED": {
712
+ throw new RpcClosedError();
713
+ }
714
+ }
715
+ };
716
+
717
+ // packages/core/mesh/rpc/src/service.ts
718
+ import { invariant as invariant2 } from "@dxos/invariant";
719
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/rpc/src/service.ts";
720
+ var createServiceBundle = (services) => services;
721
+ var ProtoRpcPeer = class {
722
+ constructor(rpc, _peer) {
723
+ this.rpc = rpc;
724
+ this._peer = _peer;
725
+ }
726
+ async open() {
727
+ await this._peer.open();
728
+ }
729
+ async close() {
730
+ await this._peer.close();
731
+ }
732
+ async abort() {
733
+ await this._peer.abort();
734
+ }
735
+ };
736
+ var createProtoRpcPeer = ({ requested, exposed, handlers, encodingOptions, ...rest }) => {
737
+ const exposedRpcs = {};
738
+ if (exposed) {
739
+ invariant2(handlers, void 0, {
740
+ F: __dxlog_file2,
741
+ L: 93,
742
+ S: void 0,
743
+ A: [
744
+ "handlers",
745
+ ""
746
+ ]
747
+ });
748
+ for (const serviceName of Object.keys(exposed)) {
749
+ const serviceFqn = exposed[serviceName].serviceProto.fullName.slice(1);
750
+ const serviceProvider = handlers[serviceName];
751
+ exposedRpcs[serviceFqn] = exposed[serviceName].createServer(serviceProvider, encodingOptions);
752
+ }
753
+ }
754
+ const peer = new RpcPeer({
755
+ ...rest,
756
+ callHandler: (method, request, options) => {
757
+ const [serviceName, methodName] = parseMethodName(method);
758
+ if (!exposedRpcs[serviceName]) {
759
+ throw new Error(`Service not supported: ${serviceName}`);
760
+ }
761
+ return exposedRpcs[serviceName].call(methodName, request, options);
762
+ },
763
+ streamHandler: (method, request, options) => {
764
+ const [serviceName, methodName] = parseMethodName(method);
765
+ if (!exposedRpcs[serviceName]) {
766
+ throw new Error(`Service not supported: ${serviceName}`);
767
+ }
768
+ return exposedRpcs[serviceName].callStream(methodName, request, options);
769
+ }
770
+ });
771
+ const requestedRpcs = {};
772
+ if (requested) {
773
+ for (const serviceName of Object.keys(requested)) {
774
+ const serviceFqn = requested[serviceName].serviceProto.fullName.slice(1);
775
+ requestedRpcs[serviceName] = requested[serviceName].createClient({
776
+ call: (method, req, options) => peer.call(`${serviceFqn}.${method}`, req, options),
777
+ callStream: (method, req, options) => peer.callStream(`${serviceFqn}.${method}`, req, options)
778
+ }, encodingOptions);
779
+ }
780
+ }
781
+ return new ProtoRpcPeer(requestedRpcs, peer);
782
+ };
783
+ var parseMethodName = (method) => {
784
+ const separator = method.lastIndexOf(".");
785
+ const serviceName = method.slice(0, separator);
786
+ const methodName = method.slice(separator + 1);
787
+ if (serviceName.length === 0 || methodName.length === 0) {
788
+ throw new Error(`Invalid method: ${method}`);
789
+ }
790
+ return [
791
+ serviceName,
792
+ methodName
793
+ ];
794
+ };
795
+ var createRpcClient = (serviceDef, options) => {
796
+ const peer = new RpcPeer({
797
+ ...options,
798
+ callHandler: () => {
799
+ throw new Error("Requests to client are not supported.");
800
+ }
801
+ });
802
+ const client = serviceDef.createClient({
803
+ call: peer.call.bind(peer),
804
+ callStream: peer.callStream.bind(peer)
805
+ });
806
+ return new ProtoRpcPeer(client, peer);
807
+ };
808
+ var createRpcServer = ({ service, handlers, ...rest }) => {
809
+ const server = service.createServer(handlers);
810
+ return new RpcPeer({
811
+ ...rest,
812
+ callHandler: server.call.bind(server),
813
+ streamHandler: server.callStream.bind(server)
814
+ });
815
+ };
816
+ var createBundledRpcClient = (descriptors, options) => {
817
+ return createProtoRpcPeer({
818
+ requested: descriptors,
819
+ ...options
820
+ });
821
+ };
822
+ var createBundledRpcServer = ({ services, handlers, ...rest }) => {
823
+ const rpc = {};
824
+ for (const serviceName of Object.keys(services)) {
825
+ const serviceFqn = services[serviceName].serviceProto.fullName.slice(1);
826
+ rpc[serviceFqn] = services[serviceName].createServer(handlers[serviceName]);
827
+ }
828
+ return new RpcPeer({
829
+ ...rest,
830
+ callHandler: (method, request) => {
831
+ const [serviceName, methodName] = parseMethodName(method);
832
+ if (!rpc[serviceName]) {
833
+ throw new Error(`Service not supported: ${serviceName}`);
834
+ }
835
+ return rpc[serviceName].call(methodName, request);
836
+ },
837
+ streamHandler: (method, request) => {
838
+ const [serviceName, methodName] = parseMethodName(method);
839
+ if (!rpc[serviceName]) {
840
+ throw new Error(`Service not supported: ${serviceName}`);
841
+ }
842
+ return rpc[serviceName].callStream(methodName, request);
843
+ }
844
+ });
845
+ };
846
+
847
+ // packages/core/mesh/rpc/src/testing.ts
848
+ import { isNode } from "@dxos/util";
849
+ var createLinkedPorts = ({ delay } = {}) => {
850
+ let port1Received;
851
+ let port2Received;
852
+ const send = (handler, msg) => {
853
+ if (delay) {
854
+ setTimeout(() => handler?.(msg), delay);
855
+ } else {
856
+ void handler?.(msg);
857
+ }
858
+ };
859
+ const port1 = {
860
+ send: (msg) => send(port2Received, msg),
861
+ subscribe: (cb) => {
862
+ port1Received = cb;
863
+ }
864
+ };
865
+ const port2 = {
866
+ send: (msg) => send(port1Received, msg),
867
+ subscribe: (cb) => {
868
+ port2Received = cb;
869
+ }
870
+ };
871
+ return [
872
+ port1,
873
+ port2
874
+ ];
875
+ };
876
+ var encodeMessage = (msg) => isNode() ? Buffer.from(msg) : new TextEncoder().encode(msg);
877
+
878
+ // packages/core/mesh/rpc/src/trace.ts
879
+ import { Event } from "@dxos/async";
880
+ import { MessageTrace } from "@dxos/protocols/proto/dxos/rpc";
881
+ var PortTracer = class {
882
+ constructor(_wrappedPort) {
883
+ this._wrappedPort = _wrappedPort;
884
+ this.message = new Event();
885
+ this._port = {
886
+ send: (msg) => {
887
+ this.message.emit({
888
+ direction: MessageTrace.Direction.OUTGOING,
889
+ data: msg
890
+ });
891
+ return this._wrappedPort.send(msg);
892
+ },
893
+ subscribe: (cb) => {
894
+ return this._wrappedPort.subscribe((msg) => {
895
+ this.message.emit({
896
+ direction: MessageTrace.Direction.INCOMING,
897
+ data: msg
898
+ });
899
+ cb(msg);
900
+ });
901
+ }
902
+ };
903
+ }
904
+ get port() {
905
+ return this._port;
906
+ }
907
+ };
908
+ export {
909
+ PortTracer,
910
+ ProtoRpcPeer,
911
+ RpcPeer,
912
+ createBundledRpcClient,
913
+ createBundledRpcServer,
914
+ createLinkedPorts,
915
+ createProtoRpcPeer,
916
+ createRpcClient,
917
+ createRpcServer,
918
+ createServiceBundle,
919
+ decodeRpcError,
920
+ encodeMessage,
921
+ parseMethodName
922
+ };
923
+ //# sourceMappingURL=index.mjs.map