-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
596 lines (523 loc) · 15.4 KB
/
main.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import "@fontsource-variable/dm-sans";
import { initializeApp } from "firebase/app";
import {
getDatabase,
onChildAdded,
onChildRemoved,
ref,
remove,
set,
} from "firebase/database";
import { Peer } from "peerjs";
import emojis from "./emojis.json";
import "./style.css";
const fireApp = initializeApp({
apiKey: atob("QUl6YVN5RF9GcUJRVko4T0N2aHhibDI1bldRek1TM1d6bzEyemNR"),
projectId: atob("cGVlci10by1wZWVyLTQyZjE2"),
});
const fireDb = getDatabase(fireApp);
const devicesOnlinePath = "devicesOnline";
/**
* Body is the file drop area for file upload.
*/
const dropArea = document.body;
const html = document.body.parentElement;
const mediaMode = "(prefers-color-scheme: dark)";
const matchMedia = window.matchMedia;
const themeMeta = document.getElementById("themeMeta");
/**
* Download file link after data is ready.
*/
const downloadLink = document.getElementById("downloadLink");
/**
* This is the message that shows right under the app title.
*/
const helperMessage = document.getElementById("helperMessage");
/**
* "Send file" button wrapper.
* This contains a label & file input.
* We need this handler to manage the visibility.
*/
const sendButtonWrap = document.getElementById("sendButtonWrap");
const filePickerButton = document.getElementById("filePickerButton");
// Wrap that holds all post connection functionalities.
const postConnectWrap = document.getElementById("postConnectWrap");
const filePicker = document.getElementById("filePicker");
const textInput = document.getElementById("textInput");
/**
* Assigned emoji of the active session.
*/
const deviceEmojiWrap = document.getElementById("deviceEmojiWrap");
//
const transferProgress = document.getElementById("transferProgress");
/**
* This is the wrapper for the emoji buttons of other connections that are discoverd.
* We will create <button> element & append to this wrap.
*/
const startPeerButtonsWrap = document.getElementById("startPeerButtonsWrap");
/**
* Get a random emoji from preset emoji list.
*/
const getDeviceEmoji = () => {
const emojiIndex = Math.floor(Math.random() * (emojis.length - 1) + 0);
const emoji = emojis[emojiIndex];
// And show in the UI.
deviceEmojiWrap.innerHTML = emoji;
return emoji;
};
const initPeer = async () => {
state.peer?.destroy();
state.peer = new Peer();
state.emoji = getDeviceEmoji();
state.remote = null;
return new Promise((resolve) => {
state.peer.once("open", (peerId) => {
resolve(peerId);
});
});
};
/**
* State of the app.
* This is where the shared variables are.
* We will have to keep the active & remote connections for later use.
*/
const state = {
/**
* Id will be created by default from peerjs.
* We have the id-emoji relation from the firebase database.
*/
/** @type {import('peerjs').Peer} */
peer: null,
emoji: null,
/** @type {import('peerjs').DataConnection} */
remote: null,
focusLostOn: 0,
devicesOnline: {},
/**
* To keep track of the received data.
* Every time, when we get the meta data for the first time, we will reset this.
*/
fileData: {
meta: {},
data: [],
size: 0,
},
};
const destroyRemote = async () => {
// TODO: Improve this.
postConnectWrap.classList.replace("flex", "hidden");
sendButtonWrap.classList.replace("flex", "hidden");
textInput.classList.add("hidden");
textInput.value = "";
state.fileData.size = 0;
state.fileData.data = [];
await delay(2000);
state.remote?.close();
state.remote = null;
startPeerButtonsWrap.classList.remove("hidden");
checkForEmptyConnections();
};
const connectToRemote = (id, isIncoming = false) => {
if (!isIncoming) {
helperMessage.innerText = "connecting...";
}
// Once a connection is clicked, hide other connections.
startPeerButtonsWrap.classList.add("hidden");
const connection = state.peer.connect(id, {
reliable: true, // For handling large files.
});
postConnectWrap.classList.replace("flex", "hidden");
sendButtonWrap.classList.replace("flex", "hidden");
textInput.classList.add("hidden");
delay(4000).then(async () => {
const connectionState = state.remote?.peerConnection?.connectionState;
// If remote is not connected even after some time, cancel.
if (connectionState !== "connected") {
helperMessage.innerText = "Device not reachable!";
await destroyRemote(id);
deRegisterDevice(id);
} else if (connectionState === "connected") {
postConnectWrap.classList.replace("hidden", "flex");
sendButtonWrap.classList.replace("hidden", "flex");
textInput.classList.remove("hidden");
helperMessage.innerText = `Connected to: ${state.devicesOnline[id].emoji}`;
}
});
return connection;
};
// Remove all connections that are added 2 hours ago.
setTimeout(() => {
for (const device of Object.values(state.devicesOnline)) {
if (Date.now() - device.timeUpdated > 7200000) {
deRegisterDevice(device.peerId);
}
}
}, 3000);
/**
* Discovery logic.
* Listening for connections added from firebase database.
*/
onChildAdded(ref(fireDb, devicesOnlinePath), (snapshot) => {
const device = snapshot.val();
if (device.peerId !== state.peer.id && !state.devicesOnline[device.peerId]) {
const button = document.createElement("button");
button.innerText = device.emoji;
button.setAttribute("data-id", device.peerId);
button.classList.add(
"dark:bg-opacity-20",
"dark:bg-black",
"bg-white",
"w-12",
"h-12",
"bg-opacity-20",
"rounded-full",
"text-3xl",
"discovered",
);
button.onclick = (event) => {
event.preventDefault();
connectToRemote(device.peerId);
};
// Add button to the list.
startPeerButtonsWrap.append(button);
// Syncing to the state.
state.devicesOnline[device.peerId] = device;
}
checkForEmptyConnections();
});
/**
* Discovery logic.
* Listening for connections removed from firebase database.
*/
onChildRemoved(ref(fireDb, devicesOnlinePath), (snapshot) => {
const device = snapshot.val();
// Finding & removing the button which should be removed.
const buttons = document.getElementsByClassName("discovered");
for (let index = 0; index < buttons.length; index++) {
const element = buttons[index];
if (element.getAttribute("data-id") === device.peerId) {
element.remove();
}
}
// Syncing to the state.
delete state.devicesOnline[device.peerId];
checkForEmptyConnections();
});
/**
* Checking if there are connections available.
* If yes, update the helper message accordingly.
*/
const checkForEmptyConnections = () => {
// We don't want to update the helper text if remote is already connected.
if (!state.remote) {
if (Object.keys(state.devicesOnline).length > 0) {
helperMessage.innerText = "Choose a device to connect.";
} else {
helperMessage.innerText = "waiting for connections...";
}
}
};
/**
* Async method for setTimeout.
*/
const delay = (timeout) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, timeout);
});
};
/**
* Add active connection id to online devices db.
* Also make a new peer connection if peer is not initiated.
*/
const registerDevice = async (peerId = null) => {
let id = peerId;
if (state.peer === null || state.peer?.destroyed) {
// If no peer on state, initiate new peer.
id = await initPeer();
}
if (state.peer?.disconnected) {
// If existing peer is not open, try reopen in.
state.peer?.reconnect();
}
// Get local peer id as failsafe.
id = id || state.peer?.id;
// If still nothing, just stop.
if (!id) return;
set(ref(fireDb, `${devicesOnlinePath}/${id}`), {
peerId: id,
emoji: state.emoji,
timeUpdated: Date.now(),
});
};
/**
* Remove a peer id from the online devices db.
*/
const deRegisterDevice = (peerId) => {
remove(ref(fireDb, `${devicesOnlinePath}/${peerId}`));
};
registerDevice();
// Set yourself as active once peerjs is connected.
state.peer.on("open", (peerId) => {
// And listener for window focus change.
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
deRegisterDevice(peerId);
} else {
registerDevice(peerId);
}
});
});
// Try reconnecting when disconnected.
state.peer.on("disconnected", registerDevice);
state.peer.on("error", () => registerDevice());
// Post connection jobs.
state.peer.on("connection", async (connection) => {
// Respond only if remote connection state is not available.
if (!state.remote?.peerConnection?.connectionState) {
// Respond only if remote id is not matching with incoming id.
if (state.peer.id !== connection.peer) {
helperMessage.innerText = `Connected to: ${
state.devicesOnline[connection.peer].emoji
}`;
// Respond back & try remote connection.
state.remote = connectToRemote(connection.peer, true);
transferProgress.classList.add("hidden");
// Show the file transfer button after a while.
await delay(1000);
postConnectWrap.classList.replace("hidden", "flex");
startPeerButtonsWrap.classList.add("hidden");
textInput.classList.remove("hidden");
sendButtonWrap.classList.replace("hidden", "flex");
// Once connected to a device, make this device unavailable for others.
deRegisterDevice(state.peer.id);
}
}
connection.on("close", () => {
if (state.remote?.peer) {
helperMessage.innerText = "reconnecting...";
state.remote = connectToRemote(state.remote?.peer, true);
}
registerDevice();
});
connection.on("error", async () => {
helperMessage.innerText = "Disconnected!";
destroyRemote();
registerDevice();
});
connection.on("data", async (incomingData) => {
const { type, data } = incomingData;
switch (type) {
case "meta":
// First step of the file transfer.
setProgress(0);
/**
* Resetting everything but the meta data.
* We need this to check the file download progress.
*/
state.fileData.meta = data;
//
state.fileData.size = 0;
state.fileData.data = [];
downloadLink.classList.add("hidden");
break;
case "chunk":
// Only accept data chunks if meta data is present.
if (state.fileData.meta) {
const totalSize = state.fileData.meta.size;
// Add size of incoming data to total received size counter.
state.fileData.size += data.data.byteLength;
/**
* Add data to state array.
* We will make the file later once the whole transfer is complete.
*/
state.fileData.data.push(data);
const progress = (100 * state.fileData.size) / totalSize;
setProgress(progress);
/**
* Reporting the progress to the sender.
* Instead of the sender guessing the progress,
* this is the best way to show the progress on both ends.
*/
state.remote.send({ type: "progress", data: progress });
// Once the transfer is complete, trigger the file download.
if (progress === 100) {
triggerFileDownload();
}
}
break;
case "text":
textInput.value = data;
break;
case "progress":
// Just update the progress UI.
setProgress(data);
break;
}
});
});
const setProgress = async (value) => {
const progress = Math.round(value * 100) / 100;
textInput.classList.add("hidden");
sendButtonWrap.classList.replace("flex", "hidden");
transferProgress.classList.remove("hidden");
// Rounding to 00
transferProgress.innerText = `${progress.toFixed(0)}%`;
// If done, show 100% for a while and hide the progress.
if (progress === 100) {
transferProgress.innerText = "Done!";
await delay(2000);
textInput.classList.remove("hidden");
sendButtonWrap.classList.replace("hidden", "flex");
transferProgress.classList.add("hidden");
}
// If it the start of a new transfer, hide the download link.
if (progress === 0) {
downloadLink.classList.add("hidden");
}
};
/**
* Make the file from data array and trigger download.
*/
const triggerFileDownload = () => {
const { data, meta } = state.fileData;
// Make a blob with available data.
const blob = new Blob(
/**
* Make sure to sort it based on the chunk index.
* This is to ensure the order of chunks of the file sent is correct.
*/
data
.sort((a, b) => a.index - b.index)
.map((item) => item.data),
{ type: meta.type },
);
const objectURL = URL.createObjectURL(blob);
// Show the download button & trigger it.
downloadLink.classList.remove("hidden");
downloadLink.href = objectURL;
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = meta.name;
downloadLink.click();
// Reset the meta data for another file transfer.
state.fileData.meta = null;
};
/**
* Return the chunk size in mb.
*/
const calculateChunkSize = (sizeBytes) => {
const maxChunkSize = 1048576 * 10; // 10mb
const minChunkSize = 1048576 * 0.1; // 100kb
// Calculate the chunk size based on the file size
const chunkSize = Math.ceil(sizeBytes / 25);
// Ensure the chunk size is within the desired range
return Math.max(minChunkSize, Math.min(maxChunkSize, chunkSize));
};
const sendFile = async (file) => {
// First step. Send the meta data.
setProgress(0);
state.remote.send({
type: "meta",
data: {
name: file.name,
size: file.size,
type: file.type,
},
});
/**
* Index is there to ensure the file received is in correct order.
* There could be a scenario where because of a network delay,
* the first packet could come after the previous packet.
* In that case, having an index helps to later sort it before making the file for download.
*/
let index = 0;
const chunkSize = calculateChunkSize(file.size);
let nextStartByteIndex = 0;
do {
const data = file.slice(nextStartByteIndex, nextStartByteIndex + chunkSize);
nextStartByteIndex += chunkSize;
// Send each packet.
state.remote.send({
type: "chunk",
data: {
index,
data,
},
});
index += 1;
// Delay is good for a nice progress animation.
await delay(1);
} while (nextStartByteIndex < file.size);
//
filePickerButton.innerText = "Send another file";
};
// Listen for file picker events.
filePicker.onchange = async (event) => {
event.preventDefault();
const file = event.target.files[0];
if (!file) return;
await sendFile(file);
};
// Listen for input change events.
textInput.addEventListener("input", async (event) => {
event.preventDefault();
const data = event.target.value?.trim();
state.remote.send({
type: "text",
data,
});
});
/**
* Only allow drag nd drop if remote connection is available.
* and send button is visible (not in the middle of a transfer).
*/
const checkIfDroppable = () => {
return (
state.remote?.peerConnection?.connectionState === "connected" &&
!sendButtonWrap.classList.contains("hidden")
);
};
const handleDragOver = (event) => {
event.preventDefault();
event.stopPropagation();
if (checkIfDroppable()) {
dropArea.setAttribute("data-dragEnabled", "");
}
};
const handleDragOut = (event) => {
event.preventDefault();
event.stopPropagation();
dropArea.removeAttribute("data-dragEnabled");
};
const handleDrop = (event) => {
handleDragOut(event);
if (checkIfDroppable()) {
dropArea.removeAttribute("data-dragEnabled");
const file = event.dataTransfer.files[0];
// Only one file allowed for now.
if (file) {
sendFile(file);
}
}
};
dropArea.addEventListener("drop", handleDrop);
dropArea.addEventListener("dragend", handleDragOut);
dropArea.addEventListener("dragover", handleDragOver);
dropArea.addEventListener("dragenter", handleDragOver);
dropArea.addEventListener("dragleave", handleDragOut);
const setAppearance = (isDarkMode = false) => {
if (isDarkMode) {
themeMeta.content = "#1e1b4b";
html.classList.add("dark");
} else {
themeMeta.content = "#dcfce7";
html.classList.remove("dark");
}
};
if (matchMedia?.(mediaMode).matches) {
setAppearance(true);
}
window.matchMedia(mediaMode).addEventListener("change", (event) => {
setAppearance(event.matches);
});