@dxos/echo-pipeline 0.6.12-staging.e11e696 → 0.6.13-main.548ca8d

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 (46) hide show
  1. package/dist/lib/browser/{chunk-YTSGJIY7.mjs → chunk-PESZVYAN.mjs} +25 -11
  2. package/dist/lib/browser/chunk-PESZVYAN.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +100 -38
  4. package/dist/lib/browser/index.mjs.map +3 -3
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +1 -1
  7. package/dist/lib/browser/testing/index.mjs.map +2 -2
  8. package/dist/lib/node/{chunk-6SSC2M4R.cjs → chunk-6EZVIJNE.cjs} +28 -14
  9. package/dist/lib/node/chunk-6EZVIJNE.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +123 -61
  11. package/dist/lib/node/index.cjs.map +3 -3
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +10 -10
  14. package/dist/lib/node/testing/index.cjs.map +2 -2
  15. package/dist/lib/node-esm/{chunk-ASOERPR6.mjs → chunk-4LW7MDPZ.mjs} +25 -11
  16. package/dist/lib/node-esm/chunk-4LW7MDPZ.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +100 -38
  18. package/dist/lib/node-esm/index.mjs.map +3 -3
  19. package/dist/lib/node-esm/meta.json +1 -1
  20. package/dist/lib/node-esm/testing/index.mjs +1 -1
  21. package/dist/lib/node-esm/testing/index.mjs.map +2 -2
  22. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
  23. package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
  24. package/dist/types/src/automerge/echo-replicator.d.ts +3 -3
  25. package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
  26. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +3 -3
  27. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -1
  28. package/dist/types/src/edge/echo-edge-replicator.d.ts.map +1 -1
  29. package/dist/types/src/edge/echo-edge-replicator.test.d.ts +2 -0
  30. package/dist/types/src/edge/echo-edge-replicator.test.d.ts.map +1 -0
  31. package/dist/types/src/metadata/metadata-store.d.ts +4 -1
  32. package/dist/types/src/metadata/metadata-store.d.ts.map +1 -1
  33. package/dist/types/src/testing/test-replicator.d.ts +4 -4
  34. package/dist/types/src/testing/test-replicator.d.ts.map +1 -1
  35. package/package.json +34 -34
  36. package/src/automerge/automerge-host.ts +4 -1
  37. package/src/automerge/echo-network-adapter.ts +5 -4
  38. package/src/automerge/echo-replicator.ts +3 -3
  39. package/src/automerge/mesh-echo-replicator-connection.ts +10 -9
  40. package/src/edge/echo-edge-replicator.test.ts +96 -0
  41. package/src/edge/echo-edge-replicator.ts +70 -19
  42. package/src/metadata/metadata-store.ts +20 -0
  43. package/src/testing/test-replicator.ts +3 -3
  44. package/dist/lib/browser/chunk-YTSGJIY7.mjs.map +0 -7
  45. package/dist/lib/node/chunk-6SSC2M4R.cjs.map +0 -7
  46. package/dist/lib/node-esm/chunk-ASOERPR6.mjs.map +0 -7
@@ -22,7 +22,7 @@ import {
22
22
  mapTimeframeToFeedIndexes,
23
23
  startAfter,
24
24
  valueEncoding
25
- } from "./chunk-YTSGJIY7.mjs";
25
+ } from "./chunk-PESZVYAN.mjs";
26
26
 
27
27
  // packages/core/echo/echo-pipeline/src/db-host/data-service.ts
28
28
  import { UpdateScheduler as UpdateScheduler2 } from "@dxos/async";
@@ -445,7 +445,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
445
445
  this._lifecycleState = LifecycleState.OPEN;
446
446
  log3("emit ready", void 0, {
447
447
  F: __dxlog_file3,
448
- L: 81,
448
+ L: 82,
449
449
  S: this,
450
450
  C: (f, a) => f(...a)
451
451
  });
