@dxos/echo-pipeline 0.7.4 → 0.7.5-feature-compute.4d9d99a

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 (51) hide show
  1. package/dist/lib/browser/{chunk-LZK5YFYE.mjs → chunk-QBMTPEMY.mjs} +111 -38
  2. package/dist/lib/browser/chunk-QBMTPEMY.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +171 -77
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +1 -1
  7. package/dist/lib/node/{chunk-MACQJ2EP.cjs → chunk-NPZ57MV5.cjs} +110 -40
  8. package/dist/lib/node/chunk-NPZ57MV5.cjs.map +7 -0
  9. package/dist/lib/node/index.cjs +184 -94
  10. package/dist/lib/node/index.cjs.map +4 -4
  11. package/dist/lib/node/meta.json +1 -1
  12. package/dist/lib/node/testing/index.cjs +10 -10
  13. package/dist/lib/node-esm/{chunk-JIZPSASG.mjs → chunk-OY5N3ZIV.mjs} +111 -38
  14. package/dist/lib/node-esm/chunk-OY5N3ZIV.mjs.map +7 -0
  15. package/dist/lib/node-esm/index.mjs +171 -77
  16. package/dist/lib/node-esm/index.mjs.map +4 -4
  17. package/dist/lib/node-esm/meta.json +1 -1
  18. package/dist/lib/node-esm/testing/index.mjs +1 -1
  19. package/dist/types/src/automerge/automerge-host.d.ts +5 -1
  20. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
  21. package/dist/types/src/automerge/collection-synchronizer.d.ts +1 -0
  22. package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
  23. package/dist/types/src/automerge/echo-network-adapter.d.ts +1 -0
  24. package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
  25. package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +1 -1
  26. package/dist/types/src/db-host/echo-host.d.ts +3 -2
  27. package/dist/types/src/db-host/echo-host.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/inflight-request-limiter.d.ts +24 -0
  30. package/dist/types/src/edge/inflight-request-limiter.d.ts.map +1 -0
  31. package/dist/types/src/pipeline/pipeline.d.ts +1 -0
  32. package/dist/types/src/pipeline/pipeline.d.ts.map +1 -1
  33. package/dist/types/src/space/control-pipeline.d.ts +9 -0
  34. package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
  35. package/dist/types/src/space/space-manager.d.ts +1 -0
  36. package/dist/types/src/space/space-manager.d.ts.map +1 -1
  37. package/dist/types/tsconfig.tsbuildinfo +1 -0
  38. package/package.json +34 -34
  39. package/src/automerge/automerge-host.ts +49 -14
  40. package/src/automerge/collection-synchronizer.ts +8 -4
  41. package/src/automerge/echo-network-adapter.ts +7 -0
  42. package/src/automerge/mesh-echo-replicator.ts +2 -2
  43. package/src/db-host/echo-host.ts +4 -1
  44. package/src/edge/echo-edge-replicator.ts +34 -18
  45. package/src/edge/inflight-request-limiter.ts +69 -0
  46. package/src/pipeline/pipeline.ts +9 -1
  47. package/src/space/control-pipeline.ts +25 -2
  48. package/src/space/space-manager.ts +17 -1
  49. package/dist/lib/browser/chunk-LZK5YFYE.mjs.map +0 -7
  50. package/dist/lib/node/chunk-MACQJ2EP.cjs.map +0 -7
  51. package/dist/lib/node-esm/chunk-JIZPSASG.mjs.map +0 -7
@@ -21,7 +21,7 @@ import {
21
21
  mapTimeframeToFeedIndexes,
22
22
  startAfter,
23
23
  valueEncoding
24
- } from "./chunk-LZK5YFYE.mjs";
24
+ } from "./chunk-QBMTPEMY.mjs";
25
25
 
26
26
  // packages/core/echo/echo-pipeline/src/db-host/data-service.ts
27
27
  import { UpdateScheduler as UpdateScheduler2 } from "@dxos/async";
@@ -310,7 +310,7 @@ var CollectionSynchronizer = class extends Resource2 {
310
310
  return;
311
311
  }
