@peerbit/document 12.3.4 → 12.3.5-000e3f1

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.
@@ -753,7 +753,24 @@ let DocumentIndex = (() => {
753
753
  this.documentEvents.removeEventListener("change", this.handleDocumentChange);
754
754
  }
755
755
  this.clearAllResultQueues();
756
- await this.index?.stop?.();
756
+ await this._resumableIterators.clearAll();
757
+ if (this.iteratorKeepAliveTimers) {
758
+ for (const timer of this.iteratorKeepAliveTimers.values()) {
759
+ clearTimeout(timer);
760
+ }
761
+ this.iteratorKeepAliveTimers.clear();
762
+ }
763
+ try {
764
+ await this.index?.stop?.();
765
+ }
766
+ catch (error) {
767
+ // Be defensive during teardown: stopping an already-stopped index shouldn't
768
+ // prevent closing the program and releasing timers/iterators.
769
+ if (error instanceof indexerTypes.NotStartedError) {
770
+ return closed;
771
+ }
772
+ throw error;
773
+ }
757
774
  }
758
775
  return closed;
759
776
  }
@@ -762,13 +779,35 @@ let DocumentIndex = (() => {
762
779
  if (dropped) {
763
780
  this.documentEvents.removeEventListener("change", this.handleDocumentChange);
764
781
  this.clearAllResultQueues();
765
- await this.index?.drop?.();
766
- await this.index?.stop?.();
782
+ await this._resumableIterators.clearAll();
783
+ if (this.iteratorKeepAliveTimers) {
784
+ for (const timer of this.iteratorKeepAliveTimers.values()) {
785
+ clearTimeout(timer);
786
+ }
787
+ this.iteratorKeepAliveTimers.clear();
788
+ }
789
+ try {
790
+ await this.index?.drop?.();
791
+ }
792
+ catch (error) {
793
+ if (!(error instanceof indexerTypes.NotStartedError)) {
794
+ throw error;
795
+ }
796
+ }
797
+ try {
798
+ await this.index?.stop?.();
799
+ }
800
+ catch (error) {
801
+ if (!(error instanceof indexerTypes.NotStartedError)) {
802
+ throw error;
803
+ }
804
+ }
767
805
  }
768
806
  return dropped;
769
807
  }
770
808
  async get(key, options) {
771
809
  let deferred;
810
+ let baseRemote;
772
811
  // Normalize the id key early so listeners can use it
773
812
  let idKey = key instanceof indexerTypes.IdKey ? key : indexerTypes.toId(key);
774
813
  if (options?.waitFor) {
@@ -801,11 +840,12 @@ let DocumentIndex = (() => {
801
840
  this.documentEvents.addEventListener("change", listener);
802
841
  deferred.promise.then(cleanup);
803
842
  // Prepare remote options without mutating caller options
804
- const baseRemote = options?.remote === false
805
- ? undefined
806
- : typeof options?.remote === "object"
807
- ? { ...options.remote }
808
- : {};
843
+ baseRemote =
844
+ options?.remote === false
845
+ ? undefined
846
+ : typeof options?.remote === "object"
847
+ ? { ...options.remote }
848
+ : {};
809
849
  if (baseRemote) {
810
850
  const waitPolicy = baseRemote.wait;
811
851
  if (!waitPolicy ||
@@ -842,7 +882,10 @@ let DocumentIndex = (() => {
842
882
  });
843
883
  }
844
884
  }
845
- const result = (await this.getDetailed(idKey, options))?.[0]?.results[0];
885
+ const initialOptions = baseRemote
886
+ ? { ...options, remote: baseRemote }
887
+ : options;
888
+ const result = (await this.getDetailed(idKey, initialOptions))?.[0]?.results[0];
846
889
  // if no results, and we have remote joining options, we wait for the timout and if there are joining peers we re-query
847
890
  if (!result) {
848
891
  return deferred?.promise;
@@ -1142,6 +1185,8 @@ let DocumentIndex = (() => {
1142
1185
  fromQuery: (fromQuery || query),
1143
1186
  resolveResults: resolveFlag,
1144
1187
  };
1188
+ // Don't keep Node alive just to GC old remote iterator state.
1189
+ prevQueued.timeout.unref?.();
1145
1190
  if (fromQuery instanceof types.IterationRequest &&
1146
1191
  fromQuery.pushUpdates) {
1147
1192
  prevQueued.pushMode = fromQuery.pushUpdates;
@@ -1228,6 +1273,8 @@ let DocumentIndex = (() => {
1228
1273
  }
1229
1274
  this._resumableIterators.close({ idString });
1230
1275
  }, delay);
1276
+ // This is a best-effort cleanup timer; it should not keep Node alive.
1277
+ timer.unref?.();
1231
1278
  timers.set(idString, timer);
1232
1279
  }
1233
1280
  cancelIteratorKeepAlive(idString) {
@@ -1409,12 +1456,7 @@ let DocumentIndex = (() => {
1409
1456
  const local = typeof options?.local === "boolean" ? options?.local : true;
1410
1457
  let remote = undefined;
1411
1458
  if (typeof options?.remote === "boolean") {
1412
- if (options?.remote) {
1413
- remote = {};
1414
- }
1415
- else {
1416
- remote = undefined;
1417
- }
1459
+ remote = options.remote ? {} : undefined;
1418
1460
  }
1419
1461
  else {
1420
1462
  remote = options?.remote || {};
@@ -1425,6 +1467,13 @@ let DocumentIndex = (() => {
1425
1467
  // this will lead to bad UX as you usually want to list/expore whats going on before doing any replication work
1426
1468
  remote.priority = 2;
1427
1469
  }
1470
+ if (remote && remote.timeout == null && options?.remote) {
1471
+ const waitPolicy = typeof options.remote === "object" ? options.remote.wait : undefined;
1472
+ const waitTimeout = typeof waitPolicy === "object" ? waitPolicy.timeout : undefined;
1473
+ if (waitTimeout != null) {
1474
+ remote.timeout = waitTimeout;
1475
+ }
1476
+ }
1428
1477
  if (!local && !remote) {
1429
1478
  throw new Error("Expecting either 'options.remote' or 'options.local' to be true");
1430
1479
  }
@@ -1443,14 +1492,69 @@ let DocumentIndex = (() => {
1443
1492
  // don't wait for responses
1444
1493
  throw new Error("Unexpected");
1445
1494
  }
1446
- const replicatorGroups = options?.remote?.from
1495
+ const coverProps = remote.domain ?? { args: undefined };
1496
+ const isDefaultDomainArgs = !("range" in coverProps) &&
1497
+ (!("args" in coverProps) || coverProps.args == null);
1498
+ let replicatorGroups = options?.remote?.from
1447
1499
  ? options?.remote?.from
1448
- : await this._log.getCover(remote.domain ?? { args: undefined }, {
1500
+ : await this._log.getCover(coverProps, {
1449
1501
  roleAge: remote.minAge,
1450
1502
  eager: remote.reach?.eager,
1451
1503
  reachableOnly: !!remote.wait, // when we want to merge joining we can ignore pending to be online peers and instead consider them once they become online
1452
1504
  signal: options?.signal,
1453
1505
  });
1506
+ // Cold start: cover can be temporarily empty/self-only while replication metadata
1507
+ // converges. For remote search, it's sometimes better to at least try currently
1508
+ // connected peers, but only if we have evidence that a remote replicator exists.
1509
+ if (!options?.remote?.from && isDefaultDomainArgs) {
1510
+ const selfHash = this.node.identity.publicKey.hashcode();
1511
+ const remoteCount = replicatorGroups.filter((h) => h !== selfHash).length;
1512
+ if (remoteCount === 0) {
1513
+ const waitEnabled = Boolean(remote.wait);
1514
+ const coverIsSelfOnly = replicatorGroups.length === 1 && replicatorGroups[0] === selfHash;
1515
+ // If the cover is explicitly empty (no shards), don't override it unless
1516
+ // the caller requested waiting for joins (e.g. get(waitFor)).
1517
+ if (!waitEnabled && !coverIsSelfOnly) {
1518
+ // no-op
1519
+ }
1520
+ else {
1521
+ let hasKnownRemoteReplicator = false;
1522
+ if (!waitEnabled) {
1523
+ try {
1524
+ const replicators = await this._log.getReplicators();
1525
+ for (const hash of replicators.keys()) {
1526
+ if (hash !== selfHash) {
1527
+ hasKnownRemoteReplicator = true;
1528
+ break;
1529
+ }
1530
+ }
1531
+ }
1532
+ catch {
1533
+ // Best-effort only.
1534
+ }
1535
+ }
1536
+ if (waitEnabled || hasKnownRemoteReplicator) {
1537
+ const peerMap = this.node.services
1538
+ .pubsub?.peers;
1539
+ if (peerMap?.keys) {
1540
+ const extra = [];
1541
+ for (const hash of peerMap.keys()) {
1542
+ if (!hash || hash === selfHash)
1543
+ continue;
1544
+ extra.push(hash);
1545
+ if (extra.length >= 8)
1546
+ break;
1547
+ }
1548
+ if (extra.length > 0) {
1549
+ replicatorGroups = [
1550
+ ...new Set([...replicatorGroups, ...extra]),
1551
+ ];
1552
+ }
1553
+ }
1554
+ }
1555
+ }
1556
+ }
1557
+ }
1454
1558
  if (replicatorGroups) {
1455
1559
  const responseHandler = async (results) => {
1456
1560
  const resultInitialized = await introduceEntries(queryRequest, results, this.documentType, this.indexedType, this._sync, options);
@@ -1529,6 +1633,9 @@ let DocumentIndex = (() => {
1529
1633
  catch (error) {
1530
1634
  if (error instanceof MissingResponsesError) {
1531
1635
  warn("Did not reciveve responses from all shard");
1636
+ if (options?.onMissingResponses) {
1637
+ await options.onMissingResponses(error);
1638
+ }
1532
1639
  if (remote?.throwOnMissing) {
1533
1640
  throw error;
1534
1641
  }
@@ -1575,9 +1682,8 @@ let DocumentIndex = (() => {
1575
1682
  // Set fetch to search size, or max value (default to max u32 (4294967295))
1576
1683
  const coercedRequest = coerceQuery(queryRequest, options, this.compatibility);
1577
1684
  coercedRequest.fetch = coercedRequest.fetch ?? 0xffffffff;
1578
- // So that the iterator is pre-fetching the right amount of entries
1685
+ // Use an iterator so large results respect message size limits.
1579
1686
  const iterator = this.iterate(coercedRequest, options);
1580
- // So that this call will not do any remote requests
1581
1687
  const allResults = [];
1582
1688
  while (iterator.done() !== true &&
1583
1689
  coercedRequest.fetch > allResults.length) {
@@ -1838,36 +1944,42 @@ let DocumentIndex = (() => {
1838
1944
  let discoveredTargetHashes;
1839
1945
  if (typeof options?.remote === "object") {
1840
1946
  let waitForTime = undefined;
1947
+ const waitPolicy = typeof options.remote.wait === "object"
1948
+ ? options.remote.wait
1949
+ : undefined;
1950
+ const waitBehavior = waitPolicy?.behavior ?? "keep-open";
1841
1951
  if (options.remote.wait) {
1842
- let t0 = +new Date();
1843
1952
  waitForTime =
1844
1953
  typeof options.remote.wait === "boolean"
1845
1954
  ? DEFAULT_TIMEOUT
1846
1955
  : (options.remote.wait.timeout ?? DEFAULT_TIMEOUT);
1847
- let setDoneIfTimeout = false;
1848
- maybeSetDone = () => {
1849
- if (t0 + waitForTime < +new Date()) {
1850
- cleanup();
1851
- done = true;
1852
- }
1853
- else {
1854
- setDoneIfTimeout = true;
1855
- }
1856
- };
1857
- unsetDone = () => {
1858
- setDoneIfTimeout = false;
1859
- done = false;
1860
- };
1861
- let timeout = setTimeout(() => {
1862
- if (setDoneIfTimeout) {
1863
- cleanup();
1864
- done = true;
1865
- }
1866
- }, waitForTime);
1867
- cleanup = () => {
1868
- this.clearResultsQueue(queryRequestCoerced);
1869
- clearTimeout(timeout);
1870
- };
1956
+ if (waitBehavior === "keep-open") {
1957
+ let t0 = +new Date();
1958
+ let setDoneIfTimeout = false;
1959
+ maybeSetDone = () => {
1960
+ if (t0 + waitForTime < +new Date()) {
1961
+ cleanup();
1962
+ done = true;
1963
+ }
1964
+ else {
1965
+ setDoneIfTimeout = true;
1966
+ }
1967
+ };
1968
+ unsetDone = () => {
1969
+ setDoneIfTimeout = false;
1970
+ done = false;
1971
+ };
1972
+ let timeout = setTimeout(() => {
1973
+ if (setDoneIfTimeout) {
1974
+ cleanup();
1975
+ done = true;
1976
+ }
1977
+ }, waitForTime);
1978
+ cleanup = () => {
1979
+ this.clearResultsQueue(queryRequestCoerced);
1980
+ clearTimeout(timeout);
1981
+ };
1982
+ }
1871
1983
  }
1872
1984
  if (options.remote.reach?.discover) {
1873
1985
  const discoverTimeout = waitForTime ??
@@ -1891,9 +2003,6 @@ let DocumentIndex = (() => {
1891
2003
  warmupPromise = prior.then(() => discoverPromise);
1892
2004
  options.remote.reach.eager = true; // include the results from the discovered peer even if it is not mature
1893
2005
  }
1894
- const waitPolicy = typeof options.remote.wait === "object"
1895
- ? options.remote.wait
1896
- : undefined;
1897
2006
  if (waitPolicy?.behavior === "block" &&
1898
2007
  (waitPolicy.until ?? "any") === "any") {
1899
2008
  const blockPromise = this.waitForCoverReady({
@@ -1912,6 +2021,7 @@ let DocumentIndex = (() => {
1912
2021
  const fetchFirst = async (n, fetchOptions) => {
1913
2022
  await warmupPromise;
1914
2023
  let hasMore = false;
2024
+ let missingResponses = false;
1915
2025
  const discoverTargets = typeof options?.remote === "object"
1916
2026
  ? options.remote.reach?.discover
1917
2027
  : undefined;
@@ -2012,7 +2122,31 @@ let DocumentIndex = (() => {
2012
2122
  throw new Error("Unsupported result type: " + response?.constructor?.name);
2013
2123
  }
2014
2124
  },
2125
+ onMissingResponses: (error) => {
2126
+ missingResponses = true;
2127
+ const missingGroups = error.missingGroups;
2128
+ if (!missingGroups?.length) {
2129
+ return;
2130
+ }
2131
+ const selfHash = this.node.identity.publicKey.hashcode();
2132
+ for (const group of missingGroups) {
2133
+ const target = group.find((hash) => {
2134
+ if (!hash || hash === selfHash)
2135
+ return false;
2136
+ const attempts = missingResponseRetryAttempts.get(hash) ?? 0;
2137
+ return attempts < maxMissingResponseRetryAttempts;
2138
+ });
2139
+ if (!target)
2140
+ continue;
2141
+ pendingMissingResponseRetryPeers.add(target);
2142
+ missingResponseRetryAttempts.set(target, (missingResponseRetryAttempts.get(target) ?? 0) + 1);
2143
+ }
2144
+ },
2015
2145
  }, fetchOptions?.fetchedFirstForRemote);
2146
+ if (missingResponses) {
2147
+ hasMore = true;
2148
+ unsetDone();
2149
+ }
2016
2150
  if (!hasMore) {
2017
2151
  maybeSetDone();
2018
2152
  }
@@ -2032,6 +2166,16 @@ let DocumentIndex = (() => {
2032
2166
  fetchPromise = fetchFirst(n);
2033
2167
  return fetchPromise;
2034
2168
  }
2169
+ if (pendingMissingResponseRetryPeers.size > 0) {
2170
+ const retryTargets = [...pendingMissingResponseRetryPeers];
2171
+ pendingMissingResponseRetryPeers.clear();
2172
+ fetchPromise = fetchFirst(n, {
2173
+ from: retryTargets,
2174
+ // retries for missing groups should not be suppressed by first-fetch dedupe
2175
+ fetchedFirstForRemote: undefined,
2176
+ });
2177
+ return fetchPromise;
2178
+ }
2035
2179
  const promises = [];
2036
2180
  let resultsLeft = 0;
2037
2181
  for (const [peer, buffer] of peerBufferMap) {
@@ -2300,25 +2444,19 @@ let DocumentIndex = (() => {
2300
2444
  };
2301
2445
  let close = async () => {
2302
2446
  cleanupAndDone();
2303
- // send close to remote
2447
+ // send close to remote (only peers that actually served results / had an active buffer)
2304
2448
  const closeRequest = new types.CloseIteratorRequest({
2305
2449
  id: queryRequestCoerced.id,
2306
2450
  });
2307
- const promises = [];
2308
- for (const [peer, buffer] of peerBufferMap) {
2309
- if (buffer.kept === 0) {
2310
- peerBufferMap.delete(peer);
2311
- continue;
2312
- }
2313
- if (peer !== this.node.identity.publicKey.hashcode()) {
2314
- // Close remote
2315
- promises.push(this._query.send(closeRequest, {
2316
- ...options,
2317
- mode: new SilentDelivery({ to: [peer], redundancy: 1 }),
2318
- }));
2319
- }
2320
- }
2321
- await Promise.all(promises);
2451
+ const selfHash = this.node.identity.publicKey.hashcode();
2452
+ const remotePeers = [...peerBufferMap.entries()]
2453
+ .filter(([peer, buffer]) => peer !== selfHash && buffer.kept > 0)
2454
+ .map(([peer]) => peer);
2455
+ peerBufferMap.clear();
2456
+ await Promise.allSettled(remotePeers.map((peer) => this._query.send(closeRequest, {
2457
+ ...options,
2458
+ mode: new SilentDelivery({ to: [peer], redundancy: 1 }),
2459
+ })));
2322
2460
  };
2323
2461
  options?.signal && options.signal.addEventListener("abort", close);
2324
2462
  let doneFn = () => {
@@ -2326,6 +2464,9 @@ let DocumentIndex = (() => {
2326
2464
  };
2327
2465
  let joinListener;
2328
2466
  let fetchedFirstForRemote = undefined;
2467
+ const pendingMissingResponseRetryPeers = new Set();
2468
+ const missingResponseRetryAttempts = new Map();
2469
+ const maxMissingResponseRetryAttempts = 2;
2329
2470
  let updateDeferred;
2330
2471
  const onLateResultsQueue = options?.outOfOrder?.mode === "queue" &&
2331
2472
  typeof options?.outOfOrder?.handle === "function"
@@ -2746,10 +2887,17 @@ let DocumentIndex = (() => {
2746
2887
  return cleanupDefaultUpdates();
2747
2888
  };
2748
2889
  }
2749
- if (typeof options?.remote === "object" && options?.remote.wait) {
2890
+ const remoteConfig = options && typeof options.remote === "object" ? options.remote : undefined;
2891
+ const remoteWaitPolicy = remoteConfig && typeof remoteConfig.wait === "object"
2892
+ ? remoteConfig.wait
2893
+ : undefined;
2894
+ const remoteWaitBehavior = remoteWaitPolicy?.behavior ?? "keep-open";
2895
+ const keepRemoteWaitOpen = !!remoteConfig?.wait &&
2896
+ remoteWaitBehavior === "keep-open";
2897
+ if (keepRemoteWaitOpen) {
2750
2898
  // was used to account for missed results when a peer joins; omitted in this minimal handler
2751
2899
  updateDeferred = pDefer();
2752
- const waitForTime = typeof options.remote.wait === "object" && options.remote.wait.timeout;
2900
+ const waitForTime = remoteWaitPolicy?.timeout;
2753
2901
  const prevMaybeSetDone = maybeSetDone;
2754
2902
  maybeSetDone = () => {
2755
2903
  prevMaybeSetDone();
@@ -2764,7 +2912,7 @@ let DocumentIndex = (() => {
2764
2912
  fetchedFirstForRemote = new Set();
2765
2913
  joinListener = this.createReplicatorJoinListener({
2766
2914
  signal: ensureController().signal,
2767
- eager: options.remote.reach?.eager,
2915
+ eager: remoteConfig?.reach?.eager,
2768
2916
  onPeer: async (pk) => {
2769
2917
  if (done)
2770
2918
  return;
@@ -2823,7 +2971,7 @@ let DocumentIndex = (() => {
2823
2971
  }
2824
2972
  };
2825
2973
  }
2826
- const remoteWaitActive = typeof options?.remote === "object" && !!options.remote.wait;
2974
+ const remoteWaitActive = keepRemoteWaitOpen;
2827
2975
  const waitForUpdateAndResetDeferred = async () => {
2828
2976
  if (remoteWaitActive) {
2829
2977
  // wait until: join fetch adds results, cleanup runs, or the join-wait times out