@trap_stevo/filetide 0.0.2 → 0.0.3

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.
@@ -11,6 +11,7 @@ class FileMessager {
11
11
  constructor(options = {}) {
12
12
  const serverOptions = FileMessagerConfigManager.getServerOptions(options);
13
13
  this.fileNet = new IoTide(serverOptions.port, serverOptions, true, this.onConnect.bind(this), this.onDisconnect.bind(this));
14
+ this.messagerClients = new Map();
14
15
  this.onlineClients = new Map();
15
16
  return;
16
17
  }
@@ -29,6 +30,8 @@ class FileMessager {
29
30
  this.fileNet.on("transfer-progress", this.handleFileTransferProgress.bind(this));
30
31
  this.fileNet.on("transfer-complete", this.handleFileTransferComplete.bind(this));
31
32
  this.fileNet.on("client-to-client-transfer", this.handleClientToClientTransfer.bind(this));
33
+ this.fileNet.on("client-offline", this.handleClientOffline.bind(this));
34
+ this.fileNet.on("client-online", this.handleClientOnline.bind(this));
32
35
  return;
33
36
  }
34
37
 
@@ -39,9 +42,14 @@ class FileMessager {
39
42
  * @param {String} fileName - The name of the file
40
43
  */
41
44
  sendFileToClient(clientId, fileData, fileName, filePath = process.cwd()) {
42
- const client = this.onlineClients.get(clientId);
45
+ if (!this.messagerClients.has(clientId)) {
46
+ console.log(`Client ~ ${clientId} not authorized.`);
47
+ return;
48
+ }
49
+ const connectionID = this.messagerClients.get(clientId);
50
+ const client = this.onlineClients.get(connectionID);
43
51
  if (!client) {
44
- console.log(`Client with ID ${clientId} not found.`);
52
+ console.log(`Client ~ ${clientId} not found.`);
45
53
  return;
46
54
  }
47
55
  client.emit("incoming-file", {
@@ -61,9 +69,24 @@ class FileMessager {
61
69
  const {
62
70
  recipientId,
63
71
  fileName,
64
- fileData
72
+ fileData,
73
+ filePath
65
74
  } = transferData;
66
- this.sendFileToClient(recipientId, fileData, fileName);
75
+ this.sendFileToClient(recipientId, fileData, fileName, filePath);
76
+ return;
77
+ }
78
+ handleClientOnline(clientID, connectionID) {
79
+ if (!this.onlineClients.has(connectionID)) {
80
+ return;
81
+ }
82
+ this.messagerClients.set(clientID, connectionID);
83
+ return;
84
+ }
85
+ handleClientOffline(clientID) {
86
+ if (!this.messagerClients.has(clientID)) {
87
+ return;
88
+ }
89
+ this.messagerClients.delete(clientID);
67
90
  return;
68
91
  }
69
92
  handleFileTransferStart(fileData, emitToChannel) {
@@ -27,24 +27,24 @@ class FileMessagerClient {
27
27
  url: this.clientOptions.url
28
28
  });
29
29
  this.clientTide.joinChannel(socketName, roomName, userID, () => {
30
- console.log(`[FileTide ~ File Messager] ~ Client ${userID} joined room ${roomName} successfully.`);
30
+ console.log(`[FileTide ~ File Messager] ~ Client ~ ${userID} joined room ${roomName} successfully.`);
31
31
  this.clients.set(userID, {
32
32
  roomName
33
33
  });
34
34
  });
35
35
  }
36
- sendFile(recipientId, fileName, fileData, path = process.cwd()) {
36
+ sendFile(recipientId, fileName, fileData, filePath = process.cwd()) {
37
37
  this.clientTide.emitEvent(recipientId, "client-to-client-transfer", {
38
38
  recipientId,
39
39
  fileName,
40
40
  fileData,
41
- path
41
+ filePath
42
42
  });
43
43
  console.log(`[FileTide ~ File Messager] ~ File ${fileName} sent to client ${recipientId}.`);
44
44
  }
45
45
  onFileChunkReceived(userID) {
46
46
  this.clientTide.onEvent(userID, "transfer-progress", data => {
47
- console.log(`[FileTide ~ File Messager] ~ Receiving file chunk for user ${userID}: `, data.fileChunk);
47
+ console.log(`[FileTide ~ File Messager] ~ Receiving file chunk for client ~ ${userID}: `, data.fileChunk);
48
48
  console.log("[FileTide ~ File Messager] ~ Progress: ", data.progress);
49
49
  FileUtilityManager.saveChunk(userID, data.fileChunk, data.path, data.name);
50
50
  });
@@ -44,14 +44,14 @@ class FileTide {
44
44
  */
45
45
  launchMessager(clientOptions = {}, roomName, userID, onLaunch, onIncomingFile) {
46
46
  if (this.clients.has(userID)) {
47
- console.log(`[FileTide] ~ Client for user ${userID} already exists.`);
47
+ console.log(`[FileTide] ~ Client ~ ${userID} already exists.`);
48
48
  return;
49
49
  }
50
50
  const newClient = new FileMessagerClient(clientOptions);
51
51
  newClient.joinRoom(userID, roomName);
52
52
  newClient.handleMultipleClientsTransfer();
53
53
  this.clients.set(userID, newClient);
54
- console.log(`[FileTide] ~ Messager launched successfully for user ${userID}!`);
54
+ console.log(`[FileTide] ~ Messager launched successfully for client ~ ${userID}!`);
55
55
  _assertClassBrand(_FileTide_brand, this, _setupFileEventListeners).call(this, userID, newClient, onIncomingFile);
56
56
  if (onLaunch) {
57
57
  onLaunch(newClient);
@@ -66,12 +66,17 @@ class FileTide {
66
66
  */
67
67
  sendFileToDevice(userID, fileData, filePath, fileName) {
68
68
  if (this.fileNet.onlineClients.has(userID)) {
69
- console.log(`[FileTide] ~ Sending file to user ${userID}...`);
70
- this.fileNet.sendFileToClient(userID, fileData, filePath, fileName);
71
- console.log(`[FileTide] ~ File sent to user ${userID}!`);
69
+ const currentClient = this.clients[userID];
70
+ if (!currentClient) {
71
+ console.log(`[FileTide] ~ Client ~ ${userID} not authorized.`);
72
+ return;
73
+ }
74
+ console.log(`[FileTide] ~ Sending file to client ~ ${userID}...`);
75
+ this.fileNet.sendFileToClient(currentClient.id, fileData, filePath, fileName);
76
+ console.log(`[FileTide] ~ File sent to client ~ ${userID}!`);
72
77
  return;
73
78
  }
74
- console.log(`[FileTide] ~ No active client found for user ${userID}.`);
79
+ console.log(`[FileTide] ~ No active client ~ ${userID} found.`);
75
80
  return;
76
81
  }
77
82
 
@@ -130,17 +135,17 @@ class FileTide {
130
135
  }
131
136
  function _setupFileEventListeners(userID, client, onIncomingFile) {
132
137
  client.clientTide.onEvent(userID, "incoming-file", data => {
133
- console.log(`[Client] Received file: ${data.fileName}`);
138
+ console.log(`[${userID}] Received file: ${data.fileName}`);
134
139
  const saveDir = data.path;
135
140
  if (!fs.existsSync(saveDir)) {
136
141
  fs.mkdirSync(saveDir, {
137
142
  recursive: true
138
143
  });
139
- console.log(`[Client] Created directory: ${saveDir}`);
144
+ console.log(`[${userID}] Created directory: ${saveDir}`);
140
145
  }
141
146
  const savePath = path.join(saveDir, data.fileName);
142
147
  fs.writeFileSync(savePath, Buffer.from(data.fileData));
143
- console.log(`[Client] File saved at: ${savePath}`);
148
+ console.log(`[${userID}] File saved at: ${savePath}`);
144
149
  if (onIncomingFile) {
145
150
  onIncomingFile(data);
146
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trap_stevo/filetide",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Revolutionizing real-time file transfer with seamless, instant communication across any device. Deliver files instantly, regardless of platform, and experience unparalleled speed and control in managing transfers. Elevate your file-sharing capabilities with a tool designed for precision, efficiency, and effortless connectivity.",
5
5
  "main": "dist/cjs/FileTide.js",
6
6
  "scripts": {
@@ -28,7 +28,8 @@
28
28
  "license": "ISC",
29
29
  "dependencies": {
30
30
  "@trap_stevo/iotide": "^0.0.36",
31
- "@trap_stevo/iotide-client": "^0.0.14"
31
+ "@trap_stevo/iotide-client": "^0.0.14",
32
+ "readline": "^1.3.0"
32
33
  },
33
34
  "devDependencies": {
34
35
  "@babel/cli": "^7.24.8",
package/test.txt ADDED
@@ -0,0 +1 @@
1
+ oiyutryuihoj