@dxos/rpc 0.8.3 → 0.8.4-main.1068cf700f

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