@dxos/edge-client 0.8.1 → 0.8.2-main.2f9c567

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.
Files changed (53) hide show
  1. package/dist/lib/browser/chunk-TKYUZ5ZK.mjs +302 -0
  2. package/dist/lib/browser/chunk-TKYUZ5ZK.mjs.map +7 -0
  3. package/dist/lib/browser/edge-ws-muxer.mjs +11 -0
  4. package/dist/lib/browser/edge-ws-muxer.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +93 -49
  6. package/dist/lib/browser/index.mjs.map +3 -3
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +32 -20
  9. package/dist/lib/browser/testing/index.mjs.map +3 -3
  10. package/dist/lib/node/chunk-ZOL3YSDR.cjs +322 -0
  11. package/dist/lib/node/chunk-ZOL3YSDR.cjs.map +7 -0
  12. package/dist/lib/node/edge-ws-muxer.cjs +33 -0
  13. package/dist/lib/node/edge-ws-muxer.cjs.map +7 -0
  14. package/dist/lib/node/index.cjs +105 -61
  15. package/dist/lib/node/index.cjs.map +3 -3
  16. package/dist/lib/node/meta.json +1 -1
  17. package/dist/lib/node/testing/index.cjs +32 -21
  18. package/dist/lib/node/testing/index.cjs.map +3 -3
  19. package/dist/lib/node-esm/chunk-25HGRGNZ.mjs +304 -0
  20. package/dist/lib/node-esm/chunk-25HGRGNZ.mjs.map +7 -0
  21. package/dist/lib/node-esm/edge-ws-muxer.mjs +12 -0
  22. package/dist/lib/node-esm/edge-ws-muxer.mjs.map +7 -0
  23. package/dist/lib/node-esm/index.mjs +93 -49
  24. package/dist/lib/node-esm/index.mjs.map +3 -3
  25. package/dist/lib/node-esm/meta.json +1 -1
  26. package/dist/lib/node-esm/testing/index.mjs +32 -20
  27. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  28. package/dist/types/src/edge-client.d.ts +7 -2
  29. package/dist/types/src/edge-client.d.ts.map +1 -1
  30. package/dist/types/src/edge-ws-connection.d.ts +1 -0
  31. package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
  32. package/dist/types/src/edge-ws-muxer.d.ts +35 -0
  33. package/dist/types/src/edge-ws-muxer.d.ts.map +1 -0
  34. package/dist/types/src/edge-ws-muxer.test.d.ts +2 -0
  35. package/dist/types/src/edge-ws-muxer.test.d.ts.map +1 -0
  36. package/dist/types/src/index.d.ts +1 -0
  37. package/dist/types/src/index.d.ts.map +1 -1
  38. package/dist/types/src/testing/test-utils.d.ts +6 -2
  39. package/dist/types/src/testing/test-utils.d.ts.map +1 -1
  40. package/package.json +19 -14
  41. package/src/edge-client.test.ts +5 -4
  42. package/src/edge-client.ts +16 -8
  43. package/src/edge-ws-connection.ts +36 -18
  44. package/src/edge-ws-muxer.test.ts +55 -0
  45. package/src/edge-ws-muxer.ts +217 -0
  46. package/src/index.ts +1 -0
  47. package/src/testing/test-utils.ts +33 -26
  48. package/dist/lib/browser/chunk-ZWJXA37R.mjs +0 -113
  49. package/dist/lib/browser/chunk-ZWJXA37R.mjs.map +0 -7
  50. package/dist/lib/node/chunk-ANV2HBEH.cjs +0 -136
  51. package/dist/lib/node/chunk-ANV2HBEH.cjs.map +0 -7
  52. package/dist/lib/node-esm/chunk-HNVT57AU.mjs +0 -115
  53. package/dist/lib/node-esm/chunk-HNVT57AU.mjs.map +0 -7
@@ -1,17 +1,21 @@
1
1
  import {
2
+ CLOUDFLARE_MESSAGE_MAX_BYTES,
3
+ CLOUDFLARE_RPC_MAX_BYTES,
2
4
  Protocol,
5
+ WebSocketMuxer,
3
6
  getTypename,
4
7
  protocol,
5
8
  toUint8Array
6
- } from "./chunk-ZWJXA37R.mjs";
9
+ } from "./chunk-TKYUZ5ZK.mjs";
7
10
 
8
11
  // packages/core/mesh/edge-client/src/index.ts
9
12
  export * from "@dxos/protocols/buf/dxos/edge/messenger_pb";
