peerpigeon 1.0.4 → 1.0.7
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/README.md +60 -1
- package/dist/peerpigeon-browser.js +800 -36
- package/dist/peerpigeon-browser.js.map +3 -3
- package/docs/BINARY_IMPLEMENTATION_SUMMARY.md +304 -0
- package/docs/BINARY_MESSAGES.md +332 -0
- package/docs/BROADCAST_STREAMING.md +388 -0
- package/docs/PEER_DISCOVERY_FIXES.md +140 -0
- package/docs/STREAMING_API.md +542 -0
- package/docs/STREAMING_IMPLEMENTATION_SUMMARY.md +286 -0
- package/examples/binary-message-demo.html +496 -0
- package/examples/broadcast-stream-demo.html +539 -0
- package/examples/browser-2/EXPRESS_SOLUTION.md +183 -0
- package/examples/browser-2/ICE_DEBUG.md +171 -0
- package/examples/browser-2/IMPLEMENTATION.md +218 -0
- package/examples/browser-2/QUICK_START.md +149 -0
- package/examples/browser-2/README.md +303 -0
- package/examples/browser-2/dist/assets/index-Cck_6WJV.css +1 -0
- package/examples/browser-2/dist/assets/index-D8-cNlWQ.js +57 -0
- package/examples/browser-2/dist/index.html +32 -0
- package/examples/browser-2/dist/peerpigeon-browser.js +12483 -0
- package/examples/browser-2/index.html +2 -1
- package/examples/browser-2/package-lock.json +1276 -0
- package/examples/browser-2/package.json +5 -3
- package/examples/browser-2/public/peerpigeon-browser.js +12483 -0
- package/examples/browser-2/src/App.vue +7 -4
- package/examples/browser-2/src/components/CryptoView.vue +4 -4
- package/examples/browser-2/src/components/TestingView.vue +794 -0
- package/examples/browser-2/src/main.js +7 -1
- package/examples/browser-2/src/router.js +63 -0
- package/examples/browser-2/src/stores/peerpigeon.js +68 -7
- package/examples/browser-2/vite.config.js +15 -1
- package/examples/stream-file-transfer-demo.html +935 -0
- package/package.json +1 -1
- package/server/index.js +175 -38
- package/src/ConnectionManager.js +68 -8
- package/src/GossipManager.js +22 -0
- package/src/PeerConnection.js +421 -11
- package/src/PeerDiscovery.js +33 -18
- package/src/PeerPigeonMesh.js +453 -1
- package/src/SignalingClient.js +12 -5
- package/src/SignalingHandler.js +18 -5
- package/src/browser-entry.js +5 -1
package/src/PeerPigeonMesh.js
CHANGED
|
@@ -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') {
|
|
@@ -444,7 +459,7 @@ export class PeerPigeonMesh extends EventEmitter {
|
|
|
444
459
|
this.connected = false;
|
|
445
460
|
this.polling = false;
|
|
446
461
|
this.peerDiscovery.stop();
|
|
447
|
-
|
|
462
|
+
// Don't disconnect peers - WebRTC connections are peer-to-peer and persist without signaling server
|
|
448
463
|
this.emit('statusChanged', { type: 'disconnected' });
|
|
449
464
|
});
|
|
450
465
|
|
|
@@ -815,6 +830,296 @@ export class PeerPigeonMesh extends EventEmitter {
|
|
|
815
830
|
return await this.gossipManager.broadcastMessage(content, 'chat');
|
|
816
831
|
}
|
|
817
832
|
|
|
833
|
+
/**
|
|
834
|
+
* Send binary data to a specific peer
|
|
835
|
+
* @param {string} targetPeerId - The destination peer's ID
|
|
836
|
+
* @param {Uint8Array|ArrayBuffer} binaryData - The binary data to send
|
|
837
|
+
* @returns {boolean} True if sent successfully
|
|
838
|
+
*/
|
|
839
|
+
async sendBinaryData(targetPeerId, binaryData) {
|
|
840
|
+
if (!targetPeerId || typeof targetPeerId !== 'string') {
|
|
841
|
+
this.debug.error('Invalid targetPeerId for binary message');
|
|
842
|
+
return false;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
const peerConnection = this.connectionManager.peers.get(targetPeerId);
|
|
846
|
+
if (!peerConnection) {
|
|
847
|
+
this.debug.error(`No connection to peer ${targetPeerId.substring(0, 8)}...`);
|
|
848
|
+
return false;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
return peerConnection.sendMessage(binaryData);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Broadcast binary data to all connected peers
|
|
856
|
+
* @param {Uint8Array|ArrayBuffer} binaryData - The binary data to broadcast
|
|
857
|
+
* @returns {number} Number of peers the data was sent to
|
|
858
|
+
*/
|
|
859
|
+
async broadcastBinaryData(binaryData) {
|
|
860
|
+
const peers = this.getConnectedPeers();
|
|
861
|
+
let sentCount = 0;
|
|
862
|
+
|
|
863
|
+
for (const peer of peers) {
|
|
864
|
+
if (await this.sendBinaryData(peer.peerId, binaryData)) {
|
|
865
|
+
sentCount++;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
this.debug.log(`📦 Binary data broadcasted to ${sentCount}/${peers.length} peers`);
|
|
870
|
+
return sentCount;
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Create a writable stream to send data to a specific peer
|
|
875
|
+
* @param {string} targetPeerId - The destination peer's ID
|
|
876
|
+
* @param {object} options - Stream options (filename, mimeType, totalSize, etc.)
|
|
877
|
+
* @returns {WritableStream} A writable stream
|
|
878
|
+
*/
|
|
879
|
+
createStreamToPeer(targetPeerId, options = {}) {
|
|
880
|
+
if (!targetPeerId || typeof targetPeerId !== 'string') {
|
|
881
|
+
throw new Error('Invalid targetPeerId for stream');
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
const peerConnection = this.connectionManager.peers.get(targetPeerId);
|
|
885
|
+
if (!peerConnection) {
|
|
886
|
+
throw new Error(`No connection to peer ${targetPeerId.substring(0, 8)}...`);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
return peerConnection.createWritableStream(options);
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Send a ReadableStream to a peer
|
|
894
|
+
* @param {string} targetPeerId - The destination peer's ID
|
|
895
|
+
* @param {ReadableStream} readableStream - The stream to send
|
|
896
|
+
* @param {object} options - Stream options (filename, mimeType, etc.)
|
|
897
|
+
* @returns {Promise<void>}
|
|
898
|
+
*/
|
|
899
|
+
async sendStream(targetPeerId, readableStream, options = {}) {
|
|
900
|
+
const writableStream = this.createStreamToPeer(targetPeerId, options);
|
|
901
|
+
|
|
902
|
+
try {
|
|
903
|
+
await readableStream.pipeTo(writableStream);
|
|
904
|
+
this.debug.log(`✅ Stream sent successfully to ${targetPeerId.substring(0, 8)}...`);
|
|
905
|
+
} catch (error) {
|
|
906
|
+
this.debug.error(`❌ Failed to send stream to ${targetPeerId.substring(0, 8)}...:`, error);
|
|
907
|
+
throw error;
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/**
|
|
912
|
+
* Send a File to a peer using streams
|
|
913
|
+
* @param {string} targetPeerId - The destination peer's ID
|
|
914
|
+
* @param {File} file - The file to send
|
|
915
|
+
* @returns {Promise<void>}
|
|
916
|
+
*/
|
|
917
|
+
async sendFile(targetPeerId, file) {
|
|
918
|
+
this.debug.log(`📁 Sending file "${file.name}" (${file.size} bytes) to ${targetPeerId.substring(0, 8)}...`);
|
|
919
|
+
|
|
920
|
+
const options = {
|
|
921
|
+
filename: file.name,
|
|
922
|
+
mimeType: file.type,
|
|
923
|
+
totalSize: file.size,
|
|
924
|
+
type: 'file'
|
|
925
|
+
};
|
|
926
|
+
|
|
927
|
+
const stream = file.stream();
|
|
928
|
+
await this.sendStream(targetPeerId, stream, options);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* Send a Blob to a peer using streams
|
|
933
|
+
* @param {string} targetPeerId - The destination peer's ID
|
|
934
|
+
* @param {Blob} blob - The blob to send
|
|
935
|
+
* @param {object} options - Additional options
|
|
936
|
+
* @returns {Promise<void>}
|
|
937
|
+
*/
|
|
938
|
+
async sendBlob(targetPeerId, blob, options = {}) {
|
|
939
|
+
this.debug.log(`📦 Sending blob (${blob.size} bytes) to ${targetPeerId.substring(0, 8)}...`);
|
|
940
|
+
|
|
941
|
+
const streamOptions = {
|
|
942
|
+
...options,
|
|
943
|
+
mimeType: blob.type,
|
|
944
|
+
totalSize: blob.size,
|
|
945
|
+
type: 'blob'
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
const stream = blob.stream();
|
|
949
|
+
await this.sendStream(targetPeerId, stream, streamOptions);
|
|
950
|
+
}
|
|
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
|
+
|
|
818
1123
|
// Helper methods for backward compatibility
|
|
819
1124
|
canAcceptMorePeers() {
|
|
820
1125
|
return this.connectionManager.canAcceptMorePeers();
|
|
@@ -1638,6 +1943,153 @@ export class PeerPigeonMesh extends EventEmitter {
|
|
|
1638
1943
|
this.signalingHandler.handleSignalingMessage(reconstitutedMessage);
|
|
1639
1944
|
}
|
|
1640
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
|
+
|
|
1641
2093
|
/**
|
|
1642
2094
|
* Send a signaling message, using mesh connections for renegotiation
|
|
1643
2095
|
* @param {Object} message - The signaling message
|
package/src/SignalingClient.js
CHANGED
|
@@ -19,6 +19,7 @@ export class SignalingClient extends EventEmitter {
|
|
|
19
19
|
this.connectionPromise = null;
|
|
20
20
|
this.reconnectTimeout = null;
|
|
21
21
|
this.isReconnecting = false;
|
|
22
|
+
this.deliberateDisconnect = false; // Track if disconnect was intentional
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
setConnectionType(type) {
|
|
@@ -200,15 +201,18 @@ export class SignalingClient extends EventEmitter {
|
|
|
200
201
|
|
|
201
202
|
this.debug.log('WebSocket closed:', event.code, event.reason);
|
|
202
203
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
204
|
+
// Always emit disconnected event
|
|
205
|
+
this.emit('disconnected');
|
|
206
|
+
|
|
207
|
+
// Attempt reconnection unless this was a deliberate disconnect
|
|
208
|
+
if (!this.deliberateDisconnect) {
|
|
208
209
|
this.emit('statusChanged', { type: 'warning', message: 'WebSocket connection lost - reconnecting...' });
|
|
209
210
|
if (!this.isReconnecting) {
|
|
210
211
|
this.attemptReconnect();
|
|
211
212
|
}
|
|
213
|
+
} else {
|
|
214
|
+
// Reset the flag for future connections
|
|
215
|
+
this.deliberateDisconnect = false;
|
|
212
216
|
}
|
|
213
217
|
};
|
|
214
218
|
|
|
@@ -295,6 +299,9 @@ export class SignalingClient extends EventEmitter {
|
|
|
295
299
|
}
|
|
296
300
|
|
|
297
301
|
disconnect() {
|
|
302
|
+
// Mark as deliberate disconnect to prevent automatic reconnection
|
|
303
|
+
this.deliberateDisconnect = true;
|
|
304
|
+
|
|
298
305
|
// Clear reconnection state
|
|
299
306
|
this.isReconnecting = false;
|
|
300
307
|
if (this.reconnectTimeout) {
|
package/src/SignalingHandler.js
CHANGED
|
@@ -100,13 +100,26 @@ export class SignalingHandler extends EventEmitter {
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
handlePeerAnnouncement(fromPeerId) {
|
|
103
|
-
//
|
|
104
|
-
if (this.
|
|
105
|
-
this.
|
|
106
|
-
|
|
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(', ')}`);
|
package/src/browser-entry.js
CHANGED
|
@@ -8,7 +8,8 @@ import * as UnSEA from 'unsea';
|
|
|
8
8
|
import { createPigeonRTC, PigeonRTC, BrowserRTCAdapter, RTCAdapter } from './PigeonRTC-browser.js';
|
|
9
9
|
|
|
10
10
|
// Import only browser-compatible components
|
|
11
|
-
|
|
11
|
+
import { PeerPigeonMesh as PeerPigeonMeshClass } from '../src/PeerPigeonMesh.js';
|
|
12
|
+
export { PeerPigeonMeshClass as PeerPigeonMesh };
|
|
12
13
|
export { PeerConnection } from '../src/PeerConnection.js';
|
|
13
14
|
export { SignalingClient } from '../src/SignalingClient.js';
|
|
14
15
|
export { WebDHT } from '../src/WebDHT.js';
|
|
@@ -53,6 +54,9 @@ if (typeof window !== 'undefined') {
|
|
|
53
54
|
RTCAdapter,
|
|
54
55
|
default: createPigeonRTC
|
|
55
56
|
};
|
|
57
|
+
|
|
58
|
+
// Make PeerPigeonMesh available globally for <script> tag usage
|
|
59
|
+
window.PeerPigeonMesh = PeerPigeonMeshClass;
|
|
56
60
|
}
|
|
57
61
|
|
|
58
62
|
console.log('🔐 PeerPigeon browser bundle loaded with embedded UnSEA crypto and PigeonRTC');
|