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
|
@@ -0,0 +1,935 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>PeerPigeon Stream File Transfer Demo</title>
|
|
5
|
+
<style>
|
|
6
|
+
* {
|
|
7
|
+
box-sizing: border-box;
|
|
8
|
+
margin: 0;
|
|
9
|
+
padding: 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
body {
|
|
13
|
+
font-family: 'Segoe UI', system-ui, sans-serif;
|
|
14
|
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
15
|
+
min-height: 100vh;
|
|
16
|
+
padding: 20px;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.container {
|
|
20
|
+
max-width: 1400px;
|
|
21
|
+
margin: 0 auto;
|
|
22
|
+
background: white;
|
|
23
|
+
border-radius: 16px;
|
|
24
|
+
padding: 40px;
|
|
25
|
+
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
h1 {
|
|
29
|
+
color: #667eea;
|
|
30
|
+
margin-bottom: 10px;
|
|
31
|
+
display: flex;
|
|
32
|
+
align-items: center;
|
|
33
|
+
gap: 12px;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.subtitle {
|
|
37
|
+
color: #666;
|
|
38
|
+
margin-bottom: 30px;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.status-bar {
|
|
42
|
+
display: grid;
|
|
43
|
+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
|
44
|
+
gap: 15px;
|
|
45
|
+
margin-bottom: 30px;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.connection-panel {
|
|
49
|
+
background: rgba(255, 255, 255, 0.85);
|
|
50
|
+
border-radius: 12px;
|
|
51
|
+
border: 2px solid #dee2e6;
|
|
52
|
+
padding: 25px;
|
|
53
|
+
margin-bottom: 30px;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.connection-panel h2 {
|
|
57
|
+
color: #495057;
|
|
58
|
+
margin-bottom: 16px;
|
|
59
|
+
font-size: 20px;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.connection-grid {
|
|
63
|
+
display: grid;
|
|
64
|
+
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
|
|
65
|
+
gap: 20px;
|
|
66
|
+
margin-bottom: 20px;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.connection-panel label {
|
|
70
|
+
font-weight: 600;
|
|
71
|
+
margin-bottom: 6px;
|
|
72
|
+
display: block;
|
|
73
|
+
color: #495057;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.connection-panel input {
|
|
77
|
+
width: 100%;
|
|
78
|
+
padding: 12px;
|
|
79
|
+
border: 2px solid #dee2e6;
|
|
80
|
+
border-radius: 8px;
|
|
81
|
+
font-size: 14px;
|
|
82
|
+
transition: border-color 0.2s, box-shadow 0.2s;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.connection-panel input:focus {
|
|
86
|
+
outline: none;
|
|
87
|
+
border-color: #667eea;
|
|
88
|
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.25);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.connection-buttons {
|
|
92
|
+
display: flex;
|
|
93
|
+
gap: 12px;
|
|
94
|
+
flex-wrap: wrap;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.connection-buttons button {
|
|
98
|
+
flex: 1;
|
|
99
|
+
min-width: 160px;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.status-tag {
|
|
103
|
+
display: inline-flex;
|
|
104
|
+
align-items: center;
|
|
105
|
+
gap: 8px;
|
|
106
|
+
padding: 8px 14px;
|
|
107
|
+
border-radius: 999px;
|
|
108
|
+
font-weight: 600;
|
|
109
|
+
font-size: 13px;
|
|
110
|
+
margin-top: 10px;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.status-connected {
|
|
114
|
+
background: rgba(40, 167, 69, 0.15);
|
|
115
|
+
color: #1e7e34;
|
|
116
|
+
border: 1px solid rgba(40, 167, 69, 0.35);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
.status-connecting {
|
|
120
|
+
background: rgba(255, 193, 7, 0.15);
|
|
121
|
+
color: #856404;
|
|
122
|
+
border: 1px solid rgba(255, 193, 7, 0.35);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.status-disconnected {
|
|
126
|
+
background: rgba(220, 53, 69, 0.15);
|
|
127
|
+
color: #841f2d;
|
|
128
|
+
border: 1px solid rgba(220, 53, 69, 0.35);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
.status-card {
|
|
132
|
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
133
|
+
color: white;
|
|
134
|
+
padding: 20px;
|
|
135
|
+
border-radius: 12px;
|
|
136
|
+
text-align: center;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.status-label {
|
|
140
|
+
font-size: 14px;
|
|
141
|
+
opacity: 0.9;
|
|
142
|
+
margin-bottom: 8px;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.status-value {
|
|
146
|
+
font-size: 28px;
|
|
147
|
+
font-weight: bold;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.main-content {
|
|
151
|
+
display: grid;
|
|
152
|
+
grid-template-columns: 1fr 1fr;
|
|
153
|
+
gap: 30px;
|
|
154
|
+
margin-bottom: 30px;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.panel {
|
|
158
|
+
background: #f8f9fa;
|
|
159
|
+
border-radius: 12px;
|
|
160
|
+
padding: 25px;
|
|
161
|
+
border: 2px solid #dee2e6;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.panel h2 {
|
|
165
|
+
color: #495057;
|
|
166
|
+
margin-bottom: 20px;
|
|
167
|
+
font-size: 20px;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
.file-upload {
|
|
171
|
+
border: 3px dashed #667eea;
|
|
172
|
+
border-radius: 12px;
|
|
173
|
+
padding: 40px;
|
|
174
|
+
text-align: center;
|
|
175
|
+
cursor: pointer;
|
|
176
|
+
transition: all 0.3s;
|
|
177
|
+
background: white;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.file-upload:hover {
|
|
181
|
+
background: #f0f4ff;
|
|
182
|
+
border-color: #5568d3;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.file-upload.drag-over {
|
|
186
|
+
background: #e0e7ff;
|
|
187
|
+
border-color: #4f46e5;
|
|
188
|
+
transform: scale(1.02);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
.file-input {
|
|
192
|
+
display: none;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
.file-info {
|
|
196
|
+
background: white;
|
|
197
|
+
border-radius: 8px;
|
|
198
|
+
padding: 15px;
|
|
199
|
+
margin: 15px 0;
|
|
200
|
+
border-left: 4px solid #667eea;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.file-name {
|
|
204
|
+
font-weight: 600;
|
|
205
|
+
color: #333;
|
|
206
|
+
margin-bottom: 5px;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
.file-size {
|
|
210
|
+
color: #666;
|
|
211
|
+
font-size: 14px;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
button {
|
|
215
|
+
background: #667eea;
|
|
216
|
+
color: white;
|
|
217
|
+
border: none;
|
|
218
|
+
padding: 14px 28px;
|
|
219
|
+
border-radius: 8px;
|
|
220
|
+
cursor: pointer;
|
|
221
|
+
font-size: 16px;
|
|
222
|
+
font-weight: 600;
|
|
223
|
+
transition: all 0.3s;
|
|
224
|
+
width: 100%;
|
|
225
|
+
margin: 10px 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
button:hover:not(:disabled) {
|
|
229
|
+
background: #5568d3;
|
|
230
|
+
transform: translateY(-2px);
|
|
231
|
+
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
button:disabled {
|
|
235
|
+
background: #ccc;
|
|
236
|
+
cursor: not-allowed;
|
|
237
|
+
transform: none;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
select {
|
|
241
|
+
width: 100%;
|
|
242
|
+
padding: 12px;
|
|
243
|
+
border: 2px solid #dee2e6;
|
|
244
|
+
border-radius: 8px;
|
|
245
|
+
font-size: 14px;
|
|
246
|
+
margin: 10px 0;
|
|
247
|
+
background: white;
|
|
248
|
+
cursor: pointer;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
.progress-container {
|
|
252
|
+
background: #e9ecef;
|
|
253
|
+
border-radius: 8px;
|
|
254
|
+
height: 30px;
|
|
255
|
+
margin: 15px 0;
|
|
256
|
+
overflow: hidden;
|
|
257
|
+
position: relative;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
.progress-bar {
|
|
261
|
+
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
|
262
|
+
height: 100%;
|
|
263
|
+
transition: width 0.3s;
|
|
264
|
+
display: flex;
|
|
265
|
+
align-items: center;
|
|
266
|
+
justify-content: center;
|
|
267
|
+
color: white;
|
|
268
|
+
font-weight: 600;
|
|
269
|
+
font-size: 14px;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
.received-files {
|
|
273
|
+
max-height: 400px;
|
|
274
|
+
overflow-y: auto;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
.received-file {
|
|
278
|
+
background: white;
|
|
279
|
+
border-radius: 8px;
|
|
280
|
+
padding: 15px;
|
|
281
|
+
margin: 10px 0;
|
|
282
|
+
border-left: 4px solid #28a745;
|
|
283
|
+
display: flex;
|
|
284
|
+
justify-content: space-between;
|
|
285
|
+
align-items: center;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
.download-btn {
|
|
289
|
+
background: #28a745;
|
|
290
|
+
padding: 8px 16px;
|
|
291
|
+
font-size: 14px;
|
|
292
|
+
width: auto;
|
|
293
|
+
margin: 0;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
.download-btn:hover {
|
|
297
|
+
background: #218838;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
.log {
|
|
301
|
+
background: #1e1e1e;
|
|
302
|
+
color: #d4d4d4;
|
|
303
|
+
padding: 20px;
|
|
304
|
+
border-radius: 12px;
|
|
305
|
+
max-height: 300px;
|
|
306
|
+
overflow-y: auto;
|
|
307
|
+
font-family: 'Courier New', monospace;
|
|
308
|
+
font-size: 13px;
|
|
309
|
+
line-height: 1.6;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
.log-entry {
|
|
313
|
+
padding: 4px 0;
|
|
314
|
+
border-bottom: 1px solid #333;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
.log-entry:last-child {
|
|
318
|
+
border-bottom: none;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
.log-entry.success { color: #4ec9b0; }
|
|
322
|
+
.log-entry.error { color: #f48771; }
|
|
323
|
+
.log-entry.info { color: #9cdcfe; }
|
|
324
|
+
.log-entry.stream { color: #dcdcaa; }
|
|
325
|
+
.log-entry.warning { color: #f2cc60; }
|
|
326
|
+
|
|
327
|
+
.peers-list {
|
|
328
|
+
display: flex;
|
|
329
|
+
gap: 10px;
|
|
330
|
+
flex-wrap: wrap;
|
|
331
|
+
margin: 15px 0;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
.peer-badge {
|
|
335
|
+
background: #667eea;
|
|
336
|
+
color: white;
|
|
337
|
+
padding: 8px 16px;
|
|
338
|
+
border-radius: 20px;
|
|
339
|
+
font-size: 14px;
|
|
340
|
+
font-weight: 500;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
.icon {
|
|
344
|
+
font-size: 48px;
|
|
345
|
+
margin-bottom: 15px;
|
|
346
|
+
}
|
|
347
|
+
</style>
|
|
348
|
+
</head>
|
|
349
|
+
<body>
|
|
350
|
+
<div class="container">
|
|
351
|
+
<h1>
|
|
352
|
+
📡 PeerPigeon Stream File Transfer
|
|
353
|
+
</h1>
|
|
354
|
+
<p class="subtitle">Transfer files efficiently using ReadableStream/WritableStream API</p>
|
|
355
|
+
|
|
356
|
+
<div class="status-bar">
|
|
357
|
+
<div class="status-card">
|
|
358
|
+
<div class="status-label">Connected Peers</div>
|
|
359
|
+
<div class="status-value" id="peer-count">0</div>
|
|
360
|
+
</div>
|
|
361
|
+
<div class="status-card">
|
|
362
|
+
<div class="status-label">Files Sent</div>
|
|
363
|
+
<div class="status-value" id="files-sent">0</div>
|
|
364
|
+
</div>
|
|
365
|
+
<div class="status-card">
|
|
366
|
+
<div class="status-label">Files Received</div>
|
|
367
|
+
<div class="status-value" id="files-received">0</div>
|
|
368
|
+
</div>
|
|
369
|
+
<div class="status-card">
|
|
370
|
+
<div class="status-label">Data Transferred</div>
|
|
371
|
+
<div class="status-value" id="data-transferred">0 MB</div>
|
|
372
|
+
</div>
|
|
373
|
+
</div>
|
|
374
|
+
|
|
375
|
+
<div class="connection-panel">
|
|
376
|
+
<h2>⚙️ Connection Settings</h2>
|
|
377
|
+
<div class="connection-grid">
|
|
378
|
+
<div>
|
|
379
|
+
<label for="signaling-url">Signaling Server URL</label>
|
|
380
|
+
<input type="text" id="signaling-url" value="ws://localhost:3000" placeholder="ws://localhost:3000">
|
|
381
|
+
</div>
|
|
382
|
+
<div>
|
|
383
|
+
<label for="network-name">Network Namespace</label>
|
|
384
|
+
<input type="text" id="network-name" value="file-transfer" placeholder="Enter namespace">
|
|
385
|
+
</div>
|
|
386
|
+
</div>
|
|
387
|
+
<div class="connection-buttons">
|
|
388
|
+
<button id="connect-btn">Connect</button>
|
|
389
|
+
<button id="disconnect-btn" disabled>Disconnect</button>
|
|
390
|
+
</div>
|
|
391
|
+
<div id="connection-status" class="status-tag status-disconnected">Disconnected</div>
|
|
392
|
+
</div>
|
|
393
|
+
|
|
394
|
+
<div class="main-content">
|
|
395
|
+
<div class="panel">
|
|
396
|
+
<h2>📤 Send Files</h2>
|
|
397
|
+
|
|
398
|
+
<div class="file-upload" id="drop-zone">
|
|
399
|
+
<div class="icon">📁</div>
|
|
400
|
+
<p style="font-size: 18px; margin-bottom: 10px;">
|
|
401
|
+
Drag & Drop files here
|
|
402
|
+
</p>
|
|
403
|
+
<p style="color: #666;">or click to browse</p>
|
|
404
|
+
<input type="file" id="file-input" class="file-input" multiple>
|
|
405
|
+
</div>
|
|
406
|
+
|
|
407
|
+
<div id="selected-files"></div>
|
|
408
|
+
|
|
409
|
+
<label style="display: block; margin-top: 20px; font-weight: 600;">Send to:</label>
|
|
410
|
+
<select id="target-peer">
|
|
411
|
+
<option value="">Select a peer...</option>
|
|
412
|
+
</select>
|
|
413
|
+
|
|
414
|
+
<div id="send-progress" style="display: none;">
|
|
415
|
+
<div class="progress-container">
|
|
416
|
+
<div class="progress-bar" id="send-progress-bar">0%</div>
|
|
417
|
+
</div>
|
|
418
|
+
</div>
|
|
419
|
+
|
|
420
|
+
<button id="send-btn" disabled>Send Files</button>
|
|
421
|
+
</div>
|
|
422
|
+
|
|
423
|
+
<div class="panel">
|
|
424
|
+
<h2>📥 Received Files</h2>
|
|
425
|
+
<div class="received-files" id="received-files">
|
|
426
|
+
<p style="color: #666; text-align: center; padding: 40px;">
|
|
427
|
+
No files received yet
|
|
428
|
+
</p>
|
|
429
|
+
</div>
|
|
430
|
+
</div>
|
|
431
|
+
</div>
|
|
432
|
+
|
|
433
|
+
<div class="panel">
|
|
434
|
+
<h2>📜 Activity Log</h2>
|
|
435
|
+
<div class="log" id="log"></div>
|
|
436
|
+
</div>
|
|
437
|
+
|
|
438
|
+
<div class="panel" style="margin-top: 20px;">
|
|
439
|
+
<h2>🔗 Connected Peers</h2>
|
|
440
|
+
<div class="peers-list" id="peers-list">
|
|
441
|
+
<span style="color: #666;">No peers connected</span>
|
|
442
|
+
</div>
|
|
443
|
+
</div>
|
|
444
|
+
</div>
|
|
445
|
+
|
|
446
|
+
<script src="../dist/peerpigeon-browser.js"></script>
|
|
447
|
+
<script>
|
|
448
|
+
const PeerPigeonMeshConstructor = window.PeerPigeonMesh || (window.PeerPigeon && window.PeerPigeon.PeerPigeonMesh);
|
|
449
|
+
|
|
450
|
+
let mesh = null;
|
|
451
|
+
let filesSent = 0;
|
|
452
|
+
let filesReceived = 0;
|
|
453
|
+
let dataTransferred = 0;
|
|
454
|
+
let selectedFiles = [];
|
|
455
|
+
let receivedFileBlobs = new Map(); // streamId -> blob
|
|
456
|
+
let isMeshConnected = false;
|
|
457
|
+
|
|
458
|
+
async function init() {
|
|
459
|
+
setupFileUpload();
|
|
460
|
+
setupConnectionControls();
|
|
461
|
+
updateUI();
|
|
462
|
+
|
|
463
|
+
if (!PeerPigeonMeshConstructor) {
|
|
464
|
+
log('❌ PeerPigeon browser bundle not found. Build the project with `npm run build`.', 'error');
|
|
465
|
+
setConnectionStatus('disconnected', 'Bundle unavailable');
|
|
466
|
+
document.getElementById('connect-btn').disabled = true;
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
log('✅ Ready! Configure your namespace and click Connect when you are set.', 'info');
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function setupConnectionControls() {
|
|
474
|
+
const connectBtn = document.getElementById('connect-btn');
|
|
475
|
+
const disconnectBtn = document.getElementById('disconnect-btn');
|
|
476
|
+
const networkInput = document.getElementById('network-name');
|
|
477
|
+
|
|
478
|
+
connectBtn.addEventListener('click', () => {
|
|
479
|
+
connectMesh().catch(error => {
|
|
480
|
+
log(`❌ Connection error: ${error.message}`, 'error');
|
|
481
|
+
setConnectionStatus('disconnected', 'Connection failed');
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
disconnectBtn.addEventListener('click', () => {
|
|
486
|
+
disconnectMesh().catch(error => {
|
|
487
|
+
log(`⚠️ Error during disconnect: ${error.message}`, 'error');
|
|
488
|
+
});
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
networkInput.addEventListener('keydown', (event) => {
|
|
492
|
+
if (event.key === 'Enter') {
|
|
493
|
+
event.preventDefault();
|
|
494
|
+
connectBtn.click();
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
async function connectMesh() {
|
|
500
|
+
const signalingUrl = document.getElementById('signaling-url').value.trim() || 'ws://localhost:3000';
|
|
501
|
+
const networkName = document.getElementById('network-name').value.trim() || 'file-transfer';
|
|
502
|
+
const connectBtn = document.getElementById('connect-btn');
|
|
503
|
+
const disconnectBtn = document.getElementById('disconnect-btn');
|
|
504
|
+
|
|
505
|
+
if (!PeerPigeonMeshConstructor) {
|
|
506
|
+
throw new Error('PeerPigeon bundle unavailable');
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
connectBtn.disabled = true;
|
|
510
|
+
disconnectBtn.disabled = true;
|
|
511
|
+
setConnectionStatus('connecting', `Connecting to ${networkName}...`);
|
|
512
|
+
|
|
513
|
+
if (mesh) {
|
|
514
|
+
try {
|
|
515
|
+
await mesh.disconnect();
|
|
516
|
+
} catch (error) {
|
|
517
|
+
log(`⚠️ Error disconnecting previous mesh: ${error.message}`, 'error');
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
mesh = new PeerPigeonMeshConstructor({
|
|
522
|
+
signalingServer: signalingUrl,
|
|
523
|
+
networkName,
|
|
524
|
+
autoConnect: true,
|
|
525
|
+
maxPeers: 8,
|
|
526
|
+
debug: true
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
attachMeshEventListeners(mesh);
|
|
530
|
+
|
|
531
|
+
try {
|
|
532
|
+
if (typeof mesh.init === 'function') {
|
|
533
|
+
setConnectionStatus('connecting', 'Initializing mesh...');
|
|
534
|
+
await mesh.init();
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
await mesh.connect(signalingUrl);
|
|
538
|
+
isMeshConnected = true;
|
|
539
|
+
setConnectionStatus('connected', `Connected to ${networkName}`);
|
|
540
|
+
log(`🤝 Connected to namespace "${networkName}"`, 'success');
|
|
541
|
+
updateUI();
|
|
542
|
+
disconnectBtn.disabled = false;
|
|
543
|
+
} catch (error) {
|
|
544
|
+
isMeshConnected = false;
|
|
545
|
+
setConnectionStatus('disconnected', 'Connection failed');
|
|
546
|
+
mesh = null;
|
|
547
|
+
throw error;
|
|
548
|
+
} finally {
|
|
549
|
+
connectBtn.disabled = false;
|
|
550
|
+
updateSendButton();
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
async function disconnectMesh() {
|
|
555
|
+
const disconnectBtn = document.getElementById('disconnect-btn');
|
|
556
|
+
const connectBtn = document.getElementById('connect-btn');
|
|
557
|
+
disconnectBtn.disabled = true;
|
|
558
|
+
connectBtn.disabled = true;
|
|
559
|
+
|
|
560
|
+
if (!mesh) {
|
|
561
|
+
setConnectionStatus('disconnected', 'No active connection');
|
|
562
|
+
connectBtn.disabled = false;
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
try {
|
|
567
|
+
await mesh.disconnect();
|
|
568
|
+
log('👋 Disconnected from mesh', 'info');
|
|
569
|
+
} finally {
|
|
570
|
+
mesh = null;
|
|
571
|
+
isMeshConnected = false;
|
|
572
|
+
updateUI();
|
|
573
|
+
setConnectionStatus('disconnected', 'Disconnected');
|
|
574
|
+
connectBtn.disabled = false;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function attachMeshEventListeners(meshInstance) {
|
|
579
|
+
const safeHandler = (callback) => (event) => {
|
|
580
|
+
if (meshInstance !== mesh) return;
|
|
581
|
+
callback(event);
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
meshInstance.addEventListener('connected', safeHandler(() => {
|
|
585
|
+
log('✅ Connected to signaling server', 'success');
|
|
586
|
+
}));
|
|
587
|
+
|
|
588
|
+
meshInstance.addEventListener('peerConnected', safeHandler((event) => {
|
|
589
|
+
log(`🤝 Peer connected: ${event.peerId.substring(0, 8)}...`, 'success');
|
|
590
|
+
updateUI();
|
|
591
|
+
}));
|
|
592
|
+
|
|
593
|
+
meshInstance.addEventListener('peerDisconnected', safeHandler((event) => {
|
|
594
|
+
log(`👋 Peer disconnected: ${event.peerId.substring(0, 8)}...`, 'info');
|
|
595
|
+
updateUI();
|
|
596
|
+
}));
|
|
597
|
+
|
|
598
|
+
meshInstance.addEventListener('disconnected', safeHandler(() => {
|
|
599
|
+
if (!isMeshConnected) return;
|
|
600
|
+
isMeshConnected = false;
|
|
601
|
+
document.getElementById('disconnect-btn').disabled = true;
|
|
602
|
+
setConnectionStatus('disconnected', 'Disconnected');
|
|
603
|
+
updateUI();
|
|
604
|
+
}));
|
|
605
|
+
|
|
606
|
+
meshInstance.addEventListener('streamReceived', safeHandler(async (event) => {
|
|
607
|
+
const { peerId, streamId, stream, metadata } = event;
|
|
608
|
+
log(`📥 Receiving file "${metadata.filename}" from ${peerId.substring(0, 8)}... (${formatBytes(metadata.totalSize)})`, 'stream');
|
|
609
|
+
|
|
610
|
+
try {
|
|
611
|
+
const chunks = [];
|
|
612
|
+
const reader = stream.getReader();
|
|
613
|
+
|
|
614
|
+
while (true) {
|
|
615
|
+
const { done, value } = await reader.read();
|
|
616
|
+
if (done) break;
|
|
617
|
+
chunks.push(value);
|
|
618
|
+
log(`📥 Received chunk (${value.length} bytes)`, 'info');
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const blob = new Blob(chunks, { type: metadata.mimeType });
|
|
622
|
+
receivedFileBlobs.set(streamId, blob);
|
|
623
|
+
|
|
624
|
+
addReceivedFile({
|
|
625
|
+
streamId,
|
|
626
|
+
filename: metadata.filename,
|
|
627
|
+
size: metadata.totalSize,
|
|
628
|
+
mimeType: metadata.mimeType,
|
|
629
|
+
from: peerId,
|
|
630
|
+
blob
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
filesReceived++;
|
|
634
|
+
dataTransferred += metadata.totalSize;
|
|
635
|
+
updateStats();
|
|
636
|
+
|
|
637
|
+
log(`✅ File "${metadata.filename}" received successfully`, 'success');
|
|
638
|
+
} catch (error) {
|
|
639
|
+
log(`❌ Error receiving file: ${error.message}`, 'error');
|
|
640
|
+
}
|
|
641
|
+
}));
|
|
642
|
+
|
|
643
|
+
meshInstance.addEventListener('streamCompleted', safeHandler((event) => {
|
|
644
|
+
log(`✅ Stream ${event.streamId} completed`, 'success');
|
|
645
|
+
}));
|
|
646
|
+
|
|
647
|
+
meshInstance.addEventListener('streamAborted', safeHandler((event) => {
|
|
648
|
+
log(`❌ Stream ${event.streamId} aborted: ${event.reason}`, 'error');
|
|
649
|
+
}));
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function setupFileUpload() {
|
|
653
|
+
const dropZone = document.getElementById('drop-zone');
|
|
654
|
+
const fileInput = document.getElementById('file-input');
|
|
655
|
+
const sendBtn = document.getElementById('send-btn');
|
|
656
|
+
const peerSelect = document.getElementById('target-peer');
|
|
657
|
+
|
|
658
|
+
dropZone.addEventListener('click', () => fileInput.click());
|
|
659
|
+
|
|
660
|
+
fileInput.addEventListener('change', (event) => {
|
|
661
|
+
handleFiles(event.target.files);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
dropZone.addEventListener('dragover', (event) => {
|
|
665
|
+
event.preventDefault();
|
|
666
|
+
dropZone.classList.add('drag-over');
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
dropZone.addEventListener('dragleave', () => {
|
|
670
|
+
dropZone.classList.remove('drag-over');
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
dropZone.addEventListener('drop', (event) => {
|
|
674
|
+
event.preventDefault();
|
|
675
|
+
dropZone.classList.remove('drag-over');
|
|
676
|
+
handleFiles(event.dataTransfer.files);
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
peerSelect.addEventListener('change', updateSendButton);
|
|
680
|
+
sendBtn.addEventListener('click', sendFiles);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function handleFiles(files) {
|
|
684
|
+
selectedFiles = Array.from(files);
|
|
685
|
+
displaySelectedFiles();
|
|
686
|
+
updateSendButton();
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function displaySelectedFiles() {
|
|
690
|
+
const container = document.getElementById('selected-files');
|
|
691
|
+
|
|
692
|
+
if (selectedFiles.length === 0) {
|
|
693
|
+
container.innerHTML = '';
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
container.innerHTML = selectedFiles.map((file) => `
|
|
698
|
+
<div class="file-info">
|
|
699
|
+
<div class="file-name">📄 ${file.name}</div>
|
|
700
|
+
<div class="file-size">${formatBytes(file.size)} • ${file.type || 'unknown type'}</div>
|
|
701
|
+
</div>
|
|
702
|
+
`).join('');
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function updateSendButton() {
|
|
706
|
+
const sendBtn = document.getElementById('send-btn');
|
|
707
|
+
const targetPeer = document.getElementById('target-peer').value;
|
|
708
|
+
sendBtn.disabled = !isMeshConnected || selectedFiles.length === 0 || !targetPeer;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
async function sendFiles() {
|
|
712
|
+
const targetPeer = document.getElementById('target-peer').value;
|
|
713
|
+
if (!isMeshConnected || !mesh) {
|
|
714
|
+
log('⚠️ Connect to a namespace before sending files.', 'warning');
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
if (!targetPeer || selectedFiles.length === 0) return;
|
|
719
|
+
|
|
720
|
+
const sendBtn = document.getElementById('send-btn');
|
|
721
|
+
const progressContainer = document.getElementById('send-progress');
|
|
722
|
+
const progressBar = document.getElementById('send-progress-bar');
|
|
723
|
+
|
|
724
|
+
sendBtn.disabled = true;
|
|
725
|
+
progressContainer.style.display = 'block';
|
|
726
|
+
|
|
727
|
+
try {
|
|
728
|
+
for (let i = 0; i < selectedFiles.length; i++) {
|
|
729
|
+
const file = selectedFiles[i];
|
|
730
|
+
const progress = Math.round(((i + 1) / selectedFiles.length) * 100);
|
|
731
|
+
|
|
732
|
+
progressBar.style.width = `${progress}%`;
|
|
733
|
+
progressBar.textContent = `${progress}% (${i + 1}/${selectedFiles.length})`;
|
|
734
|
+
|
|
735
|
+
log(`📤 Sending file "${file.name}" to ${targetPeer.substring(0, 8)}...`, 'stream');
|
|
736
|
+
|
|
737
|
+
await mesh.sendFile(targetPeer, file);
|
|
738
|
+
|
|
739
|
+
filesSent++;
|
|
740
|
+
dataTransferred += file.size;
|
|
741
|
+
updateStats();
|
|
742
|
+
|
|
743
|
+
log(`✅ File "${file.name}" sent successfully`, 'success');
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
selectedFiles = [];
|
|
747
|
+
document.getElementById('file-input').value = '';
|
|
748
|
+
displaySelectedFiles();
|
|
749
|
+
progressContainer.style.display = 'none';
|
|
750
|
+
updateSendButton();
|
|
751
|
+
|
|
752
|
+
} catch (error) {
|
|
753
|
+
log(`❌ Error sending files: ${error.message}`, 'error');
|
|
754
|
+
} finally {
|
|
755
|
+
sendBtn.disabled = false;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
function addReceivedFile(fileData) {
|
|
760
|
+
const container = document.getElementById('received-files');
|
|
761
|
+
|
|
762
|
+
if (container.querySelector('p')) {
|
|
763
|
+
container.innerHTML = '';
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
const fileEl = document.createElement('div');
|
|
767
|
+
fileEl.className = 'received-file';
|
|
768
|
+
fileEl.innerHTML = `
|
|
769
|
+
<div>
|
|
770
|
+
<div class="file-name">📄 ${fileData.filename}</div>
|
|
771
|
+
<div class="file-size">
|
|
772
|
+
${formatBytes(fileData.size)} • From ${fileData.from.substring(0, 8)}...
|
|
773
|
+
</div>
|
|
774
|
+
</div>
|
|
775
|
+
<button class="download-btn" onclick="downloadFile('${fileData.streamId}')">
|
|
776
|
+
⬇️ Download
|
|
777
|
+
</button>
|
|
778
|
+
`;
|
|
779
|
+
container.insertBefore(fileEl, container.firstChild);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
function downloadFile(streamId) {
|
|
783
|
+
const blob = receivedFileBlobs.get(streamId);
|
|
784
|
+
if (!blob) return;
|
|
785
|
+
|
|
786
|
+
const url = URL.createObjectURL(blob);
|
|
787
|
+
const a = document.createElement('a');
|
|
788
|
+
a.href = url;
|
|
789
|
+
a.download = `received-file-${Date.now()}`;
|
|
790
|
+
a.click();
|
|
791
|
+
URL.revokeObjectURL(url);
|
|
792
|
+
|
|
793
|
+
log('💾 File downloaded', 'success');
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
function updateUI() {
|
|
797
|
+
updatePeersList();
|
|
798
|
+
updatePeerDropdown();
|
|
799
|
+
updateStats();
|
|
800
|
+
updateSendButton();
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
function getConnectedPeers() {
|
|
804
|
+
if (!mesh || !isMeshConnected) {
|
|
805
|
+
return [];
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
const normalizePeer = (peer) => {
|
|
809
|
+
if (!peer) return null;
|
|
810
|
+
if (typeof peer === 'string') return { peerId: peer };
|
|
811
|
+
if (typeof peer.peerId === 'string') return { peerId: peer.peerId };
|
|
812
|
+
return null;
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
const collectPeers = (source) => {
|
|
816
|
+
if (!source) return [];
|
|
817
|
+
if (typeof source === 'string') {
|
|
818
|
+
const single = normalizePeer(source);
|
|
819
|
+
return single ? [single] : [];
|
|
820
|
+
}
|
|
821
|
+
const normalized = Array.from(source)
|
|
822
|
+
.map(normalizePeer)
|
|
823
|
+
.filter(Boolean);
|
|
824
|
+
return normalized;
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
try {
|
|
828
|
+
if (typeof mesh.getConnectedPeers === 'function') {
|
|
829
|
+
const peers = mesh.getConnectedPeers();
|
|
830
|
+
if (peers && typeof peers[Symbol.iterator] === 'function') {
|
|
831
|
+
return collectPeers(peers);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
if (typeof mesh.getConnectedPeerIds === 'function') {
|
|
836
|
+
return collectPeers(mesh.getConnectedPeerIds());
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
if (mesh.connectionManager) {
|
|
840
|
+
if (typeof mesh.connectionManager.getConnectedPeers === 'function') {
|
|
841
|
+
return collectPeers(mesh.connectionManager.getConnectedPeers());
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
if (typeof mesh.connectionManager.getPeers === 'function') {
|
|
845
|
+
const peerSummaries = mesh.connectionManager.getPeers()
|
|
846
|
+
.filter(peer => peer.status === 'connected')
|
|
847
|
+
.map(peer => peer.peerId);
|
|
848
|
+
return collectPeers(peerSummaries);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
if (mesh.peers) {
|
|
853
|
+
if (typeof mesh.peers.values === 'function') {
|
|
854
|
+
return collectPeers(mesh.peers.values());
|
|
855
|
+
}
|
|
856
|
+
return collectPeers(Object.values(mesh.peers));
|
|
857
|
+
}
|
|
858
|
+
} catch (error) {
|
|
859
|
+
log(`⚠️ Could not fetch peers: ${error.message}`, 'warning');
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
return [];
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function updatePeersList() {
|
|
866
|
+
const peers = getConnectedPeers();
|
|
867
|
+
const container = document.getElementById('peers-list');
|
|
868
|
+
|
|
869
|
+
if (peers.length === 0) {
|
|
870
|
+
container.innerHTML = '<span style="color: #666;">No peers connected</span>';
|
|
871
|
+
} else {
|
|
872
|
+
container.innerHTML = peers.map(peer =>
|
|
873
|
+
`<div class="peer-badge">${peer.peerId.substring(0, 12)}...</div>`
|
|
874
|
+
).join('');
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
document.getElementById('peer-count').textContent = peers.length;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
function updatePeerDropdown() {
|
|
881
|
+
const peers = getConnectedPeers();
|
|
882
|
+
const select = document.getElementById('target-peer');
|
|
883
|
+
const currentValue = select.value;
|
|
884
|
+
|
|
885
|
+
select.innerHTML = '<option value="">Select a peer...</option>';
|
|
886
|
+
peers.forEach(peer => {
|
|
887
|
+
const option = document.createElement('option');
|
|
888
|
+
option.value = peer.peerId;
|
|
889
|
+
option.textContent = peer.peerId.substring(0, 20) + '...';
|
|
890
|
+
select.appendChild(option);
|
|
891
|
+
});
|
|
892
|
+
|
|
893
|
+
if (peers.some(peer => peer.peerId === currentValue)) {
|
|
894
|
+
select.value = currentValue;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
function updateStats() {
|
|
899
|
+
document.getElementById('files-sent').textContent = filesSent;
|
|
900
|
+
document.getElementById('files-received').textContent = filesReceived;
|
|
901
|
+
document.getElementById('data-transferred').textContent =
|
|
902
|
+
(dataTransferred / (1024 * 1024)).toFixed(2) + ' MB';
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
function setConnectionStatus(state, message) {
|
|
906
|
+
const statusEl = document.getElementById('connection-status');
|
|
907
|
+
statusEl.textContent = message;
|
|
908
|
+
statusEl.classList.remove('status-connected', 'status-connecting', 'status-disconnected');
|
|
909
|
+
statusEl.classList.add(`status-${state}`);
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
function log(message, type = 'info') {
|
|
913
|
+
const logEl = document.getElementById('log');
|
|
914
|
+
const entry = document.createElement('div');
|
|
915
|
+
entry.className = `log-entry ${type}`;
|
|
916
|
+
entry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
|
|
917
|
+
logEl.appendChild(entry);
|
|
918
|
+
logEl.scrollTop = logEl.scrollHeight;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
function formatBytes(bytes) {
|
|
922
|
+
if (bytes < 1024) return bytes + ' B';
|
|
923
|
+
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB';
|
|
924
|
+
if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
|
|
925
|
+
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB';
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
window.downloadFile = downloadFile;
|
|
929
|
+
|
|
930
|
+
init().catch(error => {
|
|
931
|
+
log(`❌ Initialization error: ${error.message}`, 'error');
|
|
932
|
+
});
|
|
933
|
+
</script>
|
|
934
|
+
</body>
|
|
935
|
+
</html>
|