10
13
 
11
14
  // packages/core/mesh/edge-client/src/edge-client.ts
12
- import { Trigger, scheduleMicroTask, TriggerState, PersistentLifecycle } from "@dxos/async";
15
+ import { Trigger, scheduleMicroTask, TriggerState, PersistentLifecycle, Event } from "@dxos/async";
13
16
  import { Resource as Resource2 } from "@dxos/context";
14
17
  import { log as log2, logInfo as logInfo2 } from "@dxos/log";
18
+ import { EdgeStatus } from "@dxos/protocols/proto/dxos/client/services";
15
19
 
16
20
  // packages/core/mesh/edge-client/src/edge-identity.ts
17
21
  import { invariant } from "@dxos/invariant";
@@ -59,6 +63,7 @@ import { scheduleTask, scheduleTaskInterval } from "@dxos/async";
59
63
  import { Context, Resource } from "@dxos/context";
60
64
  import { invariant as invariant2 } from "@dxos/invariant";
61
65
  import { log, logInfo } from "@dxos/log";
66
+ import { EdgeWebsocketProtocol } from "@dxos/protocols";
62
67
  import { buf } from "@dxos/protocols/buf";
63
68
  import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
64
69
  function _ts_decorate(decorators, target, key, desc) {
@@ -70,7 +75,6 @@ function _ts_decorate(decorators, target, key, desc) {
70
75
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-ws-connection.ts";
71
76
  var SIGNAL_KEEPALIVE_INTERVAL = 4e3;
72
77
  var SIGNAL_KEEPALIVE_TIMEOUT = 12e3;
73
- var CLOUDFLARE_MESSAGE_LENGTH_LIMIT = 1024 * 1024;
74
78
  var EdgeWsConnection = class extends Resource {
75
79
  constructor(_identity, _connectionInfo, _callbacks) {
76
80
  super();
@@ -88,47 +92,73 @@ var EdgeWsConnection = class extends Resource {
88
92
  send(message) {
89
93
  invariant2(this._ws, void 0, {
90
94
  F: __dxlog_file2,
91
- L: 54,
95
+ L: 52,
92
96
  S: this,
93
97
  A: [
94
98
  "this._ws",
95
99
  ""
96
100
  ]
97
101
  });
102
+ invariant2(this._wsMuxer, void 0, {
103
+ F: __dxlog_file2,
104
+ L: 53,
105
+ S: this,
106
+ A: [
107
+ "this._wsMuxer",
108
+ ""
109
+ ]
110
+ });
98
111
  log("sending...", {
99
112
  peerKey: this._identity.peerKey,
100
113
  payload: protocol.getPayloadType(message)
101
114
  }, {
102
115
  F: __dxlog_file2,
103
- L: 55,
116
+ L: 54,
104
117
  S: this,
105
118
  C: (f, a) => f(...a)
106
119
  });
107
- const encoded = buf.toBinary(MessageSchema, message);
108
- if (encoded.byteLength >= CLOUDFLARE_MESSAGE_LENGTH_LIMIT) {
109
- log.error("edge message dropped due to websocket message limit", {
110
- byteLength: encoded.byteLength,
111
- serviceId: message.serviceId,
112
- payload: protocol.getPayloadType(message)
113
- }, {
120
+ if (this._ws?.protocol.includes(EdgeWebsocketProtocol.V0)) {
121
+ const binary = buf.toBinary(MessageSchema, message);
122
+ if (binary.length > CLOUDFLARE_MESSAGE_MAX_BYTES) {
123
+ log.error("Message dropped because it was too large (>1MB).", {
124
+ byteLength: binary.byteLength,
125
+ serviceId: message.serviceId,
126
+ payload: protocol.getPayloadType(message)
127
+ }, {
128
+ F: __dxlog_file2,
129
+ L: 58,
130
+ S: this,
131
+ C: (f, a) => f(...a)
132
+ });
133
+ return;
134
+ }
135
+ this._ws.send(binary);
136
+ } else {
137
+ this._wsMuxer.send(message).catch((e) => log.catch(e, void 0, {
114
138
  F: __dxlog_file2,
115
- L: 58,
139
+ L: 67,
116
140
  S: this,
117
141
  C: (f, a) => f(...a)
118
- });
119
- return;
142
+ }));
120
143
  }
121
- this._ws.send(encoded);
122
144
  }
123
145
  async _open() {
146
+ const baseProtocols = [
147
+ ...Object.values(EdgeWebsocketProtocol)
148
+ ];
124
149
  this._ws = new WebSocket(this._connectionInfo.url.toString(), this._connectionInfo.protocolHeader ? [
150
+ ...baseProtocols,
125
151
  this._connectionInfo.protocolHeader
126
- ] : []);
152
+ ] : [
153
+ ...baseProtocols
154
+ ]);
155
+ const muxer = new WebSocketMuxer(this._ws);
156
+ this._wsMuxer = muxer;
127
157
  this._ws.onopen = () => {
128
158
  if (this.isOpen) {
129
159
  log("connected", void 0, {
130
160
  F: __dxlog_file2,
131
- L: 76,
161
+ L: 84,
132
162
  S: this,
133
163
  C: (f, a) => f(...a)
134
164
  });
@@ -139,7 +169,7 @@ var EdgeWsConnection = class extends Resource {
139
169
  currentIdentity: this._identity
140
170
  }, {
141
171
  F: __dxlog_file2,
142
- L: 80,
172
+ L: 88,
143
173
  S: this,
144
174
  C: (f, a) => f(...a)
145
175
  });
@@ -152,11 +182,12 @@ var EdgeWsConnection = class extends Resource {
152
182
  reason: event.reason
153
183
  }, {
154
184
  F: __dxlog_file2,
155
- L: 85,
185
+ L: 93,
156
186
  S: this,
157
187
  C: (f, a) => f(...a)
158
188
  });
159
189
  this._callbacks.onRestartRequired();
190
+ muxer.destroy();
160
191
  }
161
192
  };
162
193
  this._ws.onerror = (event) => {
@@ -166,7 +197,7 @@ var EdgeWsConnection = class extends Resource {
166
197
  info: event.message
167
198
  }, {
168
199
  F: __dxlog_file2,
169
- L: 91,
200
+ L: 100,
170
201
  S: this,
171
202
  C: (f, a) => f(...a)
172
203
  });
@@ -176,7 +207,7 @@ var EdgeWsConnection = class extends Resource {
176
207
  error: event.error
177
208
  }, {
178
209
  F: __dxlog_file2,
179
- L: 94,
210
+ L: 103,
180
211
  S: this,
181
212
  C: (f, a) => f(...a)
182
213
  });
@@ -188,7 +219,7 @@ var EdgeWsConnection = class extends Resource {
188
219
  event: event.type
189
220
  }, {
190
221
  F: __dxlog_file2,
191
- L: 102,
222
+ L: 111,
192
223
  S: this,
193
224
  C: (f, a) => f(...a)
194
225
  });
@@ -198,15 +229,18 @@ var EdgeWsConnection = class extends Resource {
198
229
  this._rescheduleHeartbeatTimeout();
199
230
  return;
200
231
  }
201
- const data = await toUint8Array(event.data);
202
- if (this.isOpen) {
203
- const message = buf.fromBinary(MessageSchema, data);
232
+ const bytes = await toUint8Array(event.data);
233
+ if (!this.isOpen) {
234
+ return;
235
+ }
236
+ const message = this._ws?.protocol?.includes(EdgeWebsocketProtocol.V0) ? buf.fromBinary(MessageSchema, bytes) : muxer.receiveData(bytes);
237
+ if (message) {
204
238
  log("received", {
205
239
  from: message.source,
206
240
  payload: protocol.getPayloadType(message)
207
241
  }, {
208
242
  F: __dxlog_file2,
209
- L: 112,
243
+ L: 128,
210
244
  S: this,
211
245
  C: (f, a) => f(...a)
212
246
  });
@@ -220,6 +254,8 @@ var EdgeWsConnection = class extends Resource {
220
254
  try {
221
255
  this._ws?.close();
222
256
  this._ws = void 0;
257
+ this._wsMuxer?.destroy();
258
+ this._wsMuxer = void 0;
223
259
  } catch (err) {
224
260
  if (err instanceof Error && err.message.includes("WebSocket is closed before the connection is established.")) {
225
261
  return;
@@ -228,7 +264,7 @@ var EdgeWsConnection = class extends Resource {
228
264
  err
229
265
  }, {
230
266
  F: __dxlog_file2,
231
- L: 128,
267
+ L: 146,
232
268
  S: this,
233
269
  C: (f, a) => f(...a)
234
270
  });
@@ -237,7 +273,7 @@ var EdgeWsConnection = class extends Resource {
237
273
  _scheduleHeartbeats() {
238
274
  invariant2(this._ws, void 0, {
239
275
  F: __dxlog_file2,
240
- L: 133,
276
+ L: 151,
241
277
  S: this,
242
278
  A: [
243
279
  "this._ws",
@@ -257,13 +293,13 @@ var EdgeWsConnection = class extends Resource {
257
293
  void this._inactivityTimeoutCtx?.dispose();
258
294
  this._inactivityTimeoutCtx = new Context(void 0, {
259
295
  F: __dxlog_file2,
260
- L: 152
296
+ L: 170
261
297
  });
262
298
  scheduleTask(this._inactivityTimeoutCtx, () => {
263
299
  if (this.isOpen) {
264
300
  log.warn("restart due to inactivity timeout", void 0, {
265
301
  F: __dxlog_file2,
266
- L: 157,
302
+ L: 175,
267
303
  S: this,
268
304
  C: (f, a) => f(...a)
269
305
  });
@@ -310,6 +346,7 @@ var EdgeClient = class extends Resource2 {
310
346
  super();
311
347
  this._identity = _identity;
312
348
  this._config = _config;
349
+ this.statusChanged = new Event();
313
350
  this._persistentLifecycle = new PersistentLifecycle({
314
351
  start: async () => this._connect(),
315
352
  stop: async (state) => this._disconnect(state)
@@ -325,12 +362,13 @@ var EdgeClient = class extends Resource2 {
325
362
  get info() {
326
363
  return {
327
364
  open: this.isOpen,
365
+ status: this.status,
328
366
  identity: this._identity.identityKey,
329
367
  device: this._identity.peerKey
330
368
  };
331
369
  }
332
- get isConnected() {
333
- return Boolean(this._currentConnection) && this._ready.state === TriggerState.RESOLVED;
370
+ get status() {
371
+ return Boolean(this._currentConnection) && this._ready.state === TriggerState.RESOLVED ? EdgeStatus.CONNECTED : EdgeStatus.NOT_CONNECTED;
334
372
  }
335
373
  get identityKey() {
336
374
  return this._identity.identityKey;
@@ -345,13 +383,13 @@ var EdgeClient = class extends Resource2 {
345
383
  oldIdentity: this._identity
346
384
  }, {
347
385
  F: __dxlog_file3,
348
- L: 94,
386
+ L: 99,
349
387
  S: this,
350
388
  C: (f, a) => f(...a)
351
389
  });
352
390
  this._identity = identity;
353
391
  this._closeCurrentConnection(new EdgeIdentityChangedError());
354
- this._persistentLifecycle.scheduleRestart();
392
+ void this._persistentLifecycle.scheduleRestart();
355
393
  }
356
394
  }
357
395
  onMessage(listener) {
@@ -368,7 +406,7 @@ var EdgeClient = class extends Resource2 {
368
406
  } catch (error) {
369
407
  log2.catch(error, void 0, {
370
408
  F: __dxlog_file3,
371
- L: 116,
409
+ L: 121,
372
410
  S: this,
373
411
  C: (f, a) => f(...a)
374
412
  });
@@ -386,7 +424,7 @@ var EdgeClient = class extends Resource2 {
386
424
  info: this.info
387
425
  }, {
388
426
  F: __dxlog_file3,
389
- L: 128,
427
+ L: 133,
390
428
  S: this,
391
429
  C: (f, a) => f(...a)
392
430
  });
@@ -395,7 +433,7 @@ var EdgeClient = class extends Resource2 {
395
433
  err
396
434
  }, {
397
435
  F: __dxlog_file3,
398
- L: 130,
436
+ L: 135,
399
437
  S: this,
400
438
  C: (f, a) => f(...a)
401
439
  });
@@ -409,7 +447,7 @@ var EdgeClient = class extends Resource2 {
409
447
  peerKey: this._identity.peerKey
410
448
  }, {
411
449
  F: __dxlog_file3,
412
- L: 138,
450
+ L: 143,
413
451
  S: this,
414
452
  C: (f, a) => f(...a)
415
453
  });
@@ -426,7 +464,7 @@ var EdgeClient = class extends Resource2 {
426
464
  if (this._identity !== identity) {
427
465
  log2("identity changed during auth header request", void 0, {
428
466
  F: __dxlog_file3,
429
- L: 152,
467
+ L: 157,
430
468
  S: this,
431
469
  C: (f, a) => f(...a)
432
470
  });
@@ -439,7 +477,7 @@ var EdgeClient = class extends Resource2 {
439
477
  protocolHeader
440
478
  }, {
441
479
  F: __dxlog_file3,
442
- L: 158,
480
+ L: 163,
443
481
  S: this,
444
482
  C: (f, a) => f(...a)
445
483
  });
@@ -454,7 +492,7 @@ var EdgeClient = class extends Resource2 {
454
492
  } else {
455
493
  log2.verbose("connected callback ignored, because connection is not active", void 0, {
456
494
  F: __dxlog_file3,
457
- L: 168,
495
+ L: 173,
458
496
  S: this,
459
497
  C: (f, a) => f(...a)
460
498
  });
@@ -463,11 +501,11 @@ var EdgeClient = class extends Resource2 {
463
501
  onRestartRequired: () => {
464
502
  if (this._isActive(connection)) {
465
503
  this._closeCurrentConnection();
466
- this._persistentLifecycle.scheduleRestart();
504
+ void this._persistentLifecycle.scheduleRestart();
467
505
  } else {
468
506
  log2.verbose("restart requested by inactive connection", void 0, {
469
507
  F: __dxlog_file3,
470
- L: 176,
508
+ L: 181,
471
509
  S: this,
472
510
  C: (f, a) => f(...a)
473
511
  });
@@ -483,7 +521,7 @@ var EdgeClient = class extends Resource2 {
483
521
  type: message.payload?.typeUrl
484
522
  }, {
485
523
  F: __dxlog_file3,
486
- L: 184,
524
+ L: 189,
487
525
  S: this,
488
526
  C: (f, a) => f(...a)
489
527
  });
@@ -502,13 +540,16 @@ var EdgeClient = class extends Resource2 {
502
540
  }
503
541
  async _disconnect(state) {
504
542
  await state.close();
543
+ this.statusChanged.emit(this.status);
505
544
  }
506
545
  _closeCurrentConnection(error = new EdgeConnectionClosedError()) {
507
546
  this._currentConnection = void 0;
508
547
  this._ready.throw(error);
509
548
  this._ready.reset();
549
+ this.statusChanged.emit(this.status);
510
550
  }
511
551
  _notifyReconnected() {
552
+ this.statusChanged.emit(this.status);
512
553
  for (const listener of this._reconnectListeners) {
513
554
  try {
514
555
  listener();
@@ -517,7 +558,7 @@ var EdgeClient = class extends Resource2 {
517
558
  err
518
559
  }, {
519
560
  F: __dxlog_file3,
520
- L: 217,
561
+ L: 225,
521
562
  S: this,
522
563
  C: (f, a) => f(...a)
523
564
  });
@@ -534,7 +575,7 @@ var EdgeClient = class extends Resource2 {
534
575
  payload: protocol.getPayloadType(message)
535
576
  }, {
536
577
  F: __dxlog_file3,
537
- L: 227,
578
+ L: 235,
538
579
  S: this,
539
580
  C: (f, a) => f(...a)
540
581
  });
@@ -549,7 +590,7 @@ var EdgeClient = class extends Resource2 {
549
590
  if (this._ready.state !== TriggerState.RESOLVED) {
550
591
  log2("waiting for websocket to become ready", void 0, {
551
592
  F: __dxlog_file3,
552
- L: 238,
593
+ L: 246,
553
594
  S: this,
554
595
  C: (f, a) => f(...a)
555
596
  });
@@ -579,7 +620,7 @@ var EdgeClient = class extends Resource2 {
579
620
  statusText: response.statusText
580
621
  }, {
581
622
  F: __dxlog_file3,
582
- L: 263,
623
+ L: 271,
583
624
  S: this,
584
625
  C: (f, a) => f(...a)
585
626
  });
@@ -985,11 +1026,14 @@ var encodeAuthHeader = (challenge) => {
985
1026
  return `VerifiablePresentation pb;base64,${encodedChallenge}`;
986
1027
  };
987
1028
  export {
1029
+ CLOUDFLARE_MESSAGE_MAX_BYTES,
1030
+ CLOUDFLARE_RPC_MAX_BYTES,
988
1031
  EdgeClient,
989
1032
  EdgeConnectionClosedError,
990
1033
  EdgeHttpClient,
991
1034
  EdgeIdentityChangedError,
992
1035
  Protocol,
1036
+ WebSocketMuxer,
993
1037
  createChainEdgeIdentity,
994
1038
  createDeviceEdgeIdentity,
995
1039
  createEphemeralEdgeIdentity,