312
312
  for (const [collectionId, state] of this._perCollectionStates.entries()) {
313
- if (this._shouldSyncCollection(collectionId, peerId)) {
313
+ if (this._activeCollections.has(collectionId) && this._shouldSyncCollection(collectionId, peerId)) {
314
314
  state.interestedPeers.add(peerId);
315
315
  state.lastQueried.set(peerId, Date.now());
316
316
  this._queryCollectionState(collectionId, peerId);
@@ -352,11 +352,18 @@ var CollectionSynchronizer = class extends Resource2 {
352
352
  });
353
353
  validateCollectionState(state);
354
354
  const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
355
- perCollectionState.remoteStates.set(peerId, state);
356
- this.remoteStateUpdated.emit({
357
- peerId,
358
- collectionId
359
- });
355
+ const existingState = perCollectionState.remoteStates.get(peerId) ?? {
356
+ documents: {}
357
+ };
358
+ const diff = diffCollectionState(existingState, state);
359
+ if (diff.missingOnLocal.length > 0 || diff.different.length > 0) {
360
+ perCollectionState.remoteStates.set(peerId, state);
361
+ this.remoteStateUpdated.emit({
362
+ peerId,
363
+ collectionId,
364
+ newDocsAppeared: diff.missingOnLocal.length > 0
365
+ });
366
+ }
360
367
  }
361
368
  _getOrCreatePerCollectionState(collectionId) {
362
369
  return defaultMap(this._perCollectionStates, collectionId, () => ({
@@ -677,6 +684,12 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
677
684
  }
678
685
  this._params.monitor?.recordMessageReceived(message);
679
686
  }
687
+ onConnectionAuthScopeChanged(peer) {
688
+ const entry = this._connections.get(peer);
689
+ if (entry) {
690
+ this._onConnectionAuthScopeChanged(entry.connection);
691
+ }
692
+ }
680
693
  /**
681
694
  * Trigger doc-synchronizer shared documents set recalculation. Happens on peer-candidate.
682
695
  * TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
@@ -686,14 +699,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
686
699
  peerId: connection.peerId
687
700
  }, {
688
701
  F: __dxlog_file3,
689
- L: 254,
702
+ L: 261,
690
703
  S: this,
691
704
  C: (f, a) => f(...a)
692
705
  });
693
706
  const entry = this._connections.get(connection.peerId);
694
707
  invariant2(entry, void 0, {
695
708
  F: __dxlog_file3,
696
- L: 256,
709
+ L: 263,
697
710
  S: this,
698
711
  A: [
699
712
  "entry",
@@ -710,14 +723,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
710
723
  peerId: connection.peerId
711
724
  }, {
712
725
  F: __dxlog_file3,
713
- L: 262,
726
+ L: 269,
714
727
  S: this,
715
728
  C: (f, a) => f(...a)
716
729
  });
717
730
  const entry = this._connections.get(connection.peerId);
718
731
  invariant2(entry, void 0, {
719
732
  F: __dxlog_file3,
720
- L: 264,
733
+ L: 271,
721
734
  S: this,
722
735
  A: [
723
736
  "entry",
@@ -731,13 +744,13 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
731
744
  this._params.monitor?.recordPeerDisconnected(connection.peerId);
732
745
  void entry.reader.cancel().catch((err) => log3.catch(err, void 0, {
733
746
  F: __dxlog_file3,
734
- L: 270,
747
+ L: 277,
735
748
  S: this,
736
749
  C: (f, a) => f(...a)
737
750
  }));
738
751
  void entry.writer.abort().catch((err) => log3.catch(err, void 0, {
739
752
  F: __dxlog_file3,
740
- L: 271,
753
+ L: 278,
741
754
  S: this,
742
755
  C: (f, a) => f(...a)
743
756
  }));
@@ -905,7 +918,7 @@ function _ts_decorate2(decorators, target, key, desc) {
905
918
  }
906
919
  var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
907
920
  var AutomergeHost = class extends Resource4 {
908
- constructor({ db, indexMetadataStore, dataMonitor, peerIdProvider }) {
921
+ constructor({ db, indexMetadataStore, dataMonitor, peerIdProvider, getSpaceKeyByRootDocumentId }) {
909
922
  super();
910
923
  this._collectionSynchronizer = new CollectionSynchronizer({
911
924
  queryCollectionState: this._queryCollectionState.bind(this),
@@ -934,6 +947,7 @@ var AutomergeHost = class extends Resource4 {
934
947
  });
935
948
  this._indexMetadataStore = indexMetadataStore;
936
949
  this._peerIdProvider = peerIdProvider;
950
+ this._getSpaceKeyByRootDocumentId = getSpaceKeyByRootDocumentId;
937
951
  }
938
952
  async _open() {
939
953
  this._peerId = `host-${this._peerIdProvider?.() ?? PublicKey.random().toHex()}`;
@@ -947,13 +961,22 @@ var AutomergeHost = class extends Resource4 {
947
961
  this._echoNetworkAdapter
948
962
  ]
949
963
  });
950
- Event2.wrap(this._echoNetworkAdapter, "peer-candidate").on(this._ctx, (e) => this._onPeerConnected(e.peerId));
951
- Event2.wrap(this._echoNetworkAdapter, "peer-disconnected").on(this._ctx, (e) => this._onPeerDisconnected(e.peerId));
952
- this._collectionSynchronizer.remoteStateUpdated.on(this._ctx, ({ collectionId, peerId }) => {
964
+ let updatingAuthScope = false;
965
+ Event2.wrap(this._echoNetworkAdapter, "peer-candidate").on(this._ctx, (e) => !updatingAuthScope && this._onPeerConnected(e.peerId));
966
+ Event2.wrap(this._echoNetworkAdapter, "peer-disconnected").on(this._ctx, (e) => !updatingAuthScope && this._onPeerDisconnected(e.peerId));
967
+ this._collectionSynchronizer.remoteStateUpdated.on(this._ctx, ({ collectionId, peerId, newDocsAppeared }) => {
953
968
  this._onRemoteCollectionStateUpdated(collectionId, peerId);
954
969
  this.collectionStateUpdated.emit({
955
970
  collectionId
956
971
  });
972
+ if (newDocsAppeared) {
973
+ updatingAuthScope = true;
974
+ try {
975
+ this._echoNetworkAdapter.onConnectionAuthScopeChanged(peerId);
976
+ } finally {
977
+ updatingAuthScope = false;
978
+ }
979
+ }
957
980
  });
958
981
  await this._echoNetworkAdapter.open();
959
982
  await this._collectionSynchronizer.open();
@@ -1036,7 +1059,7 @@ var AutomergeHost = class extends Resource4 {
1036
1059
  await Promise.all(headsToWait.map(async (entry, index) => {
1037
1060
  const handle = await this.loadDoc(Context.default(void 0, {
1038
1061
  F: __dxlog_file4,
1039
- L: 239
1062
+ L: 264
1040
1063
  }), entry.documentId);
1041
1064
  await waitForHeads(handle, entry.heads);
1042
1065
  }));
@@ -1049,7 +1072,7 @@ var AutomergeHost = class extends Resource4 {
1049
1072
  documentId
1050
1073
  }, {
1051
1074
  F: __dxlog_file4,
1052
- L: 251,
1075
+ L: 276,
1053
1076
  S: this,
1054
1077
  C: (f, a) => f(...a)
1055
1078
  });
@@ -1065,7 +1088,7 @@ var AutomergeHost = class extends Resource4 {
1065
1088
  documentId
1066
1089
  }, {
1067
1090
  F: __dxlog_file4,
1068
- L: 255,
1091
+ L: 280,
1069
1092
  S: this,
1070
1093
  C: (f, a) => f(...a)
1071
1094
  });
@@ -1074,7 +1097,7 @@ var AutomergeHost = class extends Resource4 {
1074
1097
  const doc = handle.docSync();
1075
1098
  invariant3(doc, void 0, {
1076
1099
  F: __dxlog_file4,
1077
- L: 260,
1100
+ L: 285,
1078
1101
  S: this,
1079
1102
  A: [
1080
1103
  "doc",
@@ -1088,7 +1111,7 @@ var AutomergeHost = class extends Resource4 {
1088
1111
  }
1089
1112
  log4.info("done re-indexing heads", void 0, {
1090
1113
  F: __dxlog_file4,
1091
- L: 267,
1114
+ L: 292,
1092
1115
  S: this,
1093
1116
  C: (f, a) => f(...a)
1094
1117
  });
@@ -1172,14 +1195,17 @@ var AutomergeHost = class extends Resource4 {
1172
1195
  }
1173
1196
  async _getContainingSpaceForDocument(documentId) {
1174
1197
  const doc = this._repo.handles[documentId]?.docSync();
1175
- if (!doc) {
1176
- return null;
1198
+ if (doc) {
1199
+ const spaceKeyHex = getSpaceKeyFromDoc(doc);
1200
+ if (spaceKeyHex) {
1201
+ return PublicKey.from(spaceKeyHex);
1202
+ }
1177
1203
  }
1178
- const spaceKeyHex = getSpaceKeyFromDoc(doc);
1179
- if (!spaceKeyHex) {
1180
- return null;
1204
+ const rootDocSpaceKey = this._getSpaceKeyByRootDocumentId?.(documentId);
1205
+ if (rootDocSpaceKey) {
1206
+ return rootDocSpaceKey;
1181
1207
  }
1182
- return PublicKey.from(spaceKeyHex);
1208
+ return null;
1183
1209
  }
1184
1210
  /**
1185
1211
  * Flush documents to disk.
@@ -1293,11 +1319,14 @@ var AutomergeHost = class extends Resource4 {
1293
1319
  if (toReplicate.length === 0) {
1294
1320
  return;
1295
1321
  }
1296
- log4.info("replication documents after collection sync", {
1322
+ log4.info("replicating documents after collection sync", {
1323
+ collectionId,
1324
+ peerId,
1325
+ toReplicate,
1297
1326
  count: toReplicate.length
1298
1327
  }, {
1299
1328
  F: __dxlog_file4,
1300
- L: 499,
1329
+ L: 531,
1301
1330
  S: this,
1302
1331
  C: (f, a) => f(...a)
1303
1332
  });
@@ -1364,7 +1393,7 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1364
1393
  var decodeCollectionState = (state) => {
1365
1394
  invariant3(typeof state === "object" && state !== null, "Invalid state", {
1366
1395
  F: __dxlog_file4,
1367
- L: 557,
1396
+ L: 592,
1368
1397
  S: void 0,
1369
1398
  A: [
1370
1399
  "typeof state === 'object' && state !== null",
@@ -1656,10 +1685,10 @@ var MeshEchoReplicator = class {
1656
1685
  documentId: params.documentId,
1657
1686
  peerId: connection.peerId
1658
1687
  });
1659
- log6("document not found locally for share policy check, accepting the remote document", {
1688
+ log6("document not found locally for share policy check", {
1660
1689
  peerId: connection.peerId,
1661
1690
  documentId: params.documentId,
1662
- remoteDocumentExists
1691
+ acceptDocument: remoteDocumentExists
1663
1692
  }, {
1664
1693
  F: __dxlog_file7,
1665
1694
  L: 91,
@@ -2923,7 +2952,7 @@ var INDEXER_CONFIG = {
2923
2952
  ]
2924
2953
  };
2925
2954
  var EchoHost = class extends Resource9 {
2926
- constructor({ kv, peerIdProvider }) {
2955
+ constructor({ kv, peerIdProvider, getSpaceKeyByRootDocumentId }) {
2927
2956
  super();
2928
2957
  this._spaceStateManager = new SpaceStateManager();
2929
2958
  this._indexMetadataStore = new IndexMetadataStore({
@@ -2934,7 +2963,8 @@ var EchoHost = class extends Resource9 {
2934
2963
  db: kv,
2935
2964
  dataMonitor: this._echoDataMonitor,
2936
2965
  indexMetadataStore: this._indexMetadataStore,
2937
- peerIdProvider
2966
+ peerIdProvider,
2967
+ getSpaceKeyByRootDocumentId
2938
2968
  });
2939
2969
  this._indexer = new Indexer({
2940
2970
  db: kv,
@@ -3059,7 +3089,7 @@ var EchoHost = class extends Resource9 {
3059
3089
  async createSpaceRoot(spaceKey) {
3060
3090
  invariant11(this._lifecycleState === LifecycleState4.OPEN, void 0, {
3061
3091
  F: __dxlog_file15,
3062
- L: 217,
3092
+ L: 220,
3063
3093
  S: this,
3064
3094
  A: [
3065
3095
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3087,7 +3117,7 @@ var EchoHost = class extends Resource9 {
3087
3117
  async openSpaceRoot(spaceId, automergeUrl) {
3088
3118
  invariant11(this._lifecycleState === LifecycleState4.OPEN, void 0, {
3089
3119
  F: __dxlog_file15,
3090
- L: 236,
3120
+ L: 239,
3091
3121
  S: this,
3092
3122
  A: [
3093
3123
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3117,16 +3147,73 @@ var EchoHost = class extends Resource9 {
3117
3147
 
3118
3148
  // packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts
3119
3149
  import { Mutex, scheduleTask as scheduleTask2, scheduleMicroTask } from "@dxos/async";
3120
- import * as A5 from "@dxos/automerge/automerge";
3121
3150
  import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
3122
- import { Context as Context6, Resource as Resource10 } from "@dxos/context";
3151
+ import { Context as Context6, Resource as Resource11 } from "@dxos/context";
3123
3152
  import { randomUUID } from "@dxos/crypto";
3124
3153
  import { invariant as invariant12 } from "@dxos/invariant";
3125
- import { log as log11 } from "@dxos/log";
3154
+ import { log as log12 } from "@dxos/log";
3126
3155
  import { EdgeService } from "@dxos/protocols";
3127
3156
  import { buf } from "@dxos/protocols/buf";
3128
3157
  import { MessageSchema as RouterMessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
3129
3158
  import { bufferToArray } from "@dxos/util";
3159
+
3160
+ // packages/core/echo/echo-pipeline/src/edge/inflight-request-limiter.ts
3161
+ import { Trigger as Trigger2 } from "@dxos/async";
3162
+ import { Resource as Resource10 } from "@dxos/context";
3163
+ import { log as log11 } from "@dxos/log";
3164
+ var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/edge/inflight-request-limiter.ts";
3165
+ var InflightRequestLimiter = class extends Resource10 {
3166
+ constructor(_config) {
3167
+ super();
3168
+ this._config = _config;
3169
+ this._inflightRequestBalance = 0;
3170
+ this._requestBarrier = new Trigger2();
3171
+ }
3172
+ async _open() {
3173
+ this._inflightRequestBalance = 0;
3174
+ this._requestBarrier.reset();
3175
+ this._requestBarrier.wake();
3176
+ }
3177
+ async _close() {
3178
+ this._inflightRequestBalance = 0;
3179
+ this._requestBarrier.throw(new Error("Rate limiter closed."));
3180
+ clearTimeout(this._resetBalanceTimeout);
3181
+ }
3182
+ async rateLimit(message) {
3183
+ if (message.type !== "sync") {
3184
+ return;
3185
+ }
3186
+ while (this._inflightRequestBalance >= this._config.maxInflightRequests) {
3187
+ await this._requestBarrier.wait();
3188
+ }
3189
+ this._inflightRequestBalance++;
3190
+ if (this._inflightRequestBalance === this._config.maxInflightRequests) {
3191
+ this._requestBarrier.reset();
3192
+ this._resetBalanceTimeout = setTimeout(() => {
3193
+ log11.warn("Request balance has not changed during specified timeout, resetting request limiter.", void 0, {
3194
+ F: __dxlog_file16,
3195
+ L: 52,
3196
+ S: this,
3197
+ C: (f, a) => f(...a)
3198
+ });
3199
+ this._inflightRequestBalance = 0;
3200
+ this._requestBarrier.wake();
3201
+ }, this._config.resetBalanceTimeoutMs);
3202
+ }
3203
+ }
3204
+ handleResponse(message) {
3205
+ if (message.type !== "sync") {
3206
+ return;
3207
+ }
3208
+ this._inflightRequestBalance--;
3209
+ if (this._inflightRequestBalance + 1 === this._config.maxInflightRequests) {
3210
+ this._requestBarrier.wake();
3211
+ clearInterval(this._resetBalanceTimeout);
3212
+ }
3213
+ }
3214
+ };
3215
+
3216
+ // packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts
3130
3217
  function _using_ctx() {
3131
3218
  var _disposeSuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed) {
3132
3219
  var err = new Error();
@@ -3189,7 +3276,7 @@ function _using_ctx() {
3189
3276
  }
3190
3277
  };
3191
3278
  }
3192
- var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts";
3279
+ var __dxlog_file17 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts";
3193
3280
  var INITIAL_RESTART_DELAY = 500;
3194
3281
  var RESTART_DELAY_JITTER = 250;
3195
3282
  var MAX_RESTART_DELAY = 5e3;
@@ -3205,18 +3292,18 @@ var EchoEdgeReplicator = class {
3205
3292
  this._sharePolicyEnabled = !disableSharePolicy;
3206
3293
  }
3207
3294
  async connect(context) {
3208
- log11.info("connect", {
3295
+ log12.info("connect", {
3209
3296
  peerId: context.peerId,
3210
3297
  connectedSpaces: this._connectedSpaces.size
3211
3298
  }, {
3212
- F: __dxlog_file16,
3299
+ F: __dxlog_file17,
3213
3300
  L: 60,
3214
3301
  S: this,
3215
3302
  C: (f, a) => f(...a)
3216
3303
  });
3217
3304
  this._context = context;
3218
3305
  this._ctx = Context6.default(void 0, {
3219
- F: __dxlog_file16,
3306
+ F: __dxlog_file17,
3220
3307
  L: 63
3221
3308
  });
3222
3309
  this._ctx.onDispose(this._edgeConnection.onReconnected(() => {
@@ -3295,7 +3382,7 @@ var EchoEdgeReplicator = class {
3295
3382
  }
3296
3383
  async _openConnection(spaceId, reconnects = 0) {
3297
3384
  invariant12(this._context, void 0, {
3298
- F: __dxlog_file16,
3385
+ F: __dxlog_file17,
3299
3386
  L: 124,
3300
3387
  S: this,
3301
3388
  A: [
@@ -3304,7 +3391,7 @@ var EchoEdgeReplicator = class {
3304
3391
  ]
3305
3392
  });
3306
3393
  invariant12(!this._connections.has(spaceId), void 0, {
3307
- F: __dxlog_file16,
3394
+ F: __dxlog_file17,
3308
3395
  L: 125,
3309
3396
  S: this,
3310
3397
  A: [
@@ -3329,12 +3416,12 @@ var EchoEdgeReplicator = class {
3329
3416
  return;
3330
3417
  }
3331
3418
  const restartDelay = Math.min(MAX_RESTART_DELAY, INITIAL_RESTART_DELAY * reconnects) + Math.random() * RESTART_DELAY_JITTER;
3332
- log11.info("connection restart scheduled", {
3419
+ log12.info("connection restart scheduled", {
3333
3420
  spaceId,
3334
3421
  reconnects,
3335
3422
  restartDelay
3336
3423
  }, {
3337
- F: __dxlog_file16,
3424
+ F: __dxlog_file17,
3338
3425
  L: 148,
3339
3426
  S: this,
3340
3427
  C: (f, a) => f(...a)
@@ -3366,10 +3453,16 @@ var EchoEdgeReplicator = class {
3366
3453
  await connection.open();
3367
3454
  }
3368
3455
  };
3369
- var EdgeReplicatorConnection = class extends Resource10 {
3456
+ var MAX_INFLIGHT_REQUESTS = 5;
3457
+ var MAX_RATE_LIMIT_WAIT_TIME_MS = 3e3;
3458
+ var EdgeReplicatorConnection = class extends Resource11 {
3370
3459
  constructor({ edgeConnection, spaceId, context, sharedPolicyEnabled, onRemoteConnected, onRemoteDisconnected, onRestartRequested }) {
3371
3460
  super();
3372
3461
  this._remotePeerId = null;
3462
+ this._requestLimiter = new InflightRequestLimiter({
3463
+ maxInflightRequests: MAX_INFLIGHT_REQUESTS,
3464
+ resetBalanceTimeoutMs: MAX_RATE_LIMIT_WAIT_TIME_MS
3465
+ });
3373
3466
  this._edgeConnection = edgeConnection;
3374
3467
  this._spaceId = spaceId;
3375
3468
  this._context = context;
@@ -3386,36 +3479,39 @@ var EdgeReplicatorConnection = class extends Resource10 {
3386
3479
  });
3387
3480
  this.writable = new WritableStream({
3388
3481
  write: async (message, controller) => {
3482
+ await this._requestLimiter.rateLimit(message);
3389
3483
  await this._sendMessage(message);
3390
3484
  }
3391
3485
  });
3392
3486
  }
3393
3487
  async _open(ctx) {
3394
- log11("open", void 0, {
3395
- F: __dxlog_file16,
3396
- L: 242,
3488
+ log12("open", void 0, {
3489
+ F: __dxlog_file17,
3490
+ L: 251,
3397
3491
  S: this,
3398
3492
  C: (f, a) => f(...a)
3399
3493
  });
3494
+ await this._requestLimiter.open();
3400
3495
  this._ctx.onDispose(this._edgeConnection.onMessage((msg) => {
3401
3496
  this._onMessage(msg);
3402
3497
  }));
3403
3498
  await this._onRemoteConnected();
3404
3499
  }
3405
3500
  async _close() {
3406
- log11("close", void 0, {
3407
- F: __dxlog_file16,
3408
- L: 254,
3501
+ log12("close", void 0, {
3502
+ F: __dxlog_file17,
3503
+ L: 266,
3409
3504
  S: this,
3410
3505
  C: (f, a) => f(...a)
3411
3506
  });
3412
3507
  this._readableStreamController.close();
3508
+ await this._requestLimiter.close();
3413
3509
  await this._onRemoteDisconnected();
3414
3510
  }
3415
3511
  get peerId() {
3416
3512
  invariant12(this._remotePeerId, "Not connected", {
3417
- F: __dxlog_file16,
3418
- L: 260,
3513
+ F: __dxlog_file17,
3514
+ L: 275,
3419
3515
  S: this,
3420
3516
  A: [
3421
3517
  "this._remotePeerId",
@@ -3434,12 +3530,13 @@ var EdgeReplicatorConnection = class extends Resource10 {
3434
3530
  documentId: params.documentId,
3435
3531
  peerId: this._remotePeerId
3436
3532
  });
3437
- log11.info("document not found locally for share policy check, accepting the remote document", {
3533
+ log12.verbose("edge-replicator document not found locally for share policy check", {
3438
3534
  documentId: params.documentId,
3439
- remoteDocumentExists
3535
+ acceptDocument: remoteDocumentExists,
3536
+ remoteId: this._remotePeerId
3440
3537
  }, {
3441
- F: __dxlog_file16,
3442
- L: 275,
3538
+ F: __dxlog_file17,
3539
+ L: 290,
3443
3540
  S: this,
3444
3541
  C: (f, a) => f(...a)
3445
3542
  });
@@ -3452,23 +3549,20 @@ var EdgeReplicatorConnection = class extends Resource10 {
3452
3549
  return true;
3453
3550
  }
3454
3551
  const spaceId = getSpaceIdFromCollectionId(params.collectionId);
3455
- return spaceId === this._spaceId;
3552
+ return spaceId === this._spaceId && params.collectionId.split(":").length === 3;
3456
3553
  }
3457
3554
  _onMessage(message) {
3458
3555
  if (message.serviceId !== this._targetServiceId) {
3459
3556
  return;
3460
3557
  }
3461
3558
  const payload = cbor2.decode(message.payload.value);
3462
- log11("recv", () => {
3463
- const decodedData = payload.type === "sync" && payload.data ? A5.decodeSyncMessage(payload.data) : payload.type === "collection-state" ? payload.state : payload;
3464
- return {
3465
- from: message.serviceId,
3466
- type: payload.type,
3467
- decodedData
3468
- };
3559
+ log12.verbose("edge replicator receive", {
3560
+ type: payload.type,
3561
+ documentId: payload.type === "sync" && payload.documentId,
3562
+ remoteId: this._remotePeerId
3469
3563
  }, {
3470
- F: __dxlog_file16,
3471
- L: 302,
3564
+ F: __dxlog_file17,
3565
+ L: 319,
3472
3566
  S: this,
3473
3567
  C: (f, a) => f(...a)
3474
3568
  });
@@ -3480,18 +3574,18 @@ var EdgeReplicatorConnection = class extends Resource10 {
3480
3574
  this._onRestartRequested();
3481
3575
  return;
3482
3576
  }
3577
+ this._requestLimiter.handleResponse(message);
3483
3578
  this._readableStreamController.enqueue(message);
3484
3579
  }
3485
3580
  async _sendMessage(message) {
3486
3581
  message.targetId = this._targetServiceId;
3487
- log11("send", {
3582
+ log12.verbose("edge replicator send", {
3488
3583
  type: message.type,
3489
- senderId: message.senderId,
3490
- targetId: message.targetId,
3491
- documentId: message.documentId
3584
+ documentId: message.type === "sync" && message.documentId,
3585
+ remoteId: this._remotePeerId
3492
3586
  }, {
3493
- F: __dxlog_file16,
3494
- L: 332,
3587
+ F: __dxlog_file17,
3588
+ L: 348,
3495
3589
  S: this,
3496
3590
  C: (f, a) => f(...a)
3497
3591
  });