-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.js
More file actions
227 lines (192 loc) · 6.29 KB
/
Copy pathtransfer.js
File metadata and controls
227 lines (192 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// DropBeam Transfer Engine v2
// Fixed: role callbacks, TURN servers, connection reliability
const CHUNK_SIZE = 64 * 1024; // 64KB
// Free TURN servers for reliable cross-network NAT traversal
const ICE_SERVERS = [
{ urls: "stun:stun.l.google.com:19302" },
{ urls: "stun:stun1.l.google.com:19302" },
{ urls: "stun:stun2.l.google.com:19302" },
{ urls: "stun:stun.cloudflare.com:3478" },
{
urls: "turn:openrelay.metered.ca:80",
username: "openrelayproject",
credential: "openrelayproject"
},
{
urls: "turn:openrelay.metered.ca:443",
username: "openrelayproject",
credential: "openrelayproject"
},
{
urls: "turn:openrelay.metered.ca:443?transport=tcp",
username: "openrelayproject",
credential: "openrelayproject"
}
];
class DropBeamTransfer {
constructor(callbacks) {
this.peer = null;
this.conn = null;
this.peerId = null;
this.callbacks = callbacks;
this._incoming = null;
}
init() {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error("PeerJS server timeout — check internet connection"));
}, 15000);
try {
this.peer = new Peer(undefined, {
config: { iceServers: ICE_SERVERS },
debug: 0
});
} catch (e) {
clearTimeout(timeout);
return reject(e);
}
this.peer.on("open", id => {
clearTimeout(timeout);
this.peerId = id;
// ✅ FIX: just signal ready — role logic stays in app.js
this.callbacks.onReady?.(id);
resolve(id);
});
// ✅ FIX: host receives incoming connection (this device is the FILE SENDER)
this.peer.on("connection", conn => {
this.conn = conn;
this._setupConn(conn);
// Don't call onConnected here — wait for conn "open" event
});
this.peer.on("error", err => {
clearTimeout(timeout);
console.error("PeerJS error:", err.type, err);
const msg = {
"peer-unavailable": "Code not found. Make sure the sender's tab is still open.",
"network": "Network error — check your internet connection.",
"server-error": "Signalling server error — please retry.",
"socket-error": "Socket error — please retry.",
}[err.type] || (err.message || "Unknown error");
this.callbacks.onError?.(msg);
reject(err);
});
this.peer.on("disconnected", () => {
// Try to reconnect once
if (this.peer && !this.peer.destroyed) {
this.peer.reconnect();
}
});
});
}
// Called by the FILE RECEIVER to connect to the sender's peer ID
connect(remotePeerId) {
return new Promise((resolve, reject) => {
if (!this.peer || this.peer.destroyed) {
return reject(new Error("Peer not initialized"));
}
const timeout = setTimeout(() => {
reject(new Error("Connection timed out — make sure sender's tab is open"));
}, 25000);
let conn;
try {
conn = this.peer.connect(remotePeerId, {
reliable: true,
serialization: "binary"
});
} catch (e) {
clearTimeout(timeout);
return reject(e);
}
conn.on("open", () => {
clearTimeout(timeout);
this.conn = conn;
this._setupConn(conn);
// ✅ FIX: signal connected — app.js uses currentRole to decide screen
this.callbacks.onConnected?.();
resolve();
});
conn.on("error", err => {
clearTimeout(timeout);
this.callbacks.onError?.(err.message);
reject(err);
});
});
}
_setupConn(conn) {
conn.on("open", () => {
// ✅ FIX: for INCOMING connections (sender side), signal connected here
if (this.peer && conn === this.conn) {
this.callbacks.onConnected?.();
}
});
conn.on("data", data => this._handleData(data));
conn.on("close", () => {
this.conn = null;
this.callbacks.onDisconnect?.();
});
conn.on("error", err => {
this.callbacks.onError?.(err.message || "Connection error");
});
}
_handleData(data) {
if (!data || !data.type) return;
if (data.type === "meta") {
this._incoming = {
name: data.name,
size: data.size,
fileType: data.fileType,
chunks: [],
received: 0,
total: data.total
};
this.callbacks.onReceiveStart?.({ name: data.name, size: data.size });
} else if (data.type === "chunk") {
if (!this._incoming) return;
this._incoming.chunks.push(data.data);
this._incoming.received++;
const progress = Math.round((this._incoming.received / this._incoming.total) * 100);
this.callbacks.onProgress?.({ progress, isSender: false });
if (this._incoming.received === this._incoming.total) {
const blob = new Blob(this._incoming.chunks, {
type: this._incoming.fileType || "application/octet-stream"
});
this.callbacks.onReceiveDone?.({
blob,
name: this._incoming.name,
size: this._incoming.size
});
this._incoming = null;
}
} else if (data.type === "ack") {
this.callbacks.onSendDone?.();
}
}
async sendFile(file) {
if (!this.conn) throw new Error("Not connected");
const total = Math.ceil(file.size / CHUNK_SIZE);
// Send metadata
this.conn.send({ type: "meta", name: file.name, size: file.size, fileType: file.type, total });
let offset = 0;
let chunkIndex = 0;
while (offset < file.size) {
const slice = file.slice(offset, offset + CHUNK_SIZE);
const buffer = await slice.arrayBuffer();
this.conn.send({ type: "chunk", data: buffer, index: chunkIndex });
offset += CHUNK_SIZE;
chunkIndex++;
const progress = Math.round((chunkIndex / total) * 100);
this.callbacks.onProgress?.({ progress, isSender: true });
// Yield every 8 chunks to keep UI responsive
if (chunkIndex % 8 === 0) await new Promise(r => setTimeout(r, 0));
}
// Notify receiver transfer is complete
try { this.conn?.send({ type: "ack" }); } catch {}
}
destroy() {
try { this.conn?.close(); } catch {}
try { this.peer?.destroy(); } catch {}
this.conn = null;
this.peer = null;
}
}
window.DropBeamTransfer = DropBeamTransfer;