@dxos/rpc 0.6.13 → 0.6.14-main.1366248

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