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