peerpigeon 1.0.6 → 1.0.8

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.
@@ -56,6 +56,9 @@ export class PeerPigeonMesh extends EventEmitter {
56
56
  // Track peers for which we've already emitted peerKeyAdded events to prevent UI spam
57
57
  this.emittedPeerKeyEvents = new Set();
58
58
 
59
+ // Gossip stream tracking
60
+ this.gossipStreams = new Map(); // streamId -> { chunks: Map, metadata, controller, totalChunks }
61
+
59
62
  // Initialize managers
60
63
  this.storageManager = new StorageManager(this);
61
64
  this.mediaManager = new MediaManager();
@@ -121,6 +124,18 @@ export class PeerPigeonMesh extends EventEmitter {
121
124
  return; // Don't emit as regular message
122
125
  }
123
126
 
127
+ // Handle gossip stream chunks
128
+ if (data.message && data.message.type === 'stream-chunk') {
129
+ this._handleGossipStreamChunk(data.message, data.from);
130
+ return; // Don't emit as regular message
131
+ }
132
+
133
+ // Handle gossip stream control messages
134
+ if (data.message && data.message.type === 'stream-control') {
135
+ this._handleGossipStreamControl(data.message, data.from);
136
+ return; // Don't emit as regular message
137
+ }
138
+
124
139
  // Additional safety filter for message types that should not be emitted to UI
125
140
  // These should already be filtered at the GossipManager level, but this provides defense in depth
126
141
  if (data.content && typeof data.content === 'string') {
@@ -934,6 +949,177 @@ export class PeerPigeonMesh extends EventEmitter {
934
949
  await this.sendStream(targetPeerId, stream, streamOptions);
935
950
  }
936
951
 
952
+ /**
953
+ * Create a writable stream that broadcasts data to all peers in the mesh using gossip
954
+ * @param {object} options - Stream options (filename, mimeType, totalSize, etc.)
955
+ * @returns {WritableStream} A writable stream that broadcasts via gossip protocol
956
+ */
957
+ createBroadcastStream(options = {}) {
958
+ const streamId = options.streamId || this._generateStreamId();
959
+ const metadata = {
960
+ streamId,
961
+ type: options.type || 'broadcast',
962
+ filename: options.filename,
963
+ mimeType: options.mimeType,
964
+ totalSize: options.totalSize,
965
+ timestamp: Date.now(),
966
+ ...options
967
+ };
968
+
969
+ // Store chunks locally for gossip distribution
970
+ const chunks = [];
971
+ let chunkIndex = 0;
972
+ let totalBytesWritten = 0;
973
+ const self = this;
974
+
975
+ this.debug.log(`📡 Creating gossip broadcast stream: ${streamId}`);
976
+
977
+ return new WritableStream({
978
+ async write(chunk) {
979
+ const chunkData = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
980
+ const chunkId = chunkIndex++;
981
+
982
+ // Store chunk
983
+ chunks.push({
984
+ index: chunkId,
985
+ data: chunkData
986
+ });
987
+
988
+ totalBytesWritten += chunkData.length;
989
+
990
+ // Broadcast chunk via gossip protocol
991
+ try {
992
+ await self.gossipManager.broadcastMessage({
993
+ streamId,
994
+ chunkIndex: chunkId,
995
+ data: Array.from(chunkData), // Convert to regular array for JSON
996
+ totalSize: metadata.totalSize,
997
+ metadata: chunkId === 0 ? metadata : undefined // Include metadata with first chunk
998
+ }, 'stream-chunk');
999
+
1000
+ self.debug.log(`📡 Gossiped chunk ${chunkId} (${chunkData.length} bytes) for stream ${streamId.substring(0, 8)}...`);
1001
+ } catch (error) {
1002
+ self.debug.error(`Failed to gossip chunk ${chunkId}:`, error);
1003
+ throw error;
1004
+ }
1005
+ },
1006
+
1007
+ async close() {
1008
+ // Broadcast stream end via gossip
1009
+ try {
1010
+ await self.gossipManager.broadcastMessage({
1011
+ streamId,
1012
+ action: 'end',
1013
+ totalChunks: chunkIndex,
1014
+ totalBytes: totalBytesWritten,
1015
+ metadata
1016
+ }, 'stream-control');
1017
+
1018
+ self.debug.log(`📡 Broadcast stream completed via gossip: ${totalBytesWritten} bytes in ${chunkIndex} chunks`);
1019
+
1020
+ // Emit completion event
1021
+ self.emit('broadcastStreamComplete', {
1022
+ streamId,
1023
+ totalBytes: totalBytesWritten,
1024
+ totalChunks: chunkIndex,
1025
+ metadata
1026
+ });
1027
+ } catch (error) {
1028
+ self.debug.error('Failed to broadcast stream end:', error);
1029
+ throw error;
1030
+ }
1031
+ },
1032
+
1033
+ async abort(reason) {
1034
+ // Broadcast stream abort via gossip
1035
+ try {
1036
+ await self.gossipManager.broadcastMessage({
1037
+ streamId,
1038
+ action: 'abort',
1039
+ reason: reason?.message || String(reason),
1040
+ metadata
1041
+ }, 'stream-control');
1042
+
1043
+ self.debug.log(`❌ Broadcast stream aborted via gossip: ${reason}`);
1044
+
1045
+ // Emit abort event
1046
+ self.emit('broadcastStreamAborted', {
1047
+ streamId,
1048
+ reason: reason?.message || String(reason),
1049
+ metadata
1050
+ });
1051
+ } catch (error) {
1052
+ self.debug.error('Failed to broadcast stream abort:', error);
1053
+ }
1054
+ }
1055
+ });
1056
+ }
1057
+
1058
+ /**
1059
+ * Broadcast a ReadableStream to all connected peers
1060
+ * @param {ReadableStream} readableStream - The stream to broadcast
1061
+ * @param {object} options - Stream options (filename, mimeType, etc.)
1062
+ * @returns {Promise<void>}
1063
+ */
1064
+ async broadcastStream(readableStream, options = {}) {
1065
+ const writableStream = this.createBroadcastStream(options);
1066
+
1067
+ try {
1068
+ await readableStream.pipeTo(writableStream);
1069
+ this.debug.log(`✅ Stream broadcasted successfully to all peers`);
1070
+ } catch (error) {
1071
+ this.debug.error(`❌ Failed to broadcast stream:`, error);
1072
+ throw error;
1073
+ }
1074
+ }
1075
+
1076
+ /**
1077
+ * Broadcast a File to all connected peers using streams
1078
+ * @param {File} file - The file to broadcast
1079
+ * @returns {Promise<void>}
1080
+ */
1081
+ async broadcastFile(file) {
1082
+ this.debug.log(`📁 Broadcasting file "${file.name}" (${file.size} bytes) to all peers`);
1083
+
1084
+ const options = {
1085
+ filename: file.name,
1086
+ mimeType: file.type,
1087
+ totalSize: file.size,
1088
+ type: 'file'
1089
+ };
1090
+
1091
+ const stream = file.stream();
1092
+ await this.broadcastStream(stream, options);
1093
+ }
1094
+
1095
+ /**
1096
+ * Broadcast a Blob to all connected peers using streams
1097
+ * @param {Blob} blob - The blob to broadcast
1098
+ * @param {object} options - Additional options
1099
+ * @returns {Promise<void>}
1100
+ */
1101
+ async broadcastBlob(blob, options = {}) {
1102
+ this.debug.log(`📦 Broadcasting blob (${blob.size} bytes) to all peers`);
1103
+
1104
+ const streamOptions = {
1105
+ ...options,
1106
+ mimeType: blob.type,
1107
+ totalSize: blob.size,
1108
+ type: 'blob'
1109
+ };
1110
+
1111
+ const stream = blob.stream();
1112
+ await this.broadcastStream(stream, streamOptions);
1113
+ }
1114
+
1115
+ /**
1116
+ * Generate a unique stream ID
1117
+ * @private
1118
+ */
1119
+ _generateStreamId() {
1120
+ return `stream_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
1121
+ }
1122
+
937
1123
  // Helper methods for backward compatibility
938
1124
  canAcceptMorePeers() {
939
1125
  return this.connectionManager.canAcceptMorePeers();
@@ -1757,6 +1943,153 @@ export class PeerPigeonMesh extends EventEmitter {
1757
1943
  this.signalingHandler.handleSignalingMessage(reconstitutedMessage);
1758
1944
  }
1759
1945
 
1946
+ /**
1947
+ * Handle incoming gossip stream chunk
1948
+ * @param {Object} message - The stream chunk message
1949
+ * @param {string} from - Sender peer ID
1950
+ * @private
1951
+ */
1952
+ _handleGossipStreamChunk(message, from) {
1953
+ const { streamId, chunkIndex, data, metadata } = message;
1954
+
1955
+ if (!streamId || chunkIndex === undefined || !data) {
1956
+ this.debug.error('Invalid gossip stream chunk format');
1957
+ return;
1958
+ }
1959
+
1960
+ this.debug.log(`📥 Received gossip stream chunk ${chunkIndex} for ${streamId.substring(0, 8)}... from ${from.substring(0, 8)}...`);
1961
+
1962
+ // Initialize stream if this is the first chunk
1963
+ if (!this.gossipStreams.has(streamId)) {
1964
+ const chunks = new Map();
1965
+ let controller;
1966
+
1967
+ const readable = new ReadableStream({
1968
+ start(ctrl) {
1969
+ controller = ctrl;
1970
+ },
1971
+ cancel: (reason) => {
1972
+ this.debug.log(`📥 Gossip stream ${streamId.substring(0, 8)}... cancelled: ${reason}`);
1973
+ }
1974
+ });
1975
+
1976
+ this.gossipStreams.set(streamId, {
1977
+ chunks,
1978
+ metadata: metadata || {},
1979
+ controller,
1980
+ stream: readable,
1981
+ from,
1982
+ totalChunks: null
1983
+ });
1984
+
1985
+ // Emit stream received event
1986
+ this.emit('streamReceived', {
1987
+ peerId: from,
1988
+ streamId,
1989
+ stream: readable,
1990
+ metadata: metadata || {}
1991
+ });
1992
+ }
1993
+
1994
+ // Store chunk
1995
+ const streamData = this.gossipStreams.get(streamId);
1996
+ streamData.chunks.set(chunkIndex, new Uint8Array(data));
1997
+
1998
+ // Enqueue chunk to the readable stream in order
1999
+ this._enqueueOrderedChunks(streamId);
2000
+ }
2001
+
2002
+ /**
2003
+ * Handle incoming gossip stream control message
2004
+ * @param {Object} message - The stream control message
2005
+ * @param {string} from - Sender peer ID
2006
+ * @private
2007
+ */
2008
+ _handleGossipStreamControl(message, from) {
2009
+ const { streamId, action, totalChunks, totalBytes, metadata, reason } = message;
2010
+
2011
+ if (!streamId || !action) {
2012
+ this.debug.error('Invalid gossip stream control format');
2013
+ return;
2014
+ }
2015
+
2016
+ const streamData = this.gossipStreams.get(streamId);
2017
+ if (!streamData) {
2018
+ this.debug.warn(`Received control message for unknown stream: ${streamId.substring(0, 8)}...`);
2019
+ return;
2020
+ }
2021
+
2022
+ if (action === 'end') {
2023
+ this.debug.log(`📥 Gossip stream ${streamId.substring(0, 8)}... ended: ${totalBytes} bytes in ${totalChunks} chunks`);
2024
+
2025
+ streamData.totalChunks = totalChunks;
2026
+
2027
+ // Enqueue any remaining chunks and close
2028
+ this._enqueueOrderedChunks(streamId);
2029
+
2030
+ if (streamData.controller) {
2031
+ streamData.controller.close();
2032
+ }
2033
+
2034
+ // Emit completion event
2035
+ this.emit('streamCompleted', {
2036
+ peerId: from,
2037
+ streamId,
2038
+ totalChunks,
2039
+ totalBytes
2040
+ });
2041
+
2042
+ // Clean up after a delay
2043
+ setTimeout(() => {
2044
+ this.gossipStreams.delete(streamId);
2045
+ }, 5000);
2046
+ } else if (action === 'abort') {
2047
+ this.debug.log(`❌ Gossip stream ${streamId.substring(0, 8)}... aborted: ${reason}`);
2048
+
2049
+ if (streamData.controller) {
2050
+ streamData.controller.error(new Error(reason || 'Stream aborted'));
2051
+ }
2052
+
2053
+ // Emit abort event
2054
+ this.emit('streamAborted', {
2055
+ peerId: from,
2056
+ streamId,
2057
+ reason
2058
+ });
2059
+
2060
+ // Clean up
2061
+ this.gossipStreams.delete(streamId);
2062
+ }
2063
+ }
2064
+
2065
+ /**
2066
+ * Enqueue ordered chunks to the readable stream
2067
+ * @param {string} streamId - The stream ID
2068
+ * @private
2069
+ */
2070
+ _enqueueOrderedChunks(streamId) {
2071
+ const streamData = this.gossipStreams.get(streamId);
2072
+ if (!streamData || !streamData.controller) return;
2073
+
2074
+ const { chunks, controller } = streamData;
2075
+ let nextIndex = streamData.nextIndex || 0;
2076
+
2077
+ // Enqueue all consecutive chunks
2078
+ while (chunks.has(nextIndex)) {
2079
+ const chunk = chunks.get(nextIndex);
2080
+ try {
2081
+ controller.enqueue(chunk);
2082
+ chunks.delete(nextIndex);
2083
+ nextIndex++;
2084
+ } catch (error) {
2085
+ this.debug.error(`Failed to enqueue chunk ${nextIndex}:`, error);
2086
+ break;
2087
+ }
2088
+ }
2089
+
2090
+ streamData.nextIndex = nextIndex;
2091
+ }
2092
+
1760
2093
  /**
1761
2094
  * Send a signaling message, using mesh connections for renegotiation
1762
2095
  * @param {Object} message - The signaling message
@@ -100,13 +100,26 @@ export class SignalingHandler extends EventEmitter {
100
100
  }
101
101
 
102
102
  handlePeerAnnouncement(fromPeerId) {
103
- // Prevent duplicate announcements - only announce if we haven't seen this peer before
104
- if (this.mesh.peerDiscovery.hasPeer(fromPeerId)) {
105
- this.debug.log(`Peer ${fromPeerId.substring(0, 8)}... already known, skipping announcement`);
106
- return;
103
+ // Check if we already have an active connection to this peer
104
+ if (this.connectionManager.hasPeer(fromPeerId)) {
105
+ const peerStatus = this.connectionManager.getPeer(fromPeerId).getStatus();
106
+ if (peerStatus === 'connected') {
107
+ this.debug.log(`Peer ${fromPeerId.substring(0, 8)}... already connected (${peerStatus}), updating timestamp`);
108
+ this.mesh.peerDiscovery.updateDiscoveryTimestamp(fromPeerId);
109
+ return;
110
+ }
111
+ this.debug.log(`Peer ${fromPeerId.substring(0, 8)}... exists but not connected (${peerStatus}), allowing re-discovery`);
112
+ }
113
+
114
+ // Allow re-discovery to refresh timestamps and potentially retry connections
115
+ const wasKnown = this.mesh.peerDiscovery.hasPeer(fromPeerId);
116
+ if (wasKnown) {
117
+ this.debug.log(`Peer ${fromPeerId.substring(0, 8)}... already known, refreshing discovery`);
118
+ } else {
119
+ this.debug.log(`New peer discovered: ${fromPeerId.substring(0, 8)}...`);
107
120
  }
108
121
 
109
- this.mesh.emit('statusChanged', { type: 'info', message: `Peer ${fromPeerId.substring(0, 8)}... announced` });
122
+ this.mesh.emit('statusChanged', { type: 'info', message: `Peer ${fromPeerId.substring(0, 8)}... ${wasKnown ? 're-announced' : 'announced'}` });
110
123
  this.debug.log(`Adding discovered peer: ${fromPeerId.substring(0, 8)}... to PeerDiscovery`);
111
124
  this.debug.log(`Current peer count: ${this.connectionManager.getConnectedPeerCount()}/${this.mesh.maxPeers}`);
112
125
  this.debug.log(`All discovered peers: ${Array.from(this.mesh.peerDiscovery.discoveredPeers.keys()).map(p => p.substring(0, 8)).join(', ')}`);