@peerbit/pubsub 5.4.2 → 5.4.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerbit/pubsub",
3
- "version": "5.4.2",
3
+ "version": "5.4.3",
4
4
  "description": "Direct streaming for libp2p",
5
5
  "type": "module",
6
6
  "types": "./dist/src/index.d.ts",
@@ -85,11 +85,11 @@
85
85
  "p-defer": "^4.0.0",
86
86
  "uint8arraylist": "^3.0.2",
87
87
  "uint8arrays": "^5.1.0",
88
- "@peerbit/any-store-interface": "1.1.2",
88
+ "@peerbit/any-store-interface": "1.1.3",
89
89
  "@peerbit/crypto": "3.1.6",
90
- "@peerbit/stream": "5.2.1",
91
90
  "@peerbit/logger": "2.0.2",
92
91
  "@peerbit/pubsub-interface": "5.2.2",
92
+ "@peerbit/stream": "5.2.1",
93
93
  "@peerbit/time": "3.0.1",
94
94
  "@peerbit/stream-interface": "6.0.16"
95
95
  },
@@ -654,6 +654,8 @@ export type FanoutProviderHandle = {
654
654
  close: () => void;
655
655
  };
656
656
 
657
+ const PROVIDER_BATCH_ANNOUNCE_CONCURRENCY = 8;
658
+
657
659
  export type FanoutTreeDataEvent = {
658
660
  topic: string;
659
661
  root: string;
@@ -1740,12 +1742,24 @@ export class FanoutTree extends DirectStream<FanoutTreeEvents> {
1740
1742
  public async announceProvider(
1741
1743
  namespace: string,
1742
1744
  options: Omit<FanoutProviderAnnounceOptions, "announceIntervalMs"> = {},
1745
+ ): Promise<void> {
1746
+ await this.announceProviders([namespace], options);
1747
+ }
1748
+
1749
+ /**
1750
+ * Announce multiple provider namespaces using the existing single-namespace
1751
+ * wire frame. Bootstrap discovery is shared across the batch and sends are
1752
+ * bounded, so released trackers and readers remain compatible without one
1753
+ * bootstrap lookup or unbounded promise per namespace.
1754
+ */
1755
+ public async announceProviders(
1756
+ namespaces: Iterable<string>,
1757
+ options: Omit<FanoutProviderAnnounceOptions, "announceIntervalMs"> = {},
1743
1758
  ): Promise<void> {
1744
1759
  if (!this.started) {
1745
1760
  throw new Error("FanoutTree must be started before providing");
1746
1761
  }
1747
1762
 
1748
- const id = this.getProviderNamespaceId(namespace);
1749
1763
  const ttlMsRaw = Math.max(0, Math.floor(options.ttlMs ?? 120_000));
1750
1764
  const ttlMs = Math.min(ttlMsRaw, 120_000);
1751
1765
 
@@ -1764,20 +1778,58 @@ export class FanoutTree extends DirectStream<FanoutTreeEvents> {
1764
1778
  0,
1765
1779
  Math.floor(options.bootstrapMaxPeers ?? 0),
1766
1780
  );
1767
-
1768
- const state: ProviderAnnounceState = {
1769
- id,
1770
- ttlMs,
1771
- // ignored by `announceProviderOnce`
1772
- announceIntervalMs: 0,
1773
- bootstrapOverride,
1781
+ const bootstraps = bootstrapOverride ?? this.bootstraps;
1782
+ if (bootstraps.length === 0) return;
1783
+ const signal = this.closeController.signal;
1784
+ const peers = await this.ensureBootstrapPeers(
1785
+ bootstraps,
1774
1786
  bootstrapDialTimeoutMs,
1787
+ signal,
1775
1788
  bootstrapMaxPeers,
1776
- closeController: new AbortController(),
1777
- closed: false,
1778
- };
1789
+ );
1790
+ if (peers.length === 0 || signal.aborted) return;
1791
+
1792
+ const iterator = namespaces[Symbol.iterator]();
1793
+ const firstNamespaces: string[] = [];
1794
+ for (
1795
+ let index = 0;
1796
+ index < PROVIDER_BATCH_ANNOUNCE_CONCURRENCY;
1797
+ index++
1798
+ ) {
1799
+ const next = iterator.next();
1800
+ if (next.done) break;
1801
+ firstNamespaces.push(next.value);
1802
+ }
1803
+ if (firstNamespaces.length === 0) return;
1779
1804
 
1780
- await this.announceProviderOnce(state, this.closeController.signal, ttlMs);
1805
+ const addrs = this.getSelfAnnounceAddrs();
1806
+ let firstError: unknown;
1807
+ let failureCount = 0;
1808
+ const announce = async (firstNamespace: string) => {
1809
+ let namespace = firstNamespace;
1810
+ for (;;) {
1811
+ if (signal.aborted) return;
1812
+ try {
1813
+ const id = this.getProviderNamespaceId(namespace);
1814
+ const bytes = this.codec.encodeProviderAnnounce(id.key, ttlMs, addrs);
1815
+ await this._sendControlMany(peers, bytes);
1816
+ } catch (error) {
1817
+ failureCount++;
1818
+ firstError ??= error;
1819
+ }
1820
+ const next = iterator.next();
1821
+ if (next.done) return;
1822
+ namespace = next.value;
1823
+ }
1824
+ };
1825
+ await Promise.all(firstNamespaces.map((namespace) => announce(namespace)));
1826
+ if (failureCount === 1) throw firstError;
1827
+ if (failureCount > 1) {
1828
+ throw new AggregateError(
1829
+ [firstError],
1830
+ `Failed to announce ${failureCount} provider namespaces`,
1831
+ );
1832
+ }
1781
1833
  }
1782
1834
 
1783
1835
  private async _providerAnnounceLoop(