@@ -471,7 +471,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
471
471
  async addReplicator(replicator) {
472
472
  invariant2(this._lifecycleState === LifecycleState.OPEN, void 0, {
473
473
  F: __dxlog_file3,
474
- L: 107,
474
+ L: 108,
475
475
  S: this,
476
476
  A: [
477
477
  "this._lifecycleState === LifecycleState.OPEN",
@@ -480,7 +480,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
480
480
  });
481
481
  invariant2(this.peerId, void 0, {
482
482
  F: __dxlog_file3,
483
- L: 108,
483
+ L: 109,
484
484
  S: this,
485
485
  A: [
486
486
  "this.peerId",
@@ -489,7 +489,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
489
489
  });
490
490
  invariant2(!this._replicators.has(replicator), void 0, {
491
491
  F: __dxlog_file3,
492
- L: 109,
492
+ L: 110,
493
493
  S: this,
494
494
  A: [
495
495
  "!this._replicators.has(replicator)",
@@ -513,7 +513,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
513
513
  async removeReplicator(replicator) {
514
514
  invariant2(this._lifecycleState === LifecycleState.OPEN, void 0, {
515
515
  F: __dxlog_file3,
516
- L: 128,
516
+ L: 129,
517
517
  S: this,
518
518
  A: [
519
519
  "this._lifecycleState === LifecycleState.OPEN",
@@ -522,7 +522,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
522
522
  });
523
523
  invariant2(this._replicators.has(replicator), void 0, {
524
524
  F: __dxlog_file3,
525
- L: 129,
525
+ L: 130,
526
526
  S: this,
527
527
  A: [
528
528
  "this._replicators.has(replicator)",
@@ -578,7 +578,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
578
578
  if (connectionEntry.isOpen) {
579
579
  log3.catch(err, void 0, {
580
580
  F: __dxlog_file3,
581
- L: 189,
581
+ L: 190,
582
582
  S: this,
583
583
  C: (f, a) => f(...a)
584
584
  });
@@ -599,13 +599,13 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
599
599
  peerId: connection.peerId
600
600
  }, {
601
601
  F: __dxlog_file3,
602
- L: 207,
602
+ L: 208,
603
603
  S: this,
604
604
  C: (f, a) => f(...a)
605
605
  });
606
606
  invariant2(!this._connections.has(connection.peerId), void 0, {
607
607
  F: __dxlog_file3,
608
- L: 208,
608
+ L: 209,
609
609
  S: this,
610
610
  A: [
611
611
  "!this._connections.has(connection.peerId as PeerId)",
@@ -634,7 +634,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
634
634
  if (connectionEntry.isOpen) {
635
635
  log3.catch(err, void 0, {
636
636
  F: __dxlog_file3,
637
- L: 227,
637
+ L: 228,
638
638
  S: this,
639
639
  C: (f, a) => f(...a)
640
640
  });
@@ -645,7 +645,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
645
645
  peerId: connection.peerId
646
646
  }, {
647
647
  F: __dxlog_file3,
648
- L: 232,
648
+ L: 233,
649
649
  S: this,
650
650
  C: (f, a) => f(...a)
651
651
  });
@@ -671,14 +671,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
671
671
  peerId: connection.peerId
672
672
  }, {
673
673
  F: __dxlog_file3,
674
- L: 253,
674
+ L: 254,
675
675
  S: this,
676
676
  C: (f, a) => f(...a)
677
677
  });
678
678
  const entry = this._connections.get(connection.peerId);
679
679
  invariant2(entry, void 0, {
680
680
  F: __dxlog_file3,
681
- L: 255,
681
+ L: 256,
682
682
  S: this,
683
683
  A: [
684
684
  "entry",
@@ -695,14 +695,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
695
695
  peerId: connection.peerId
696
696
  }, {
697
697
  F: __dxlog_file3,
698
- L: 261,
698
+ L: 262,
699
699
  S: this,
700
700
  C: (f, a) => f(...a)
701
701
  });
702
702
  const entry = this._connections.get(connection.peerId);
703
703
  invariant2(entry, void 0, {
704
704
  F: __dxlog_file3,
705
- L: 263,
705
+ L: 264,
706
706
  S: this,
707
707
  A: [
708
708
  "entry",
@@ -716,13 +716,13 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
716
716
  this._params.monitor?.recordPeerDisconnected(connection.peerId);
717
717
  void entry.reader.cancel().catch((err) => log3.catch(err, void 0, {
718
718
  F: __dxlog_file3,
719
- L: 269,
719
+ L: 270,
720
720
  S: this,
721
721
  C: (f, a) => f(...a)
722
722
  }));
723
723
  void entry.writer.abort().catch((err) => log3.catch(err, void 0, {
724
724
  F: __dxlog_file3,
725
- L: 270,
725
+ L: 271,
726
726
  S: this,
727
727
  C: (f, a) => f(...a)
728
728
  }));
@@ -1169,7 +1169,8 @@ var AutomergeHost = class extends Resource4 {
1169
1169
  * Flush documents to disk.
1170
1170
  */
1171
1171
  async flush({ documentIds } = {}) {
1172
- await this._repo.flush(documentIds);
1172
+ const loadedDocuments = documentIds?.filter((documentId) => !!this._repo.handles[documentId]);
1173
+ await this._repo.flush(loadedDocuments);
1173
1174
  }
1174
1175
  async getHeads(documentIds) {
1175
1176
  const result = [];
@@ -1277,7 +1278,7 @@ var AutomergeHost = class extends Resource4 {
1277
1278
  count: toReplicate.length
1278
1279
  }, {
1279
1280
  F: __dxlog_file4,
1280
- L: 483,
1281
+ L: 486,
1281
1282
  S: this,
1282
1283
  C: (f, a) => f(...a)
1283
1284
  });
@@ -1344,7 +1345,7 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1344
1345
  var decodeCollectionState = (state) => {
1345
1346
  invariant3(typeof state === "object" && state !== null, "Invalid state", {
1346
1347
  F: __dxlog_file4,
1347
- L: 541,
1348
+ L: 544,
1348
1349
  S: void 0,
1349
1350
  A: [
1350
1351
  "typeof state === 'object' && state !== null",
@@ -1390,7 +1391,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1390
1391
  write: async (message, controller) => {
1391
1392
  invariant4(this._isEnabled, "Writing to a disabled connection", {
1392
1393
  F: __dxlog_file5,
1393
- L: 49,
1394
+ L: 50,
1394
1395
  S: this,
1395
1396
  A: [
1396
1397
  "this._isEnabled",
@@ -1423,7 +1424,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1423
1424
  remotePeerId: remotePeerId.toHex()
1424
1425
  }, {
1425
1426
  F: __dxlog_file5,
1426
- L: 84,
1427
+ L: 85,
1427
1428
  S: this,
1428
1429
  C: (f, a) => f(...a)
1429
1430
  });
@@ -1450,7 +1451,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1450
1451
  get peerId() {
1451
1452
  invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1452
1453
  F: __dxlog_file5,
1453
- L: 110,
1454
+ L: 111,
1454
1455
  S: this,
1455
1456
  A: [
1456
1457
  "this._remotePeerId != null",
@@ -1472,7 +1473,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1472
1473
  enable() {
1473
1474
  invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1474
1475
  F: __dxlog_file5,
1475
- L: 127,
1476
+ L: 128,
1476
1477
  S: this,
1477
1478
  A: [
1478
1479
  "this._remotePeerId != null",
@@ -1503,7 +1504,7 @@ var logSendSync = (message) => {
1503
1504
  };
1504
1505
  }, {
1505
1506
  F: __dxlog_file5,
1506
- L: 140,
1507
+ L: 141,
1507
1508
  S: void 0,
1508
1509
  C: (f, a) => f(...a)
1509
1510
  });
@@ -3102,7 +3103,7 @@ var findInlineObjectOfType = (spaceDoc, typename) => {
3102
3103
  };
3103
3104
 
3104
3105
  // packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts
3105
- import { Mutex, Trigger as Trigger2 } from "@dxos/async";
3106
+ import { Mutex, scheduleTask as scheduleTask2 } from "@dxos/async";
3106
3107
  import * as A5 from "@dxos/automerge/automerge";
3107
3108
  import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
3108
3109
  import { Context as Context6, Resource as Resource10 } from "@dxos/context";
@@ -3176,6 +3177,7 @@ function _using_ctx() {
3176
3177
  };
3177
3178
  }
3178
3179
  var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts";
3180
+ var RESTART_DELAY = 500;
3179
3181
  var EchoEdgeReplicator = class {
3180
3182
  constructor({ edgeConnection, disableSharePolicy }) {
3181
3183
  this._mutex = new Mutex();
@@ -3189,17 +3191,18 @@ var EchoEdgeReplicator = class {
3189
3191
  }
3190
3192
  async connect(context) {
3191
3193
  log11.info("connect", {
3192
- peerId: context.peerId
3194
+ peerId: context.peerId,
3195
+ connectedSpaces: this._connectedSpaces.size
3193
3196
  }, {
3194
3197
  F: __dxlog_file16,
3195
- L: 53,
3198
+ L: 58,
3196
3199
  S: this,
3197
3200
  C: (f, a) => f(...a)
3198
3201
  });
3199
3202
  this._context = context;
3200
3203
  this._ctx = Context6.default(void 0, {
3201
3204
  F: __dxlog_file16,
3202
- L: 56
3205
+ L: 61
3203
3206
  });
3204
3207
  this._edgeConnection.reconnect.on(this._ctx, async () => {
3205
3208
  try {
@@ -3231,6 +3234,7 @@ var EchoEdgeReplicator = class {
3231
3234
  try {
3232
3235
  var _usingCtx = _using_ctx();
3233
3236
  const _guard = _usingCtx.u(await this._mutex.acquire());
3237
+ await this._ctx?.dispose();
3234
3238
  for (const connection of this._connections.values()) {
3235
3239
  await connection.close();
3236
3240
  }
@@ -3274,13 +3278,22 @@ var EchoEdgeReplicator = class {
3274
3278
  async _openConnection(spaceId) {
3275
3279
  invariant12(this._context, void 0, {
3276
3280
  F: __dxlog_file16,
3277
- L: 111,
3281
+ L: 117,
3278
3282
  S: this,
3279
3283
  A: [
3280
3284
  "this._context",
3281
3285
  ""
3282
3286
  ]
3283
3287
  });
3288
+ invariant12(!this._connections.has(spaceId), void 0, {
3289
+ F: __dxlog_file16,
3290
+ L: 118,
3291
+ S: this,
3292
+ A: [
3293
+ "!this._connections.has(spaceId)",
3294
+ ""
3295
+ ]
3296
+ });
3284
3297
  const connection = new EdgeReplicatorConnection({
3285
3298
  edgeConnection: this._edgeConnection,
3286
3299
  ownPeerId: this._context.peerId,
@@ -3292,6 +3305,23 @@ var EchoEdgeReplicator = class {
3292
3305
  },
3293
3306
  onRemoteDisconnected: async () => {
3294
3307
  this._context?.onConnectionClosed(connection);
3308
+ },
3309
+ onRestartRequested: async () => {
3310
+ try {
3311
+ var _usingCtx = _using_ctx();
3312
+ const _guard = _usingCtx.u(await this._mutex.acquire());
3313
+ const ctx = this._ctx;
3314
+ await connection.close();
3315
+ this._connections.delete(spaceId);
3316
+ if (ctx?.disposed) {
3317
+ return;
3318
+ }
3319
+ await this._openConnection(spaceId);
3320
+ } catch (_) {
3321
+ _usingCtx.e = _;
3322
+ } finally {
3323
+ _usingCtx.d();
3324
+ }
3295
3325
  }
3296
3326
  });
3297
3327
  this._connections.set(spaceId, connection);
@@ -3299,10 +3329,10 @@ var EchoEdgeReplicator = class {
3299
3329
  }
3300
3330
  };
3301
3331
  var EdgeReplicatorConnection = class extends Resource10 {
3302
- constructor({ edgeConnection, ownPeerId, spaceId, context, sharedPolicyEnabled, onRemoteConnected, onRemoteDisconnected }) {
3332
+ constructor({ edgeConnection, ownPeerId, spaceId, context, sharedPolicyEnabled, onRemoteConnected, onRemoteDisconnected, onRestartRequested }) {
3303
3333
  super();
3304
3334
  this._remotePeerId = null;
3305
- this._streamStarted = new Trigger2();
3335
+ this._restartScheduled = false;
3306
3336
  this._edgeConnection = edgeConnection;
3307
3337
  this._ownPeerId = ownPeerId;
3308
3338
  this._spaceId = spaceId;
@@ -3312,11 +3342,10 @@ var EdgeReplicatorConnection = class extends Resource10 {
3312
3342
  this._sharedPolicyEnabled = sharedPolicyEnabled;
3313
3343
  this._onRemoteConnected = onRemoteConnected;
3314
3344
  this._onRemoteDisconnected = onRemoteDisconnected;
3345
+ this._onRestartRequested = onRestartRequested;
3315
3346
  this.readable = new ReadableStream({
3316
3347
  start: (controller) => {
3317
3348
  this._readableStreamController = controller;
3318
- this._ctx.onDispose(() => controller.close());
3319
- this._streamStarted.wake();
3320
3349
  }
3321
3350
  });
3322
3351
  this.writable = new WritableStream({
@@ -3326,18 +3355,31 @@ var EdgeReplicatorConnection = class extends Resource10 {
3326
3355
  });
3327
3356
  }
3328
3357
  async _open(ctx) {
3358
+ log11("open", void 0, {
3359
+ F: __dxlog_file16,
3360
+ L: 219,
3361
+ S: this,
3362
+ C: (f, a) => f(...a)
3363
+ });
3329
3364
  this._ctx.onDispose(this._edgeConnection.addListener((msg) => {
3330
3365
  this._onMessage(msg);
3331
3366
  }));
3332
3367
  await this._onRemoteConnected();
3333
3368
  }
3334
3369
  async _close() {
3370
+ log11("close", void 0, {
3371
+ F: __dxlog_file16,
3372
+ L: 231,
3373
+ S: this,
3374
+ C: (f, a) => f(...a)
3375
+ });
3376
+ this._readableStreamController.close();
3335
3377
  await this._onRemoteDisconnected();
3336
3378
  }
3337
3379
  get peerId() {
3338
3380
  invariant12(this._remotePeerId, "Not connected", {
3339
3381
  F: __dxlog_file16,
3340
- L: 214,
3382
+ L: 237,
3341
3383
  S: this,
3342
3384
  A: [
3343
3385
  "this._remotePeerId",
@@ -3377,7 +3419,7 @@ var EdgeReplicatorConnection = class extends Resource10 {
3377
3419
  };
3378
3420
  }, {
3379
3421
  F: __dxlog_file16,
3380
- L: 245,
3422
+ L: 268,
3381
3423
  S: this,
3382
3424
  C: (f, a) => f(...a)
3383
3425
  });
@@ -3385,6 +3427,25 @@ var EdgeReplicatorConnection = class extends Resource10 {
3385
3427
  this._processMessage(payload);
3386
3428
  }
3387
3429
  _processMessage(message) {
3430
+ if (isForbiddenErrorMessage(message)) {
3431
+ if (!this._restartScheduled) {
3432
+ log11.warn("Forbidden error received, replicator will restart the connection", {
3433
+ spaceId: this._spaceId,
3434
+ delayMs: RESTART_DELAY,
3435
+ remotePeerId: this._remotePeerId
3436
+ }, {
3437
+ F: __dxlog_file16,
3438
+ L: 288,
3439
+ S: this,
3440
+ C: (f, a) => f(...a)
3441
+ });
3442
+ this._restartScheduled = true;
3443
+ scheduleTask2(this._ctx, async () => {
3444
+ await this._onRestartRequested();
3445
+ }, RESTART_DELAY);
3446
+ }
3447
+ return;
3448
+ }
3388
3449
  this._readableStreamController.enqueue(message);
3389
3450
  }
3390
3451
  async _sendMessage(message) {
@@ -3396,7 +3457,7 @@ var EdgeReplicatorConnection = class extends Resource10 {
3396
3457
  documentId: message.documentId
3397
3458
  }, {
3398
3459
  F: __dxlog_file16,
3399
- L: 267,
3460
+ L: 312,
3400
3461
  S: this,
3401
3462
  C: (f, a) => f(...a)
3402
3463
  });
@@ -3413,6 +3474,7 @@ var EdgeReplicatorConnection = class extends Resource10 {
3413
3474
  }));
3414
3475
  }
3415
3476
  };
3477
+ var isForbiddenErrorMessage = (message) => message.type === "error" && message.message === "Forbidden";
3416
3478
  export {
3417
3479
  AuthExtension,
3418
3480
  AuthStatus,